diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java index bc0bbe95488f4..f75f6cdef2405 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java @@ -176,7 +176,7 @@ public void verifyRepositoryAsync(VerifyRepositoryRequest verifyRepositoryReques * See Snapshot and Restore * API on elastic.co */ - public CreateSnapshotResponse createSnapshot(CreateSnapshotRequest createSnapshotRequest, RequestOptions options) + public CreateSnapshotResponse create(CreateSnapshotRequest createSnapshotRequest, RequestOptions options) throws IOException { return restHighLevelClient.performRequestAndParseEntity(createSnapshotRequest, RequestConverters::createSnapshot, options, CreateSnapshotResponse::fromXContent, emptySet()); @@ -188,7 +188,7 @@ public CreateSnapshotResponse createSnapshot(CreateSnapshotRequest createSnapsho * See Snapshot and Restore * API on elastic.co */ - public void createSnapshotAsync(CreateSnapshotRequest createSnapshotRequest, RequestOptions options, + public void createAsync(CreateSnapshotRequest createSnapshotRequest, RequestOptions options, ActionListener listener) { restHighLevelClient.performRequestAsyncAndParseEntity(createSnapshotRequest, RequestConverters::createSnapshot, options, CreateSnapshotResponse::fromXContent, listener, emptySet()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java index 45f9b5bbb0b0a..6d035f5db654a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java @@ -61,8 +61,8 @@ private PutRepositoryResponse createTestRepository(String repository, String typ private CreateSnapshotResponse createTestSnapshot(CreateSnapshotRequest createSnapshotRequest) throws IOException { // assumes the repository already exists - return execute(createSnapshotRequest, highLevelClient().snapshot()::createSnapshot, - highLevelClient().snapshot()::createSnapshotAsync); + return execute(createSnapshotRequest, highLevelClient().snapshot()::create, + highLevelClient().snapshot()::createAsync); } public void testCreateRepository() throws IOException { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java index 403ebc7d774a4..68a8113af6d38 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java @@ -425,7 +425,7 @@ public void testSnapshotCreate() throws IOException { // end::create-snapshot-request-waitForCompletion // tag::create-snapshot-execute - CreateSnapshotResponse response = client.snapshot().createSnapshot(request, RequestOptions.DEFAULT); + CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT); // end::create-snapshot-execute // tag::create-snapshot-response @@ -433,6 +433,12 @@ public void testSnapshotCreate() throws IOException { // end::create-snapshot-response assertEquals(RestStatus.OK, status); + + // tag::create-snapshot-response-snapshot-info + SnapshotInfo snapshotInfo = response.getSnapshotInfo(); // <1> + // end::create-snapshot-response-snapshot-info + + assertNotNull(snapshotInfo); } public void testSnapshotCreateAsync() throws InterruptedException { @@ -460,7 +466,7 @@ public void onFailure(Exception exception) { listener = new LatchedActionListener<>(listener, latch); // tag::create-snapshot-execute-async - client.snapshot().createSnapshotAsync(request, RequestOptions.DEFAULT, listener); // <1> + client.snapshot().createAsync(request, RequestOptions.DEFAULT, listener); // <1> // end::create-snapshot-execute-async assertTrue(latch.await(30L, TimeUnit.SECONDS)); diff --git a/docs/java-rest/high-level/snapshot/create_snapshot.asciidoc b/docs/java-rest/high-level/snapshot/create_snapshot.asciidoc index dbd31380a9b4b..971a6ee486711 100644 --- a/docs/java-rest/high-level/snapshot/create_snapshot.asciidoc +++ b/docs/java-rest/high-level/snapshot/create_snapshot.asciidoc @@ -73,11 +73,22 @@ include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-r [[java-rest-high-snapshot-create-snapshot-sync]] ==== Synchronous Execution +Execute a `CreateSnapshotRequest` synchronously to receive a `CreateSnapshotResponse`. + ["source","java",subs="attributes,callouts,macros"] -------------------------------------------------- include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-execute] -------------------------------------------------- +Retrieve the `SnapshotInfo` from a `CreateSnapshotResponse` when the snapshot is fully created. +(The `waitForCompletion` parameter is `true`). + +["source","java",subs="attributes,callouts,macros"] +-------------------------------------------------- +include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-response-snapshot-info] +-------------------------------------------------- +<1> The `SnapshotInfo` object. + [[java-rest-high-snapshot-create-snapshot-async]] ==== Asynchronous Execution diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java index 2ff01ab01ed1f..15fbac35bffd2 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequest.java @@ -42,9 +42,9 @@ import static org.elasticsearch.action.ValidateActions.addValidationError; import static org.elasticsearch.common.Strings.EMPTY_ARRAY; +import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS; import static org.elasticsearch.common.settings.Settings.readSettingsFromStream; import static org.elasticsearch.common.settings.Settings.writeSettingsToStream; -import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS; import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue; /** @@ -433,8 +433,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (indicesOptions != null) { indicesOptions.toXContent(builder, params); } - builder.field("wait_for_completion", waitForCompletion); - builder.field("master_node_timeout", masterNodeTimeout.toString()); builder.endObject(); return builder; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponse.java index a2dc02c5c8299..d0a82e36a97da 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponse.java @@ -21,14 +21,16 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.snapshots.SnapshotInfo; +import org.elasticsearch.snapshots.SnapshotInfo.SnapshotInfoBuilder; import java.io.IOException; import java.util.Objects; @@ -38,6 +40,14 @@ */ public class CreateSnapshotResponse extends ActionResponse implements ToXContentObject { + private static final ObjectParser PARSER = + new ObjectParser<>(CreateSnapshotResponse.class.getName(), true, CreateSnapshotResponse::new); + + static { + PARSER.declareObject(CreateSnapshotResponse::setSnapshotInfoFromBuilder, + SnapshotInfo.SNAPSHOT_INFO_PARSER, new ParseField("snapshot")); + } + @Nullable private SnapshotInfo snapshotInfo; @@ -48,8 +58,8 @@ public class CreateSnapshotResponse extends ActionResponse implements ToXContent CreateSnapshotResponse() { } - void setSnapshotInfo(SnapshotInfo snapshotInfo) { - this.snapshotInfo = snapshotInfo; + private void setSnapshotInfoFromBuilder(SnapshotInfoBuilder snapshotInfoBuilder) { + this.snapshotInfo = snapshotInfoBuilder.build(); } /** @@ -101,38 +111,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder; } - public static CreateSnapshotResponse fromXContent(XContentParser parser) throws IOException { - CreateSnapshotResponse createSnapshotResponse = new CreateSnapshotResponse(); - - parser.nextToken(); // move to '{' - - if (parser.currentToken() != Token.START_OBJECT) { - throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "], expected ['{']"); - } - - parser.nextToken(); // move to 'snapshot' || 'accepted' - - if ("snapshot".equals(parser.currentName())) { - createSnapshotResponse.snapshotInfo = SnapshotInfo.fromXContent(parser); - } else if ("accepted".equals(parser.currentName())) { - parser.nextToken(); // move to 'accepted' field value - - if (parser.booleanValue()) { - // ensure accepted is a boolean value - } - - parser.nextToken(); // move past 'true'/'false' - } else { - throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "] expected ['snapshot', 'accepted']"); - } - - if (parser.currentToken() != Token.END_OBJECT) { - throw new IllegalArgumentException("unexpected token [" + parser.currentToken() + "], expected ['}']"); - } - - parser.nextToken(); // move past '}' - - return createSnapshotResponse; + public static CreateSnapshotResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); } @Override diff --git a/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java b/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java index 19572a6c212a2..f2cf0b5444d52 100644 --- a/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java +++ b/server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java @@ -22,6 +22,7 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestRequest; @@ -316,21 +317,6 @@ public static IndicesOptions fromMap(Map map, IndicesOptions def defaultSettings); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startArray("expand_wildcards"); - for (WildcardStates expandWildcard : expandWildcards) { - builder.value(expandWildcard.toString().toLowerCase(Locale.ROOT)); - } - builder.endArray(); - builder.field("ignore_unavailable", ignoreUnavailable()); - builder.field("allow_no_indices", allowNoIndices()); - builder.field("forbid_aliases_to_multiple_indices", allowAliasesToMultipleIndices() == false); - builder.field("forbid_closed_indices", forbidClosedIndices()); - builder.field("ignore_aliases", ignoreAliases()); - return builder; - } - /** * Returns true if the name represents a valid name for one of the indices option * false otherwise @@ -360,6 +346,18 @@ public static IndicesOptions fromParameters(Object wildcardsString, Object ignor ); } + @Override + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startArray("expand_wildcards"); + for (WildcardStates expandWildcard : expandWildcards) { + builder.value(expandWildcard.toString().toLowerCase(Locale.ROOT)); + } + builder.endArray(); + builder.field("ignore_unavailable", ignoreUnavailable()); + builder.field("allow_no_indices", allowNoIndices()); + return builder; + } + /** * @return indices options that requires every specified index to exist, expands wildcards only to open indices and * allows that no indices are resolved from wildcard expressions (not returning an error). diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java index cf2f66a750cd7..67ddabc37fa30 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java @@ -140,22 +140,6 @@ private void setShardFailures(List shardFailures) { this.shardFailures = shardFailures; } - private void ignoreVersion(String version) { - // ignore extra field - } - - private void ignoreStartTime(String startTime) { - // ignore extra field - } - - private void ignoreEndTime(String endTime) { - // ignore extra field - } - - private void ignoreDurationInMillis(long durationInMillis) { - // ignore extra field - } - public SnapshotInfo build() { SnapshotId snapshotId = new SnapshotId(snapshotName, snapshotUUID); @@ -197,10 +181,6 @@ private void setSuccessfulShards(int successfulShards) { int getSuccessfulShards() { return successfulShards; } - - private void ignoreFailedShards(int failedShards) { - // ignore extra field - } } public static final ObjectParser SNAPSHOT_INFO_PARSER = @@ -222,14 +202,9 @@ private void ignoreFailedShards(int failedShards) { SNAPSHOT_INFO_PARSER.declareInt(SnapshotInfoBuilder::setVersion, new ParseField(VERSION_ID)); SNAPSHOT_INFO_PARSER.declareObjectArray(SnapshotInfoBuilder::setShardFailures, SnapshotShardFailure.SNAPSHOT_SHARD_FAILURE_PARSER, new ParseField(FAILURES)); - SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::ignoreVersion, new ParseField(VERSION)); - SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::ignoreStartTime, new ParseField(START_TIME)); - SNAPSHOT_INFO_PARSER.declareString(SnapshotInfoBuilder::ignoreEndTime, new ParseField(END_TIME)); - SNAPSHOT_INFO_PARSER.declareLong(SnapshotInfoBuilder::ignoreDurationInMillis, new ParseField(DURATION_IN_MILLIS)); SHARD_STATS_PARSER.declareInt(ShardStatsBuilder::setTotalShards, new ParseField(TOTAL)); SHARD_STATS_PARSER.declareInt(ShardStatsBuilder::setSuccessfulShards, new ParseField(SUCCESSFUL)); - SHARD_STATS_PARSER.declareInt(ShardStatsBuilder::ignoreFailedShards, new ParseField(FAILED)); } private final SnapshotId snapshotId; diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequestTests.java index 1bde8ab572b72..0b598be6849cb 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequestTests.java @@ -102,8 +102,8 @@ public void testToXContent() throws IOException { NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput()); Map map = parser.mapOrdered(); CreateSnapshotRequest processed = new CreateSnapshotRequest((String)map.get("repository"), (String)map.get("snapshot")); - processed.waitForCompletion((boolean)map.getOrDefault("wait_for_completion", false)); - processed.masterNodeTimeout((String)map.getOrDefault("master_node_timeout", "30s")); + processed.waitForCompletion(original.waitForCompletion()); + processed.masterNodeTimeout(original.masterNodeTimeout()); processed.source(map); assertEquals(original, processed); diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponseTests.java index bbfc9755bf215..bbb11fc6feef0 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponseTests.java @@ -40,7 +40,7 @@ protected CreateSnapshotResponse doParseInstance(XContentParser parser) throws I @Override protected boolean supportsUnknownFields() { - return false; + return true; } @Override @@ -63,9 +63,7 @@ protected CreateSnapshotResponse createTestInstance() { boolean globalState = randomBoolean(); - CreateSnapshotResponse response = new CreateSnapshotResponse(); - response.setSnapshotInfo( + return new CreateSnapshotResponse( new SnapshotInfo(snapshotId, indices, startTime, reason, endTime, totalShards, shardFailures, globalState)); - return response; } } diff --git a/server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java b/server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java index 8e94764cc7acc..f564066b4e3df 100644 --- a/server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java @@ -320,8 +320,5 @@ public void testToXContent() throws IOException { } assertEquals(map.get("ignore_unavailable"), options.contains(Option.IGNORE_UNAVAILABLE)); assertEquals(map.get("allow_no_indices"), options.contains(Option.ALLOW_NO_INDICES)); - assertEquals(map.get("forbid_aliases_to_multiple_indices"), options.contains(Option.FORBID_ALIASES_TO_MULTIPLE_INDICES)); - assertEquals(map.get("forbid_closed_indices"), options.contains(Option.FORBID_CLOSED_INDICES)); - assertEquals(map.get("ignore_aliases"), options.contains(Option.IGNORE_ALIASES)); } }