-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix delete_expired_data/nightly maintenance when many model snapshots need deleting #57041
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import org.elasticsearch.search.sort.SortBuilder; | ||
| import org.elasticsearch.search.sort.SortOrder; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.xpack.core.common.time.TimeUtils; | ||
| import org.elasticsearch.xpack.core.ml.action.DeleteModelSnapshotAction; | ||
| import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
| import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; | ||
|
|
@@ -104,13 +105,16 @@ void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener<CutoffDe | |
| private void latestSnapshotTimeStamp(String jobId, ActionListener<Long> listener) { | ||
| SortBuilder<?> sortBuilder = new FieldSortBuilder(ModelSnapshot.TIMESTAMP.getPreferredName()).order(SortOrder.DESC); | ||
| QueryBuilder snapshotQuery = QueryBuilders.boolQuery() | ||
| .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())); | ||
| .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())) | ||
| .filter(QueryBuilders.existsQuery(ModelSnapshot.TIMESTAMP.getPreferredName())); | ||
|
|
||
| SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); | ||
| searchSourceBuilder.sort(sortBuilder); | ||
| searchSourceBuilder.query(snapshotQuery); | ||
| searchSourceBuilder.size(1); | ||
| searchSourceBuilder.trackTotalHits(false); | ||
| searchSourceBuilder.fetchSource(false); | ||
| searchSourceBuilder.docValueField(ModelSnapshot.TIMESTAMP.getPreferredName(), "epoch_millis"); | ||
|
|
||
| String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); | ||
| SearchRequest searchRequest = new SearchRequest(indexName); | ||
|
|
@@ -124,8 +128,14 @@ private void latestSnapshotTimeStamp(String jobId, ActionListener<Long> listener | |
| // no snapshots found | ||
| listener.onResponse(null); | ||
| } else { | ||
| ModelSnapshot snapshot = ModelSnapshot.fromJson(hits[0].getSourceRef()); | ||
| listener.onResponse(snapshot.getTimestamp().getTime()); | ||
| String timestamp = stringFieldValueOrNull(hits[0], ModelSnapshot.TIMESTAMP.getPreferredName()); | ||
| if (timestamp == null) { | ||
| LOGGER.warn("Model snapshot document [{}] has a null timestamp field", hits[0].getId()); | ||
| listener.onResponse(null); | ||
| } else { | ||
| long timestampMs = TimeUtils.parseToEpochMs(timestamp); | ||
| listener.onResponse(timestampMs); | ||
| } | ||
| } | ||
| }, | ||
| listener::onFailure) | ||
|
|
@@ -159,8 +169,15 @@ protected void removeDataBefore( | |
| .mustNot(activeSnapshotFilter) | ||
| .mustNot(retainFilter); | ||
|
|
||
| searchRequest.source(new SearchSourceBuilder().query(query).size(MODEL_SNAPSHOT_SEARCH_SIZE) | ||
| .sort(ModelSnapshot.TIMESTAMP.getPreferredName())); | ||
| SearchSourceBuilder source = new SearchSourceBuilder(); | ||
| source.query(query); | ||
| source.size(MODEL_SNAPSHOT_SEARCH_SIZE); | ||
| source.sort(ModelSnapshot.TIMESTAMP.getPreferredName()); | ||
| source.fetchSource(false); | ||
| source.docValueField(Job.ID.getPreferredName(), null); | ||
| source.docValueField(ModelSnapshotField.SNAPSHOT_ID.getPreferredName(), null); | ||
| source.docValueField(ModelSnapshot.TIMESTAMP.getPreferredName(), "epoch_millis"); | ||
| searchRequest.source(source); | ||
|
|
||
| long deleteAllBeforeMs = (job.getModelSnapshotRetentionDays() == null) | ||
| ? 0 : latestTimeMs - TimeValue.timeValueDays(job.getModelSnapshotRetentionDays()).getMillis(); | ||
|
|
@@ -175,19 +192,29 @@ private ActionListener<SearchResponse> expiredSnapshotsListener(String jobId, lo | |
| public void onResponse(SearchResponse searchResponse) { | ||
| long nextToKeepMs = deleteAllBeforeMs; | ||
| try { | ||
| List<ModelSnapshot> modelSnapshots = new ArrayList<>(); | ||
| List<JobSnapshotId> snapshotIds = new ArrayList<>(); | ||
| for (SearchHit hit : searchResponse.getHits()) { | ||
| ModelSnapshot modelSnapshot = ModelSnapshot.fromJson(hit.getSourceRef()); | ||
| long timestampMs = modelSnapshot.getTimestamp().getTime(); | ||
| String timestamp = stringFieldValueOrNull(hit, ModelSnapshot.TIMESTAMP.getPreferredName()); | ||
| if (timestamp == null) { | ||
| LOGGER.warn("Model snapshot document [{}] has a null timestamp field", hit.getId()); | ||
| continue; | ||
|
||
| } | ||
| long timestampMs = TimeUtils.parseToEpochMs(timestamp); | ||
| if (timestampMs >= nextToKeepMs) { | ||
| do { | ||
| nextToKeepMs += MS_IN_ONE_DAY; | ||
| } while (timestampMs >= nextToKeepMs); | ||
| continue; | ||
| } | ||
| modelSnapshots.add(modelSnapshot); | ||
| JobSnapshotId idPair = new JobSnapshotId( | ||
| stringFieldValueOrNull(hit, Job.ID.getPreferredName()), | ||
| stringFieldValueOrNull(hit, ModelSnapshotField.SNAPSHOT_ID.getPreferredName())); | ||
|
|
||
| if (idPair.hasNullValue() == false) { | ||
| snapshotIds.add(idPair); | ||
| } | ||
| } | ||
| deleteModelSnapshots(new VolatileCursorIterator<>(modelSnapshots), listener); | ||
| deleteModelSnapshots(new VolatileCursorIterator<>(snapshotIds), listener); | ||
| } catch (Exception e) { | ||
| onFailure(e); | ||
| } | ||
|
|
@@ -200,15 +227,15 @@ public void onFailure(Exception e) { | |
| }; | ||
| } | ||
|
|
||
| private void deleteModelSnapshots(Iterator<ModelSnapshot> modelSnapshotIterator, ActionListener<Boolean> listener) { | ||
| private void deleteModelSnapshots(Iterator<JobSnapshotId> modelSnapshotIterator, ActionListener<Boolean> listener) { | ||
| if (modelSnapshotIterator.hasNext() == false) { | ||
| listener.onResponse(true); | ||
| return; | ||
| } | ||
| ModelSnapshot modelSnapshot = modelSnapshotIterator.next(); | ||
| DeleteModelSnapshotAction.Request deleteSnapshotRequest = new DeleteModelSnapshotAction.Request( | ||
| modelSnapshot.getJobId(), modelSnapshot.getSnapshotId()); | ||
| client.execute(DeleteModelSnapshotAction.INSTANCE, deleteSnapshotRequest, new ActionListener<AcknowledgedResponse>() { | ||
| JobSnapshotId idPair = modelSnapshotIterator.next(); | ||
| DeleteModelSnapshotAction.Request deleteSnapshotRequest = | ||
| new DeleteModelSnapshotAction.Request(idPair.jobId, idPair.snapshotId); | ||
| client.execute(DeleteModelSnapshotAction.INSTANCE, deleteSnapshotRequest, new ActionListener<>() { | ||
| @Override | ||
| public void onResponse(AcknowledgedResponse response) { | ||
| try { | ||
|
|
@@ -220,9 +247,23 @@ public void onResponse(AcknowledgedResponse response) { | |
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| listener.onFailure(new ElasticsearchException("[" + modelSnapshot.getJobId() + "] Failed to delete snapshot [" | ||
| + modelSnapshot.getSnapshotId() + "]", e)); | ||
| listener.onFailure(new ElasticsearchException("[" + idPair.jobId + "] Failed to delete snapshot [" | ||
| + idPair.snapshotId + "]", e)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| static class JobSnapshotId { | ||
| private final String jobId; | ||
| private final String snapshotId; | ||
|
|
||
| JobSnapshotId(String jobId, String snapshotId) { | ||
| this.jobId = jobId; | ||
| this.snapshotId = snapshotId; | ||
| } | ||
|
|
||
| boolean hasNullValue() { | ||
| return jobId == null || snapshotId == null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will removing this make:
less useful? (Around line 141/145.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot I couldn't see where it was checked so removed it.
The benefit of setting
trackTotalHits(true)as the only purpose is to log this message saying there are more forecasts to be deleted later. However, in the interest of making this change as small as possible I've reverted this line