Search in sources :

Example 11 with RoutingNode

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

the class SameShardAllocationDecider method canAllocate.

@Override
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) {
    Iterable<ShardRouting> assignedShards = allocation.routingNodes().assignedShards(shardRouting.shardId());
    Decision decision = decideSameNode(shardRouting, node, allocation, assignedShards);
    if (decision.type() == Decision.Type.NO || sameHost == false) {
        // if its already a NO decision looking at the node, or we aren't configured to look at the host, return the decision
        return decision;
    }
    if (node.node() != null) {
        for (RoutingNode checkNode : allocation.routingNodes()) {
            if (checkNode.node() == null) {
                continue;
            }
            // check if its on the same host as the one we want to allocate to
            boolean checkNodeOnSameHostName = false;
            boolean checkNodeOnSameHostAddress = false;
            if (Strings.hasLength(checkNode.node().getHostAddress()) && Strings.hasLength(node.node().getHostAddress())) {
                if (checkNode.node().getHostAddress().equals(node.node().getHostAddress())) {
                    checkNodeOnSameHostAddress = true;
                }
            } else if (Strings.hasLength(checkNode.node().getHostName()) && Strings.hasLength(node.node().getHostName())) {
                if (checkNode.node().getHostName().equals(node.node().getHostName())) {
                    checkNodeOnSameHostName = true;
                }
            }
            if (checkNodeOnSameHostAddress || checkNodeOnSameHostName) {
                for (ShardRouting assignedShard : assignedShards) {
                    if (checkNode.nodeId().equals(assignedShard.currentNodeId())) {
                        String hostType = checkNodeOnSameHostAddress ? "address" : "name";
                        String host = checkNodeOnSameHostAddress ? node.node().getHostAddress() : node.node().getHostName();
                        return allocation.decision(Decision.NO, NAME, "the shard cannot be allocated on host %s [%s], where it already exists on node [%s]; " + "set cluster setting [%s] to false to allow multiple nodes on the same host to hold the same " + "shard copies", hostType, host, node.nodeId(), CLUSTER_ROUTING_ALLOCATION_SAME_HOST_SETTING.getKey());
                    }
                }
            }
        }
    }
    return allocation.decision(Decision.YES, NAME, "the shard does not exist on the same host");
}
Also used : RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 12 with RoutingNode

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

the class CancelAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    DiscoveryNode discoNode = allocation.nodes().resolveNode(node);
    ShardRouting shardRouting = null;
    RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    IndexMetaData indexMetaData = null;
    if (routingNode != null) {
        indexMetaData = allocation.metaData().index(index());
        if (indexMetaData == null) {
            throw new IndexNotFoundException(index());
        }
        ShardId shardId = new ShardId(indexMetaData.getIndex(), shardId());
        shardRouting = routingNode.getByShardId(shardId);
    }
    if (shardRouting == null) {
        if (explain) {
            return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command", "can't cancel " + shardId + ", failed to find it on node " + discoNode));
        }
        throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + ", failed to find it on node " + discoNode);
    }
    if (shardRouting.primary() && allowPrimary == false) {
        if ((shardRouting.initializing() && shardRouting.relocatingNodeId() != null) == false) {
            // only allow cancelling initializing shard of primary relocation without allowPrimary flag
            if (explain) {
                return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command", "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and " + shardRouting.state().name().toLowerCase(Locale.ROOT)));
            }
            throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " + discoNode + ", shard is primary and " + shardRouting.state().name().toLowerCase(Locale.ROOT));
        }
    }
    routingNodes.failShard(Loggers.getLogger(CancelAllocationCommand.class), shardRouting, new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null), indexMetaData, allocation.changes());
    return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command", "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 13 with RoutingNode

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

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;
    for (ShardRouting shardRouting : allocation.routingNodes().node(fromDiscoNode.getId())) {
        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() + "]");
        }
        RoutingNode toRoutingNode = allocation.routingNodes().node(toDiscoNode.getId());
        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 14 with RoutingNode

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

the class SimpleAllocationIT method testSaneAllocation.

/**
     * Test for
     * https://groups.google.com/d/msg/elasticsearch/y-SY_HyoB-8/EZdfNt9VO44J
     */
public void testSaneAllocation() {
    assertAcked(prepareCreate("test", 3));
    ensureGreen();
    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(0));
    for (RoutingNode node : state.getRoutingNodes()) {
        if (!node.isEmpty()) {
            assertThat(node.size(), equalTo(2));
        }
    }
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_REPLICAS, 0)).execute().actionGet();
    ensureGreen();
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(0));
    for (RoutingNode node : state.getRoutingNodes()) {
        if (!node.isEmpty()) {
            assertThat(node.size(), equalTo(1));
        }
    }
    // create another index
    assertAcked(prepareCreate("test2", 3));
    ensureGreen();
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_REPLICAS, 1)).execute().actionGet();
    ensureGreen();
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(0));
    for (RoutingNode node : state.getRoutingNodes()) {
        if (!node.isEmpty()) {
            assertThat(node.size(), equalTo(4));
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode)

Example 15 with RoutingNode

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

the class AllocateEmptyPrimaryAllocationCommand method execute.

@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    final DiscoveryNode discoNode;
    try {
        discoNode = allocation.nodes().resolveNode(node);
    } catch (IllegalArgumentException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    final RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    if (routingNode == null) {
        return explainOrThrowMissingRoutingNode(allocation, explain, discoNode);
    }
    final ShardRouting shardRouting;
    try {
        shardRouting = allocation.routingTable().shardRoutingTable(index, shardId).primaryShard();
    } catch (IndexNotFoundException | ShardNotFoundException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    if (shardRouting.unassigned() == false) {
        return explainOrThrowRejectedCommand(explain, allocation, "primary [" + index + "][" + shardId + "] is already assigned");
    }
    if (shardRouting.recoverySource().getType() != RecoverySource.Type.EMPTY_STORE && acceptDataLoss == false) {
        return explainOrThrowRejectedCommand(explain, allocation, "allocating an empty primary for [" + index + "][" + shardId + "] can result in data loss. Please confirm by setting the accept_data_loss parameter to true");
    }
    UnassignedInfo unassignedInfoToUpdate = null;
    if (shardRouting.unassignedInfo().getReason() != UnassignedInfo.Reason.FORCED_EMPTY_PRIMARY) {
        unassignedInfoToUpdate = new UnassignedInfo(UnassignedInfo.Reason.FORCED_EMPTY_PRIMARY, "force empty allocation from previous reason " + shardRouting.unassignedInfo().getReason() + ", " + shardRouting.unassignedInfo().getMessage(), shardRouting.unassignedInfo().getFailure(), 0, System.nanoTime(), System.currentTimeMillis(), false, shardRouting.unassignedInfo().getLastAllocationStatus());
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting, unassignedInfoToUpdate, StoreRecoverySource.EMPTY_STORE_INSTANCE);
    return new RerouteExplanation(this, allocation.decision(Decision.YES, name() + " (allocation command)", "ignore deciders"));
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)61 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)45 ClusterState (org.elasticsearch.cluster.ClusterState)28 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)23 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)20 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)16 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)15 MetaData (org.elasticsearch.cluster.metadata.MetaData)13 Settings (org.elasticsearch.common.settings.Settings)12 Decision (org.elasticsearch.cluster.routing.allocation.decider.Decision)10 ShardId (org.elasticsearch.index.shard.ShardId)10 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)9 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)9 Matchers.containsString (org.hamcrest.Matchers.containsString)9 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)8 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)8 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)8 IndexShard (org.elasticsearch.index.shard.IndexShard)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7