Skip to content

Commit 291ced7

Browse files
authored
Change from Version to BuildVersion in PersistedClusterStateService (#115301)
1 parent 387062e commit 291ced7

File tree

9 files changed

+32
-46
lines changed

9 files changed

+32
-46
lines changed

server/src/internalClusterTest/java/org/elasticsearch/env/NodeEnvironmentIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Settings onNodeStopped(String nodeName) {
123123

124124
public void testFailsToStartIfDowngraded() {
125125
final IllegalStateException illegalStateException = expectThrowsOnRestart(
126-
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooNewVersion(), dataPaths)
126+
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooNewBuildVersion(), dataPaths)
127127
);
128128
assertThat(
129129
illegalStateException.getMessage(),
@@ -133,7 +133,7 @@ public void testFailsToStartIfDowngraded() {
133133

134134
public void testFailsToStartIfUpgradedTooFar() {
135135
final IllegalStateException illegalStateException = expectThrowsOnRestart(
136-
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooOldVersion(), dataPaths)
136+
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooOldBuildVersion(), dataPaths)
137137
);
138138
assertThat(
139139
illegalStateException.getMessage(),

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public abstract class BuildVersion {
5959
public abstract boolean isFutureVersion();
6060

6161
// temporary
62-
// TODO[wrb]: remove from PersistedClusterStateService
6362
// TODO[wrb]: remove from security bootstrap checks
6463
@Deprecated
6564
public Version toVersion() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ public int hashCode() {
7272

7373
@Override
7474
public String toString() {
75-
return Version.fromId(versionId).toString();
75+
return version.toString();
7676
}
7777
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public final class NodeMetadata {
4242

4343
private final IndexVersion oldestIndexVersion;
4444

45-
@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA) // version should be non-null in the node metadata from v9 onwards
4645
private NodeMetadata(
4746
final String nodeId,
4847
final BuildVersion buildVersion,
@@ -112,11 +111,7 @@ public IndexVersion oldestIndexVersion() {
112111
return oldestIndexVersion;
113112
}
114113

115-
@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA)
116114
public void verifyUpgradeToCurrentVersion() {
117-
// Enable the following assertion for V9:
118-
// assert (nodeVersion.equals(BuildVersion.empty()) == false) : "version is required in the node metadata from v9 onwards";
119-
120115
if (nodeVersion.onOrAfterMinimumCompatible() == false) {
121116
throw new IllegalStateException(
122117
"cannot upgrade a node from version ["

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void processDataPaths(Terminal terminal, Path[] paths, OptionSet optio
7474
"found ["
7575
+ nodeMetadata
7676
+ "] which is compatible with current version ["
77-
+ Version.CURRENT
77+
+ BuildVersion.current()
7878
+ "], so there is no need to override the version checks"
7979
);
8080
} catch (IllegalStateException e) {
@@ -86,10 +86,10 @@ protected void processDataPaths(Terminal terminal, Path[] paths, OptionSet optio
8686
(nodeMetadata.nodeVersion().onOrAfterMinimumCompatible() == false ? TOO_OLD_MESSAGE : TOO_NEW_MESSAGE).replace(
8787
"V_OLD",
8888
nodeMetadata.nodeVersion().toString()
89-
).replace("V_NEW", nodeMetadata.nodeVersion().toString()).replace("V_CUR", Version.CURRENT.toString())
89+
).replace("V_NEW", nodeMetadata.nodeVersion().toString()).replace("V_CUR", BuildVersion.current().toString())
9090
);
9191

92-
PersistedClusterStateService.overrideVersion(Version.CURRENT, paths);
92+
PersistedClusterStateService.overrideVersion(BuildVersion.current(), paths);
9393

9494
terminal.println(SUCCESS_MESSAGE);
9595
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.apache.lucene.util.BytesRef;
4343
import org.apache.lucene.util.InfoStream;
4444
import org.apache.lucene.util.SetOnce;
45-
import org.elasticsearch.Version;
4645
import org.elasticsearch.cluster.ClusterState;
4746
import org.elasticsearch.cluster.metadata.IndexMetadata;
4847
import org.elasticsearch.cluster.metadata.MappingMetadata;
@@ -159,8 +158,6 @@ public class PersistedClusterStateService {
159158
public static final int IS_LAST_PAGE = 1;
160159
public static final int IS_NOT_LAST_PAGE = 0;
161160
private static final int COMMIT_DATA_SIZE = 7;
162-
// We added CLUSTER_UUID_KEY and CLUSTER_UUID_COMMITTED_KEY in 8.8
163-
private static final int COMMIT_DATA_SIZE_BEFORE_8_8 = 5;
164161

165162
private static final MergePolicy NO_MERGE_POLICY = noMergePolicy();
166163
private static final MergePolicy DEFAULT_MERGE_POLICY = defaultMergePolicy();
@@ -350,7 +347,7 @@ public record OnDiskStateMetadata(
350347
@Nullable
351348
public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
352349
String nodeId = null;
353-
Version version = null;
350+
BuildVersion version = null;
354351
IndexVersion oldestIndexVersion = IndexVersions.ZERO;
355352
for (final Path dataPath : dataPaths) {
356353
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
@@ -367,7 +364,7 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
367364
);
368365
} else if (nodeId == null) {
369366
nodeId = thisNodeId;
370-
version = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
367+
version = BuildVersion.fromVersionId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
371368
if (userData.containsKey(OLDEST_INDEX_VERSION_KEY)) {
372369
oldestIndexVersion = IndexVersion.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY)));
373370
} else {
@@ -382,14 +379,13 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
382379
if (nodeId == null) {
383380
return null;
384381
}
385-
// TODO: remove use of Version here (ES-7343)
386-
return new NodeMetadata(nodeId, BuildVersion.fromVersionId(version.id()), oldestIndexVersion);
382+
return new NodeMetadata(nodeId, version, oldestIndexVersion);
387383
}
388384

389385
/**
390386
* Overrides the version field for the metadata in the given data path
391387
*/
392-
public static void overrideVersion(Version newVersion, Path... dataPaths) throws IOException {
388+
public static void overrideVersion(BuildVersion newVersion, Path... dataPaths) throws IOException {
393389
for (final Path dataPath : dataPaths) {
394390
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
395391
if (Files.exists(indexPath)) {
@@ -399,7 +395,7 @@ public static void overrideVersion(Version newVersion, Path... dataPaths) throws
399395

400396
try (IndexWriter indexWriter = createIndexWriter(new NIOFSDirectory(dataPath.resolve(METADATA_DIRECTORY_NAME)), true)) {
401397
final Map<String, String> commitData = new HashMap<>(userData);
402-
commitData.put(NODE_VERSION_KEY, Integer.toString(newVersion.id));
398+
commitData.put(NODE_VERSION_KEY, Integer.toString(newVersion.id()));
403399
commitData.put(OVERRIDDEN_NODE_VERSION_KEY, Boolean.toString(true));
404400
indexWriter.setLiveCommitData(commitData.entrySet());
405401
indexWriter.commit();
@@ -664,11 +660,9 @@ public OnDiskStateMetadata loadOnDiskStateMetadataFromUserData(Map<String, Strin
664660
assert userData.get(LAST_ACCEPTED_VERSION_KEY) != null;
665661
assert userData.get(NODE_ID_KEY) != null;
666662
assert userData.get(NODE_VERSION_KEY) != null;
667-
var nodeVersion = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
668-
assert userData.get(CLUSTER_UUID_KEY) != null || nodeVersion.before(Version.V_8_8_0);
669-
assert userData.get(CLUSTER_UUID_COMMITTED_KEY) != null || nodeVersion.before(Version.V_8_8_0);
670-
assert userData.get(OVERRIDDEN_NODE_VERSION_KEY) != null
671-
|| userData.size() == (nodeVersion.onOrAfter(Version.V_8_8_0) ? COMMIT_DATA_SIZE : COMMIT_DATA_SIZE_BEFORE_8_8) : userData;
663+
assert userData.get(CLUSTER_UUID_KEY) != null;
664+
assert userData.get(CLUSTER_UUID_COMMITTED_KEY) != null;
665+
assert userData.get(OVERRIDDEN_NODE_VERSION_KEY) != null || userData.size() == COMMIT_DATA_SIZE;
672666
return new OnDiskStateMetadata(
673667
Long.parseLong(userData.get(CURRENT_TERM_KEY)),
674668
Long.parseLong(userData.get(LAST_ACCEPTED_VERSION_KEY)),
@@ -858,7 +852,7 @@ void prepareCommit(
858852
final Map<String, String> commitData = Maps.newMapWithExpectedSize(COMMIT_DATA_SIZE);
859853
commitData.put(CURRENT_TERM_KEY, Long.toString(currentTerm));
860854
commitData.put(LAST_ACCEPTED_VERSION_KEY, Long.toString(lastAcceptedVersion));
861-
commitData.put(NODE_VERSION_KEY, Integer.toString(Version.CURRENT.id));
855+
commitData.put(NODE_VERSION_KEY, Integer.toString(BuildVersion.current().id()));
862856
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id()));
863857
commitData.put(NODE_ID_KEY, nodeId);
864858
commitData.put(CLUSTER_UUID_KEY, clusterUUID);

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,6 @@ public void testUpgradeMarksPreviousVersion() {
167167
assertThat(nodeMetadata.previousNodeVersion(), equalTo(buildVersion));
168168
}
169169

170-
public static Version tooNewVersion() {
171-
return Version.fromId(between(Version.CURRENT.id + 1, 99999999));
172-
}
173-
174170
public static IndexVersion tooNewIndexVersion() {
175171
return IndexVersion.fromId(between(IndexVersion.current().id() + 1, 99999999));
176172
}
@@ -179,10 +175,6 @@ public static BuildVersion tooNewBuildVersion() {
179175
return BuildVersion.fromVersionId(between(Version.CURRENT.id() + 1, 99999999));
180176
}
181177

182-
public static Version tooOldVersion() {
183-
return Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
184-
}
185-
186178
public static BuildVersion tooOldBuildVersion() {
187179
return BuildVersion.fromVersionId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
188180
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public void testFailsOnEmptyPath() {
9696
}
9797

9898
public void testFailsIfUnnecessary() throws IOException {
99-
final Version nodeVersion = Version.fromId(between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id));
99+
final BuildVersion nodeVersion = BuildVersion.fromVersionId(
100+
between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id)
101+
);
100102
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
101103
final MockTerminal mockTerminal = MockTerminal.create();
102104
final ElasticsearchException elasticsearchException = expectThrows(
@@ -107,15 +109,15 @@ public void testFailsIfUnnecessary() throws IOException {
107109
elasticsearchException.getMessage(),
108110
allOf(
109111
containsString("compatible with current version"),
110-
containsString(Version.CURRENT.toString()),
112+
containsString(BuildVersion.current().toString()),
111113
containsString(nodeVersion.toString())
112114
)
113115
);
114116
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
115117
}
116118

117119
public void testWarnsIfTooOld() throws Exception {
118-
final Version nodeVersion = NodeMetadataTests.tooOldVersion();
120+
final BuildVersion nodeVersion = NodeMetadataTests.tooOldBuildVersion();
119121
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
120122
final MockTerminal mockTerminal = MockTerminal.create();
121123
mockTerminal.addTextInput("n");
@@ -137,11 +139,11 @@ public void testWarnsIfTooOld() throws Exception {
137139
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
138140

139141
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPaths);
140-
assertThat(nodeMetadata.nodeVersion().toVersion(), equalTo(nodeVersion));
142+
assertThat(nodeMetadata.nodeVersion(), equalTo(nodeVersion));
141143
}
142144

143145
public void testWarnsIfTooNew() throws Exception {
144-
final Version nodeVersion = NodeMetadataTests.tooNewVersion();
146+
final BuildVersion nodeVersion = NodeMetadataTests.tooNewBuildVersion();
145147
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
146148
final MockTerminal mockTerminal = MockTerminal.create();
147149
mockTerminal.addTextInput(randomFrom("yy", "Yy", "n", "yes", "true", "N", "no"));
@@ -162,11 +164,11 @@ public void testWarnsIfTooNew() throws Exception {
162164
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
163165

164166
final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPaths);
165-
assertThat(nodeMetadata.nodeVersion().toVersion(), equalTo(nodeVersion));
167+
assertThat(nodeMetadata.nodeVersion(), equalTo(nodeVersion));
166168
}
167169

168170
public void testOverwritesIfTooOld() throws Exception {
169-
final Version nodeVersion = NodeMetadataTests.tooOldVersion();
171+
final BuildVersion nodeVersion = NodeMetadataTests.tooOldBuildVersion();
170172
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
171173
final MockTerminal mockTerminal = MockTerminal.create();
172174
mockTerminal.addTextInput(randomFrom("y", "Y"));
@@ -189,7 +191,7 @@ public void testOverwritesIfTooOld() throws Exception {
189191
}
190192

191193
public void testOverwritesIfTooNew() throws Exception {
192-
final Version nodeVersion = NodeMetadataTests.tooNewVersion();
194+
final BuildVersion nodeVersion = NodeMetadataTests.tooNewBuildVersion();
193195
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
194196
final MockTerminal mockTerminal = MockTerminal.create();
195197
mockTerminal.addTextInput(randomFrom("y", "Y"));

server/src/test/java/org/elasticsearch/gateway/PersistedClusterStateServiceTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.elasticsearch.common.unit.ByteSizeValue;
5555
import org.elasticsearch.core.IOUtils;
5656
import org.elasticsearch.core.TimeValue;
57+
import org.elasticsearch.core.UpdateForV9;
5758
import org.elasticsearch.env.BuildVersion;
5859
import org.elasticsearch.env.Environment;
5960
import org.elasticsearch.env.NodeEnvironment;
@@ -1414,14 +1415,17 @@ public void testOverrideLuceneVersion() throws IOException {
14141415
assertThat(clusterState.metadata().version(), equalTo(version));
14151416

14161417
}
1418+
@UpdateForV9(owner = UpdateForV9.Owner.SEARCH_FOUNDATIONS)
1419+
BuildVersion overrideVersion = BuildVersion.fromVersionId(Version.V_8_0_0.id);
1420+
14171421
NodeMetadata prevMetadata = PersistedClusterStateService.nodeMetadata(persistedClusterStateService.getDataPaths());
14181422
assertEquals(BuildVersion.current(), prevMetadata.nodeVersion());
1419-
PersistedClusterStateService.overrideVersion(Version.V_8_0_0, persistedClusterStateService.getDataPaths());
1423+
PersistedClusterStateService.overrideVersion(overrideVersion, persistedClusterStateService.getDataPaths());
14201424
NodeMetadata metadata = PersistedClusterStateService.nodeMetadata(persistedClusterStateService.getDataPaths());
1421-
assertEquals(BuildVersion.fromVersionId(Version.V_8_0_0.id()), metadata.nodeVersion());
1425+
assertEquals(overrideVersion, metadata.nodeVersion());
14221426
for (Path p : persistedClusterStateService.getDataPaths()) {
14231427
NodeMetadata individualMetadata = PersistedClusterStateService.nodeMetadata(p);
1424-
assertEquals(BuildVersion.fromVersionId(Version.V_8_0_0.id()), individualMetadata.nodeVersion());
1428+
assertEquals(overrideVersion, individualMetadata.nodeVersion());
14251429
}
14261430
}
14271431
}

0 commit comments

Comments
 (0)