forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
299 additions
and
1 deletion.
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
84 changes: 84 additions & 0 deletions
84
...lugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/DeleteForecastAction.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,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.action; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
import org.elasticsearch.xpack.core.ml.job.process.autodetect.state.ModelSnapshotField; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
|
||
public class DeleteForecastAction extends Action<AcknowledgedResponse> { | ||
|
||
public static final DeleteForecastAction INSTANCE = new DeleteForecastAction(); | ||
public static final String NAME = "cluster:admin/xpack/ml/job/forecast/delete"; | ||
|
||
private DeleteForecastAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public AcknowledgedResponse newResponse() { | ||
return new AcknowledgedResponse(); | ||
} | ||
|
||
public static class Request extends ActionRequest { | ||
|
||
private String jobId; | ||
private String forecastId; | ||
|
||
public Request() { | ||
} | ||
|
||
public Request(String jobId, String forecastId) { | ||
this.jobId = ExceptionsHelper.requireNonNull(jobId, Job.ID.getPreferredName()); | ||
this.forecastId = ExceptionsHelper.requireNonNull(forecastId, ModelSnapshotField.SNAPSHOT_ID.getPreferredName()); | ||
} | ||
|
||
public String getJobId() { | ||
return jobId; | ||
} | ||
|
||
public String getForecastId() { | ||
return forecastId; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
jobId = in.readString(); | ||
forecastId = in.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(jobId); | ||
out.writeString(forecastId); | ||
} | ||
} | ||
|
||
public static class RequestBuilder extends ActionRequestBuilder<Request, AcknowledgedResponse> { | ||
|
||
public RequestBuilder(ElasticsearchClient client, DeleteForecastAction action) { | ||
super(client, action, new Request()); | ||
} | ||
} | ||
|
||
} |
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
File renamed without changes.
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
111 changes: 111 additions & 0 deletions
111
...gin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportDeleteForecastAction.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,111 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.action; | ||
|
||
import org.elasticsearch.ResourceNotFoundException; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.action.support.master.AcknowledgedResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.query.BoolQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilders; | ||
import org.elasticsearch.index.reindex.DeleteByQueryAction; | ||
import org.elasticsearch.index.reindex.DeleteByQueryRequest; | ||
import org.elasticsearch.search.builder.SearchSourceBuilder; | ||
import org.elasticsearch.tasks.Task; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.xpack.core.ml.action.DeleteForecastAction; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
import org.elasticsearch.xpack.core.ml.job.messages.Messages; | ||
import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; | ||
import org.elasticsearch.xpack.core.ml.job.results.Forecast; | ||
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; | ||
import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats.ForecastRequestStatus; | ||
import org.elasticsearch.xpack.core.ml.job.results.Result; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
import org.elasticsearch.xpack.ml.job.persistence.JobResultsProvider; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Set; | ||
|
||
import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; | ||
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; | ||
|
||
|
||
public class TransportDeleteForecastAction extends HandledTransportAction<DeleteForecastAction.Request, | ||
AcknowledgedResponse> { | ||
|
||
private static final String RESULTS_INDEX_PATTERN = AnomalyDetectorsIndex.jobResultsIndexPrefix() + "*"; | ||
private final Client client; | ||
private final JobResultsProvider jobResultsProvider; | ||
private static final Set<ForecastRequestStatus> DELETABLE_STATUSES = | ||
EnumSet.of(ForecastRequestStatus.FINISHED, ForecastRequestStatus.FAILED); | ||
|
||
@Inject | ||
public TransportDeleteForecastAction(Settings settings, TransportService transportService, ActionFilters actionFilters, Client client, | ||
JobResultsProvider jobResultsProvider) { | ||
super(settings, DeleteForecastAction.NAME, transportService, actionFilters, | ||
DeleteForecastAction.Request::new); | ||
this.client = client; | ||
this.jobResultsProvider = jobResultsProvider; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, DeleteForecastAction.Request request, ActionListener<AcknowledgedResponse> listener) { | ||
final String forecastId = request.getForecastId(); | ||
final String jobId = request.getJobId(); | ||
jobResultsProvider.getForecastRequestStats(jobId, forecastId, (forecastRequestStats) -> { | ||
if (forecastRequestStats == null) { | ||
listener.onFailure( | ||
new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, forecastId, jobId))); | ||
return; | ||
} | ||
|
||
if (DELETABLE_STATUSES.contains(forecastRequestStats.getStatus())) { | ||
DeleteByQueryRequest deleteByQueryRequest = buildDeleteByQuery(jobId, forecastId); | ||
executeAsyncWithOrigin(client, ML_ORIGIN, DeleteByQueryAction.INSTANCE, deleteByQueryRequest, ActionListener.wrap( | ||
response -> { | ||
if (response.getDeleted() > 0) { | ||
logger.info("Deleted forecast [{}] from job [{}]", forecastId, jobId); | ||
listener.onResponse(new AcknowledgedResponse(true)); | ||
} else { | ||
listener.onFailure( | ||
new ResourceNotFoundException(Messages.getMessage(Messages.REST_NO_SUCH_FORECAST, forecastId, jobId))); | ||
} | ||
}, | ||
listener::onFailure)); | ||
} else { | ||
listener.onFailure( | ||
ExceptionsHelper.conflictStatusException(Messages.getMessage(Messages.REST_BAD_FORECAST_STATE, forecastId, jobId))); | ||
} | ||
}, listener::onFailure); | ||
} | ||
|
||
private DeleteByQueryRequest buildDeleteByQuery(String jobId, String forecastId) { | ||
SearchRequest searchRequest = new SearchRequest(); | ||
// We need to create the DeleteByQueryRequest before we modify the SearchRequest | ||
// because the constructor of the former wipes the latter | ||
DeleteByQueryRequest request = new DeleteByQueryRequest(searchRequest); | ||
|
||
searchRequest.indices(RESULTS_INDEX_PATTERN); | ||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery() | ||
.minimumShouldMatch(1) | ||
.must(QueryBuilders.termsQuery(Result.RESULT_TYPE.getPreferredName(), | ||
ForecastRequestStats.RESULT_TYPE_VALUE, | ||
Forecast.RESULT_TYPE_VALUE)) | ||
.should(QueryBuilders.boolQuery() | ||
.must(QueryBuilders.termQuery(Job.ID.getPreferredName(), jobId)) | ||
.must(QueryBuilders.termQuery(Forecast.FORECAST_ID.getPreferredName(), forecastId))); | ||
QueryBuilder query = QueryBuilders.boolQuery().filter(boolQuery); | ||
searchRequest.source(new SearchSourceBuilder().query(query)); | ||
return request; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/job/RestDeleteForecastAction.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,44 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.ml.rest.job; | ||
|
||
import org.elasticsearch.client.node.NodeClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.RestController; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.action.RestToXContentListener; | ||
import org.elasticsearch.xpack.core.ml.action.DeleteForecastAction; | ||
import org.elasticsearch.xpack.core.ml.job.config.Job; | ||
import org.elasticsearch.xpack.core.ml.job.results.Forecast; | ||
import org.elasticsearch.xpack.ml.MachineLearning; | ||
|
||
import java.io.IOException; | ||
|
||
public class RestDeleteForecastAction extends BaseRestHandler { | ||
|
||
public RestDeleteForecastAction(Settings settings, RestController controller) { | ||
super(settings); | ||
controller.registerHandler(RestRequest.Method.DELETE, | ||
MachineLearning.BASE_PATH + | ||
"anomaly_detectors/{" + Job.ID.getPreferredName() + | ||
"}/_forecast/{" + Forecast.FORECAST_ID.getPreferredName() + "}", | ||
this); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "xpack_ml_delete_forecast_action"; | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { | ||
String jobId = restRequest.param(Job.ID.getPreferredName()); | ||
String forecastId = restRequest.param(Forecast.FORECAST_ID.getPreferredName()); | ||
final DeleteForecastAction.Request request = new DeleteForecastAction.Request(jobId, forecastId); | ||
return channel -> client.execute(DeleteForecastAction.INSTANCE, request, new RestToXContentListener<>(channel)); | ||
} | ||
} |