Search in sources :

Example 21 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 22 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 23 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)

Example 24 with RoutingNodes

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

the class AllocationService method reroute.

/**
     * Reroutes the routing table based on the live nodes.
     * <p>
     * If the same instance of ClusterState is returned, then no change has been made.
     */
protected ClusterState reroute(final ClusterState clusterState, String reason, boolean debug) {
    RoutingNodes routingNodes = getMutableRoutingNodes(clusterState);
    // shuffle the unassigned nodes, just so we won't have things like poison failed shards
    routingNodes.unassigned().shuffle();
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState, clusterInfoService.getClusterInfo(), currentNanoTime(), false);
    allocation.debugDecision(debug);
    reroute(allocation);
    if (allocation.routingNodesChanged() == false) {
        return clusterState;
    }
    return buildResultAndLogHealthChange(clusterState, allocation, reason);
}
Also used : RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes)

Example 25 with RoutingNodes

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

the class AllocationService method deassociateDeadNodes.

/**
     * unassigned an shards that are associated with nodes that are no longer part of the cluster, potentially promoting replicas
     * if needed.
     */
public ClusterState deassociateDeadNodes(final ClusterState clusterState, boolean reroute, String reason) {
    RoutingNodes routingNodes = getMutableRoutingNodes(clusterState);
    // shuffle the unassigned nodes, just so we won't have things like poison failed shards
    routingNodes.unassigned().shuffle();
    RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, routingNodes, clusterState, clusterInfoService.getClusterInfo(), currentNanoTime(), false);
    // first, clear from the shards any node id they used to belong to that is now dead
    deassociateDeadNodes(allocation);
    if (reroute) {
        reroute(allocation);
    }
    if (allocation.routingNodesChanged() == false) {
        return clusterState;
    }
    return buildResultAndLogHealthChange(clusterState, allocation, reason);
}
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