Search in sources :

Example 1 with AllocateStalePrimaryAllocationCommand

use of org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand in project elasticsearch by elastic.

the class AllocationCommandsTests method testAllocateCommand.

public void testAllocateCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none").put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none").build());
    final String index = "test";
    logger.info("--> building initial routing table");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(index).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("asdf")).putInSyncAllocationIds(1, Collections.singleton("qwertz"))).build();
    // shard routing is added as "from recovery" instead of "new index creation" so that we can test below that allocating an empty
    // primary with accept_data_loss flag set to false fails
    RoutingTable routingTable = RoutingTable.builder().addAsRecovery(metaData.index(index)).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    final ShardId shardId = new ShardId(metaData.index(index).getIndex(), 0);
    logger.info("--> adding 3 nodes on same rack and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).add(newNode("node4", singleton(DiscoveryNode.Role.MASTER)))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(0));
    logger.info("--> allocating to non-existent node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand(index, shardId.id(), "node42")), false, false);
        fail("expected IllegalArgumentException when allocating to non-existing node");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("failed to resolve [node42], no matching nodes"));
    }
    logger.info("--> allocating to non-data node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand(index, shardId.id(), "node4")), false, false);
        fail("expected IllegalArgumentException when allocating to non-data node");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocation can only be done on data nodes"));
    }
    logger.info("--> allocating non-existing shard, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test", 1, "node2")), false, false);
        fail("expected ShardNotFoundException when allocating non-existing shard");
    } catch (ShardNotFoundException e) {
        assertThat(e.getMessage(), containsString("no such shard"));
    }
    logger.info("--> allocating non-existing index, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test2", 0, "node2")), false, false);
        fail("expected ShardNotFoundException when allocating non-existing index");
    } catch (IndexNotFoundException e) {
        assertThat(e.getMessage(), containsString("no such index"));
    }
    logger.info("--> allocating empty primary with acceptDataLoss flag set to false");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", false)), false, false);
        fail("expected IllegalArgumentException when allocating empty primary with acceptDataLoss flag set to false");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocating an empty primary for " + shardId + " can result in data loss. Please confirm by setting the accept_data_loss parameter to true"));
    }
    logger.info("--> allocating stale primary with acceptDataLoss flag set to false");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateStalePrimaryAllocationCommand(index, shardId.id(), "node1", false)), false, false);
        fail("expected IllegalArgumentException when allocating stale primary with acceptDataLoss flag set to false");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("allocating an empty primary for " + shardId + " can result in data loss. Please confirm by setting the accept_data_loss parameter to true"));
    }
    logger.info("--> allocating empty primary with acceptDataLoss flag set to true");
    ClusterState newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 0, "node1", true)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(INITIALIZING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> start the primary shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(0));
    logger.info("--> allocate the replica shard on the primary shard node, should fail");
    try {
        allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node1")), false, false);
        fail("expected IllegalArgumentException when allocating replica shard on the primary shard node");
    } catch (IllegalArgumentException e) {
    }
    logger.info("--> allocate the replica shard on on the second node");
    newState = allocation.reroute(clusterState, new AllocationCommands(new AllocateReplicaAllocationCommand("test", 0, "node2")), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> start the replica shard");
    clusterState = allocation.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().node("node1").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node1").shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().node("node2").shardsWithState(STARTED).size(), equalTo(1));
    logger.info("--> verify that we fail when there are no unassigned shards");
    try {
        allocation.reroute(clusterState, new AllocationCommands(randomAllocateCommand("test", 0, "node3")), false, false);
        fail("expected IllegalArgumentException when allocating shard while no unassigned shard available");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) AllocateReplicaAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ShardId(org.elasticsearch.index.shard.ShardId) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 2 with AllocateStalePrimaryAllocationCommand

use of org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand in project elasticsearch by elastic.

the class PrimaryAllocationIT method testForceStaleReplicaToBePromotedToPrimary.

public void testForceStaleReplicaToBePromotedToPrimary() throws Exception {
    // if true, use stale replica, otherwise a completely empty copy
    boolean useStaleReplica = randomBoolean();
    createStaleReplicaScenario();
    logger.info("--> explicitly promote old primary shard");
    final String idxName = "test";
    ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> storeStatuses = client().admin().indices().prepareShardStores(idxName).get().getStoreStatuses().get(idxName);
    ClusterRerouteRequestBuilder rerouteBuilder = client().admin().cluster().prepareReroute();
    for (IntObjectCursor<List<IndicesShardStoresResponse.StoreStatus>> shardStoreStatuses : storeStatuses) {
        int shardId = shardStoreStatuses.key;
        IndicesShardStoresResponse.StoreStatus storeStatus = randomFrom(shardStoreStatuses.value);
        logger.info("--> adding allocation command for shard {}", shardId);
        // force allocation based on node id
        if (useStaleReplica) {
            rerouteBuilder.add(new AllocateStalePrimaryAllocationCommand(idxName, shardId, storeStatus.getNode().getId(), true));
        } else {
            rerouteBuilder.add(new AllocateEmptyPrimaryAllocationCommand(idxName, shardId, storeStatus.getNode().getId(), true));
        }
    }
    rerouteBuilder.get();
    logger.info("--> check that the stale primary shard gets allocated and that documents are available");
    ensureYellow(idxName);
    if (useStaleReplica == false) {
        // When invoking AllocateEmptyPrimaryAllocationCommand, due to the UnassignedInfo.Reason being changed to INDEX_CREATION,
        // its possible that the shard has not completed initialization, even though the cluster health is yellow, so the
        // search can throw an "all shards failed" exception.  We will wait until the shard initialization has completed before
        // verifying the search hit count.
        assertBusy(() -> assertTrue(client().admin().cluster().prepareState().get().getState().routingTable().index(idxName).allPrimaryShardsActive()));
    }
    assertHitCount(client().prepareSearch(idxName).setSize(0).setQuery(matchAllQuery()).get(), useStaleReplica ? 1L : 0L);
    // allocation id of old primary was cleaned from the in-sync set
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertEquals(Collections.singleton(state.routingTable().index(idxName).shard(0).primary.allocationId().getId()), state.metaData().index(idxName).inSyncAllocationIds(0));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndicesShardStoresResponse(org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse) ClusterRerouteRequestBuilder(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequestBuilder) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) List(java.util.List)

Example 3 with AllocateStalePrimaryAllocationCommand

use of org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand in project crate by crate.

the class AllocationCommandsTests method testAllocateStalePrimaryCommand.

@Test
public void testAllocateStalePrimaryCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none").put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none").build());
    final String index = "test";
    logger.info("--> building initial routing table");
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder(index).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("asdf")).putInSyncAllocationIds(1, Collections.singleton("qwertz"))).build();
    // shard routing is added as "from recovery" instead of "new index creation" so that we can test below that allocating an empty
    // primary with accept_data_loss flag set to false fails
    RoutingTable routingTable = RoutingTable.builder().addAsRecovery(metadata.index(index)).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).build();
    final String node1 = "node1";
    final String node2 = "node2";
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode(node1)).add(newNode(node2))).build();
    clusterState = allocation.reroute(clusterState, "reroute");
    // mark all shards as stale
    final List<ShardRouting> shardRoutings = clusterState.getRoutingNodes().shardsWithState(UNASSIGNED);
    assertThat(shardRoutings, hasSize(2));
    logger.info("--> allocating empty primary with acceptDataLoss flag set to true");
    clusterState = allocation.reroute(clusterState, new AllocationCommands(new AllocateStalePrimaryAllocationCommand(index, 0, node1, true)), false, false).getClusterState();
    RoutingNode routingNode1 = clusterState.getRoutingNodes().node(node1);
    assertThat(routingNode1.size(), equalTo(1));
    assertThat(routingNode1.shardsWithState(INITIALIZING).size(), equalTo(1));
    Set<String> inSyncAllocationIds = clusterState.metadata().index(index).inSyncAllocationIds(0);
    assertThat(inSyncAllocationIds, equalTo(Collections.singleton(RecoverySource.ExistingStoreRecoverySource.FORCED_ALLOCATION_ID)));
    clusterState = startInitializingShardsAndReroute(allocation, clusterState);
    routingNode1 = clusterState.getRoutingNodes().node(node1);
    assertThat(routingNode1.size(), equalTo(1));
    assertThat(routingNode1.shardsWithState(STARTED).size(), equalTo(1));
    inSyncAllocationIds = clusterState.metadata().index(index).inSyncAllocationIds(0);
    assertThat(inSyncAllocationIds, hasSize(1));
    assertThat(inSyncAllocationIds, not(Collections.singleton(RecoverySource.ExistingStoreRecoverySource.FORCED_ALLOCATION_ID)));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) Matchers.containsString(org.hamcrest.Matchers.containsString) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) Test(org.junit.Test)

