Skip to content

Commit badcb57

Browse files
committed
[ML] Wait for aliases in multi-node tests (#32086)
1 parent d223845 commit badcb57

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

x-pack/qa/ml-native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java

+40-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.apache.http.entity.ContentType;
99
import org.apache.http.entity.StringEntity;
10+
import org.elasticsearch.client.Request;
1011
import org.elasticsearch.client.Response;
1112
import org.elasticsearch.client.ResponseException;
1213
import org.elasticsearch.common.settings.Settings;
@@ -185,23 +186,32 @@ public void testCreateJobsWithIndexNameOption() throws Exception {
185186
+ "anomaly_detectors/" + jobId2, Collections.emptyMap(), new StringEntity(jobConfig, ContentType.APPLICATION_JSON));
186187
assertEquals(200, response.getStatusLine().getStatusCode());
187188

188-
response = client().performRequest("get", "_aliases");
189-
assertEquals(200, response.getStatusLine().getStatusCode());
190-
String responseAsString = responseEntityToString(response);
189+
// With security enabled GET _aliases throws an index_not_found_exception
190+
// if no aliases have been created. In multi-node tests the alias may not
191+
// appear immediately so wait here.
192+
assertBusy(() -> {
193+
try {
194+
Response aliasesResponse = client().performRequest("get", "_aliases");
195+
assertEquals(200, aliasesResponse.getStatusLine().getStatusCode());
196+
String responseAsString = responseEntityToString(aliasesResponse);
197+
assertThat(responseAsString,
198+
containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) + "\":{\"aliases\":{"));
199+
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1)
200+
+ "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId1 + "\",\"boost\":1.0}}}}"));
201+
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) + "\":{}"));
202+
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)
203+
+ "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId2 + "\",\"boost\":1.0}}}}"));
204+
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) + "\":{}"));
205+
} catch (ResponseException e) {
206+
throw new AssertionError(e);
207+
}
208+
});
191209

210+
Response indicesResponse = client().performRequest("get", "_cat/indices");
211+
assertEquals(200, indicesResponse.getStatusLine().getStatusCode());
212+
String responseAsString = responseEntityToString(indicesResponse);
192213
assertThat(responseAsString,
193-
containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) + "\":{\"aliases\":{"));
194-
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1)
195-
+ "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId1 + "\",\"boost\":1.0}}}}"));
196-
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) + "\":{}"));
197-
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2)
198-
+ "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" + jobId2 + "\",\"boost\":1.0}}}}"));
199-
assertThat(responseAsString, containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) + "\":{}"));
200-
201-
response = client().performRequest("get", "_cat/indices");
202-
assertEquals(200, response.getStatusLine().getStatusCode());
203-
responseAsString = responseEntityToString(response);
204-
assertThat(responseAsString, containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName));
214+
containsString(AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + "custom-" + indexName));
205215
assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId1))));
206216
assertThat(responseAsString, not(containsString(AnomalyDetectorsIndex.jobResultsAliasedName(jobId2))));
207217

@@ -438,23 +448,31 @@ public void testDeleteJobAfterMissingIndex() throws Exception {
438448
client().performRequest("get", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_stats"));
439449
}
440450

441-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/32034")
442451
public void testDeleteJobAfterMissingAliases() throws Exception {
443452
String jobId = "delete-job-after-missing-alias-job";
444453
String readAliasName = AnomalyDetectorsIndex.jobResultsAliasedName(jobId);
445454
String writeAliasName = AnomalyDetectorsIndex.resultsWriteAlias(jobId);
446455
String indexName = AnomalyDetectorsIndexFields.RESULTS_INDEX_PREFIX + AnomalyDetectorsIndexFields.RESULTS_INDEX_DEFAULT;
447456
createFarequoteJob(jobId);
448457

449-
Response response = client().performRequest("get", "_cat/aliases");
450-
assertEquals(200, response.getStatusLine().getStatusCode());
451-
String responseAsString = responseEntityToString(response);
452-
assertThat(responseAsString, containsString(readAliasName));
453-
assertThat(responseAsString, containsString(writeAliasName));
458+
// With security enabled cat aliases throws an index_not_found_exception
459+
// if no aliases have been created. In multi-node tests the alias may not
460+
// appear immediately so wait here.
461+
assertBusy(() -> {
462+
try {
463+
Response aliasesResponse = client().performRequest(new Request("get", "_cat/aliases"));
464+
assertEquals(200, aliasesResponse.getStatusLine().getStatusCode());
465+
String responseAsString = responseEntityToString(aliasesResponse);
466+
assertThat(responseAsString, containsString(readAliasName));
467+
assertThat(responseAsString, containsString(writeAliasName));
468+
} catch (ResponseException e) {
469+
throw new AssertionError(e);
470+
}
471+
});
454472

455473
// Manually delete the aliases so that we can test that deletion proceeds
456474
// normally anyway
457-
response = client().performRequest("delete", indexName + "/_alias/" + readAliasName);
475+
Response response = client().performRequest("delete", indexName + "/_alias/" + readAliasName);
458476
assertEquals(200, response.getStatusLine().getStatusCode());
459477
response = client().performRequest("delete", indexName + "/_alias/" + writeAliasName);
460478
assertEquals(200, response.getStatusLine().getStatusCode());

x-pack/qa/ml-native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/RestoreModelSnapshotIT.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ public void test() throws Exception {
7272
openJob(job.getId());
7373
String forecastId = forecast(job.getId(), TimeValue.timeValueHours(3), null);
7474
waitForecastToFinish(job.getId(), forecastId);
75-
ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastId);
76-
assertThat(forecastStats.getMessages(), anyOf(nullValue(), empty()));
77-
assertThat(forecastStats.getMemoryUsage(), greaterThan(0L));
78-
assertEquals(forecastStats.getRecordCount(), 3L);
75+
// In a multi-node cluster the replica may not be up to date
76+
// so wait for the change
77+
assertBusy(() -> {
78+
ForecastRequestStats forecastStats = getForecastStats(job.getId(), forecastId);
79+
assertThat(forecastStats.getMessages(), anyOf(nullValue(), empty()));
80+
assertThat(forecastStats.getMemoryUsage(), greaterThan(0L));
81+
assertThat(forecastStats.getRecordCount(), equalTo(3L));
82+
});
7983

8084
closeJob(job.getId());
8185

0 commit comments

Comments
 (0)