Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Job in index: delete filter action #34642

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,63 @@
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.ml.action.DeleteFilterAction;
import org.elasticsearch.xpack.core.ml.MlMetaIndex;
import org.elasticsearch.xpack.core.ml.MlMetadata;
import org.elasticsearch.xpack.core.ml.action.DeleteFilterAction;
import org.elasticsearch.xpack.core.ml.job.config.Detector;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.config.MlFilter;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.ml.job.persistence.JobConfigProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN;
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;

public class TransportDeleteFilterAction extends HandledTransportAction<DeleteFilterAction.Request, AcknowledgedResponse> {

private final Client client;
private final ClusterService clusterService;
private final JobConfigProvider jobConfigProvider;

@Inject
public TransportDeleteFilterAction(Settings settings, ThreadPool threadPool,
TransportService transportService, ActionFilters actionFilters,
IndexNameExpressionResolver indexNameExpressionResolver,
ClusterService clusterService, Client client) {
Client client, JobConfigProvider jobConfigProvider) {
super(settings, DeleteFilterAction.NAME, threadPool, transportService, actionFilters,
indexNameExpressionResolver, DeleteFilterAction.Request::new);
this.clusterService = clusterService;
this.client = client;
this.jobConfigProvider = jobConfigProvider;
}

@Override
protected void doExecute(DeleteFilterAction.Request request, ActionListener<AcknowledgedResponse> listener) {

final String filterId = request.getFilterId();
ClusterState state = clusterService.state();
Map<String, Job> jobs = MlMetadata.getMlMetadata(state).getJobs();
jobConfigProvider.findJobsWithCustomRules(ActionListener.wrap(
jobs-> {
List<String> currentlyUsedBy = findJobsUsingFilter(jobs, filterId);
if (!currentlyUsedBy.isEmpty()) {
listener.onFailure(ExceptionsHelper.conflictStatusException(
"Cannot delete filter, currently used by jobs: " + currentlyUsedBy));
} else {
deleteFilter(filterId, listener);
}
},
listener::onFailure
)
);
}

private static List<String> findJobsUsingFilter(List<Job> jobs, String filterId) {
List<String> currentlyUsedBy = new ArrayList<>();
for (Job job : jobs.values()) {
for (Job job : jobs) {
List<Detector> detectors = job.getAnalysisConfig().getDetectors();
for (Detector detector : detectors) {
if (detector.extractReferencedFilters().contains(filterId)) {
Expand All @@ -71,31 +81,31 @@ protected void doExecute(DeleteFilterAction.Request request, ActionListener<Ackn
}
}
}
if (!currentlyUsedBy.isEmpty()) {
throw ExceptionsHelper.conflictStatusException("Cannot delete filter, currently used by jobs: "
+ currentlyUsedBy);
}
return currentlyUsedBy;
}

DeleteRequest deleteRequest = new DeleteRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE, MlFilter.documentId(filterId));
private void deleteFilter(String filterId, ActionListener<AcknowledgedResponse> listener) {
DeleteRequest deleteRequest = new DeleteRequest(MlMetaIndex.INDEX_NAME, MlMetaIndex.TYPE,
MlFilter.documentId(filterId));
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
bulkRequestBuilder.add(deleteRequest);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why we use a bulk request containing a single request rather than using delete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall ES pushing towards always using bulk instead of isolated doc actions. I recall deprecations as well. Doesn't seem to be there now though, at least not for delete. In any case, I'd rather leave it as is to minimise churn.

bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
executeAsyncWithOrigin(client, ML_ORIGIN, BulkAction.INSTANCE, bulkRequestBuilder.request(),
new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkResponse) {
if (bulkResponse.getItems()[0].status() == RestStatus.NOT_FOUND) {
listener.onFailure(new ResourceNotFoundException("Could not delete filter with ID [" + filterId
+ "] because it does not exist"));
} else {
listener.onResponse(new AcknowledgedResponse(true));
}
new ActionListener<BulkResponse>() {
@Override
public void onResponse(BulkResponse bulkResponse) {
if (bulkResponse.getItems()[0].status() == RestStatus.NOT_FOUND) {
listener.onFailure(new ResourceNotFoundException("Could not delete filter with ID [" + filterId
+ "] because it does not exist"));
} else {
listener.onResponse(new AcknowledgedResponse(true));
}
}

@Override
public void onFailure(Exception e) {
listener.onFailure(ExceptionsHelper.serverError("Could not delete filter with ID [" + filterId + "]", e));
}
});
@Override
public void onFailure(Exception e) {
listener.onFailure(ExceptionsHelper.serverError("Could not delete filter with ID [" + filterId + "]", e));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void putJob(Job job, ActionListener<IndexResponse> listener) {
ElasticsearchMappings.DOC_TYPE, Job.documentId(job.getId()))
.setSource(source)
.setOpType(DocWriteRequest.OpType.CREATE)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
.request();

executeAsyncWithOrigin(client, ML_ORIGIN, IndexAction.INSTANCE, indexRequest, ActionListener.wrap(
Expand Down