Skip to content

Commit 54b33f0

Browse files
committed
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 213bb25 commit 54b33f0

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
@@ -26,6 +26,10 @@
2626
import org.elasticsearch.test.ESIntegTestCase;
2727
import org.elasticsearch.test.discovery.TestZenDiscovery;
2828
import org.elasticsearch.xpack.core.XPackSettings;
29+
import org.elasticsearch.xpack.core.ml.action.GetDatafeedsAction;
30+
import org.elasticsearch.xpack.core.ml.action.GetJobsAction;
31+
import org.elasticsearch.xpack.core.ml.action.util.QueryPage;
32+
import org.elasticsearch.xpack.core.ml.client.MachineLearningClient;
2933
import org.elasticsearch.xpack.ml.LocalStateMachineLearning;
3034
import org.elasticsearch.xpack.ml.MachineLearning;
3135
import org.elasticsearch.xpack.core.ml.MachineLearningField;
@@ -270,7 +274,9 @@ public static GetDatafeedsStatsAction.Response.DatafeedStats getDatafeedStats(St
270274
}
271275

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

294-
for (DatafeedConfig datafeed : mlMetadata.getDatafeeds().values()) {
295-
String datafeedId = datafeed.getId();
300+
for (final DatafeedConfig datafeed : datafeeds.results()) {
296301
assertBusy(() -> {
297302
try {
298-
GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeedId);
303+
GetDatafeedsStatsAction.Request request = new GetDatafeedsStatsAction.Request(datafeed.getId());
299304
GetDatafeedsStatsAction.Response r = client.execute(GetDatafeedsStatsAction.INSTANCE, request).get();
300305
assertThat(r.getResponse().results().get(0).getDatafeedState(), equalTo(DatafeedState.STOPPED));
301306
} catch (InterruptedException | ExecutionException e) {
302307
throw new RuntimeException(e);
303308
}
304309
});
305310
DeleteDatafeedAction.Response deleteResponse =
306-
client.execute(DeleteDatafeedAction.INSTANCE, new DeleteDatafeedAction.Request(datafeedId)).get();
311+
client.execute(DeleteDatafeedAction.INSTANCE, new DeleteDatafeedAction.Request(datafeed.getId())).get();
307312
assertTrue(deleteResponse.isAcknowledged());
308313
}
309314
}
310315

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

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

336-
for (Map.Entry<String, Job> entry : mlMetadata.getJobs().entrySet()) {
337-
String jobId = entry.getKey();
342+
for (final Job job : jobs.results()) {
338343
assertBusy(() -> {
339344
GetJobsStatsAction.Response statsResponse =
340-
client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(jobId)).actionGet();
345+
client().execute(GetJobsStatsAction.INSTANCE, new GetJobsStatsAction.Request(job.getId())).actionGet();
341346
assertEquals(JobState.CLOSED, statsResponse.getResponse().results().get(0).getState());
342347
});
343348
DeleteJobAction.Response response =
344-
client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(jobId)).get();
349+
client.execute(DeleteJobAction.INSTANCE, new DeleteJobAction.Request(job.getId())).get();
345350
assertTrue(response.isAcknowledged());
346351
}
347352
}

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)