|
| 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.common.xcontent.support.XContentMapValues; |
| 23 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * This is temporarily duplicated from the server side. |
| 31 | + * @TODO Replace with an implementation using the HLRC once |
| 32 | + * the APIs for managing datafeeds are implemented. |
| 33 | + */ |
| 34 | +public class MlRestTestStateCleaner { |
| 35 | + |
| 36 | + private final Logger logger; |
| 37 | + private final RestClient adminClient; |
| 38 | + |
| 39 | + public MlRestTestStateCleaner(Logger logger, RestClient adminClient) { |
| 40 | + this.logger = logger; |
| 41 | + this.adminClient = adminClient; |
| 42 | + } |
| 43 | + |
| 44 | + public void clearMlMetadata() throws IOException { |
| 45 | + deleteAllDatafeeds(); |
| 46 | + deleteAllJobs(); |
| 47 | + // indices will be deleted by the ESRestTestCase class |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + private void deleteAllDatafeeds() throws IOException { |
| 52 | + final Request datafeedsRequest = new Request("GET", "/_xpack/ml/datafeeds"); |
| 53 | + datafeedsRequest.addParameter("filter_path", "datafeeds"); |
| 54 | + final Response datafeedsResponse = adminClient.performRequest(datafeedsRequest); |
| 55 | + final List<Map<String, Object>> datafeeds = |
| 56 | + (List<Map<String, Object>>) XContentMapValues.extractValue("datafeeds", ESRestTestCase.entityAsMap(datafeedsResponse)); |
| 57 | + if (datafeeds == null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + adminClient.performRequest(new Request("POST", "/_xpack/ml/datafeeds/_all/_stop")); |
| 63 | + } catch (Exception e1) { |
| 64 | + logger.warn("failed to stop all datafeeds. Forcing stop", e1); |
| 65 | + try { |
| 66 | + adminClient.performRequest(new Request("POST", "/_xpack/ml/datafeeds/_all/_stop?force=true")); |
| 67 | + } catch (Exception e2) { |
| 68 | + logger.warn("Force-closing all data feeds failed", e2); |
| 69 | + } |
| 70 | + throw new RuntimeException( |
| 71 | + "Had to resort to force-stopping datafeeds, something went wrong?", e1); |
| 72 | + } |
| 73 | + |
| 74 | + for (Map<String, Object> datafeed : datafeeds) { |
| 75 | + String datafeedId = (String) datafeed.get("datafeed_id"); |
| 76 | + adminClient.performRequest(new Request("DELETE", "/_xpack/ml/datafeeds/" + datafeedId)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void deleteAllJobs() throws IOException { |
| 81 | + final Request jobsRequest = new Request("GET", "/_xpack/ml/anomaly_detectors"); |
| 82 | + jobsRequest.addParameter("filter_path", "jobs"); |
| 83 | + final Response response = adminClient.performRequest(jobsRequest); |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + final List<Map<String, Object>> jobConfigs = |
| 86 | + (List<Map<String, Object>>) XContentMapValues.extractValue("jobs", ESRestTestCase.entityAsMap(response)); |
| 87 | + if (jobConfigs == null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + adminClient.performRequest(new Request("POST", "/_xpack/ml/anomaly_detectors/_all/_close")); |
| 93 | + } catch (Exception e1) { |
| 94 | + logger.warn("failed to close all jobs. Forcing closed", e1); |
| 95 | + try { |
| 96 | + adminClient.performRequest(new Request("POST", "/_xpack/ml/anomaly_detectors/_all/_close?force=true")); |
| 97 | + } catch (Exception e2) { |
| 98 | + logger.warn("Force-closing all jobs failed", e2); |
| 99 | + } |
| 100 | + throw new RuntimeException("Had to resort to force-closing jobs, something went wrong?", |
| 101 | + e1); |
| 102 | + } |
| 103 | + |
| 104 | + for (Map<String, Object> jobConfig : jobConfigs) { |
| 105 | + String jobId = (String) jobConfig.get("job_id"); |
| 106 | + adminClient.performRequest(new Request("DELETE", "/_xpack/ml/anomaly_detectors/" + jobId)); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments