Search in sources :

Example 1 with SnapshotInfo

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

the class ClusterShardLimitIT method testRestoreSnapshotOverLimit.

public void testRestoreSnapshotOverLimit() {
    Client client = client();
    logger.info("-->  creating repository");
    Settings.Builder repoSettings = Settings.builder();
    repoSettings.put("location", randomRepoPath());
    repoSettings.put("compress", randomBoolean());
    repoSettings.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES);
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(repoSettings.build()));
    int dataNodes = client().admin().cluster().prepareState().get().getState().getNodes().getDataNodes().size();
    ShardCounts counts = ShardCounts.forDataNodeCount(dataNodes);
    createIndex("snapshot-index", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFailingIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFailingIndexReplicas()).build());
    ensureGreen();
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("snapshot-index").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    SnapshotInfo snapshotInfo = snapshotInfos.get(0);
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
    // Test restore after index deletion
    logger.info("--> delete indices");
    cluster().wipeIndices("snapshot-index");
    // Reduce the shard limit and fill it up
    setShardsPerNode(counts.getShardsPerNode());
    createIndex("test-fill", Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, counts.getFirstIndexShards()).put(SETTING_NUMBER_OF_REPLICAS, counts.getFirstIndexReplicas()).build());
    logger.info("--> restore one index after deletion");
    try {
        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("snapshot-index").execute().actionGet();
        fail("Should not have been able to restore snapshot in full cluster");
    } catch (IllegalArgumentException e) {
        verifyException(dataNodes, counts, e);
    }
    ensureGreen();
    ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
    assertFalse(clusterState.getMetadata().hasIndex("snapshot-index"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 2 with SnapshotInfo

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

the class SnapshotDisruptionIT method assertSnapshotExists.

private void assertSnapshotExists(String repository, String snapshot) {
    GetSnapshotsResponse snapshotsStatusResponse = dataNodeClient().admin().cluster().prepareGetSnapshots(repository).setSnapshots(snapshot).get();
    SnapshotInfo snapshotInfo = snapshotsStatusResponse.getSnapshots().get(0);
    assertEquals(SnapshotState.SUCCESS, snapshotInfo.state());
    assertEquals(snapshotInfo.totalShards(), snapshotInfo.successfulShards());
    assertEquals(0, snapshotInfo.failedShards());
    logger.info("--> done verifying, snapshot exists");
}
Also used : GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo)

Example 3 with SnapshotInfo

use of org.opensearch.snapshots.SnapshotInfo 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 SnapshotInfo

use of org.opensearch.snapshots.SnapshotInfo 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 SnapshotInfo

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

the class TransportGetSnapshotsAction method buildSimpleSnapshotInfos.

private List<SnapshotInfo> buildSimpleSnapshotInfos(final Set<SnapshotId> toResolve, final RepositoryData repositoryData, final List<SnapshotInfo> currentSnapshots) {
    List<SnapshotInfo> snapshotInfos = new ArrayList<>();
    for (SnapshotInfo snapshotInfo : currentSnapshots) {
        if (toResolve.remove(snapshotInfo.snapshotId())) {
            snapshotInfos.add(snapshotInfo.basic());
        }
    }
    Map<SnapshotId, List<String>> snapshotsToIndices = new HashMap<>();
    for (IndexId indexId : repositoryData.getIndices().values()) {
        for (SnapshotId snapshotId : repositoryData.getSnapshots(indexId)) {
            if (toResolve.contains(snapshotId)) {
                snapshotsToIndices.computeIfAbsent(snapshotId, (k) -> new ArrayList<>()).add(indexId.getName());
            }
        }
    }
    for (SnapshotId snapshotId : toResolve) {
        final List<String> indices = snapshotsToIndices.getOrDefault(snapshotId, Collections.emptyList());
        CollectionUtil.timSort(indices);
        snapshotInfos.add(new SnapshotInfo(snapshotId, indices, Collections.emptyList(), repositoryData.getSnapshotState(snapshotId)));
    }
    CollectionUtil.timSort(snapshotInfos);
    return unmodifiableList(snapshotInfos);
}
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) SnapshotId(org.opensearch.snapshots.SnapshotId) IndexId(org.opensearch.repositories.IndexId) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SnapshotInfo (org.opensearch.snapshots.SnapshotInfo)20 SnapshotId (org.opensearch.snapshots.SnapshotId)11 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 ClusterService (org.opensearch.cluster.service.ClusterService)7 RepositoryData (org.opensearch.repositories.RepositoryData)7 Collections (java.util.Collections)6 Map (java.util.Map)6 ClusterState (org.opensearch.cluster.ClusterState)6 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)6 IndexId (org.opensearch.repositories.IndexId)6 Repository (org.opensearch.repositories.Repository)6 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 SnapshotMissingException (org.opensearch.snapshots.SnapshotMissingException)5 ThreadPool (org.opensearch.threadpool.ThreadPool)5 HashSet (java.util.HashSet)4 LogManager (org.apache.logging.log4j.LogManager)4