@@ -112,6 +112,7 @@ public abstract class AbstractAsyncBulkByScrollAction<Request extends AbstractBu
112
112
* {@link RequestWrapper} completely.
113
113
*/
114
114
private final BiFunction <RequestWrapper <?>, ScrollableHitSource .Hit , RequestWrapper <?>> scriptApplier ;
115
+ private int lastBatchSize ;
115
116
116
117
public AbstractAsyncBulkByScrollAction (BulkByScrollTask task , boolean needsSourceDocumentVersions ,
117
118
boolean needsSourceDocumentSeqNoAndPrimaryTerm , Logger logger , ParentTaskAssigningClient client ,
@@ -211,7 +212,8 @@ private BulkRequest buildBulk(Iterable<? extends ScrollableHitSource.Hit> docs)
211
212
}
212
213
213
214
protected ScrollableHitSource buildScrollableResultSource (BackoffPolicy backoffPolicy ) {
214
- return new ClientScrollableHitSource (logger , backoffPolicy , threadPool , worker ::countSearchRetry , this ::finishHim , client ,
215
+ return new ClientScrollableHitSource (logger , backoffPolicy , threadPool , worker ::countSearchRetry ,
216
+ this ::onScrollResponse , this ::finishHim , client ,
215
217
mainRequest .getSearchRequest ());
216
218
}
217
219
@@ -235,19 +237,26 @@ public void start() {
235
237
}
236
238
try {
237
239
startTime .set (System .nanoTime ());
238
- scrollSource .start (response -> onScrollResponse ( timeValueNanos ( System . nanoTime ()), 0 , response ) );
240
+ scrollSource .start ();
239
241
} catch (Exception e ) {
240
242
finishHim (e );
241
243
}
242
244
}
243
245
246
+ void onScrollResponse (ScrollableHitSource .AsyncResponse asyncResponse ) {
247
+ // lastBatchStartTime is essentially unused (see WorkerBulkByScrollTaskState.throttleWaitTime. Leaving it for now, since it seems
248
+ // like a bug?
249
+ onScrollResponse (new TimeValue (System .nanoTime ()), this .lastBatchSize , asyncResponse );
250
+ }
251
+
244
252
/**
245
253
* Process a scroll response.
246
254
* @param lastBatchStartTime the time when the last batch started. Used to calculate the throttling delay.
247
255
* @param lastBatchSize the size of the last batch. Used to calculate the throttling delay.
248
- * @param response the scroll response to process
256
+ * @param asyncResponse the response to process from ScrollableHitSource
249
257
*/
250
- void onScrollResponse (TimeValue lastBatchStartTime , int lastBatchSize , ScrollableHitSource .Response response ) {
258
+ void onScrollResponse (TimeValue lastBatchStartTime , int lastBatchSize , ScrollableHitSource .AsyncResponse asyncResponse ) {
259
+ ScrollableHitSource .Response response = asyncResponse .response ();
251
260
logger .debug ("[{}]: got scroll response with [{}] hits" , task .getId (), response .getHits ().size ());
252
261
if (task .isCancelled ()) {
253
262
logger .debug ("[{}]: finishing early because the task was cancelled" , task .getId ());
@@ -274,7 +283,7 @@ protected void doRun() throws Exception {
274
283
* It is important that the batch start time be calculated from here, scroll response to scroll response. That way the time
275
284
* waiting on the scroll doesn't count against this batch in the throttle.
276
285
*/
277
- prepareBulkRequest (timeValueNanos (System .nanoTime ()), response );
286
+ prepareBulkRequest (timeValueNanos (System .nanoTime ()), asyncResponse );
278
287
}
279
288
280
289
@ Override
@@ -291,7 +300,8 @@ public void onFailure(Exception e) {
291
300
* delay has been slept. Uses the generic thread pool because reindex is rare enough not to need its own thread pool and because the
292
301
* thread may be blocked by the user script.
293
302
*/
294
- void prepareBulkRequest (TimeValue thisBatchStartTime , ScrollableHitSource .Response response ) {
303
+ void prepareBulkRequest (TimeValue thisBatchStartTime , ScrollableHitSource .AsyncResponse asyncResponse ) {
304
+ ScrollableHitSource .Response response = asyncResponse .response ();
295
305
logger .debug ("[{}]: preparing bulk request" , task .getId ());
296
306
if (task .isCancelled ()) {
297
307
logger .debug ("[{}]: finishing early because the task was cancelled" , task .getId ());
@@ -316,18 +326,18 @@ void prepareBulkRequest(TimeValue thisBatchStartTime, ScrollableHitSource.Respon
316
326
/*
317
327
* If we noop-ed the entire batch then just skip to the next batch or the BulkRequest would fail validation.
318
328
*/
319
- startNextScroll (thisBatchStartTime , timeValueNanos ( System . nanoTime ()) , 0 );
329
+ notifyDone (thisBatchStartTime , asyncResponse , 0 );
320
330
return ;
321
331
}
322
332
request .timeout (mainRequest .getTimeout ());
323
333
request .waitForActiveShards (mainRequest .getWaitForActiveShards ());
324
- sendBulkRequest (thisBatchStartTime , request );
334
+ sendBulkRequest (request , () -> notifyDone ( thisBatchStartTime , asyncResponse , request . requests (). size ()) );
325
335
}
326
336
327
337
/**
328
338
* Send a bulk request, handling retries.
329
339
*/
330
- void sendBulkRequest (TimeValue thisBatchStartTime , BulkRequest request ) {
340
+ void sendBulkRequest (BulkRequest request , Runnable onSuccess ) {
331
341
if (logger .isDebugEnabled ()) {
332
342
logger .debug ("[{}]: sending [{}] entry, [{}] bulk request" , task .getId (), request .requests ().size (),
333
343
new ByteSizeValue (request .estimatedSizeInBytes ()));
@@ -340,7 +350,7 @@ void sendBulkRequest(TimeValue thisBatchStartTime, BulkRequest request) {
340
350
bulkRetry .withBackoff (client ::bulk , request , new ActionListener <BulkResponse >() {
341
351
@ Override
342
352
public void onResponse (BulkResponse response ) {
343
- onBulkResponse (thisBatchStartTime , response );
353
+ onBulkResponse (response , onSuccess );
344
354
}
345
355
346
356
@ Override
@@ -353,7 +363,7 @@ public void onFailure(Exception e) {
353
363
/**
354
364
* Processes bulk responses, accounting for failures.
355
365
*/
356
- void onBulkResponse (TimeValue thisBatchStartTime , BulkResponse response ) {
366
+ void onBulkResponse (BulkResponse response , Runnable onSuccess ) {
357
367
try {
358
368
List <Failure > failures = new ArrayList <>();
359
369
Set <String > destinationIndicesThisBatch = new HashSet <>();
@@ -401,28 +411,20 @@ void onBulkResponse(TimeValue thisBatchStartTime, BulkResponse response) {
401
411
return ;
402
412
}
403
413
404
- startNextScroll ( thisBatchStartTime , timeValueNanos ( System . nanoTime ()), response . getItems (). length );
414
+ onSuccess . run ( );
405
415
} catch (Exception t ) {
406
416
finishHim (t );
407
417
}
408
418
}
409
419
410
- /**
411
- * Start the next scroll request.
412
- *
413
- * @param lastBatchSize the number of requests sent in the last batch. This is used to calculate the throttling values which are applied
414
- * when the scroll returns
415
- */
416
- void startNextScroll (TimeValue lastBatchStartTime , TimeValue now , int lastBatchSize ) {
420
+ void notifyDone (TimeValue thisBatchStartTime , ScrollableHitSource .AsyncResponse asyncResponse , int batchSize ) {
417
421
if (task .isCancelled ()) {
418
422
logger .debug ("[{}]: finishing early because the task was cancelled" , task .getId ());
419
423
finishHim (null );
420
424
return ;
421
425
}
422
- TimeValue extraKeepAlive = worker .throttleWaitTime (lastBatchStartTime , now , lastBatchSize );
423
- scrollSource .startNextScroll (extraKeepAlive , response -> {
424
- onScrollResponse (lastBatchStartTime , lastBatchSize , response );
425
- });
426
+ this .lastBatchSize = batchSize ;
427
+ asyncResponse .done (worker .throttleWaitTime (thisBatchStartTime , timeValueNanos (System .nanoTime ()), batchSize ));
426
428
}
427
429
428
430
private void recordFailure (Failure failure , List <Failure > failures ) {
0 commit comments