Search in sources :

Example 1 with IndexShardSnapshotStatus

use of org.opensearch.index.snapshots.IndexShardSnapshotStatus in project OpenSearch by opensearch-project.

the class TransportSnapshotsStatusAction method snapshotShards.

/**
 * Returns status of shards currently finished snapshots
 * <p>
 * This method is executed on master node and it's complimentary to the
 * {@link SnapshotShardsService#currentSnapshotShards(Snapshot)} because it
 * returns similar information but for already finished snapshots.
 * </p>
 *
 * @param repositoryName  repository name
 * @param snapshotInfo    snapshot info
 * @return map of shard id to snapshot status
 */
private Map<ShardId, IndexShardSnapshotStatus> snapshotShards(final String repositoryName, final RepositoryData repositoryData, final SnapshotInfo snapshotInfo) throws IOException {
    final Repository repository = repositoriesService.repository(repositoryName);
    final Map<ShardId, IndexShardSnapshotStatus> shardStatus = new HashMap<>();
    for (String index : snapshotInfo.indices()) {
        IndexId indexId = repositoryData.resolveIndexId(index);
        IndexMetadata indexMetadata = repository.getSnapshotIndexMetaData(repositoryData, snapshotInfo.snapshotId(), indexId);
        if (indexMetadata != null) {
            int numberOfShards = indexMetadata.getNumberOfShards();
            for (int i = 0; i < numberOfShards; i++) {
                ShardId shardId = new ShardId(indexMetadata.getIndex(), i);
                SnapshotShardFailure shardFailure = findShardFailure(snapshotInfo.shardFailures(), shardId);
                if (shardFailure != null) {
                    shardStatus.put(shardId, IndexShardSnapshotStatus.newFailed(shardFailure.reason()));
                } else {
                    final IndexShardSnapshotStatus shardSnapshotStatus;
                    if (snapshotInfo.state() == SnapshotState.FAILED) {
                        // If the snapshot failed, but the shard's snapshot does
                        // not have an exception, it means that partial snapshots
                        // were disabled and in this case, the shard snapshot will
                        // *not* have any metadata, so attempting to read the shard
                        // snapshot status will throw an exception. Instead, we create
                        // a status for the shard to indicate that the shard snapshot
                        // could not be taken due to partial being set to false.
                        shardSnapshotStatus = IndexShardSnapshotStatus.newFailed("skipped");
                    } else {
                        shardSnapshotStatus = repository.getShardSnapshotStatus(snapshotInfo.snapshotId(), indexId, shardId);
                    }
                    shardStatus.put(shardId, shardSnapshotStatus);
                }
            }
        }
    }
    return unmodifiableMap(shardStatus);
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) IndexId(org.opensearch.repositories.IndexId) Repository(org.opensearch.repositories.Repository) SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) HashMap(java.util.HashMap) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with IndexShardSnapshotStatus

use of org.opensearch.index.snapshots.IndexShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotShardsService method startNewSnapshots.

