diff --git a/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java b/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java index 79da77b89a6..b1d1f74682c 100644 --- a/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java +++ b/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; import java.util.List; @@ -157,6 +158,20 @@ public class SnapshotStatus implements ToXContent, Streamable { return snapshotInfo; } + @Override + public String toString() { + try { + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + builder.startObject(); + toXContent(builder, EMPTY_PARAMS); + builder.endObject(); + return builder.string(); + } catch (IOException e) { + return "{ \"error\" : \"" + e.getMessage() + "\"}"; + } + } + + /** * Returns number of files in the snapshot */ diff --git a/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreTests.java b/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreTests.java index ca18350c9ab..510c170727a 100644 --- a/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreTests.java +++ b/src/test/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreTests.java @@ -20,6 +20,8 @@ package org.elasticsearch.snapshots; import com.carrotsearch.randomizedtesting.LifecycleScope; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListenableFuture; import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; @@ -247,11 +249,37 @@ public class DedicatedClusterSnapshotRestoreTests extends AbstractSnapshotTests assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.FAILED)); - createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(true).setPartial(true).execute().actionGet(); - logger.info("State: [{}], Reason: [{}]", createSnapshotResponse.getSnapshotInfo().state(), createSnapshotResponse.getSnapshotInfo().reason()); - assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(12)); - assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), lessThan(12)); - assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(6)); + if (randomBoolean()) { + logger.info("checking snapshot completion using status"); + client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(false).setPartial(true).execute().actionGet(); + awaitBusy(new Predicate() { + @Override + public boolean apply(Object o) { + SnapshotsStatusResponse snapshotsStatusResponse = client().admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("test-snap-2").get(); + ImmutableList snapshotStatuses = snapshotsStatusResponse.getSnapshots(); + if(snapshotStatuses.size() == 1) { + logger.trace("current snapshot status [{}]", snapshotStatuses.get(0)); + return snapshotStatuses.get(0).getState().completed(); + } + return false; + } + }); + SnapshotsStatusResponse snapshotsStatusResponse = client().admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("test-snap-2").get(); + ImmutableList snapshotStatuses = snapshotsStatusResponse.getSnapshots(); + assertThat(snapshotStatuses.size(), equalTo(1)); + SnapshotStatus snapshotStatus = snapshotStatuses.get(0); + logger.info("State: [{}], Reason: [{}]", createSnapshotResponse.getSnapshotInfo().state(), createSnapshotResponse.getSnapshotInfo().reason()); + assertThat(snapshotStatus.getShardsStats().getTotalShards(), equalTo(12)); + assertThat(snapshotStatus.getShardsStats().getDoneShards(), lessThan(12)); + assertThat(snapshotStatus.getShardsStats().getDoneShards(), greaterThan(6)); + } else { + logger.info("checking snapshot completion using wait_for_completion flag"); + createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-2").setWaitForCompletion(true).setPartial(true).execute().actionGet(); + logger.info("State: [{}], Reason: [{}]", createSnapshotResponse.getSnapshotInfo().state(), createSnapshotResponse.getSnapshotInfo().reason()); + assertThat(createSnapshotResponse.getSnapshotInfo().totalShards(), equalTo(12)); + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), lessThan(12)); + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(6)); + } assertThat(client().admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap-2").execute().actionGet().getSnapshots().get(0).state(), equalTo(SnapshotState.PARTIAL)); assertAcked(client().admin().indices().prepareClose("test-idx-1", "test-idx-2").execute().actionGet());