Search in sources :

Example 16 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class MoveAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    DiscoveryNode fromDiscoNode = allocation.nodes().resolveNode(fromNode);
    DiscoveryNode toDiscoNode = allocation.nodes().resolveNode(toNode);
    Decision decision = null;
    boolean found = false;
    RoutingNode fromRoutingNode = allocation.routingNodes().node(fromDiscoNode.getId());
    if (fromRoutingNode == null && !fromDiscoNode.isDataNode()) {
        throw new IllegalArgumentException("[move_allocation] can't move [" + index + "][" + shardId + "] from " + fromDiscoNode + " to " + toDiscoNode + ": source [" + fromDiscoNode.getName() + "] is not a data node.");
    }
    RoutingNode toRoutingNode = allocation.routingNodes().node(toDiscoNode.getId());
    if (toRoutingNode == null && !toDiscoNode.isDataNode()) {
        throw new IllegalArgumentException("[move_allocation] can't move [" + index + "][" + shardId + "] from " + fromDiscoNode + " to " + toDiscoNode + ": source [" + toDiscoNode.getName() + "] is not a data node.");
    }
    for (ShardRouting shardRouting : fromRoutingNode) {
        if (!shardRouting.shardId().getIndexName().equals(index)) {
            continue;
        }
        if (shardRouting.shardId().id() != shardId) {
            continue;
        }
        found = true;
        // TODO we can possibly support also relocating cases, where we cancel relocation and move...
        if (!shardRouting.started()) {
            if (explain) {
                return new RerouteExplanation(this, allocation.decision(Decision.NO, "move_allocation_command", "shard " + shardId + " has not been started"));
            }
            throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", shard is not started (state = " + shardRouting.state() + "]");
        }
        decision = allocation.deciders().canAllocate(shardRouting, toRoutingNode, allocation);
        if (decision.type() == Decision.Type.NO) {
            if (explain) {
                return new RerouteExplanation(this, decision);
            }
            throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", from " + fromDiscoNode + ", to " + toDiscoNode + ", since its not allowed, reason: " + decision);
        }
        if (decision.type() == Decision.Type.THROTTLE) {
        // its being throttled, maybe have a flag to take it into account and fail? for now, just do it since the "user" wants it...
        }
        allocation.routingNodes().relocateShard(shardRouting, toRoutingNode.nodeId(), allocation.clusterInfo().getShardSize(shardRouting, ShardRouting.UNAVAILABLE_EXPECTED_SHARD_SIZE), allocation.changes());
    }
    if (!found) {
        if (explain) {
            return new RerouteExplanation(this, allocation.decision(Decision.NO, "move_allocation_command", "shard " + shardId + " not found"));
        }
        throw new IllegalArgumentException("[move_allocation] can't move " + shardId + ", failed to find it on node " + fromDiscoNode);
    }
    return new RerouteExplanation(this, decision);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 17 with Decision

use of org.elasticsearch.cluster.routing.allocation.decider.Decision in project crate by crate.

the class PrimaryShardAllocator method buildNodesToAllocate.

/**
 * Split the list of node shard states into groups yes/no/throttle based on allocation deciders
 */
