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