Skip to content

Commit ad20d6b

Browse files
author
David Roberts
authored
[ML] Followup to annotations index creation (#36824)
Fixes two minor problems reported after merge of #36731: 1. Name the creation method to make clear it only creates if necessary 2. Avoid multiple simultaneous in-flight creation requests
1 parent 8f141b8 commit ad20d6b

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/annotations/AnnotationIndex.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ public class AnnotationIndex {
3838
public static final String INDEX_NAME = ".ml-annotations-6";
3939

4040
/**
41-
* Create the .ml-annotations index with correct mappings.
42-
* This index is read and written by the UI results views,
43-
* so needs to exist when there might be ML results to view.
41+
* Create the .ml-annotations index with correct mappings if it does not already
42+
* exist. This index is read and written by the UI results views, so needs to
43+
* exist when there might be ML results to view.
4444
*/
45-
public static void createAnnotationsIndex(Settings settings, Client client, ClusterState state,
46-
final ActionListener<Boolean> finalListener) {
45+
public static void createAnnotationsIndexIfNecessary(Settings settings, Client client, ClusterState state,
46+
final ActionListener<Boolean> finalListener) {
4747

4848
final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> {
4949
final IndicesAliasesRequest request = client.admin().indices().prepareAliases()

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlInitializationService.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.elasticsearch.threadpool.ThreadPool;
2020
import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex;
2121

22+
import java.util.concurrent.atomic.AtomicBoolean;
23+
2224
class MlInitializationService implements LocalNodeMasterListener, ClusterStateListener {
2325

2426
private static final Logger logger = LogManager.getLogger(MlInitializationService.class);
@@ -27,6 +29,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
2729
private final ThreadPool threadPool;
2830
private final ClusterService clusterService;
2931
private final Client client;
32+
private final AtomicBoolean isIndexCreationInProgress = new AtomicBoolean(false);
3033

3134
private volatile MlDailyMaintenanceService mlDailyMaintenanceService;
3235

@@ -55,14 +58,20 @@ public void clusterChanged(ClusterChangedEvent event) {
5558
return;
5659
}
5760

58-
if (event.localNodeMaster()) {
59-
AnnotationIndex.createAnnotationsIndex(settings, client, event.state(), ActionListener.wrap(
61+
// The atomic flag prevents multiple simultaneous attempts to create the
62+
// index if there is a flurry of cluster state updates in quick succession
63+
if (event.localNodeMaster() && isIndexCreationInProgress.compareAndSet(false, true)) {
64+
AnnotationIndex.createAnnotationsIndexIfNecessary(settings, client, event.state(), ActionListener.wrap(
6065
r -> {
66+
isIndexCreationInProgress.set(false);
6167
if (r) {
6268
logger.info("Created ML annotations index and aliases");
6369
}
6470
},
65-
e -> logger.error("Error creating ML annotations index or aliases", e)));
71+
e -> {
72+
isIndexCreationInProgress.set(false);
73+
logger.error("Error creating ML annotations index or aliases", e);
74+
}));
6675
}
6776
}
6877

0 commit comments

Comments
 (0)