Skip to content

Commit a7ddbcf

Browse files
authored
Change the delete policy api to not pass wildcard expressions to the 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
1 parent 0ebd61b commit a7ddbcf

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyAction.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.ResourceNotFoundException;
1010
import org.elasticsearch.action.ActionListener;
1111
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
12+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
1213
import org.elasticsearch.action.support.ActionFilters;
1314
import org.elasticsearch.action.support.IndicesOptions;
1415
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -128,7 +129,12 @@ protected void masterOperation(
128129
return;
129130
}
130131

131-
deleteIndicesAndPolicy(request.getName(), ActionListener.wrap((response) -> {
132+
GetIndexRequest indices = new GetIndexRequest().indices(EnrichPolicy.getBaseName(request.getName()) + "-*")
133+
.indicesOptions(IndicesOptions.lenientExpand());
134+
135+
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, indices);
136+
137+
deleteIndicesAndPolicy(concreteIndices, request.getName(), ActionListener.wrap((response) -> {
132138
enrichPolicyLocks.releasePolicy(request.getName());
133139
listener.onResponse(response);
134140
}, (exc) -> {
@@ -137,10 +143,15 @@ protected void masterOperation(
137143
}));
138144
}
139145

140-
private void deleteIndicesAndPolicy(String name, ActionListener<AcknowledgedResponse> listener) {
141-
// delete all enrich indices for this policy
142-
DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(EnrichPolicy.getBaseName(name) + "-*")
143-
.indicesOptions(LENIENT_OPTIONS);
146+
private void deleteIndicesAndPolicy(String[] indices, String name, ActionListener<AcknowledgedResponse> listener) {
147+
if (indices.length == 0) {
148+
deletePolicy(name, listener);
149+
return;
150+
}
151+
152+
// delete all enrich indices for this policy, we delete concrete indices here but not a wildcard index expression
153+
// as the setting 'action.destructive_requires_name' may be set to true
154+
DeleteIndexRequest deleteRequest = new DeleteIndexRequest().indices(indices).indicesOptions(LENIENT_OPTIONS);
144155

145156
client.admin().indices().delete(deleteRequest, ActionListener.wrap((response) -> {
146157
if (response.isAcknowledged() == false) {

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/TransportDeleteEnrichPolicyActionTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import org.elasticsearch.ResourceNotFoundException;
1010
import org.elasticsearch.action.ActionListener;
1111
import org.elasticsearch.action.support.ActionTestUtils;
12+
import org.elasticsearch.action.support.DestructiveOperations;
1213
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1314
import org.elasticsearch.cluster.service.ClusterService;
15+
import org.elasticsearch.common.settings.Settings;
1416
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
1517
import org.elasticsearch.common.xcontent.XContentType;
1618
import org.elasticsearch.index.IndexNotFoundException;
@@ -24,6 +26,7 @@
2426
import java.util.concurrent.CountDownLatch;
2527
import java.util.concurrent.atomic.AtomicReference;
2628

29+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
2730
import static org.elasticsearch.xpack.enrich.EnrichPolicyTests.randomEnrichPolicy;
2831
import static org.hamcrest.Matchers.equalTo;
2932
import static org.hamcrest.Matchers.nullValue;
@@ -116,6 +119,14 @@ public void testDeleteIsNotLocked() throws Exception {
116119
AtomicReference<Exception> error = saveEnrichPolicy(name, policy, clusterService);
117120
assertThat(error.get(), nullValue());
118121

122+
boolean destructiveRequiresName = randomBoolean();
123+
if (destructiveRequiresName) {
124+
Settings settings = Settings.builder()
125+
.put(DestructiveOperations.REQUIRES_NAME_SETTING.getKey(), destructiveRequiresName)
126+
.build();
127+
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));
128+
}
129+
119130
createIndex(EnrichPolicy.getBaseName(name) + "-foo1");
120131
createIndex(EnrichPolicy.getBaseName(name) + "-foo2");
121132

@@ -152,6 +163,11 @@ public void onFailure(final Exception e) {
152163
.get()
153164
);
154165

166+
if (destructiveRequiresName) {
167+
Settings settings = Settings.builder().putNull(DestructiveOperations.REQUIRES_NAME_SETTING.getKey()).build();
168+
assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings));
169+
}
170+
155171
EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode(EnrichPolicyLocks.class);
156172
assertFalse(enrichPolicyLocks.captureExecutionState().isAnyPolicyInFlight());
157173

0 commit comments

Comments
 (0)