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());
}
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);
}
}
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));
}
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));
}
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);
}
Aggregations