Search in sources :

Example 1 with SnapshotsInProgress

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

the class ConcurrentSnapshotsIT method testAbortOneOfMultipleSnapshots.

public void testAbortOneOfMultipleSnapshots() throws Exception {
    internalCluster().startMasterOnlyNode();
    final String dataNode = internalCluster().startDataOnlyNode();
    final String repoName = "test-repo";
    createRepository(repoName, "mock");
    final String firstIndex = "index-one";
    createIndexWithContent(firstIndex);
    final String firstSnapshot = "snapshot-one";
    final ActionFuture<CreateSnapshotResponse> firstSnapshotResponse = startFullSnapshotBlockedOnDataNode(firstSnapshot, repoName, dataNode);
    final String dataNode2 = internalCluster().startDataOnlyNode();
    ensureStableCluster(3);
    final String secondIndex = "index-two";
    createIndexWithContent(secondIndex, dataNode2, dataNode);
    final String secondSnapshot = "snapshot-two";
    final ActionFuture<CreateSnapshotResponse> secondSnapshotResponse = startFullSnapshot(repoName, secondSnapshot);
    logger.info("--> wait for snapshot on second data node to finish");
    awaitClusterState(state -> {
        final SnapshotsInProgress snapshotsInProgress = state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY);
        return snapshotsInProgress.entries().size() == 2 && snapshotHasCompletedShard(secondSnapshot, snapshotsInProgress);
    });
    final ActionFuture<AcknowledgedResponse> deleteSnapshotsResponse = startDeleteSnapshot(repoName, firstSnapshot);
    awaitNDeletionsInProgress(1);
    logger.info("--> start third snapshot");
    final ActionFuture<CreateSnapshotResponse> thirdSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(repoName, "snapshot-three").setIndices(secondIndex).setWaitForCompletion(true).execute();
    assertThat(firstSnapshotResponse.isDone(), is(false));
    assertThat(secondSnapshotResponse.isDone(), is(false));
    unblockNode(repoName, dataNode);
    final SnapshotInfo firstSnapshotInfo = firstSnapshotResponse.get().getSnapshotInfo();
    assertThat(firstSnapshotInfo.state(), is(SnapshotState.FAILED));
    assertThat(firstSnapshotInfo.reason(), is("Snapshot was aborted by deletion"));
    final SnapshotInfo secondSnapshotInfo = assertSuccessful(secondSnapshotResponse);
    final SnapshotInfo thirdSnapshotInfo = assertSuccessful(thirdSnapshotResponse);
    assertThat(deleteSnapshotsResponse.get().isAcknowledged(), is(true));
    logger.info("--> verify that the first snapshot is gone");
    assertThat(client().admin().cluster().prepareGetSnapshots(repoName).get().getSnapshots(), containsInAnyOrder(secondSnapshotInfo, thirdSnapshotInfo));
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with SnapshotsInProgress

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

the class ConcurrentSnapshotsIT method testCascadedAborts.

public void testCascadedAborts() throws Exception {
    internalCluster().startMasterOnlyNode();
    final String dataNode = internalCluster().startDataOnlyNode();
    final String repoName = "test-repo";
    createRepository(repoName, "mock");
    createIndexWithContent("index-one");
    final String firstSnapshot = "snapshot-one";
    final ActionFuture<CreateSnapshotResponse> firstSnapshotResponse = startFullSnapshotBlockedOnDataNode(firstSnapshot, repoName, dataNode);
    final String dataNode2 = internalCluster().startDataOnlyNode();
    ensureStableCluster(3);
    createIndexWithContent("index-two", dataNode2, dataNode);
    final String secondSnapshot = "snapshot-two";
    final ActionFuture<CreateSnapshotResponse> secondSnapshotResponse = startFullSnapshot(repoName, secondSnapshot);
    logger.info("--> wait for snapshot on second data node to finish");
    awaitClusterState(state -> {
        final SnapshotsInProgress snapshotsInProgress = state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY);
        return snapshotsInProgress.entries().size() == 2 && snapshotHasCompletedShard(secondSnapshot, snapshotsInProgress);
    });
    final ActionFuture<AcknowledgedResponse> deleteSnapshotsResponse = startDeleteSnapshot(repoName, firstSnapshot);
    awaitNDeletionsInProgress(1);
    final ActionFuture<CreateSnapshotResponse> thirdSnapshotResponse = startFullSnapshot(repoName, "snapshot-three");
    assertThat(firstSnapshotResponse.isDone(), is(false));
    assertThat(secondSnapshotResponse.isDone(), is(false));
    logger.info("--> waiting for all three snapshots to show up as in-progress");
    assertBusy(() -> assertThat(currentSnapshots(repoName), hasSize(3)), 30L, TimeUnit.SECONDS);
    final ActionFuture<AcknowledgedResponse> allDeletedResponse = startDeleteSnapshot(repoName, "*");
    logger.info("--> waiting for second and third snapshot to finish");
    assertBusy(() -> {
        assertThat(currentSnapshots(repoName), hasSize(1));
        final SnapshotsInProgress snapshotsInProgress = clusterService().state().custom(SnapshotsInProgress.TYPE);
        assertThat(snapshotsInProgress.entries().get(0).state(), is(SnapshotsInProgress.State.ABORTED));
    }, 30L, TimeUnit.SECONDS);
    unblockNode(repoName, dataNode);
    logger.info("--> verify all snapshots were aborted");
    assertThat(firstSnapshotResponse.get().getSnapshotInfo().state(), is(SnapshotState.FAILED));
    assertThat(secondSnapshotResponse.get().getSnapshotInfo().state(), is(SnapshotState.FAILED));
    assertThat(thirdSnapshotResponse.get().getSnapshotInfo().state(), is(SnapshotState.FAILED));
    logger.info("--> verify both deletes have completed");
    assertAcked(deleteSnapshotsResponse.get());
    assertAcked(allDeletedResponse.get());
    logger.info("--> verify that all snapshots are gone");
    assertThat(client().admin().cluster().prepareGetSnapshots(repoName).get().getSnapshots(), empty());
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with SnapshotsInProgress

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

