Skip to content

Commit 609ccaa

Browse files
[ML][HLRC] Replace REST-based ML test cleanup with the ML client (#34109)
Now that all basic APIs for managing jobs and datafeeds have been implemented we replace the duplicated `MlRestTestStateCleaner` with an implementation that uses the HLRC Machine Learning client itself.
1 parent 269ae0b commit 609ccaa

File tree

5 files changed

+106
-114
lines changed

5 files changed

+106
-114
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void addCategoriesIndexRequests(BulkRequest bulkRequest) {
147147

148148
@After
149149
public void deleteJob() throws IOException {
150-
new MlRestTestStateCleaner(logger, client()).clearMlMetadata();
150+
new MlTestStateCleaner(logger, highLevelClient().machineLearning()).clearMlMetadata();
151151
}
152152

153153
public void testGetCategories() throws IOException {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package org.elasticsearch.client;
2020

2121
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
22-
2322
import org.elasticsearch.ElasticsearchStatusException;
2423
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
2524
import org.elasticsearch.action.bulk.BulkRequest;
@@ -93,7 +92,7 @@ public class MachineLearningIT extends ESRestHighLevelClientTestCase {
9392

9493
@After
9594
public void cleanUp() throws IOException {
96-
new MlRestTestStateCleaner(logger, client()).clearMlMetadata();
95+
new MlTestStateCleaner(logger, highLevelClient().machineLearning()).clearMlMetadata();
9796
}
9897

9998
public void testPutJob() throws Exception {

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

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client;
20+
21+
import org.apache.logging.log4j.Logger;
22+
import org.elasticsearch.client.ml.CloseJobRequest;
23+
import org.elasticsearch.client.ml.DeleteDatafeedRequest;
24+
import org.elasticsearch.client.ml.DeleteJobRequest;
25+
import org.elasticsearch.client.ml.GetDatafeedRequest;
26+
import org.elasticsearch.client.ml.GetDatafeedResponse;
27+
import org.elasticsearch.client.ml.GetJobRequest;
28+
import org.elasticsearch.client.ml.GetJobResponse;
29+
import org.elasticsearch.client.ml.StopDatafeedRequest;
30+
import org.elasticsearch.client.ml.datafeed.DatafeedConfig;
31+
import org.elasticsearch.client.ml.job.config.Job;
32+
33+
import java.io.IOException;
34+
35+
/**
36+
* Cleans up and ML resources created during tests
37+
*/
38+
public class MlTestStateCleaner {
39+
40+
private final Logger logger;
41+
private final MachineLearningClient mlClient;
42+
43+
public MlTestStateCleaner(Logger logger, MachineLearningClient mlClient) {
44+
this.logger = logger;
45+
this.mlClient = mlClient;
46+
}
47+
48+
public void clearMlMetadata() throws IOException {
49+
deleteAllDatafeeds();
50+
deleteAllJobs();
51+
}
52+
53+
private void deleteAllDatafeeds() throws IOException {
54+
stopAllDatafeeds();
55+
56+
GetDatafeedResponse getDatafeedResponse = mlClient.getDatafeed(GetDatafeedRequest.getAllDatafeedsRequest(), RequestOptions.DEFAULT);
57+
for (DatafeedConfig datafeed : getDatafeedResponse.datafeeds()) {
58+
mlClient.deleteDatafeed(new DeleteDatafeedRequest(datafeed.getId()), RequestOptions.DEFAULT);
59+
}
60+
}
61+
62+
private void stopAllDatafeeds() {
63+
StopDatafeedRequest stopAllDatafeedsRequest = StopDatafeedRequest.stopAllDatafeedsRequest();
64+
try {
65+
mlClient.stopDatafeed(stopAllDatafeedsRequest, RequestOptions.DEFAULT);
66+
} catch (Exception e1) {
67+
logger.warn("failed to stop all datafeeds. Forcing stop", e1);
68+
try {
69+
stopAllDatafeedsRequest.setForce(true);
70+
mlClient.stopDatafeed(stopAllDatafeedsRequest, RequestOptions.DEFAULT);
71+
} catch (Exception e2) {
72+
logger.warn("Force-closing all data feeds failed", e2);
73+
}
74+
throw new RuntimeException("Had to resort to force-stopping datafeeds, something went wrong?", e1);
75+
}
76+
}
77+
78+
private void deleteAllJobs() throws IOException {
79+
closeAllJobs();
80+
81+
GetJobResponse getJobResponse = mlClient.getJob(GetJobRequest.getAllJobsRequest(), RequestOptions.DEFAULT);
82+
for (Job job : getJobResponse.jobs()) {
83+
mlClient.deleteJob(new DeleteJobRequest(job.getId()), RequestOptions.DEFAULT);
84+
}
85+
}
86+
87+
private void closeAllJobs() {
88+
CloseJobRequest closeAllJobsRequest = CloseJobRequest.closeAllJobsRequest();
89+
try {
90+
mlClient.closeJob(closeAllJobsRequest, RequestOptions.DEFAULT);
91+
} catch (Exception e1) {
92+
logger.warn("failed to close all jobs. Forcing closed", e1);
93+
closeAllJobsRequest.setForce(true);
94+
try {
95+
mlClient.closeJob(closeAllJobsRequest, RequestOptions.DEFAULT);
96+
} catch (Exception e2) {
97+
logger.warn("Force-closing all jobs failed", e2);
98+
}
99+
throw new RuntimeException("Had to resort to force-closing jobs, something went wrong?", e1);
100+
}
101+
}
102+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.client.ESRestHighLevelClientTestCase;
3131
import org.elasticsearch.client.MachineLearningGetResultsIT;
3232
import org.elasticsearch.client.MachineLearningIT;
33-
import org.elasticsearch.client.MlRestTestStateCleaner;
33+
import org.elasticsearch.client.MlTestStateCleaner;
3434
import org.elasticsearch.client.RequestOptions;
3535
import org.elasticsearch.client.RestHighLevelClient;
3636
import org.elasticsearch.client.ml.CloseJobRequest;
@@ -126,7 +126,7 @@ public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
126126

127127
@After
128128
public void cleanUp() throws IOException {
129-
new MlRestTestStateCleaner(logger, client()).clearMlMetadata();
129+
new MlTestStateCleaner(logger, highLevelClient().machineLearning()).clearMlMetadata();
130130
}
131131

132132
public void testCreateJob() throws Exception {

0 commit comments

Comments
 (0)