Search in sources :

Example 51 with RoutingTable

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

the class FailedShardsRoutingTests method testFailedShardPrimaryRelocatingToAndFrom.

public void testFailedShardPrimaryRelocatingToAndFrom() {
    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(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 node3 being initialized");
    clusterState = allocation.applyFailedShard(clusterState, clusterState.getRoutingNodes().node("node3").iterator().next());
    assertThat(clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.getRoutingNodes().node("node3").size(), equalTo(0));
    logger.info("--> moving primary shard to node3");
    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 node1 being relocated");
    clusterState = allocation.applyFailedShard(clusterState, clusterState.getRoutingNodes().node(origPrimaryNodeId).iterator().next());
    // check promotion of replica to primary
    assertThat(clusterState.getRoutingNodes().node(origReplicaNodeId).iterator().next().state(), equalTo(STARTED));
    assertThat(clusterState.routingTable().index("test").shard(0).primaryShard().currentNodeId(), equalTo(origReplicaNodeId));
    assertThat(clusterState.routingTable().index("test").shard(0).replicaShards().get(0).currentNodeId(), anyOf(equalTo(origPrimaryNodeId), equalTo("node3")));
}
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 52 with RoutingTable

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

the class FailedShardsRoutingTests method testFailAllReplicasInitializingOnPrimaryFailWhileHavingAReplicaToElect.

public void testFailAllReplicasInitializingOnPrimaryFailWhileHavingAReplicaToElect() {
    AllocationService allocation = createAllocationService(Settings.builder().build());
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(2)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metaData.index("test")).build();
    ClusterState clusterState = ClusterState.builder(CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    // add 4 nodes
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4"))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(2));
    // start primary shards
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2));
    // start another replica shard, while keep one initializing
    clusterState = allocation.applyStartedShards(clusterState, Collections.singletonList(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).get(0)));
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1));
    // fail the primary shard, check one replica gets elected to primary, others become INITIALIZING (from it)
    ShardRouting primaryShardToFail = clusterState.routingTable().index("test").shard(0).primaryShard();
    ClusterState newState = allocation.applyFailedShard(clusterState, primaryShardToFail);
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2));
    ShardRouting newPrimaryShard = clusterState.routingTable().index("test").shard(0).primaryShard();
    assertThat(newPrimaryShard, not(equalTo(primaryShardToFail)));
}
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) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 53 with RoutingTable

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

the class FilterAllocationDeciderTests method createInitialClusterState.

private ClusterState createInitialClusterState(AllocationService service, Settings settings) {
    RecoverySource.Type recoveryType = randomFrom(RecoverySource.Type.EMPTY_STORE, RecoverySource.Type.LOCAL_SHARDS, RecoverySource.Type.SNAPSHOT);
    MetaData.Builder metaData = MetaData.builder();
    final Settings.Builder indexSettings = settings(Version.CURRENT).put(settings);
    final IndexMetaData sourceIndex;
    if (recoveryType == RecoverySource.Type.LOCAL_SHARDS) {
        //put a fake closed source index
        sourceIndex = IndexMetaData.builder("sourceIndex").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0).putInSyncAllocationIds(0, Collections.singleton("aid0")).putInSyncAllocationIds(1, Collections.singleton("aid1")).build();
        metaData.put(sourceIndex, false);
        indexSettings.put(INDEX_SHRINK_SOURCE_UUID.getKey(), sourceIndex.getIndexUUID());
        indexSettings.put(INDEX_SHRINK_SOURCE_NAME.getKey(), sourceIndex.getIndex().getName());
    } else {
        sourceIndex = null;
    }
    final IndexMetaData.Builder indexMetaDataBuilder = IndexMetaData.builder("idx").settings(indexSettings).numberOfShards(1).numberOfReplicas(1);
    if (recoveryType == RecoverySource.Type.SNAPSHOT) {
        indexMetaDataBuilder.putInSyncAllocationIds(0, Collections.singleton("_snapshot_restore"));
    }
    final IndexMetaData indexMetaData = indexMetaDataBuilder.build();
    metaData.put(indexMetaData, false);
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    switch(recoveryType) {
        case EMPTY_STORE:
            routingTableBuilder.addAsNew(indexMetaData);
            break;
        case SNAPSHOT:
            routingTableBuilder.addAsRestore(indexMetaData, new RecoverySource.SnapshotRecoverySource(new Snapshot("repository", new SnapshotId("snapshot_name", "snapshot_uuid")), Version.CURRENT, indexMetaData.getIndex().getName()));
            break;
        case LOCAL_SHARDS:
            routingTableBuilder.addAsFromCloseToOpen(sourceIndex);
            routingTableBuilder.addAsNew(indexMetaData);
            break;
        default:
            throw new UnsupportedOperationException(recoveryType + " is not supported");
    }
    RoutingTable routingTable = routingTableBuilder.build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    return service.reroute(clusterState, "reroute", false);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings)

Example 54 with RoutingTable

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

the class FilterAllocationDeciderTests method testFilterInitialRecovery.