the class TransportGetSnapshotsAction method sortedCurrentSnapshots.

/**
 * Returns a list of currently running snapshots from repository sorted by snapshot creation date
 *
 * @param snapshotsInProgress snapshots in progress in the cluster state
 * @param repositoryName repository name
 * @return list of snapshots
 */
private static List<SnapshotInfo> sortedCurrentSnapshots(@Nullable SnapshotsInProgress snapshotsInProgress, String repositoryName) {
    List<SnapshotInfo> snapshotList = new ArrayList<>();
    List<SnapshotsInProgress.Entry> entries = SnapshotsService.currentSnapshots(snapshotsInProgress, repositoryName, Collections.emptyList());
    for (SnapshotsInProgress.Entry entry : entries) {
        snapshotList.add(new SnapshotInfo(entry));
    }
    CollectionUtil.timSort(snapshotList);
    return unmodifiableList(snapshotList);
}
Also used : SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) ArrayList(java.util.ArrayList) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress)

Example 4 with SnapshotsInProgress

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

the class TransportGetSnapshotsAction method snapshots.

/**
 * Returns a list of snapshots from repository sorted by snapshot creation date
 *
 * @param snapshotsInProgress snapshots in progress in the cluster state
 * @param repositoryName      repository name
 * @param snapshotIds         snapshots for which to fetch snapshot information
 * @param ignoreUnavailable   if true, snapshots that could not be read will only be logged with a warning,
 *                            if false, they will throw an error
 * @return list of snapshots
 */
private List<SnapshotInfo> snapshots(@Nullable SnapshotsInProgress snapshotsInProgress, String repositoryName, List<SnapshotId> snapshotIds, boolean ignoreUnavailable) {
    final Set<SnapshotInfo> snapshotSet = new HashSet<>();
    final Set<SnapshotId> snapshotIdsToIterate = new HashSet<>(snapshotIds);
    // first, look at the snapshots in progress
    final List<SnapshotsInProgress.Entry> entries = SnapshotsService.currentSnapshots(snapshotsInProgress, repositoryName, snapshotIdsToIterate.stream().map(SnapshotId::getName).collect(Collectors.toList()));
    for (SnapshotsInProgress.Entry entry : entries) {
        snapshotSet.add(new SnapshotInfo(entry));
        snapshotIdsToIterate.remove(entry.snapshot().getSnapshotId());
    }
    // then, look in the repository
    final Repository repository = repositoriesService.repository(repositoryName);
    for (SnapshotId snapshotId : snapshotIdsToIterate) {
        try {
            snapshotSet.add(repository.getSnapshotInfo(snapshotId));
        } catch (Exception ex) {
            if (ignoreUnavailable) {
                logger.warn(() -> new ParameterizedMessage("failed to get snapshot [{}]", snapshotId), ex);
            } else {
                if (ex instanceof SnapshotException) {
                    throw ex;
                }
                throw new SnapshotException(repositoryName, snapshotId, "Snapshot could not be read", ex);
            }
        }
    }
    final ArrayList<SnapshotInfo> snapshotList = new ArrayList<>(snapshotSet);
    CollectionUtil.timSort(snapshotList);
    return unmodifiableList(snapshotList);
}
Also used : ArrayList(java.util.ArrayList) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) SnapshotException(org.opensearch.snapshots.SnapshotException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) SnapshotException(org.opensearch.snapshots.SnapshotException) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) Repository(org.opensearch.repositories.Repository) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) HashSet(java.util.HashSet)

Example 5 with SnapshotsInProgress

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

the class TransportGetSnapshotsAction method masterOperation.

