Skip to content

Commit

Permalink
Change the delete policy api to not pass wildcard expressions to the …
Browse files Browse the repository at this point in the history
…delete index api (#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
  • Loading branch information
gaobinlong committed Feb 18, 2020
1 parent 0ebd61b commit a7ddbcf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -128,7 +129,12 @@ protected void masterOperation(
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) -> {
Expand All @@ -137,10 +143,15 @@ protected void masterOperation(
}));
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionTestUtils;
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;
Expand All @@ -24,6 +26,7 @@
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;
Expand Down Expand Up @@ -116,6 +119,14 @@ public void testDeleteIsNotLocked() throws Exception {
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");

Expand Down Expand Up @@ -152,6 +163,11 @@ public void onFailure(final Exception e) {
.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());

Expand Down

0 comments on commit a7ddbcf

Please sign in to comment.