Skip to content

Commit 7817a9f

Browse files
committed
Add rollover-creation-date setting to rolled over index
This PR introduces a new index setting `index.rollover.creation_date` much like the `index.creation_date`, it captures the approximate time that the index was rolled over to a new one.
1 parent 05ee0f8 commit 7817a9f

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java

+18-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesClusterStateUpdateRequest;
2424
import org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest;
2525
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
26+
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
27+
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder;
2628
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
2729
import org.elasticsearch.action.support.ActionFilters;
2830
import org.elasticsearch.action.support.ActiveShardCount;
@@ -52,6 +54,7 @@
5254

5355
import java.util.Arrays;
5456
import java.util.Collection;
57+
import java.util.Date;
5558
import java.util.List;
5659
import java.util.Locale;
5760
import java.util.Map;
@@ -141,16 +144,18 @@ public void onResponse(IndicesStatsResponse statsResponse) {
141144
rolloverRequest),
142145
ActionListener.wrap(aliasClusterStateUpdateResponse -> {
143146
if (aliasClusterStateUpdateResponse.isAcknowledged()) {
144-
activeShardsObserver.waitForActiveShards(new String[]{rolloverIndexName},
145-
rolloverRequest.getCreateIndexRequest().waitForActiveShards(),
146-
rolloverRequest.masterNodeTimeout(),
147-
isShardsAcknowledged -> listener.onResponse(new RolloverResponse(
148-
sourceIndexName, rolloverIndexName, conditionResults, false, true, true,
149-
isShardsAcknowledged)),
150-
listener::onFailure);
147+
client.admin().indices().updateSettings(prepareRolloverUpdateSettingsRequest(sourceIndexName),
148+
ActionListener.wrap(updateSettingsResponse ->
149+
activeShardsObserver.waitForActiveShards(new String[]{rolloverIndexName},
150+
rolloverRequest.getCreateIndexRequest().waitForActiveShards(),
151+
rolloverRequest.masterNodeTimeout(),
152+
isShardsAcknowledged -> listener.onResponse(new RolloverResponse(
153+
sourceIndexName, rolloverIndexName, conditionResults, false, true, true,
154+
isShardsAcknowledged)),
155+
listener::onFailure), listener::onFailure));
151156
} else {
152157
listener.onResponse(new RolloverResponse(sourceIndexName, rolloverIndexName, conditionResults,
153-
false, true, false, false));
158+
false, true, false, false));
154159
}
155160
}, listener::onFailure));
156161
}, listener::onFailure));
@@ -181,6 +186,11 @@ static IndicesAliasesClusterStateUpdateRequest prepareRolloverAliasesUpdateReque
181186
return updateRequest;
182187
}
183188

189+
static UpdateSettingsRequest prepareRolloverUpdateSettingsRequest(String oldIndex) {
190+
return new UpdateSettingsRequest(oldIndex).settings(Settings.builder()
191+
.put(IndexMetaData.SETTING_ROLLOVER_CREATION_DATE, new Date().getTime()));
192+
}
193+
184194

