Search in sources :

Example 76 with MetaData

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

the class DeadNodesAllocationTests method testSimpleDeadNodeOnStartedPrimaryShard.

public void testSimpleDeadNodeOnStartedPrimaryShard() {
    AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").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 2 nodes on same rack and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    // starting primaries
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    // starting replicas
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    logger.info("--> verifying all is allocated");
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").iterator().next().state(), equalTo(STARTED));
    logger.info("--> fail node with primary");
    String nodeIdToFail = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId();
    String nodeIdRemaining = nodeIdToFail.equals("node1") ? "node2" : "node1";
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode(nodeIdRemaining))).build();
    clusterState = allocation.deassociateDeadNodes(clusterState, true, "reroute");
    assertThat(clusterState.getRoutingNodes().node(nodeIdRemaining).iterator().next().primary(), equalTo(true));
    assertThat(clusterState.getRoutingNodes().node(nodeIdRemaining).iterator().next().state(), equalTo(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)

Example 77 with MetaData

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

the class DeadNodesAllocationTests method testDeadNodeWhileRelocatingOnFromNode.

public void testDeadNodeWhileRelocatingOnFromNode() {
    AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").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 2 nodes on same rack and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    // starting primaries
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    // starting replicas
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    logger.info("--> verifying all is allocated");
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").iterator().next().state(), equalTo(STARTED));
    logger.info("--> adding additional node");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0));
    String origPrimaryNodeId = clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId();
    String origReplicaNodeId = clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId();
    logger.info("--> moving primary shard to node3");
    AllocationService.CommandsResult commandsResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), "node3")), false, false);
    assertThat(commandsResult.getClusterState(), not(equalTo(clusterState)));
    clusterState = commandsResult.getClusterState();
    assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(RELOCATING));
    assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING));
    logger.info("--> fail primary shard recovering instance on 'origPrimaryNodeId' being relocated");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node3")).add(newNode(origReplicaNodeId))).build();
    clusterState = allocation.deassociateDeadNodes(clusterState, true, "reroute");
    assertThat(clusterState.getRoutingNodes().node(origReplicaNodeId).iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node3").iterator().next().state(), equalTo(INITIALIZING));
}
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) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)

Example 78 with MetaData

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

the class DecisionsImpactOnClusterHealthTests method runAllocationTest.

private ClusterState runAllocationTest(final Settings settings, final String indexName, final Set<AllocationDecider> allocationDeciders, final ClusterHealthStatus expectedStatus) throws IOException {
    final String clusterName = "test-cluster";
    final AllocationService allocationService = newAllocationService(settings, allocationDeciders);
    logger.info("Building initial routing table");
    final int numShards = randomIntBetween(1, 5);
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(indexName).settings(settings(Version.CURRENT)).numberOfShards(numShards).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index(indexName)).build();
    ClusterState clusterState = ClusterState.builder(new ClusterName(clusterName)).metaData(metaData).routingTable(routingTable).build();
    logger.info("--> adding nodes");
    // we need at least as many nodes as shards for the THROTTLE case, because
    // once a shard has been throttled on a node, that node no longer accepts
    // any allocations on it
    final DiscoveryNodes.Builder discoveryNodes = DiscoveryNodes.builder();
    for (int i = 0; i < numShards; i++) {
        discoveryNodes.add(newNode("node" + i));
    }
    clusterState = ClusterState.builder(clusterState).nodes(discoveryNodes).build();
    logger.info("--> do the reroute");
    routingTable = allocationService.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    logger.info("--> assert cluster health");
    ClusterStateHealth health = new ClusterStateHealth(clusterState);
    assertThat(health.getStatus(), equalTo(expectedStatus));
    return clusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateHealth(org.elasticsearch.cluster.health.ClusterStateHealth) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) 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)

Example 79 with MetaData

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

the class ElectReplicaAsPrimaryDuringRelocationTests method testElectReplicaAsPrimaryDuringRelocation.

public void testElectReplicaAsPrimaryDuringRelocation() {
    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(2).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 two nodes and performing rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    logger.info("Start the primary shards");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    logger.info("Start the replica shards");
    routingNodes = clusterState.getRoutingNodes();
    ClusterState resultingState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState(INITIALIZING));
    assertThat(resultingState, not(equalTo(clusterState)));
    clusterState = resultingState;
    routingNodes = clusterState.getRoutingNodes();
    assertThat(clusterState.routingTable().index("test").shards().size(), equalTo(2));
    assertThat(routingNodes.node("node1").numberOfShardsWithState(STARTED), equalTo(2));
    assertThat(routingNodes.node("node2").numberOfShardsWithState(STARTED), equalTo(2));
    logger.info("Start another node and perform rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node3"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    logger.info("find the replica shard that gets relocated");
    IndexShardRoutingTable indexShardRoutingTable = null;
    if (clusterState.routingTable().index("test").shard(0).replicaShards().get(0).relocating()) {
        indexShardRoutingTable = clusterState.routingTable().index("test").shard(0);
    } else if (clusterState.routingTable().index("test").shard(1).replicaShards().get(0).relocating()) {
        indexShardRoutingTable = clusterState.routingTable().index("test").shard(1);
    }
    // we might have primary relocating, and the test is only for replicas, so only test in the case of replica allocation
    if (indexShardRoutingTable != null) {
        logger.info("kill the node [{}] of the primary shard for the relocating replica", indexShardRoutingTable.primaryShard().currentNodeId());
        clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).remove(indexShardRoutingTable.primaryShard().currentNodeId())).build();
        clusterState = strategy.deassociateDeadNodes(clusterState, true, "reroute");
        logger.info("make sure all the primary shards are active");
        assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().active(), equalTo(true));
        assertThat(clusterState.routingTable().index("test").shard(1).primaryShard().active(), equalTo(true));
    }
}
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 80 with MetaData

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

the class ExpectedShardSizeAllocationTests method testExpectedSizeOnMove.

public void testExpectedSizeOnMove() {
    final long byteSize = randomIntBetween(0, Integer.MAX_VALUE);
    final AllocationService allocation = createAllocationService(Settings.EMPTY, new ClusterInfoService() {

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

                @Override
                public Long getShardSize(ShardRouting shardRouting) {
                    if (shardRouting.getIndexName().equals("test") && shardRouting.shardId().getId() == 0) {
                        return byteSize;
                    }
                    return null;
                }
            };
        }

        @Override
        public void addListener(Listener listener) {
        }
    });
    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";
    }
    AllocationService.CommandsResult commandsResult = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, existingNodeId, toNodeId)), false, false);
    assertThat(commandsResult.getClusterState(), not(equalTo(clusterState)));
    clusterState = commandsResult.getClusterState();
    assertEquals(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().state(), ShardRoutingState.RELOCATING);
    assertEquals(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), ShardRoutingState.INITIALIZING);
    assertEquals(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().getExpectedShardSize(), byteSize);
    assertEquals(clusterState.getRoutingNodes().node(toNodeId).iterator().next().getExpectedShardSize(), byteSize);
    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));
    assertEquals(clusterState.getRoutingNodes().node(toNodeId).iterator().next().getExpectedShardSize(), -1);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

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