Skip to content

Commit 6ca4fb2

Browse files
authored
Replace DeleteIndexClusterStateUpdateRequest with private class (elastic#113509)
No need to extend `IndicesClusterStateUpdateRequest` here, nor to attach the listener to the request. Instead we can encapsulate the cluster state update task directly instead. Backport of elastic#113288 to 8.x
1 parent e4a174d commit 6ca4fb2

File tree

5 files changed

+92
-88
lines changed

5 files changed

+92
-88
lines changed

server/src/internalClusterTest/java/org/elasticsearch/snapshots/ConcurrentSnapshotsIT.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
1919
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus;
2020
import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse;
21-
import org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest;
2221
import org.elasticsearch.action.support.ActionTestUtils;
2322
import org.elasticsearch.action.support.GroupedActionListener;
2423
import org.elasticsearch.action.support.PlainActionFuture;
@@ -33,9 +32,7 @@
3332
import org.elasticsearch.common.util.concurrent.ListenableFuture;
3433
import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException;
3534
import org.elasticsearch.core.PathUtils;
36-
import org.elasticsearch.core.TimeValue;
3735
import org.elasticsearch.discovery.AbstractDisruptionTestCase;
38-
import org.elasticsearch.index.Index;
3936
import org.elasticsearch.plugins.Plugin;
4037
import org.elasticsearch.repositories.IndexId;
4138
import org.elasticsearch.repositories.RepositoryConflictException;
@@ -59,6 +56,7 @@
5956
import java.util.HashMap;
6057
import java.util.List;
6158
import java.util.Map;
59+
import java.util.Set;
6260
import java.util.concurrent.ExecutionException;
6361
import java.util.concurrent.TimeUnit;
6462
import java.util.stream.Collectors;
@@ -2214,12 +2212,17 @@ public void testDeleteIndexWithOutOfOrderFinalization() {
22142212
.anyMatch(e -> e.snapshot().getSnapshotId().getName().equals("snapshot-with-index-1") && e.state().completed())
22152213
)
22162214
// execute the index deletion _directly on the master_ so it happens before the snapshot finalization executes
2217-
.andThen(l -> masterDeleteIndexService.deleteIndices(new DeleteIndexClusterStateUpdateRequest(l.map(r -> {
2218-
assertTrue(r.isAcknowledged());
2219-
return null;
2220-
})).indices(new Index[] { internalCluster().clusterService().state().metadata().index(indexToDelete).getIndex() })
2221-
.ackTimeout(TimeValue.timeValueSeconds(10))
2222-
.masterNodeTimeout(TimeValue.timeValueSeconds(10))))
2215+
.andThen(
2216+
l -> masterDeleteIndexService.deleteIndices(
2217+
TEST_REQUEST_TIMEOUT,
2218+
TEST_REQUEST_TIMEOUT,
2219+
Set.of(internalCluster().clusterService().state().metadata().index(indexToDelete).getIndex()),
2220+
l.map(r -> {
2221+
assertTrue(r.isAcknowledged());
2222+
return null;
2223+
})
2224+
)
2225+
)
22232226
// ultimately create the index again so that taking a full snapshot will pick up any missing shard gen blob, and deleting that
22242227
// full snapshot will clean up all dangling shard-level blobs
22252228
.andThen((l, ignored) -> prepareCreate(indexToDelete, indexSettingsNoReplicas(1)).execute(l.map(r -> {

server/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexClusterStateUpdateRequest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/action/admin/indices/delete/TransportDeleteIndexAction.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,14 @@ protected void masterOperation(
9292
return;
9393
}
9494

95-
DeleteIndexClusterStateUpdateRequest deleteRequest = new DeleteIndexClusterStateUpdateRequest(listener.delegateResponse((l, e) -> {
96-
logger.debug(() -> "failed to delete indices [" + concreteIndices + "]", e);
97-
listener.onFailure(e);
98-
})).ackTimeout(request.ackTimeout()).masterNodeTimeout(request.masterNodeTimeout()).indices(concreteIndices.toArray(new Index[0]));
99-
100-
deleteIndexService.deleteIndices(deleteRequest);
95+
deleteIndexService.deleteIndices(
96+
request.masterNodeTimeout(),
97+
request.ackTimeout(),
98+
concreteIndices,
99+
listener.delegateResponse((l, e) -> {
100+
logger.debug(() -> "failed to delete indices [" + concreteIndices + "]", e);
101+
listener.onFailure(e);
102+
})
103+
);
101104
}
102105
}

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataDeleteIndexService.java

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,35 @@
1111

1212
import org.apache.logging.log4j.LogManager;
1313
import org.apache.logging.log4j.Logger;
14-
import org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest;
14+
import org.elasticsearch.action.ActionListener;
15+
import org.elasticsearch.action.support.master.AcknowledgedResponse;
1516
import org.elasticsearch.cluster.ClusterState;
1617
import org.elasticsearch.cluster.ClusterStateAckListener;
1718
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
19+
import org.elasticsearch.cluster.ClusterStateTaskListener;
1820
import org.elasticsearch.cluster.RestoreInProgress;
1921
import org.elasticsearch.cluster.SimpleBatchedAckListenerTaskExecutor;
2022
import org.elasticsearch.cluster.block.ClusterBlocks;
23+
import org.elasticsearch.cluster.node.DiscoveryNode;
2124
import org.elasticsearch.cluster.routing.RoutingTable;
2225
import org.elasticsearch.cluster.routing.allocation.AllocationService;
2326
import org.elasticsearch.cluster.service.ClusterService;
2427
import org.elasticsearch.cluster.service.MasterServiceTaskQueue;
2528
import org.elasticsearch.common.Priority;
2629
import org.elasticsearch.common.collect.ImmutableOpenMap;
2730
import org.elasticsearch.common.settings.Settings;
28-
import org.elasticsearch.common.util.set.Sets;
31+
import org.elasticsearch.core.TimeValue;
2932
import org.elasticsearch.core.Tuple;
3033
import org.elasticsearch.index.Index;
3134
import org.elasticsearch.injection.guice.Inject;
3235
import org.elasticsearch.snapshots.RestoreService;
3336
import org.elasticsearch.snapshots.SnapshotInProgressException;
3437
import org.elasticsearch.snapshots.SnapshotsService;
3538

36-
import java.util.Arrays;
3739
import java.util.HashMap;
3840
import java.util.HashSet;
3941
import java.util.Map;
42+
import java.util.Objects;
4043
import java.util.Set;
4144

4245
import static org.elasticsearch.cluster.routing.allocation.allocator.AllocationActionListener.rerouteCompletionIsNotRequired;
@@ -48,22 +51,19 @@ public class MetadataDeleteIndexService {
4851

4952
private static final Logger logger = LogManager.getLogger(MetadataDeleteIndexService.class);
5053

51-
private final Settings settings;
52-
5354
// package private for tests
54-
final ClusterStateTaskExecutor<DeleteIndexClusterStateUpdateRequest> executor;
55-
private final MasterServiceTaskQueue<DeleteIndexClusterStateUpdateRequest> taskQueue;
55+
final ClusterStateTaskExecutor<DeleteIndicesClusterStateUpdateTask> executor;
56+
private final MasterServiceTaskQueue<DeleteIndicesClusterStateUpdateTask> taskQueue;
5657

5758
@Inject
5859
public MetadataDeleteIndexService(Settings settings, ClusterService clusterService, AllocationService allocationService) {
59-
this.settings = settings;
6060
executor = new SimpleBatchedAckListenerTaskExecutor<>() {
6161
@Override
6262
public Tuple<ClusterState, ClusterStateAckListener> executeTask(
63-
DeleteIndexClusterStateUpdateRequest task,
63+
DeleteIndicesClusterStateUpdateTask task,
6464
ClusterState clusterState
6565
) {
66-
return Tuple.tuple(MetadataDeleteIndexService.deleteIndices(clusterState, Sets.newHashSet(task.indices()), settings), task);
66+
return Tuple.tuple(MetadataDeleteIndexService.deleteIndices(clusterState, task.indices, settings), task);
6767
}
6868

6969
@Override
@@ -81,11 +81,64 @@ public ClusterState afterBatchExecution(ClusterState clusterState, boolean clust
8181
taskQueue = clusterService.createTaskQueue("delete-index", Priority.URGENT, executor);
8282
}
8383

84-
public void deleteIndices(final DeleteIndexClusterStateUpdateRequest request) {
85-
if (request.indices() == null || request.indices().length == 0) {
86-
throw new IllegalArgumentException("Index name is required");
84+
public void deleteIndices(
85+
TimeValue masterNodeTimeout,
86+
TimeValue ackTimeout,
87+
Set<Index> indices,
88+
ActionListener<AcknowledgedResponse> listener
89+
) {
90+
if (indices == null || indices.isEmpty()) {
91+
throw new IllegalArgumentException("Indices are required");
92+
}
93+
taskQueue.submitTask(
94+
"delete-index " + indices,
95+
new DeleteIndicesClusterStateUpdateTask(indices, ackTimeout, listener),
96+
masterNodeTimeout
97+
);
98+
}
99+
100+
// package private for tests
101+
static class DeleteIndicesClusterStateUpdateTask implements ClusterStateTaskListener, ClusterStateAckListener {
102+
103+
private final Set<Index> indices;
104+
private final TimeValue ackTimeout;
105+
private final ActionListener<AcknowledgedResponse> listener;
106+
107+
DeleteIndicesClusterStateUpdateTask(Set<Index> indices, TimeValue ackTimeout, ActionListener<AcknowledgedResponse> listener) {
108+
this.indices = Objects.requireNonNull(indices);
109+
this.ackTimeout = Objects.requireNonNull(ackTimeout);
110+
this.listener = Objects.requireNonNull(listener);
111+
}
112+
113+
@Override
114+
public boolean mustAck(DiscoveryNode discoveryNode) {
115+
return true;
116+
}
117+
118+
@Override
119+
public void onAllNodesAcked() {
120+
listener.onResponse(AcknowledgedResponse.TRUE);
121+
}
122+
123+
@Override
124+
public void onAckFailure(Exception e) {
125+
listener.onResponse(AcknowledgedResponse.FALSE);
126+
}
127+
128+
@Override
129+
public void onAckTimeout() {
130+
listener.onResponse(AcknowledgedResponse.FALSE);
131+
}
132+
133+
@Override
134+
public TimeValue ackTimeout() {
135+
return ackTimeout;
136+
}
137+
138+
@Override
139+
public void onFailure(Exception e) {
140+
listener.onFailure(e);
87141
}
88-
taskQueue.submitTask("delete-index " + Arrays.toString(request.indices()), request, request.masterNodeTimeout());
89142
}
90143

91144
/**

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataDeleteIndexServiceTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
package org.elasticsearch.cluster.metadata;
1010

1111
import org.elasticsearch.action.ActionListener;
12-
import org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest;
1312
import org.elasticsearch.cluster.ClusterName;
1413
import org.elasticsearch.cluster.ClusterState;
1514
import org.elasticsearch.cluster.SnapshotsInProgress;
@@ -132,8 +131,10 @@ public void testDeleteUnassigned() throws Exception {
132131
before,
133132
service.executor,
134133
List.of(
135-
new DeleteIndexClusterStateUpdateRequest(ActionListener.noop()).indices(
136-
new Index[] { before.metadata().getIndices().get(index).getIndex() }
134+
new MetadataDeleteIndexService.DeleteIndicesClusterStateUpdateTask(
135+
Set.of(before.metadata().getIndices().get(index).getIndex()),
136+
TEST_REQUEST_TIMEOUT,
137+
ActionListener.noop()
137138
)
138139
)
139140
);

0 commit comments

Comments
 (0)