@Override
protected void masterOperation(final GetSnapshotsRequest request, final ClusterState state, final ActionListener<GetSnapshotsResponse> listener) {
    try {
        final String repository = request.repository();
        final SnapshotsInProgress snapshotsInProgress = state.custom(SnapshotsInProgress.TYPE);
        final Map<String, SnapshotId> allSnapshotIds = new HashMap<>();
        final List<SnapshotInfo> currentSnapshots = new ArrayList<>();
        for (SnapshotInfo snapshotInfo : sortedCurrentSnapshots(snapshotsInProgress, repository)) {
            SnapshotId snapshotId = snapshotInfo.snapshotId();
            allSnapshotIds.put(snapshotId.getName(), snapshotId);
            currentSnapshots.add(snapshotInfo);
        }
        final RepositoryData repositoryData;
        if (isCurrentSnapshotsOnly(request.snapshots()) == false) {
            repositoryData = PlainActionFuture.get(fut -> repositoriesService.getRepositoryData(repository, fut));
            for (SnapshotId snapshotId : repositoryData.getSnapshotIds()) {
                allSnapshotIds.put(snapshotId.getName(), snapshotId);
            }
        } else {
            repositoryData = null;
        }
        final Set<SnapshotId> toResolve = new HashSet<>();
        if (isAllSnapshots(request.snapshots())) {
            toResolve.addAll(allSnapshotIds.values());
        } else {
            for (String snapshotOrPattern : request.snapshots()) {
                if (GetSnapshotsRequest.CURRENT_SNAPSHOT.equalsIgnoreCase(snapshotOrPattern)) {
                    toResolve.addAll(currentSnapshots.stream().map(SnapshotInfo::snapshotId).collect(Collectors.toList()));
                } else if (Regex.isSimpleMatchPattern(snapshotOrPattern) == false) {
                    if (allSnapshotIds.containsKey(snapshotOrPattern)) {
                        toResolve.add(allSnapshotIds.get(snapshotOrPattern));
                    } else if (request.ignoreUnavailable() == false) {
                        throw new SnapshotMissingException(repository, snapshotOrPattern);
                    }
                } else {
                    for (Map.Entry<String, SnapshotId> entry : allSnapshotIds.entrySet()) {
                        if (Regex.simpleMatch(snapshotOrPattern, entry.getKey())) {
                            toResolve.add(entry.getValue());
                        }
                    }
                }
            }
            if (toResolve.isEmpty() && request.ignoreUnavailable() == false && isCurrentSnapshotsOnly(request.snapshots()) == false) {
                throw new SnapshotMissingException(repository, request.snapshots()[0]);
            }
        }
        final List<SnapshotInfo> snapshotInfos;
        if (request.verbose()) {
            snapshotInfos = snapshots(snapshotsInProgress, repository, new ArrayList<>(toResolve), request.ignoreUnavailable());
        } else {
            if (repositoryData != null) {
                // want non-current snapshots as well, which are found in the repository data
                snapshotInfos = buildSimpleSnapshotInfos(toResolve, repositoryData, currentSnapshots);
            } else {
                // only want current snapshots
                snapshotInfos = currentSnapshots.stream().map(SnapshotInfo::basic).collect(Collectors.toList());
                CollectionUtil.timSort(snapshotInfos);
            }
        }
        listener.onResponse(new GetSnapshotsResponse(snapshotInfos));
    } catch (Exception e) {
        listener.onFailure(e);
    }
}
Also used : RepositoriesService(org.opensearch.repositories.RepositoriesService) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ThreadPool(org.opensearch.threadpool.ThreadPool) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) TransportMasterNodeAction(org.opensearch.action.support.master.TransportMasterNodeAction) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Regex(org.opensearch.common.regex.Regex) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) IndexId(org.opensearch.repositories.IndexId) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) SnapshotException(org.opensearch.snapshots.SnapshotException) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) Repository(org.opensearch.repositories.Repository) StreamInput(org.opensearch.common.io.stream.StreamInput) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Set(java.util.Set) IOException(java.io.IOException) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) CollectionUtil(org.apache.lucene.util.CollectionUtil) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterService(org.opensearch.cluster.service.ClusterService) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) SnapshotException(org.opensearch.snapshots.SnapshotException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashSet(java.util.HashSet)

Aggregations

SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)55 ClusterState (org.opensearch.cluster.ClusterState)35 IOException (java.io.IOException)27 Repository (org.opensearch.repositories.Repository)26 HashSet (java.util.HashSet)25 SnapshotDeletionsInProgress (org.opensearch.cluster.SnapshotDeletionsInProgress)24 IndexId (org.opensearch.repositories.IndexId)24 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 List (java.util.List)21 Map (java.util.Map)21 ActionListener (org.opensearch.action.ActionListener)21 StepListener (org.opensearch.action.StepListener)21 ClusterService (org.opensearch.cluster.service.ClusterService)21 Set (java.util.Set)20 Collectors (java.util.stream.Collectors)20 LogManager (org.apache.logging.log4j.LogManager)20 Logger (org.apache.logging.log4j.Logger)20 ShardId (org.opensearch.index.shard.ShardId)20 RepositoriesService (org.opensearch.repositories.RepositoriesService)20