Example 4 with AllocateStalePrimaryAllocationCommand

use of org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand in project crate by crate.

the class AllocationCommandsTests method testConflictingCommandsInSingleRequest.

@Test
public void testConflictingCommandsInSingleRequest() {
    AllocationService allocation = createAllocationService(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none").put(EnableAllocationDecider.CLUSTER_ROUTING_REBALANCE_ENABLE_SETTING.getKey(), "none").build());
    final String index1 = "test1";
    final String index2 = "test2";
    final String index3 = "test3";
    logger.info("--> building initial routing table");
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder(index1).settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, false).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build()).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("randomAllocID")).putInSyncAllocationIds(1, Collections.singleton("randomAllocID2"))).put(IndexMetadata.builder(index2).settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, false).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build()).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("randomAllocID")).putInSyncAllocationIds(1, Collections.singleton("randomAllocID2"))).put(IndexMetadata.builder(index3).settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, false).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build()).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, Collections.singleton("randomAllocID")).putInSyncAllocationIds(1, Collections.singleton("randomAllocID2"))).build();
    RoutingTable routingTable = RoutingTable.builder().addAsRecovery(metadata.index(index1)).addAsRecovery(metadata.index(index2)).addAsRecovery(metadata.index(index3)).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metadata(metadata).routingTable(routingTable).build();
    final String node1 = "node1";
    final String node2 = "node2";
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode(node1)).add(newNode(node2))).build();
    final ClusterState finalClusterState = allocation.reroute(clusterState, "reroute");
    logger.info("--> allocating same index primary in multiple commands should fail");
    assertThat(expectThrows(IllegalArgumentException.class, () -> {
        allocation.reroute(finalClusterState, new AllocationCommands(new AllocateStalePrimaryAllocationCommand(index1, 0, node1, true), new AllocateStalePrimaryAllocationCommand(index1, 0, node2, true)), false, false);
    }).getMessage(), containsString("primary [" + index1 + "][0] is already assigned"));
    assertThat(expectThrows(IllegalArgumentException.class, () -> {
        allocation.reroute(finalClusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand(index2, 0, node1, true), new AllocateEmptyPrimaryAllocationCommand(index2, 0, node2, true)), false, false);
    }).getMessage(), containsString("primary [" + index2 + "][0] is already assigned"));
    clusterState = allocation.reroute(clusterState, new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand(index3, 0, node1, true)), false, false).getClusterState();
    clusterState = startInitializingShardsAndReroute(allocation, clusterState);
    final ClusterState updatedClusterState = clusterState;
    assertThat(updatedClusterState.getRoutingNodes().node(node1).shardsWithState(STARTED).size(), equalTo(1));
    logger.info("--> subsequent replica allocation fails as all configured replicas have been allocated");
    assertThat(expectThrows(IllegalArgumentException.class, () -> {
        allocation.reroute(updatedClusterState, new AllocationCommands(new AllocateReplicaAllocationCommand(index3, 0, node2), new AllocateReplicaAllocationCommand(index3, 0, node2)), false, false);
    }).getMessage(), containsString("all copies of [" + index3 + "][0] are already assigned. Use the move allocation command instead"));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) AllocateReplicaAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) Test(org.junit.Test)

