Skip to content

Commit

Permalink
Change from Version to BuildVersion in PersistedClusterStateService (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoop authored Oct 23, 2024
1 parent 387062e commit 291ced7
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public Settings onNodeStopped(String nodeName) {

public void testFailsToStartIfDowngraded() {
final IllegalStateException illegalStateException = expectThrowsOnRestart(
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooNewVersion(), dataPaths)
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooNewBuildVersion(), dataPaths)
);
assertThat(
illegalStateException.getMessage(),
Expand All @@ -133,7 +133,7 @@ public void testFailsToStartIfDowngraded() {

public void testFailsToStartIfUpgradedTooFar() {
final IllegalStateException illegalStateException = expectThrowsOnRestart(
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooOldVersion(), dataPaths)
dataPaths -> PersistedClusterStateService.overrideVersion(NodeMetadataTests.tooOldBuildVersion(), dataPaths)
);
assertThat(
illegalStateException.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public abstract class BuildVersion {
public abstract boolean isFutureVersion();

// temporary
// TODO[wrb]: remove from PersistedClusterStateService
// TODO[wrb]: remove from security bootstrap checks
@Deprecated
public Version toVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public int hashCode() {

@Override
public String toString() {
return Version.fromId(versionId).toString();
return version.toString();
}
}
5 changes: 0 additions & 5 deletions server/src/main/java/org/elasticsearch/env/NodeMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public final class NodeMetadata {

private final IndexVersion oldestIndexVersion;

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

@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA)
public void verifyUpgradeToCurrentVersion() {
// Enable the following assertion for V9:
// assert (nodeVersion.equals(BuildVersion.empty()) == false) : "version is required in the node metadata from v9 onwards";

if (nodeVersion.onOrAfterMinimumCompatible() == false) {
throw new IllegalStateException(
"cannot upgrade a node from version ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void processDataPaths(Terminal terminal, Path[] paths, OptionSet optio
"found ["
+ nodeMetadata
+ "] which is compatible with current version ["
+ Version.CURRENT
+ BuildVersion.current()
+ "], so there is no need to override the version checks"
);
} catch (IllegalStateException e) {
Expand All @@ -86,10 +86,10 @@ protected void processDataPaths(Terminal terminal, Path[] paths, OptionSet optio
(nodeMetadata.nodeVersion().onOrAfterMinimumCompatible() == false ? TOO_OLD_MESSAGE : TOO_NEW_MESSAGE).replace(
"V_OLD",
nodeMetadata.nodeVersion().toString()
).replace("V_NEW", nodeMetadata.nodeVersion().toString()).replace("V_CUR", Version.CURRENT.toString())
).replace("V_NEW", nodeMetadata.nodeVersion().toString()).replace("V_CUR", BuildVersion.current().toString())
);

PersistedClusterStateService.overrideVersion(Version.CURRENT, paths);
PersistedClusterStateService.overrideVersion(BuildVersion.current(), paths);

terminal.println(SUCCESS_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.InfoStream;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
Expand Down Expand Up @@ -159,8 +158,6 @@ public class PersistedClusterStateService {
public static final int IS_LAST_PAGE = 1;
public static final int IS_NOT_LAST_PAGE = 0;
private static final int COMMIT_DATA_SIZE = 7;
// We added CLUSTER_UUID_KEY and CLUSTER_UUID_COMMITTED_KEY in 8.8
private static final int COMMIT_DATA_SIZE_BEFORE_8_8 = 5;

private static final MergePolicy NO_MERGE_POLICY = noMergePolicy();
private static final MergePolicy DEFAULT_MERGE_POLICY = defaultMergePolicy();
Expand Down Expand Up @@ -350,7 +347,7 @@ public record OnDiskStateMetadata(
@Nullable
public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
String nodeId = null;
Version version = null;
BuildVersion version = null;
IndexVersion oldestIndexVersion = IndexVersions.ZERO;
for (final Path dataPath : dataPaths) {
final Path indexPath = dataPath.resolve(METADATA_DIRECTORY_NAME);
Expand All @@ -367,7 +364,7 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
);
} else if (nodeId == null) {
nodeId = thisNodeId;
version = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
version = BuildVersion.fromVersionId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
if (userData.containsKey(OLDEST_INDEX_VERSION_KEY)) {
oldestIndexVersion = IndexVersion.fromId(Integer.parseInt(userData.get(OLDEST_INDEX_VERSION_KEY)));
} else {
Expand All @@ -382,14 +379,13 @@ public static NodeMetadata nodeMetadata(Path... dataPaths) throws IOException {
if (nodeId == null) {
return null;
}
// TODO: remove use of Version here (ES-7343)
return new NodeMetadata(nodeId, BuildVersion.fromVersionId(version.id()), oldestIndexVersion);
return new NodeMetadata(nodeId, version, oldestIndexVersion);
}

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

try (IndexWriter indexWriter = createIndexWriter(new NIOFSDirectory(dataPath.resolve(METADATA_DIRECTORY_NAME)), true)) {
final Map<String, String> commitData = new HashMap<>(userData);
commitData.put(NODE_VERSION_KEY, Integer.toString(newVersion.id));
commitData.put(NODE_VERSION_KEY, Integer.toString(newVersion.id()));
commitData.put(OVERRIDDEN_NODE_VERSION_KEY, Boolean.toString(true));
indexWriter.setLiveCommitData(commitData.entrySet());
indexWriter.commit();
Expand Down Expand Up @@ -664,11 +660,9 @@ public OnDiskStateMetadata loadOnDiskStateMetadataFromUserData(Map<String, Strin
assert userData.get(LAST_ACCEPTED_VERSION_KEY) != null;
assert userData.get(NODE_ID_KEY) != null;
assert userData.get(NODE_VERSION_KEY) != null;
var nodeVersion = Version.fromId(Integer.parseInt(userData.get(NODE_VERSION_KEY)));
assert userData.get(CLUSTER_UUID_KEY) != null || nodeVersion.before(Version.V_8_8_0);
assert userData.get(CLUSTER_UUID_COMMITTED_KEY) != null || nodeVersion.before(Version.V_8_8_0);
assert userData.get(OVERRIDDEN_NODE_VERSION_KEY) != null
|| userData.size() == (nodeVersion.onOrAfter(Version.V_8_8_0) ? COMMIT_DATA_SIZE : COMMIT_DATA_SIZE_BEFORE_8_8) : userData;
assert userData.get(CLUSTER_UUID_KEY) != null;
assert userData.get(CLUSTER_UUID_COMMITTED_KEY) != null;
assert userData.get(OVERRIDDEN_NODE_VERSION_KEY) != null || userData.size() == COMMIT_DATA_SIZE;
return new OnDiskStateMetadata(
Long.parseLong(userData.get(CURRENT_TERM_KEY)),
Long.parseLong(userData.get(LAST_ACCEPTED_VERSION_KEY)),
Expand Down Expand Up @@ -858,7 +852,7 @@ void prepareCommit(
final Map<String, String> commitData = Maps.newMapWithExpectedSize(COMMIT_DATA_SIZE);
commitData.put(CURRENT_TERM_KEY, Long.toString(currentTerm));
commitData.put(LAST_ACCEPTED_VERSION_KEY, Long.toString(lastAcceptedVersion));
commitData.put(NODE_VERSION_KEY, Integer.toString(Version.CURRENT.id));
commitData.put(NODE_VERSION_KEY, Integer.toString(BuildVersion.current().id()));
commitData.put(OLDEST_INDEX_VERSION_KEY, Integer.toString(oldestIndexVersion.id()));
commitData.put(NODE_ID_KEY, nodeId);
commitData.put(CLUSTER_UUID_KEY, clusterUUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,6 @@ public void testUpgradeMarksPreviousVersion() {
assertThat(nodeMetadata.previousNodeVersion(), equalTo(buildVersion));
}

public static Version tooNewVersion() {
return Version.fromId(between(Version.CURRENT.id + 1, 99999999));
}

public static IndexVersion tooNewIndexVersion() {
return IndexVersion.fromId(between(IndexVersion.current().id() + 1, 99999999));
}
Expand All @@ -179,10 +175,6 @@ public static BuildVersion tooNewBuildVersion() {
return BuildVersion.fromVersionId(between(Version.CURRENT.id() + 1, 99999999));
}

public static Version tooOldVersion() {
return Version.fromId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
}

public static BuildVersion tooOldBuildVersion() {
return BuildVersion.fromVersionId(between(1, Version.CURRENT.minimumCompatibilityVersion().id - 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public void testFailsOnEmptyPath() {
}

public void testFailsIfUnnecessary() throws IOException {
final Version nodeVersion = Version.fromId(between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id));
final BuildVersion nodeVersion = BuildVersion.fromVersionId(
between(Version.CURRENT.minimumCompatibilityVersion().id, Version.CURRENT.id)
);
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
final MockTerminal mockTerminal = MockTerminal.create();
final ElasticsearchException elasticsearchException = expectThrows(
Expand All @@ -107,15 +109,15 @@ public void testFailsIfUnnecessary() throws IOException {
elasticsearchException.getMessage(),
allOf(
containsString("compatible with current version"),
containsString(Version.CURRENT.toString()),
containsString(BuildVersion.current().toString()),
containsString(nodeVersion.toString())
)
);
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));
}

public void testWarnsIfTooOld() throws Exception {
final Version nodeVersion = NodeMetadataTests.tooOldVersion();
final BuildVersion nodeVersion = NodeMetadataTests.tooOldBuildVersion();
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
final MockTerminal mockTerminal = MockTerminal.create();
mockTerminal.addTextInput("n");
Expand All @@ -137,11 +139,11 @@ public void testWarnsIfTooOld() throws Exception {
expectThrows(IllegalStateException.class, () -> mockTerminal.readText(""));

final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPaths);
assertThat(nodeMetadata.nodeVersion().toVersion(), equalTo(nodeVersion));
assertThat(nodeMetadata.nodeVersion(), equalTo(nodeVersion));
}

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

final NodeMetadata nodeMetadata = PersistedClusterStateService.nodeMetadata(dataPaths);
assertThat(nodeMetadata.nodeVersion().toVersion(), equalTo(nodeVersion));
assertThat(nodeMetadata.nodeVersion(), equalTo(nodeVersion));
}

public void testOverwritesIfTooOld() throws Exception {
final Version nodeVersion = NodeMetadataTests.tooOldVersion();
final BuildVersion nodeVersion = NodeMetadataTests.tooOldBuildVersion();
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
final MockTerminal mockTerminal = MockTerminal.create();
mockTerminal.addTextInput(randomFrom("y", "Y"));
Expand All @@ -189,7 +191,7 @@ public void testOverwritesIfTooOld() throws Exception {
}

public void testOverwritesIfTooNew() throws Exception {
final Version nodeVersion = NodeMetadataTests.tooNewVersion();
final BuildVersion nodeVersion = NodeMetadataTests.tooNewBuildVersion();
PersistedClusterStateService.overrideVersion(nodeVersion, dataPaths);
final MockTerminal mockTerminal = MockTerminal.create();
mockTerminal.addTextInput(randomFrom("y", "Y"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.core.IOUtils;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.core.UpdateForV9;
import org.elasticsearch.env.BuildVersion;
import org.elasticsearch.env.Environment;
import org.elasticsearch.env.NodeEnvironment;
Expand Down Expand Up @@ -1414,14 +1415,17 @@ public void testOverrideLuceneVersion() throws IOException {
assertThat(clusterState.metadata().version(), equalTo(version));

}
@UpdateForV9(owner = UpdateForV9.Owner.SEARCH_FOUNDATIONS)
BuildVersion overrideVersion = BuildVersion.fromVersionId(Version.V_8_0_0.id);

NodeMetadata prevMetadata = PersistedClusterStateService.nodeMetadata(persistedClusterStateService.getDataPaths());
assertEquals(BuildVersion.current(), prevMetadata.nodeVersion());
PersistedClusterStateService.overrideVersion(Version.V_8_0_0, persistedClusterStateService.getDataPaths());
PersistedClusterStateService.overrideVersion(overrideVersion, persistedClusterStateService.getDataPaths());
NodeMetadata metadata = PersistedClusterStateService.nodeMetadata(persistedClusterStateService.getDataPaths());
assertEquals(BuildVersion.fromVersionId(Version.V_8_0_0.id()), metadata.nodeVersion());
assertEquals(overrideVersion, metadata.nodeVersion());
for (Path p : persistedClusterStateService.getDataPaths()) {
NodeMetadata individualMetadata = PersistedClusterStateService.nodeMetadata(p);
assertEquals(BuildVersion.fromVersionId(Version.V_8_0_0.id()), individualMetadata.nodeVersion());
assertEquals(overrideVersion, individualMetadata.nodeVersion());
}
}
}
Expand Down

0 comments on commit 291ced7

Please sign in to comment.