19
19
import org .elasticsearch .action .admin .indices .refresh .RefreshAction ;
20
20
import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
21
21
import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
22
+ import org .elasticsearch .action .bulk .BulkItemResponse ;
22
23
import org .elasticsearch .action .support .ContextPreservingActionListener ;
23
24
import org .elasticsearch .client .Client ;
24
25
import org .elasticsearch .client .ParentTaskAssigningClient ;
@@ -214,6 +215,7 @@ private void reindexDataframeAndStartAnalysis(DataFrameAnalyticsTask task, DataF
214
215
// Reindexing is complete; start analytics
215
216
ActionListener <BulkByScrollResponse > reindexCompletedListener = ActionListener .wrap (
216
217
reindexResponse -> {
218
+
217
219
// If the reindex task is canceled, this listener is called.
218
220
// Consequently, we should not signal reindex completion.
219
221
if (task .isStopping ()) {
@@ -222,7 +224,18 @@ private void reindexDataframeAndStartAnalysis(DataFrameAnalyticsTask task, DataF
222
224
task .markAsCompleted ();
223
225
return ;
224
226
}
227
+
225
228
task .setReindexingTaskId (null );
229
+
230
+ Exception reindexError = getReindexError (task .getParams ().getId (), reindexResponse );
231
+ if (reindexError != null ) {
232
+ task .markAsFailed (reindexError );
233
+ return ;
234
+ }
235
+
236
+ LOGGER .debug ("[{}] Reindex completed; created [{}]; retries [{}]" , task .getParams ().getId (),
237
+ reindexResponse .getCreated (), reindexResponse .getBulkRetries ());
238
+
226
239
auditor .info (
227
240
config .getId (),
228
241
Messages .getMessage (Messages .DATA_FRAME_ANALYTICS_AUDIT_FINISHED_REINDEXING , config .getDest ().getIndex (),
@@ -251,6 +264,7 @@ private void reindexDataframeAndStartAnalysis(DataFrameAnalyticsTask task, DataF
251
264
reindexRequest .setDestIndex (config .getDest ().getIndex ());
252
265
reindexRequest .setScript (new Script ("ctx._source." + DestinationIndex .ID_COPY + " = ctx._id" ));
253
266
reindexRequest .setParentTask (task .getParentTaskId ());
267
+ reindexRequest .getSearchRequest ().allowPartialSearchResults (false );
254
268
255
269
final ThreadContext threadContext = parentTaskClient .threadPool ().getThreadContext ();
256
270
final Supplier <ThreadContext .StoredContext > supplier = threadContext .newRestorableContext (false );
@@ -295,6 +309,26 @@ private void reindexDataframeAndStartAnalysis(DataFrameAnalyticsTask task, DataF
295
309
new GetIndexRequest ().indices (config .getDest ().getIndex ()), destIndexListener );
296
310
}
297
311
312
+ private static Exception getReindexError (String jobId , BulkByScrollResponse reindexResponse ) {
313
+ if (reindexResponse .getBulkFailures ().isEmpty () == false ) {
314
+ LOGGER .error ("[{}] reindexing encountered {} failures" , jobId ,
315
+ reindexResponse .getBulkFailures ().size ());
316
+ for (BulkItemResponse .Failure failure : reindexResponse .getBulkFailures ()) {
317
+ LOGGER .error ("[{}] reindexing failure: {}" , jobId , failure );
318
+ }
319
+ return ExceptionsHelper .serverError ("reindexing encountered " + reindexResponse .getBulkFailures ().size () + " failures" );
320
+ }
321
+ if (reindexResponse .getReasonCancelled () != null ) {
322
+ LOGGER .error ("[{}] reindex task got cancelled with reason [{}]" , jobId , reindexResponse .getReasonCancelled ());
323
+ return ExceptionsHelper .serverError ("reindex task got cancelled with reason [" + reindexResponse .getReasonCancelled () + "]" );
324
+ }
325
+ if (reindexResponse .isTimedOut ()) {
326
+ LOGGER .error ("[{}] reindex task timed out after [{}]" , jobId , reindexResponse .getTook ().getStringRep ());
327
+ return ExceptionsHelper .serverError ("reindex task timed out after [" + reindexResponse .getTook ().getStringRep () + "]" );
328
+ }
329
+ return null ;
330
+ }
331
+
298
332
private static boolean isTaskCancelledException (Exception error ) {
299
333
return ExceptionsHelper .unwrapCause (error ) instanceof TaskCancelledException
300
334
|| ExceptionsHelper .unwrapCause (error .getCause ()) instanceof TaskCancelledException ;
0 commit comments