Skip to content

Commit

Permalink
[ML][TEST] Fix randomly failing HLRC test (elastic#40973)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
edsavage committed Apr 9, 2019
1 parent 2ac514b commit fdc1bdd
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,18 @@ private String createExpiredData(String jobId) throws Exception {

waitForJobToClose(jobId);

long prevJobTimeStamp = System.currentTimeMillis() / 1000;

// Check that the current timestamp component, in seconds, differs from previously.
// Note that we used to use an 'awaitBusy(() -> false, 1, TimeUnit.SECONDS);'
// for the same purpose but the new approach...
// a) explicitly checks that the timestamps, in seconds, are actually different and
// b) is slightly more efficient since we may not need to wait an entire second for the timestamp to increment
assertBusy(() -> {
long timeNow = System.currentTimeMillis() / 1000;
assertFalse(prevJobTimeStamp >= timeNow);
});

// Update snapshot timestamp to force it out of snapshot retention window
long oneDayAgo = nowMillis - TimeValue.timeValueHours(24).getMillis() - 1;
updateModelSnapshotTimestamp(jobId, String.valueOf(oneDayAgo));
Expand Down Expand Up @@ -1418,6 +1430,7 @@ private void startDatafeed(String datafeedId, String start, String end) throws E
}

private void updateModelSnapshotTimestamp(String jobId, String timestamp) throws Exception {

MachineLearningClient machineLearningClient = highLevelClient().machineLearning();

GetModelSnapshotsRequest getModelSnapshotsRequest = new GetModelSnapshotsRequest(jobId);
Expand All @@ -1435,9 +1448,6 @@ private void updateModelSnapshotTimestamp(String jobId, String timestamp) throws
UpdateRequest updateSnapshotRequest = new UpdateRequest(".ml-anomalies-" + jobId, "_doc", documentId);
updateSnapshotRequest.doc(snapshotUpdate.getBytes(StandardCharsets.UTF_8), XContentType.JSON);
highLevelClient().update(updateSnapshotRequest, RequestOptions.DEFAULT);

// Wait a second to ensure subsequent model snapshots will have a different ID (it depends on epoch seconds)
awaitBusy(() -> false, 1, TimeUnit.SECONDS);
}


Expand Down

0 comments on commit fdc1bdd

Please sign in to comment.