diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexClusterStateUpdateRequest.java b/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexClusterStateUpdateRequest.java index 7b3b2a0a2f0..a2290a5e255 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexClusterStateUpdateRequest.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexClusterStateUpdateRequest.java @@ -41,6 +41,7 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ private final TransportMessage originalMessage; private final String cause; private final String index; + private final String providedName; private final boolean updateAllTypes; private Index shrinkFrom; @@ -59,11 +60,13 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT; - public CreateIndexClusterStateUpdateRequest(TransportMessage originalMessage, String cause, String index, boolean updateAllTypes) { + public CreateIndexClusterStateUpdateRequest(TransportMessage originalMessage, String cause, String index, String providedName, + boolean updateAllTypes) { this.originalMessage = originalMessage; this.cause = cause; this.index = index; this.updateAllTypes = updateAllTypes; + this.providedName = providedName; } public CreateIndexClusterStateUpdateRequest settings(Settings settings) { @@ -151,6 +154,14 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ return updateAllTypes; } + /** + * The name that was provided by the user. This might contain a date math expression. + * @see IndexMetaData#SETTING_INDEX_PROVIDED_NAME + */ + public String getProvidedName() { + return providedName; + } + public ActiveShardCount waitForActiveShards() { return waitForActiveShards; } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java index d3ce1975e89..354dcf23873 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/create/TransportCreateIndexAction.java @@ -72,7 +72,7 @@ public class TransportCreateIndexAction extends TransportMasterNodeAction() { @@ -113,16 +115,18 @@ public class TransportRolloverAction extends TransportMasterNodeAction conditionResults = evaluateConditions(rolloverRequest.getConditions(), statsResponse.getTotal().getDocs(), metaData.index(sourceIndexName)); - final String rolloverIndexName = (rolloverRequest.getNewIndexName() != null) + final String unresolvedName = (rolloverRequest.getNewIndexName() != null) ? rolloverRequest.getNewIndexName() - : generateRolloverIndexName(sourceIndexName); + : generateRolloverIndexName(sourceProvidedName, indexNameExpressionResolver); + final String rolloverIndexName = indexNameExpressionResolver.resolveDateMathExpression(unresolvedName); if (rolloverRequest.isDryRun()) { listener.onResponse( new RolloverResponse(sourceIndexName, rolloverIndexName, conditionResults, true, false, false, false)); return; } if (conditionResults.size() == 0 || conditionResults.stream().anyMatch(result -> result.matched)) { - CreateIndexClusterStateUpdateRequest updateRequest = prepareCreateIndexRequest(rolloverIndexName, rolloverRequest); + CreateIndexClusterStateUpdateRequest updateRequest = prepareCreateIndexRequest(unresolvedName, rolloverIndexName, + rolloverRequest); createIndexService.createIndex(updateRequest, ActionListener.wrap(createIndexClusterStateUpdateResponse -> { // switch the alias to point to the newly created index indexAliasesService.indicesAliases( @@ -170,12 +174,17 @@ public class TransportRolloverAction extends TransportMasterNodeAction" : ""); + return newName; } else { throw new IllegalArgumentException("index name [" + sourceIndexName + "] does not match pattern '^.*-(\\d)+$'"); } @@ -203,14 +212,14 @@ public class TransportRolloverAction extends TransportMasterNodeAction perShardDocStats, IndexNameExpressionResolver indexNameExpressionResolver) { - final String sourceIndex = indexNameExpressionResolver.resolveDateMathExpression(shrinkReqeust.getSourceIndex()); - final CreateIndexRequest targetIndex = shrinkReqeust.getShrinkIndexRequest(); + final String sourceIndex = indexNameExpressionResolver.resolveDateMathExpression(shrinkRequest.getSourceIndex()); + final CreateIndexRequest targetIndex = shrinkRequest.getShrinkIndexRequest(); final String targetIndexName = indexNameExpressionResolver.resolveDateMathExpression(targetIndex.index()); final IndexMetaData metaData = state.metaData().index(sourceIndex); final Settings targetIndexSettings = Settings.builder().put(targetIndex.settings()) @@ -137,7 +137,7 @@ public class TransportShrinkAction extends TransportMasterNodeAction, FromXContentBuild public static final String SETTING_VERSION_UPGRADED_STRING = "index.version.upgraded_string"; public static final String SETTING_VERSION_MINIMUM_COMPATIBLE = "index.version.minimum_compatible"; public static final String SETTING_CREATION_DATE = "index.creation_date"; + /** + * The user provided name for an index. This is the plain string provided by the user when the index was created. + * It might still contain date math expressions etc. (added in 5.0) + */ + public static final String SETTING_INDEX_PROVIDED_NAME = "index.provided_name"; public static final String SETTING_PRIORITY = "index.priority"; public static final Setting INDEX_PRIORITY_SETTING = Setting.intSetting("index.priority", 1, 0, Property.Dynamic, Property.IndexScope); diff --git a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java index 6f587854825..373c62ab114 100644 --- a/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java +++ b/core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java @@ -318,7 +318,7 @@ public class MetaDataCreateIndexService extends AbstractComponent { if (indexSettingsBuilder.get(SETTING_CREATION_DATE) == null) { indexSettingsBuilder.put(SETTING_CREATION_DATE, new DateTime(DateTimeZone.UTC).getMillis()); } - + indexSettingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getProvidedName()); indexSettingsBuilder.put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID()); final Index shrinkFromIndex = request.shrinkFrom(); int routingNumShards = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(indexSettingsBuilder.build());; diff --git a/core/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java b/core/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java index fc0d5f4df9f..1cc4f747a94 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java +++ b/core/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java @@ -186,6 +186,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings { case IndexMetaData.SETTING_INDEX_UUID: case IndexMetaData.SETTING_VERSION_CREATED: case IndexMetaData.SETTING_VERSION_UPGRADED: + case IndexMetaData.SETTING_INDEX_PROVIDED_NAME: case MergePolicyConfig.INDEX_MERGE_ENABLED: return true; default: 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 6566eb96db8..d5bc16207f5 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> 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 = ""; + 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, + "")).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("", getSettingsResponse.getSetting(response.getOldIndex(), + IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + assertEquals("", getSettingsResponse.getSetting(response.getNewIndex(), + IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + + response = client().admin().indices().prepareRolloverIndex("test_alias").setNewIndexName("").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 2bd4c2883fc..9e80e92a281 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( "", TransportRolloverAction.generateRolloverIndexName("", + 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")); diff --git a/core/src/test/java/org/elasticsearch/indices/DateMathIndexExpressionsIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/DateMathIndexExpressionsIntegrationIT.java index e91ed066cc6..1ec4fa0f4fe 100644 --- a/core/src/test/java/org/elasticsearch/indices/DateMathIndexExpressionsIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/DateMathIndexExpressionsIntegrationIT.java @@ -20,11 +20,13 @@ package org.elasticsearch.indices; import org.elasticsearch.action.DocWriteResponse; +import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.test.ESIntegTestCase; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -45,6 +47,12 @@ public class DateMathIndexExpressionsIntegrationIT extends ESIntegTestCase { String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2)); createIndex(index1, index2, index3); + GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get(); + assertEquals(index1, getSettingsResponse.getSetting(index1, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + assertEquals(index2, getSettingsResponse.getSetting(index2, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + assertEquals(index3, getSettingsResponse.getSetting(index3, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + + String dateMathExp1 = "<.marvel-{now/d}>"; String dateMathExp2 = "<.marvel-{now/d-1d}>"; String dateMathExp3 = "<.marvel-{now/d-2d}>"; @@ -122,6 +130,12 @@ public class DateMathIndexExpressionsIntegrationIT extends ESIntegTestCase { String dateMathExp3 = "<.marvel-{now/d-2d}>"; createIndex(dateMathExp1, dateMathExp2, dateMathExp3); + + GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get(); + assertEquals(dateMathExp1, getSettingsResponse.getSetting(index1, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + assertEquals(dateMathExp2, getSettingsResponse.getSetting(index2, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + assertEquals(dateMathExp3, getSettingsResponse.getSetting(index3, IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); + ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); assertThat(clusterState.metaData().index(index1), notNullValue()); assertThat(clusterState.metaData().index(index2), notNullValue()); diff --git a/docs/reference/api-conventions.asciidoc b/docs/reference/api-conventions.asciidoc index d361f3d07d1..e2d10ca62b2 100644 --- a/docs/reference/api-conventions.asciidoc +++ b/docs/reference/api-conventions.asciidoc @@ -399,7 +399,8 @@ Returns: "index.number_of_shards": "1", "index.creation_date": "1474389951325", "index.uuid": "n6gzFZTgS664GUfx0Xrpjw", - "index.version.created": ... + "index.version.created": ..., + "index.provided_name" : "twitter" } } } @@ -432,7 +433,8 @@ Returns: "uuid": "n6gzFZTgS664GUfx0Xrpjw", "version": { "created": ... - } + }, + "provided_name" : "twitter" } } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/InternalSettingsPlugin.java b/test/framework/src/main/java/org/elasticsearch/test/InternalSettingsPlugin.java index f46069484d6..a5b1667a13f 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/InternalSettingsPlugin.java +++ b/test/framework/src/main/java/org/elasticsearch/test/InternalSettingsPlugin.java @@ -21,7 +21,6 @@ package org.elasticsearch.test; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.plugins.Plugin; import java.util.Arrays; @@ -31,6 +30,8 @@ public final class InternalSettingsPlugin extends Plugin { public static final Setting VERSION_CREATED = Setting.intSetting("index.version.created", 0, Property.IndexScope, Property.NodeScope); + public static final Setting PROVIDED_NAME_SETTING = + Setting.simpleString("index.provided_name",Property.IndexScope, Property.NodeScope); public static final Setting MERGE_ENABLED = Setting.boolSetting("index.merge.enabled", true, Property.IndexScope, Property.NodeScope); public static final Setting INDEX_CREATION_DATE_SETTING = @@ -38,6 +39,7 @@ public final class InternalSettingsPlugin extends Plugin { @Override public List> getSettings() { - return Arrays.asList(VERSION_CREATED, MERGE_ENABLED, INDEX_CREATION_DATE_SETTING); + return Arrays.asList(VERSION_CREATED, MERGE_ENABLED, + INDEX_CREATION_DATE_SETTING, PROVIDED_NAME_SETTING); } }