[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
This commit is contained in:
David Roberts 2018-12-19 13:06:24 +00:00 committed by GitHub
parent 8f141b8a41
commit ad20d6bb83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -38,12 +38,12 @@ public class AnnotationIndex {
public static final String INDEX_NAME = ".ml-annotations-6"; public static final String INDEX_NAME = ".ml-annotations-6";
/** /**
* Create the .ml-annotations index with correct mappings. * Create the .ml-annotations index with correct mappings if it does not already
* This index is read and written by the UI results views, * exist. This index is read and written by the UI results views, so needs to
* so needs to exist when there might be ML results to view. * exist when there might be ML results to view.
*/ */
public static void createAnnotationsIndex(Settings settings, Client client, ClusterState state, public static void createAnnotationsIndexIfNecessary(Settings settings, Client client, ClusterState state,
final ActionListener<Boolean> finalListener) { final ActionListener<Boolean> finalListener) {
final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> { final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> {
final IndicesAliasesRequest request = client.admin().indices().prepareAliases() final IndicesAliasesRequest request = client.admin().indices().prepareAliases()

View File

@ -19,6 +19,8 @@ import org.elasticsearch.gateway.GatewayService;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex; import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex;
import java.util.concurrent.atomic.AtomicBoolean;
class MlInitializationService implements LocalNodeMasterListener, ClusterStateListener { class MlInitializationService implements LocalNodeMasterListener, ClusterStateListener {
private static final Logger logger = LogManager.getLogger(MlInitializationService.class); private static final Logger logger = LogManager.getLogger(MlInitializationService.class);
@ -27,6 +29,7 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
private final ThreadPool threadPool; private final ThreadPool threadPool;
private final ClusterService clusterService; private final ClusterService clusterService;
private final Client client; private final Client client;
private final AtomicBoolean isIndexCreationInProgress = new AtomicBoolean(false);
private volatile MlDailyMaintenanceService mlDailyMaintenanceService; private volatile MlDailyMaintenanceService mlDailyMaintenanceService;
@ -55,14 +58,20 @@ class MlInitializationService implements LocalNodeMasterListener, ClusterStateLi
return; return;
} }
if (event.localNodeMaster()) { // The atomic flag prevents multiple simultaneous attempts to create the
AnnotationIndex.createAnnotationsIndex(settings, client, event.state(), ActionListener.wrap( // index if there is a flurry of cluster state updates in quick succession
if (event.localNodeMaster() && isIndexCreationInProgress.compareAndSet(false, true)) {
AnnotationIndex.createAnnotationsIndexIfNecessary(settings, client, event.state(), ActionListener.wrap(
r -> { r -> {
isIndexCreationInProgress.set(false);
if (r) { if (r) {
logger.info("Created ML annotations index and aliases"); logger.info("Created ML annotations index and aliases");
} }
}, },
e -> logger.error("Error creating ML annotations index or aliases", e))); e -> {
isIndexCreationInProgress.set(false);
logger.error("Error creating ML annotations index or aliases", e);
}));
} }
} }