Search in sources :

Example 36 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testRestoreIncreasesPrimaryTerms.

public void testRestoreIncreasesPrimaryTerms() {
    final String indexName = randomAlphaOfLengthBetween(5, 10).toLowerCase(Locale.ROOT);
    createIndex(indexName, indexSettingsNoReplicas(2).build());
    ensureGreen(indexName);
    if (randomBoolean()) {
        // open and close the index to increase the primary terms
        for (int i = 0; i < randomInt(3); i++) {
            assertAcked(client().admin().indices().prepareClose(indexName));
            assertAcked(client().admin().indices().prepareOpen(indexName));
        }
    }
    final IndexMetadata indexMetadata = clusterAdmin().prepareState().clear().setIndices(indexName).setMetadata(true).get().getState().metadata().index(indexName);
    assertThat(indexMetadata.getSettings().get(IndexMetadata.SETTING_HISTORY_UUID), nullValue());
    final int numPrimaries = getNumShards(indexName).numPrimaries;
    final Map<Integer, Long> primaryTerms = IntStream.range(0, numPrimaries).boxed().collect(Collectors.toMap(shardId -> shardId, indexMetadata::primaryTerm));
    createRepository("test-repo", "fs");
    final CreateSnapshotResponse createSnapshotResponse = clusterAdmin().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices(indexName).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(numPrimaries));
    assertThat(createSnapshotResponse.getSnapshotInfo().failedShards(), equalTo(0));
    assertAcked(client().admin().indices().prepareClose(indexName));
    final RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).get();
    assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(numPrimaries));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
    final IndexMetadata restoredIndexMetadata = clusterAdmin().prepareState().clear().setIndices(indexName).setMetadata(true).get().getState().metadata().index(indexName);
    for (int shardId = 0; shardId < numPrimaries; shardId++) {
        assertThat(restoredIndexMetadata.primaryTerm(shardId), greaterThan(primaryTerms.get(shardId)));
    }
    assertThat(restoredIndexMetadata.getSettings().get(IndexMetadata.SETTING_HISTORY_UUID), notNullValue());
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) RepositoriesService(org.opensearch.repositories.RepositoriesService) IntStream(java.util.stream.IntStream) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Matchers.not(org.hamcrest.Matchers.not) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) INDEX_SOFT_DELETES_SETTING(org.opensearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING) OpenSearchAssertions.assertHitCount(org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) XContentFactory(org.opensearch.common.xcontent.XContentFactory) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Path(java.nio.file.Path) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) OpenSearchAssertions.assertIndexTemplateMissing(org.opensearch.test.hamcrest.OpenSearchAssertions.assertIndexTemplateMissing) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.opensearch.client.Client) OpenSearchAssertions.assertIndexTemplateExists(org.opensearch.test.hamcrest.OpenSearchAssertions.assertIndexTemplateExists) TimeValue(org.opensearch.common.unit.TimeValue) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers.allOf(org.hamcrest.Matchers.allOf) Settings(org.opensearch.common.settings.Settings) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) INDEX_REFRESH_INTERVAL_SETTING(org.opensearch.index.IndexSettings.INDEX_REFRESH_INTERVAL_SETTING) QueryBuilders.matchQuery(org.opensearch.index.query.QueryBuilders.matchQuery) ActionFuture(org.opensearch.action.ActionFuture) TimeUnit(java.util.concurrent.TimeUnit) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) GetIndexTemplatesResponse(org.opensearch.action.admin.indices.template.get.GetIndexTemplatesResponse) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING(org.opensearch.indices.recovery.RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 37 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testRecreateBlocksOnRestore.

public void testRecreateBlocksOnRestore() throws Exception {
    Client client = client();
    createRepository("test-repo", "fs");
    Settings.Builder indexSettings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s");
    logger.info("--> create index");
    assertAcked(prepareCreate("test-idx", 2, indexSettings));
    try {
        List<String> initialBlockSettings = randomSubsetOf(randomInt(3), IndexMetadata.SETTING_BLOCKS_WRITE, IndexMetadata.SETTING_BLOCKS_METADATA, IndexMetadata.SETTING_READ_ONLY);
        Settings.Builder initialSettingsBuilder = Settings.builder();
        for (String blockSetting : initialBlockSettings) {
            initialSettingsBuilder.put(blockSetting, true);
        }
        Settings initialSettings = initialSettingsBuilder.build();
        logger.info("--> using initial block settings {}", initialSettings);
        if (!initialSettings.isEmpty()) {
            logger.info("--> apply initial blocks to index");
            client().admin().indices().prepareUpdateSettings("test-idx").setSettings(initialSettingsBuilder).get();
        }
        logger.info("--> snapshot index");
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        logger.info("--> remove blocks and delete index");
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_READ);
        cluster().wipeIndices("test-idx");
        logger.info("--> restore index with additional block changes");
        List<String> changeBlockSettings = randomSubsetOf(randomInt(4), IndexMetadata.SETTING_BLOCKS_METADATA, IndexMetadata.SETTING_BLOCKS_WRITE, IndexMetadata.SETTING_READ_ONLY, IndexMetadata.SETTING_BLOCKS_READ);
        Settings.Builder changedSettingsBuilder = Settings.builder();
        for (String blockSetting : changeBlockSettings) {
            changedSettingsBuilder.put(blockSetting, randomBoolean());
        }
        Settings changedSettings = changedSettingsBuilder.build();
        logger.info("--> applying changed block settings {}", changedSettings);
        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndexSettings(changedSettings).setWaitForCompletion(true).execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        ClusterBlocks blocks = client.admin().cluster().prepareState().clear().setBlocks(true).get().getState().blocks();
        // compute current index settings (as we cannot query them if they contain SETTING_BLOCKS_METADATA)
        Settings mergedSettings = Settings.builder().put(initialSettings).put(changedSettings).build();
        logger.info("--> merged block settings {}", mergedSettings);
        logger.info("--> checking consistency between settings and blocks");
        assertThat(mergedSettings.getAsBoolean(IndexMetadata.SETTING_BLOCKS_METADATA, false), is(blocks.hasIndexBlock("test-idx", IndexMetadata.INDEX_METADATA_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetadata.SETTING_BLOCKS_READ, false), is(blocks.hasIndexBlock("test-idx", IndexMetadata.INDEX_READ_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetadata.SETTING_BLOCKS_WRITE, false), is(blocks.hasIndexBlock("test-idx", IndexMetadata.INDEX_WRITE_BLOCK)));
        assertThat(mergedSettings.getAsBoolean(IndexMetadata.SETTING_READ_ONLY, false), is(blocks.hasIndexBlock("test-idx", IndexMetadata.INDEX_READ_ONLY_BLOCK)));
    } finally {
        logger.info("--> cleaning up blocks");
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_METADATA);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_READ_ONLY);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_WRITE);
        disableIndexBlock("test-idx", IndexMetadata.SETTING_BLOCKS_READ);
    }
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 38 with RestoreSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testSnapshottingWithMissingSequenceNumbers.

