Search in sources :

Example 31 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testRestoreSnapshotWithCorruptedGlobalState.

/**
 * Tests that a snapshot with a corrupted global state file can still be restored
 */
public void testRestoreSnapshotWithCorruptedGlobalState() throws Exception {
    final Path repo = randomRepoPath();
    final String repoName = "test-repo";
    createRepository(repoName, "fs", repo);
    createIndex("test-idx-1", "test-idx-2");
    indexRandom(true, client().prepareIndex("test-idx-1").setSource("foo", "bar"), client().prepareIndex("test-idx-2").setSource("foo", "bar"), client().prepareIndex("test-idx-2").setSource("foo", "bar"));
    flushAndRefresh("test-idx-1", "test-idx-2");
    final String snapshotName = "test-snap";
    final SnapshotInfo snapshotInfo = createFullSnapshot(repoName, snapshotName);
    final Path globalStatePath = repo.resolve("meta-" + snapshotInfo.snapshotId().getUUID() + ".dat");
    try (SeekableByteChannel outChan = Files.newByteChannel(globalStatePath, StandardOpenOption.WRITE)) {
        outChan.truncate(randomInt(10));
    }
    List<SnapshotInfo> snapshotInfos = clusterAdmin().prepareGetSnapshots(repoName).get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    assertThat(snapshotInfos.get(0).state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfos.get(0).snapshotId().getName(), equalTo(snapshotName));
    SnapshotsStatusResponse snapshotStatusResponse = clusterAdmin().prepareSnapshotStatus(repoName).setSnapshots(snapshotName).get();
    assertThat(snapshotStatusResponse.getSnapshots(), hasSize(1));
    assertThat(snapshotStatusResponse.getSnapshots().get(0).getSnapshot().getSnapshotId().getName(), equalTo(snapshotName));
    assertAcked(client().admin().indices().prepareDelete("test-idx-1", "test-idx-2"));
    SnapshotException ex = expectThrows(SnapshotException.class, () -> clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName).setRestoreGlobalState(true).setWaitForCompletion(true).get());
    assertThat(ex.getRepositoryName(), equalTo(repoName));
    assertThat(ex.getSnapshotName(), equalTo(snapshotName));
    assertThat(ex.getMessage(), containsString("failed to read global metadata"));
    RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true).get();
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(snapshotInfo.successfulShards()));
    ensureGreen("test-idx-1", "test-idx-2");
    assertHitCount(client().prepareSearch("test-idx-*").setSize(0).get(), 3);
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 32 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testThrottling.

public void testThrottling() throws Exception {
    Client client = client();
    boolean throttleSnapshot = randomBoolean();
    boolean throttleRestore = randomBoolean();
    boolean throttleRestoreViaRecoverySettings = throttleRestore && randomBoolean();
    createRepository("test-repo", "fs", Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(1000, 10000), ByteSizeUnit.BYTES).put("max_restore_bytes_per_sec", throttleRestore && (throttleRestoreViaRecoverySettings == false) ? "10k" : "0").put("max_snapshot_bytes_per_sec", throttleSnapshot ? "10k" : "0"));
    createIndexWithRandomDocs("test-idx", 100);
    createSnapshot("test-repo", "test-snap", Collections.singletonList("test-idx"));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index");
    client.admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), throttleRestoreViaRecoverySettings ? "10k" : "0").build()).get();
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx", 100L);
    long snapshotPause = 0L;
    long restorePause = 0L;
    for (RepositoriesService repositoriesService : internalCluster().getDataNodeInstances(RepositoriesService.class)) {
        snapshotPause += repositoriesService.repository("test-repo").getSnapshotThrottleTimeInNanos();
        restorePause += repositoriesService.repository("test-repo").getRestoreThrottleTimeInNanos();
    }
    if (throttleSnapshot) {
        assertThat(snapshotPause, greaterThan(0L));
    } else {
        assertThat(snapshotPause, equalTo(0L));
    }
    if (throttleRestore) {
        assertThat(restorePause, greaterThan(0L));
    } else {
        assertThat(restorePause, equalTo(0L));
    }
    client.admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().putNull(INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey()).build()).get();
}
Also used : RepositoriesService(org.opensearch.repositories.RepositoriesService) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 33 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testBasicWorkFlow.

