|
15 | 15 | import org.elasticsearch.action.support.ThreadedActionListener;
|
16 | 16 | import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
17 | 17 | import org.elasticsearch.client.OriginSettingClient;
|
| 18 | +import org.elasticsearch.common.unit.TimeValue; |
18 | 19 | import org.elasticsearch.index.query.QueryBuilder;
|
19 | 20 | import org.elasticsearch.index.query.QueryBuilders;
|
20 | 21 | import org.elasticsearch.search.SearchHit;
|
21 | 22 | import org.elasticsearch.search.builder.SearchSourceBuilder;
|
| 23 | +import org.elasticsearch.search.sort.FieldSortBuilder; |
| 24 | +import org.elasticsearch.search.sort.SortBuilder; |
| 25 | +import org.elasticsearch.search.sort.SortOrder; |
22 | 26 | import org.elasticsearch.threadpool.ThreadPool;
|
23 | 27 | import org.elasticsearch.xpack.core.ml.action.DeleteModelSnapshotAction;
|
24 | 28 | import org.elasticsearch.xpack.core.ml.job.config.Job;
|
|
27 | 31 | import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot;
|
28 | 32 | import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotField;
|
29 | 33 | import org.elasticsearch.xpack.ml.MachineLearning;
|
| 34 | +import org.elasticsearch.xpack.ml.utils.MlIndicesUtils; |
30 | 35 | import org.elasticsearch.xpack.ml.utils.VolatileCursorIterator;
|
31 | 36 |
|
32 | 37 | import java.util.ArrayList;
|
33 | 38 | import java.util.Iterator;
|
34 | 39 | import java.util.List;
|
35 | 40 | import java.util.Objects;
|
| 41 | +import java.util.concurrent.TimeUnit; |
36 | 42 |
|
37 | 43 | /**
|
38 | 44 | * Deletes all model snapshots that have expired the configured retention time
|
@@ -65,10 +71,59 @@ public ExpiredModelSnapshotsRemover(OriginSettingClient client, ThreadPool threa
|
65 | 71 | }
|
66 | 72 |
|
67 | 73 | @Override
|
68 |
| - protected Long getRetentionDays(Job job) { |
| 74 | + Long getRetentionDays(Job job) { |
69 | 75 | return job.getModelSnapshotRetentionDays();
|
70 | 76 | }
|
71 | 77 |
|
| 78 | + @Override |
| 79 | + void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener<Long> listener) { |
| 80 | + ThreadedActionListener<Long> threadedActionListener = new ThreadedActionListener<>(LOGGER, threadPool, |
| 81 | + MachineLearning.UTILITY_THREAD_POOL_NAME, listener, false); |
| 82 | + |
| 83 | + latestSnapshotTimeStamp(jobId, ActionListener.wrap( |
| 84 | + latestTime -> { |
| 85 | + if (latestTime == null) { |
| 86 | + threadedActionListener.onResponse(null); |
| 87 | + } else { |
| 88 | + long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); |
| 89 | + threadedActionListener.onResponse(cutoff); |
| 90 | + } |
| 91 | + }, |
| 92 | + listener::onFailure |
| 93 | + )); |
| 94 | + } |
| 95 | + |
| 96 | + private void latestSnapshotTimeStamp(String jobId, ActionListener<Long> listener) { |
| 97 | + SortBuilder<?> sortBuilder = new FieldSortBuilder(ModelSnapshot.TIMESTAMP.getPreferredName()).order(SortOrder.DESC); |
| 98 | + QueryBuilder snapshotQuery = QueryBuilders.boolQuery() |
| 99 | + .filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName())); |
| 100 | + |
| 101 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 102 | + searchSourceBuilder.sort(sortBuilder); |
| 103 | + searchSourceBuilder.query(snapshotQuery); |
| 104 | + searchSourceBuilder.size(1); |
| 105 | + searchSourceBuilder.trackTotalHits(false); |
| 106 | + |
| 107 | + String indexName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId); |
| 108 | + SearchRequest searchRequest = new SearchRequest(indexName); |
| 109 | + searchRequest.source(searchSourceBuilder); |
| 110 | + searchRequest.indicesOptions(MlIndicesUtils.addIgnoreUnavailable(SearchRequest.DEFAULT_INDICES_OPTIONS)); |
| 111 | + |
| 112 | + client.search(searchRequest, ActionListener.wrap( |
| 113 | + response -> { |
| 114 | + SearchHit[] hits = response.getHits().getHits(); |
| 115 | + if (hits.length == 0) { |
| 116 | + // no snapshots found |
| 117 | + listener.onResponse(null); |
| 118 | + } else { |
| 119 | + ModelSnapshot snapshot = ModelSnapshot.fromJson(hits[0].getSourceRef()); |
| 120 | + listener.onResponse(snapshot.getTimestamp().getTime()); |
| 121 | + } |
| 122 | + }, |
| 123 | + listener::onFailure) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
72 | 127 | @Override
|
73 | 128 | protected void removeDataBefore(Job job, long cutoffEpochMs, ActionListener<Boolean> listener) {
|
74 | 129 | if (job.getModelSnapshotId() == null) {
|
@@ -96,7 +151,7 @@ protected void removeDataBefore(Job job, long cutoffEpochMs, ActionListener<Bool
|
96 | 151 | }
|
97 | 152 |
|
98 | 153 | private ActionListener<SearchResponse> expiredSnapshotsListener(String jobId, ActionListener<Boolean> listener) {
|
99 |
| - return new ActionListener<SearchResponse>() { |
| 154 | + return new ActionListener<>() { |
100 | 155 | @Override
|
101 | 156 | public void onResponse(SearchResponse searchResponse) {
|
102 | 157 | try {
|
|
0 commit comments