Search in sources :

Example 21 with ShardRouting

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

the class BalancedSingleShardTests method testRebalancePossible.

public void testRebalancePossible() {
    AllocationDecider canAllocateDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            return Decision.YES;
        }
    };
    Tuple<ClusterState, MoveDecision> rebalance = setupStateAndRebalance(canAllocateDecider, Settings.EMPTY, true);
    ClusterState clusterState = rebalance.v1();
    MoveDecision rebalanceDecision = rebalance.v2();
    assertEquals(Type.YES, rebalanceDecision.getClusterRebalanceDecision().type());
    assertNotNull(rebalanceDecision.getExplanation());
    assertEquals(clusterState.nodes().getSize() - 1, rebalanceDecision.getNodeDecisions().size());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Example 22 with ShardRouting

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

the class BalancedSingleShardTests method executeRebalanceFor.

private MoveDecision executeRebalanceFor(final ShardRouting shardRouting, final ClusterState clusterState, final Set<String> noDecisionNodes, final float threshold) {
    Settings settings = Settings.EMPTY;
    if (Float.compare(-1.0f, threshold) != 0) {
        settings = Settings.builder().put(BalancedShardsAllocator.THRESHOLD_SETTING.getKey(), threshold).build();
    }
    AllocationDecider allocationDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (noDecisionNodes.contains(node.nodeId())) {
                return Decision.NO;
            }
            return Decision.YES;
        }
    };
    AllocationDecider rebalanceDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation allocation) {
            return Decision.YES;
        }
    };
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(settings);
    RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, Arrays.asList(allocationDecider, rebalanceDecider)), clusterState);
    return allocator.decideShardAllocation(shardRouting, routingAllocation).getMoveDecision();
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)

Example 23 with ShardRouting

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

the class AwarenessAllocationTests method testUnassignedShardsWithUnbalancedZones.

public void testUnassignedShardsWithUnbalancedZones() {
    AllocationService strategy = createAllocationService(Settings.builder().put("cluster.routing.allocation.node_concurrent_recoveries", 10).put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always").put("cluster.routing.allocation.awareness.attributes", "zone").build());
    logger.info("Building initial routing table for 'testUnassignedShardsWithUnbalancedZones'");
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(4)).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 5 nodes in different zones and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("A-0", singletonMap("zone", "a"))).add(newNode("A-1", singletonMap("zone", "a"))).add(newNode("A-2", singletonMap("zone", "a"))).add(newNode("A-3", singletonMap("zone", "a"))).add(newNode("A-4", singletonMap("zone", "a"))).add(newNode("B-0", singletonMap("zone", "b")))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1));
    logger.info("--> start the shard (primary)");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(3));
    // Unassigned shard is expected.
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1));
    // Cancel all initializing shards and move started primary to another node.
    AllocationCommands commands = new AllocationCommands();
    String primaryNode = null;
    for (ShardRouting routing : clusterState.routingTable().allShards()) {
        if (routing.primary()) {
            primaryNode = routing.currentNodeId();
        } else if (routing.initializing()) {
            commands.add(new CancelAllocationCommand(routing.shardId().getIndexName(), routing.id(), routing.currentNodeId(), false));
        }
    }
    commands.add(new MoveAllocationCommand("test", 0, primaryNode, "A-4"));
    clusterState = strategy.reroute(clusterState, commands, false, false).getClusterState();
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(1));
    // +1 for relocating shard.
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(4));
    // Still 1 unassigned.
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1));
}
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) CancelAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.CancelAllocationCommand) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands)

Example 24 with ShardRouting

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

the class BalancedSingleShardTests method testRebalanceNotAllowedDuringPendingAsyncFetch.

public void testRebalanceNotAllowedDuringPendingAsyncFetch() {
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY);
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), ShardRoutingState.STARTED);
    ShardRouting shard = clusterState.routingTable().index("idx").shard(0).primaryShard();
    RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), clusterState);
    routingAllocation.setHasPendingAsyncFetch();
    MoveDecision rebalanceDecision = allocator.decideShardAllocation(shard, routingAllocation).getMoveDecision();
    assertNotNull(rebalanceDecision.getClusterRebalanceDecision());
    assertEquals(AllocationDecision.AWAITING_INFO, rebalanceDecision.getAllocationDecision());
    assertThat(rebalanceDecision.getExplanation(), startsWith("cannot rebalance as information about existing copies of this shard in the cluster is still being gathered"));
    assertEquals(clusterState.nodes().getSize() - 1, rebalanceDecision.getNodeDecisions().size());
    assertNull(rebalanceDecision.getTargetNode());
    assertAssignedNodeRemainsSame(allocator, routingAllocation, shard);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 25 with ShardRouting

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