private void startNewSnapshots(SnapshotsInProgress snapshotsInProgress) {
    final String localNodeId = clusterService.localNode().getId();
    for (SnapshotsInProgress.Entry entry : snapshotsInProgress.entries()) {
        final State entryState = entry.state();
        if (entry.isClone()) {
            // This is a snapshot clone, it will be executed on the current master
            continue;
        }
        if (entryState == State.STARTED) {
            Map<ShardId, IndexShardSnapshotStatus> startedShards = null;
            final Snapshot snapshot = entry.snapshot();
            Map<ShardId, IndexShardSnapshotStatus> snapshotShards = shardSnapshots.getOrDefault(snapshot, emptyMap());
            for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shard : entry.shards()) {
                // Add all new shards to start processing on
                final ShardId shardId = shard.key;
                final ShardSnapshotStatus shardSnapshotStatus = shard.value;
                if (shardSnapshotStatus.state() == ShardState.INIT && localNodeId.equals(shardSnapshotStatus.nodeId()) && snapshotShards.containsKey(shardId) == false) {
                    logger.trace("[{}] - Adding shard to the queue", shardId);
                    if (startedShards == null) {
                        startedShards = new HashMap<>();
                    }
                    startedShards.put(shardId, IndexShardSnapshotStatus.newInitializing(shardSnapshotStatus.generation()));
                }
            }
            if (startedShards != null && startedShards.isEmpty() == false) {
                shardSnapshots.computeIfAbsent(snapshot, s -> new HashMap<>()).putAll(startedShards);
                startNewShards(entry, startedShards);
            }
        } else if (entryState == State.ABORTED) {
            // Abort all running shards for this snapshot
            final Snapshot snapshot = entry.snapshot();
            Map<ShardId, IndexShardSnapshotStatus> snapshotShards = shardSnapshots.getOrDefault(snapshot, emptyMap());
            for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shard : entry.shards()) {
                final IndexShardSnapshotStatus snapshotStatus = snapshotShards.get(shard.key);
                if (snapshotStatus == null) {
                    // notify master that abort has completed by moving to FAILED
                    if (shard.value.state() == ShardState.ABORTED && localNodeId.equals(shard.value.nodeId())) {
                        notifyFailedSnapshotShard(snapshot, shard.key, shard.value.reason());
                    }
                } else {
                    snapshotStatus.abortIfNotCompleted("snapshot has been aborted");
                }
            }
        }
    }
}
Also used : IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) IndexCommit(org.apache.lucene.index.IndexCommit) RepositoriesService(org.opensearch.repositories.RepositoriesService) ShardGenerations(org.opensearch.repositories.ShardGenerations) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) IndexShardState(org.opensearch.index.shard.IndexShardState) TransportRequestDeduplicator(org.opensearch.transport.TransportRequestDeduplicator) ThreadPool(org.opensearch.threadpool.ThreadPool) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) Version(org.opensearch.Version) HashMap(java.util.HashMap) ClusterStateListener(org.opensearch.cluster.ClusterStateListener) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexShard(org.opensearch.index.shard.IndexShard) IndexId(org.opensearch.repositories.IndexId) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) ShardSnapshotStatus(org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) Repository(org.opensearch.repositories.Repository) StreamInput(org.opensearch.common.io.stream.StreamInput) Collections.emptyMap(java.util.Collections.emptyMap) IndexEventListener(org.opensearch.index.shard.IndexEventListener) Iterator(java.util.Iterator) Stage(org.opensearch.index.snapshots.IndexShardSnapshotStatus.Stage) IndicesService(org.opensearch.indices.IndicesService) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) IndexShardSnapshotFailedException(org.opensearch.index.snapshots.IndexShardSnapshotFailedException) IOUtils(org.opensearch.core.internal.io.IOUtils) Engine(org.opensearch.index.engine.Engine) ShardId(org.opensearch.index.shard.ShardId) AbstractLifecycleComponent(org.opensearch.common.component.AbstractLifecycleComponent) ShardState(org.opensearch.cluster.SnapshotsInProgress.ShardState) Logger(org.apache.logging.log4j.Logger) ClusterService(org.opensearch.cluster.service.ClusterService) State(org.opensearch.cluster.SnapshotsInProgress.State) LogManager(org.apache.logging.log4j.LogManager) TransportException(org.opensearch.transport.TransportException) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) HashMap(java.util.HashMap) ShardId(org.opensearch.index.shard.ShardId) IndexShardState(org.opensearch.index.shard.IndexShardState) ShardState(org.opensearch.cluster.SnapshotsInProgress.ShardState) State(org.opensearch.cluster.SnapshotsInProgress.State) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) ShardSnapshotStatus(org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 3 with IndexShardSnapshotStatus

use of org.opensearch.index.snapshots.IndexShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotShardsService method syncShardStatsOnNewMaster.

/**
 * Checks if any shards were processed that the new master doesn't know about
 */
private void syncShardStatsOnNewMaster(ClusterChangedEvent event) {
    SnapshotsInProgress snapshotsInProgress = event.state().custom(SnapshotsInProgress.TYPE);
    if (snapshotsInProgress == null) {
        return;
    }
    // Clear request deduplicator since we need to send all requests that were potentially not handled by the previous
    // master again
    remoteFailedRequestDeduplicator.clear();
    for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) {
        if (snapshot.state() == State.STARTED || snapshot.state() == State.ABORTED) {
            Map<ShardId, IndexShardSnapshotStatus> localShards = currentSnapshotShards(snapshot.snapshot());
            if (localShards != null) {
                ImmutableOpenMap<ShardId, ShardSnapshotStatus> masterShards = snapshot.shards();
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> localShard : localShards.entrySet()) {
                    ShardId shardId = localShard.getKey();
                    ShardSnapshotStatus masterShard = masterShards.get(shardId);
                    if (masterShard != null && masterShard.state().completed() == false) {
                        final IndexShardSnapshotStatus.Copy indexShardSnapshotStatus = localShard.getValue().asCopy();
                        final Stage stage = indexShardSnapshotStatus.getStage();
                        // Master knows about the shard and thinks it has not completed
                        if (stage == Stage.DONE) {
                            // but we think the shard is done - we need to make new master know that the shard is done
                            logger.debug("[{}] new master thinks the shard [{}] is not completed but the shard is done locally, " + "updating status on the master", snapshot.snapshot(), shardId);
                            notifySuccessfulSnapshotShard(snapshot.snapshot(), shardId, localShard.getValue().generation());
                        } else if (stage == Stage.FAILURE) {
                            // but we think the shard failed - we need to make new master know that the shard failed
                            logger.debug("[{}] new master thinks the shard [{}] is not completed but the shard failed locally, " + "updating status on master", snapshot.snapshot(), shardId);
                            notifyFailedSnapshotShard(snapshot.snapshot(), shardId, indexShardSnapshotStatus.getFailure());
                        }
                    }
                }
            }
        }
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) Stage(org.opensearch.index.snapshots.IndexShardSnapshotStatus.Stage) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) ShardSnapshotStatus(org.opensearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 4 with IndexShardSnapshotStatus

use of org.opensearch.index.snapshots.IndexShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotShardsService method startNewShards.

private void startNewShards(SnapshotsInProgress.Entry entry, Map<ShardId, IndexShardSnapshotStatus> startedShards) {
    threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(() -> {
        final Snapshot snapshot = entry.snapshot();
        final Map<String, IndexId> indicesMap = entry.indices().stream().collect(Collectors.toMap(IndexId::getName, Function.identity()));
        for (final Map.Entry<ShardId, IndexShardSnapshotStatus> shardEntry : startedShards.entrySet()) {
            final ShardId shardId = shardEntry.getKey();
            final IndexShardSnapshotStatus snapshotStatus = shardEntry.getValue();
            final IndexId indexId = indicesMap.get(shardId.getIndexName());
            assert indexId != null;
            assert SnapshotsService.useShardGenerations(entry.version()) || ShardGenerations.fixShardGeneration(snapshotStatus.generation()) == null : "Found non-null, non-numeric shard generation [" + snapshotStatus.generation() + "] for snapshot with old-format compatibility";
            snapshot(shardId, snapshot, indexId, entry.userMetadata(), snapshotStatus, entry.version(), new ActionListener<String>() {

                @Override
                public void onResponse(String newGeneration) {
                    assert newGeneration != null;
                    assert newGeneration.equals(snapshotStatus.generation());
                    if (logger.isDebugEnabled()) {
                        final IndexShardSnapshotStatus.Copy lastSnapshotStatus = snapshotStatus.asCopy();
                        logger.debug("snapshot [{}] completed to [{}] with [{}] at generation [{}]", snapshot, snapshot.getRepository(), lastSnapshotStatus, snapshotStatus.generation());
                    }
                    notifySuccessfulSnapshotShard(snapshot, shardId, newGeneration);
                }

                @Override
                public void onFailure(Exception e) {
                    final String failure;
                    if (e instanceof AbortedSnapshotException) {
                        failure = "aborted";
                        logger.debug(() -> new ParameterizedMessage("[{}][{}] aborted shard snapshot", shardId, snapshot), e);
                    } else {
                        failure = summarizeFailure(e);
                        logger.warn(() -> new ParameterizedMessage("[{}][{}] failed to snapshot shard", shardId, snapshot), e);
                    }
                    snapshotStatus.moveToFailed(threadPool.absoluteTimeInMillis(), failure);
                    notifyFailedSnapshotShard(snapshot, shardId, failure);
                }
            });
        }
    });
}
Also used : IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) IndexId(org.opensearch.repositories.IndexId) IOException(java.io.IOException) IndexShardSnapshotFailedException(org.opensearch.index.snapshots.IndexShardSnapshotFailedException) TransportException(org.opensearch.transport.TransportException) ShardId(org.opensearch.index.shard.ShardId) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Example 5 with IndexShardSnapshotStatus

