Skip to content

Commit 8b697ef

Browse files
author
Vladimir Dolzhenko
committed
extended testAllocateCommand: added AllocateStalePrimaryAllocationCommand
1 parent 02e5e05 commit 8b697ef

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
import org.elasticsearch.cluster.metadata.MetaData;
2929
import org.elasticsearch.cluster.node.DiscoveryNode;
3030
import org.elasticsearch.cluster.node.DiscoveryNodes;
31+
import org.elasticsearch.cluster.routing.RecoverySource;
32+
import org.elasticsearch.cluster.routing.RoutingNode;
3133
import org.elasticsearch.cluster.routing.RoutingNodes;
3234
import org.elasticsearch.cluster.routing.RoutingTable;
35+
import org.elasticsearch.cluster.routing.ShardRouting;
3336
import org.elasticsearch.cluster.routing.ShardRoutingState;
3437
import org.elasticsearch.cluster.routing.allocation.command.AbstractAllocateAllocationCommand;
3538
import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand;
@@ -58,6 +61,8 @@
5861
import java.util.Collections;
5962
import java.util.EnumSet;
6063
import java.util.HashSet;
64+
import java.util.List;
65+
import java.util.Set;
6166

6267
import static java.util.Collections.emptyMap;
6368
import static java.util.Collections.singleton;
@@ -66,6 +71,7 @@
6671
import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED;
6772
import static org.hamcrest.Matchers.containsString;
6873
import static org.hamcrest.Matchers.equalTo;
74+
import static org.hamcrest.Matchers.hasSize;
6975
import static org.hamcrest.Matchers.not;
7076
import static org.hamcrest.Matchers.nullValue;
7177

@@ -245,6 +251,29 @@ public void testAllocateCommand() {
245251
fail("expected IllegalArgumentException when allocating shard while no unassigned shard available");
246252
} catch (IllegalArgumentException e) {
247253
}
254+
255+
// mark all shards as stale
256+
final List<ShardRouting> shardRoutings = clusterState.getRoutingNodes().shardsWithState(STARTED);
257+
assertThat(shardRoutings, hasSize(2));
258+
clusterState = allocation.applyFailedShard(clusterState, shardRoutings.get(0), true);
259+
clusterState = allocation.applyFailedShard(clusterState, shardRoutings.get(1), true);
260+
261+
logger.info("--> allocating empty primary with acceptDataLoss flag set to true");
262+
clusterState = allocation.reroute(clusterState,
263+
new AllocationCommands(new AllocateStalePrimaryAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
264+
RoutingNode routingNode1 = clusterState.getRoutingNodes().node("node1");
265+
assertThat(routingNode1.size(), equalTo(1));
266+
assertThat(routingNode1.shardsWithState(INITIALIZING).size(), equalTo(1));
267+
Set<String> inSyncAllocationIds = clusterState.metaData().index("test").inSyncAllocationIds(0);
268+
assertThat(inSyncAllocationIds, equalTo(Collections.singleton(RecoverySource.ExistingStoreRecoverySource.FORCED_ALLOCATION_ID)));
269+
270+
clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
271+
routingNode1 = clusterState.getRoutingNodes().node("node1");
272+
assertThat(routingNode1.size(), equalTo(1));
273+
assertThat(routingNode1.shardsWithState(STARTED).size(), equalTo(1));
274+
inSyncAllocationIds = clusterState.metaData().index("test").inSyncAllocationIds(0);
275+
assertThat(inSyncAllocationIds, hasSize(1));
276+
assertThat(inSyncAllocationIds, not(Collections.singleton(RecoverySource.ExistingStoreRecoverySource.FORCED_ALLOCATION_ID)));
248277
}
249278

250279
public void testCancelCommand() {

0 commit comments

Comments
 (0)