-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
1 parent
67185fc
commit bf035f2
Showing
5 changed files
with
106 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 0 additions & 109 deletions
109
client/rest-high-level/src/test/java/org/elasticsearch/client/MlRestTestStateCleaner.java
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
client/rest-high-level/src/test/java/org/elasticsearch/client/MlTestStateCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.client.ml.CloseJobRequest; | ||
import org.elasticsearch.client.ml.DeleteDatafeedRequest; | ||
import org.elasticsearch.client.ml.DeleteJobRequest; | ||
import org.elasticsearch.client.ml.GetDatafeedRequest; | ||
import org.elasticsearch.client.ml.GetDatafeedResponse; | ||
import org.elasticsearch.client.ml.GetJobRequest; | ||
import org.elasticsearch.client.ml.GetJobResponse; | ||
import org.elasticsearch.client.ml.StopDatafeedRequest; | ||
import org.elasticsearch.client.ml.datafeed.DatafeedConfig; | ||
import org.elasticsearch.client.ml.job.config.Job; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Cleans up and ML resources created during tests | ||
*/ | ||
public class MlTestStateCleaner { | ||
|
||
private final Logger logger; | ||
private final MachineLearningClient mlClient; | ||
|
||
public MlTestStateCleaner(Logger logger, MachineLearningClient mlClient) { | ||
this.logger = logger; | ||
this.mlClient = mlClient; | ||
} | ||
|
||
public void clearMlMetadata() throws IOException { | ||
deleteAllDatafeeds(); | ||
deleteAllJobs(); | ||
} | ||
|
||
private void deleteAllDatafeeds() throws IOException { | ||
stopAllDatafeeds(); | ||
|
||
GetDatafeedResponse getDatafeedResponse = mlClient.getDatafeed(GetDatafeedRequest.getAllDatafeedsRequest(), RequestOptions.DEFAULT); | ||
for (DatafeedConfig datafeed : getDatafeedResponse.datafeeds()) { | ||
mlClient.deleteDatafeed(new DeleteDatafeedRequest(datafeed.getId()), RequestOptions.DEFAULT); | ||
} | ||
} | ||
|
||
private void stopAllDatafeeds() { | ||
StopDatafeedRequest stopAllDatafeedsRequest = StopDatafeedRequest.stopAllDatafeedsRequest(); | ||
try { | ||
mlClient.stopDatafeed(stopAllDatafeedsRequest, RequestOptions.DEFAULT); | ||
} catch (Exception e1) { | ||
logger.warn("failed to stop all datafeeds. Forcing stop", e1); | ||
try { | ||
stopAllDatafeedsRequest.setForce(true); | ||
mlClient.stopDatafeed(stopAllDatafeedsRequest, RequestOptions.DEFAULT); | ||
} catch (Exception e2) { | ||
logger.warn("Force-closing all data feeds failed", e2); | ||
} | ||
throw new RuntimeException("Had to resort to force-stopping datafeeds, something went wrong?", e1); | ||
} | ||
} | ||
|
||
private void deleteAllJobs() throws IOException { | ||
closeAllJobs(); | ||
|
||
GetJobResponse getJobResponse = mlClient.getJob(GetJobRequest.getAllJobsRequest(), RequestOptions.DEFAULT); | ||
for (Job job : getJobResponse.jobs()) { | ||
mlClient.deleteJob(new DeleteJobRequest(job.getId()), RequestOptions.DEFAULT); | ||
} | ||
} | ||
|
||
private void closeAllJobs() { | ||
CloseJobRequest closeAllJobsRequest = CloseJobRequest.closeAllJobsRequest(); | ||
try { | ||
mlClient.closeJob(closeAllJobsRequest, RequestOptions.DEFAULT); | ||
} catch (Exception e1) { | ||
logger.warn("failed to close all jobs. Forcing closed", e1); | ||
closeAllJobsRequest.setForce(true); | ||
try { | ||
mlClient.closeJob(closeAllJobsRequest, RequestOptions.DEFAULT); | ||
} catch (Exception e2) { | ||
logger.warn("Force-closing all jobs failed", e2); | ||
} | ||
throw new RuntimeException("Had to resort to force-closing jobs, something went wrong?", e1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters