Search in sources :

Example 16 with SnapshotInfo

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());
}
Also used : SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) GetSnapshotsRequest(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) SnapshotId(org.opensearch.snapshots.SnapshotId) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) SnapshotState(org.opensearch.snapshots.SnapshotState) RestStatus(org.opensearch.rest.RestStatus)

Example 17 with SnapshotInfo

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);
}
Also used : SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) RestStatus(org.opensearch.rest.RestStatus) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest)

Example 18 with 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);
    }
}
Also used : GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) GetSnapshotsRequest(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with SnapshotInfo

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);
}
Also used : Arrays(java.util.Arrays) SnapshotState(org.opensearch.snapshots.SnapshotState) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Strings(org.opensearch.common.Strings) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) IndexId(org.opensearch.repositories.IndexId) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) Repository(org.opensearch.repositories.Repository) CollectionUtils(org.opensearch.common.util.CollectionUtils) SnapshotId(org.opensearch.snapshots.SnapshotId) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Set(java.util.Set) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) Logger(org.apache.logging.log4j.Logger) SnapshotShardFailure(org.opensearch.snapshots.SnapshotShardFailure) StepListener(org.opensearch.action.StepListener) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) RepositoriesService(org.opensearch.repositories.RepositoriesService) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) TransportMasterNodeAction(org.opensearch.action.support.master.TransportMasterNodeAction) SnapshotShardsService(org.opensearch.snapshots.SnapshotShardsService) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) StreamInput(org.opensearch.common.io.stream.StreamInput) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IOException(java.io.IOException) ShardId(org.opensearch.index.shard.ShardId) Sets(org.opensearch.common.util.set.Sets) Snapshot(org.opensearch.snapshots.Snapshot) ClusterService(org.opensearch.cluster.service.ClusterService) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) ArrayList(java.util.ArrayList) ShardId(org.opensearch.index.shard.ShardId) SnapshotMissingException(org.opensearch.snapshots.SnapshotMissingException) RepositoryData(org.opensearch.repositories.RepositoryData) SnapshotId(org.opensearch.snapshots.SnapshotId) Snapshot(org.opensearch.snapshots.Snapshot) SnapshotInfo(org.opensearch.snapshots.SnapshotInfo) IndexShardSnapshotStatus(org.opensearch.index.snapshots.IndexShardSnapshotStatus) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) StepListener(org.opensearch.action.StepListener) Map(java.util.Map) HashMap(java.util.HashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 20 with SnapshotInfo

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;
}
Also used : SnapshotInfo(org.opensearch.snapshots.SnapshotInfo)

Aggregations

SnapshotInfo (org.opensearch.snapshots.SnapshotInfo)22 SnapshotId (org.opensearch.snapshots.SnapshotId)12 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ClusterService (org.opensearch.cluster.service.ClusterService)7 Repository (org.opensearch.repositories.Repository)7 RepositoryData (org.opensearch.repositories.RepositoryData)7 Collections (java.util.Collections)6 List (java.util.List)6 Map (java.util.Map)6 ClusterState (org.opensearch.cluster.ClusterState)6 SnapshotsInProgress (org.opensearch.cluster.SnapshotsInProgress)6 IndexId (org.opensearch.repositories.IndexId)6 HashMap (java.util.HashMap)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 RepositoriesService (org.opensearch.repositories.RepositoriesService)5 SnapshotMissingException (org.opensearch.snapshots.SnapshotMissingException)5 HashSet (java.util.HashSet)4 LogManager (org.apache.logging.log4j.LogManager)4