From 1e329099f99d86dea0e3aaa08374cf8b9345f701 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Mon, 6 Jun 2016 18:50:45 -0400 Subject: [PATCH] enhance rollover request --- .../admin/indices/rollover/Condition.java | 14 ++++ .../indices/rollover/RolloverRequest.java | 12 +++ .../rollover/RolloverRequestBuilder.java | 5 ++ .../indices/rollover/RolloverResponse.java | 79 ++++++++++++++++++- .../rollover/TransportRolloverAction.java | 56 ++++++++----- .../indices/RestRolloverIndexAction.java | 21 +---- .../indices/rollover/ConditionTests.java | 41 ++++++++++ .../admin/indices/rollover/RolloverIT.java | 38 +++++++++ .../TransportRolloverActionTests.java | 73 ++++------------- 9 files changed, 244 insertions(+), 95 deletions(-) create mode 100644 core/src/test/java/org/elasticsearch/action/admin/indices/rollover/ConditionTests.java diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/Condition.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/Condition.java index bd70dda62fc..fee08433e9c 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/Condition.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/Condition.java @@ -44,10 +44,12 @@ public abstract class Condition implements NamedWriteable { public final static String NAME = "max_age"; public MaxAge(TimeValue value) { + super(NAME); this.value = value; } public MaxAge(StreamInput in) throws IOException { + super(NAME); this.value = TimeValue.timeValueMillis(in.readLong()); } @@ -71,10 +73,12 @@ public abstract class Condition implements NamedWriteable { public final static String NAME = "max_docs"; public MaxDocs(Long value) { + super(NAME); this.value = value; } public MaxDocs(StreamInput in) throws IOException { + super(NAME); this.value = in.readLong(); } @@ -95,6 +99,16 @@ public abstract class Condition implements NamedWriteable { } protected T value; + protected final String name; + + protected Condition(String name) { + this.name = name; + } public abstract boolean matches(T value); + + @Override + public final String toString() { + return "[" + name + ": " + value + "]"; + } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java index 03c288f53dd..0d55d256390 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequest.java @@ -48,6 +48,7 @@ import static org.elasticsearch.action.ValidateActions.addValidationError; public class RolloverRequest extends AcknowledgedRequest implements IndicesRequest { private String sourceAlias; + private boolean simulate; private Set conditions = new HashSet<>(2); public static ObjectParser, ParseFieldMatcherSupplier> TLP_PARSER = @@ -77,6 +78,7 @@ public class RolloverRequest extends AcknowledgedRequest implem public void readFrom(StreamInput in) throws IOException { super.readFrom(in); sourceAlias = in.readString(); + simulate = in.readBoolean(); int size = in.readVInt(); for (int i = 0; i < size; i++) { this.conditions.add(in.readNamedWriteable(Condition.class)); @@ -87,6 +89,7 @@ public class RolloverRequest extends AcknowledgedRequest implem public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(sourceAlias); + out.writeBoolean(simulate); out.writeVInt(conditions.size()); for (Condition condition : conditions) { out.writeNamedWriteable(condition); @@ -107,6 +110,10 @@ public class RolloverRequest extends AcknowledgedRequest implem this.sourceAlias = sourceAlias; } + public void simulate(boolean simulate) { + this.simulate = simulate; + } + public void addMaxIndexAgeCondition(TimeValue age) { this.conditions.add(new Condition.MaxAge(age)); } @@ -115,6 +122,10 @@ public class RolloverRequest extends AcknowledgedRequest implem this.conditions.add(new Condition.MaxDocs(docs)); } + public boolean isSimulate() { + return simulate; + } + public Set getConditions() { return conditions; } @@ -135,4 +146,5 @@ public class RolloverRequest extends AcknowledgedRequest implem throw new ElasticsearchParseException("failed to parse content type for rollover index source"); } } + } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java index 2a95468de69..ea495bc6eba 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java @@ -46,4 +46,9 @@ public class RolloverRequestBuilder extends MasterNodeOperationRequestBuilder> conditionStatus; + private boolean simulate; + private boolean rolledOver; + private boolean rolloverIndexCreated; RolloverResponse() { } - RolloverResponse(String oldIndex, String newIndex) { + RolloverResponse(String oldIndex, String newIndex, Set> conditionStatus, + boolean simulate, boolean rolledOver, boolean rolloverIndexCreated) { this.oldIndex = oldIndex; this.newIndex = newIndex; + this.simulate = simulate; + this.rolledOver = rolledOver; + this.rolloverIndexCreated = rolloverIndexCreated; + this.conditionStatus = conditionStatus; } public String getOldIndex() { @@ -46,11 +62,38 @@ public final class RolloverResponse extends ActionResponse { return newIndex; } + public Set> getConditionStatus() { + return conditionStatus; + } + + public boolean isSimulate() { + return simulate; + } + + public boolean isRolledOver() { + return rolledOver; + } + + public boolean isRolloverIndexCreated() { + return rolloverIndexCreated; + } + @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); oldIndex = in.readString(); newIndex = in.readString(); + int conditionSize = in.readVInt(); + Set> conditions = new HashSet<>(conditionSize); + for (int i = 0; i < conditionSize; i++) { + String condition = in.readString(); + boolean satisfied = in.readBoolean(); + conditions.add(new AbstractMap.SimpleEntry<>(condition, satisfied)); + } + conditionStatus = conditions; + simulate = in.readBoolean(); + rolledOver = in.readBoolean(); + rolloverIndexCreated = in.readBoolean(); } @Override @@ -58,5 +101,37 @@ public final class RolloverResponse extends ActionResponse { super.writeTo(out); out.writeString(oldIndex); out.writeString(newIndex); + out.writeVInt(conditionStatus.size()); + for (Map.Entry entry : conditionStatus) { + out.writeString(entry.getKey()); + out.writeBoolean(entry.getValue()); + } + out.writeBoolean(simulate); + out.writeBoolean(rolledOver); + out.writeBoolean(rolloverIndexCreated); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field(Fields.OLD_INDEX, oldIndex); + builder.field(Fields.NEW_INDEX, newIndex); + builder.field(Fields.ROLLED_OVER, rolledOver); + builder.field(Fields.SIMULATED, simulate); + builder.field(Fields.ROLLOVER_INDEX_CREATED, rolloverIndexCreated); + builder.startObject(Fields.CONDITIONS); + for (Map.Entry entry : conditionStatus) { + builder.field(entry.getKey(), entry.getValue()); + } + builder.endObject(); + return builder; + } + + static final class Fields { + static final String NEW_INDEX = "new_index"; + static final String OLD_INDEX = "old_index"; + static final String SIMULATED = "simulated"; + static final String ROLLED_OVER = "rolled_over"; + static final String ROLLOVER_INDEX_CREATED = "rollover_index_created"; + static final String CONDITIONS = "conditions"; } } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java index fa8782aca03..05bc674b8d3 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java @@ -47,7 +47,11 @@ import org.elasticsearch.indices.IndexAlreadyExistsException; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.TransportService; +import java.util.AbstractMap; +import java.util.HashSet; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * Main class to swap the index pointed to by an alias, given some predicates @@ -105,9 +109,23 @@ public class TransportRolloverAction extends TransportMasterNodeAction> evaluatedConditions = + evaluateConditions(rolloverRequest.getConditions(), docCount, indexAge); + final Set> conditionStatus = evaluatedConditions.stream() + .map(entry -> new AbstractMap.SimpleEntry<>(entry.getKey().toString(), entry.getValue())) + .collect(Collectors.toSet()); + final String rolloverIndexName = generateRolloverIndexName(sourceIndexName); + final boolean createRolloverIndex = metaData.index(rolloverIndexName) == null; + if (rolloverRequest.isSimulate()) { + listener.onResponse( + new RolloverResponse(sourceIndexName, rolloverIndexName, conditionStatus, true, false, + false)); + return; + } + if (conditionStatus.stream().allMatch(Map.Entry::getValue)) { + final RolloverResponse rolloverResponse = + new RolloverResponse(sourceIndexName, rolloverIndexName, conditionStatus, false, true, + createRolloverIndex); if (createRolloverIndex) { CreateIndexClusterStateUpdateRequest updateRequest = prepareCreateIndexRequest(rolloverIndexName, rolloverRequest); @@ -116,7 +134,7 @@ public class TransportRolloverAction extends TransportMasterNodeAction { private final ActionListener listener; - private final String oldIndex; - private final String newIndex; + private final RolloverResponse response; - public IndicesAliasesListener(String oldIndex, String newIndex, ActionListener listener) { - this.oldIndex = oldIndex; - this.newIndex = newIndex; + public IndicesAliasesListener(RolloverResponse response, ActionListener listener) { + this.response = response; this.listener = listener; } @Override public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) { - listener.onResponse(new RolloverResponse(oldIndex, newIndex)); + listener.onResponse(response); } @Override @@ -199,24 +218,21 @@ public class TransportRolloverAction extends TransportMasterNodeAction conditions, long docCount, long indexAge) { + static Set> evaluateConditions(Set conditions, long docCount, long indexAge) { + Set> result = new HashSet<>(conditions.size()); for (Condition condition: conditions) { if (condition instanceof Condition.MaxAge) { Condition.MaxAge maxAge = (Condition.MaxAge) condition; final TimeValue age = TimeValue.timeValueMillis(indexAge); - if (maxAge.matches(age) == false) { - return false; - } + result.add(new AbstractMap.SimpleEntry<>(condition, maxAge.matches(age))); } else if (condition instanceof Condition.MaxDocs) { final Condition.MaxDocs maxDocs = (Condition.MaxDocs) condition; - if (maxDocs.matches(docCount) == false) { - return false; - } + result.add(new AbstractMap.SimpleEntry<>(condition, maxDocs.matches(docCount))); } else { throw new IllegalArgumentException("unknown condition [" + condition.getClass().getSimpleName() + "]"); } } - return true; + return result; } static void validate(MetaData metaData, RolloverRequest request) { diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRolloverIndexAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRolloverIndexAction.java index 50ec45e9229..b56b04166fb 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRolloverIndexAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestRolloverIndexAction.java @@ -20,20 +20,14 @@ package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; -import org.elasticsearch.action.admin.indices.rollover.RolloverResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; -import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.RestResponse; -import org.elasticsearch.rest.action.support.RestBuilderListener; - -import static org.elasticsearch.rest.RestStatus.OK; +import org.elasticsearch.rest.action.support.RestToXContentListener; /** * @@ -45,6 +39,7 @@ public class RestRolloverIndexAction extends BaseRestHandler { super(settings, client); controller.registerHandler(RestRequest.Method.PUT, "/{alias}/_rollover", this); controller.registerHandler(RestRequest.Method.POST, "/{alias}/_rollover", this); + controller.registerHandler(RestRequest.Method.GET, "/{alias}/_rollover", this); } @Override @@ -56,17 +51,9 @@ public class RestRolloverIndexAction extends BaseRestHandler { if (request.hasContent()) { rolloverIndexRequest.source(request.content()); } + rolloverIndexRequest.simulate(request.method() == RestRequest.Method.GET || request.paramAsBoolean("simulate", false)); rolloverIndexRequest.timeout(request.paramAsTime("timeout", rolloverIndexRequest.timeout())); rolloverIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", rolloverIndexRequest.masterNodeTimeout())); - client.admin().indices().rolloverIndex(rolloverIndexRequest, new RestBuilderListener(channel) { - @Override - public RestResponse buildResponse(RolloverResponse rolloverResponse, XContentBuilder builder) throws Exception { - builder.startObject(); - builder.field("old_index", rolloverResponse.getOldIndex()); - builder.field("new_index", rolloverResponse.getNewIndex()); - builder.endObject(); - return new BytesRestResponse(OK, builder); - } - }); + client.admin().indices().rolloverIndex(rolloverIndexRequest, new RestToXContentListener<>(channel)); } } diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/ConditionTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/ConditionTests.java new file mode 100644 index 00000000000..3ce1b84d82c --- /dev/null +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/ConditionTests.java @@ -0,0 +1,41 @@ +/* + * 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.action.admin.indices.rollover; + +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.Matchers.equalTo; + +public class ConditionTests extends ESTestCase { + + public void testMaxAge() throws Exception { + final Condition.MaxAge maxAge = new Condition.MaxAge(TimeValue.timeValueMillis(10)); + assertThat(maxAge.matches(TimeValue.timeValueMillis(randomIntBetween(0, 9))), equalTo(false)); + assertThat(maxAge.matches(TimeValue.timeValueMillis(randomIntBetween(10, 100))), equalTo(true)); + } + + public void testMaxDocs() throws Exception { + final Condition.MaxDocs maxDocs = new Condition.MaxDocs(10L); + assertThat(maxDocs.matches((long) randomIntBetween(0, 9)), equalTo(false)); + assertThat(maxDocs.matches((long) randomIntBetween(10, 100)), equalTo(true)); + + } +} diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java index 3ab9c8cc040..9c0f3b7b96a 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java @@ -25,6 +25,8 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESIntegTestCase; +import java.util.Map; + import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.hamcrest.Matchers.equalTo; @@ -36,6 +38,10 @@ public class RolloverIT extends ESIntegTestCase { final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get(); assertThat(response.getOldIndex(), equalTo("test_index")); assertThat(response.getNewIndex(), equalTo("test_index-1")); + assertThat(response.isSimulate(), equalTo(false)); + assertThat(response.isRolledOver(), equalTo(true)); + assertThat(response.isRolloverIndexCreated(), equalTo(true)); + assertThat(response.getConditionStatus().size(), equalTo(0)); final ClusterState state = client().admin().cluster().prepareState().get().getState(); final IndexMetaData oldIndex = state.metaData().index("test_index"); assertFalse(oldIndex.getAliases().containsKey("test_alias")); @@ -50,6 +56,10 @@ public class RolloverIT extends ESIntegTestCase { final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get(); assertThat(response.getOldIndex(), equalTo("test_index")); assertThat(response.getNewIndex(), equalTo("test_index-1")); + assertThat(response.isSimulate(), equalTo(false)); + assertThat(response.isRolledOver(), equalTo(true)); + assertThat(response.isRolloverIndexCreated(), equalTo(true)); + assertThat(response.getConditionStatus().size(), equalTo(0)); final ClusterState state = client().admin().cluster().prepareState().get().getState(); final IndexMetaData oldIndex = state.metaData().index("test_index"); assertFalse(oldIndex.getAliases().containsKey("test_alias")); @@ -57,6 +67,24 @@ public class RolloverIT extends ESIntegTestCase { assertTrue(newIndex.getAliases().containsKey("test_alias")); } + public void testRolloverSimulate() throws Exception { + assertAcked(prepareCreate("test_index").addAlias(new Alias("test_alias")).get()); + index("test_index", "type1", "1", "field", "value"); + flush("test_index"); + final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").simulate(true).get(); + assertThat(response.getOldIndex(), equalTo("test_index")); + assertThat(response.getNewIndex(), equalTo("test_index-1")); + assertThat(response.isSimulate(), equalTo(true)); + assertThat(response.isRolledOver(), equalTo(false)); + assertThat(response.isRolloverIndexCreated(), equalTo(false)); + assertThat(response.getConditionStatus().size(), equalTo(0)); + final ClusterState state = client().admin().cluster().prepareState().get().getState(); + final IndexMetaData oldIndex = state.metaData().index("test_index"); + assertTrue(oldIndex.getAliases().containsKey("test_alias")); + final IndexMetaData newIndex = state.metaData().index("test_index-1"); + assertNull(newIndex); + } + public void testRolloverConditionsNotMet() throws Exception { assertAcked(prepareCreate("test_index").addAlias(new Alias("test_alias")).get()); index("test_index", "type1", "1", "field", "value"); @@ -65,6 +93,13 @@ public class RolloverIT extends ESIntegTestCase { .addMaxIndexAgeCondition(TimeValue.timeValueHours(4)).get(); assertThat(response.getOldIndex(), equalTo("test_index")); assertThat(response.getNewIndex(), equalTo("test_index")); + assertThat(response.isSimulate(), equalTo(false)); + assertThat(response.isRolledOver(), equalTo(false)); + assertThat(response.isRolloverIndexCreated(), equalTo(false)); + assertThat(response.getConditionStatus().size(), equalTo(1)); + final Map.Entry conditionEntry = response.getConditionStatus().iterator().next(); + assertThat(conditionEntry.getKey(), equalTo(new Condition.MaxAge(TimeValue.timeValueHours(4)).toString())); + assertThat(conditionEntry.getValue(), equalTo(false)); final ClusterState state = client().admin().cluster().prepareState().get().getState(); final IndexMetaData oldIndex = state.metaData().index("test_index"); assertTrue(oldIndex.getAliases().containsKey("test_alias")); @@ -81,6 +116,9 @@ public class RolloverIT extends ESIntegTestCase { final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get(); assertThat(response.getOldIndex(), equalTo("test_index")); assertThat(response.getNewIndex(), equalTo("test_index-1")); + assertThat(response.isSimulate(), equalTo(false)); + assertThat(response.isRolledOver(), equalTo(true)); + assertThat(response.isRolloverIndexCreated(), equalTo(false)); final ClusterState state = client().admin().cluster().prepareState().get().getState(); final IndexMetaData oldIndex = state.metaData().index("test_index"); assertFalse(oldIndex.getAliases().containsKey("test_alias")); diff --git a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java index ff121ff3dfb..636378a5872 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java @@ -27,44 +27,13 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.UUIDs; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.test.ESTestCase; -import java.util.Collections; -import java.util.Set; - import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; public class TransportRolloverActionTests extends ESTestCase { - public void testSatisfyConditions() throws Exception { - Set conditions = Collections.emptySet(); - assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomLong(), - randomLong())); - - conditions = Collections.singleton(new Condition.MaxAge(TimeValue.timeValueMillis(10))); - assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomLong(), - TimeValue.timeValueMillis(randomIntBetween(10, 100)).getMillis())); - assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomLong(), - TimeValue.timeValueMillis(randomIntBetween(0, 9)).getMillis())); - - conditions = Collections.singleton(new Condition.MaxDocs(10L)); - assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(10, 100), - randomLong())); - assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1, 9), - randomLong())); - - conditions = Sets.newHashSet(new Condition.MaxAge(TimeValue.timeValueMillis(100)), new Condition.MaxDocs(1000L)); - assertTrue(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1000, 1500), - TimeValue.timeValueMillis(randomIntBetween(100, 1000)).getMillis())); - assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1, 999), - TimeValue.timeValueMillis(randomIntBetween(100, 1000)).getMillis())); - assertFalse(TransportRolloverAction.satisfiesConditions(conditions, randomIntBetween(1000, 1500), - TimeValue.timeValueMillis(randomIntBetween(0, 99)).getMillis())); - } - public void testCreateUpdateAliasRequest() throws Exception { String sourceAlias = randomAsciiOfLength(10); String sourceIndex = randomAsciiOfLength(10); @@ -114,35 +83,27 @@ public class TransportRolloverActionTests extends ESTestCase { .putAlias(AliasMetaData.builder(aliasWithMultipleIndices)) ).build(); - try { - TransportRolloverAction.validate(metaData, new RolloverRequest(aliasWithMultipleIndices)); - fail("expected to throw exception"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("source alias maps to multiple indices")); - } - - try { - TransportRolloverAction.validate(metaData, new RolloverRequest(randomFrom(index1, index2))); - fail("expected to throw exception"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("source alias is a concrete index")); - } - - try { - TransportRolloverAction.validate(metaData, new RolloverRequest(randomAsciiOfLength(5))); - fail("expected to throw exception"); - } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("source alias does not exist")); - } - + expectThrows(IllegalArgumentException.class, () -> + TransportRolloverAction.validate(metaData, new RolloverRequest(aliasWithMultipleIndices))); + expectThrows(IllegalArgumentException.class, () -> + TransportRolloverAction.validate(metaData, new RolloverRequest(randomFrom(index1, index2)))); + expectThrows(IllegalArgumentException.class, () -> + TransportRolloverAction.validate(metaData, new RolloverRequest(randomAsciiOfLength(5))) + ); TransportRolloverAction.validate(metaData, new RolloverRequest(alias)); } public void testGenerateRolloverIndexName() throws Exception { - String index = randomAsciiOfLength(10); - assertThat(TransportRolloverAction.generateRolloverIndexName(index), not(equalTo(index))); - assertThat(TransportRolloverAction.generateRolloverIndexName("index"), equalTo("index-1")); - assertThat(TransportRolloverAction.generateRolloverIndexName("index-1"), equalTo("index-2")); + String indexNotEndingInNumbers = randomAsciiOfLength(10) + "A"; + assertThat(TransportRolloverAction.generateRolloverIndexName(indexNotEndingInNumbers), + not(equalTo(indexNotEndingInNumbers))); + assertThat(TransportRolloverAction.generateRolloverIndexName(indexNotEndingInNumbers), + equalTo(indexNotEndingInNumbers + "-1")); + int num = randomIntBetween(0, 100); + final String indexPrefix = randomAsciiOfLength(10); + String indexEndingInNumbers = indexPrefix + "-" + num; + assertThat(TransportRolloverAction.generateRolloverIndexName(indexEndingInNumbers), + equalTo(indexPrefix + "-" + (num + 1))); assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-2")); assertThat(TransportRolloverAction.generateRolloverIndexName("index-name"), equalTo("index-name-1")); }