Search in sources :

Example 71 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class RebalanceAfterActiveTests method testRebalanceOnlyAfterAllShardsAreActive.

public void testRebalanceOnlyAfterAllShardsAreActive() {
    final long[] sizes = new long[5];
    for (int i = 0; i < sizes.length; i++) {
        sizes[i] = randomIntBetween(0, Integer.MAX_VALUE);
    }
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.cluster_concurrent_rebalance", -1).build(), new ClusterInfoService() {

        @Override
        public ClusterInfo getClusterInfo() {
            return new ClusterInfo() {

                @Override
                public Long getShardSize(ShardRouting shardRouting) {
                    if (shardRouting.getIndexName().equals("test")) {
                        return sizes[shardRouting.getId()];
                    }
                    return null;
                }
            };
        }

        @Override
        public void addListener(Listener listener) {
        }
    });
    logger.info("Building initial routing table");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(5).numberOfReplicas(1)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build();
    assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(5));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2));
        assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).state(), equalTo(UNASSIGNED));
        assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).state(), equalTo(UNASSIGNED));
        assertThat(clusterState.routingTable().index("test").shard(i).shards().get(0).currentNodeId(), nullValue());
        assertThat(clusterState.routingTable().index("test").shard(i).shards().get(1).currentNodeId(), nullValue());
    }
    logger.info("start two nodes and fully start the shards");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING));
        assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(UNASSIGNED));
    }
    logger.info("start all the primary shards, replicas will start initializing");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
        assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING));
        assertEquals(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]);
    }
    logger.info("now, start 8 more nodes, and check that no rebalancing/relocation have happened");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3")).add(newNode("node4")).add(newNode("node5")).add(newNode("node6")).add(newNode("node7")).add(newNode("node8")).add(newNode("node9")).add(newNode("node10"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    routingNodes = clusterState.getRoutingNodes();
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(2));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
        assertThat(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).state(), equalTo(INITIALIZING));
        assertEquals(clusterState.routingTable().index("test").shard(i).replicaShards().get(0).getExpectedShardSize(), sizes[i]);
    }
    logger.info("start the replica shards, rebalancing should start");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    // we only allow one relocation at a time
    assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(5));
    assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(5));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        int num = 0;
        for (ShardRouting routing : clusterState.routingTable().index("test").shard(i).shards()) {
            if (routing.state() == RELOCATING || routing.state() == INITIALIZING) {
                assertEquals(routing.getExpectedShardSize(), sizes[i]);
                num++;
            }
        }
        assertTrue(num > 0);
    }
    logger.info("complete relocation, other half of relocation should happen");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    // we now only relocate 3, since 2 remain where they are!
    assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(7));
    assertThat(clusterState.routingTable().shardsWithState(RELOCATING).size(), equalTo(3));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        for (ShardRouting routing : clusterState.routingTable().index("test").shard(i).shards()) {
            if (routing.state() == RELOCATING || routing.state() == INITIALIZING) {
                assertEquals(routing.getExpectedShardSize(), sizes[i]);
            }
        }
    }
    logger.info("complete relocation, that's it!");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.routingTable().shardsWithState(STARTED).size(), equalTo(10));
    // make sure we have an even relocation
    for (RoutingNode routingNode : routingNodes) {
        assertThat(routingNode.size(), equalTo(1));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 72 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class ReplicaAllocatedAfterPrimaryTests method testBackupIsAllocatedAfterPrimary.

public void testBackupIsAllocatedAfterPrimary() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).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();
    assertThat(routingTable.index("test").shards().size(), equalTo(1));
    assertThat(routingTable.index("test").shard(0).size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).shards().get(0).state(), equalTo(UNASSIGNED));
    assertThat(routingTable.index("test").shard(0).shards().get(1).state(), equalTo(UNASSIGNED));
    assertThat(routingTable.index("test").shard(0).shards().get(0).currentNodeId(), nullValue());
    assertThat(routingTable.index("test").shard(0).shards().get(1).currentNodeId(), nullValue());
    logger.info("Adding one node and performing rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    RoutingTable prevRoutingTable = routingTable;
    routingTable = strategy.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    final String nodeHoldingPrimary = routingTable.index("test").shard(0).primaryShard().currentNodeId();
    assertThat(prevRoutingTable != routingTable, equalTo(true));
    assertThat(routingTable.index("test").shards().size(), equalTo(1));
    assertThat(routingTable.index("test").shard(0).size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(INITIALIZING));
    assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary));
    assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1));
    assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(UNASSIGNED));
    assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), nullValue());
    logger.info("Start all the primary shards");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    prevRoutingTable = routingTable;
    routingTable = strategy.applyStartedShards(clusterState, routingNodes.node(nodeHoldingPrimary).shardsWithState(INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    final String nodeHoldingReplica = routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId();
    assertThat(nodeHoldingPrimary, not(equalTo(nodeHoldingReplica)));
    assertThat(prevRoutingTable != routingTable, equalTo(true));
    assertThat(routingTable.index("test").shards().size(), equalTo(1));
    assertThat(routingTable.index("test").shard(0).size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).shards().size(), equalTo(2));
    assertThat(routingTable.index("test").shard(0).primaryShard().state(), equalTo(STARTED));
    assertThat(routingTable.index("test").shard(0).primaryShard().currentNodeId(), equalTo(nodeHoldingPrimary));
    assertThat(routingTable.index("test").shard(0).replicaShards().size(), equalTo(1));
    assertThat(routingTable.index("test").shard(0).replicaShards().get(0).state(), equalTo(INITIALIZING));
    assertThat(routingTable.index("test").shard(0).replicaShards().get(0).currentNodeId(), equalTo(nodeHoldingReplica));
}
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)

