Search in sources :

Example 6 with SnapshotState

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

the class RepositoryDataTests method testIndexThatReferencesAnUnknownSnapshot.

public void testIndexThatReferencesAnUnknownSnapshot() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    final RepositoryData repositoryData = generateRandomRepoData();
    XContentBuilder builder = XContentBuilder.builder(xContent);
    repositoryData.snapshotsToXContent(builder, Version.CURRENT);
    RepositoryData parsedRepositoryData;
    try (XContentParser xParser = createParser(builder)) {
        parsedRepositoryData = RepositoryData.snapshotsFromXContent(xParser, repositoryData.getGenId(), randomBoolean());
    }
    assertEquals(repositoryData, parsedRepositoryData);
    Map<String, SnapshotId> snapshotIds = new HashMap<>();
    Map<String, SnapshotState> snapshotStates = new HashMap<>();
    Map<String, Version> snapshotVersions = new HashMap<>();
    for (SnapshotId snapshotId : parsedRepositoryData.getSnapshotIds()) {
        snapshotIds.put(snapshotId.getUUID(), snapshotId);
        snapshotStates.put(snapshotId.getUUID(), parsedRepositoryData.getSnapshotState(snapshotId));
        snapshotVersions.put(snapshotId.getUUID(), parsedRepositoryData.getVersion(snapshotId));
    }
    final IndexId corruptedIndexId = randomFrom(parsedRepositoryData.getIndices().values());
    Map<IndexId, List<SnapshotId>> indexSnapshots = new HashMap<>();
    final ShardGenerations.Builder shardGenBuilder = ShardGenerations.builder();
    for (Map.Entry<String, IndexId> snapshottedIndex : parsedRepositoryData.getIndices().entrySet()) {
        IndexId indexId = snapshottedIndex.getValue();
        List<SnapshotId> snapshotsIds = new ArrayList<>(parsedRepositoryData.getSnapshots(indexId));
        if (corruptedIndexId.equals(indexId)) {
            snapshotsIds.add(new SnapshotId("_uuid", "_does_not_exist"));
        }
        indexSnapshots.put(indexId, snapshotsIds);
        final int shardCount = randomIntBetween(1, 10);
        for (int i = 0; i < shardCount; ++i) {
            shardGenBuilder.put(indexId, i, UUIDs.randomBase64UUID(random()));
        }
    }
    assertNotNull(corruptedIndexId);
    RepositoryData corruptedRepositoryData = new RepositoryData(parsedRepositoryData.getGenId(), snapshotIds, snapshotStates, snapshotVersions, indexSnapshots, shardGenBuilder.build(), IndexMetaDataGenerations.EMPTY);
    final XContentBuilder corruptedBuilder = XContentBuilder.builder(xContent);
    corruptedRepositoryData.snapshotsToXContent(corruptedBuilder, Version.CURRENT);
    try (XContentParser xParser = createParser(corruptedBuilder)) {
        OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> RepositoryData.snapshotsFromXContent(xParser, corruptedRepositoryData.getGenId(), randomBoolean()));
        assertThat(e.getMessage(), equalTo("Detected a corrupted repository, index " + corruptedIndexId + " references an unknown " + "snapshot uuid [_does_not_exist]"));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotState(org.opensearch.snapshots.SnapshotState) OpenSearchParseException(org.opensearch.OpenSearchParseException) Version(org.opensearch.Version) XContent(org.opensearch.common.xcontent.XContent) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 7 with SnapshotState

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

the class RepositoryDataTests method testGetSnapshotState.

public void testGetSnapshotState() {
    final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID());
    final SnapshotState state = randomFrom(SnapshotState.values());
    final RepositoryData repositoryData = RepositoryData.EMPTY.addSnapshot(snapshotId, state, randomFrom(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion()), ShardGenerations.EMPTY, Collections.emptyMap(), Collections.emptyMap());
    assertEquals(state, repositoryData.getSnapshotState(snapshotId));
    assertNull(repositoryData.getSnapshotState(new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID())));
}
Also used : SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotState(org.opensearch.snapshots.SnapshotState)

Example 8 with SnapshotState

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

the class RepositoryData method removeSnapshots.

/**
 * Remove snapshots and remove any indices that no longer exist in the repository due to the deletion of the snapshots.
 *
 * @param snapshots               Snapshot ids to remove
 * @param updatedShardGenerations Shard generations that changed as a result of removing the snapshot.
 *                                The {@code String[]} passed for each {@link IndexId} contains the new shard generation id for each
 *                                changed shard indexed by its shardId
 */
