use of org.opensearch.snapshots.SnapshotInfo 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());
}
use of org.opensearch.snapshots.SnapshotInfo in project OpenSearch by opensearch-project.
the class SnapshotClientDocumentationIT method testSnapshotCreate.
public void testSnapshotCreate() throws IOException {
RestHighLevelClient client = highLevelClient();
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index0");
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
createIndexRequest = new CreateIndexRequest("test-index1");
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
createTestRepositories();
// tag::create-snapshot-request
CreateSnapshotRequest request = new CreateSnapshotRequest();
// end::create-snapshot-request
// tag::create-snapshot-request-repositoryName
// <1>
request.repository(repositoryName);
// end::create-snapshot-request-repositoryName
// tag::create-snapshot-request-snapshotName
// <1>
request.snapshot(snapshotName);
// end::create-snapshot-request-snapshotName
// tag::create-snapshot-request-indices
// <1>
request.indices("test-index0", "test-index1");
// end::create-snapshot-request-indices
// tag::create-snapshot-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true));
// end::create-snapshot-request-indicesOptions
// tag::create-snapshot-request-partial
// <1>
request.partial(false);
// end::create-snapshot-request-partial
// tag::create-snapshot-request-includeGlobalState
// <1>
request.includeGlobalState(true);
// end::create-snapshot-request-includeGlobalState
// tag::create-snapshot-request-masterTimeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
// <2>
request.masterNodeTimeout("1m");
// end::create-snapshot-request-masterTimeout
// tag::create-snapshot-request-waitForCompletion
// <1>
request.waitForCompletion(true);
// end::create-snapshot-request-waitForCompletion
// tag::create-snapshot-execute
CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT);
// end::create-snapshot-execute
// tag::create-snapshot-response
// <1>
RestStatus status = response.status();
// end::create-snapshot-response
assertEquals(RestStatus.OK, status);
// tag::create-snapshot-response-snapshot-info
// <1>
SnapshotInfo snapshotInfo = response.getSnapshotInfo();
// end::create-snapshot-response-snapshot-info
assertNotNull(snapshotInfo);
}
use of org.opensearch.snapshots.SnapshotInfo in project OpenSearch by opensearch-project.
the class SnapshotIT method testGetSnapshots.
public void testGetSnapshots() throws IOException {
String repository = "test_repository";
String snapshot1 = "test_snapshot1";
String snapshot2 = "test_snapshot2";
AcknowledgedResponse putRepositoryResponse = createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}");
assertTrue(putRepositoryResponse.isAcknowledged());
CreateSnapshotRequest createSnapshotRequest1 = new CreateSnapshotRequest(repository, snapshot1);
createSnapshotRequest1.waitForCompletion(true);
CreateSnapshotResponse putSnapshotResponse1 = createTestSnapshot(createSnapshotRequest1);
CreateSnapshotRequest createSnapshotRequest2 = new CreateSnapshotRequest(repository, snapshot2);
createSnapshotRequest2.waitForCompletion(true);
Map<String, Object> originalMetadata = randomUserMetadata();
createSnapshotRequest2.userMetadata(originalMetadata);
CreateSnapshotResponse putSnapshotResponse2 = createTestSnapshot(createSnapshotRequest2);
// check that the request went ok without parsing JSON here. When using the high level client, check acknowledgement instead.
assertEquals(RestStatus.OK, putSnapshotResponse1.status());
assertEquals(RestStatus.OK, putSnapshotResponse2.status());
GetSnapshotsRequest request;
if (randomBoolean()) {
request = new GetSnapshotsRequest(repository);
} else if (randomBoolean()) {
request = new GetSnapshotsRequest(repository, new String[] { "_all" });
} else {
request = new GetSnapshotsRequest(repository, new String[] { snapshot1, snapshot2 });
}
GetSnapshotsResponse response = execute(request, highLevelClient().snapshot()::get, highLevelClient().snapshot()::getAsync);
assertEquals(2, response.getSnapshots().size());
assertThat(response.getSnapshots().stream().map((s) -> s.snapshotId().getName()).collect(Collectors.toList()), contains("test_snapshot1", "test_snapshot2"));
Optional<Map<String, Object>> returnedMetadata = response.getSnapshots().stream().filter(s -> s.snapshotId().getName().equals("test_snapshot2")).findFirst().map(SnapshotInfo::userMetadata);
if (returnedMetadata.isPresent()) {
assertEquals(originalMetadata, returnedMetadata.get());
} else {
assertNull("retrieved metadata is null, expected non-null metadata", originalMetadata);
}
}
use of org.opensearch.snapshots.SnapshotInfo in project OpenSearch by opensearch-project.
the class TransportSnapshotsStatusAction method loadRepositoryData.
private void loadRepositoryData(SnapshotsInProgress snapshotsInProgress, SnapshotsStatusRequest request, List<SnapshotStatus> builder, Set<String> currentSnapshotNames, String repositoryName, ActionListener<SnapshotsStatusResponse> listener) {
final Set<String> requestedSnapshotNames = Sets.newHashSet(request.snapshots());
final StepListener<RepositoryData> repositoryDataListener = new StepListener<>();
repositoriesService.getRepositoryData(repositoryName, repositoryDataListener);
repositoryDataListener.whenComplete(repositoryData -> {
final Map<String, SnapshotId> matchedSnapshotIds = repositoryData.getSnapshotIds().stream().filter(s -> requestedSnapshotNames.contains(s.getName())).collect(Collectors.toMap(SnapshotId::getName, Function.identity()));
for (final String snapshotName : request.snapshots()) {
if (currentSnapshotNames.contains(snapshotName)) {
// we've already found this snapshot in the current snapshot entries, so skip over
continue;
}
SnapshotId snapshotId = matchedSnapshotIds.get(snapshotName);
if (snapshotId == null) {
// neither in the current snapshot entries nor found in the repository
if (request.ignoreUnavailable()) {
// ignoring unavailable snapshots, so skip over
logger.debug("snapshot status request ignoring snapshot [{}], not found in repository [{}]", snapshotName, repositoryName);
continue;
} else {
throw new SnapshotMissingException(repositoryName, snapshotName);
}
}
SnapshotInfo snapshotInfo = snapshot(snapshotsInProgress, repositoryName, snapshotId);
List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
if (snapshotInfo.state().completed()) {
Map<ShardId, IndexShardSnapshotStatus> shardStatuses = snapshotShards(repositoryName, repositoryData, snapshotInfo);
for (Map.Entry<ShardId, IndexShardSnapshotStatus> shardStatus : shardStatuses.entrySet()) {
IndexShardSnapshotStatus.Copy lastSnapshotStatus = shardStatus.getValue().asCopy();
shardStatusBuilder.add(new SnapshotIndexShardStatus(shardStatus.getKey(), lastSnapshotStatus));
}
final SnapshotsInProgress.State state;
switch(snapshotInfo.state()) {
case FAILED:
state = SnapshotsInProgress.State.FAILED;
break;
case SUCCESS:
case PARTIAL:
// Translating both PARTIAL and SUCCESS to SUCCESS for now
// TODO: add the differentiation on the metadata level in the next major release
state = SnapshotsInProgress.State.SUCCESS;
break;
default:
throw new IllegalArgumentException("Unknown snapshot state " + snapshotInfo.state());
}
final long startTime = snapshotInfo.startTime();
final long endTime = snapshotInfo.endTime();
assert endTime >= startTime || (endTime == 0L && snapshotInfo.state().completed() == false) : "Inconsistent timestamps found in SnapshotInfo [" + snapshotInfo + "]";
builder.add(new SnapshotStatus(new Snapshot(repositoryName, snapshotId), state, Collections.unmodifiableList(shardStatusBuilder), snapshotInfo.includeGlobalState(), startTime, // Use current time to calculate overall runtime for in-progress snapshots that have endTime == 0
(endTime == 0 ? threadPool.absoluteTimeInMillis() : endTime) - startTime));
}
}
listener.onResponse(new SnapshotsStatusResponse(Collections.unmodifiableList(builder)));
}, listener::onFailure);
}
use of org.opensearch.snapshots.SnapshotInfo in project OpenSearch by opensearch-project.
the class GetSnapshotsResponse method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.startArray("snapshots");
for (SnapshotInfo snapshotInfo : snapshots) {
snapshotInfo.toXContent(builder, params);
}
builder.endArray();
builder.endObject();
return builder;
}
Aggregations