Example 73 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class RoutingNodesIntegrityTests method testBalanceAllNodesStartedAddIndex.

public void testBalanceAllNodesStartedAddIndex() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 1).put("cluster.routing.allocation.node_initial_primaries_recoveries", 3).put(ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_CONCURRENT_OUTGOING_RECOVERIES_SETTING.getKey(), 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.cluster_concurrent_rebalance", -1).build());
    logger.info("Building initial routing table");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(3).numberOfReplicas(1)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build();
    logger.info("Adding three node and performing rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build();
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(false));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(true));
    clusterState = strategy.reroute(clusterState, "reroute");
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(true));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    logger.info("Another round of rebalancing");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build();
    ClusterState newState = strategy.reroute(clusterState, "reroute");
    assertThat(newState, equalTo(clusterState));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(routingNodes.node("node1").numberOfShardsWithState(INITIALIZING), equalTo(1));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(INITIALIZING), equalTo(1));
    assertThat(routingNodes.node("node3").numberOfShardsWithState(INITIALIZING), equalTo(1));
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(1));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(1));
    assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(1));
    logger.info("Reroute, nothing should change");
    newState = strategy.reroute(clusterState, "reroute");
    assertThat(newState, equalTo(clusterState));
    logger.info("Start the more shards");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(false));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(2));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(2));
    assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(2));
    assertThat(routingNodes.node("node1").shardsWithState("test", STARTED).size(), equalTo(2));
    assertThat(routingNodes.node("node2").shardsWithState("test", STARTED).size(), equalTo(2));
    assertThat(routingNodes.node("node3").shardsWithState("test", STARTED).size(), equalTo(2));
    logger.info("Add new index 3 shards 1 replica");
    metaData = MetaData.builder(clusterState.metaData()).put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 3).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1))).build();
    RoutingTable updatedRoutingTable = RoutingTable.builder(clusterState.routingTable()).addAsNew(metaData.index("test1")).build();
    clusterState = ClusterState.builder(clusterState).metaData(metaData).routingTable(updatedRoutingTable).build();
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(false));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(true));
    assertThat(clusterState.routingTable().index("test1").shards().size(), equalTo(3));
    clusterState = strategy.reroute(clusterState, "reroute");
    logger.info("Reroute, assign");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes())).build();
    newState = strategy.reroute(clusterState, "reroute");
    assertThat(newState, equalTo(clusterState));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(true));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    logger.info("Reroute, start the primaries");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    logger.info("Reroute, start the replicas");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(false));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(4));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(4));
    assertThat(routingNodes.node("node3").numberOfShardsWithState(STARTED), equalTo(4));
    assertThat(routingNodes.node("node1").shardsWithState("test1", STARTED).size(), equalTo(2));
    assertThat(routingNodes.node("node2").shardsWithState("test1", STARTED).size(), equalTo(2));
    assertThat(routingNodes.node("node3").shardsWithState("test1", STARTED).size(), equalTo(2));
    logger.info("kill one node");
    IndexShardRoutingTable indexShardRoutingTable = clusterState.routingTable().index("test").shard(0);
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(indexShardRoutingTable.primaryShard().currentNodeId())).build();
    clusterState = strategy.deassociateDeadNodes(clusterState, true, "reroute");
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    // replica got promoted to primary
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    logger.info("Start Recovering shards round 1");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(true));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
    logger.info("Start Recovering shards round 2");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    routingNodes = clusterState.getRoutingNodes();
    assertThat(assertShardStats(routingNodes), equalTo(true));
    assertThat(routingNodes.hasInactiveShards(), equalTo(false));
    assertThat(routingNodes.hasInactivePrimaries(), equalTo(false));
    assertThat(routingNodes.hasUnassignedPrimaries(), equalTo(false));
}
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) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 74 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class EnableAllocationTests method testEnableClusterBalanceNoReplicas.

