1515import org .elasticsearch .action .admin .indices .get .GetIndexAction ;
1616import org .elasticsearch .action .admin .indices .get .GetIndexRequest ;
1717import org .elasticsearch .action .admin .indices .get .GetIndexResponse ;
18+ import org .elasticsearch .action .admin .indices .refresh .RefreshAction ;
19+ import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
20+ import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
1821import org .elasticsearch .action .support .ContextPreservingActionListener ;
1922import org .elasticsearch .client .node .NodeClient ;
2023import org .elasticsearch .cluster .ClusterState ;
4245import java .util .function .Supplier ;
4346
4447import static org .elasticsearch .xpack .core .ClientHelper .ML_ORIGIN ;
48+ import static org .elasticsearch .xpack .core .ClientHelper .executeWithHeadersAsync ;
4549
4650public class DataFrameAnalyticsManager {
4751
@@ -225,9 +229,6 @@ private void reindexDataframeAndStartAnalysis(DataFrameAnalyticsTask task, DataF
225229 }
226230
227231 private void startAnalytics (DataFrameAnalyticsTask task , DataFrameAnalyticsConfig config ) {
228- // Ensure we mark reindexing is finished for the case we are recovering a task that had finished reindexing
229- task .setReindexingFinished ();
230-
231232 // Update state to ANALYZING and start process
232233 ActionListener <DataFrameDataExtractorFactory > dataExtractorFactoryListener = ActionListener .wrap (
233234 dataExtractorFactory -> {
@@ -247,10 +248,23 @@ private void startAnalytics(DataFrameAnalyticsTask task, DataFrameAnalyticsConfi
247248 error -> task .updateState (DataFrameAnalyticsState .FAILED , error .getMessage ())
248249 );
249250
250- // TODO This could fail with errors. In that case we get stuck with the copied index.
251- // We could delete the index in case of failure or we could try building the factory before reindexing
252- // to catch the error early on.
253- DataFrameDataExtractorFactory .createForDestinationIndex (client , config , dataExtractorFactoryListener );
251+ ActionListener <RefreshResponse > refreshListener = ActionListener .wrap (
252+ refreshResponse -> {
253+ // Ensure we mark reindexing is finished for the case we are recovering a task that had finished reindexing
254+ task .setReindexingFinished ();
255+
256+ // TODO This could fail with errors. In that case we get stuck with the copied index.
257+ // We could delete the index in case of failure or we could try building the factory before reindexing
258+ // to catch the error early on.
259+ DataFrameDataExtractorFactory .createForDestinationIndex (client , config , dataExtractorFactoryListener );
260+ },
261+ dataExtractorFactoryListener ::onFailure
262+ );
263+
264+ // First we need to refresh the dest index to ensure data is searchable in case the job
265+ // was stopped after reindexing was complete but before the index was refreshed.
266+ executeWithHeadersAsync (config .getHeaders (), ML_ORIGIN , client , RefreshAction .INSTANCE ,
267+ new RefreshRequest (config .getDest ().getIndex ()), refreshListener );
254268 }
255269
256270 public void stop (DataFrameAnalyticsTask task ) {
0 commit comments