From 999d66fc87d82a95fc962edcbea0df89ab36241b Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Wed, 13 Nov 2019 10:23:53 +0000 Subject: [PATCH] Add client-side ResizeRequest and ResizeResponse classes (#48937) Closes #48468 --- .../elasticsearch/client/IndicesClient.java | 106 ++++++++++- .../client/IndicesRequestConverters.java | 43 ++++- .../client/indices/ResizeRequest.java | 166 ++++++++++++++++++ .../client/indices/ResizeResponse.java | 76 ++++++++ .../client/IndicesRequestConvertersTests.java | 49 ++---- .../client/indices/ResizeRequestTests.java | 57 ++++++ .../client/indices/ResizeResponseTests.java | 48 +++++ 7 files changed, 503 insertions(+), 42 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeRequest.java create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java index fd179ef3555..8c0c5e72a28 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java @@ -37,8 +37,6 @@ import org.elasticsearch.action.admin.indices.refresh.RefreshResponse; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; -import org.elasticsearch.action.admin.indices.shrink.ResizeResponse; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse; @@ -65,6 +63,8 @@ import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; import org.elasticsearch.client.indices.ReloadAnalyzersResponse; +import org.elasticsearch.client.indices.ResizeRequest; +import org.elasticsearch.client.indices.ResizeResponse; import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.rollover.RolloverRequest; import org.elasticsearch.client.indices.rollover.RolloverResponse; @@ -907,6 +907,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, emptySet()); } + /** + * Shrinks an index using the Shrink Index API. + * See + * Shrink Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + * @deprecated use {@link #shrink(ResizeRequest, RequestOptions)} + */ + @Deprecated + public org.elasticsearch.action.admin.indices.shrink.ResizeResponse shrink( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + } + /** * Asynchronously shrinks an index using the Shrink index API. * See @@ -921,6 +938,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, listener, emptySet()); } + /** + * Asynchronously shrinks an index using the Shrink index API. + * See + * Shrink Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + * @deprecated use {@link #shrinkAsync(ResizeRequest, RequestOptions, ActionListener)} + */ + @Deprecated + public Cancellable shrinkAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::shrink, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + } + /** * Splits an index using the Split Index API. * See @@ -935,6 +969,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, emptySet()); } + /** + * Splits an index using the Split Index API. + * See + * Split Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + * @deprecated use {@link #split(ResizeRequest, RequestOptions)} + */ + @Deprecated + public org.elasticsearch.action.admin.indices.shrink.ResizeResponse split( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + } + /** * Asynchronously splits an index using the Split Index API. * See @@ -949,6 +1000,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, listener, emptySet()); } + /** + * Asynchronously splits an index using the Split Index API. + * See + * Split Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + * @deprecated use {@link #splitAsync(ResizeRequest, RequestOptions, ActionListener)} + */ + @Deprecated + public Cancellable splitAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::split, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + } + /** * Clones an index using the Clone Index API. * See @@ -963,6 +1031,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, emptySet()); } + /** + * Clones an index using the Clone Index API. + * See + * Clone Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + * @deprecated use {@link #clone(ResizeRequest, RequestOptions)} + */ + @Deprecated + public org.elasticsearch.action.admin.indices.shrink.ResizeResponse clone( + org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, emptySet()); + } + /** * Asynchronously clones an index using the Clone Index API. * See @@ -977,6 +1062,23 @@ public final class IndicesClient { ResizeResponse::fromXContent, listener, emptySet()); } + /** + * Asynchronously clones an index using the Clone Index API. + * See + * Clone Index API on elastic.co + * @param resizeRequest the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + * @return cancellable that may be used to cancel the request + * @deprecated use {@link #cloneAsync(ResizeRequest, RequestOptions, ActionListener)} + */ + @Deprecated + public Cancellable cloneAsync(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest, RequestOptions options, + ActionListener listener) { + return restHighLevelClient.performRequestAsyncAndParseEntity(resizeRequest, IndicesRequestConverters::clone, options, + org.elasticsearch.action.admin.indices.shrink.ResizeResponse::fromXContent, listener, emptySet()); + } + /** * Rolls over an index using the Rollover Index API. * See diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java index aba961d9f90..55bd4b98780 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java @@ -35,7 +35,6 @@ import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; @@ -52,8 +51,10 @@ import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; +import org.elasticsearch.client.indices.ResizeRequest; import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.rollover.RolloverRequest; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; import java.io.IOException; @@ -325,6 +326,14 @@ final class IndicesRequestConverters { } static Request split(ResizeRequest resizeRequest) throws IOException { + if (IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.exists(resizeRequest.getSettings()) == false) { + throw new IllegalArgumentException("index.number_of_shards is required for split operations"); + } + return resize(resizeRequest, ResizeType.SPLIT); + } + + @Deprecated + static Request split(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest) throws IOException { if (resizeRequest.getResizeType() != ResizeType.SPLIT) { throw new IllegalArgumentException("Wrong resize type [" + resizeRequest.getResizeType() + "] for indices split request"); } @@ -332,6 +341,11 @@ final class IndicesRequestConverters { } static Request shrink(ResizeRequest resizeRequest) throws IOException { + return resize(resizeRequest, ResizeType.SHRINK); + } + + @Deprecated + static Request shrink(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest) throws IOException { if (resizeRequest.getResizeType() != ResizeType.SHRINK) { throw new IllegalArgumentException("Wrong resize type [" + resizeRequest.getResizeType() + "] for indices shrink request"); } @@ -339,16 +353,37 @@ final class IndicesRequestConverters { } static Request clone(ResizeRequest resizeRequest) throws IOException { + return resize(resizeRequest, ResizeType.CLONE); + } + + @Deprecated + static Request clone(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest) throws IOException { if (resizeRequest.getResizeType() != ResizeType.CLONE) { throw new IllegalArgumentException("Wrong resize type [" + resizeRequest.getResizeType() + "] for indices clone request"); } return resize(resizeRequest); } - private static Request resize(ResizeRequest resizeRequest) throws IOException { + private static Request resize(ResizeRequest resizeRequest, ResizeType type) throws IOException { String endpoint = new RequestConverters.EndpointBuilder().addPathPart(resizeRequest.getSourceIndex()) - .addPathPartAsIs("_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT)) - .addPathPart(resizeRequest.getTargetIndexRequest().index()).build(); + .addPathPartAsIs("_" + type.name().toLowerCase(Locale.ROOT)) + .addPathPart(resizeRequest.getTargetIndex()).build(); + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + + RequestConverters.Params params = new RequestConverters.Params(); + params.withTimeout(resizeRequest.timeout()); + params.withMasterTimeout(resizeRequest.masterNodeTimeout()); + params.withWaitForActiveShards(resizeRequest.getWaitForActiveShards()); + request.addParameters(params.asMap()); + request.setEntity(RequestConverters.createEntity(resizeRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE)); + return request; + } + + @Deprecated + private static Request resize(org.elasticsearch.action.admin.indices.shrink.ResizeRequest resizeRequest) throws IOException { + String endpoint = new RequestConverters.EndpointBuilder().addPathPart(resizeRequest.getSourceIndex()) + .addPathPartAsIs("_" + resizeRequest.getResizeType().name().toLowerCase(Locale.ROOT)) + .addPathPart(resizeRequest.getTargetIndexRequest().index()).build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params params = new RequestConverters.Params(); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeRequest.java new file mode 100644 index 00000000000..16457ea68e0 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeRequest.java @@ -0,0 +1,166 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.client.indices; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.TimedRequest; +import org.elasticsearch.client.Validatable; +import org.elasticsearch.client.ValidationException; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * Request class to resize an index + */ +public class ResizeRequest extends TimedRequest implements Validatable, ToXContentObject { + + private ActiveShardCount waitForActiveShards; + private final String sourceIndex; + private final String targetIndex; + private Settings settings = Settings.EMPTY; + private Set aliases = new HashSet<>(); + + /** + * Creates a new resize request + * @param targetIndex the new index to create with resized shards + * @param sourceIndex the index to resize + */ + public ResizeRequest(String targetIndex, String sourceIndex) { + this.targetIndex = Objects.requireNonNull(targetIndex); + this.sourceIndex = Objects.requireNonNull(sourceIndex); + } + + /** + * Sets the Settings to be used on the target index + */ + public ResizeRequest setSettings(Settings settings) { + this.settings = settings; + return this; + } + + /** + * Returns the Settings to be used on the target index + */ + public Settings getSettings() { + return this.settings; + } + + /** + * Sets the Aliases to be used on the target index + */ + public ResizeRequest setAliases(List aliases) { + this.aliases.clear(); + this.aliases.addAll(aliases); + return this; + } + + /** + * Returns the Aliases to be used on the target index + */ + public Set getAliases() { + return Collections.unmodifiableSet(this.aliases); + } + + @Override + public Optional validate() { + ValidationException validationException = new ValidationException(); + if (settings.getByPrefix("index.sort.").isEmpty() == false) { + validationException.addValidationError("can't override index sort when resizing an index"); + } + return validationException.validationErrors().isEmpty() ? Optional.empty() : Optional.of(validationException); + } + + /** + * Returns the target index name + */ + public String getTargetIndex() { + return targetIndex; + } + + /** + * Returns the source index name + */ + public String getSourceIndex() { + return sourceIndex; + } + + /** + * Sets the number of shard copies that should be active for creation of the + * new shrunken index to return. Defaults to {@link ActiveShardCount#DEFAULT}, which will + * wait for one shard copy (the primary) to become active. Set this value to + * {@link ActiveShardCount#ALL} to wait for all shards (primary and all replicas) to be active + * before returning. Otherwise, use {@link ActiveShardCount#from(int)} to set this value to any + * non-negative integer, up to the number of copies per shard (number of replicas + 1), + * to wait for the desired amount of shard copies to become active before returning. + * Index creation will only wait up until the timeout value for the number of shard copies + * to be active before returning. Check {@link ResizeResponse#isShardsAcknowledged()} to + * determine if the requisite shard copies were all started before returning or timing out. + * + * @param waitForActiveShards number of active shard copies to wait on + */ + public ResizeRequest setWaitForActiveShards(ActiveShardCount waitForActiveShards) { + this.waitForActiveShards = waitForActiveShards; + return this; + } + + /** + * A shortcut for {@link #setWaitForActiveShards(ActiveShardCount)} where the numerical + * shard count is passed in, instead of having to first call {@link ActiveShardCount#from(int)} + * to get the ActiveShardCount. + */ + public ResizeRequest setWaitForActiveShards(final int waitForActiveShards) { + return setWaitForActiveShards(ActiveShardCount.from(waitForActiveShards)); + } + + public ActiveShardCount getWaitForActiveShards() { + return waitForActiveShards; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + { + builder.startObject(CreateIndexRequest.SETTINGS.getPreferredName()); + { + settings.toXContent(builder, params); + } + builder.endObject(); + builder.startObject(CreateIndexRequest.ALIASES.getPreferredName()); + { + for (Alias alias : aliases) { + alias.toXContent(builder, params); + } + } + builder.endObject(); + } + builder.endObject(); + return builder; + } + +} diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java new file mode 100644 index 00000000000..f46ebbed0c6 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/ResizeResponse.java @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.indices; + +import org.elasticsearch.client.core.AcknowledgedResponse; +import org.elasticsearch.client.core.ShardsAcknowledgedResponse; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.util.Objects; + +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; + +/** + * The response from a {@link ResizeRequest} call + */ +public class ResizeResponse extends ShardsAcknowledgedResponse { + + private static final ParseField INDEX = new ParseField("index"); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("resize_index", + true, args -> new ResizeResponse((boolean) args[0], (boolean) args[1], (String) args[2])); + + static { + PARSER.declareBoolean(constructorArg(), new ParseField(AcknowledgedResponse.PARSE_FIELD_NAME)); + PARSER.declareBoolean(constructorArg(), new ParseField(SHARDS_PARSE_FIELD_NAME)); + PARSER.declareField(constructorArg(), (parser, context) -> parser.textOrNull(), INDEX, ObjectParser.ValueType.STRING_OR_NULL); + } + + private final String index; + + public ResizeResponse(boolean acknowledged, boolean shardsAcknowledged, String index) { + super(acknowledged, shardsAcknowledged); + this.index = index; + } + + public String index() { + return index; + } + + public static ResizeResponse fromXContent(XContentParser parser) { + return PARSER.apply(parser, null); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + ResizeResponse that = (ResizeResponse) o; + return Objects.equals(index, that.index); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), index); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 971e1c357e1..1fdbf65c41e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -38,7 +38,6 @@ import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; -import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; import org.elasticsearch.action.admin.indices.shrink.ResizeType; import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; @@ -56,6 +55,7 @@ import org.elasticsearch.client.indices.PutIndexTemplateRequest; import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.RandomCreateIndexGenerator; import org.elasticsearch.client.indices.ReloadAnalyzersRequest; +import org.elasticsearch.client.indices.ResizeRequest; import org.elasticsearch.client.indices.rollover.RolloverRequest; import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.Strings; @@ -79,6 +79,7 @@ import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.elasticsearch.client.indices.RandomCreateIndexGenerator.randomAliases; import static org.elasticsearch.client.indices.RandomCreateIndexGenerator.randomMapping; +import static org.elasticsearch.index.RandomCreateIndexGenerator.randomAlias; import static org.elasticsearch.index.RandomCreateIndexGenerator.randomIndexSettings; import static org.elasticsearch.index.alias.RandomAliasActionsGenerator.randomAliasAction; import static org.elasticsearch.rest.BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER; @@ -829,37 +830,10 @@ public class IndicesRequestConvertersTests extends ESTestCase { resizeTest(ResizeType.SPLIT, IndicesRequestConverters::split); } - public void testSplitWrongResizeType() { - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); - ResizeType wrongType = randomFrom(ResizeType.SHRINK, ResizeType.CLONE); - resizeRequest.setResizeType(wrongType); - IllegalArgumentException iae = LuceneTestCase.expectThrows(IllegalArgumentException.class, () - -> IndicesRequestConverters.split(resizeRequest)); - Assert.assertEquals("Wrong resize type [" + wrongType.name() + "] for indices split request", iae.getMessage()); - } - public void testClone() throws IOException { resizeTest(ResizeType.CLONE, IndicesRequestConverters::clone); } - public void testCloneWrongResizeType() { - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); - ResizeType wrongType = randomFrom(ResizeType.SHRINK, ResizeType.SPLIT); - resizeRequest.setResizeType(wrongType); - IllegalArgumentException iae = LuceneTestCase.expectThrows(IllegalArgumentException.class, () - -> IndicesRequestConverters.clone(resizeRequest)); - Assert.assertEquals("Wrong resize type [" + wrongType.name() + "] for indices clone request", iae.getMessage()); - } - - public void testShrinkWrongResizeType() { - ResizeRequest resizeRequest = new ResizeRequest("target", "source"); - ResizeType wrongType = randomFrom(ResizeType.SPLIT, ResizeType.CLONE); - resizeRequest.setResizeType(wrongType); - IllegalArgumentException iae = LuceneTestCase.expectThrows(IllegalArgumentException.class, () - -> IndicesRequestConverters.shrink(resizeRequest)); - Assert.assertEquals("Wrong resize type [" + wrongType.name() + "] for indices shrink request", iae.getMessage()); - } - public void testShrink() throws IOException { resizeTest(ResizeType.SHRINK, IndicesRequestConverters::shrink); } @@ -868,28 +842,31 @@ public class IndicesRequestConvertersTests extends ESTestCase { throws IOException { String[] indices = RequestConvertersTests.randomIndicesNames(2, 2); ResizeRequest resizeRequest = new ResizeRequest(indices[0], indices[1]); - resizeRequest.setResizeType(resizeType); Map expectedParams = new HashMap<>(); RequestConvertersTests.setRandomMasterTimeout(resizeRequest, expectedParams); - RequestConvertersTests.setRandomTimeout(resizeRequest::timeout, resizeRequest.timeout(), expectedParams); + RequestConvertersTests.setRandomTimeout(s -> resizeRequest.setTimeout(TimeValue.parseTimeValue(s, "timeout")), + resizeRequest.timeout(), expectedParams); if (ESTestCase.randomBoolean()) { - org.elasticsearch.action.admin.indices.create.CreateIndexRequest createIndexRequest = - new org.elasticsearch.action.admin.indices.create.CreateIndexRequest(ESTestCase.randomAlphaOfLengthBetween(3, 10)); if (ESTestCase.randomBoolean()) { - createIndexRequest.settings(randomIndexSettings()); + resizeRequest.setSettings(randomIndexSettings()); } if (ESTestCase.randomBoolean()) { - org.elasticsearch.index.RandomCreateIndexGenerator.randomAliases(createIndexRequest); + int count = randomIntBetween(0, 2); + for (int i = 0; i < count; i++) { + resizeRequest.setAliases(singletonList(randomAlias())); + } } - resizeRequest.setTargetIndex(createIndexRequest); } RequestConvertersTests.setRandomWaitForActiveShards(resizeRequest::setWaitForActiveShards, expectedParams); + if (resizeType == ResizeType.SPLIT) { + resizeRequest.setSettings(Settings.builder().put("index.number_of_shards", 2).build()); + } Request request = function.apply(resizeRequest); Assert.assertEquals(HttpPut.METHOD_NAME, request.getMethod()); String expectedEndpoint = "/" + resizeRequest.getSourceIndex() + "/_" + resizeType.name().toLowerCase(Locale.ROOT) + "/" - + resizeRequest.getTargetIndexRequest().index(); + + resizeRequest.getTargetIndex(); Assert.assertEquals(expectedEndpoint, request.getEndpoint()); Assert.assertEquals(expectedParams, request.getParameters()); RequestConvertersTests.assertToXContentBody(resizeRequest, request.getEntity()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java new file mode 100644 index 00000000000..16ccc6923f4 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeRequestTests.java @@ -0,0 +1,57 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.indices; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.client.AbstractRequestTestCase; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Arrays; + +public class ResizeRequestTests extends AbstractRequestTestCase { + + @Override + protected ResizeRequest createClientTestInstance() { + return new ResizeRequest("target", "source") + .setAliases(Arrays.asList(new Alias("target1"), new Alias("target2"))) + .setSettings(Settings.builder().put("index.foo", "bar").build()); + } + + @Override + protected org.elasticsearch.action.admin.indices.shrink.ResizeRequest doParseToServerInstance(XContentParser parser) + throws IOException { + org.elasticsearch.action.admin.indices.shrink.ResizeRequest req + = new org.elasticsearch.action.admin.indices.shrink.ResizeRequest("target", "source"); + req.fromXContent(parser); + return req; + } + + @Override + protected void assertInstances(org.elasticsearch.action.admin.indices.shrink.ResizeRequest serverInstance, + ResizeRequest clientTestInstance) { + assertEquals(serverInstance.getSourceIndex(), clientTestInstance.getSourceIndex()); + assertEquals(serverInstance.getTargetIndexRequest().index(), clientTestInstance.getTargetIndex()); + assertEquals(serverInstance.getTargetIndexRequest().settings(), clientTestInstance.getSettings()); + assertEquals(serverInstance.getTargetIndexRequest().aliases(), clientTestInstance.getAliases()); + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java new file mode 100644 index 00000000000..87079c06a80 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/ResizeResponseTests.java @@ -0,0 +1,48 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.client.indices; + +import org.elasticsearch.client.AbstractResponseTestCase; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; + +import java.io.IOException; + +public class ResizeResponseTests extends + AbstractResponseTestCase { + + @Override + protected org.elasticsearch.action.admin.indices.shrink.ResizeResponse createServerTestInstance(XContentType xContentType) { + return new org.elasticsearch.action.admin.indices.shrink.ResizeResponse(randomBoolean(), randomBoolean(), randomAlphaOfLength(5)); + } + + @Override + protected ResizeResponse doParseToClientInstance(XContentParser parser) throws IOException { + return ResizeResponse.fromXContent(parser); + } + + @Override + protected void assertInstances(org.elasticsearch.action.admin.indices.shrink.ResizeResponse serverTestInstance, + ResizeResponse clientInstance) { + assertEquals(serverTestInstance.isAcknowledged(), clientInstance.isAcknowledged()); + assertEquals(serverTestInstance.isShardsAcknowledged(), clientInstance.isShardsAcknowledged()); + assertEquals(serverTestInstance.index(), clientInstance.index()); + } +}