forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Clean left behind model state docs (elastic#30659)
It is possible for state documents to be left behind in the state index. This may be because of bugs or uncontrollable scenarios. In any case, those documents may take up quite some disk space when they add up. This commit adds a step in the expired data deletion that is part of the daily maintenance service. The new step searches for state documents that do not belong to any of the current jobs and deletes them. Closes elastic#30551
- Loading branch information
1 parent
7c2fc26
commit 75665a2
Showing
11 changed files
with
324 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...a/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/CategorizerStateTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
public class CategorizerStateTests extends ESTestCase { | ||
|
||
public void testExtractJobId_GivenValidDocId() { | ||
assertThat(CategorizerState.extractJobId("foo_categorizer_state#1"), equalTo("foo")); | ||
assertThat(CategorizerState.extractJobId("bar_categorizer_state#2"), equalTo("bar")); | ||
assertThat(CategorizerState.extractJobId("foo_bar_categorizer_state#3"), equalTo("foo_bar")); | ||
assertThat(CategorizerState.extractJobId("_categorizer_state_categorizer_state#3"), equalTo("_categorizer_state")); | ||
} | ||
|
||
public void testExtractJobId_GivenInvalidDocId() { | ||
assertThat(CategorizerState.extractJobId(""), is(nullValue())); | ||
assertThat(CategorizerState.extractJobId("foo"), is(nullValue())); | ||
assertThat(CategorizerState.extractJobId("_categorizer_state"), is(nullValue())); | ||
assertThat(CategorizerState.extractJobId("foo_model_state_3141341341"), is(nullValue())); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...st/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/ModelStateTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.job.process.autodetect.state; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
public class ModelStateTests extends ESTestCase { | ||
|
||
public void testExtractJobId_GivenValidDocId() { | ||
assertThat(ModelState.extractJobId("foo_model_state_3151373783#1"), equalTo("foo")); | ||
assertThat(ModelState.extractJobId("bar_model_state_451515#3"), equalTo("bar")); | ||
assertThat(ModelState.extractJobId("foo_bar_model_state_blah_blah"), equalTo("foo_bar")); | ||
assertThat(ModelState.extractJobId("_model_state_model_state_11111"), equalTo("_model_state")); | ||
} | ||
|
||
public void testExtractJobId_GivenInvalidDocId() { | ||
assertThat(ModelState.extractJobId(""), is(nullValue())); | ||
assertThat(ModelState.extractJobId("foo"), is(nullValue())); | ||
assertThat(ModelState.extractJobId("_model_3141341341"), is(nullValue())); | ||
assertThat(ModelState.extractJobId("_state_3141341341"), is(nullValue())); | ||
assertThat(ModelState.extractJobId("_model_state_3141341341"), is(nullValue())); | ||
assertThat(ModelState.extractJobId("foo_quantiles"), is(nullValue())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../src/main/java/org/elasticsearch/xpack/ml/job/persistence/BatchedStateDocIdsIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.job.persistence; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.search.SearchHit; | ||
|
||
/** | ||
* Iterates through the state doc ids | ||
*/ | ||
public class BatchedStateDocIdsIterator extends BatchedDocumentsIterator<String> { | ||
|
||
public BatchedStateDocIdsIterator(Client client, String index) { | ||
super(client, index); | ||
} | ||
|
||
@Override | ||
protected boolean shouldFetchSource() { | ||
return false; | ||
} | ||
|
||
@Override | ||
protected QueryBuilder getQuery() { | ||
return QueryBuilders.matchAllQuery(); | ||
} | ||
|
||
@Override | ||
protected String map(SearchHit hit) { | ||
return hit.getId(); | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
.../plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/UnusedStateRemover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.job.retention; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.bulk.BulkRequestBuilder; | ||
import org.elasticsearch.action.bulk.BulkResponse; | ||
import org.elasticsearch.action.delete.DeleteRequest; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.xpack.core.ml.MLMetadataField; | ||
import org.elasticsearch.xpack.core.ml.MlMetadata; | ||
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; | ||
import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings; | ||
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.CategorizerState; | ||
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelState; | ||
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.Quantiles; | ||
import org.elasticsearch.xpack.ml.job.persistence.BatchedStateDocIdsIterator; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Deque; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* If for any reason a job is deleted by some of its state documents | ||
* are left behind, this class deletes any unused documents stored | ||
* in the .ml-state index. | ||
*/ | ||
public class UnusedStateRemover implements MlDataRemover { | ||
|
||
private static final Logger LOGGER = Loggers.getLogger(UnusedStateRemover.class); | ||
|
||
private final Client client; | ||
private final ClusterService clusterService; | ||
|
||
public UnusedStateRemover(Client client, ClusterService clusterService) { | ||
this.client = Objects.requireNonNull(client); | ||
this.clusterService = Objects.requireNonNull(clusterService); | ||
} | ||
|
||
@Override | ||
public void remove(ActionListener<Boolean> listener) { | ||
try { | ||
BulkRequestBuilder deleteUnusedStateRequestBuilder = findUnusedStateDocs(); | ||
if (deleteUnusedStateRequestBuilder.numberOfActions() > 0) { | ||
executeDeleteUnusedStateDocs(deleteUnusedStateRequestBuilder, listener); | ||
} else { | ||
listener.onResponse(true); | ||
} | ||
} catch (Exception e) { | ||
listener.onFailure(e); | ||
} | ||
} | ||
|
||
private BulkRequestBuilder findUnusedStateDocs() { | ||
Set<String> jobIds = getJobIds(); | ||
BulkRequestBuilder deleteUnusedStateRequestBuilder = client.prepareBulk(); | ||
BatchedStateDocIdsIterator stateDocIdsIterator = new BatchedStateDocIdsIterator(client, AnomalyDetectorsIndex.jobStateIndexName()); | ||
while (stateDocIdsIterator.hasNext()) { | ||
Deque<String> stateDocIds = stateDocIdsIterator.next(); | ||
for (String stateDocId : stateDocIds) { | ||
String jobId = JobIdExtractor.extractJobId(stateDocId); | ||
if (jobId == null) { | ||
// not a managed state document id | ||
continue; | ||
} | ||
if (jobIds.contains(jobId) == false) { | ||
deleteUnusedStateRequestBuilder.add(new DeleteRequest( | ||
AnomalyDetectorsIndex.jobStateIndexName(), ElasticsearchMappings.DOC_TYPE, stateDocId)); | ||
} | ||
} | ||
} | ||
return deleteUnusedStateRequestBuilder; | ||
} | ||
|
||
private Set<String> getJobIds() { | ||
ClusterState clusterState = clusterService.state(); | ||
MlMetadata mlMetadata = clusterState.getMetaData().custom(MLMetadataField.TYPE); | ||
if (mlMetadata != null) { | ||
return mlMetadata.getJobs().keySet(); | ||
} | ||
return Collections.emptySet(); | ||
} | ||
|
||
private void executeDeleteUnusedStateDocs(BulkRequestBuilder deleteUnusedStateRequestBuilder, ActionListener<Boolean> listener) { | ||
LOGGER.info("Found [{}] unused state documents; attempting to delete", | ||
deleteUnusedStateRequestBuilder.numberOfActions()); | ||
deleteUnusedStateRequestBuilder.execute(new ActionListener<BulkResponse>() { | ||
@Override | ||
public void onResponse(BulkResponse bulkItemResponses) { | ||
if (bulkItemResponses.hasFailures()) { | ||
LOGGER.error("Some unused state documents could not be deleted due to failures: {}", | ||
bulkItemResponses.buildFailureMessage()); | ||
} else { | ||
LOGGER.info("Successfully deleted all unused state documents"); | ||
} | ||
listener.onResponse(true); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
LOGGER.error("Error deleting unused model state documents: ", e); | ||
listener.onFailure(e); | ||
} | ||
}); | ||
} | ||
|
||
private static class JobIdExtractor { | ||
|
||
private static List<Function<String, String>> extractors = Arrays.asList( | ||
ModelState::extractJobId, Quantiles::extractJobId, CategorizerState::extractJobId); | ||
|
||
private static String extractJobId(String docId) { | ||
String jobId; | ||
for (Function<String, String> extractor : extractors) { | ||
jobId = extractor.apply(docId); | ||
if (jobId != null) { | ||
return jobId; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.