Skip to content

Commit a74d822

Browse files
author
David Roberts
authored
[ML] Add daily_model_snapshot_retention_after_days to job config (#55878)
This change adds a new setting, daily_model_snapshot_retention_after_days, to the anomaly detection job config. Initially this has no effect, the effect will be added in a followup PR. This PR gets the complexities of making changes that interact with BWC over well before feature freeze. Relates #52150
1 parent c03fae2 commit a74d822

File tree

9 files changed

+167
-26
lines changed

9 files changed

+167
-26
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/Job.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class Job implements ToXContentObject {
6363
public static final ParseField RENORMALIZATION_WINDOW_DAYS = new ParseField("renormalization_window_days");
6464
public static final ParseField BACKGROUND_PERSIST_INTERVAL = new ParseField("background_persist_interval");
6565
public static final ParseField MODEL_SNAPSHOT_RETENTION_DAYS = new ParseField("model_snapshot_retention_days");
66+
public static final ParseField DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS =
67+
new ParseField("daily_model_snapshot_retention_after_days");
6668
public static final ParseField RESULTS_RETENTION_DAYS = new ParseField("results_retention_days");
6769
public static final ParseField MODEL_SNAPSHOT_ID = new ParseField("model_snapshot_id");
6870
public static final ParseField RESULTS_INDEX_NAME = new ParseField("results_index_name");
@@ -93,6 +95,7 @@ public class Job implements ToXContentObject {
9395
TimeValue.parseTimeValue(val, BACKGROUND_PERSIST_INTERVAL.getPreferredName())), BACKGROUND_PERSIST_INTERVAL);
9496
PARSER.declareLong(Builder::setResultsRetentionDays, RESULTS_RETENTION_DAYS);
9597
PARSER.declareLong(Builder::setModelSnapshotRetentionDays, MODEL_SNAPSHOT_RETENTION_DAYS);
98+
PARSER.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS);
9699
PARSER.declareField(Builder::setCustomSettings, (p, c) -> p.mapOrdered(), CUSTOM_SETTINGS, ValueType.OBJECT);
97100
PARSER.declareStringOrNull(Builder::setModelSnapshotId, MODEL_SNAPSHOT_ID);
98101
PARSER.declareString(Builder::setResultsIndexName, RESULTS_INDEX_NAME);
@@ -114,6 +117,7 @@ public class Job implements ToXContentObject {
114117
private final Long renormalizationWindowDays;
115118
private final TimeValue backgroundPersistInterval;
116119
private final Long modelSnapshotRetentionDays;
120+
private final Long dailyModelSnapshotRetentionAfterDays;
117121
private final Long resultsRetentionDays;
118122
private final Map<String, Object> customSettings;
119123
private final String modelSnapshotId;
@@ -125,8 +129,9 @@ private Job(String jobId, String jobType, List<String> groups, String descriptio
125129
Date createTime, Date finishedTime,
126130
AnalysisConfig analysisConfig, AnalysisLimits analysisLimits, DataDescription dataDescription,
127131
ModelPlotConfig modelPlotConfig, Long renormalizationWindowDays, TimeValue backgroundPersistInterval,
128-
Long modelSnapshotRetentionDays, Long resultsRetentionDays, Map<String, Object> customSettings,
129-
String modelSnapshotId, String resultsIndexName, Boolean deleting, Boolean allowLazyOpen) {
132+
Long modelSnapshotRetentionDays, Long dailyModelSnapshotRetentionAfterDays, Long resultsRetentionDays,
133+
Map<String, Object> customSettings, String modelSnapshotId, String resultsIndexName, Boolean deleting,
134+
Boolean allowLazyOpen) {
130135

131136
this.jobId = jobId;
132137
this.jobType = jobType;
@@ -141,6 +146,7 @@ private Job(String jobId, String jobType, List<String> groups, String descriptio
141146
this.renormalizationWindowDays = renormalizationWindowDays;
142147
this.backgroundPersistInterval = backgroundPersistInterval;
143148
this.modelSnapshotRetentionDays = modelSnapshotRetentionDays;
149+
this.dailyModelSnapshotRetentionAfterDays = dailyModelSnapshotRetentionAfterDays;
144150
this.resultsRetentionDays = resultsRetentionDays;
145151
this.customSettings = customSettings == null ? null : Collections.unmodifiableMap(customSettings);
146152
this.modelSnapshotId = modelSnapshotId;
@@ -259,6 +265,10 @@ public Long getModelSnapshotRetentionDays() {
259265
return modelSnapshotRetentionDays;
260266
}
261267

268+
public Long getDailyModelSnapshotRetentionAfterDays() {
269+
return dailyModelSnapshotRetentionAfterDays;
270+
}
271+
262272
public Long getResultsRetentionDays() {
263273
return resultsRetentionDays;
264274
}
@@ -319,6 +329,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
319329
if (modelSnapshotRetentionDays != null) {
320330
builder.field(MODEL_SNAPSHOT_RETENTION_DAYS.getPreferredName(), modelSnapshotRetentionDays);
321331
}
332+
if (dailyModelSnapshotRetentionAfterDays != null) {
333+
builder.field(DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), dailyModelSnapshotRetentionAfterDays);
334+
}
322335
if (resultsRetentionDays != null) {
323336
builder.field(RESULTS_RETENTION_DAYS.getPreferredName(), resultsRetentionDays);
324337
}
@@ -365,6 +378,7 @@ public boolean equals(Object other) {
365378
&& Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays)
366379
&& Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval)
367380
&& Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays)
381+
&& Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays)
368382
&& Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays)
369383
&& Objects.equals(this.customSettings, that.customSettings)
370384
&& Objects.equals(this.modelSnapshotId, that.modelSnapshotId)
@@ -377,8 +391,8 @@ public boolean equals(Object other) {
377391
public int hashCode() {
378392
return Objects.hash(jobId, jobType, groups, description, createTime, finishedTime,
379393
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
380-
backgroundPersistInterval, modelSnapshotRetentionDays, resultsRetentionDays, customSettings,
381-
modelSnapshotId, resultsIndexName, deleting, allowLazyOpen);
394+
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
395+
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen);
382396
}
383397

