Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Ensure annotations index mappings are up to date #61107

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.cluster.metadata.IndexAbstraction;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.template.TemplateUtils;

Expand All @@ -40,14 +41,24 @@ public class AnnotationIndex {
*/
public static void createAnnotationsIndexIfNecessary(Client client, ClusterState state, final ActionListener<Boolean> finalListener) {

final ActionListener<Boolean> checkMappingsListener = ActionListener.wrap(success -> {
ElasticsearchMappings.addDocMappingIfMissing(
WRITE_ALIAS_NAME,
AnnotationIndex::annotationsMapping,
client,
state,
finalListener);
}, finalListener::onFailure);

final ActionListener<Boolean> createAliasListener = ActionListener.wrap(success -> {
final IndicesAliasesRequest request =
client.admin().indices().prepareAliases()
.addAliasAction(IndicesAliasesRequest.AliasActions.add().index(INDEX_NAME).alias(READ_ALIAS_NAME).isHidden(true))
.addAliasAction(IndicesAliasesRequest.AliasActions.add().index(INDEX_NAME).alias(WRITE_ALIAS_NAME).isHidden(true))
.request();
executeAsyncWithOrigin(client.threadPool().getThreadContext(), ML_ORIGIN, request,
ActionListener.<AcknowledgedResponse>wrap(r -> finalListener.onResponse(r.isAcknowledged()), finalListener::onFailure),
ActionListener.<AcknowledgedResponse>wrap(
r -> checkMappingsListener.onResponse(r.isAcknowledged()), finalListener::onFailure),
client.admin().indices()::aliases);
}, finalListener::onFailure);

Expand Down Expand Up @@ -88,6 +99,10 @@ public static void createAnnotationsIndexIfNecessary(Client client, ClusterState
createAliasListener.onResponse(true);
return;
}

// Check the mappings
checkMappingsListener.onResponse(false);
return;
}

// Nothing to do, but respond to the listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.xpack.core.action.util.QueryPage;
import org.elasticsearch.xpack.core.ml.MlMetadata;
import org.elasticsearch.xpack.core.ml.action.GetFiltersAction;
import org.elasticsearch.xpack.core.ml.annotations.AnnotationIndex;
import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.config.JobState;
Expand Down Expand Up @@ -448,8 +449,20 @@ protected void doRun() {
);

// Try adding the results doc mapping - this updates to the latest version if an old mapping is present
ElasticsearchMappings.addDocMappingIfMissing(AnomalyDetectorsIndex.jobResultsAliasedName(jobId),
AnomalyDetectorsIndex::resultsMapping, client, clusterState, resultsMappingUpdateHandler);
ActionListener<Boolean> annotationsIndexUpdateHandler = ActionListener.wrap(
ack -> ElasticsearchMappings.addDocMappingIfMissing(AnomalyDetectorsIndex.jobResultsAliasedName(jobId),
AnomalyDetectorsIndex::resultsMapping, client, clusterState, resultsMappingUpdateHandler),
e -> {
// Due to a bug in 7.9.0 it's possible that the annotations index already has incorrect mappings
// and it would cause more harm than good to block jobs from opening in subsequent releases
logger.warn(new ParameterizedMessage("[{}] ML annotations index could not be updated with latest mappings", jobId), e);
ElasticsearchMappings.addDocMappingIfMissing(AnomalyDetectorsIndex.jobResultsAliasedName(jobId),
AnomalyDetectorsIndex::resultsMapping, client, clusterState, resultsMappingUpdateHandler);
}
);

// Create the annotations index if necessary - this also updates the mappings if an old mapping is present
AnnotationIndex.createAnnotationsIndexIfNecessary(client, clusterState, annotationsIndexUpdateHandler);
}

private boolean createProcessAndSetRunning(ProcessContext processContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,14 @@ setup:
job_id: old-cluster-function-shortcut-expansion
- match: { count: 1 }
- match: { jobs.0.analysis_config.detectors.0.function: "non_zero_count" }

---
"Test annotation index mappings":

- do:
indices.get_mapping:
index: .ml-annotations-write

- match: { \.ml-annotations-6.mappings.properties.type.type: "keyword" }
- match: { \.ml-annotations-6.mappings.properties.event.type: "keyword" }
- match: { \.ml-annotations-6.mappings.properties.detector_index.type: "integer" }