Search in sources :

Example 1 with AllocationDeciders

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

the class PrimaryShardAllocatorTests method testForceAllocatePrimary.

/**
     * Tests that when the nodes with prior copies of the given shard all return a decision of NO, but
     * {@link AllocationDecider#canForceAllocatePrimary(ShardRouting, RoutingNode, RoutingAllocation)}
     * returns a YES decision for at least one of those NO nodes, then we force allocate to one of them
     */
public void testForceAllocatePrimary() {
    testAllocator.addData(node1, "allocId1", randomBoolean());
    AllocationDeciders deciders = new AllocationDeciders(Settings.EMPTY, Arrays.asList(// the allocator will see if it can force assign the primary, where the decision will be YES
    new TestAllocateDecision(randomBoolean() ? Decision.YES : Decision.NO), getNoDeciderThatAllowsForceAllocate()));
    RoutingAllocation allocation = routingAllocationWithOnePrimaryNoReplicas(deciders, CLUSTER_RECOVERED, "allocId1");
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodesChanged(), equalTo(true));
    assertTrue(allocation.routingNodes().unassigned().ignored().isEmpty());
    assertEquals(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).size(), 1);
    assertEquals(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).get(0).currentNodeId(), node1.getId());
}
Also used : AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Example 2 with AllocationDeciders

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

the class PrimaryShardAllocatorTests method testDontForceAllocateOnThrottleDecision.

/**
     * Tests that when the nodes with prior copies of the given shard return a THROTTLE decision,
     * then we do not force allocate to that node but instead throttle.
     */
public void testDontForceAllocateOnThrottleDecision() {
    testAllocator.addData(node1, "allocId1", randomBoolean());
    AllocationDeciders deciders = new AllocationDeciders(Settings.EMPTY, Arrays.asList(// force allocating the shard, we still THROTTLE due to the decision from TestAllocateDecision
    new TestAllocateDecision(Decision.THROTTLE), getNoDeciderThatAllowsForceAllocate()));
    RoutingAllocation allocation = routingAllocationWithOnePrimaryNoReplicas(deciders, CLUSTER_RECOVERED, "allocId1");
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodesChanged(), equalTo(true));
    List<ShardRouting> ignored = allocation.routingNodes().unassigned().ignored();
    assertEquals(ignored.size(), 1);
    assertEquals(ignored.get(0).unassignedInfo().getLastAllocationStatus(), AllocationStatus.DECIDERS_THROTTLED);
    assertTrue(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).isEmpty());
}
Also used : AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 3 with AllocationDeciders

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

the class PrimaryShardAllocatorTests method testDontAllocateOnNoOrThrottleForceAllocationDecision.

/**
     * Tests that when the nodes with prior copies of the given shard all return a decision of NO, and
     * {@link AllocationDecider#canForceAllocatePrimary(ShardRouting, RoutingNode, RoutingAllocation)}
     * returns a NO or THROTTLE decision for a node, then we do not force allocate to that node.
     */
public void testDontAllocateOnNoOrThrottleForceAllocationDecision() {
    testAllocator.addData(node1, "allocId1", randomBoolean());
    boolean forceDecisionNo = randomBoolean();
    AllocationDeciders deciders = new AllocationDeciders(Settings.EMPTY, Arrays.asList(// so the shard will remain un-initialized
    new TestAllocateDecision(Decision.NO), forceDecisionNo ? getNoDeciderThatDeniesForceAllocate() : getNoDeciderThatThrottlesForceAllocate()));
    RoutingAllocation allocation = routingAllocationWithOnePrimaryNoReplicas(deciders, CLUSTER_RECOVERED, "allocId1");
    testAllocator.allocateUnassigned(allocation);
    assertThat(allocation.routingNodesChanged(), equalTo(true));
    List<ShardRouting> ignored = allocation.routingNodes().unassigned().ignored();
    assertEquals(ignored.size(), 1);
    assertEquals(ignored.get(0).unassignedInfo().getLastAllocationStatus(), forceDecisionNo ? AllocationStatus.DECIDERS_NO : AllocationStatus.DECIDERS_THROTTLED);
    assertTrue(allocation.routingNodes().shardsWithState(ShardRoutingState.INITIALIZING).isEmpty());
}
Also used : AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 4 with AllocationDeciders

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

the class ClusterRerouteTests method testClusterStateUpdateTask.

public void testClusterStateUpdateTask() {
    AllocationService allocationService = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    ClusterState clusterState = createInitialClusterState(allocationService);
    ClusterRerouteRequest req = new ClusterRerouteRequest();
    req.dryRun(true);
    AtomicReference<ClusterRerouteResponse> responseRef = new AtomicReference<>();
    ActionListener<ClusterRerouteResponse> responseActionListener = new ActionListener<ClusterRerouteResponse>() {

        @Override
        public void onResponse(ClusterRerouteResponse clusterRerouteResponse) {
            responseRef.set(clusterRerouteResponse);
        }

        @Override
        public void onFailure(Exception e) {
        }
    };
    TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask task = new TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask(logger, allocationService, req, responseActionListener);
    ClusterState execute = task.execute(clusterState);
    // dry-run
    assertSame(execute, clusterState);
    task.onAllNodesAcked(null);
    assertNotSame(responseRef.get().getState(), execute);
    // now we allocate
    req.dryRun(false);
    final int retries = MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY.get(Settings.EMPTY);
    // now fail it N-1 times
    for (int i = 0; i < retries; i++) {
        ClusterState newState = task.execute(clusterState);
        // dry-run=false
        assertNotSame(newState, clusterState);
        clusterState = newState;
        RoutingTable routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i);
        List<FailedShard> failedShards = Collections.singletonList(new FailedShard(routingTable.index("idx").shard(0).shards().get(0), "boom" + i, new UnsupportedOperationException()));
        newState = allocationService.applyFailedShards(clusterState, failedShards);
        assertThat(newState, not(equalTo(clusterState)));
        clusterState = newState;
        routingTable = clusterState.routingTable();
        assertEquals(routingTable.index("idx").shards().size(), 1);
        if (i == retries - 1) {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
        } else {
            assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
        }
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), i + 1);
    }
    // without retry_failed we won't allocate that shard
    ClusterState newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    task.onAllNodesAcked(null);
    assertSame(responseRef.get().getState(), newState);
    RoutingTable routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
    // now we manually retry and get the shard back into initializing
    req.setRetryFailed(true);
    newState = task.execute(clusterState);
    // dry-run=false
    assertNotSame(newState, clusterState);
    clusterState = newState;
    routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).unassignedInfo().getNumFailedAllocations(), retries);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) FailedShard(org.elasticsearch.cluster.routing.allocation.FailedShard) AtomicReference(java.util.concurrent.atomic.AtomicReference) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) IOException(java.io.IOException) ActionListener(org.elasticsearch.action.ActionListener) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 5 with AllocationDeciders

use of org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders 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)

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