Search in sources :

Example 26 with Snapshot

use of org.opensearch.snapshots.Snapshot in project OpenSearch by opensearch-project.

the class TransportSnapshotsStatusAction method loadRepositoryData.

private void loadRepositoryData(SnapshotsInProgress snapshotsInProgress, SnapshotsStatusRequest request, List<SnapshotStatus> builder, Set<String> currentSnapshotNames, String repositoryName, ActionListener<SnapshotsStatusResponse> listener) {
    final Set<String> requestedSnapshotNames = Sets.newHashSet(request.snapshots());
    final StepListener<RepositoryData> repositoryDataListener = new StepListener<>();
    repositoriesService.getRepositoryData(repositoryName, repositoryDataListener);
    repositoryDataListener.whenComplete(repositoryData -> {
        final Map<String, SnapshotId> matchedSnapshotIds = repositoryData.getSnapshotIds().stream().filter(s -> requestedSnapshotNames.contains(s.getName())).collect(Collectors.toMap(SnapshotId::getName, Function.identity()));
        for (final String snapshotName : request.snapshots()) {
            if (currentSnapshotNames.contains(snapshotName)) {
                // we've already found this snapshot in the current snapshot entries, so skip over
                continue;
            }
            SnapshotId snapshotId = matchedSnapshotIds.get(snapshotName);
            if (snapshotId == null) {
                // neither in the current snapshot entries nor found in the repository
                if (request.ignoreUnavailable()) {
                    // ignoring unavailable snapshots, so skip over
                    logger.debug("snapshot status request ignoring snapshot [{}], not found in repository [{}]", snapshotName, repositoryName);
                    continue;
                } else {
                    throw new SnapshotMissingException(repositoryName, snapshotName);
                }
            }
            SnapshotInfo snapshotInfo = snapshot(snapshotsInProgress, repositoryName, snapshotId);
            List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
            if (snapshotInfo.state().completed()) {
                Map<ShardId, IndexShardSnapshotStatus> shardStatuses = snapshotShards(repositoryName, repositoryData, snapshotInfo);
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> shardStatus : shardStatuses.entrySet()) {
                    IndexShardSnapshotStatus.Copy lastSnapshotStatus = shardStatus.getValue().asCopy();
                    shardStatusBuilder.add(new SnapshotIndexShardStatus(shardStatus.getKey(), lastSnapshotStatus));
                }
                final SnapshotsInProgress.State state;
                switch(snapshotInfo.state()) {
                    case FAILED:
                        state = SnapshotsInProgress.State.FAILED;
                        break;
                    case SUCCESS:
                    case PARTIAL:
                        // Translating both PARTIAL and SUCCESS to SUCCESS for now
                        // TODO: add the differentiation on the metadata level in the next major release
                        state = SnapshotsInProgress.State.SUCCESS;
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown snapshot state " + snapshotInfo.state());
                }
                final long startTime = snapshotInfo.startTime();
                final long endTime = snapshotInfo.endTime();
                assert endTime >= startTime || (endTime == 0L && snapshotInfo.state().completed() == false) : "Inconsistent timestamps found in SnapshotInfo [" + snapshotInfo + "]";
                builder.add(new SnapshotStatus(new Snapshot(repositoryName, snapshotId), state, Collections.unmodifiableList(shardStatusBuilder), snapshotInfo.includeGlobalState(), startTime, // Use current time to calculate overall runtime for in-progress snapshots that have endTime == 0
                (endTime == 0 ? threadPool.absoluteTimeInMillis() : endTime) - startTime));
            }
        }
        listener.onResponse(new SnapshotsStatusResponse(Collections.unmodifiableList(builder)));
    }, listener::onFailure);
}
Also used : Arrays(java.util.Arrays) SnapshotState(org.opensearch.snapshots.SnapshotState) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Strings(org.opensearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) IndexId(org.opensearch.repositories.IndexId) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) Repository(org.opensearch.repositories.Repository) CollectionUtils(org.opensearch.common.util.CollectionUtils) SnapshotId(org.opensearch.snapshots.SnapshotId) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Set(java.util.Set) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) StepListener(org.opensearch.action.StepListener) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) RepositoriesService(org.opensearch.repositories.RepositoriesService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) TransportMasterNodeAction(org.opensearch.action.support.master.TransportMasterNodeAction) SnapshotShardsService(org.opensearch.snapshots.SnapshotShardsService) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) StreamInput(org.opensearch.common.io.stream.StreamInput) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IOException(java.io.IOException) ShardId(org.opensearch.index.shard.ShardId) Sets(org.opensearch.common.util.set.Sets) Snapshot(org.opensearch.snapshots.Snapshot) ClusterService(org.opensearch.cluster.service.ClusterService) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) ArrayList(java.util.ArrayList) ShardId(org.opensearch.index.shard.ShardId) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotId(org.opensearch.snapshots.SnapshotId) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) StepListener(org.opensearch.action.StepListener) Map(java.util.Map) HashMap(java.util.HashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Aggregations

Snapshot (org.opensearch.snapshots.Snapshot)26 SnapshotId (org.opensearch.snapshots.SnapshotId)23 IndexId (org.opensearch.repositories.IndexId)18 ShardId (org.opensearch.index.shard.ShardId)15 ClusterState (org.opensearch.cluster.ClusterState)13 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)12 ShardRouting (org.opensearch.cluster.routing.ShardRouting)12 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 Metadata (org.opensearch.cluster.metadata.Metadata)7 SnapshotRecoverySource (org.opensearch.cluster.routing.RecoverySource.SnapshotRecoverySource)7 HashSet (java.util.HashSet)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 Collectors (java.util.stream.Collectors)5 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)5