11
11
import org .elasticsearch .action .ActionListener ;
12
12
import org .elasticsearch .action .DocWriteRequest ;
13
13
import org .elasticsearch .action .DocWriteResponse ;
14
+ import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
15
+ import org .elasticsearch .action .admin .indices .create .CreateIndexResponse ;
14
16
import org .elasticsearch .action .bulk .BulkItemResponse ;
15
17
import org .elasticsearch .action .bulk .BulkRequestBuilder ;
16
18
import org .elasticsearch .action .bulk .BulkResponse ;
21
23
import org .elasticsearch .client .Client ;
22
24
import org .elasticsearch .cluster .ClusterState ;
23
25
import org .elasticsearch .cluster .ClusterStateUpdateTask ;
26
+ import org .elasticsearch .cluster .metadata .IndexMetaData ;
24
27
import org .elasticsearch .cluster .metadata .MetaData ;
25
28
import org .elasticsearch .cluster .service .ClusterService ;
26
29
import org .elasticsearch .common .settings .Settings ;
29
32
import org .elasticsearch .common .xcontent .ToXContentObject ;
30
33
import org .elasticsearch .common .xcontent .XContentBuilder ;
31
34
import org .elasticsearch .common .xcontent .XContentFactory ;
35
+ import org .elasticsearch .index .IndexSettings ;
32
36
import org .elasticsearch .persistent .PersistentTasksCustomMetaData ;
33
37
import org .elasticsearch .xpack .core .ml .MlMetadata ;
34
38
import org .elasticsearch .xpack .core .ml .MlTasks ;
@@ -126,19 +130,11 @@ public MlConfigMigrator(Settings settings, Client client, ClusterService cluster
126
130
* @param listener The success listener
127
131
*/
128
132
public void migrateConfigsWithoutTasks (ClusterState clusterState , ActionListener <Boolean > listener ) {
129
-
130
- if (migrationEligibilityCheck .canStartMigration (clusterState ) == false ) {
131
- listener .onResponse (false );
132
- return ;
133
- }
134
-
135
133
if (migrationInProgress .compareAndSet (false , true ) == false ) {
136
134
listener .onResponse (Boolean .FALSE );
137
135
return ;
138
136
}
139
137
140
- logger .debug ("migrating ml configurations" );
141
-
142
138
ActionListener <Boolean > unMarkMigrationInProgress = ActionListener .wrap (
143
139
response -> {
144
140
migrationInProgress .set (false );
@@ -150,19 +146,34 @@ public void migrateConfigsWithoutTasks(ClusterState clusterState, ActionListener
150
146
}
151
147
);
152
148
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
+
153
170
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
166
177
));
167
178
}
168
179
@@ -296,13 +307,15 @@ static RemovalResult removeJobsAndDatafeeds(List<String> jobsToRemove, List<Stri
296
307
private void addJobIndexRequests (Collection <Job > jobs , BulkRequestBuilder bulkRequestBuilder ) {
297
308
ToXContent .Params params = new ToXContent .MapParams (JobConfigProvider .TO_XCONTENT_PARAMS );
298
309
for (Job job : jobs ) {
310
+ logger .debug ("adding job to migrate: " + job .getId ());
299
311
bulkRequestBuilder .add (indexRequest (job , Job .documentId (job .getId ()), params ));
300
312
}
301
313
}
302
314
303
315
private void addDatafeedIndexRequests (Collection <DatafeedConfig > datafeedConfigs , BulkRequestBuilder bulkRequestBuilder ) {
304
316
ToXContent .Params params = new ToXContent .MapParams (DatafeedConfigProvider .TO_XCONTENT_PARAMS );
305
317
for (DatafeedConfig datafeedConfig : datafeedConfigs ) {
318
+ logger .debug ("adding datafeed to migrate: " + datafeedConfig .getId ());
306
319
bulkRequestBuilder .add (indexRequest (datafeedConfig , DatafeedConfig .documentId (datafeedConfig .getId ()), params ));
307
320
}
308
321
}
@@ -318,7 +331,6 @@ private IndexRequest indexRequest(ToXContentObject source, String documentId, To
318
331
return indexRequest ;
319
332
}
320
333
321
-
322
334
// public for testing
323
335
public void snapshotMlMeta (MlMetadata mlMetadata , ActionListener <Boolean > listener ) {
324
336
@@ -361,6 +373,30 @@ public void snapshotMlMeta(MlMetadata mlMetadata, ActionListener<Boolean> listen
361
373
);
362
374
}
363
375
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
+ }
364
400
365
401
public static Job updateJobForMigration (Job job ) {
366
402
Job .Builder builder = new Job .Builder (job );
0 commit comments