Skip to content

Commit aa6a5c9

Browse files
committed
Also test non-existent allocation IDs
This commit adds some shards with the same shard ID as existing shards, but with a non-existent allocation ID. This tests that these shards are correctly handled by the shard failure cluster state task executor.
1 parent 8d8bd8b commit aa6a5c9

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

core/src/test/java/org/elasticsearch/cluster/action/shard/ShardFailedClusterStateTaskExecutorTests.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
import org.elasticsearch.cluster.routing.ShardIterator;
3434
import org.elasticsearch.cluster.routing.ShardRouting;
3535
import org.elasticsearch.cluster.routing.ShardRoutingState;
36+
import org.elasticsearch.cluster.routing.TestShardRouting;
3637
import org.elasticsearch.cluster.routing.allocation.AllocationService;
3738
import org.elasticsearch.cluster.routing.allocation.FailedRerouteAllocation;
3839
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
3940
import org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider;
40-
import org.elasticsearch.common.settings.Settings;
4141
import org.elasticsearch.test.ESAllocationTestCase;
4242
import org.junit.Before;
4343

@@ -95,7 +95,9 @@ public void testDuplicateFailuresAreOkay() throws Exception {
9595
}
9696

9797
public void testNonExistentShardsAreMarkedAsSuccessful() throws Exception {
98-
List<ShardStateAction.ShardRoutingEntry> tasks = createNonExistentShards("test non existent shards are marked as successful");
98+
String reason = "test non existent shards are marked as successful";
99+
ClusterState currentState = createClusterStateWithStartedShards(reason);
100+
List<ShardStateAction.ShardRoutingEntry> tasks = createNonExistentShards(currentState, reason);
99101
ClusterStateTaskExecutor.BatchResult<ShardStateAction.ShardRoutingEntry> result = executor.execute(clusterState, tasks);
100102
assertTasksSuccessful(tasks, result, clusterState, false);
101103
}
@@ -104,7 +106,7 @@ public void testTriviallySuccessfulTasksBatchedWithFailingTasks() throws Excepti
104106
String reason = "test trivially successful tasks batched with failing tasks";
105107
ClusterState currentState = createClusterStateWithStartedShards(reason);
106108
List<ShardStateAction.ShardRoutingEntry> failingTasks = createExistingShards(currentState, reason);
107-
List<ShardStateAction.ShardRoutingEntry> nonExistentTasks = createNonExistentShards(reason);
109+
List<ShardStateAction.ShardRoutingEntry> nonExistentTasks = createNonExistentShards(currentState, reason);
108110
ShardStateAction.ShardFailedClusterStateTaskExecutor failingExecutor = new ShardStateAction.ShardFailedClusterStateTaskExecutor(allocationService, null, logger) {
109111
@Override
110112
RoutingAllocation.Result applyFailedShards(ClusterState currentState, List<FailedRerouteAllocation.FailedShard> failedShards) {
@@ -155,14 +157,28 @@ private List<ShardStateAction.ShardRoutingEntry> createExistingShards(ClusterSta
155157
return toTasks(shardsToFail, indexUUID, reason);
156158
}
157159

158-
private List<ShardStateAction.ShardRoutingEntry> createNonExistentShards(String reason) {
160+
private List<ShardStateAction.ShardRoutingEntry> createNonExistentShards(ClusterState currentState, String reason) {
161+
// add shards from a non-existent index
159162
MetaData nonExistentMetaData =
160163
MetaData.builder()
161164
.put(IndexMetaData.builder("non-existent").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(numberOfReplicas))
162165
.build();
163166
RoutingTable routingTable = RoutingTable.builder().addAsNew(nonExistentMetaData.index("non-existent")).build();
164167
String nonExistentIndexUUID = nonExistentMetaData.index("non-existent").getIndexUUID();
165-
return toTasks(routingTable.allShards(), nonExistentIndexUUID, reason);
168+
169+
List<ShardStateAction.ShardRoutingEntry> existingShards = createExistingShards(currentState, reason);
170+
List<ShardStateAction.ShardRoutingEntry> shardsWithMismatchedAllocationIds = new ArrayList<>();
171+
for (ShardStateAction.ShardRoutingEntry existingShard : existingShards) {
172+
ShardRouting sr = existingShard.getShardRouting();
173+
ShardRouting nonExistentShardRouting =
174+
TestShardRouting.newShardRouting(sr.index(), sr.id(), sr.currentNodeId(), sr.relocatingNodeId(), sr.restoreSource(), sr.primary(), sr.state(), sr.version());
175+
shardsWithMismatchedAllocationIds.add(new ShardStateAction.ShardRoutingEntry(nonExistentShardRouting, existingShard.indexUUID, existingShard.message, existingShard.failure));
176+
}
177+
178+
List<ShardStateAction.ShardRoutingEntry> tasks = new ArrayList<>();
179+
tasks.addAll(toTasks(routingTable.allShards(), nonExistentIndexUUID, reason));
180+
tasks.addAll(shardsWithMismatchedAllocationIds);
181+
return tasks;
166182
}
167183

168184
private static void assertTasksSuccessful(

0 commit comments

Comments
 (0)