185195
static String generateRolloverIndexName(String sourceIndexName, IndexNameExpressionResolver indexNameExpressionResolver) {
186196
String resolvedName = indexNameExpressionResolver.resolveDateMathExpression(sourceIndexName);

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

+4
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ public Iterator<Setting<Integer>> settings() {
271271
Setting.prefixKeySetting("index.routing.allocation.initial_recovery.", key -> Setting.simpleString(key));
272272
// this is only setable internally not a registered setting!!
273273

274+
public static final String SETTING_ROLLOVER_CREATION_DATE = "index.rollover.creation_date";
275+
public static final Setting<Long> ROLLOVER_CREATION_DATE_SETTING = Setting.longSetting(SETTING_ROLLOVER_CREATION_DATE,
276+
-1, -1, Property.Dynamic, Property.IndexScope);
277+
274278
/**
275279
* The number of active shard copies to check for before proceeding with a write operation.
276280
*/

server/src/main/java/org/elasticsearch/common/settings/IndexScopedSettings.java

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings {
157157
EngineConfig.INDEX_CODEC_SETTING,
158158
EngineConfig.INDEX_OPTIMIZE_AUTO_GENERATED_IDS,
159159
IndexMetaData.SETTING_WAIT_FOR_ACTIVE_SHARDS,
160+
IndexMetaData.ROLLOVER_CREATION_DATE_SETTING,
160161

161162
// validate that built-in similarities don't get redefined
162163
Setting.groupSetting("index.similarity.", (s) -> {

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import static org.hamcrest.Matchers.containsInAnyOrder;
4444
import static org.hamcrest.Matchers.equalTo;
4545
import static org.hamcrest.Matchers.everyItem;
46+
import static org.hamcrest.Matchers.greaterThan;
4647
import static org.hamcrest.Matchers.is;
4748

4849
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST)
@@ -65,6 +66,7 @@ public void testRolloverOnEmptyIndex() throws Exception {
6566
final ClusterState state = client().admin().cluster().prepareState().get().getState();
6667
final IndexMetaData oldIndex = state.metaData().index("test_index-1");
6768
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
69+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), greaterThan(0L));
6870
final IndexMetaData newIndex = state.metaData().index("test_index-000002");
6971
assertTrue(newIndex.getAliases().containsKey("test_alias"));
7072
}
@@ -81,6 +83,7 @@ public void testRollover() throws Exception {
8183
assertThat(response.getConditionStatus().size(), equalTo(0));
8284
final ClusterState state = client().admin().cluster().prepareState().get().getState();
8385
final IndexMetaData oldIndex = state.metaData().index("test_index-2");
86+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), greaterThan(0L));
8487
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
8588
final IndexMetaData newIndex = state.metaData().index("test_index-000003");
8689
assertTrue(newIndex.getAliases().containsKey("test_alias"));
@@ -104,6 +107,7 @@ public void testRolloverWithIndexSettings() throws Exception {
104107
final ClusterState state = client().admin().cluster().prepareState().get().getState();
105108
final IndexMetaData oldIndex = state.metaData().index("test_index-2");
106109
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
110+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), greaterThan(0L));
107111
final IndexMetaData newIndex = state.metaData().index("test_index-000003");
108112
assertThat(newIndex.getNumberOfShards(), equalTo(1));
109113
assertThat(newIndex.getNumberOfReplicas(), equalTo(0));
@@ -123,6 +127,7 @@ public void testRolloverDryRun() throws Exception {
123127
assertThat(response.getConditionStatus().size(), equalTo(0));
124128
final ClusterState state = client().admin().cluster().prepareState().get().getState();
125129
final IndexMetaData oldIndex = state.metaData().index("test_index-1");
130+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), equalTo(-1L));
126131
assertTrue(oldIndex.getAliases().containsKey("test_alias"));
127132
final IndexMetaData newIndex = state.metaData().index("test_index-000002");
128133
assertNull(newIndex);
@@ -149,6 +154,7 @@ public void testRolloverConditionsNotMet() throws Exception {
149154
final ClusterState state = client().admin().cluster().prepareState().get().getState();
150155
final IndexMetaData oldIndex = state.metaData().index("test_index-0");
151156
assertTrue(oldIndex.getAliases().containsKey("test_alias"));
157+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), equalTo(-1L));
152158
final IndexMetaData newIndex = state.metaData().index("test_index-000001");
153159
assertNull(newIndex);
154160
}
@@ -167,6 +173,7 @@ public void testRolloverWithNewIndexName() throws Exception {
167173
final ClusterState state = client().admin().cluster().prepareState().get().getState();
168174
final IndexMetaData oldIndex = state.metaData().index("test_index");
169175
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
176+
assertThat(IndexMetaData.ROLLOVER_CREATION_DATE_SETTING.get(oldIndex.getSettings()), greaterThan(0L));
170177
final IndexMetaData newIndex = state.metaData().index("test_new_index");
171178
assertTrue(newIndex.getAliases().containsKey("test_alias"));
172179
}

0 commit comments

Comments
 (0)