Skip to content

Commit

Permalink
Prevent upgrades to 8.0 from 7.non-last
Browse files Browse the repository at this point in the history
This PR introduces a check to prevent upgrading
to 8.0 without first upgrading to 7.last.

Closes elastic#81865
  • Loading branch information
Nikola Grcevski committed Jan 6, 2022
1 parent 44a98f2 commit c4b7415
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
7 changes: 1 addition & 6 deletions server/src/main/java/org/elasticsearch/env/NodeMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ public Version previousNodeVersion() {
}

public NodeMetadata upgradeToCurrentVersion() {
if (nodeVersion.equals(Version.V_EMPTY)) {
assert Version.CURRENT.major <= Version.V_7_0_0.major + 1 : "version is required in the node metadata from v9 onwards";
return new NodeMetadata(nodeId, Version.CURRENT, Version.V_EMPTY);
}

if (nodeVersion.before(Version.CURRENT.minimumIndexCompatibilityVersion())) {
if (nodeVersion.before(Version.CURRENT.minimumCompatibilityVersion())) {
throw new IllegalStateException(
"cannot upgrade a node from version [" + nodeVersion + "] directly to version [" + Version.CURRENT + "]"
);
Expand Down
29 changes: 21 additions & 8 deletions server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testUpgradesLegitimateVersions() {
final NodeMetadata nodeMetadata = new NodeMetadata(
nodeId,
randomValueOtherThanMany(
v -> v.after(Version.CURRENT) || v.before(Version.CURRENT.minimumIndexCompatibilityVersion()),
v -> v.after(Version.CURRENT) || v.before(Version.CURRENT.minimumCompatibilityVersion()),
this::randomVersion
)
).upgradeToCurrentVersion();
Expand All @@ -83,9 +83,15 @@ public void testUpgradesLegitimateVersions() {

public void testUpgradesMissingVersion() {
final String nodeId = randomAlphaOfLength(10);
final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, Version.V_EMPTY).upgradeToCurrentVersion();
assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
assertThat(nodeMetadata.nodeId(), equalTo(nodeId));

final IllegalStateException illegalStateException = expectThrows(
IllegalStateException.class,
() -> new NodeMetadata(nodeId, Version.V_EMPTY).upgradeToCurrentVersion()
);
assertThat(
illegalStateException.getMessage(),
equalTo("cannot upgrade a node from version [" + Version.V_EMPTY + "] directly to version [" + Version.CURRENT + "]")
);
}

public void testDoesNotUpgradeFutureVersion() {
Expand All @@ -110,12 +116,19 @@ public void testDoesNotUpgradeAncientVersion() {
);
}

public void testUpgradeMarksPreviousVersion() {
public void testUpgradePreviousVersionButNotLatest() {
final String nodeId = randomAlphaOfLength(10);
final Version version = VersionUtils.randomVersionBetween(random(), Version.V_7_3_0, Version.V_7_16_0);
final NodeMetadata nodeMetadata = new NodeMetadata(nodeId, version).upgradeToCurrentVersion();
assertThat(nodeMetadata.nodeVersion(), equalTo(Version.CURRENT));
assertThat(nodeMetadata.previousNodeVersion(), equalTo(version));

final IllegalStateException illegalStateException = expectThrows(
IllegalStateException.class,
() -> new NodeMetadata(nodeId, version).upgradeToCurrentVersion()
);

assertThat(
illegalStateException.getMessage(),
equalTo("cannot upgrade a node from version [" + version + "] directly to version [" + Version.CURRENT + "]")
);
}

public static Version tooNewVersion() {
Expand Down

0 comments on commit c4b7415

Please sign in to comment.