use of org.opensearch.index.snapshots.IndexShardSnapshotStatus in project OpenSearch by opensearch-project.

the class SnapshotShardsService method cancelRemoved.

private void cancelRemoved(SnapshotsInProgress snapshotsInProgress) {
    // First, remove snapshots that are no longer there
    Iterator<Map.Entry<Snapshot, Map<ShardId, IndexShardSnapshotStatus>>> it = shardSnapshots.entrySet().iterator();
    while (it.hasNext()) {
        final Map.Entry<Snapshot, Map<ShardId, IndexShardSnapshotStatus>> entry = it.next();
        final Snapshot snapshot = entry.getKey();
        if (snapshotsInProgress.snapshot(snapshot) == null) {
            // abort any running snapshots of shards for the removed entry;
            // this could happen if for some reason the cluster state update for aborting
            // running shards is missed, then the snapshot is removed is a subsequent cluster
            // state update, which is being processed here
            it.remove();
            for (IndexShardSnapshotStatus snapshotStatus : entry.getValue().values()) {
                snapshotStatus.abortIfNotCompleted("snapshot has been removed in cluster state, aborting");
            }
        }
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap)

Aggregations

IndexShardSnapshotStatus (org.opensearch.index.snapshots.IndexShardSnapshotStatus)13 ShardId (org.opensearch.index.shard.ShardId)12 Map (java.util.Map)10 IndexId (org.opensearch.repositories.IndexId)10 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)7 Repository (org.opensearch.repositories.Repository)7 ThreadPool (org.opensearch.threadpool.ThreadPool)7 Collections (java.util.Collections)6 Function (java.util.function.Function)6 Version (org.opensearch.Version)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 ClusterService (org.opensearch.cluster.service.ClusterService)6 Settings (org.opensearch.common.settings.Settings)6 Collectors (java.util.stream.Collectors)5 ClusterState (org.opensearch.cluster.ClusterState)5 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)5 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)5 Collections.emptyMap (java.util.Collections.emptyMap)4