Search in sources :

Example 1 with RoutingNodes

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

the class AddIncrementallyTests method testMinimalRelocationsNoLimit.

public void testMinimalRelocationsNoLimit() {
    Settings.Builder settings = Settings.builder();
    settings.put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), ClusterRebalanceAllocationDecider.ClusterRebalanceType.ALWAYS.toString()).put("cluster.routing.allocation.node_concurrent_recoveries", 100).put("cluster.routing.allocation.node_initial_primaries_recoveries", 100);
    AllocationService service = createAllocationService(settings.build());
    ClusterState clusterState = initCluster(service, 1, 3, 3, 1);
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(STARTED).size(), equalTo(9));
    assertThat(clusterState.getRoutingNodes().unassigned().size(), equalTo(9));
    int nodeOffset = 1;
    clusterState = addNodes(clusterState, service, 1, nodeOffset++);
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(STARTED).size(), equalTo(9));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(9));
    assertThat(clusterState.getRoutingNodes().unassigned().size(), equalTo(0));
    assertNumIndexShardsPerNode(clusterState, equalTo(3));
    logger.info("now, start one more node, check that rebalancing will happen because we set it to always");
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterState.nodes());
    nodes.add(newNode("node2"));
    clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build();
    clusterState = service.reroute(clusterState, "reroute");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(2));
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(0));
    ClusterState newState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(2));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(2));
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(0));
    newState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(4));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(2));
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(0));
    newState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(6));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node0").shardsWithState(INITIALIZING).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(0));
    newState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    assertThat(newState, equalTo(clusterState));
    assertNumIndexShardsPerNode(clusterState, equalTo(2));
    logger.debug("ClusterState: {}", clusterState.getRoutingNodes());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) Settings(org.elasticsearch.common.settings.Settings) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 2 with RoutingNodes

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

the class AddIncrementallyTests method initCluster.

private ClusterState initCluster(AllocationService service, int numberOfNodes, int numberOfIndices, int numberOfShards, int numberOfReplicas) {
    MetaData.Builder metaDataBuilder = MetaData.builder();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    for (int i = 0; i < numberOfIndices; i++) {
        IndexMetaData.Builder index = IndexMetaData.builder("test" + i).settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(numberOfReplicas);
        metaDataBuilder = metaDataBuilder.put(index);
    }
    MetaData metaData = metaDataBuilder.build();
    for (ObjectCursor<IndexMetaData> cursor : metaData.indices().values()) {
        routingTableBuilder.addAsNew(cursor.value);
    }
    RoutingTable initialRoutingTable = routingTableBuilder.build();
    logger.info("start {} nodes", numberOfNodes);
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
    for (int i = 0; i < numberOfNodes; i++) {
        nodes.add(newNode("node" + i));
    }
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(nodes).metaData(metaData).routingTable(initialRoutingTable).build();
    clusterState = service.reroute(clusterState, "reroute");
    logger.info("restart all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("start the replica shards");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    logger.info("complete rebalancing");
    return applyStartedShardsUntilNoChange(clusterState, service);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 3 with RoutingNodes

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

the class AddIncrementallyTests method removeNodes.

private ClusterState removeNodes(ClusterState clusterState, AllocationService service, int numNodes) {
    logger.info("Removing [{}] nodes", numNodes);
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterState.nodes());
    ArrayList<DiscoveryNode> discoveryNodes = CollectionUtils.iterableAsArrayList(clusterState.nodes());
    Collections.shuffle(discoveryNodes, random());
    for (DiscoveryNode node : discoveryNodes) {
        nodes.remove(node.getId());
        numNodes--;
        if (numNodes <= 0) {
            break;
        }
    }
    clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build();
    clusterState = service.deassociateDeadNodes(clusterState, true, "reroute");
    logger.info("start all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("start the replica shards");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = service.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("rebalancing");
    clusterState = service.reroute(clusterState, "reroute");
    logger.info("complete rebalancing");
    clusterState = applyStartedShardsUntilNoChange(clusterState, service);
    return clusterState;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 4 with RoutingNodes

use of org.elasticsearch.cluster.routing.RoutingNodes 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 5 with RoutingNodes

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

the class BalanceConfigurationTests method removeNodes.

private ClusterState removeNodes(ClusterState clusterState, AllocationService strategy) {
    logger.info("Removing half the nodes (" + (numberOfNodes + 1) / 2 + ")");
    DiscoveryNodes.Builder nodes = DiscoveryNodes.builder(clusterState.nodes());
    boolean removed = false;
    for (int i = (numberOfNodes + 1) / 2; i <= numberOfNodes; i++) {
        nodes.remove("node" + i);
        removed = true;
    }
    clusterState = ClusterState.builder(clusterState).nodes(nodes.build()).build();
    if (removed) {
        clusterState = strategy.deassociateDeadNodes(clusterState, randomBoolean(), "removed nodes");
    }
    logger.info("start all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("start the replica shards");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("rebalancing");
    clusterState = strategy.reroute(clusterState, "reroute");
    logger.info("complete rebalancing");
    return applyStartedShardsUntilNoChange(clusterState, strategy);
}
Also used : RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Aggregations

RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)104 ClusterState (org.elasticsearch.cluster.ClusterState)72 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)64 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)55 MetaData (org.elasticsearch.cluster.metadata.MetaData)53 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)39 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)24 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)20 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)15 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)15 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)14 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)14 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)14 Metadata (org.elasticsearch.cluster.metadata.Metadata)13 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)11 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)11 ArrayList (java.util.ArrayList)9 Settings (org.elasticsearch.common.settings.Settings)9 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)8 RerouteExplanation (org.elasticsearch.cluster.routing.allocation.RerouteExplanation)8