Skip to content

Commit

Permalink
Core: Store index creation time in index metadata
Browse files Browse the repository at this point in the history
This change stores the index creation time in the index metadata when an index is created.  The creation time cannot be changed but can be set as part of the create index request to allow for correct creation times for historical data.

Closes #7119
  • Loading branch information
colings86 committed Aug 12, 2014
1 parent 7fa8a0a commit b52455f
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 10 deletions.
18 changes: 18 additions & 0 deletions docs/reference/indices/create-index.asciidoc
Expand Up @@ -129,3 +129,21 @@ curl -XPUT localhost:9200/test -d '{
}
}'
--------------------------------------------------

[float]
=== Creation Date

coming[1.4.0]

When an index is created, a timestamp is stored in the index metadata for the creation date. By
default this it is automatically generated but it can also be specified using the
`creation_date` parameter on the create index API:

[source,js]
--------------------------------------------------
curl -XPUT localhost:9200/test -d '{
"creation_date" : 1407751337000 <1>
}'
--------------------------------------------------

<1> `creation_date` is set using epoch time in milliseconds.
Expand Up @@ -45,9 +45,9 @@

import static com.google.common.collect.Maps.newHashMap;
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.ImmutableSettings.readSettingsFromStream;
import static org.elasticsearch.common.settings.ImmutableSettings.writeSettingsToStream;
import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;

/**
* A request to create an index. Best created with {@link org.elasticsearch.client.Requests#createIndexRequest(String)}.
Expand Down
Expand Up @@ -160,6 +160,7 @@ public static State fromString(String state) {
public static final String SETTING_BLOCKS_WRITE = "index.blocks.write";
public static final String SETTING_BLOCKS_METADATA = "index.blocks.metadata";
public static final String SETTING_VERSION_CREATED = "index.version.created";
public static final String SETTING_CREATION_DATE = "index.creation_date";
public static final String SETTING_UUID = "index.uuid";
public static final String INDEX_UUID_NA_VALUE = "_na_";

Expand Down Expand Up @@ -251,6 +252,14 @@ public long getVersion() {
return this.version;
}

public long creationDate() {
return settings.getAsLong(SETTING_CREATION_DATE, -1l);
}

public long getCreationDate() {
return creationDate();
}

public State state() {
return this.state;
}
Expand Down Expand Up @@ -457,6 +466,15 @@ public Builder numberOfReplicas(int numberOfReplicas) {
public int numberOfReplicas() {
return settings.getAsInt(SETTING_NUMBER_OF_REPLICAS, -1);
}

public Builder creationDate(long creationDate) {
settings = settingsBuilder().put(settings).put(SETTING_CREATION_DATE, creationDate).build();
return this;
}

public long creationDate() {
return settings.getAsLong(SETTING_CREATION_DATE, -1l);
}

public Builder settings(Settings.Builder settings) {
this.settings = settings.build();
Expand Down
Expand Up @@ -36,6 +36,8 @@
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
import org.elasticsearch.cluster.block.ClusterBlock;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.metadata.IndexMetaData.Custom;
import org.elasticsearch.cluster.metadata.IndexMetaData.State;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
Expand Down Expand Up @@ -324,6 +326,11 @@ public ClusterState execute(ClusterState currentState) throws Exception {
final Version createdVersion = Version.smallest(version, nodes.smallestNonClientNodeVersion());
indexSettingsBuilder.put(SETTING_VERSION_CREATED, createdVersion);
}

if (indexSettingsBuilder.get(SETTING_CREATION_DATE) == null) {
indexSettingsBuilder.put(SETTING_CREATION_DATE, System.currentTimeMillis());
}

indexSettingsBuilder.put(SETTING_UUID, Strings.randomBase64UUID());

Settings actualIndexSettings = indexSettingsBuilder.build();
Expand Down
@@ -0,0 +1,73 @@
/*
* 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.create;

import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
import org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
import org.junit.Test;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsNull.notNullValue;

@ClusterScope(scope = Scope.TEST)
public class CreateIndexTests extends ElasticsearchIntegrationTest{

@Test
public void testCreationDate_Given() {
prepareCreate("test").setSettings(ImmutableSettings.builder().put(IndexMetaData.SETTING_CREATION_DATE, 4l)).get();
ClusterStateResponse response = client().admin().cluster().prepareState().get();
ClusterState state = response.getState();
assertThat(state, notNullValue());
MetaData metadata = state.getMetaData();
assertThat(metadata, notNullValue());
ImmutableOpenMap<String, IndexMetaData> indices = metadata.getIndices();
assertThat(indices, notNullValue());
assertThat(indices.size(), equalTo(1));
IndexMetaData index = indices.get("test");
assertThat(index, notNullValue());
assertThat(index.creationDate(), equalTo(4l));
}

@Test
public void testCreationDate_Generated() {
long timeBeforeRequest = System.currentTimeMillis();
prepareCreate("test").get();
long timeAfterRequest = System.currentTimeMillis();
ClusterStateResponse response = client().admin().cluster().prepareState().get();
ClusterState state = response.getState();
assertThat(state, notNullValue());
MetaData metadata = state.getMetaData();
assertThat(metadata, notNullValue());
ImmutableOpenMap<String, IndexMetaData> indices = metadata.getIndices();
assertThat(indices, notNullValue());
assertThat(indices.size(), equalTo(1));
IndexMetaData index = indices.get("test");
assertThat(index, notNullValue());
assertThat(index.creationDate(), allOf(lessThanOrEqualTo(timeAfterRequest), greaterThanOrEqualTo(timeBeforeRequest)));
}

}

0 comments on commit b52455f

Please sign in to comment.