Skip to content

Commit ac7d791

Browse files
authored
Convert NodeMetadata.oldestIndexVersion to IndexVersion (#96819)
1 parent 857cd50 commit ac7d791

File tree

11 files changed

+97
-61
lines changed

11 files changed

+97
-61
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.logging.log4j.Logger;
1313
import org.apache.lucene.util.CollectionUtil;
1414
import org.elasticsearch.TransportVersion;
15-
import org.elasticsearch.Version;
1615
import org.elasticsearch.cluster.ClusterState;
1716
import org.elasticsearch.cluster.Diff;
1817
import org.elasticsearch.cluster.Diffable;
@@ -50,6 +49,7 @@
5049
import org.elasticsearch.index.IndexMode;
5150
import org.elasticsearch.index.IndexNotFoundException;
5251
import org.elasticsearch.index.IndexSettings;
52+
import org.elasticsearch.index.IndexVersion;
5353
import org.elasticsearch.plugins.MapperPlugin;
5454
import org.elasticsearch.rest.RestStatus;
5555
import org.elasticsearch.transport.Transports;
@@ -231,7 +231,7 @@ default boolean isRestorable() {
231231
private volatile SortedMap<String, IndexAbstraction> indicesLookup;
232232
private final Map<String, MappingMetadata> mappingsByHash;
233233

234-
private final Version oldestIndexVersion;
234+
private final IndexVersion oldestIndexVersion;
235235

236236
private Metadata(
237237
String clusterUUID,
@@ -256,7 +256,7 @@ private Metadata(
256256
String[] visibleClosedIndices,
257257
SortedMap<String, IndexAbstraction> indicesLookup,
258258
Map<String, MappingMetadata> mappingsByHash,
259-
Version oldestIndexVersion,
259+
IndexVersion oldestIndexVersion,
260260
Map<String, ReservedStateMetadata> reservedStateMetadata
261261
) {
262262
this.clusterUUID = clusterUUID;
@@ -638,7 +638,7 @@ public Metadata withAddedIndex(IndexMetadata index) {
638638
updatedVisibleClosedIndices,
639639
null,
640640
updatedMappingsByHash,
641-
index.getCompatibilityVersion().before(oldestIndexVersion) ? index.getCompatibilityVersion() : oldestIndexVersion,
641+
IndexVersion.min(IndexVersion.fromId(index.getCompatibilityVersion().id), oldestIndexVersion),
642642
reservedStateMetadata
643643
);
644644
}
@@ -719,7 +719,7 @@ public CoordinationMetadata coordinationMetadata() {
719719
return this.coordinationMetadata;
720720
}
721721

722-
public Version oldestIndexVersion() {
722+
public IndexVersion oldestIndexVersion() {
723723
return this.oldestIndexVersion;
724724
}
725725

@@ -2272,7 +2272,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
22722272
final List<String> visibleClosedIndices = new ArrayList<>();
22732273
final ImmutableOpenMap<String, IndexMetadata> indicesMap = indices.build();
22742274

2275-
int oldestIndexVersionId = Version.CURRENT.id;
2275+
int oldestIndexVersionId = IndexVersion.CURRENT.id();
22762276
int totalNumberOfShards = 0;
22772277
int totalOpenIndexShards = 0;
22782278

@@ -2362,7 +2362,7 @@ public Metadata build(boolean skipNameCollisionChecks) {
23622362
visibleClosedIndicesArray,
23632363
indicesLookup,
23642364
Collections.unmodifiableMap(mappingsByHash),
2365-
Version.fromId(oldestIndexVersionId),
2365+
IndexVersion.fromId(oldestIndexVersionId),
23662366
Collections.unmodifiableMap(reservedStateMetadata)
23672367
);
23682368
}

server/src/main/java/org/elasticsearch/env/NodeEnvironment.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.gateway.PersistedClusterStateService;
4848
import org.elasticsearch.index.Index;
4949
import org.elasticsearch.index.IndexSettings;
50+
import org.elasticsearch.index.IndexVersion;
5051
import org.elasticsearch.index.shard.ShardId;
5152
import org.elasticsearch.index.shard.ShardPath;
5253
import org.elasticsearch.index.store.FsDirectoryFactory;
@@ -523,13 +524,13 @@ static void checkForIndexCompatibility(Logger logger, DataPath... dataPaths) thr
523524

524525
if (metadata.oldestIndexVersion().isLegacyIndexVersion()) {
525526
throw new IllegalStateException(
526-
"Cannot start this node because it holds metadata for indices created with version ["
527+
"Cannot start this node because it holds metadata for indices with version ["
527528
+ metadata.oldestIndexVersion()
528529
+ "] with which this node of version ["
529530
+ Version.CURRENT
530531
+ "] is incompatible. Revert this node to version ["
531532
+ Version.max(Version.CURRENT.minimumCompatibilityVersion(), metadata.previousNodeVersion())
532-
+ "] and delete any indices created in versions earlier than ["
533+
+ "] and delete any indices with versions earlier than ["
533534
+ Version.CURRENT.minimumIndexCompatibilityVersion()
534535
+ "] before upgrading to version ["
535536
+ Version.CURRENT
@@ -623,7 +624,7 @@ private static NodeMetadata loadNodeMetadata(Settings settings, Logger logger, D
623624
assert nodeIds.isEmpty() : nodeIds;
624625
// If we couldn't find legacy metadata, we set the latest index version to this version. This happens
625626
// when we are starting a new node and there are no indices to worry about.
626-
metadata = new NodeMetadata(generateNodeId(settings), Version.CURRENT, Version.CURRENT);
627+
metadata = new NodeMetadata(generateNodeId(settings), Version.CURRENT, IndexVersion.CURRENT);
627628
} else {
628629
assert nodeIds.equals(Collections.singleton(legacyMetadata.nodeId())) : nodeIds + " doesn't match " + legacyMetadata;
629630
metadata = legacyMetadata;

server/src/main/java/org/elasticsearch/env/NodeMetadata.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.elasticsearch.Version;
1212
import org.elasticsearch.gateway.MetadataStateFormat;
13+
import org.elasticsearch.index.IndexVersion;
1314
import org.elasticsearch.xcontent.ObjectParser;
1415
import org.elasticsearch.xcontent.ParseField;
1516
import org.elasticsearch.xcontent.XContentBuilder;
@@ -36,21 +37,21 @@ public final class NodeMetadata {
3637

3738
private final Version previousNodeVersion;
3839

39-
private final Version oldestIndexVersion;
40+
private final IndexVersion oldestIndexVersion;
4041

4142
private NodeMetadata(
4243
final String nodeId,
4344
final Version nodeVersion,
4445
final Version previousNodeVersion,
45-
final Version oldestIndexVersion
46+
final IndexVersion oldestIndexVersion
4647
) {
4748
this.nodeId = Objects.requireNonNull(nodeId);
4849
this.nodeVersion = Objects.requireNonNull(nodeVersion);
4950
this.previousNodeVersion = Objects.requireNonNull(previousNodeVersion);
5051
this.oldestIndexVersion = Objects.requireNonNull(oldestIndexVersion);
5152
}
5253

53-
public NodeMetadata(final String nodeId, final Version nodeVersion, final Version oldestIndexVersion) {
54+
public NodeMetadata(final String nodeId, final Version nodeVersion, final IndexVersion oldestIndexVersion) {
5455
this(nodeId, nodeVersion, nodeVersion, oldestIndexVersion);
5556
}
5657

@@ -103,7 +104,7 @@ public Version previousNodeVersion() {
103104
return previousNodeVersion;
104105
}
105106

106-
public Version oldestIndexVersion() {
107+
public IndexVersion oldestIndexVersion() {
107108
return oldestIndexVersion;
108109
}
109110

@@ -141,7 +142,7 @@ private static class Builder {
141142
String nodeId;
142143
Version nodeVersion;
143144
Version previousNodeVersion;
144-
Version oldestIndexVersion;
145+
IndexVersion oldestIndexVersion;
145146

146147
public void setNodeId(String nodeId) {
147148
this.nodeId = nodeId;
@@ -156,12 +157,12 @@ public void setPreviousNodeVersionId(int previousNodeVersionId) {
156157
}
157158

158159
public void setOldestIndexVersion(int oldestIndexVersion) {
159-
this.oldestIndexVersion = Version.fromId(oldestIndexVersion);
160+
this.oldestIndexVersion = IndexVersion.fromId(oldestIndexVersion);
160161
}
161162

162163
public NodeMetadata build() {
163164
final Version nodeVersion;
164-
final Version oldestIndexVersion;
165+
final IndexVersion oldestIndexVersion;
165166
if (this.nodeVersion == null) {
166167
assert Version.CURRENT.major <= Version.V_7_0_0.major + 1 : "version is required in the node metadata from v9 onwards";
167168
nodeVersion = Version.V_EMPTY;
@@ -172,7 +173,7 @@ public NodeMetadata build() {
172173
previousNodeVersion = nodeVersion;
173174
}
174175
if (this.oldestIndexVersion == null) {
175-
oldestIndexVersion = Version.V_EMPTY;
176+
oldestIndexVersion = IndexVersion.ZERO;
176177
} else {
177178
oldestIndexVersion = this.oldestIndexVersion;
178179
}
@@ -208,7 +209,7 @@ protected XContentBuilder newXContentBuilder(XContentType type, OutputStream str
208209
public void toXContent(XContentBuilder builder, NodeMetadata nodeMetadata) throws IOException {
209210
builder.field(NODE_ID_KEY, nodeMetadata.nodeId);
210211
builder.field(NODE_VERSION_KEY, nodeMetadata.nodeVersion.id);
211-
builder.field(OLDEST_INDEX_VERSION_KEY, nodeMetadata.oldestIndexVersion.id);
212+
builder.field(OLDEST_INDEX_VERSION_KEY, nodeMetadata.oldestIndexVersion.id());
212213
}
213214

214215
@Override

server/src/main/java/org/elasticsearch/gateway/PersistedClusterStateService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import org.elasticsearch.core.TimeValue;
7171
import org.elasticsearch.env.NodeEnvironment;
7272
import org.elasticsearch.env.NodeMetadata;
73+
import org.elasticsearch.index.IndexVersion;
7374
import org.elasticsearch.xcontent.NamedXContentRegistry;
7475
import org.elasticsearch.xcontent.ToXContent;
7576
import org.elasticsearch.xcontent.XContentBuilder;
@@ -343,7 +344,7 @@ public record OnDiskStateMetadata(
343344
public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
344345
String nodeId = null;
345346
Version version = null;
346-
Version oldestIndexVersion = Version.V_EMPTY;
347+
IndexVersion oldestIndexVersion = IndexVersion.ZERO;
347348
for (final Path dataPath : dataPaths) {
348349
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
349350
if (Files.exists(indexPath)) {
@@ -361,9 +362,9 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
361362
nodeId = thisNodeId;
362363
version = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
363364
if (userData.containsKey(OLDEST_INDEX_VERSION_KEY)) {
364-
oldestIndexVersion = Version.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY)));
365+
oldestIndexVersion = IndexVersion.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY)));
365366
} else {
366-
oldestIndexVersion = Version.V_EMPTY;
367+
oldestIndexVersion = IndexVersion.ZERO;
367368
}
368369
}
369370
} catch (IndexNotFoundException e) {
@@ -835,7 +836,7 @@ void prepareCommit(
835836
String nodeId,
836837
long currentTerm,
837838
long lastAcceptedVersion,
838-
Version oldestIndexVersion,
839+
IndexVersion oldestIndexVersion,
839840
String clusterUUID,
840841
boolean clusterUUIDCommitted
841842
) throws IOException {
@@ -846,7 +847,7 @@ void prepareCommit(
846847
commitData.put(CURRENT_TERM_KEY, Long.toString(currentTerm));
847848
commitData.put(LAST_ACCEPTED_VERSION_KEY, Long.toString(lastAcceptedVersion));
848849
commitData.put(NODE_VERSION_KEY, Integer.toString(Version.CURRENT.id));
849-
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id));
850+
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id()));
850851
commitData.put(NODE_ID_KEY, nodeId);
851852
commitData.put(CLUSTER_UUID_KEY, clusterUUID);
852853
commitData.put(CLUSTER_UUID_COMMITTED_KEY, Boolean.toString(clusterUUIDCommitted));
@@ -1211,7 +1212,7 @@ private WriterStats addMetadata(Metadata metadata) throws IOException {
12111212
public void writeIncrementalTermUpdateAndCommit(
12121213
long currentTerm,
12131214
long lastAcceptedVersion,
1214-
Version oldestIndexVersion,
1215+
IndexVersion oldestIndexVersion,
12151216
String clusterUUID,
12161217
boolean clusterUUIDCommitted
12171218
) throws IOException {
@@ -1223,7 +1224,7 @@ public void writeIncrementalTermUpdateAndCommit(
12231224
void commit(
12241225
long currentTerm,
12251226
long lastAcceptedVersion,
1226-
Version oldestIndexVersion,
1227+
IndexVersion oldestIndexVersion,
12271228
String clusterUUID,
12281229
boolean clusterUUIDCommitted
12291230
) throws IOException {
@@ -1251,7 +1252,7 @@ private boolean assertOnCommit() {
12511252
private void prepareCommit(
12521253
long currentTerm,
12531254
long lastAcceptedVersion,
1254-
Version oldestIndexVersion,
1255+
IndexVersion oldestIndexVersion,
12551256
String clusterUUID,
12561257
boolean clusterUUIDCommitted
12571258
) throws IOException {

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.index.Index;
3434
import org.elasticsearch.index.IndexNotFoundException;
3535
import org.elasticsearch.index.IndexSettings;
36+
import org.elasticsearch.index.IndexVersion;
3637
import org.elasticsearch.index.alias.RandomAliasActionsGenerator;
3738
import org.elasticsearch.index.mapper.MapperService;
3839
import org.elasticsearch.plugins.MapperPlugin;
@@ -765,28 +766,29 @@ public void testFindMappingsWithFilters() throws IOException {
765766

766767
public void testOldestIndexComputation() {
767768
Metadata metadata = buildIndicesWithVersions(
768-
new Version[] { Version.V_7_0_0, Version.CURRENT, Version.fromId(Version.CURRENT.id + 1) }
769+
IndexVersion.V_7_0_0,
770+
IndexVersion.CURRENT,
771+
IndexVersion.fromId(IndexVersion.CURRENT.id() + 1)
769772
).build();
770773

771-
assertEquals(Version.V_7_0_0, metadata.oldestIndexVersion());
774+
assertEquals(IndexVersion.V_7_0_0, metadata.oldestIndexVersion());
772775

773776
Metadata.Builder b = Metadata.builder();
774-
assertEquals(Version.CURRENT, b.build().oldestIndexVersion());
777+
assertEquals(IndexVersion.CURRENT, b.build().oldestIndexVersion());
775778

776779
Throwable ex = expectThrows(
777780
IllegalArgumentException.class,
778-
() -> buildIndicesWithVersions(new Version[] { Version.V_7_0_0, Version.V_EMPTY, Version.fromId(Version.CURRENT.id + 1) })
781+
() -> buildIndicesWithVersions(IndexVersion.V_7_0_0, IndexVersion.ZERO, IndexVersion.fromId(IndexVersion.CURRENT.id() + 1))
779782
.build()
780783
);
781784

782785
assertEquals("[index.version.created] is not present in the index settings for index with UUID [null]", ex.getMessage());
783786
}
784787

785-
private Metadata.Builder buildIndicesWithVersions(Version... indexVersions) {
786-
788+
private Metadata.Builder buildIndicesWithVersions(IndexVersion... indexVersions) {
787789
int lastIndexNum = randomIntBetween(9, 50);
788790
Metadata.Builder b = Metadata.builder();
789-
for (Version indexVersion : indexVersions) {
791+
for (IndexVersion indexVersion : indexVersions) {
790792
IndexMetadata im = IndexMetadata.builder(DataStream.getDefaultBackingIndexName("index", lastIndexNum))
791793
.settings(settings(indexVersion))
792794
.numberOfShards(1)
@@ -801,7 +803,7 @@ private Metadata.Builder buildIndicesWithVersions(Version... indexVersions) {
801803

802804
private static IndexMetadata.Builder buildIndexMetadata(String name, String alias, Boolean writeIndex) {
803805
return IndexMetadata.builder(name)
804-
.settings(settings(Version.CURRENT))
806+
.settings(settings(IndexVersion.CURRENT))
805807
.creationDate(randomNonNegativeLong())
806808
.putAlias(AliasMetadata.builder(alias).writeIndex(writeIndex))
807809
.numberOfShards(1)

server/src/test/java/org/elasticsearch/env/NodeEnvironmentTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.gateway.PersistedClusterStateService;
3333
import org.elasticsearch.index.Index;
3434
import org.elasticsearch.index.IndexSettings;
35+
import org.elasticsearch.index.IndexVersion;
3536
import org.elasticsearch.index.shard.ShardId;
3637
import org.elasticsearch.test.ESTestCase;
3738
import org.elasticsearch.test.IndexSettingsModule;
@@ -580,7 +581,8 @@ public void testIndexCompatibilityChecks() throws IOException {
580581
);
581582
}
582583

583-
Version oldIndexVersion = Version.fromId(between(1, Version.CURRENT.minimumIndexCompatibilityVersion().id - 1));
584+
Version oldVersion = Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
585+
IndexVersion oldIndexVersion = IndexVersion.fromId(between(1, IndexVersion.MINIMUM_COMPATIBLE.id() - 1));
584586
Version previousNodeVersion = Version.fromId(between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id - 1));
585587
overrideOldestIndexVersion(oldIndexVersion, previousNodeVersion, env.nodeDataPaths());
586588

@@ -594,29 +596,29 @@ public void testIndexCompatibilityChecks() throws IOException {
594596
ex.getMessage(),
595597
allOf(
596598
containsString("Cannot start this node"),
597-
containsString("it holds metadata for indices created with version [" + oldIndexVersion + "]"),
599+
containsString("it holds metadata for indices with version [" + oldIndexVersion + "]"),
598600
containsString("Revert this node to version [" + previousNodeVersion + "]")
599601
)
600602
);
601603

602604
// This should work
603-
overrideOldestIndexVersion(Version.CURRENT.minimumIndexCompatibilityVersion(), previousNodeVersion, env.nodeDataPaths());
605+
overrideOldestIndexVersion(IndexVersion.MINIMUM_COMPATIBLE, previousNodeVersion, env.nodeDataPaths());
604606
checkForIndexCompatibility(logger, env.dataPaths());
605607

606608
// Trying to boot with newer version should pass this check
607-
overrideOldestIndexVersion(NodeMetadataTests.tooNewVersion(), previousNodeVersion, env.nodeDataPaths());
609+
overrideOldestIndexVersion(NodeMetadataTests.tooNewIndexVersion(), previousNodeVersion, env.nodeDataPaths());
608610
checkForIndexCompatibility(logger, env.dataPaths());
609611

610612
// Simulate empty old index version, attempting to upgrade before 7.17
611-
removeOldestIndexVersion(oldIndexVersion, env.nodeDataPaths());
613+
removeOldestIndexVersion(oldVersion, env.nodeDataPaths());
612614

613615
ex = expectThrows(
614616
IllegalStateException.class,
615617
"Must fail the check on index that's too old",
616618
() -> checkForIndexCompatibility(logger, env.dataPaths())
617619
);
618620

619-
assertThat(ex.getMessage(), startsWith("cannot upgrade a node from version [" + oldIndexVersion + "] directly"));
621+
assertThat(ex.getMessage(), startsWith("cannot upgrade a node from version [" + oldVersion + "] directly"));
620622
assertThat(ex.getMessage(), containsString("upgrade to version [" + Version.CURRENT.minimumCompatibilityVersion()));
621623
}
622624
}
@@ -714,7 +716,7 @@ public NodeEnvironment newNodeEnvironment(String[] dataPaths, String sharedDataP
714716
return new NodeEnvironment(build, TestEnvironment.newEnvironment(build));
715717
}
716718

717-
private static void overrideOldestIndexVersion(Version oldestIndexVersion, Version previousNodeVersion, Path... dataPaths)
719+
private static void overrideOldestIndexVersion(IndexVersion oldestIndexVersion, Version previousNodeVersion, Path... dataPaths)
718720
throws IOException {
719721
for (final Path dataPath : dataPaths) {
720722
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
@@ -731,7 +733,7 @@ private static void overrideOldestIndexVersion(Version oldestIndexVersion, Versi
731733
) {
732734
final Map<String, String> commitData = new HashMap<>(userData);
733735
commitData.put(NODE_VERSION_KEY, Integer.toString(previousNodeVersion.id));
734-
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id));
736+
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id()));
735737
indexWriter.setLiveCommitData(commitData.entrySet());
736738
indexWriter.commit();
737739
}

0 commit comments

Comments
 (0)