public void testEnableClusterBalanceNoReplicas() {
    final boolean useClusterSetting = randomBoolean();
    Settings build = Settings.builder().put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), // index settings override cluster settings
    useClusterSetting ? Rebalance.NONE : RandomPicks.randomFrom(random(), Rebalance.values())).put(ConcurrentRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE_SETTING.getKey(), 3).build();
    ClusterSettings clusterSettings = new ClusterSettings(build, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    AllocationService strategy = createAllocationService(build, clusterSettings, random());
    Settings indexSettings = useClusterSetting ? Settings.EMPTY : Settings.builder().put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), Rebalance.NONE).build();
    logger.info("Building initial routing table");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT).put(indexSettings)).numberOfShards(6).numberOfReplicas(0)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build();
    logger.info("--> adding one nodes and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(6));
    logger.info("--> start the shards (primaries)");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(6));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0));
    logger.info("--> adding one nodes and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(6));
    assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(0));
    metaData = clusterState.metaData();
    if (useClusterSetting) {
        clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).transientSettings(Settings.builder().put(CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), randomBoolean() ? Rebalance.PRIMARIES : Rebalance.ALL).build())).build();
    } else {
        IndexMetaData meta = clusterState.getMetaData().index("test");
        clusterState = ClusterState.builder(clusterState).metaData(MetaData.builder(metaData).removeAllIndices().put(IndexMetaData.builder(meta).settings(Settings.builder().put(meta.getSettings()).put(EnableAllocationDecider.INDEX_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), randomBoolean() ? Rebalance.PRIMARIES : Rebalance.ALL).build()))).build();
    }
    clusterSettings.applySettings(clusterState.metaData().settings());
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat("expected 4 primaries to be started and 2 to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(4));
    assertThat("expected 2 primaries to relocate useClusterSettings: " + useClusterSetting, clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(2));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 75 with MetaData

use of org.elasticsearch.cluster.metadata.MetaData in project elasticsearch by elastic.

the class ClusterSerializationTests method testClusterStateSerialization.

public void testClusterStateSerialization() throws Exception {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).localNodeId("node1").masterNodeId("node2").build();
    ClusterState clusterState = ClusterState.builder(new ClusterName("clusterName1")).nodes(nodes).metaData(metaData).routingTable(routingTable).build();
    AllocationService strategy = createAllocationService();
    clusterState = ClusterState.builder(clusterState).routingTable(strategy.reroute(clusterState, "reroute").routingTable()).build();
    ClusterState serializedClusterState = ClusterState.Builder.fromBytes(ClusterState.Builder.toBytes(clusterState), newNode("node1"), new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
    assertThat(serializedClusterState.getClusterName().value(), equalTo(clusterState.getClusterName().value()));
    assertThat(serializedClusterState.routingTable().toString(), equalTo(clusterState.routingTable().toString()));
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ClusterName(org.elasticsearch.cluster.ClusterName) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Aggregations

MetaData (org.elasticsearch.cluster.metadata.MetaData)244 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)223 ClusterState (org.elasticsearch.cluster.ClusterState)179 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)138 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)52 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)48 Settings (org.elasticsearch.common.settings.Settings)43 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)32 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)30 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)27 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)26 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)26 Index (org.elasticsearch.index.Index)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)21 HashMap (java.util.HashMap)19 HashSet (java.util.HashSet)18 ShardId (org.elasticsearch.index.shard.ShardId)17 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)17 ArrayList (java.util.ArrayList)15