public void testFilterInitialRecovery() {
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    FilterAllocationDecider filterAllocationDecider = new FilterAllocationDecider(Settings.EMPTY, clusterSettings);
    AllocationDeciders allocationDeciders = new AllocationDeciders(Settings.EMPTY, Arrays.asList(filterAllocationDecider, new SameShardAllocationDecider(Settings.EMPTY, clusterSettings), new ReplicaAfterPrimaryActiveAllocationDecider(Settings.EMPTY)));
    AllocationService service = new AllocationService(Settings.builder().build(), allocationDeciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    ClusterState state = createInitialClusterState(service, Settings.builder().put("index.routing.allocation.initial_recovery._id", "node2").build());
    RoutingTable routingTable = state.routingTable();
    // we can initally only allocate on node2
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).currentNodeId(), "node2");
    routingTable = service.applyFailedShard(state, routingTable.index("idx").shard(0).shards().get(0)).routingTable();
    state = ClusterState.builder(state).routingTable(routingTable).build();
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertNull(routingTable.index("idx").shard(0).shards().get(0).currentNodeId());
    // after failing the shard we are unassigned since the node is blacklisted and we can't initialize on the other node
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, 0, false);
    allocation.debugDecision(true);
    Decision.Single decision = (Decision.Single) filterAllocationDecider.canAllocate(routingTable.index("idx").shard(0).primaryShard(), state.getRoutingNodes().node("node2"), allocation);
    assertEquals(Type.YES, decision.type());
    assertEquals("node passes include/exclude/require filters", decision.getExplanation());
    ShardRouting primaryShard = routingTable.index("idx").shard(0).primaryShard();
    decision = (Decision.Single) filterAllocationDecider.canAllocate(routingTable.index("idx").shard(0).primaryShard(), state.getRoutingNodes().node("node1"), allocation);
    assertEquals(Type.NO, decision.type());
    if (primaryShard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        assertEquals("initial allocation of the shrunken index is only allowed on nodes [_id:\"node2\"] that " + "hold a copy of every shard in the index", decision.getExplanation());
    } else {
        assertEquals("initial allocation of the index is only allowed on nodes [_id:\"node2\"]", decision.getExplanation());
    }
    state = service.reroute(state, "try allocate again");
    routingTable = state.routingTable();
    assertEquals(routingTable.index("idx").shard(0).primaryShard().state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).primaryShard().currentNodeId(), "node2");
    state = service.applyStartedShards(state, routingTable.index("idx").shard(0).shardsWithState(INITIALIZING));
    routingTable = state.routingTable();
    // ok now we are started and can be allocated anywhere!! lets see...
    // first create another copy
    assertEquals(routingTable.index("idx").shard(0).replicaShards().get(0).state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).replicaShards().get(0).currentNodeId(), "node1");
    state = service.applyStartedShards(state, routingTable.index("idx").shard(0).replicaShardsWithState(INITIALIZING));
    routingTable = state.routingTable();
    assertEquals(routingTable.index("idx").shard(0).replicaShards().get(0).state(), STARTED);
    assertEquals(routingTable.index("idx").shard(0).replicaShards().get(0).currentNodeId(), "node1");
    // now remove the node of the other copy and fail the current
    DiscoveryNode node1 = state.nodes().resolveNode("node1");
    state = service.deassociateDeadNodes(ClusterState.builder(state).nodes(DiscoveryNodes.builder(state.nodes()).remove("node1")).build(), true, "test");
    state = service.applyFailedShard(state, routingTable.index("idx").shard(0).primaryShard());
    // now bring back node1 and see it's assigned
    state = service.reroute(ClusterState.builder(state).nodes(DiscoveryNodes.builder(state.nodes()).add(node1)).build(), "test");
    routingTable = state.routingTable();
    assertEquals(routingTable.index("idx").shard(0).primaryShard().state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).primaryShard().currentNodeId(), "node1");
    allocation = new RoutingAllocation(allocationDeciders, state.getRoutingNodes(), state, null, 0, false);
    allocation.debugDecision(true);
    decision = (Decision.Single) filterAllocationDecider.canAllocate(routingTable.index("idx").shard(0).shards().get(0), state.getRoutingNodes().node("node2"), allocation);
    assertEquals(Type.YES, decision.type());
    assertEquals("node passes include/exclude/require filters", decision.getExplanation());
    decision = (Decision.Single) filterAllocationDecider.canAllocate(routingTable.index("idx").shard(0).shards().get(0), state.getRoutingNodes().node("node1"), allocation);
    assertEquals(Type.YES, decision.type());
    assertEquals("node passes include/exclude/require filters", decision.getExplanation());
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) FilterAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 55 with RoutingTable

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

the class FilterRoutingTests method testClusterFilters.

public void testClusterFilters() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.include.tag1", "value1,value2").put("cluster.routing.allocation.exclude.tag1", "value3,value4").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 four nodes and performing rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1", singletonMap("tag1", "value1"))).add(newNode("node2", singletonMap("tag1", "value2"))).add(newNode("node3", singletonMap("tag1", "value3"))).add(newNode("node4", singletonMap("tag1", "value4")))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(2));
    logger.info("--> start the shards (primaries)");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    logger.info("--> start the shards (replicas)");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    logger.info("--> make sure shards are only allocated on tag1 with value1 and value2");
    List<ShardRouting> startedShards = clusterState.getRoutingNodes().shardsWithState(ShardRoutingState.STARTED);
    assertThat(startedShards.size(), equalTo(4));
    for (ShardRouting startedShard : startedShards) {
        assertThat(startedShard.currentNodeId(), Matchers.anyOf(equalTo("node1"), equalTo("node2")));
    }
}
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) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)165 ClusterState (org.elasticsearch.cluster.ClusterState)144 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)140 MetaData (org.elasticsearch.cluster.metadata.MetaData)135 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)57 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)53 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)43 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)33 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)31 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)23 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)21 ShardId (org.elasticsearch.index.shard.ShardId)21 Settings (org.elasticsearch.common.settings.Settings)20 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)18 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)18 HashSet (java.util.HashSet)17 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)16 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)14