Search in sources :

Example 41 with MoveAllocationCommand

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

the class AllocationCommandsTests method testMoveShardCommand.

@Test
public void testMoveShardCommand() {
    AllocationService allocation = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).build());
    logger.info("creating an index with 1 shard, no replica");
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, false).build()).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 = startInitializingShardsAndReroute(allocation, clusterState);
    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";
    }
    ClusterState newState = allocation.reroute(clusterState, new AllocationCommands(new MoveAllocationCommand("test", 0, existingNodeId, toNodeId)), false, false).getClusterState();
    assertThat(newState, not(equalTo(clusterState)));
    clusterState = newState;
    assertThat(clusterState.getRoutingNodes().node(existingNodeId).iterator().next().state(), equalTo(ShardRoutingState.RELOCATING));
    assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    logger.info("finish moving the shard");
    clusterState = startInitializingShardsAndReroute(allocation, clusterState);
    assertThat(clusterState.getRoutingNodes().node(existingNodeId).isEmpty(), equalTo(true));
    assertThat(clusterState.getRoutingNodes().node(toNodeId).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) Test(org.junit.Test)

Example 42 with MoveAllocationCommand

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

the class AllocationCommandsTests method testSerialization.

@Test
public void testSerialization() throws Exception {
    AllocationCommands commands = new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 1, "node1", true), new AllocateStalePrimaryAllocationCommand("test", 2, "node1", true), new AllocateReplicaAllocationCommand("test", 2, "node1"), new MoveAllocationCommand("test", 3, "node2", "node3"), new CancelAllocationCommand("test", 4, "node5", true));
    BytesStreamOutput bytes = new BytesStreamOutput();
    AllocationCommands.writeTo(commands, bytes);
    StreamInput in = bytes.bytes().streamInput();
    // Since the commands are named writeable we need to register them and wrap the input stream
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(NetworkModule.getNamedWriteables());
    in = new NamedWriteableAwareStreamInput(in, namedWriteableRegistry);
    // Now we can read them!
    AllocationCommands sCommands = AllocationCommands.readFrom(in);
    assertThat(sCommands.commands().size(), equalTo(5));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).shardId(), equalTo(1));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).index(), equalTo("test"));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).node(), equalTo("node1"));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).acceptDataLoss(), equalTo(true));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).shardId(), equalTo(2));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).index(), equalTo("test"));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).node(), equalTo("node1"));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).acceptDataLoss(), equalTo(true));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).shardId(), equalTo(2));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).index(), equalTo("test"));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).node(), equalTo("node1"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).shardId(), equalTo(3));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).index(), equalTo("test"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).fromNode(), equalTo("node2"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).toNode(), equalTo("node3"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).shardId(), equalTo(4));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).index(), equalTo("test"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).node(), equalTo("node5"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).allowPrimary(), equalTo(true));
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) AllocateStalePrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) CancelAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) AllocateReplicaAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) Test(org.junit.Test)

Aggregations

MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)42 ClusterState (org.elasticsearch.cluster.ClusterState)24 AllocationCommands (org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)15 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)14 Test (org.junit.Test)12 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)10 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 MetaData (org.elasticsearch.cluster.metadata.MetaData)9 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)9 Index (org.elasticsearch.index.Index)9 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 Metadata (org.elasticsearch.cluster.metadata.Metadata)5 AllocateEmptyPrimaryAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand)5 CancelAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand)5 Settings (org.elasticsearch.common.settings.Settings)5 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)4 AllocateReplicaAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4