30
30
import org .elasticsearch .common .util .concurrent .ThreadContext ;
31
31
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
32
32
import org .elasticsearch .persistent .AllocatedPersistentTask ;
33
+ import org .elasticsearch .persistent .DynamicPersistentTasksExecutor ;
33
34
import org .elasticsearch .persistent .PersistentTaskState ;
34
35
import org .elasticsearch .persistent .PersistentTasksCustomMetaData ;
35
- import org .elasticsearch .persistent .PersistentTasksExecutor ;
36
36
import org .elasticsearch .script .ScriptService ;
37
37
import org .elasticsearch .tasks .Task ;
38
38
import org .elasticsearch .tasks .TaskId ;
39
+ import org .elasticsearch .tasks .TaskInfo ;
39
40
import org .elasticsearch .tasks .TaskManager ;
40
41
import org .elasticsearch .threadpool .ThreadPool ;
41
42
@@ -57,8 +58,9 @@ public class ReindexTask extends AllocatedPersistentTask {
57
58
private volatile BulkByScrollTask .Status transientStatus ;
58
59
private volatile String description ;
59
60
private volatile boolean assignmentConflictDetected ;
61
+ private volatile Float requestsPerSecond ;
60
62
61
- public static class ReindexPersistentTasksExecutor extends PersistentTasksExecutor < ReindexTaskParams > {
63
+ public static class ReindexPersistentTasksExecutor extends DynamicPersistentTasksExecutor < ReindexTask , ReindexTaskParams > {
62
64
63
65
private final ClusterService clusterService ;
64
66
private final Client client ;
@@ -92,6 +94,11 @@ protected AllocatedPersistentTask createTask(long id, String type, String action
92
94
Reindexer reindexer = new Reindexer (clusterService , client , threadPool , scriptService , reindexSslConfig );
93
95
return new ReindexTask (id , type , action , parentTaskId , headers , clusterService , xContentRegistry , client , reindexer );
94
96
}
97
+
98
+ @ Override
99
+ protected void paramsUpdated (ReindexTask task , ReindexTaskParams newParams ) {
100
+ task .requestsPerSecondUpdated (newParams .getRequestsPerSecond ());
101
+ }
95
102
}
96
103
97
104
private ReindexTask (long id , String type , String action , TaskId parentTask , Map <String , String > headers ,
@@ -138,6 +145,29 @@ BulkByScrollTask getChildTask() {
138
145
return childTask ;
139
146
}
140
147
148
+ private void requestsPerSecondUpdated (float requestsPerSecond ) {
149
+ this .requestsPerSecond = requestsPerSecond ;
150
+ if (childTask .isLeader () || childTask .isWorker ()) {
151
+ // reindex is running
152
+ rethrottle (requestsPerSecond );
153
+ }
154
+ }
155
+
156
+ private void rethrottle (float requestsPerSecond ) {
157
+ TransportRethrottleAction .rethrottle (logger , client .getLocalNodeId (), client , childTask , requestsPerSecond ,
158
+ new ActionListener <>() {
159
+ @ Override
160
+ public void onResponse (TaskInfo taskInfo ) {
161
+ }
162
+
163
+ @ Override
164
+ public void onFailure (Exception e ) {
165
+ assert false ;
166
+ logger .error ("Unable to rethrottle [{}]" , getPersistentTaskId ());
167
+ }
168
+ });
169
+ }
170
+
141
171
private void execute (ReindexTaskParams reindexTaskParams ) {
142
172
long allocationId = getAllocationId ();
143
173
@@ -160,9 +190,13 @@ public void onFailure(Exception e) {
160
190
public void onResponse (ReindexTaskStateDoc stateDoc ) {
161
191
ReindexRequest reindexRequest = stateDoc .getReindexRequest ();
162
192
description = reindexRequest .getDescription ();
163
- reindexer .initTask (childTask , reindexRequest , new ActionListener <>() {
193
+ reindexer .initTask (childTask , reindexRequest , reindexTaskParams . getRequestsPerSecond (), new ActionListener <>() {
164
194
@ Override
165
195
public void onResponse (Void aVoid ) {
196
+ if (requestsPerSecond != null ) {
197
+ // updated while starting
198
+ rethrottle (requestsPerSecond );
199
+ }
166
200
// TODO: need to store status in state so we can continue from it.
167
201
transientStatus = childTask .getStatus ();
168
202
performReindex (reindexTaskParams , stateDoc , taskUpdater );
0 commit comments