public void testBasicWorkFlow() throws Exception {
    createRepository("test-repo", "fs");
    createIndexWithRandomDocs("test-idx-1", 100);
    createIndexWithRandomDocs("test-idx-2", 100);
    createIndexWithRandomDocs("test-idx-3", 100);
    ActionFuture<FlushResponse> flushResponseFuture = null;
    if (randomBoolean()) {
        ArrayList<String> indicesToFlush = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            if (randomBoolean()) {
                indicesToFlush.add("test-idx-" + i);
            }
        }
        if (!indicesToFlush.isEmpty()) {
            String[] indices = indicesToFlush.toArray(new String[indicesToFlush.size()]);
            logger.info("--> starting asynchronous flush for indices {}", Arrays.toString(indices));
            flushResponseFuture = client().admin().indices().prepareFlush(indices).execute();
        }
    }
    final String[] indicesToSnapshot = { "test-idx-*", "-test-idx-3" };
    logger.info("--> capturing history UUIDs");
    final Map<ShardId, String> historyUUIDs = new HashMap<>();
    for (ShardStats shardStats : client().admin().indices().prepareStats(indicesToSnapshot).clear().get().getShards()) {
        String historyUUID = shardStats.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY);
        ShardId shardId = shardStats.getShardRouting().shardId();
        if (historyUUIDs.containsKey(shardId)) {
            assertThat(shardStats.getShardRouting() + " has a different history uuid", historyUUID, equalTo(historyUUIDs.get(shardId)));
        } else {
            historyUUIDs.put(shardId, historyUUID);
        }
    }
    final boolean snapshotClosed = randomBoolean();
    if (snapshotClosed) {
        assertAcked(client().admin().indices().prepareClose(indicesToSnapshot).setWaitForActiveShards(ActiveShardCount.ALL).get());
    }
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = clusterAdmin().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndicesOptions(IndicesOptions.lenientExpand()).setIndices(indicesToSnapshot).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = clusterAdmin().prepareGetSnapshots("test-repo").setSnapshots(randomFrom("test-snap", "_all", "*", "*-snap", "test*")).get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    SnapshotInfo snapshotInfo = snapshotInfos.get(0);
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
    if (snapshotClosed) {
        assertAcked(client().admin().indices().prepareOpen(indicesToSnapshot).setWaitForActiveShards(ActiveShardCount.ALL).get());
    }
    logger.info("--> delete some data");
    for (int i = 0; i < 50; i++) {
        client().prepareDelete("test-idx-1", Integer.toString(i)).get();
    }
    for (int i = 50; i < 100; i++) {
        client().prepareDelete("test-idx-2", Integer.toString(i)).get();
    }
    for (int i = 0; i < 100; i += 2) {
        client().prepareDelete("test-idx-3", Integer.toString(i)).get();
    }
    assertAllSuccessful(refresh());
    assertDocCount("test-idx-1", 50L);
    assertDocCount("test-idx-2", 50L);
    assertDocCount("test-idx-3", 50L);
    logger.info("--> close indices");
    client().admin().indices().prepareClose("test-idx-1", "test-idx-2").get();
    logger.info("--> restore all indices from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    assertDocCount("test-idx-1", 100L);
    assertDocCount("test-idx-2", 100L);
    assertDocCount("test-idx-3", 50L);
    assertNull(client().admin().indices().prepareGetSettings("test-idx-1").get().getSetting("test-idx-1", MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey()));
    for (ShardStats shardStats : client().admin().indices().prepareStats(indicesToSnapshot).clear().get().getShards()) {
        String historyUUID = shardStats.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY);
        ShardId shardId = shardStats.getShardRouting().shardId();
        assertThat(shardStats.getShardRouting() + " doesn't have a history uuid", historyUUID, notNullValue());
        assertThat(shardStats.getShardRouting() + " doesn't have a new history", historyUUID, not(equalTo(historyUUIDs.get(shardId))));
    }
    // Test restore after index deletion
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-2");
    logger.info("--> restore one index after deletion");
    restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-2").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    assertDocCount("test-idx-1", 100);
    ClusterState clusterState = clusterAdmin().prepareState().get().getState();
    assertThat(clusterState.getMetadata().hasIndex("test-idx-1"), equalTo(true));
    assertThat(clusterState.getMetadata().hasIndex("test-idx-2"), equalTo(false));
    assertNull(client().admin().indices().prepareGetSettings("test-idx-1").get().getSetting("test-idx-1", MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey()));
    for (ShardStats shardStats : client().admin().indices().prepareStats(indicesToSnapshot).clear().get().getShards()) {
        String historyUUID = shardStats.getCommitStats().getUserData().get(Engine.HISTORY_UUID_KEY);
        ShardId shardId = shardStats.getShardRouting().shardId();
        assertThat(shardStats.getShardRouting() + " doesn't have a history uuid", historyUUID, notNullValue());
        assertThat(shardStats.getShardRouting() + " doesn't have a new history", historyUUID, not(equalTo(historyUUIDs.get(shardId))));
    }
    if (flushResponseFuture != null) {
        // Finish flush
        flushResponseFuture.actionGet();
    }
}
Also used : ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) ClusterState(org.opensearch.cluster.ClusterState) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) ShardId(org.opensearch.index.shard.ShardId) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)

Example 34 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class CorruptedBlobStoreRepositoryIT method testSnapshotWithMissingShardLevelIndexFile.

