Search in sources :

Example 61 with ShardRouting

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

the class IndexMetaDataUpdater method removeStaleIdsWithoutRoutings.

/**
     * Removes allocation ids from the in-sync set for shard copies for which there is no routing entries in the routing table.
     * This method is called in AllocationService before any changes to the routing table are made.
     */
public static ClusterState removeStaleIdsWithoutRoutings(ClusterState clusterState, List<StaleShard> staleShards) {
    MetaData oldMetaData = clusterState.metaData();
    RoutingTable oldRoutingTable = clusterState.routingTable();
    MetaData.Builder metaDataBuilder = null;
    // group staleShards entries by index
    for (Map.Entry<Index, List<StaleShard>> indexEntry : staleShards.stream().collect(Collectors.groupingBy(fs -> fs.getShardId().getIndex())).entrySet()) {
        final IndexMetaData oldIndexMetaData = oldMetaData.getIndexSafe(indexEntry.getKey());
        IndexMetaData.Builder indexMetaDataBuilder = null;
        // group staleShards entries by shard id
        for (Map.Entry<ShardId, List<StaleShard>> shardEntry : indexEntry.getValue().stream().collect(Collectors.groupingBy(staleShard -> staleShard.getShardId())).entrySet()) {
            int shardNumber = shardEntry.getKey().getId();
            Set<String> oldInSyncAllocations = oldIndexMetaData.inSyncAllocationIds(shardNumber);
            Set<String> idsToRemove = shardEntry.getValue().stream().map(e -> e.getAllocationId()).collect(Collectors.toSet());
            assert idsToRemove.stream().allMatch(id -> oldRoutingTable.getByAllocationId(shardEntry.getKey(), id) == null) : "removing stale ids: " + idsToRemove + ", some of which have still a routing entry: " + oldRoutingTable;
            Set<String> remainingInSyncAllocations = Sets.difference(oldInSyncAllocations, idsToRemove);
            assert remainingInSyncAllocations.isEmpty() == false : "Set of in-sync ids cannot become empty for shard " + shardEntry.getKey() + " (before: " + oldInSyncAllocations + ", ids to remove: " + idsToRemove + ")";
            // (see ShardRouting#allocatedPostIndexCreate)
            if (remainingInSyncAllocations.isEmpty() == false) {
                if (indexMetaDataBuilder == null) {
                    indexMetaDataBuilder = IndexMetaData.builder(oldIndexMetaData);
                }
                indexMetaDataBuilder.putInSyncAllocationIds(shardNumber, remainingInSyncAllocations);
            }
        }
        if (indexMetaDataBuilder != null) {
            if (metaDataBuilder == null) {
                metaDataBuilder = MetaData.builder(oldMetaData);
            }
            metaDataBuilder.put(indexMetaDataBuilder);
        }
    }
    if (metaDataBuilder != null) {
        return ClusterState.builder(clusterState).metaData(metaDataBuilder).build();
    } else {
        return clusterState;
    }
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardId(org.elasticsearch.index.shard.ShardId) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) HashMap(java.util.HashMap) RoutingChangesObserver(org.elasticsearch.cluster.routing.RoutingChangesObserver) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) Collectors(java.util.stream.Collectors) Sets(org.elasticsearch.common.util.set.Sets) HashSet(java.util.HashSet) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) ClusterState(org.elasticsearch.cluster.ClusterState) List(java.util.List) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) Map(java.util.Map) Comparator(java.util.Comparator) Collections(java.util.Collections) Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 62 with ShardRouting

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

Example 63 with ShardRouting

use of org.elasticsearch.cluster.routing.ShardRouting 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 64 with ShardRouting

use of org.elasticsearch.cluster.routing.ShardRouting 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 65 with ShardRouting

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

Aggregations

ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)252 ClusterState (org.elasticsearch.cluster.ClusterState)119 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)71 ShardId (org.elasticsearch.index.shard.ShardId)60 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)59 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)56 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)52 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)51 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)46 MetaData (org.elasticsearch.cluster.metadata.MetaData)45 Index (org.elasticsearch.index.Index)36 Settings (org.elasticsearch.common.settings.Settings)32 ArrayList (java.util.ArrayList)29 HashSet (java.util.HashSet)29 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)29 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)28 IOException (java.io.IOException)27 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)26 HashMap (java.util.HashMap)25 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)25