|
37 | 37 | import org.elasticsearch.action.support.IndicesOptions;
|
38 | 38 | import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest;
|
39 | 39 | import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse;
|
| 40 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStats; |
| 41 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus; |
| 42 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest; |
| 43 | +import org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse; |
40 | 44 | import org.elasticsearch.client.ESRestHighLevelClientTestCase;
|
41 | 45 | import org.elasticsearch.client.Request;
|
42 | 46 | import org.elasticsearch.client.RequestOptions;
|
43 | 47 | import org.elasticsearch.client.Response;
|
44 | 48 | import org.elasticsearch.client.RestHighLevelClient;
|
| 49 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
45 | 50 | import org.elasticsearch.cluster.metadata.RepositoryMetaData;
|
46 | 51 | import org.elasticsearch.common.settings.Settings;
|
47 | 52 | import org.elasticsearch.common.unit.TimeValue;
|
|
84 | 89 | public class SnapshotClientDocumentationIT extends ESRestHighLevelClientTestCase {
|
85 | 90 |
|
86 | 91 | private static final String repositoryName = "test_repository";
|
87 |
| - |
88 | 92 | private static final String snapshotName = "test_snapshot";
|
| 93 | + private static final String indexName = "test_index"; |
89 | 94 |
|
90 | 95 | public void testSnapshotCreateRepository() throws IOException {
|
91 | 96 | RestHighLevelClient client = highLevelClient();
|
@@ -466,6 +471,7 @@ public void testSnapshotGetSnapshots() throws IOException {
|
466 | 471 | RestHighLevelClient client = highLevelClient();
|
467 | 472 |
|
468 | 473 | createTestRepositories();
|
| 474 | + createTestIndex(); |
469 | 475 | createTestSnapshots();
|
470 | 476 |
|
471 | 477 | // tag::get-snapshots-request
|
@@ -543,10 +549,84 @@ public void onFailure(Exception e) {
|
543 | 549 | }
|
544 | 550 | }
|
545 | 551 |
|
| 552 | + public void testSnapshotSnapshotsStatus() throws IOException { |
| 553 | + RestHighLevelClient client = highLevelClient(); |
| 554 | + createTestRepositories(); |
| 555 | + createTestIndex(); |
| 556 | + createTestSnapshots(); |
| 557 | + |
| 558 | + // tag::snapshots-status-request |
| 559 | + SnapshotsStatusRequest request = new SnapshotsStatusRequest(); |
| 560 | + // end::snapshots-status-request |
| 561 | + |
| 562 | + // tag::snapshots-status-request-repository |
| 563 | + request.repository(repositoryName); // <1> |
| 564 | + // end::snapshots-status-request-repository |
| 565 | + // tag::snapshots-status-request-snapshots |
| 566 | + String [] snapshots = new String[] {snapshotName}; |
| 567 | + request.snapshots(snapshots); // <1> |
| 568 | + // end::snapshots-status-request-snapshots |
| 569 | + // tag::snapshots-status-request-ignoreUnavailable |
| 570 | + request.ignoreUnavailable(true); // <1> |
| 571 | + // end::snapshots-status-request-ignoreUnavailable |
| 572 | + // tag::snapshots-status-request-masterTimeout |
| 573 | + request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> |
| 574 | + request.masterNodeTimeout("1m"); // <2> |
| 575 | + // end::snapshots-status-request-masterTimeout |
| 576 | + |
| 577 | + // tag::snapshots-status-execute |
| 578 | + SnapshotsStatusResponse response = client.snapshot().status(request, RequestOptions.DEFAULT); |
| 579 | + // end::snapshots-status-execute |
| 580 | + |
| 581 | + // tag::snapshots-status-response |
| 582 | + List<SnapshotStatus> snapshotStatusesResponse = response.getSnapshots(); |
| 583 | + SnapshotStatus snapshotStatus = snapshotStatusesResponse.get(0); // <1> |
| 584 | + SnapshotsInProgress.State snapshotState = snapshotStatus.getState(); // <2> |
| 585 | + SnapshotStats shardStats = snapshotStatus.getIndices().get(indexName).getShards().get(0).getStats(); // <3> |
| 586 | + // end::snapshots-status-response |
| 587 | + assertThat(snapshotStatusesResponse.size(), equalTo(1)); |
| 588 | + assertThat(snapshotStatusesResponse.get(0).getSnapshot().getRepository(), equalTo(SnapshotClientDocumentationIT.repositoryName)); |
| 589 | + assertThat(snapshotStatusesResponse.get(0).getSnapshot().getSnapshotId().getName(), equalTo(snapshotName)); |
| 590 | + assertThat(snapshotState.completed(), equalTo(true)); |
| 591 | + } |
| 592 | + |
| 593 | + public void testSnapshotSnapshotsStatusAsync() throws InterruptedException { |
| 594 | + RestHighLevelClient client = highLevelClient(); |
| 595 | + { |
| 596 | + SnapshotsStatusRequest request = new SnapshotsStatusRequest(); |
| 597 | + |
| 598 | + // tag::snapshots-status-execute-listener |
| 599 | + ActionListener<SnapshotsStatusResponse> listener = |
| 600 | + new ActionListener<SnapshotsStatusResponse>() { |
| 601 | + @Override |
| 602 | + public void onResponse(SnapshotsStatusResponse snapshotsStatusResponse) { |
| 603 | + // <1> |
| 604 | + } |
| 605 | + |
| 606 | + @Override |
| 607 | + public void onFailure(Exception e) { |
| 608 | + // <2> |
| 609 | + } |
| 610 | + }; |
| 611 | + // end::snapshots-status-execute-listener |
| 612 | + |
| 613 | + // Replace the empty listener with a blocking listener in test |
| 614 | + final CountDownLatch latch = new CountDownLatch(1); |
| 615 | + listener = new LatchedActionListener<>(listener, latch); |
| 616 | + |
| 617 | + // tag::snapshots-status-execute-async |
| 618 | + client.snapshot().statusAsync(request, RequestOptions.DEFAULT, listener); // <1> |
| 619 | + // end::snapshots-status-execute-async |
| 620 | + |
| 621 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 622 | + } |
| 623 | + } |
| 624 | + |
546 | 625 | public void testSnapshotDeleteSnapshot() throws IOException {
|
547 | 626 | RestHighLevelClient client = highLevelClient();
|
548 | 627 |
|
549 | 628 | createTestRepositories();
|
| 629 | + createTestIndex(); |
550 | 630 | createTestSnapshots();
|
551 | 631 |
|
552 | 632 | // tag::delete-snapshot-request
|
@@ -608,9 +688,14 @@ private void createTestRepositories() throws IOException {
|
608 | 688 | assertTrue(highLevelClient().snapshot().createRepository(request, RequestOptions.DEFAULT).isAcknowledged());
|
609 | 689 | }
|
610 | 690 |
|
| 691 | + private void createTestIndex() throws IOException { |
| 692 | + createIndex(indexName, Settings.EMPTY); |
| 693 | + } |
| 694 | + |
611 | 695 | private void createTestSnapshots() throws IOException {
|
612 | 696 | Request createSnapshot = new Request("put", String.format(Locale.ROOT, "_snapshot/%s/%s", repositoryName, snapshotName));
|
613 | 697 | createSnapshot.addParameter("wait_for_completion", "true");
|
| 698 | + createSnapshot.setJsonEntity("{\"indices\":\"" + indexName + "\"}"); |
614 | 699 | Response response = highLevelClient().getLowLevelClient().performRequest(createSnapshot);
|
615 | 700 | // check that the request went ok without parsing JSON here. When using the high level client, check acknowledgement instead.
|
616 | 701 | assertEquals(200, response.getStatusLine().getStatusCode());
|
|
0 commit comments