Search in sources :

Example 6 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class AllocationCommandsTests method testMoveShardCommand.

public void testMoveShardCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build());
    logger.info("creating an index with 1 shard, no replica");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    logger.info("adding two nodes and performing rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    logger.info("start primary shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    logger.info("move the shard");
    String existingNodeId = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId();
    String toNodeId;
    if ("node1".equals(existingNodeId)) {
        toNodeId = "node2";
    } else {
        toNodeId = "node1";
    }
    ClusterState newState = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, existingNodeId, toNodeId)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().state(), equalTo(ShardRoutingState.RELOCATING));
    assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    logger.info("finish moving the shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node(existingNodeId).isEmpty(), equalTo(true));
    assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)

Example 7 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class BalanceConfigurationTests method testNoRebalanceOnPrimaryOverload.

public void testNoRebalanceOnPrimaryOverload() {
    Settings.Builder settings = Settings.builder();
    AllocationService strategy = new AllocationService(settings.build(), randomAllocationDeciders(settings.build(), new ClusterSettings(Settings.Builder.EMPTY_SETTINGS, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), random()), new TestGatewayAllocator(), new ShardsAllocator() {

        /*
             *  // this allocator tries to rebuild this scenario where a rebalance is
             *  // triggered solely by the primary overload on node [1] where a shard
             *  // is rebalanced to node 0
                routing_nodes:
                -----node_id[0][V]
                --------[test][0], node[0], [R], s[STARTED]
                --------[test][4], node[0], [R], s[STARTED]
                -----node_id[1][V]
                --------[test][0], node[1], [P], s[STARTED]
                --------[test][1], node[1], [P], s[STARTED]
                --------[test][3], node[1], [R], s[STARTED]
                -----node_id[2][V]
                --------[test][1], node[2], [R], s[STARTED]
                --------[test][2], node[2], [R], s[STARTED]
                --------[test][4], node[2], [P], s[STARTED]
                -----node_id[3][V]
                --------[test][2], node[3], [P], s[STARTED]
                --------[test][3], node[3], [P], s[STARTED]
                ---- unassigned
            */
        public void allocate(RoutingAllocation allocation) {
            RoutingNodes.UnassignedShards unassigned = allocation.routingNodes().unassigned();
            ShardRouting[] drain = unassigned.drain();
            // we have to allocate primaries first
            ArrayUtil.timSort(drain, (a, b) -> {
                return a.primary() ? -1 : 1;
            });
            for (ShardRouting sr : drain) {
                switch(sr.id()) {
                    case 0:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node0", null, -1, allocation.changes());
                        }
                        break;
                    case 1:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        }
                        break;
                    case 2:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node3", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        }
                        break;
                    case 3:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node3", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node1", null, -1, allocation.changes());
                        }
                        break;
                    case 4:
                        if (sr.primary()) {
                            allocation.routingNodes().initializeShard(sr, "node2", null, -1, allocation.changes());
                        } else {
                            allocation.routingNodes().initializeShard(sr, "node0", null, -1, allocation.changes());
                        }
                        break;
                }
            }
        }

        @Override
        public ShardAllocationDecision decideShardAllocation(ShardRouting shard, RoutingAllocation allocation) {
            throw new UnsupportedOperationException("explain not supported");
        }
    }, EmptyClusterInfoService.INSTANCE);
    MetaData.Builder metaDataBuilder = MetaData.builder();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    IndexMetaData.Builder indexMeta = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1);
    metaDataBuilder = metaDataBuilder.put(indexMeta);
    MetaData metaData = metaDataBuilder.build();
    for (ObjectCursor<IndexMetaData> cursor : metaData.indices().values()) {
        routingTableBuilder.addAsNew(cursor.value);
    }
    RoutingTable routingTable = routingTableBuilder.build();
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
    for (int i = 0; i < 4; i++) {
        DiscoveryNode node = newNode("node" + i);
        nodes.add(node);
    }
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(routingTable).build();
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.INITIALIZING));
        }
    }
    strategy = createAllocationService(settings.build(), new NoopGatewayAllocator());
    logger.info("use the new allocator and check if it moves shards");
    routingNodes = clusterState.getRoutingNodes();
    routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
    logger.info("start the replica shards");
    routingTable = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
    logger.info("rebalancing");
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    for (RoutingNode routingNode : routingNodes) {
        for (ShardRouting shardRouting : routingNode) {
            assertThat(shardRouting.state(), Matchers.equalTo(ShardRoutingState.STARTED));
        }
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ESAllocationTestCase(org.elasticsearch.cluster.ESAllocationTestCase) INITIALIZING(org.elasticsearch.cluster.routing.ShardRoutingState.INITIALIZING) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) STARTED(org.elasticsearch.cluster.routing.ShardRoutingState.STARTED) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) EmptyClusterInfoService(org.elasticsearch.cluster.EmptyClusterInfoService) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterRebalanceAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider) Loggers(org.elasticsearch.common.logging.Loggers) ArrayUtil(org.apache.lucene.util.ArrayUtil) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) Matchers(org.hamcrest.Matchers) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) GatewayAllocator(org.elasticsearch.gateway.GatewayAllocator) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) ShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 8 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class BalanceConfigurationTests method addNode.