384398
@Override
@@ -405,6 +419,7 @@ public static class Builder {
405419
private Long renormalizationWindowDays;
406420
private TimeValue backgroundPersistInterval;
407421
private Long modelSnapshotRetentionDays;
422+
private Long dailyModelSnapshotRetentionAfterDays;
408423
private Long resultsRetentionDays;
409424
private Map<String, Object> customSettings;
410425
private String modelSnapshotId;
@@ -433,6 +448,7 @@ public Builder(Job job) {
433448
this.renormalizationWindowDays = job.getRenormalizationWindowDays();
434449
this.backgroundPersistInterval = job.getBackgroundPersistInterval();
435450
this.modelSnapshotRetentionDays = job.getModelSnapshotRetentionDays();
451+
this.dailyModelSnapshotRetentionAfterDays = job.getDailyModelSnapshotRetentionAfterDays();
436452
this.resultsRetentionDays = job.getResultsRetentionDays();
437453
this.customSettings = job.getCustomSettings() == null ? null : new LinkedHashMap<>(job.getCustomSettings());
438454
this.modelSnapshotId = job.getModelSnapshotId();
@@ -515,6 +531,11 @@ public Builder setModelSnapshotRetentionDays(Long modelSnapshotRetentionDays) {
515531
return this;
516532
}
517533

534+
public Builder setDailyModelSnapshotRetentionAfterDays(Long dailyModelSnapshotRetentionAfterDays) {
535+
this.dailyModelSnapshotRetentionAfterDays = dailyModelSnapshotRetentionAfterDays;
536+
return this;
537+
}
538+
518539
public Builder setResultsRetentionDays(Long resultsRetentionDays) {
519540
this.resultsRetentionDays = resultsRetentionDays;
520541
return this;
@@ -551,8 +572,8 @@ public Job build() {
551572
return new Job(
552573
id, jobType, groups, description, createTime, finishedTime,
553574
analysisConfig, analysisLimits, dataDescription, modelPlotConfig, renormalizationWindowDays,
554-
backgroundPersistInterval, modelSnapshotRetentionDays, resultsRetentionDays, customSettings,
555-
modelSnapshotId, resultsIndexName, deleting, allowLazyOpen);
575+
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
576+
customSettings, modelSnapshotId, resultsIndexName, deleting, allowLazyOpen);
556577
}
557578
}
558579
}

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobUpdate.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class JobUpdate implements ToXContentObject {
5252
PARSER.declareLong(Builder::setRenormalizationWindowDays, Job.RENORMALIZATION_WINDOW_DAYS);
5353
PARSER.declareLong(Builder::setResultsRetentionDays, Job.RESULTS_RETENTION_DAYS);
5454
PARSER.declareLong(Builder::setModelSnapshotRetentionDays, Job.MODEL_SNAPSHOT_RETENTION_DAYS);
55+
PARSER.declareLong(Builder::setDailyModelSnapshotRetentionAfterDays, Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS);
5556
PARSER.declareStringArray(Builder::setCategorizationFilters, AnalysisConfig.CATEGORIZATION_FILTERS);
5657
PARSER.declareField(Builder::setCustomSettings, (p, c) -> p.map(), Job.CUSTOM_SETTINGS, ObjectParser.ValueType.OBJECT);
5758
PARSER.declareBoolean(Builder::setAllowLazyOpen, Job.ALLOW_LAZY_OPEN);
@@ -66,6 +67,7 @@ public class JobUpdate implements ToXContentObject {
6667
private final Long renormalizationWindowDays;
6768
private final TimeValue backgroundPersistInterval;
6869
private final Long modelSnapshotRetentionDays;
70+
private final Long dailyModelSnapshotRetentionAfterDays;
6971
private final Long resultsRetentionDays;
7072
private final List<String> categorizationFilters;
7173
private final Map<String, Object> customSettings;
@@ -75,7 +77,8 @@ private JobUpdate(String jobId, @Nullable List<String> groups, @Nullable String
7577
@Nullable List<DetectorUpdate> detectorUpdates, @Nullable ModelPlotConfig modelPlotConfig,
7678
@Nullable AnalysisLimits analysisLimits, @Nullable TimeValue backgroundPersistInterval,
7779
@Nullable Long renormalizationWindowDays, @Nullable Long resultsRetentionDays,
78-
@Nullable Long modelSnapshotRetentionDays, @Nullable List<String> categorisationFilters,
80+
@Nullable Long modelSnapshotRetentionDays, @Nullable Long dailyModelSnapshotRetentionAfterDays,
81+
@Nullable List<String> categorizationFilters,
7982
@Nullable Map<String, Object> customSettings, @Nullable Boolean allowLazyOpen) {
8083
this.jobId = jobId;
8184
this.groups = groups;
@@ -86,8 +89,9 @@ private JobUpdate(String jobId, @Nullable List<String> groups, @Nullable String
8689
this.renormalizationWindowDays = renormalizationWindowDays;
8790
this.backgroundPersistInterval = backgroundPersistInterval;
8891
this.modelSnapshotRetentionDays = modelSnapshotRetentionDays;
92+
this.dailyModelSnapshotRetentionAfterDays = dailyModelSnapshotRetentionAfterDays;
8993
this.resultsRetentionDays = resultsRetentionDays;
90-
this.categorizationFilters = categorisationFilters;
94+
this.categorizationFilters = categorizationFilters;
9195
this.customSettings = customSettings;
9296
this.allowLazyOpen = allowLazyOpen;
9397
}
@@ -172,6 +176,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
172176
if (modelSnapshotRetentionDays != null) {
173177
builder.field(Job.MODEL_SNAPSHOT_RETENTION_DAYS.getPreferredName(), modelSnapshotRetentionDays);
174178
}
179+
if (dailyModelSnapshotRetentionAfterDays != null) {
180+
builder.field(Job.DAILY_MODEL_SNAPSHOT_RETENTION_AFTER_DAYS.getPreferredName(), dailyModelSnapshotRetentionAfterDays);
181+
}
175182
if (resultsRetentionDays != null) {
176183
builder.field(Job.RESULTS_RETENTION_DAYS.getPreferredName(), resultsRetentionDays);
177184
}
@@ -209,6 +216,7 @@ public boolean equals(Object other) {
209216
&& Objects.equals(this.renormalizationWindowDays, that.renormalizationWindowDays)
210217
&& Objects.equals(this.backgroundPersistInterval, that.backgroundPersistInterval)
211218
&& Objects.equals(this.modelSnapshotRetentionDays, that.modelSnapshotRetentionDays)
219+
&& Objects.equals(this.dailyModelSnapshotRetentionAfterDays, that.dailyModelSnapshotRetentionAfterDays)
212220
&& Objects.equals(this.resultsRetentionDays, that.resultsRetentionDays)
213221
&& Objects.equals(this.categorizationFilters, that.categorizationFilters)
214222
&& Objects.equals(this.customSettings, that.customSettings)
@@ -218,8 +226,8 @@ public boolean equals(Object other) {
218226
@Override
219227
public int hashCode() {
220228
return Objects.hash(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, renormalizationWindowDays,
221-
backgroundPersistInterval, modelSnapshotRetentionDays, resultsRetentionDays, categorizationFilters, customSettings,
222-
allowLazyOpen);
229+
backgroundPersistInterval, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays, resultsRetentionDays,
230+
categorizationFilters, customSettings, allowLazyOpen);
223231
}
224232

225233
public static class DetectorUpdate implements ToXContentObject {
@@ -312,6 +320,7 @@ public static class Builder {
312320
private Long renormalizationWindowDays;
313321
private TimeValue backgroundPersistInterval;
314322
private Long modelSnapshotRetentionDays;
323+
private Long dailyModelSnapshotRetentionAfterDays;
315324
private Long resultsRetentionDays;
316325
private List<String> categorizationFilters;
317326
private Map<String, Object> customSettings;
@@ -422,6 +431,18 @@ public Builder setModelSnapshotRetentionDays(Long modelSnapshotRetentionDays) {
422431
return this;
423432
}
424433

434+
/**
435+
* The time in days after which only one model snapshot per day is retained for the job.
436+
*
437+
* Updates the {@link Job#dailyModelSnapshotRetentionAfterDays} setting
438+
*
439+
* @param dailyModelSnapshotRetentionAfterDays number of days to keep a model snapshot
440+
*/
441+
public Builder setDailyModelSnapshotRetentionAfterDays(Long dailyModelSnapshotRetentionAfterDays) {
442+
this.dailyModelSnapshotRetentionAfterDays = dailyModelSnapshotRetentionAfterDays;
443+
return this;
444+
}
445+
425446
/**
426447
* Advanced configuration option. The number of days for which job results are retained
427448
*
@@ -466,8 +487,8 @@ public Builder setAllowLazyOpen(boolean allowLazyOpen) {
466487

467488
public JobUpdate build() {
468489
return new JobUpdate(jobId, groups, description, detectorUpdates, modelPlotConfig, analysisLimits, backgroundPersistInterval,
469-
renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, categorizationFilters, customSettings,
470-
allowLazyOpen);
490+
renormalizationWindowDays, resultsRetentionDays, modelSnapshotRetentionDays, dailyModelSnapshotRetentionAfterDays,
491+
categorizationFilters, customSettings, allowLazyOpen);
471492
}
472493
}
473494
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public static Job.Builder createRandomizedJobBuilder() {
144144
if (randomBoolean()) {
145145
builder.setModelSnapshotRetentionDays(randomNonNegativeLong());
146146
}
147+
if (randomBoolean()) {
148+
builder.setDailyModelSnapshotRetentionAfterDays(randomNonNegativeLong());
149+
}
147150
if (randomBoolean()) {
148151
builder.setResultsRetentionDays(randomNonNegativeLong());
149152
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobUpdateTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public static JobUpdate createRandom(String jobId) {
7070
if (randomBoolean()) {
7171
update.setModelSnapshotRetentionDays(randomNonNegativeLong());
7272
}
73+
if (randomBoolean()) {
74+
update.setDailyModelSnapshotRetentionAfterDays(randomNonNegativeLong());
75+
}
7376
if (randomBoolean()) {
7477
update.setResultsRetentionDays(randomNonNegativeLong());
7578
}

0 commit comments

Comments
 (0)