Skip to content

Commit 11fa81b

Browse files
committed
Fix the tests
1 parent 6c56dda commit 11fa81b

File tree

6 files changed

+186
-181
lines changed

6 files changed

+186
-181
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemover.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,15 @@ private void removeData(WrappedBatchedJobsIterator jobIterator, ActionListener<B
7070
}
7171

7272
calcCutoffEpochMs(job.getId(), retentionDays, ActionListener.wrap(
73-
cutoffEpochMs -> removeDataBefore(job, cutoffEpochMs,
74-
ActionListener.wrap(response -> removeData(jobIterator, listener, isTimedOutSupplier), listener::onFailure)),
73+
cutoffEpochMs -> {
74+
if (cutoffEpochMs == null) {
75+
removeData(jobIterator, listener, isTimedOutSupplier);
76+
} else {
77+
removeDataBefore(job, cutoffEpochMs, ActionListener.wrap(
78+
response -> removeData(jobIterator, listener, isTimedOutSupplier),
79+
listener::onFailure));
80+
}
81+
},
7582
listener::onFailure
7683
));
7784
}

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredModelSnapshotsRemover.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import org.elasticsearch.xpack.core.ml.job.persistence.ElasticsearchMappings;
3131
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshot;
3232
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotField;
33-
import org.elasticsearch.xpack.core.ml.job.results.Bucket;
34-
import org.elasticsearch.xpack.core.ml.job.results.Result;
3533
import org.elasticsearch.xpack.ml.MachineLearning;
3634
import org.elasticsearch.xpack.ml.utils.MlIndicesUtils;
3735
import org.elasticsearch.xpack.ml.utils.VolatileCursorIterator;
@@ -84,20 +82,25 @@ void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener<Long> li
8482

8583
latestSnapshotTimeStamp(jobId, ActionListener.wrap(
8684
latestTime -> {
87-
long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis();
88-
threadedActionListener.onResponse(cutoff);
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+
}
8991
},
9092
listener::onFailure
9193
));
9294
}
9395

9496
private void latestSnapshotTimeStamp(String jobId, ActionListener<Long> listener) {
9597
SortBuilder<?> sortBuilder = new FieldSortBuilder(ModelSnapshot.TIMESTAMP.getPreferredName()).order(SortOrder.DESC);
96-
QueryBuilder bucketType = QueryBuilders.termQuery(Result.RESULT_TYPE.getPreferredName(), Bucket.RESULT_TYPE_VALUE);
98+
QueryBuilder snapshotQuery = QueryBuilders.boolQuery()
99+
.filter(QueryBuilders.existsQuery(ModelSnapshot.SNAPSHOT_DOC_COUNT.getPreferredName()));
97100

98101
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
99102
searchSourceBuilder.sort(sortBuilder);
100-
searchSourceBuilder.query(bucketType);
103+
searchSourceBuilder.query(snapshotQuery);
101104
searchSourceBuilder.size(1);
102105
searchSourceBuilder.trackTotalHits(false);
103106

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/job/retention/ExpiredResultsRemover.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ void calcCutoffEpochMs(String jobId, long retentionDays, ActionListener<Long> li
136136
MachineLearning.UTILITY_THREAD_POOL_NAME, listener, false);
137137
latestBucketTime(jobId, ActionListener.wrap(
138138
latestTime -> {
139-
long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis();
140-
threadedActionListener.onResponse(cutoff);
139+
if (latestTime == null) {
140+
threadedActionListener.onResponse(null);
141+
} else {
142+
long cutoff = latestTime - new TimeValue(retentionDays, TimeUnit.DAYS).getMillis();
143+
threadedActionListener.onResponse(cutoff);
144+
}
141145
},
142146
listener::onFailure
143147
));

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/retention/AbstractExpiredJobDataRemoverTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ static SearchResponse createSearchResponse(List<? extends ToXContent> toXContent
8282
static void givenJobs(Client client, List<Job> jobs) throws IOException {
8383
SearchResponse response = AbstractExpiredJobDataRemoverTests.createSearchResponse(jobs);
8484

85-
doAnswer(invocationOnMock -> {
86-
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocationOnMock.getArguments()[2];
87-
listener.onResponse(response);
88-
return null;
85+
doAnswer(invocationOnMock -> {
86+
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) invocationOnMock.getArguments()[2];
87+
listener.onResponse(response);
88+
return null;
8989
}).when(client).execute(eq(SearchAction.INSTANCE), any(), any());
9090
}
9191

0 commit comments

Comments
 (0)