public RepositoryData removeSnapshots(final Collection<SnapshotId> snapshots, final ShardGenerations updatedShardGenerations) {
    Map<String, SnapshotId> newSnapshotIds = snapshotIds.values().stream().filter(sn -> snapshots.contains(sn) == false).collect(Collectors.toMap(SnapshotId::getUUID, Function.identity()));
    if (newSnapshotIds.size() != snapshotIds.size() - snapshots.size()) {
        final Collection<SnapshotId> notFound = new HashSet<>(snapshots);
        notFound.removeAll(snapshotIds.values());
        throw new ResourceNotFoundException("Attempting to remove non-existent snapshots {} from repository data", notFound);
    }
    Map<String, SnapshotState> newSnapshotStates = new HashMap<>(snapshotStates);
    final Map<String, Version> newSnapshotVersions = new HashMap<>(snapshotVersions);
    for (SnapshotId snapshotId : snapshots) {
        newSnapshotStates.remove(snapshotId.getUUID());
        newSnapshotVersions.remove(snapshotId.getUUID());
    }
    Map<IndexId, List<SnapshotId>> indexSnapshots = new HashMap<>();
    for (final IndexId indexId : indices.values()) {
        List<SnapshotId> snapshotIds = this.indexSnapshots.get(indexId);
        assert snapshotIds != null;
        List<SnapshotId> remaining = new ArrayList<>(snapshotIds);
        if (remaining.removeAll(snapshots)) {
            remaining = Collections.unmodifiableList(remaining);
        } else {
            remaining = snapshotIds;
        }
        if (remaining.isEmpty() == false) {
            indexSnapshots.put(indexId, remaining);
        }
    }
    return new RepositoryData(genId, newSnapshotIds, newSnapshotStates, newSnapshotVersions, indexSnapshots, ShardGenerations.builder().putAll(shardGenerations).putAll(updatedShardGenerations).retainIndicesAndPruneDeletes(indexSnapshots.keySet()).build(), indexMetaDataGenerations.withRemovedSnapshots(snapshots));
}
Also used : SnapshotId(org.opensearch.snapshots.SnapshotId) Collection(java.util.Collection) OpenSearchParseException(org.opensearch.OpenSearchParseException) SnapshotState(org.opensearch.snapshots.SnapshotState) Set(java.util.Set) Version(org.opensearch.Version) IOException(java.io.IOException) HashMap(java.util.HashMap) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) ArrayList(java.util.ArrayList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) Map(java.util.Map) XContentParserUtils(org.opensearch.common.xcontent.XContentParserUtils) UUIDs(org.opensearch.common.UUIDs) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SnapshotId(org.opensearch.snapshots.SnapshotId) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) ArrayList(java.util.ArrayList) List(java.util.List) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) HashSet(java.util.HashSet)

Example 9 with SnapshotState

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

the class URLSnapshotRestoreIT method testUrlRepository.

public void testUrlRepository() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path repositoryLocation = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType(FsRepository.TYPE).setSettings(Settings.builder().put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation).put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()).put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits().value, equalTo(100L));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards));
    SnapshotState state = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state();
    assertThat(state, equalTo(SnapshotState.SUCCESS));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> create read-only URL repository");
    assertAcked(client.admin().cluster().preparePutRepository("url-repo").setType(URLRepository.TYPE).setSettings(Settings.builder().put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL().toString()).put("list_directories", randomBoolean())));
    logger.info("--> restore index after deletion");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("url-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits().value, equalTo(100L));
    logger.info("--> list available shapshots");
    GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
    logger.info("--> delete snapshot");
    AcknowledgedResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
    assertAcked(deleteSnapshotResponse);
    logger.info("--> list available shapshot again, no snapshots should be returned");
    getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
}
Also used : Path(java.nio.file.Path) SnapshotState(org.opensearch.snapshots.SnapshotState) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 10 with SnapshotState

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

the class SnapshotClientDocumentationIT method testSnapshotGetSnapshots.

@SuppressWarnings("unused")
public void testSnapshotGetSnapshots() throws IOException {
    RestHighLevelClient client = highLevelClient();
    createTestRepositories();
    createTestIndex();
    createTestSnapshots();
    // tag::get-snapshots-request
    GetSnapshotsRequest request = new GetSnapshotsRequest();
    // end::get-snapshots-request
    // tag::get-snapshots-request-repositoryName
    // <1>
    request.repository(repositoryName);
    // end::get-snapshots-request-repositoryName
    // tag::get-snapshots-request-snapshots
    String[] snapshots = { snapshotName };
    // <1>
    request.snapshots(snapshots);
    // end::get-snapshots-request-snapshots
    // tag::get-snapshots-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::get-snapshots-request-masterTimeout
    // tag::get-snapshots-request-verbose
    // <1>
    request.verbose(true);
    // end::get-snapshots-request-verbose
    // tag::get-snapshots-request-ignore-unavailable
    // <1>
    request.ignoreUnavailable(false);
    // end::get-snapshots-request-ignore-unavailable
    // tag::get-snapshots-execute
    GetSnapshotsResponse response = client.snapshot().get(request, RequestOptions.DEFAULT);
    // end::get-snapshots-execute
    // tag::get-snapshots-response
    List<SnapshotInfo> snapshotsInfos = response.getSnapshots();
    SnapshotInfo snapshotInfo = snapshotsInfos.get(0);
    // <1>
    RestStatus restStatus = snapshotInfo.status();
    // <2>
    SnapshotId snapshotId = snapshotInfo.snapshotId();
    // <3>
    SnapshotState snapshotState = snapshotInfo.state();
    // <4>
    List<SnapshotShardFailure> snapshotShardFailures = snapshotInfo.shardFailures();
    // <5>
    long startTime = snapshotInfo.startTime();
    // <6>
    long endTime = snapshotInfo.endTime();
    // end::get-snapshots-response
    assertEquals(1, snapshotsInfos.size());
}
Also used : SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) GetSnapshotsRequest(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) SnapshotId(org.opensearch.snapshots.SnapshotId) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) SnapshotState(org.opensearch.snapshots.SnapshotState) RestStatus(org.opensearch.rest.RestStatus)

Aggregations

SnapshotState (org.opensearch.snapshots.SnapshotState)10 SnapshotId (org.opensearch.snapshots.SnapshotId)8 HashMap (java.util.HashMap)6 Version (org.opensearch.Version)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 OpenSearchParseException (org.opensearch.OpenSearchParseException)3 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)2