private ClusterState addNode(ClusterState clusterState, AllocationService strategy) {
    logger.info("now, start 1 more node, check that rebalancing will happen because we set it to always");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node" + numberOfNodes))).build();
    RoutingTable routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // move initializing to started
    return applyStartedShardsUntilNoChange(clusterState, strategy);
}
Also used : RoutingTable(org.elasticsearch.cluster.routing.RoutingTable)

Example 9 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class BalanceUnbalancedClusterTests method allocateNew.

@Override
protected ClusterState allocateNew(ClusterState state) {
    String index = "tweets-2014-12-29:00";
    AllocationService strategy = createAllocationService(Settings.builder().build());
    MetaData metaData = MetaData.builder(state.metaData()).put(IndexMetaData.builder(index).settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder(state.routingTable()).addAsNew(metaData.index(index)).build();
    ClusterState clusterState = ClusterState.builder(state).metaData(metaData).routingTable(initialRoutingTable).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    while (true) {
        if (clusterState.routingTable().shardsWithState(INITIALIZING).isEmpty()) {
            break;
        }
        clusterState = strategy.applyStartedShards(clusterState, clusterState.routingTable().shardsWithState(INITIALIZING));
    }
    Map<String, Integer> counts = new HashMap<>();
    for (IndexShardRoutingTable table : clusterState.routingTable().index(index)) {
        for (ShardRouting r : table) {
            String s = r.currentNodeId();
            Integer count = counts.get(s);
            if (count == null) {
                count = 0;
            }
            count++;
            counts.put(s, count);
        }
    }
    for (Map.Entry<String, Integer> count : counts.entrySet()) {
        // we have 10 shards and 4 nodes so 2 nodes have 3 shards and 2 nodes have 2 shards
        assertTrue("Node: " + count.getKey() + " has shard mismatch: " + count.getValue(), count.getValue() >= 2);
        assertTrue("Node: " + count.getKey() + " has shard mismatch: " + count.getValue(), count.getValue() <= 3);
    }
    return clusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) HashMap(java.util.HashMap) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class AllocationCommandsTests method testCancelCommand.

public void testCancelCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none").put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none").build());
    logger.info("--> building initial routing table");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    logger.info("--> adding 3 nodes");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0));
    logger.info("--> allocating empty primary shard with accept_data_loss flag set to true");
    ClusterState newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> cancel primary allocation, make sure it fails...");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", false)), false, false);
        fail();
    } catch (IllegalArgumentException e) {
    }
    logger.info("--> start the primary shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> cancel primary allocation, make sure it fails...");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", false)), false, false);
        fail();
    } catch (IllegalArgumentException e) {
    }
    logger.info("--> allocate the replica shard on on the second node");
    newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> cancel the relocation allocation");
    newState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0));
    logger.info("--> allocate the replica shard on on the second node");
    newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> cancel the primary being replicated, make sure it fails");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", false)), false, false);
        fail();
    } catch (IllegalArgumentException e) {
    }
    logger.info("--> start the replica shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(1));
    logger.info("--> cancel allocation of the replica shard");
    newState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0));
    logger.info("--> allocate the replica shard on on the second node");
    newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> start the replica shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(1));
    logger.info("--> move the replica shard");
    clusterState = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, "node2", "node3")), false, false).getClusterState();
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(RELOCATING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(INITIALIZING).size(), equalTo(1));
    if (randomBoolean()) {
        logger.info("--> cancel the primary allocation (with allow_primary set to true)");
        newState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0));
        assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).iterator().next().primary(), equalTo(true));
        assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0));
    } else {
        logger.info("--> cancel the move of the replica shard");
        clusterState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node3", false)), false, false).getClusterState();
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(1));
        logger.info("--> move the replica shard again");
        clusterState = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, "node2", "node3")), false, false).getClusterState();
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(RELOCATING).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(INITIALIZING).size(), equalTo(1));
        logger.info("--> cancel the source replica shard");
        clusterState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node2", false)), false, false).getClusterState();
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
        assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(INITIALIZING).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(INITIALIZING).get(0).relocatingNodeId(), nullValue());
        logger.info("--> start the former target replica shard");
        clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
        assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
        assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(STARTED).size(), equalTo(1));
        logger.info("--> cancel the primary allocation (with allow_primary set to true)");
        newState = allocation.reroute(clusterState, new AllocationCommands(new CancelAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        assertThat(clusterState.getRoutingNodes().node("node3").shardsWithState(STARTED).iterator().next().primary(), equalTo(true));
        assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(0));
        assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) CancelAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AllocateReplicaAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)

Aggregations

RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)228 ClusterState (org.elasticsearch.cluster.ClusterState)201 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)140 MetaData (org.elasticsearch.cluster.metadata.MetaData)135 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)74 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)66 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)64 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)59 Metadata (org.elasticsearch.cluster.metadata.Metadata)57 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)51 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)48 ShardId (org.elasticsearch.index.shard.ShardId)37 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)33 Settings (org.elasticsearch.common.settings.Settings)33 Index (org.elasticsearch.index.Index)31 Matchers.containsString (org.hamcrest.Matchers.containsString)30 HashSet (java.util.HashSet)29 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)29 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)29 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)28