|
| 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 | +} |
0 commit comments