Skip to content

Commit bcfdcca

Browse files
authored
Use dedicated ML APIs in tests (#30941)
ML has dedicated APIs for datafeeds and jobs yet base test classes and some tests were relying on the cluster state for this state. This commit removes this usage in favor of using the dedicated endpoints.
1 parent 83a7ade commit bcfdcca

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/integration/MlRestTestStateCleaner.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package org.elasticsearch.xpack.core.ml.integration;
77

88
import org.apache.logging.log4j.Logger;
9+
import org.elasticsearch.client.Request;
10+
import org.elasticsearch.client.Response;
911
import org.elasticsearch.client.RestClient;
1012
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1113
import org.elasticsearch.test.rest.ESRestTestCase;
@@ -35,10 +37,12 @@ public void clearMlMetadata() throws IOException {
3537

3638
@SuppressWarnings("unchecked")
3739
private void deleteAllDatafeeds() throws IOException {
38-
Map<String, Object> clusterStateAsMap = testCase.entityAsMap(adminClient.performRequest("GET", "/_cluster/state",
39-
Collections.singletonMap("filter_path", "metadata.ml.datafeeds")));
40-
List<Map<String, Object>> datafeeds =
41-
(List<Map<String, Object>>) XContentMapValues.extractValue("metadata.ml.datafeeds", clusterStateAsMap);
40+
final Request datafeedsRequest = new Request("GET", "/_xpack/ml/datafeeds");
41+
datafeedsRequest.addParameter("filter_path", "datafeeds");
42+
final Response datafeedsResponse = adminClient.performRequest(datafeedsRequest);
43+
@SuppressWarnings("unchecked")
44+
final List<Map<String, Object>> datafeeds =
45+
(List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", testCase.entityAsMap(datafeedsResponse));
4246
if (datafeeds == null) {
4347
return;
4448
}
@@ -75,11 +79,12 @@ private void deleteAllDatafeeds() throws IOException {
7579
}
7680

7781
private void deleteAllJobs() throws IOException {
78-
Map<String, Object> clusterStateAsMap = testCase.entityAsMap(adminClient.performRequest("GET", "/_cluster/state",
79-
Collections.singletonMap("filter_path", "metadata.ml.jobs")));
82+
final Request jobsRequest = new Request("GET", "/_xpack/ml/anomaly_detectors");
83+
jobsRequest.addParameter("filter_path", "jobs");
84+
final Response response = adminClient.performRequest(jobsRequest);
8085
@SuppressWarnings("unchecked")
81-
List<Map<String, Object>> jobConfigs =
82-
(List<Map<String, Object>>) XContentMapValues.extractValue("metadata.ml.jobs", clusterStateAsMap);
86+
final List<Map<String, Object>> jobConfigs =
87+
(List<Map<String, Object>>) XContentMapValues.extractValue("jobs", testCase.entityAsMap(response));
8388
if (jobConfigs == null) {
8489
return;
8590
}

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import org.elasticsearch.test.MockHttpTransport;
2828
import org.elasticsearch.test.discovery.TestZenDiscovery;
2929
import org.elasticsearch.xpack.core.XPackSettings;
30+
import org.elasticsearch.xpack.core.ml.action.GetDatafeedsAction;
31+
import org.elasticsearch.xpack.core.ml.action.GetJobsAction;
32+
import org.elasticsearch.xpack.core.ml.action.util.QueryPage;
33+
import org.elasticsearch.xpack.core.ml.client.MachineLearningClient;
3034
import org.elasticsearch.xpack.ml.LocalStateMachineLearning;
3135
import org.elasticsearch.xpack.ml.MachineLearning;
3236
import org.elasticsearch.xpack.core.ml.MachineLearningField;
@@ -271,7 +275,9 @@ public static GetDatafeedsStatsAction.Response.DatafeedStats getDatafeedStats(St
271275
}
272276

273277
public static void deleteAllDatafeeds(Logger logger, Client client) throws Exception {
274-
MlMetadata mlMetadata = MlMetadata.getMlMetadata(client.admin().cluster().prepareState().get().getState());
278+
final MachineLearningClient mlClient = new MachineLearningClient(client);
279+
final QueryPage<DatafeedConfig> datafeeds =
280+
mlClient.getDatafeeds(new GetDatafeedsAction.Request(GetDatafeedsAction.ALL)).actionGet().getResponse();
275281
try {
276282
logger.info("Closing all datafeeds (using _all)");
277283
StopDatafeedAction.Response stopResponse = client
@@ -292,25 +298,25 @@ public static void deleteAllDatafeeds(Logger logger, Client client) throws Excep
292298
"Had to resort to force-stopping datafeed, something went wrong?", e1);
293299
}
294300

295-
for (DatafeedConfig datafeed : mlMetadata.getDatafeeds().values()) {
296-
String datafeedId = datafeed.getId();
301+
for (final DatafeedConfig datafeed : datafeeds.results()) {
297302
assertBusy(() -> {
298303
try {
299-
GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeedId);
304+
GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeed.getId());
300305
GetDatafeedsStatsAction.Response r = client.execute(GetDatafeedsStatsAction.INSTANCE, request).get();
301306
assertThat(r.getResponse().results().get(0).getDatafeedState(), equalTo(DatafeedState.STOPPED));
302307
} catch (InterruptedException | ExecutionException e) {
303308
throw new RuntimeException(e);
304309
}
305310
});
306311
DeleteDatafeedAction.Response deleteResponse =
307-
client.execute(DeleteDatafeedAction.INSTANCE, new DeleteDatafeedAction.Request(datafeedId)).get();
312+
client.execute(DeleteDatafeedAction.INSTANCE, new DeleteDatafeedAction.Request(datafeed.getId())).get();
308313
assertTrue(deleteResponse.isAcknowledged());
309314
}
310315
}
311316

312317
public static void deleteAllJobs(Logger logger, Client client) throws Exception {
313-
MlMetadata mlMetadata = MlMetadata.getMlMetadata(client.admin().cluster().prepareState().get().getState());
318+
final MachineLearningClient mlClient = new MachineLearningClient(client);
319+
final QueryPage<Job> jobs = mlClient.getJobs(new GetJobsAction.Request(MetaData.ALL)).actionGet().getResponse();
314320

315321
try {
316322
CloseJobAction.Request closeRequest = new CloseJobAction.Request(MetaData.ALL);
@@ -334,15 +340,14 @@ public static void deleteAllJobs(Logger logger, Client client) throws Exception
334340
e1);
335341
}
336342

337-
for (Map.Entry<String, Job> entry : mlMetadata.getJobs().entrySet()) {
338-
String jobId = entry.getKey();
343+
for (final Job job : jobs.results()) {
339344
assertBusy(() -> {
340345
GetJobsStatsAction.Response statsResponse =
341-
client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet();
346+
client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet();
342347
assertEquals(JobState.CLOSED, statsResponse.getResponse().results().get(0).getState());
343348
});
344349
DeleteJobAction.Response response =
345-
client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(jobId)).get();
350+
client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(job.getId())).get();
346351
assertTrue(response.isAcknowledged());
347352
}
348353
}

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/jobs_crud.yml

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,9 @@
548548
- do:
549549
headers:
550550
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
551-
cluster.state:
552-
metric: [ metadata ]
553-
filter_path: metadata.persistent_tasks
554-
- match: {"metadata.persistent_tasks.tasks.0.task.xpack/ml/job.status.state": opened}
551+
xpack.ml.get_job_stats:
552+
job_id: jobs-crud-close-job
553+
- match: {"jobs.0.state": opened}
555554

556555
- do:
557556
xpack.ml.close_job:
@@ -561,11 +560,9 @@
561560
- do:
562561
headers:
563562
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
564-
cluster.state:
565-
metric: [ metadata ]
566-
filter_path: metadata.persistent_tasks
567-
- match:
568-
metadata.persistent_tasks.tasks: []
563+
xpack.ml.get_job_stats:
564+
job_id: jobs-crud-close-job
565+
- match: {"jobs.0.state": closed}
569566

570567
---
571568
"Test closing a closed job isn't an error":
@@ -789,10 +786,9 @@
789786
- do:
790787
headers:
791788
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
792-
cluster.state:
793-
metric: [ metadata ]
794-
filter_path: metadata.persistent_tasks
795-
- match: {"metadata.persistent_tasks.tasks.0.task.xpack/ml/job.status.state": opened}
789+
xpack.ml.get_job_stats:
790+
job_id: jobs-crud-force-close-job
791+
- match: {"jobs.0.state": opened}
796792

797793
- do:
798794
xpack.ml.close_job:
@@ -803,11 +799,9 @@
803799
- do:
804800
headers:
805801
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
806-
cluster.state:
807-
metric: [ metadata ]
808-
filter_path: metadata.persistent_tasks
809-
- match:
810-
metadata.persistent_tasks.tasks: []
802+
xpack.ml.get_job_stats:
803+
job_id: jobs-crud-force-close-job
804+
- match: {"jobs.0.state": closed}
811805

812806
---
813807
"Test force closing a closed job isn't an error":

0 commit comments

Comments
 (0)