the class ClusterRebalanceRoutingTests method testRebalanceWithIgnoredUnassignedShards.

public void testRebalanceWithIgnoredUnassignedShards() {
    final AtomicBoolean allocateTest1 = new AtomicBoolean(false);
    AllocationService strategy = createAllocationService(Settings.EMPTY, new TestGatewayAllocator() {

        @Override
        public void allocateUnassigned(RoutingAllocation allocation) {
            if (allocateTest1.get() == false) {
                RoutingNodes.UnassignedShards unassigned = allocation.routingNodes().unassigned();
                RoutingNodes.UnassignedShards.UnassignedIterator iterator = unassigned.iterator();
                while (iterator.hasNext()) {
                    ShardRouting next = iterator.next();
                    if ("test1".equals(next.index().getName())) {
                        iterator.removeAndIgnore(UnassignedInfo.AllocationStatus.NO_ATTEMPT, allocation.changes());
                    }
                }
            }
            super.allocateUnassigned(allocation);
        }
    });
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)).put(IndexMetaData.builder("test1").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0)).build();
    RoutingTable initialRoutingTable = RoutingTable.builder().addAsNew(metaData.index("test")).addAsNew(metaData.index("test1")).build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build();
    logger.info("start two nodes");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(INITIALIZING));
    }
    logger.debug("start all the primary shards for test");
    RoutingNodes routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test", INITIALIZING));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    logger.debug("now, start 1 more node, check that rebalancing will not happen since we unassigned shards");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder(clusterState.nodes()).add(newNode("node2"))).build();
    logger.debug("reroute and check that nothing has changed");
    ClusterState resultingState = strategy.reroute(clusterState, "reroute");
    assertThat(resultingState, equalTo(clusterState));
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(UNASSIGNED));
    }
    logger.debug("now set allocateTest1 to true and reroute we should see the [test1] index initializing");
    allocateTest1.set(true);
    resultingState = strategy.reroute(clusterState, "reroute");
    assertThat(resultingState, not(equalTo(clusterState)));
    clusterState = resultingState;
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(INITIALIZING));
    }
    logger.debug("now start initializing shards and expect exactly one rebalance from node1 to node 2 since index [test] is all on node1");
    routingNodes = clusterState.getRoutingNodes();
    clusterState = strategy.applyStartedShards(clusterState, routingNodes.shardsWithState("test1", INITIALIZING));
    for (int i = 0; i < clusterState.routingTable().index("test1").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test1").shard(i).shards().size(), equalTo(1));
        assertThat(clusterState.routingTable().index("test1").shard(i).primaryShard().state(), equalTo(STARTED));
    }
    int numStarted = 0;
    int numRelocating = 0;
    for (int i = 0; i < clusterState.routingTable().index("test").shards().size(); i++) {
        assertThat(clusterState.routingTable().index("test").shard(i).shards().size(), equalTo(1));
        if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == STARTED) {
            numStarted++;
        } else if (clusterState.routingTable().index("test").shard(i).primaryShard().state() == RELOCATING) {
            numRelocating++;
        }
    }
    assertEquals(numStarted, 1);
    assertEquals(numRelocating, 1);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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

ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)372 ClusterState (org.elasticsearch.cluster.ClusterState)162 ShardId (org.elasticsearch.index.shard.ShardId)104 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)92 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)79 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)75 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)75 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)74 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)71 Index (org.elasticsearch.index.Index)50 HashSet (java.util.HashSet)49 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)49 ArrayList (java.util.ArrayList)48 IndexShard (org.elasticsearch.index.shard.IndexShard)48 Settings (org.elasticsearch.common.settings.Settings)46 MetaData (org.elasticsearch.cluster.metadata.MetaData)45 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)44 IOException (java.io.IOException)43 HashMap (java.util.HashMap)41 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)41