Example 5 with AllocateStalePrimaryAllocationCommand

use of org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand in project crate by crate.

the class AlterTableRerouteAnalyzerTest method test_promote_replica_shard_with_literals.

@Test
public void test_promote_replica_shard_with_literals() {
    AllocateStalePrimaryAllocationCommand command = analyze("ALTER TABLE users REROUTE PROMOTE REPLICA SHARD 2 ON 'n1' WITH (accept_data_loss = true)");
    assertThat(command.index(), is("users"));
    assertThat(command.shardId(), is(2));
    assertThat(command.node(), is("n1"));
    assertThat(command.acceptDataLoss(), is(true));
}
Also used : AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

AllocateStalePrimaryAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand)10 Test (org.junit.Test)7 AllocateEmptyPrimaryAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand)6 AllocationCommands (org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)6 ClusterState (org.elasticsearch.cluster.ClusterState)5 AllocateReplicaAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand)5 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)3 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)3 Metadata (org.elasticsearch.cluster.metadata.Metadata)3 CancelAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand)2 MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)2 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)2 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)2 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)2 StreamInput (org.elasticsearch.common.io.stream.StreamInput)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 ShardId (org.elasticsearch.index.shard.ShardId)2 ShardNotFoundException (org.elasticsearch.index.shard.ShardNotFoundException)2