|
28 | 28 | import org.elasticsearch.cluster.metadata.MetaData; |
29 | 29 | import org.elasticsearch.cluster.node.DiscoveryNode; |
30 | 30 | import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 31 | +import org.elasticsearch.cluster.routing.RecoverySource; |
| 32 | +import org.elasticsearch.cluster.routing.RoutingNode; |
31 | 33 | import org.elasticsearch.cluster.routing.RoutingNodes; |
32 | 34 | import org.elasticsearch.cluster.routing.RoutingTable; |
| 35 | +import org.elasticsearch.cluster.routing.ShardRouting; |
33 | 36 | import org.elasticsearch.cluster.routing.ShardRoutingState; |
34 | 37 | import org.elasticsearch.cluster.routing.allocation.command.AbstractAllocateAllocationCommand; |
35 | 38 | import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand; |
|
58 | 61 | import java.util.Collections; |
59 | 62 | import java.util.EnumSet; |
60 | 63 | import java.util.HashSet; |
| 64 | +import java.util.List; |
| 65 | +import java.util.Set; |
61 | 66 |
|
62 | 67 | import static java.util.Collections.emptyMap; |
63 | 68 | import static java.util.Collections.singleton; |
|
66 | 71 | import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; |
67 | 72 | import static org.hamcrest.Matchers.containsString; |
68 | 73 | import static org.hamcrest.Matchers.equalTo; |
| 74 | +import static org.hamcrest.Matchers.hasSize; |
69 | 75 | import static org.hamcrest.Matchers.not; |
70 | 76 | import static org.hamcrest.Matchers.nullValue; |
71 | 77 |
|
@@ -245,6 +251,29 @@ public void testAllocateCommand() { |
245 | 251 | fail("expected IllegalArgumentException when allocating shard while no unassigned shard available"); |
246 | 252 | } catch (IllegalArgumentException e) { |
247 | 253 | } |
| 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))); |
248 | 277 | } |
249 | 278 |
|
250 | 279 | public void testCancelCommand() { |
|
0 commit comments