public void testSnapshotWithMissingShardLevelIndexFile() throws Exception {
    disableRepoConsistencyCheck("This test uses a purposely broken repository so it would fail consistency checks");
    Path repo = randomRepoPath();
    createRepository("test-repo", "fs", repo);
    createIndex("test-idx-1", "test-idx-2");
    logger.info("--> indexing some data");
    indexRandom(true, client().prepareIndex("test-idx-1").setSource("foo", "bar"), client().prepareIndex("test-idx-2").setSource("foo", "bar"));
    logger.info("--> creating snapshot");
    clusterAdmin().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx-*").get();
    logger.info("--> deleting shard level index file");
    final Path indicesPath = repo.resolve("indices");
    for (IndexId indexId : getRepositoryData("test-repo").getIndices().values()) {
        final Path shardGen;
        try (Stream<Path> shardFiles = Files.list(indicesPath.resolve(indexId.getId()).resolve("0"))) {
            shardGen = shardFiles.filter(file -> file.getFileName().toString().startsWith(BlobStoreRepository.INDEX_FILE_PREFIX)).findFirst().orElseThrow(() -> new AssertionError("Failed to find shard index blob"));
        }
        Files.delete(shardGen);
    }
    logger.info("--> creating another snapshot");
    CreateSnapshotResponse createSnapshotResponse = clusterAdmin().prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(true).setIndices("test-idx-1").get();
    assertEquals(createSnapshotResponse.getSnapshotInfo().successfulShards(), createSnapshotResponse.getSnapshotInfo().totalShards() - 1);
    logger.info("--> restoring the first snapshot, the repository should not have lost any shard data despite deleting index-N, " + "because it uses snap-*.data files and not the index-N to determine what files to restore");
    client().admin().indices().prepareDelete("test-idx-1", "test-idx-2").get();
    RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).get();
    assertEquals(0, restoreSnapshotResponse.getRestoreInfo().failedShards());
}
Also used : Path(java.nio.file.Path) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) RepositoriesService(org.opensearch.repositories.RepositoriesService) ShardGenerations(org.opensearch.repositories.ShardGenerations) BlobStoreRepository(org.opensearch.repositories.blobstore.BlobStoreRepository) Metadata(org.opensearch.cluster.metadata.Metadata) BytesReference(org.opensearch.common.bytes.BytesReference) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) Version(org.opensearch.Version) Function(java.util.function.Function) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) ClusterState(org.opensearch.cluster.ClusterState) IndexId(org.opensearch.repositories.IndexId) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Locale(java.util.Locale) IndexMetaDataGenerations(org.opensearch.repositories.IndexMetaDataGenerations) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Matchers.hasSize(org.hamcrest.Matchers.hasSize) OpenSearchAssertions.assertFileExists(org.opensearch.test.hamcrest.OpenSearchAssertions.assertFileExists) Path(java.nio.file.Path) Repository(org.opensearch.repositories.Repository) RepositoryException(org.opensearch.repositories.RepositoryException) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) RepositoryData(org.opensearch.repositories.RepositoryData) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.opensearch.client.Client) RepositoriesMetadata(org.opensearch.cluster.metadata.RepositoriesMetadata) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) IOUtils(org.opensearch.core.internal.io.IOUtils) ExecutionException(java.util.concurrent.ExecutionException) SeekableByteChannel(java.nio.channels.SeekableByteChannel) List(java.util.List) Stream(java.util.stream.Stream) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexId(org.opensearch.repositories.IndexId) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 35 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class RestoreSnapshotIT method testRestoreTemplates.

public void testRestoreTemplates() throws Exception {
    createRepository("test-repo", "fs");
    logger.info("-->  creating test template");
    assertThat(client().admin().indices().preparePutTemplate("test-template").setPatterns(Collections.singletonList("te*")).setMapping(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("field1").field("type", "text").field("store", true).endObject().startObject("field2").field("type", "keyword").field("store", true).endObject().endObject().endObject()).get().isAcknowledged(), equalTo(true));
    logger.info("--> snapshot");
    final SnapshotInfo snapshotInfo = assertSuccessful(clusterAdmin().prepareCreateSnapshot("test-repo", "test-snap").setIndices().setWaitForCompletion(true).execute());
    assertThat(snapshotInfo.totalShards(), equalTo(0));
    assertThat(snapshotInfo.successfulShards(), equalTo(0));
    assertThat(getSnapshot("test-repo", "test-snap").state(), equalTo(SnapshotState.SUCCESS));
    logger.info("-->  delete test template");
    assertThat(client().admin().indices().prepareDeleteTemplate("test-template").get().isAcknowledged(), equalTo(true));
    GetIndexTemplatesResponse getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateMissing(getIndexTemplatesResponse, "test-template");
    logger.info("--> restore cluster state");
    RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setRestoreGlobalState(true).execute().actionGet();
    // We don't restore any indices here
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(0));
    logger.info("--> check that template is restored");
    getIndexTemplatesResponse = client().admin().indices().prepareGetTemplates().get();
    assertIndexTemplateExists(getIndexTemplatesResponse, "test-template");
}
Also used : GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Aggregations

RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)49 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)24 Client (org.opensearch.client.Client)23 Path (java.nio.file.Path)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Settings (org.opensearch.common.settings.Settings)11 ClusterState (org.opensearch.cluster.ClusterState)8 RepositoriesService (org.opensearch.repositories.RepositoriesService)8 List (java.util.List)6 Map (java.util.Map)6 SnapshotsStatusResponse (org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse)6 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)6 Collections (java.util.Collections)5 Matchers.is (org.hamcrest.Matchers.is)5 RestoreSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest)5 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)5 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4