public void testSnapshottingWithMissingSequenceNumbers() {
    final String repositoryName = "test-repo";
    final String snapshotName = "test-snap";
    final String indexName = "test-idx";
    final Client client = client();
    createRepository(repositoryName, "fs");
    logger.info("--> creating an index and indexing documents");
    final String dataNode = internalCluster().getDataNodeInstance(ClusterService.class).localNode().getName();
    final Settings settings = indexSettingsNoReplicas(1).put("index.routing.allocation.include._name", dataNode).build();
    createIndex(indexName, settings);
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        index(indexName, "_doc", Integer.toString(i), "foo", "bar" + i);
    }
    final Index index = resolveIndex(indexName);
    final IndexShard primary = internalCluster().getInstance(IndicesService.class, dataNode).getShardOrNull(new ShardId(index, 0));
    // create a gap in the sequence numbers
    EngineTestCase.generateNewSeqNo(getEngineFromShard(primary));
    for (int i = 5; i < 10; i++) {
        index(indexName, "_doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    createSnapshot(repositoryName, snapshotName, Collections.singletonList(indexName));
    logger.info("--> delete indices");
    assertAcked(client.admin().indices().prepareDelete(indexName));
    logger.info("--> restore all indices from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    IndicesStatsResponse stats = client().admin().indices().prepareStats(indexName).clear().get();
    ShardStats shardStats = stats.getShards()[0];
    assertTrue(shardStats.getShardRouting().primary());
    // 10 indexed docs and one "missing" op.
    assertThat(shardStats.getSeqNoStats().getLocalCheckpoint(), equalTo(10L));
    assertThat(shardStats.getSeqNoStats().getGlobalCheckpoint(), equalTo(10L));
    logger.info("--> indexing some more");
    for (int i = 10; i < 15; i++) {
        index(indexName, "_doc", Integer.toString(i), "foo", "bar" + i);
    }
    client().admin().indices().prepareFlush(indexName).setForce(true).setWaitIfOngoing(true).get();
    stats = client().admin().indices().prepareStats(indexName).clear().get();
    shardStats = stats.getShards()[0];
    assertTrue(shardStats.getShardRouting().primary());
    // 15 indexed docs and one "missing" op.
    assertThat(shardStats.getSeqNoStats().getLocalCheckpoint(), equalTo(15L));
    assertThat(shardStats.getSeqNoStats().getGlobalCheckpoint(), equalTo(15L));
    assertThat(shardStats.getSeqNoStats().getMaxSeqNo(), equalTo(15L));
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndicesStatsResponse(org.opensearch.action.admin.indices.stats.IndicesStatsResponse) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 39 with RestoreSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testCloseIndexDuringRestore.

public void testCloseIndexDuringRestore() throws Exception {
    Client client = client();
    createRepository("test-repo", "mock");
    createIndexWithRandomDocs("test-idx-1", 100);
    createIndexWithRandomDocs("test-idx-2", 100);
    logger.info("--> snapshot");
    assertThat(client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setIndices("test-idx-*").setWaitForCompletion(true).get().getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> deleting indices before restoring");
    assertAcked(client.admin().indices().prepareDelete("test-idx-*").get());
    blockAllDataNodes("test-repo");
    logger.info("--> execution will be blocked on all data nodes");
    final ActionFuture<RestoreSnapshotResponse> restoreFut;
    try {
        logger.info("--> start restore");
        restoreFut = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute();
        logger.info("--> waiting for block to kick in");
        waitForBlockOnAnyDataNode("test-repo", TimeValue.timeValueMinutes(1));
        logger.info("--> close index while restore is running");
        try {
            client.admin().indices().prepareClose("test-idx-1").get();
            fail("Expected closing index to fail during restore");
        } catch (IllegalArgumentException e) {
            assertThat(e.getMessage(), containsString("Cannot close indices that are being restored: [[test-idx-1/"));
        }
    } finally {
        // unblock even if the try block fails otherwise we will get bogus failures when we delete all indices in test teardown.
        logger.info("--> unblocking all data nodes");
        unblockAllDataNodes("test-repo");
    }
    logger.info("--> wait for restore to finish");
    RestoreSnapshotResponse restoreSnapshotResponse = restoreFut.get();
    logger.info("--> check that all shards were recovered");
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
}
Also used : Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 40 with RestoreSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method unrestorableUseCase.

/**
 * Execute the unrestorable test use case *
 */
private void unrestorableUseCase(final String indexName, final Settings createIndexSettings, final Settings repositorySettings, final Settings restoreIndexSettings, final Consumer<UnassignedInfo> checkUnassignedInfo, final Runnable fixUpAction) throws Exception {
    // create a test repository
    final Path repositoryLocation = randomRepoPath();
    createRepository("test-repo", "fs", repositoryLocation);
    // create a test index
    assertAcked(prepareCreate(indexName, Settings.builder().put(createIndexSettings)));
    // index some documents
    final int nbDocs = scaledRandomIntBetween(10, 100);
    indexRandomDocs(indexName, nbDocs);
    // create a snapshot
    final NumShards numShards = getNumShards(indexName);
    final SnapshotInfo snapshotInfo = createSnapshot("test-repo", "test-snap", Collections.singletonList(indexName));
    assertThat(snapshotInfo.successfulShards(), equalTo(numShards.numPrimaries));
    // delete the test index
    assertAcked(client().admin().indices().prepareDelete(indexName));
    // update the test repository
    assertAcked(clusterAdmin().preparePutRepository("test-repo").setType("mock").setSettings(Settings.builder().put("location", repositoryLocation).put(repositorySettings).build()));
    // attempt to restore the snapshot with the given settings
    RestoreSnapshotResponse restoreResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setIndices(indexName).setIndexSettings(restoreIndexSettings).setWaitForCompletion(true).get();
    // check that all shards failed during restore
    assertThat(restoreResponse.getRestoreInfo().totalShards(), equalTo(numShards.numPrimaries));
    assertThat(restoreResponse.getRestoreInfo().successfulShards(), equalTo(0));
    ClusterStateResponse clusterStateResponse = clusterAdmin().prepareState().setCustoms(true).setRoutingTable(true).get();
    // check that there is no restore in progress
    RestoreInProgress restoreInProgress = clusterStateResponse.getState().custom(RestoreInProgress.TYPE);
    assertNotNull("RestoreInProgress must be not null", restoreInProgress);
    assertTrue("RestoreInProgress must be empty but found entries in " + restoreInProgress, restoreInProgress.isEmpty());
    // check that the shards have been created but are not assigned
    assertThat(clusterStateResponse.getState().getRoutingTable().allShards(indexName), hasSize(numShards.totalNumShards));
    // check that every primary shard is unassigned
    for (ShardRouting shard : clusterStateResponse.getState().getRoutingTable().allShards(indexName)) {
        if (shard.primary()) {
            assertThat(shard.state(), equalTo(ShardRoutingState.UNASSIGNED));
            assertThat(shard.recoverySource().getType(), equalTo(RecoverySource.Type.SNAPSHOT));
            assertThat(shard.unassignedInfo().getLastAllocationStatus(), equalTo(UnassignedInfo.AllocationStatus.DECIDERS_NO));
            checkUnassignedInfo.accept(shard.unassignedInfo());
        }
    }
    // update the test repository in order to make it work
    createRepository("test-repo", "fs", repositoryLocation);
    // execute action to eventually fix the situation
    fixUpAction.run();
    // delete the index and restore again
    assertAcked(client().admin().indices().prepareDelete(indexName));
    restoreResponse = clusterAdmin().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).get();
    assertThat(restoreResponse.getRestoreInfo().totalShards(), equalTo(numShards.numPrimaries));
    assertThat(restoreResponse.getRestoreInfo().successfulShards(), equalTo(numShards.numPrimaries));
    // Wait for the shards to be assigned
    ensureGreen(indexName);
    refresh(indexName);
    assertDocCount(indexName, nbDocs);
}
Also used : Path(java.nio.file.Path) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ShardRouting(org.opensearch.cluster.routing.ShardRouting) 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