Search in sources :

Example 16 with RoutingNodes

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

the class AllocateReplicaAllocationCommand 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 primaryShardRouting;
    try {
        primaryShardRouting = allocation.routingTable().shardRoutingTable(index, shardId).primaryShard();
    } catch (IndexNotFoundException | ShardNotFoundException e) {
        return explainOrThrowRejectedCommand(explain, allocation, e);
    }
    if (primaryShardRouting.unassigned()) {
        return explainOrThrowRejectedCommand(explain, allocation, "trying to allocate a replica shard [" + index + "][" + shardId + "], while corresponding primary shard is still unassigned");
    }
    List<ShardRouting> replicaShardRoutings = allocation.routingTable().shardRoutingTable(index, shardId).replicaShardsWithState(ShardRoutingState.UNASSIGNED);
    ShardRouting shardRouting;
    if (replicaShardRoutings.isEmpty()) {
        return explainOrThrowRejectedCommand(explain, allocation, "all copies of [" + index + "][" + shardId + "] are already assigned. Use the move allocation command instead");
    } else {
        shardRouting = replicaShardRoutings.get(0);
    }
    Decision decision = allocation.deciders().canAllocate(shardRouting, routingNode, allocation);
    if (decision.type() == Decision.Type.NO) {
        // don't use explainOrThrowRejectedCommand to keep the original "NO" decision
        if (explain) {
            return new RerouteExplanation(this, decision);
        }
        throw new IllegalArgumentException("[" + name() + "] allocation of [" + index + "][" + shardId + "] on node " + discoNode + " is not allowed, reason: " + decision);
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting);
    return new RerouteExplanation(this, decision);
}
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) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Decision(org.elasticsearch.cluster.routing.allocation.decider.Decision)

Example 17 with RoutingNodes

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

the class AllocateStalePrimaryAllocationCommand 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 (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");
    }
    if (shardRouting.recoverySource().getType() != RecoverySource.Type.EXISTING_STORE) {
        return explainOrThrowRejectedCommand(explain, allocation, "trying to allocate an existing primary shard [" + index + "][" + shardId + "], while no such shard has ever been active");
    }
    initializeUnassignedShard(allocation, routingNodes, routingNode, shardRouting);
    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) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 18 with RoutingNodes

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

the class AllocationService method applyFailedShards.

/**
     * Applies the failed shards. Note, only assigned ShardRouting instances that exist in the routing table should be
     * provided as parameter. Also applies a list of allocation ids to remove from the in-sync set for shard copies for which there
     * are no routing entries in the routing table.
     *
     * <p>
     * If the same instance of ClusterState is returned, then no change has been made.</p>
     */
public ClusterState applyFailedShards(final ClusterState clusterState, final List<FailedShard> failedShards, final List<StaleShard> staleShards) {
    if (staleShards.isEmpty() && failedShards.isEmpty()) {
        return clusterState;
    }
    ClusterState tmpState = IndexMetaDataUpdater.removeStaleIdsWithoutRoutings(clusterState, staleShards);
    RoutingNodes routingNodes = getMutableRoutingNodes(tmpState);
    // shuffle the unassigned nodes, just so we won't have things like poison failed shards
    routingNodes.unassigned().shuffle();
    long currentNanoTime = currentNanoTime();
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, tmpState, clusterInfoService.getClusterInfo(), currentNanoTime, false);
    for (FailedShard failedShardEntry : failedShards) {
        ShardRouting shardToFail = failedShardEntry.getRoutingEntry();
        IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(shardToFail.shardId().getIndex());
        allocation.addIgnoreShardForNode(shardToFail.shardId(), shardToFail.currentNodeId());
        // failing a primary also fails initializing replica shards, re-resolve ShardRouting
        ShardRouting failedShard = routingNodes.getByAllocationId(shardToFail.shardId(), shardToFail.allocationId().getId());
        if (failedShard != null) {
            if (failedShard != shardToFail) {
                logger.trace("{} shard routing modified in an earlier iteration (previous: {}, current: {})", shardToFail.shardId(), shardToFail, failedShard);
            }
            int failedAllocations = failedShard.unassignedInfo() != null ? failedShard.unassignedInfo().getNumFailedAllocations() : 0;
            UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, failedShardEntry.getMessage(), failedShardEntry.getFailure(), failedAllocations + 1, currentNanoTime, System.currentTimeMillis(), false, AllocationStatus.NO_ATTEMPT);
            routingNodes.failShard(logger, failedShard, unassignedInfo, indexMetaData, allocation.changes());
        } else {
            logger.trace("{} shard routing failed in an earlier iteration (routing: {})", shardToFail.shardId(), shardToFail);
        }
    }
    gatewayAllocator.applyFailedShards(allocation, failedShards);
    reroute(allocation);
    String failedShardsAsString = firstListElementsToCommaDelimitedString(failedShards, s -> s.getRoutingEntry().shardId().toString());
    return buildResultAndLogHealthChange(clusterState, allocation, "shards failed [" + failedShardsAsString + "] ...");
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 19 with RoutingNodes

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

