Skip to content

Commit fdc1bdd

Browse files
committed
[ML][TEST] Fix randomly failing HLRC test (#40973)
Made changes to ensure that unique IDs are generated for model snapshots used by the deleteExpiredDataTest test in the MachineLearningIT suite. Previously a sleep of 1s was performed between jobs under the assumption that this would be sufficient to guarantee that the timestamps used in the composition of the snapshot IDs would be different. The new approach is to wait on the condition that the old and new timestamps are in fact different (to 1s resolution).
1 parent 2ac514b commit fdc1bdd

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,18 @@ private String createExpiredData(String jobId) throws Exception {
878878

879879
waitForJobToClose(jobId);
880880

881+
long prevJobTimeStamp = System.currentTimeMillis() / 1000;
882+
883+
// Check that the current timestamp component, in seconds, differs from previously.
884+
// Note that we used to use an 'awaitBusy(() -> false, 1, TimeUnit.SECONDS);'
885+
// for the same purpose but the new approach...
886+
// a) explicitly checks that the timestamps, in seconds, are actually different and
887+
// b) is slightly more efficient since we may not need to wait an entire second for the timestamp to increment
888+
assertBusy(() -> {
889+
long timeNow = System.currentTimeMillis() / 1000;
890+
assertFalse(prevJobTimeStamp >= timeNow);
891+
});
892+
881893
// Update snapshot timestamp to force it out of snapshot retention window
882894
long oneDayAgo = nowMillis - TimeValue.timeValueHours(24).getMillis() - 1;
883895
updateModelSnapshotTimestamp(jobId, String.valueOf(oneDayAgo));
@@ -1418,6 +1430,7 @@ private void startDatafeed(String datafeedId, String start, String end) throws E
14181430
}
14191431

14201432
private void updateModelSnapshotTimestamp(String jobId, String timestamp) throws Exception {
1433+
14211434
MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
14221435

14231436
GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId);
@@ -1435,9 +1448,6 @@ private void updateModelSnapshotTimestamp(String jobId, String timestamp) throws
14351448
UpdateRequest updateSnapshotRequest = new UpdateRequest(".ml-anomalies-" + jobId, "_doc", documentId);
14361449
updateSnapshotRequest.doc(snapshotUpdate.getBytes(StandardCharsets.UTF_8), XContentType.JSON);
14371450
highLevelClient().update(updateSnapshotRequest, RequestOptions.DEFAULT);
1438-
1439-
// Wait a second to ensure subsequent model snapshots will have a different ID (it depends on epoch seconds)
1440-
awaitBusy(() -> false, 1, TimeUnit.SECONDS);
14411451
}
14421452

14431453

0 commit comments

Comments
 (0)