Search in sources :

Example 6 with AllocateReplicaAllocationCommand

use of org.opensearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand in project OpenSearch by opensearch-project.

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.opensearch.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(DiscoveryNodeRole.MASTER_ROLE)))).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 [test2]"));
    }
    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 = startInitializingShardsAndReroute(allocation, clusterState);
    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 = startInitializingShardsAndReroute(allocation, clusterState);
    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.opensearch.cluster.ClusterState) AllocateStalePrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocateReplicaAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.opensearch.cluster.routing.allocation.command.AllocationCommands) ShardId(org.opensearch.index.shard.ShardId) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Aggregations

AllocateReplicaAllocationCommand (org.opensearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand)6 AllocationCommands (org.opensearch.cluster.routing.allocation.command.AllocationCommands)5 ClusterState (org.opensearch.cluster.ClusterState)4 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)4 Metadata (org.opensearch.cluster.metadata.Metadata)4 AllocateEmptyPrimaryAllocationCommand (org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand)4 RoutingTable (org.opensearch.cluster.routing.RoutingTable)3 AllocateStalePrimaryAllocationCommand (org.opensearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand)3 Matchers.containsString (org.hamcrest.Matchers.containsString)2 CancelAllocationCommand (org.opensearch.cluster.routing.allocation.command.CancelAllocationCommand)2 MoveAllocationCommand (org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OpenSearchException (org.opensearch.OpenSearchException)1 ClusterName (org.opensearch.cluster.ClusterName)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)1 RerouteExplanation (org.opensearch.cluster.routing.allocation.RerouteExplanation)1 RoutingExplanations (org.opensearch.cluster.routing.allocation.RoutingExplanations)1 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)1