From 440d005b1a7a813a2be220fa0b1d43451a30435e Mon Sep 17 00:00:00 2001 From: David Roberts Date: Fri, 3 Mar 2017 10:59:24 +0000 Subject: [PATCH] [ML] Prevent repeated cluster state changes when creating index templates (elastic/x-pack-elasticsearch#687) Switched the order of the local atomic flag check and cluster state check, based on the observation that we used to sometimes get a storm of cluster state updates on initial startup, due to a race between checking for the presence of an index template and starting to create one. Original commit: elastic/x-pack-elasticsearch@7ae83648ce75707291a21e4eae246af71e4ac788 --- .../ml/MachineLearningTemplateRegistry.java | 25 ++++++++++++------- .../MachineLearningTemplateRegistryTests.java | 4 +-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java index 17d646a6a19..4153455539a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistry.java @@ -40,7 +40,6 @@ import org.elasticsearch.xpack.ml.notifications.Auditor; import java.io.IOException; import java.util.Collections; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BiConsumer; @@ -114,8 +113,8 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen } private void addMlNotificationsIndexTemplate(MetaData metaData) { - if (templateIsPresentAndUpToDate(Auditor.NOTIFICATIONS_INDEX, metaData) == false) { - if (putMlNotificationsIndexTemplateCheck.compareAndSet(false, true)) { + if (putMlNotificationsIndexTemplateCheck.compareAndSet(false, true)) { + if (templateIsPresentAndUpToDate(Auditor.NOTIFICATIONS_INDEX, metaData) == false) { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { putNotificationMessageIndexTemplate((result, error) -> { putMlNotificationsIndexTemplateCheck.set(false); @@ -127,13 +126,15 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen } }); }); + } else { + putMlNotificationsIndexTemplateCheck.set(false); } } } private void addMlMetaIndexTemplate(MetaData metaData) { - if (templateIsPresentAndUpToDate(AnomalyDetectorsIndex.ML_META_INDEX, metaData) == false) { - if (putMlMetaIndexTemplateCheck.compareAndSet(false, true)) { + if (putMlMetaIndexTemplateCheck.compareAndSet(false, true)) { + if (templateIsPresentAndUpToDate(AnomalyDetectorsIndex.ML_META_INDEX, metaData) == false) { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { putMetaIndexTemplate((result, error) -> { putMlMetaIndexTemplateCheck.set(false); @@ -145,14 +146,16 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen } }); }); + } else { + putMlMetaIndexTemplateCheck.set(false); } } } private void addStateIndexTemplate(MetaData metaData) { String stateIndexName = AnomalyDetectorsIndex.jobStateIndexName(); - if (templateIsPresentAndUpToDate(stateIndexName, metaData) == false) { - if (putStateIndexTemplateCheck.compareAndSet(false, true)) { + if (putStateIndexTemplateCheck.compareAndSet(false, true)) { + if (templateIsPresentAndUpToDate(stateIndexName, metaData) == false) { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { putJobStateIndexTemplate((result, error) -> { putStateIndexTemplateCheck.set(false); @@ -163,13 +166,15 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen } }); }); + } else { + putStateIndexTemplateCheck.set(false); } } } private void addResultsIndexTemplate(MetaData metaData) { - if (templateIsPresentAndUpToDate(AnomalyDetectorsIndex.jobResultsIndexPrefix(), metaData) == false) { - if (putResultsIndexTemplateCheck.compareAndSet(false, true)) { + if (putResultsIndexTemplateCheck.compareAndSet(false, true)) { + if (templateIsPresentAndUpToDate(AnomalyDetectorsIndex.jobResultsIndexPrefix(), metaData) == false) { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> { putJobResultsIndexTemplate((result, error) -> { putResultsIndexTemplateCheck.set(false); @@ -182,6 +187,8 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen } }); }); + } else { + putResultsIndexTemplateCheck.set(false); } } } diff --git a/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java index 9255073d3f6..e61aee8f232 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/ml/MachineLearningTemplateRegistryTests.java @@ -148,7 +148,7 @@ public class MachineLearningTemplateRegistryTests extends ESTestCase { verify(threadPool, times(0)).executor(anyString()); assertFalse(templateRegistry.putMlNotificationsIndexTemplateCheck.get()); assertFalse(templateRegistry.putMlMetaIndexTemplateCheck.get()); - assertFalse(templateRegistry.putMlNotificationsIndexTemplateCheck.get()); + assertFalse(templateRegistry.putStateIndexTemplateCheck.get()); assertFalse(templateRegistry.putResultsIndexTemplateCheck.get()); } @@ -320,4 +320,4 @@ public class MachineLearningTemplateRegistryTests extends ESTestCase { .put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1001L) .build(); } -} \ No newline at end of file +}