-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Calculate results and model snapshot retention using latest bucket timestamps #51061
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7c0161a
Calculate the results retention period based on the latest bucket time
davidkyle 7ca8db9
Define retention period in docs
davidkyle f620d6b
Start expired snapshots
davidkyle 6c56dda
Adapt for origin setting client
davidkyle 11fa81b
Fix the tests
davidkyle aee2d13
Rework docs
davidkyle 498f5b0
Address review comments
davidkyle 580c517
Make package-private
davidkyle e7724b9
nits
davidkyle e5026ee
Update docs/reference/ml/ml-shared.asciidoc
davidkyle 8bd4f99
Add ‘in days’
davidkyle 542869a
Apply docs suggestions from code review
davidkyle c553a42
Update docs/reference/ml/ml-shared.asciidoc
davidkyle 7569a2a
Remove duplicated line
davidkyle 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 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -57,15 +57,15 @@ public void setUpData() throws IOException { | |||||
.setMapping("time", "type=date,format=epoch_millis") | ||||||
.get(); | ||||||
|
||||||
// We are going to create data for last 2 days | ||||||
long nowMillis = System.currentTimeMillis(); | ||||||
// We are going to create 3 days of data starting 1 hr ago | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
long latestBucketTime = System.currentTimeMillis() - TimeValue.timeValueHours(1).millis(); | ||||||
int totalBuckets = 3 * 24; | ||||||
int normalRate = 10; | ||||||
int anomalousRate = 100; | ||||||
int anomalousBucket = 30; | ||||||
BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); | ||||||
for (int bucket = 0; bucket < totalBuckets; bucket++) { | ||||||
long timestamp = nowMillis - TimeValue.timeValueHours(totalBuckets - bucket).getMillis(); | ||||||
long timestamp = latestBucketTime - TimeValue.timeValueHours(totalBuckets - bucket).getMillis(); | ||||||
int bucketRate = bucket == anomalousBucket ? anomalousRate : normalRate; | ||||||
for (int point = 0; point < bucketRate; point++) { | ||||||
IndexRequest indexRequest = new IndexRequest(DATA_INDEX); | ||||||
|
@@ -208,7 +208,7 @@ public void testDeleteExpiredData() throws Exception { | |||||
assertThat(getModelSnapshots("no-retention").size(), equalTo(2)); | ||||||
|
||||||
List<Bucket> buckets = getBuckets("results-retention"); | ||||||
assertThat(buckets.size(), is(lessThanOrEqualTo(24))); | ||||||
assertThat(buckets.size(), is(lessThanOrEqualTo(25))); | ||||||
assertThat(buckets.size(), is(greaterThanOrEqualTo(22))); | ||||||
assertThat(buckets.get(0).getTimestamp().getTime(), greaterThanOrEqualTo(oneDayAgo)); | ||||||
assertThat(getRecords("results-retention").size(), equalTo(0)); | ||||||
|
@@ -223,7 +223,7 @@ public void testDeleteExpiredData() throws Exception { | |||||
assertThat(getModelSnapshots("snapshots-retention-with-retain").size(), equalTo(2)); | ||||||
|
||||||
buckets = getBuckets("results-and-snapshots-retention"); | ||||||
assertThat(buckets.size(), is(lessThanOrEqualTo(24))); | ||||||
assertThat(buckets.size(), is(lessThanOrEqualTo(25))); | ||||||
assertThat(buckets.size(), is(greaterThanOrEqualTo(22))); | ||||||
assertThat(buckets.get(0).getTimestamp().getTime(), greaterThanOrEqualTo(oneDayAgo)); | ||||||
assertThat(getRecords("results-and-snapshots-retention").size(), equalTo(0)); | ||||||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.client.OriginSettingClient; | ||
import org.elasticsearch.common.unit.TimeValue; | ||
import org.elasticsearch.index.query.BoolQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
|
@@ -16,12 +15,9 @@ | |
import org.elasticsearch.xpack.ml.job.persistence.BatchedJobsIterator; | ||
import org.elasticsearch.xpack.ml.utils.VolatileCursorIterator; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.util.Deque; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
|
@@ -68,22 +64,29 @@ private void removeData(WrappedBatchedJobsIterator jobIterator, ActionListener<B | |
removeData(jobIterator, listener, isTimedOutSupplier); | ||
return; | ||
} | ||
long cutoffEpochMs = calcCutoffEpochMs(retentionDays); | ||
removeDataBefore(job, cutoffEpochMs, | ||
ActionListener.wrap(response -> removeData(jobIterator, listener, isTimedOutSupplier), listener::onFailure)); | ||
|
||
calcCutoffEpochMs(job.getId(), retentionDays, ActionListener.wrap( | ||
cutoffEpochMs -> { | ||
if (cutoffEpochMs == null) { | ||
removeData(jobIterator, listener, isTimedOutSupplier); | ||
} else { | ||
removeDataBefore(job, cutoffEpochMs, ActionListener.wrap( | ||
response -> removeData(jobIterator, listener, isTimedOutSupplier), | ||
listener::onFailure)); | ||
} | ||
}, | ||
listener::onFailure | ||
)); | ||
} | ||
|
||
private WrappedBatchedJobsIterator newJobIterator() { | ||
BatchedJobsIterator jobsIterator = new BatchedJobsIterator(client, AnomalyDetectorsIndex.configIndexName()); | ||
return new WrappedBatchedJobsIterator(jobsIterator); | ||
} | ||
|
||
private long calcCutoffEpochMs(long retentionDays) { | ||
long nowEpochMs = Instant.now(Clock.systemDefaultZone()).toEpochMilli(); | ||
return nowEpochMs - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis(); | ||
} | ||
abstract void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener<Long> listener); | ||
|
||
protected abstract Long getRetentionDays(Job job); | ||
abstract Long getRetentionDays(Job job); | ||
|
||
/** | ||
* Template method to allow implementation details of various types of data (e.g. results, model snapshots). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The next two methods ( |
||
|
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
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.
@szabosteve can you look over these docs changes please. Do they make sense?
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.
@davidkyle Sorry, I haven't noticed this one earlier.