From 8242fdc0e8b9c2aae186bfdc1a4cdd752225bf36 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Wed, 15 Aug 2018 16:16:46 -0700 Subject: [PATCH 1/4] migrate more actions to protocol.xpack this PR creates client-side instances of the following actions: - DeleteAction - ForceMergeAction - ReadOnlyAction - RolloverAction - ForceMergeAction ShrinkAction and AllocateAction are done separately --- .../xpack/core/XPackClientPlugin.java | 20 ++- .../xpack/indexlifecycle/DeleteAction.java | 69 ++++++++++ .../indexlifecycle/ForceMergeAction.java | 92 ++++++++++++++ .../xpack/indexlifecycle/ReadOnlyAction.java | 68 ++++++++++ .../xpack/indexlifecycle/RolloverAction.java | 118 ++++++++++++++++++ .../indexlifecycle/DeleteActionTests.java | 40 ++++++ .../indexlifecycle/ForceMergeActionTests.java | 62 +++++++++ .../indexlifecycle/ReadOnlyActionTests.java | 40 ++++++ .../indexlifecycle/RolloverActionTests.java | 55 ++++++++ 9 files changed, 562 insertions(+), 2 deletions(-) create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteActionTests.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeActionTests.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyActionTests.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverActionTests.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index dc09766563306..01705f8ad4d3d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -452,8 +452,24 @@ public List getNamedXContent() { new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ReadOnlyAction.NAME), ReadOnlyAction::parse), new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(RolloverAction.NAME), RolloverAction::parse), new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), - new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse) - ); + new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), + // ILM - Client - Lifecycle Actions + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction::parse), + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction::parse), + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction::parse) + ); } @Override diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java new file mode 100644 index 0000000000000..965519c0ede80 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java @@ -0,0 +1,69 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +public class DeleteAction implements ToXContentObject { + public static final String NAME = "delete"; + + private static final ObjectParser PARSER = new ObjectParser<>(NAME, DeleteAction::new); + + public static DeleteAction parse(XContentParser parser) { + return PARSER.apply(parser, null); + } + + public DeleteAction() { + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { + builder.startObject(); + builder.endObject(); + return builder; + } + + @Override + public int hashCode() { + return 1; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + return true; + } + + @Override + public String toString() { + return Strings.toString(this); + } +} diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java new file mode 100644 index 0000000000000..046dcc955d429 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java @@ -0,0 +1,92 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Objects; + +public class ForceMergeAction implements ToXContentObject { + public static final String NAME = "forcemerge"; + private static final ParseField MAX_NUM_SEGMENTS_FIELD = new ParseField("max_num_segments"); + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, + false, a -> { + int maxNumSegments = (int) a[0]; + return new ForceMergeAction(maxNumSegments); + }); + + static { + PARSER.declareInt(ConstructingObjectParser.constructorArg(), MAX_NUM_SEGMENTS_FIELD); + } + + private final int maxNumSegments; + + public static ForceMergeAction parse(XContentParser parser) { + return PARSER.apply(parser, null); + } + + public ForceMergeAction(int maxNumSegments) { + if (maxNumSegments <= 0) { + throw new IllegalArgumentException("[" + MAX_NUM_SEGMENTS_FIELD.getPreferredName() + + "] must be a positive integer"); + } + this.maxNumSegments = maxNumSegments; + } + + public int getMaxNumSegments() { + return maxNumSegments; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field(MAX_NUM_SEGMENTS_FIELD.getPreferredName(), maxNumSegments); + builder.endObject(); + return builder; + } + + @Override + public int hashCode() { + return Objects.hash(maxNumSegments); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + ForceMergeAction other = (ForceMergeAction) obj; + return Objects.equals(maxNumSegments, other.maxNumSegments); + } + + @Override + public String toString() { + return Strings.toString(this); + } +} diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java new file mode 100644 index 0000000000000..86e86a172f8b6 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java @@ -0,0 +1,68 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.Strings; +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 java.io.IOException; + +public class ReadOnlyAction implements ToXContentObject { + public static final String NAME = "readonly"; + + private static final ObjectParser PARSER = new ObjectParser<>(NAME, false, ReadOnlyAction::new); + + public static ReadOnlyAction parse(XContentParser parser) { + return PARSER.apply(parser, null); + } + + public ReadOnlyAction() { + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.endObject(); + return builder; + } + + @Override + public int hashCode() { + return ReadOnlyAction.class.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + return true; + } + + @Override + public String toString() { + return Strings.toString(this); + } +} diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java new file mode 100644 index 0000000000000..c1b067c5748b8 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java @@ -0,0 +1,118 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ObjectParser.ValueType; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Objects; + + +public class RolloverAction implements ToXContentObject { + public static final String NAME = "rollover"; + private static final ParseField MAX_SIZE_FIELD = new ParseField("max_size"); + private static final ParseField MAX_DOCS_FIELD = new ParseField("max_docs"); + private static final ParseField MAX_AGE_FIELD = new ParseField("max_age"); + + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(NAME, + a -> new RolloverAction((ByteSizeValue) a[0], (TimeValue) a[1], (Long) a[2])); + static { + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MAX_SIZE_FIELD.getPreferredName()), MAX_SIZE_FIELD, ValueType.VALUE); + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), + (p, c) -> TimeValue.parseTimeValue(p.text(), MAX_AGE_FIELD.getPreferredName()), MAX_AGE_FIELD, ValueType.VALUE); + PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), MAX_DOCS_FIELD); + } + + private final ByteSizeValue maxSize; + private final Long maxDocs; + private final TimeValue maxAge; + + public static RolloverAction parse(XContentParser parser) { + return PARSER.apply(parser, null); + } + + public RolloverAction(ByteSizeValue maxSize, TimeValue maxAge, Long maxDocs) { + if (maxSize == null && maxAge == null && maxDocs == null) { + throw new IllegalArgumentException("At least one rollover condition must be set."); + } + this.maxSize = maxSize; + this.maxAge = maxAge; + this.maxDocs = maxDocs; + } + public ByteSizeValue getMaxSize() { + return maxSize; + } + + public TimeValue getMaxAge() { + return maxAge; + } + + public Long getMaxDocs() { + return maxDocs; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + if (maxSize != null) { + builder.field(MAX_SIZE_FIELD.getPreferredName(), maxSize.getStringRep()); + } + if (maxAge != null) { + builder.field(MAX_AGE_FIELD.getPreferredName(), maxAge.getStringRep()); + } + if (maxDocs != null) { + builder.field(MAX_DOCS_FIELD.getPreferredName(), maxDocs); + } + builder.endObject(); + return builder; + } + + @Override + public int hashCode() { + return Objects.hash(maxSize, maxAge, maxDocs); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj.getClass() != getClass()) { + return false; + } + RolloverAction other = (RolloverAction) obj; + return Objects.equals(maxSize, other.maxSize) && + Objects.equals(maxAge, other.maxAge) && + Objects.equals(maxDocs, other.maxDocs); + } + + @Override + public String toString() { + return Strings.toString(this); + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteActionTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteActionTests.java new file mode 100644 index 0000000000000..64d9c73dc0acd --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteActionTests.java @@ -0,0 +1,40 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +public class DeleteActionTests extends AbstractXContentTestCase { + + @Override + protected DeleteAction createTestInstance() { + return new DeleteAction(); + } + + @Override + protected DeleteAction doParseInstance(XContentParser parser) { + return DeleteAction.parse(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeActionTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeActionTests.java new file mode 100644 index 0000000000000..e1ffdf6b49d79 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeActionTests.java @@ -0,0 +1,62 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.DeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +import static org.hamcrest.Matchers.equalTo; + +public class ForceMergeActionTests extends AbstractXContentTestCase { + + @Override + protected ForceMergeAction doParseInstance(XContentParser parser) { + return ForceMergeAction.parse(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } + + @Override + protected ForceMergeAction createTestInstance() { + return new ForceMergeAction(randomIntBetween(1, 100)); + } + + public void testMissingMaxNumSegments() throws IOException { + BytesReference emptyObject = BytesReference.bytes(JsonXContent.contentBuilder().startObject().endObject()); + XContentParser parser = XContentHelper.createParser(null, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + emptyObject, XContentType.JSON); + Exception e = expectThrows(IllegalArgumentException.class, () -> ForceMergeAction.parse(parser)); + assertThat(e.getMessage(), equalTo("Required [max_num_segments]")); + } + + public void testInvalidNegativeSegmentNumber() { + Exception r = expectThrows(IllegalArgumentException.class, () -> new ForceMergeAction(randomIntBetween(-10, 0))); + assertThat(r.getMessage(), equalTo("[max_num_segments] must be a positive integer")); + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyActionTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyActionTests.java new file mode 100644 index 0000000000000..96ab6fa5ec426 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyActionTests.java @@ -0,0 +1,40 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +public class ReadOnlyActionTests extends AbstractXContentTestCase { + + @Override + protected ReadOnlyAction doParseInstance(XContentParser parser) { + return ReadOnlyAction.parse(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } + + @Override + protected ReadOnlyAction createTestInstance() { + return new ReadOnlyAction(); + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverActionTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverActionTests.java new file mode 100644 index 0000000000000..9e644e5fec8b5 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverActionTests.java @@ -0,0 +1,55 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.unit.ByteSizeUnit; +import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +public class RolloverActionTests extends AbstractXContentTestCase { + + @Override + protected RolloverAction doParseInstance(XContentParser parser) { + return RolloverAction.parse(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } + + @Override + protected RolloverAction createTestInstance() { + ByteSizeUnit maxSizeUnit = randomFrom(ByteSizeUnit.values()); + ByteSizeValue maxSize = randomBoolean() ? null : new ByteSizeValue(randomNonNegativeLong() / maxSizeUnit.toBytes(1), maxSizeUnit); + Long maxDocs = randomBoolean() ? null : randomNonNegativeLong(); + TimeValue maxAge = (maxDocs == null && maxSize == null || randomBoolean()) + ? TimeValue.parseTimeValue(randomPositiveTimeValue(), "rollover_action_test") + : null; + return new RolloverAction(maxSize, maxAge, maxDocs); + } + + public void testNoConditions() { + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, + () -> new RolloverAction(null, null, null)); + assertEquals("At least one rollover condition must be set.", exception.getMessage()); + } +} From 7983c5d03c78598e98003223b68d9d388c6274b1 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Wed, 15 Aug 2018 16:36:59 -0700 Subject: [PATCH 2/4] add shrink action --- .../xpack/core/XPackClientPlugin.java | 5 +- .../xpack/indexlifecycle/ShrinkAction.java | 84 +++++++++++++++++++ .../indexlifecycle/ShrinkActionTests.java | 49 +++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkActionTests.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index 01705f8ad4d3d..c103c069944cf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -468,7 +468,10 @@ public List getNamedXContent() { org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.NAME), - org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction::parse) + org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction::parse), + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction.class, + new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction.NAME), + org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction::parse) ); } diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java new file mode 100644 index 0000000000000..d2fe621fefb84 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java @@ -0,0 +1,84 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; +import java.util.Objects; + +public class ShrinkAction implements ToXContentObject { + public static final String NAME = "shrink"; + private static final ParseField NUMBER_OF_SHARDS_FIELD = new ParseField("number_of_shards"); + + private static final ConstructingObjectParser PARSER = + new ConstructingObjectParser<>(NAME, a -> new ShrinkAction((Integer) a[0])); + + static { + PARSER.declareInt(ConstructingObjectParser.constructorArg(), NUMBER_OF_SHARDS_FIELD); + } + + private int numberOfShards; + + public static ShrinkAction parse(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } + + public ShrinkAction(int numberOfShards) { + if (numberOfShards <= 0) { + throw new IllegalArgumentException("[" + NUMBER_OF_SHARDS_FIELD.getPreferredName() + "] must be greater than 0"); + } + this.numberOfShards = numberOfShards; + } + + int getNumberOfShards() { + return numberOfShards; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + builder.field(NUMBER_OF_SHARDS_FIELD.getPreferredName(), numberOfShards); + builder.endObject(); + return builder; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ShrinkAction that = (ShrinkAction) o; + return Objects.equals(numberOfShards, that.numberOfShards); + } + + @Override + public int hashCode() { + return Objects.hash(numberOfShards); + } + + @Override + public String toString() { + return Strings.toString(this); + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkActionTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkActionTests.java new file mode 100644 index 0000000000000..1472d60a3362e --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkActionTests.java @@ -0,0 +1,49 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +import static org.hamcrest.Matchers.equalTo; + +public class ShrinkActionTests extends AbstractXContentTestCase { + + @Override + protected ShrinkAction doParseInstance(XContentParser parser) throws IOException { + return ShrinkAction.parse(parser); + } + + @Override + protected ShrinkAction createTestInstance() { + return new ShrinkAction(randomIntBetween(1, 100)); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } + + public void testNonPositiveShardNumber() { + Exception e = expectThrows(Exception.class, () -> new ShrinkAction(randomIntBetween(-100, 0))); + assertThat(e.getMessage(), equalTo("[number_of_shards] must be greater than 0")); + } +} From 7c5705b577b50118e7b7ed3d6c00c213fdf49f08 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Wed, 15 Aug 2018 19:36:14 -0700 Subject: [PATCH 3/4] remove duplicate xcontent registration of readonly --- .../java/org/elasticsearch/xpack/core/XPackClientPlugin.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index c103c069944cf..10a7625011563 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -463,9 +463,6 @@ public List getNamedXContent() { new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.class, - new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.NAME), - org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction::parse), From d030431a15dbb8c99dfc6afb28a9d251018e254d Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 16 Aug 2018 06:36:03 -0700 Subject: [PATCH 4/4] add shared LifecycleAction --- .../xpack/core/XPackClientPlugin.java | 10 +++---- .../xpack/indexlifecycle/DeleteAction.java | 2 +- .../indexlifecycle/ForceMergeAction.java | 2 +- .../xpack/indexlifecycle/LifecycleAction.java | 27 +++++++++++++++++++ .../xpack/indexlifecycle/ReadOnlyAction.java | 2 +- .../xpack/indexlifecycle/RolloverAction.java | 2 +- .../xpack/indexlifecycle/ShrinkAction.java | 2 +- 7 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecycleAction.java diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java index 10a7625011563..333ce6d5c21b9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackClientPlugin.java @@ -454,19 +454,19 @@ public List getNamedXContent() { new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(ShrinkAction.NAME), ShrinkAction::parse), new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse), // ILM - Client - Lifecycle Actions - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction.class, + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.DeleteAction::parse), - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction.class, + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.ForceMergeAction::parse), - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.class, + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.ReadOnlyAction::parse), - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.class, + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.RolloverAction::parse), - new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction.class, + new NamedXContentRegistry.Entry(org.elasticsearch.protocol.xpack.indexlifecycle.LifecycleAction.class, new ParseField(org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction.NAME), org.elasticsearch.protocol.xpack.indexlifecycle.ShrinkAction::parse) ); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java index 965519c0ede80..e2978655c08cf 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/DeleteAction.java @@ -27,7 +27,7 @@ import java.io.IOException; -public class DeleteAction implements ToXContentObject { +public class DeleteAction extends LifecycleAction implements ToXContentObject { public static final String NAME = "delete"; private static final ObjectParser PARSER = new ObjectParser<>(NAME, DeleteAction::new); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java index 046dcc955d429..a0ef59c10e96c 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ForceMergeAction.java @@ -28,7 +28,7 @@ import java.io.IOException; import java.util.Objects; -public class ForceMergeAction implements ToXContentObject { +public class ForceMergeAction extends LifecycleAction implements ToXContentObject { public static final String NAME = "forcemerge"; private static final ParseField MAX_NUM_SEGMENTS_FIELD = new ParseField("max_num_segments"); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecycleAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecycleAction.java new file mode 100644 index 0000000000000..8a9761cf7e206 --- /dev/null +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/LifecycleAction.java @@ -0,0 +1,27 @@ +/* + * 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.protocol.xpack.indexlifecycle; + +import org.elasticsearch.common.xcontent.NamedXContentRegistry; + +/** + * Parent class for the client-side ilm actions for the {@link NamedXContentRegistry} + */ +public class LifecycleAction { +} diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java index 86e86a172f8b6..cb1bd1bc5d8b6 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ReadOnlyAction.java @@ -26,7 +26,7 @@ import java.io.IOException; -public class ReadOnlyAction implements ToXContentObject { +public class ReadOnlyAction extends LifecycleAction implements ToXContentObject { public static final String NAME = "readonly"; private static final ObjectParser PARSER = new ObjectParser<>(NAME, false, ReadOnlyAction::new); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java index c1b067c5748b8..9d5c4baf2d471 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/RolloverAction.java @@ -32,7 +32,7 @@ import java.util.Objects; -public class RolloverAction implements ToXContentObject { +public class RolloverAction extends LifecycleAction implements ToXContentObject { public static final String NAME = "rollover"; private static final ParseField MAX_SIZE_FIELD = new ParseField("max_size"); private static final ParseField MAX_DOCS_FIELD = new ParseField("max_docs"); diff --git a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java index d2fe621fefb84..a2f8e0e86fcfc 100644 --- a/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java +++ b/x-pack/protocol/src/main/java/org/elasticsearch/protocol/xpack/indexlifecycle/ShrinkAction.java @@ -28,7 +28,7 @@ import java.io.IOException; import java.util.Objects; -public class ShrinkAction implements ToXContentObject { +public class ShrinkAction extends LifecycleAction implements ToXContentObject { public static final String NAME = "shrink"; private static final ParseField NUMBER_OF_SHARDS_FIELD = new ParseField("number_of_shards");