summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/action/admin/indices/rollover
diff options
context:
space:
mode:
authorSimon Willnauer <simon.willnauer@elasticsearch.com>2016-10-03 16:52:33 +0200
committerGitHub <noreply@github.com>2016-10-03 16:52:33 +0200
commit56f35baf47435c963f63e4453b244fbe77a97c7c (patch)
tree732026f40631b759124695156838d053e8a2a7b5 /core/src/test/java/org/elasticsearch/action/admin/indices/rollover
parent9271c0302f37486507d0949673ba60618a79dfc5 (diff)
Add date-math support to `_rollover` (#20709)
today it's not possible to use date-math efficiently with the `_rollover` API. This change adds support for date-math in the target index as well as support for preserving the math logic when an existing index that was created with a date math expression all subsequent indices are created with the same expression.
Diffstat (limited to 'core/src/test/java/org/elasticsearch/action/admin/indices/rollover')
-rw-r--r--core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java57
-rw-r--r--core/src/test/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverActionTests.java19
2 files changed, 71 insertions, 5 deletions
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 6566eb96db..d5bc16207f 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
@@ -20,13 +20,21 @@
package org.elasticsearch.action.admin.indices.rollover;
import org.elasticsearch.action.admin.indices.alias.Alias;
+import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.indices.IndexAlreadyExistsException;
+import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESIntegTestCase;
+import org.elasticsearch.test.InternalSettingsPlugin;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Map;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
@@ -35,6 +43,12 @@ import static org.hamcrest.Matchers.equalTo;
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST)
public class RolloverIT extends ESIntegTestCase {
+ @Override
+ protected Collection<Class<? extends Plugin>> nodePlugins() {
+ return Collections.singleton(InternalSettingsPlugin.class);
+ }
+
+
public void testRolloverOnEmptyIndex() throws Exception {
assertAcked(prepareCreate("test_index-1").addAlias(new Alias("test_alias")).get());
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
@@ -161,4 +175,47 @@ public class RolloverIT extends ESIntegTestCase {
assertThat(e.getIndex().getName(), equalTo("test_index-000001"));
}
}
+
+ public void testRolloverWithDateMath() {
+ DateTime now = new DateTime(DateTimeZone.UTC);
+ String index = "test-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now) + "-1";
+ String dateMathExp = "<test-{now/d}-1>";
+ assertAcked(prepareCreate(dateMathExp).addAlias(new Alias("test_alias")).get());
+ ensureGreen(index);
+ // now we modify the provided name such that we can test that the pattern is carried on
+ client().admin().indices().prepareClose(index).get();
+ client().admin().indices().prepareUpdateSettings(index).setSettings(Settings.builder()
+ .put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME,
+ "<test-{now/M{YYYY.MM}}-1>")).get();
+
+ client().admin().indices().prepareOpen(index).get();
+ ensureGreen(index);
+ RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get();
+ assertThat(response.getOldIndex(), equalTo(index));
+ assertThat(response.getNewIndex(), equalTo("test-" + DateTimeFormat.forPattern("YYYY.MM").print(now) + "-000002"));
+ assertThat(response.isDryRun(), equalTo(false));
+ assertThat(response.isRolledOver(), equalTo(true));
+ assertThat(response.getConditionStatus().size(), equalTo(0));
+
+ response = client().admin().indices().prepareRolloverIndex("test_alias").get();
+ assertThat(response.getOldIndex(), equalTo("test-" + DateTimeFormat.forPattern("YYYY.MM").print(now) + "-000002"));
+ assertThat(response.getNewIndex(), equalTo("test-" + DateTimeFormat.forPattern("YYYY.MM").print(now) + "-000003"));
+ assertThat(response.isDryRun(), equalTo(false));
+ assertThat(response.isRolledOver(), equalTo(true));
+ assertThat(response.getConditionStatus().size(), equalTo(0));
+
+ GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(response.getOldIndex(),
+ response.getNewIndex()).get();
+ assertEquals("<test-{now/M{YYYY.MM}}-000002>", getSettingsResponse.getSetting(response.getOldIndex(),
+ IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
+ assertEquals("<test-{now/M{YYYY.MM}}-000003>", getSettingsResponse.getSetting(response.getNewIndex(),
+ IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
+
+ response = client().admin().indices().prepareRolloverIndex("test_alias").setNewIndexName("<test-{now/d}-000004>").get();
+ assertThat(response.getOldIndex(), equalTo("test-" + DateTimeFormat.forPattern("YYYY.MM").print(now) + "-000003"));
+ assertThat(response.getNewIndex(), equalTo("test-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now) + "-000004"));
+ assertThat(response.isDryRun(), equalTo(false));
+ assertThat(response.isRolledOver(), equalTo(true));
+ assertThat(response.getConditionStatus().size(), equalTo(0));
+ }
}
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 2bd4c2883f..9e80e92a28 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
@@ -26,6 +26,7 @@ import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.IndexMetaData;
+import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.settings.Settings;
@@ -33,6 +34,9 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.test.ESTestCase;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
import java.util.HashSet;
import java.util.List;
@@ -154,15 +158,20 @@ public class TransportRolloverActionTests extends ESTestCase {
public void testGenerateRolloverIndexName() throws Exception {
String invalidIndexName = randomAsciiOfLength(10) + "A";
+ IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(Settings.EMPTY);
expectThrows(IllegalArgumentException.class, () ->
- TransportRolloverAction.generateRolloverIndexName(invalidIndexName));
+ TransportRolloverAction.generateRolloverIndexName(invalidIndexName, indexNameExpressionResolver));
int num = randomIntBetween(0, 100);
final String indexPrefix = randomAsciiOfLength(10);
String indexEndingInNumbers = indexPrefix + "-" + num;
- assertThat(TransportRolloverAction.generateRolloverIndexName(indexEndingInNumbers),
+ assertThat(TransportRolloverAction.generateRolloverIndexName(indexEndingInNumbers, indexNameExpressionResolver),
equalTo(indexPrefix + "-" + String.format(Locale.ROOT, "%06d", num + 1)));
- assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1"), equalTo("index-name-000002"));
- assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-2"), equalTo("index-name-000003"));
+ assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-1", indexNameExpressionResolver),
+ equalTo("index-name-000002"));
+ assertThat(TransportRolloverAction.generateRolloverIndexName("index-name-2", indexNameExpressionResolver),
+ equalTo("index-name-000003"));
+ assertEquals( "<index-name-{now/d}-000002>", TransportRolloverAction.generateRolloverIndexName("<index-name-{now/d}-1>",
+ indexNameExpressionResolver));
}
public void testCreateIndexRequest() throws Exception {
@@ -179,7 +188,7 @@ public class TransportRolloverActionTests extends ESTestCase {
.build();
rolloverRequest.getCreateIndexRequest().settings(settings);
final CreateIndexClusterStateUpdateRequest createIndexRequest =
- TransportRolloverAction.prepareCreateIndexRequest(rolloverIndex, rolloverRequest);
+ TransportRolloverAction.prepareCreateIndexRequest(rolloverIndex, rolloverIndex, rolloverRequest);
assertThat(createIndexRequest.settings(), equalTo(settings));
assertThat(createIndexRequest.index(), equalTo(rolloverIndex));
assertThat(createIndexRequest.cause(), equalTo("rollover_index"));