Skip to content

Commit 261c610

Browse files
committed
IndexMetaData: Add internal format index setting (#25292)
This setting is supposed to ease index upgrades as it allows you to check for a new setting called `index.internal.version` which can be used to check before upgrading indices.
1 parent ebca51c commit 261c610

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.carrotsearch.hppc.cursors.IntObjectCursor;
2424
import com.carrotsearch.hppc.cursors.ObjectCursor;
2525
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
26-
2726
import org.elasticsearch.Version;
2827
import org.elasticsearch.action.support.ActiveShardCount;
2928
import org.elasticsearch.cluster.Diff;
@@ -270,6 +269,13 @@ static Setting<Integer> buildNumberOfShardsSetting() {
270269
Setting.Property.Dynamic,
271270
Setting.Property.IndexScope);
272271

272+
/**
273+
* an internal index format description, allowing us to find out if this index is upgraded or needs upgrading
274+
*/
275+
private static final String INDEX_FORMAT = "index.format";
276+
public static final Setting<Integer> INDEX_FORMAT_SETTING =
277+
Setting.intSetting(INDEX_FORMAT, 0, Setting.Property.IndexScope, Setting.Property.Final);
278+
273279
public static final String KEY_IN_SYNC_ALLOCATIONS = "in_sync_allocations";
274280
static final String KEY_VERSION = "version";
275281
static final String KEY_ROUTING_NUM_SHARDS = "routing_num_shards";
@@ -1062,6 +1068,7 @@ public IndexMetaData build() {
10621068
}
10631069

10641070
final String uuid = settings.get(SETTING_INDEX_UUID, INDEX_UUID_NA_VALUE);
1071+
10651072
return new IndexMetaData(new Index(index, uuid), version, primaryTerms, state, numberOfShards, numberOfReplicas, tmpSettings, mappings.build(),
10661073
tmpAliases.build(), customs.build(), filledInSyncAllocationIds.build(), requireFilters, initialRecoveryFilters, includeFilters, excludeFilters,
10671074
indexCreatedVersion, indexUpgradedVersion, getRoutingNumShards(), routingPartitionSize, waitForActiveShards);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public final class IndexScopedSettings extends AbstractScopedSettings {
8282
IndexMetaData.INDEX_BLOCKS_READ_ONLY_ALLOW_DELETE_SETTING,
8383
IndexMetaData.INDEX_PRIORITY_SETTING,
8484
IndexMetaData.INDEX_DATA_PATH_SETTING,
85+
IndexMetaData.INDEX_FORMAT_SETTING,
8586
SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING,
8687
SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING,
8788
SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING,

core/src/test/java/org/elasticsearch/cluster/metadata/IndexMetaDataTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import java.io.IOException;
3434
import java.util.Set;
3535

36+
import static org.hamcrest.Matchers.is;
37+
import static org.hamcrest.Matchers.nullValue;
38+
3639
public class IndexMetaDataTests extends ESTestCase {
3740

3841
public void testIndexMetaDataSerialization() throws IOException {
@@ -122,4 +125,36 @@ public void testSelectShrinkShards() {
122125
assertEquals("the number of target shards (8) must be greater than the shard id: 8",
123126
expectThrows(IllegalArgumentException.class, () -> IndexMetaData.selectShrinkShards(8, metaData, 8)).getMessage());
124127
}
128+
129+
public void testIndexFormat() {
130+
Settings defaultSettings = Settings.builder()
131+
.put("index.version.created", 1)
132+
.put("index.number_of_shards", 1)
133+
.put("index.number_of_replicas", 1)
134+
.build();
135+
136+
// matching version
137+
{
138+
IndexMetaData metaData = IndexMetaData.builder("foo")
139+
.settings(Settings.builder()
140+
.put(defaultSettings)
141+
// intentionally not using the constant, so upgrading requires you to look at this test
142+
// where you have to update this part and the next one
143+
.put("index.format", 6)
144+
.build())
145+
.build();
146+
147+
assertThat(metaData.getSettings().getAsInt(IndexMetaData.INDEX_FORMAT_SETTING.getKey(), 0), is(6));
148+
}
149+
150+
// no setting configured
151+
{
152+
IndexMetaData metaData = IndexMetaData.builder("foo")
153+
.settings(Settings.builder()
154+
.put(defaultSettings)
155+
.build())
156+
.build();
157+
assertThat(metaData.getSettings().getAsInt(IndexMetaData.INDEX_FORMAT_SETTING.getKey(), 0), is(0));
158+
}
159+
}
125160
}

0 commit comments

Comments
 (0)