1111import org .elasticsearch .action .ActionListener ;
1212import org .elasticsearch .action .DocWriteRequest ;
1313import org .elasticsearch .action .DocWriteResponse ;
14+ import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
15+ import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
1416import org .elasticsearch .action .bulk .BulkItemResponse ;
1517import org .elasticsearch .action .bulk .BulkRequestBuilder ;
1618import org .elasticsearch .action .bulk .BulkResponse ;
2123import org .elasticsearch .client .Client ;
2224import org .elasticsearch .cluster .ClusterState ;
2325import org .elasticsearch .cluster .ClusterStateUpdateTask ;
26+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
2427import org .elasticsearch .cluster .metadata .MetaData ;
2528import org .elasticsearch .cluster .service .ClusterService ;
2629import org .elasticsearch .common .settings .Settings ;
2932import org .elasticsearch .common .xcontent .ToXContentObject ;
3033import org .elasticsearch .common .xcontent .XContentBuilder ;
3134import org .elasticsearch .common .xcontent .XContentFactory ;
35+ import org .elasticsearch .index .IndexSettings ;
3236import org .elasticsearch .persistent .PersistentTasksCustomMetaData ;
3337import org .elasticsearch .xpack .core .ml .MlMetadata ;
3438import org .elasticsearch .xpack .core .ml .MlTasks ;
@@ -126,19 +130,11 @@ public MlConfigMigrator(Settings settings, Client client, ClusterService cluster
126130 * @param listener The success listener
127131 */
128132 public void migrateConfigsWithoutTasks (ClusterState clusterState , ActionListener <Boolean > listener ) {
129-
130- if (migrationEligibilityCheck .canStartMigration (clusterState ) == false ) {
131- listener .onResponse (false );
132- return ;
133- }
134-
135133 if (migrationInProgress .compareAndSet (false , true ) == false ) {
136134 listener .onResponse (Boolean .FALSE );
137135 return ;
138136 }
139137
140- logger .debug ("migrating ml configurations" );
141-
142138 ActionListener <Boolean > unMarkMigrationInProgress = ActionListener .wrap (
143139 response -> {
144140 migrationInProgress .set (false );
@@ -150,19 +146,34 @@ public void migrateConfigsWithoutTasks(ClusterState clusterState, ActionListener
150146 }
151147 );
152148
149+ List <JobsAndDatafeeds > batches = splitInBatches (clusterState );
150+ if (batches .isEmpty ()) {
151+ unMarkMigrationInProgress .onResponse (Boolean .FALSE );
152+ return ;
153+ }
154+
155+ if (clusterState .metaData ().hasIndex (AnomalyDetectorsIndex .configIndexName ()) == false ) {
156+ createConfigIndex (ActionListener .wrap (
157+ response -> {
158+ unMarkMigrationInProgress .onResponse (Boolean .FALSE );
159+ },
160+ unMarkMigrationInProgress ::onFailure
161+ ));
162+ return ;
163+ }
164+
165+ if (migrationEligibilityCheck .canStartMigration (clusterState ) == false ) {
166+ unMarkMigrationInProgress .onResponse (Boolean .FALSE );
167+ return ;
168+ }
169+
153170 snapshotMlMeta (MlMetadata .getMlMetadata (clusterState ), ActionListener .wrap (
154- response -> {
155- // We have successfully snapshotted the ML configs so we don't need to try again
156- tookConfigSnapshot .set (true );
157-
158- List <JobsAndDatafeeds > batches = splitInBatches (clusterState );
159- if (batches .isEmpty ()) {
160- unMarkMigrationInProgress .onResponse (Boolean .FALSE );
161- return ;
162- }
163- migrateBatches (batches , unMarkMigrationInProgress );
164- },
165- unMarkMigrationInProgress ::onFailure
171+ response -> {
172+ // We have successfully snapshotted the ML configs so we don't need to try again
173+ tookConfigSnapshot .set (true );
174+ migrateBatches (batches , unMarkMigrationInProgress );
175+ },
176+ unMarkMigrationInProgress ::onFailure
166177 ));
167178 }
168179
@@ -296,13 +307,15 @@ static RemovalResult removeJobsAndDatafeeds(List<String> jobsToRemove, List<Stri
296307 private void addJobIndexRequests (Collection <Job > jobs , BulkRequestBuilder bulkRequestBuilder ) {
297308 ToXContent .Params params = new ToXContent .MapParams (JobConfigProvider .TO_XCONTENT_PARAMS );
298309 for (Job job : jobs ) {
310+ logger .debug ("adding job to migrate: " + job .getId ());
299311 bulkRequestBuilder .add (indexRequest (job , Job .documentId (job .getId ()), params ));
300312 }
301313 }
302314
303315 private void addDatafeedIndexRequests (Collection <DatafeedConfig > datafeedConfigs , BulkRequestBuilder bulkRequestBuilder ) {
304316 ToXContent .Params params = new ToXContent .MapParams (DatafeedConfigProvider .TO_XCONTENT_PARAMS );
305317 for (DatafeedConfig datafeedConfig : datafeedConfigs ) {
318+ logger .debug ("adding datafeed to migrate: " + datafeedConfig .getId ());
306319 bulkRequestBuilder .add (indexRequest (datafeedConfig , DatafeedConfig .documentId (datafeedConfig .getId ()), params ));
307320 }
308321 }
@@ -318,7 +331,6 @@ private IndexRequest indexRequest(ToXContentObject source, String documentId, To
318331 return indexRequest ;
319332 }
320333
321-
322334 // public for testing
323335 public void snapshotMlMeta (MlMetadata mlMetadata , ActionListener <Boolean > listener ) {
324336
@@ -361,6 +373,30 @@ public void snapshotMlMeta(MlMetadata mlMetadata, ActionListener<Boolean> listen
361373 );
362374 }
363375
376+ private void createConfigIndex (ActionListener <Boolean > listener ) {
377+ logger .info ("creating the .ml-config index" );
378+ CreateIndexRequest createIndexRequest = new CreateIndexRequest (AnomalyDetectorsIndex .configIndexName ());
379+ try
380+ {
381+ createIndexRequest .settings (
382+ Settings .builder ()
383+ .put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
384+ .put (IndexMetaData .SETTING_AUTO_EXPAND_REPLICAS , "0-1" )
385+ .put (IndexSettings .MAX_RESULT_WINDOW_SETTING .getKey (), AnomalyDetectorsIndex .CONFIG_INDEX_MAX_RESULTS_WINDOW )
386+ );
387+ createIndexRequest .mapping (ElasticsearchMappings .DOC_TYPE , ElasticsearchMappings .configMapping ());
388+ } catch (Exception e ) {
389+ logger .error ("error writing the .ml-config mappings" , e );
390+ listener .onFailure (e );
391+ return ;
392+ }
393+
394+ executeAsyncWithOrigin (client .threadPool ().getThreadContext (), ML_ORIGIN , createIndexRequest ,
395+ ActionListener .<CreateIndexResponse >wrap (
396+ r -> listener .onResponse (r .isAcknowledged ()),
397+ listener ::onFailure
398+ ), client .admin ().indices ()::create );
399+ }
364400
365401 public static Job updateJobForMigration (Job job ) {
366402 Job .Builder builder = new Job .Builder (job );
0 commit comments