[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@7ae83648ce
This commit is contained in:
David Roberts 2017-03-03 10:59:24 +00:00 committed by GitHub
parent 01de84a19f
commit 440d005b1a
2 changed files with 18 additions and 11 deletions

View File

@ -40,7 +40,6 @@ import org.elasticsearch.xpack.ml.notifications.Auditor;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -114,8 +113,8 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen
} }
private void addMlNotificationsIndexTemplate(MetaData metaData) { 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(() -> { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
putNotificationMessageIndexTemplate((result, error) -> { putNotificationMessageIndexTemplate((result, error) -> {
putMlNotificationsIndexTemplateCheck.set(false); putMlNotificationsIndexTemplateCheck.set(false);
@ -127,13 +126,15 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen
} }
}); });
}); });
} else {
putMlNotificationsIndexTemplateCheck.set(false);
} }
} }
} }
private void addMlMetaIndexTemplate(MetaData metaData) { 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(() -> { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
putMetaIndexTemplate((result, error) -> { putMetaIndexTemplate((result, error) -> {
putMlMetaIndexTemplateCheck.set(false); putMlMetaIndexTemplateCheck.set(false);
@ -145,14 +146,16 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen
} }
}); });
}); });
} else {
putMlMetaIndexTemplateCheck.set(false);
} }
} }
} }
private void addStateIndexTemplate(MetaData metaData) { private void addStateIndexTemplate(MetaData metaData) {
String stateIndexName = AnomalyDetectorsIndex.jobStateIndexName(); 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(() -> { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
putJobStateIndexTemplate((result, error) -> { putJobStateIndexTemplate((result, error) -> {
putStateIndexTemplateCheck.set(false); putStateIndexTemplateCheck.set(false);
@ -163,13 +166,15 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen
} }
}); });
}); });
} else {
putStateIndexTemplateCheck.set(false);
} }
} }
} }
private void addResultsIndexTemplate(MetaData metaData) { 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(() -> { threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
putJobResultsIndexTemplate((result, error) -> { putJobResultsIndexTemplate((result, error) -> {
putResultsIndexTemplateCheck.set(false); putResultsIndexTemplateCheck.set(false);
@ -182,6 +187,8 @@ public class MachineLearningTemplateRegistry extends AbstractComponent implemen
} }
}); });
}); });
} else {
putResultsIndexTemplateCheck.set(false);
} }
} }
} }

View File

@ -148,7 +148,7 @@ public class MachineLearningTemplateRegistryTests extends ESTestCase {
verify(threadPool, times(0)).executor(anyString()); verify(threadPool, times(0)).executor(anyString());
assertFalse(templateRegistry.putMlNotificationsIndexTemplateCheck.get()); assertFalse(templateRegistry.putMlNotificationsIndexTemplateCheck.get());
assertFalse(templateRegistry.putMlMetaIndexTemplateCheck.get()); assertFalse(templateRegistry.putMlMetaIndexTemplateCheck.get());
assertFalse(templateRegistry.putMlNotificationsIndexTemplateCheck.get()); assertFalse(templateRegistry.putStateIndexTemplateCheck.get());
assertFalse(templateRegistry.putResultsIndexTemplateCheck.get()); assertFalse(templateRegistry.putResultsIndexTemplateCheck.get());
} }
@ -320,4 +320,4 @@ public class MachineLearningTemplateRegistryTests extends ESTestCase {
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1001L) .put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1001L)
.build(); .build();
} }
} }