Change the delete policy api to not pass wildcard expressions to the delete index api (#52448)

Backport from #52179

Don't rely on the delete index api to resolve all the enrich indices for a particular enrich policy using a '[policy_name]-*' wildcard expression. With this change, the delete policy api will resolve the indices to remove and pass that directly to the delete index api.

This resolves a bug, that if `action.destructive_requires_name` setting has been set to true then the delete policy api is unable to remove the enrich indices related to the policy being deleted.

Closes #51228 

Co-authored-by: bellengao <gbl_long@163.com>
This commit is contained in:
Martijn van Groningen 2020-02-18 10:53:39 +01:00 committed by GitHub
parent 2071f85e1a
commit d17ecb5936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 5 deletions

View File

@ -9,6 +9,7 @@ import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@ -126,7 +127,12 @@ public class TransportDeleteEnrichPolicyAction extends TransportMasterNodeAction
return;
}
deleteIndicesAndPolicy(request.getName(), ActionListener.wrap((response) -> {
GetIndexRequest indices = new GetIndexRequest().indices(EnrichPolicy.getBaseName(request.getName()) + "-*")
.indicesOptions(IndicesOptions.lenientExpand());
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, indices);
deleteIndicesAndPolicy(concreteIndices, request.getName(), ActionListener.wrap((response) -> {
enrichPolicyLocks.releasePolicy(request.getName());
listener.onResponse(response);
}, (exc) -> {
@ -135,10 +141,15 @@ public class TransportDeleteEnrichPolicyAction extends TransportMasterNodeAction
}));
}
private void deleteIndicesAndPolicy(String name, ActionListener<AcknowledgedResponse> listener) {
// delete all enrich indices for this policy
DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(EnrichPolicy.getBaseName(name) + "-*")
.indicesOptions(LENIENT_OPTIONS);
private void deleteIndicesAndPolicy(String[] indices, String name, ActionListener<AcknowledgedResponse> listener) {
if (indices.length == 0) {
deletePolicy(name, listener);
return;
}
// delete all enrich indices for this policy, we delete concrete indices here but not a wildcard index expression
// as the setting 'action.destructive_requires_name' may be set to true
DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(indices).indicesOptions(LENIENT_OPTIONS);
client.admin().indices().delete(deleteRequest, ActionListener.wrap((response) -> {
if (response.isAcknowledged() == false) {

View File

@ -8,8 +8,10 @@ package org.elasticsearch.xpack.enrich.action;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.DestructiveOperations;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexNotFoundException;
@ -23,6 +25,7 @@ import org.junit.After;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@ -115,6 +118,14 @@ public class TransportDeleteEnrichPolicyActionTests extends AbstractEnrichTestCa
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
assertThat(error.get(), nullValue());
boolean destructiveRequiresName = randomBoolean();
if (destructiveRequiresName) {
Settings settings = Settings.builder()
.put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), destructiveRequiresName)
.build();
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));
}
createIndex(EnrichPolicy.getBaseName(name) + "-foo1");
createIndex(EnrichPolicy.getBaseName(name) + "-foo2");
@ -151,6 +162,11 @@ public class TransportDeleteEnrichPolicyActionTests extends AbstractEnrichTestCa
.get()
);
if (destructiveRequiresName) {
Settings settings = Settings.builder().putNull(DestructiveOperations.REQUIRES_NAME_SETTING.getKey()).build();
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));
}
EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode(EnrichPolicyLocks.class);
assertFalse(enrichPolicyLocks.captureExecutionState().isAnyPolicyInFlight());