Search in sources :

Example 16 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders in project elasticsearch by elastic.

the class BalancedSingleShardTests method testRebalanceNonStartedShardNotAllowed.

public void testRebalanceNonStartedShardNotAllowed() {
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY);
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomBoolean(), randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.UNASSIGNED, ShardRoutingState.RELOCATING));
    ShardRouting shard = clusterState.routingTable().index("idx").shard(0).primaryShard();
    MoveDecision rebalanceDecision = allocator.decideShardAllocation(shard, newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), clusterState)).getMoveDecision();
    assertSame(MoveDecision.NOT_TAKEN, rebalanceDecision);
}
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 17 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders in project elasticsearch by elastic.

the class BalancedSingleShardTests method testSingleShardBalanceProducesSameResultsAsBalanceStep.

public void testSingleShardBalanceProducesSameResultsAsBalanceStep() {
    final String[] indices = { "idx1", "idx2" };
    // Create a cluster state with 2 indices, each with 1 started primary shard, and only
    // one node initially so that all primary shards get allocated to the same node.  We are only
    // using 2 indices (i.e. 2 total primary shards) because if we have any more than 2 started shards
    // in the routing table, then we have no guarantees about the order in which the 3 or more shards
    // are selected to be rebalanced to the new node, and hence the node to which they are rebalanced
    // is not deterministic.  Using only two shards guarantees that only one of those two shards will
    // be rebalanced, and so we pick the one that was chosen to be rebalanced and execute the single-shard
    // rebalance step on it to make sure it gets assigned to the same node.
    ClusterState clusterState = ClusterStateCreationUtils.state(1, indices, 1);
    // add new nodes so one of the primaries can be rebalanced
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(clusterState.nodes());
    int numAddedNodes = randomIntBetween(1, 5);
    // randomly select a subset of the newly added nodes to set filter allocation on (but not all)
    int excludeNodesSize = randomIntBetween(0, numAddedNodes - 1);
    final Set<String> excludeNodes = new HashSet<>();
    for (int i = 0; i < numAddedNodes; i++) {
        DiscoveryNode discoveryNode = newNode(randomAsciiOfLength(7));
        nodesBuilder.add(discoveryNode);
        if (i < excludeNodesSize) {
            excludeNodes.add(discoveryNode.getId());
        }
    }
    clusterState = ClusterState.builder(clusterState).nodes(nodesBuilder).build();
    AllocationDecider allocationDecider = new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (excludeNodes.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;
        }
    };
    List<AllocationDecider> allocationDeciders = Arrays.asList(rebalanceDecider, allocationDecider);
    RoutingAllocation routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, allocationDeciders), clusterState);
    // allocate and get the node that is now relocating
    BalancedShardsAllocator allocator = new BalancedShardsAllocator(Settings.EMPTY);
    allocator.allocate(routingAllocation);
    ShardRouting shardToRebalance = null;
    for (RoutingNode routingNode : routingAllocation.routingNodes()) {
        List<ShardRouting> relocatingShards = routingNode.shardsWithState(ShardRoutingState.RELOCATING);
        if (relocatingShards.size() > 0) {
            shardToRebalance = randomFrom(relocatingShards);
            break;
        }
    }
    routingAllocation = newRoutingAllocation(new AllocationDeciders(Settings.EMPTY, allocationDeciders), clusterState);
    routingAllocation.debugDecision(true);
    ShardRouting shard = clusterState.getRoutingNodes().activePrimary(shardToRebalance.shardId());
    MoveDecision rebalanceDecision = allocator.decideShardAllocation(shard, routingAllocation).getMoveDecision();
    assertEquals(shardToRebalance.relocatingNodeId(), rebalanceDecision.getTargetNode().getId());
    // make sure all excluded nodes returned a NO decision
    for (NodeAllocationResult nodeResult : rebalanceDecision.getNodeDecisions()) {
        if (excludeNodes.contains(nodeResult.getNode().getId())) {
            assertEquals(Type.NO, nodeResult.getCanAllocateDecision().type());
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) Matchers.containsString(org.hamcrest.Matchers.containsString) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) HashSet(java.util.HashSet)

Example 18 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders in project elasticsearch by elastic.

the class ReplicaShardAllocatorTests method testThrottleWhenAllocatingToMatchingNode.

/**
     * Tests when the node to allocate to due to matching is being throttled, we move the shard to ignored
     * to wait till throttling on it is done.
     */
public void testThrottleWhenAllocatingToMatchingNode() {
    RoutingAllocation allocation = onePrimaryOnNode1And1Replica(new AllocationDeciders(Settings.EMPTY, Arrays.asList(new TestAllocateDecision(Decision.YES), new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)), new AllocationDecider(Settings.EMPTY) {

        @Override
        public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
            if (node.node().equals(node2)) {
                return Decision.THROTTLE;
            }
            return Decision.YES;
        }
    })));
    testAllocator.addData(node1, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM")).addData(node2, "MATCH", new StoreFileMetaData("file1", 10, "MATCH_CHECKSUM"));
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodes().unassigned().ignored().size(), equalTo(1));
    assertThat(allocation.routingNodes().unassigned().ignored().get(0).shardId(), equalTo(shardId));
}
Also used : SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) StoreFileMetaData(org.elasticsearch.index.store.StoreFileMetaData) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) AllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 19 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders in project elasticsearch by elastic.