private static NodesToAllocate buildNodesToAllocate(RoutingAllocation allocation, List<NodeGatewayStartedShards> nodeShardStates, ShardRouting shardRouting, boolean forceAllocate) {
    List<DecidedNode> yesNodeShards = new ArrayList<>();
    List<DecidedNode> throttledNodeShards = new ArrayList<>();
    List<DecidedNode> noNodeShards = new ArrayList<>();
    for (NodeGatewayStartedShards nodeShardState : nodeShardStates) {
        RoutingNode node = allocation.routingNodes().node(nodeShardState.getNode().getId());
        if (node == null) {
            continue;
        }
        Decision decision = forceAllocate ? allocation.deciders().canForceAllocatePrimary(shardRouting, node, allocation) : allocation.deciders().canAllocate(shardRouting, node, allocation);
        DecidedNode decidedNode = new DecidedNode(nodeShardState, decision);
        if (decision.type() == Type.THROTTLE) {
            throttledNodeShards.add(decidedNode);
        } else if (decision.type() == Type.NO) {
            noNodeShards.add(decidedNode);
        } else {
            yesNodeShards.add(decidedNode);
        }
    }
    return new NodesToAllocate(Collections.unmodifiableList(yesNodeShards), Collections.unmodifiableList(throttledNodeShards), Collections.unmodifiableList(noNodeShards));
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ArrayList(java.util.ArrayList) NodeGatewayStartedShards(org.elasticsearch.gateway.TransportNodesListGatewayStartedShards.NodeGatewayStartedShards) AllocateUnassignedDecision(org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 18 with Decision

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

the class RerouteExplanation method readFrom.

public static RerouteExplanation readFrom(StreamInput in) throws IOException {
    AllocationCommand command = in.readNamedWriteable(AllocationCommand.class);
    Decision decisions = Decision.readFrom(in);
    return new RerouteExplanation(command, decisions);
}
Also used : AllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocationCommand) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 19 with Decision

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

the class MoveDecisionTests method testDecisionWithNodeExplanations.

public void testDecisionWithNodeExplanations() {
    DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    Decision nodeDecision = randomFrom(Decision.NO, Decision.THROTTLE, Decision.YES);
    List<NodeAllocationResult> nodeDecisions = new ArrayList<>();
    nodeDecisions.add(new NodeAllocationResult(node1, nodeDecision, 2));
    nodeDecisions.add(new NodeAllocationResult(node2, nodeDecision, 1));
    MoveDecision decision = MoveDecision.cannotRemain(Decision.NO, AllocationDecision.NO, null, nodeDecisions);
    assertNotNull(decision.getAllocationDecision());
    assertNotNull(decision.getExplanation());
    assertNotNull(decision.getNodeDecisions());
    assertEquals(2, decision.getNodeDecisions().size());
    // both nodes have the same decision type but node2 has a higher weight ranking, so node2 comes first
    assertEquals("node2", decision.getNodeDecisions().iterator().next().getNode().getId());
    decision = MoveDecision.cannotRemain(Decision.NO, AllocationDecision.YES, node2, null);
    assertEquals("node2", decision.getTargetNode().getId());
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 20 with Decision

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

the class NodeAllocationResultTests method testSerialization.

public void testSerialization() throws IOException {
    DiscoveryNode node = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    Decision decision = randomFrom(Decision.YES, Decision.THROTTLE, Decision.NO);
    NodeAllocationResult explanation = new NodeAllocationResult(node, decision, 1);
    BytesStreamOutput output = new BytesStreamOutput();
    explanation.writeTo(output);
    NodeAllocationResult readExplanation = new NodeAllocationResult(output.bytes().streamInput());
    assertNodeExplanationEquals(explanation, readExplanation);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Aggregations

Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)41 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)24 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)18 AllocateUnassignedDecision (org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision)18 NodeAllocationResult (org.elasticsearch.cluster.routing.allocation.NodeAllocationResult)16 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)12 ArrayList (java.util.ArrayList)10 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)10 AllocationDecision (org.elasticsearch.cluster.routing.allocation.AllocationDecision)10 ShardId (org.elasticsearch.index.shard.ShardId)9 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)8 ShardRoutingState (org.elasticsearch.cluster.routing.ShardRoutingState)8 MoveDecision (org.elasticsearch.cluster.routing.allocation.MoveDecision)8 XContentParser (org.elasticsearch.common.xcontent.XContentParser)8 HashMap (java.util.HashMap)7 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)6 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)5 AllocationDeciders (org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders)5 Settings (org.elasticsearch.common.settings.Settings)5 Test (org.junit.Test)5