Search in sources :

Example 11 with ShardSnapshotStatus

use of org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotsService method processWaitingShardsAndRemovedNodes.

private static ImmutableOpenMap<ShardId, ShardSnapshotStatus> processWaitingShardsAndRemovedNodes(ImmutableOpenMap<ShardId, ShardSnapshotStatus> snapshotShards, RoutingTable routingTable, DiscoveryNodes nodes, Map<ShardId, ShardSnapshotStatus> knownFailures) {
    boolean snapshotChanged = false;
    ImmutableOpenMap.Builder<ShardId, ShardSnapshotStatus> shards = ImmutableOpenMap.builder();
    for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : snapshotShards) {
        ShardSnapshotStatus shardStatus = shardEntry.value;
        ShardId shardId = shardEntry.key;
        if (shardStatus.equals(ShardSnapshotStatus.UNASSIGNED_QUEUED)) {
            // this shard snapshot is waiting for a previous snapshot to finish execution for this shard
            final ShardSnapshotStatus knownFailure = knownFailures.get(shardId);
            if (knownFailure == null) {
                // if no failure is known for the shard we keep waiting
                shards.put(shardId, shardStatus);
            } else {
                // If a failure is known for an execution we waited on for this shard then we fail with the same exception here
                // as well
                snapshotChanged = true;
                shards.put(shardId, knownFailure);
            }
        } else if (shardStatus.state() == ShardState.WAITING) {
            IndexRoutingTable indexShardRoutingTable = routingTable.index(shardId.getIndex());
            if (indexShardRoutingTable != null) {
                IndexShardRoutingTable shardRouting = indexShardRoutingTable.shard(shardId.id());
                if (shardRouting != null && shardRouting.primaryShard() != null) {
                    if (shardRouting.primaryShard().started()) {
                        // Shard that we were waiting for has started on a node, let's process it
                        snapshotChanged = true;
                        logger.trace("starting shard that we were waiting for [{}] on node [{}]", shardId, shardStatus.nodeId());
                        shards.put(shardId, new ShardSnapshotStatus(shardRouting.primaryShard().currentNodeId(), shardStatus.generation()));
                        continue;
                    } else if (shardRouting.primaryShard().initializing() || shardRouting.primaryShard().relocating()) {
                        // Shard that we were waiting for hasn't started yet or still relocating - will continue to wait
                        shards.put(shardId, shardStatus);
                        continue;
                    }
                }
            }
            // Shard that we were waiting for went into unassigned state or disappeared - giving up
            snapshotChanged = true;
            logger.warn("failing snapshot of shard [{}] on unassigned shard [{}]", shardId, shardStatus.nodeId());
            final ShardSnapshotStatus failedState = new ShardSnapshotStatus(shardStatus.nodeId(), ShardState.FAILED, "shard is unassigned", shardStatus.generation());
            shards.put(shardId, failedState);
            knownFailures.put(shardId, failedState);
        } else if (shardStatus.state().completed() == false && shardStatus.nodeId() != null) {
            if (nodes.nodeExists(shardStatus.nodeId())) {
                shards.put(shardId, shardStatus);
            } else {
                // TODO: Restart snapshot on another node?
                snapshotChanged = true;
                logger.warn("failing snapshot of shard [{}] on closed node [{}]", shardId, shardStatus.nodeId());
                final ShardSnapshotStatus failedState = new ShardSnapshotStatus(shardStatus.nodeId(), ShardState.FAILED, "node shutdown", shardStatus.generation());
                shards.put(shardId, failedState);
                knownFailures.put(shardId, failedState);
            }
        } else {
            shards.put(shardId, shardStatus);
        }
    }
    if (snapshotChanged) {
        return shards.build();
    } else {
        return null;
    }
}
Also used : RepositoryShardId(org.opensearch.repositories.RepositoryShardId) ShardId(org.opensearch.index.shard.ShardId) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ShardSnapshotStatus(org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap)

Example 12 with ShardSnapshotStatus

use of org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotsService method startShardSnapshotAfterClone.

/**
 * Creates a {@link ShardSnapshotStatus} entry for a snapshot after the shard has become available for snapshotting as a result
 * of a snapshot clone completing.
 *
 * @param currentState            current cluster state
 * @param shardGeneration         shard generation of the shard in the repository
 * @param shardId shard id of the shard that just finished cloning
 * @return shard snapshot status
 */
private static ShardSnapshotStatus startShardSnapshotAfterClone(ClusterState currentState, String shardGeneration, ShardId shardId) {
    final ShardRouting primary = currentState.routingTable().index(shardId.getIndex()).shard(shardId.id()).primaryShard();
    final ShardSnapshotStatus shardSnapshotStatus;
    if (primary == null || !primary.assignedToNode()) {
        shardSnapshotStatus = new ShardSnapshotStatus(null, ShardState.MISSING, "primary shard is not allocated", shardGeneration);
    } else if (primary.relocating() || primary.initializing()) {
        shardSnapshotStatus = new ShardSnapshotStatus(primary.currentNodeId(), ShardState.WAITING, shardGeneration);
    } else if (primary.started() == false) {
        shardSnapshotStatus = new ShardSnapshotStatus(primary.currentNodeId(), ShardState.MISSING, "primary shard hasn't been started yet", shardGeneration);
    } else {
        shardSnapshotStatus = new ShardSnapshotStatus(primary.currentNodeId(), shardGeneration);
    }
    return shardSnapshotStatus;
}
Also used : ShardSnapshotStatus(org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Aggregations

ShardSnapshotStatus (org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus)12 ShardId (org.opensearch.index.shard.ShardId)10 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)9 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)8 ShardRouting (org.opensearch.cluster.routing.ShardRouting)8 RepositoryShardId (org.opensearch.repositories.RepositoryShardId)8 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)7 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7 Function (java.util.function.Function)7 Collectors (java.util.stream.Collectors)7 LogManager (org.apache.logging.log4j.LogManager)7 Logger (org.apache.logging.log4j.Logger)7 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)7 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)6 ArrayList (java.util.ArrayList)6