Skip to content

Commit d17ecb5

Browse files
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 <[email protected]>
1 parent 2071f85 commit d17ecb5

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;
@@ -126,7 +127,12 @@ protected void masterOperation(
126127
return;
127128
}
128129

129-
deleteIndicesAndPolicy(request.getName(), ActionListener.wrap((response) -> {
130+
GetIndexRequest indices = new GetIndexRequest().indices(EnrichPolicy.getBaseName(request.getName()) + "-*")
131+
.indicesOptions(IndicesOptions.lenientExpand());
132+
133+
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, indices);
134+
135+
deleteIndicesAndPolicy(concreteIndices, request.getName(), ActionListener.wrap((response) -> {
130136
enrichPolicyLocks.releasePolicy(request.getName());
131137
listener.onResponse(response);
132138
}, (exc) -> {
@@ -135,10 +141,15 @@ protected void masterOperation(
135141
}));
136142
}
137143

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

143154
client.admin().indices().delete(deleteRequest, ActionListener.wrap((response) -> {
144155
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
@@ -8,8 +8,10 @@
88

99
import org.elasticsearch.ResourceNotFoundException;
1010
import org.elasticsearch.action.ActionListener;
11+
import org.elasticsearch.action.support.DestructiveOperations;
1112
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1213
import org.elasticsearch.cluster.service.ClusterService;
14+
import org.elasticsearch.common.settings.Settings;
1315
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
1416
import org.elasticsearch.common.xcontent.XContentType;
1517
import org.elasticsearch.index.IndexNotFoundException;
@@ -23,6 +25,7 @@
2325
import java.util.concurrent.CountDownLatch;
2426
import java.util.concurrent.atomic.AtomicReference;
2527

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

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

@@ -151,6 +162,11 @@ public void onFailure(final Exception e) {
151162
.get()
152163
);
153164

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

0 commit comments

Comments
 (0)