the class SameShardRoutingTests method testForceAllocatePrimaryOnSameNodeNotAllowed.

public void testForceAllocatePrimaryOnSameNodeNotAllowed() {
    SameShardAllocationDecider decider = new SameShardAllocationDecider(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    ClusterState clusterState = ClusterStateCreationUtils.state("idx", randomIntBetween(2, 4), 1);
    Index index = clusterState.getMetaData().index("idx").getIndex();
    ShardRouting primaryShard = clusterState.routingTable().index(index).shard(0).primaryShard();
    RoutingNode routingNode = clusterState.getRoutingNodes().node(primaryShard.currentNodeId());
    RoutingAllocation routingAllocation = new RoutingAllocation(new AllocationDeciders(Settings.EMPTY, Collections.emptyList()), new RoutingNodes(clusterState, false), clusterState, ClusterInfo.EMPTY, System.nanoTime(), false);
    // can't force allocate same shard copy to the same node
    ShardRouting newPrimary = TestShardRouting.newShardRouting(primaryShard.shardId(), null, true, ShardRoutingState.UNASSIGNED);
    Decision decision = decider.canForceAllocatePrimary(newPrimary, routingNode, routingAllocation);
    assertEquals(Decision.Type.NO, decision.type());
    // can force allocate to a different node
    RoutingNode unassignedNode = null;
    for (RoutingNode node : clusterState.getRoutingNodes()) {
        if (node.isEmpty()) {
            unassignedNode = node;
            break;
        }
    }
    decision = decider.canForceAllocatePrimary(newPrimary, unassignedNode, routingAllocation);
    assertEquals(Decision.Type.YES, decision.type());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) SameShardAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) Index(org.elasticsearch.index.Index) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 20 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders in project elasticsearch by elastic.

the class NodeVersionAllocationDeciderTests method testRestoreDoesNotAllocateSnapshotOnOlderNodes.

public void testRestoreDoesNotAllocateSnapshotOnOlderNodes() {
    final DiscoveryNode newNode = new DiscoveryNode("newNode", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, Version.CURRENT);
    final DiscoveryNode oldNode1 = new DiscoveryNode("oldNode1", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    final DiscoveryNode oldNode2 = new DiscoveryNode("oldNode2", buildNewFakeTransportAddress(), emptyMap(), MASTER_DATA_ROLES, VersionUtils.getPreviousVersion());
    int numberOfShards = randomIntBetween(1, 3);
    final IndexMetaData.Builder indexMetaData = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(numberOfShards).numberOfReplicas(randomIntBetween(0, 3));
    for (int i = 0; i < numberOfShards; i++) {
        indexMetaData.putInSyncAllocationIds(i, Collections.singleton("_test_"));
    }
    MetaData metaData = MetaData.builder().put(indexMetaData).build();
    ClusterState state = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(RoutingTable.builder().addAsRestore(metaData.index("test"), new SnapshotRecoverySource(new Snapshot("rep1", new SnapshotId("snp1", UUIDs.randomBase64UUID())), Version.CURRENT, "test")).build()).nodes(DiscoveryNodes.builder().add(newNode).add(oldNode1).add(oldNode2)).build();
    AllocationDeciders allocationDeciders = new AllocationDeciders(Settings.EMPTY, Arrays.asList(new ReplicaAfterPrimaryActiveAllocationDecider(Settings.EMPTY), new NodeVersionAllocationDecider(Settings.EMPTY)));
    AllocationService strategy = new MockAllocationService(Settings.EMPTY, allocationDeciders, new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    state = strategy.reroute(state, new AllocationCommands(), true, false).getClusterState();
    // Make sure that primary shards are only allocated on the new node
    for (int i = 0; i < numberOfShards; i++) {
        assertEquals("newNode", state.routingTable().index("test").getShards().get(i).primaryShard().currentNodeId());
    }
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ReplicaAfterPrimaryActiveAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) NodeVersionAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.NodeVersionAllocationDecider)

Aggregations

AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)23 ClusterState (org.elasticsearch.cluster.ClusterState)17 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)16 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)13 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)10 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)8 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)6 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)6 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)5 AllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.AllocationDecider)5 MaxRetryAllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)5 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 MetaData (org.elasticsearch.cluster.metadata.MetaData)4 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)4 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)4 SameShardAllocationDecider (org.elasticsearch.cluster.routing.allocation.decider.SameShardAllocationDecider)4 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)4 HashSet (java.util.HashSet)3 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)3