the class AllocationService method buildResultAndLogHealthChange.

protected ClusterState buildResultAndLogHealthChange(ClusterState oldState, RoutingAllocation allocation, String reason) {
    RoutingTable oldRoutingTable = oldState.routingTable();
    RoutingNodes newRoutingNodes = allocation.routingNodes();
    final RoutingTable newRoutingTable = new RoutingTable.Builder().updateNodes(oldRoutingTable.version(), newRoutingNodes).build();
    MetaData newMetaData = allocation.updateMetaDataWithRoutingChanges(newRoutingTable);
    // validates the routing table is coherent with the cluster state metadata
    assert newRoutingTable.validate(newMetaData);
    final ClusterState.Builder newStateBuilder = ClusterState.builder(oldState).routingTable(newRoutingTable).metaData(newMetaData);
    final RestoreInProgress restoreInProgress = allocation.custom(RestoreInProgress.TYPE);
    if (restoreInProgress != null) {
        RestoreInProgress updatedRestoreInProgress = allocation.updateRestoreInfoWithRoutingChanges(restoreInProgress);
        if (updatedRestoreInProgress != restoreInProgress) {
            ImmutableOpenMap.Builder<String, ClusterState.Custom> customsBuilder = ImmutableOpenMap.builder(allocation.getCustoms());
            customsBuilder.put(RestoreInProgress.TYPE, updatedRestoreInProgress);
            newStateBuilder.customs(customsBuilder.build());
        }
    }
    final ClusterState newState = newStateBuilder.build();
    logClusterHealthStateChange(new ClusterStateHealth(oldState), new ClusterStateHealth(newState), reason);
    return newState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterStateHealth(org.elasticsearch.cluster.health.ClusterStateHealth) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap)

Example 20 with RoutingNodes

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

the class AllocationService method reroute.

public CommandsResult reroute(final ClusterState clusterState, AllocationCommands commands, boolean explain, boolean retryFailed) {
    RoutingNodes routingNodes = getMutableRoutingNodes(clusterState);
    // we don't shuffle the unassigned shards here, to try and get as close as possible to
    // a consistent result of the effect the commands have on the routing
    // this allows systems to dry run the commands, see the resulting cluster state, and act on it
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState, clusterInfoService.getClusterInfo(), currentNanoTime(), retryFailed);
    // don't short circuit deciders, we want a full explanation
    allocation.debugDecision(true);
    // we ignore disable allocation, because commands are explicit
    allocation.ignoreDisable(true);
    RoutingExplanations explanations = commands.execute(allocation, explain);
    // we revert the ignore disable flag, since when rerouting, we want the original setting to take place
    allocation.ignoreDisable(false);
    // the assumption is that commands will move / act on shards (or fail through exceptions)
    // so, there will always be shard "movements", so no need to check on reroute
    reroute(allocation);
    return new CommandsResult(explanations, buildResultAndLogHealthChange(clusterState, allocation, "reroute commands"));
}
Also used : RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes)

Aggregations

RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)77 ClusterState (org.elasticsearch.cluster.ClusterState)57 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)55 MetaData (org.elasticsearch.cluster.metadata.MetaData)53 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)53 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)24 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)15 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)12 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)10 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)10 RoutingAllocation (org.elasticsearch.cluster.routing.allocation.RoutingAllocation)9 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)6 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)6 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)5 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)5 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)4 RerouteExplanation (org.elasticsearch.cluster.routing.allocation.RerouteExplanation)4