Skip to content

Commit

Permalink
Move static utility method from Build to NodeMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
williamrandolph committed Oct 4, 2023
1 parent 61a842a commit 881766e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
15 changes: 0 additions & 15 deletions server/src/main/java/org/elasticsearch/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,6 @@ public static String minimumCompatString(IndexVersion minimumCompatible) {
}
}

/**
* Check whether a node version is compatible with the current minimum transport version.
* @param version A version identifier as a string
* @throws IllegalArgumentException if version is not a valid transport version identifier
* @return true if the version is compatible, false otherwise
*/
public static boolean isNodeVersionWireCompatible(String version) {
try {
Version esVersion = Version.fromString(version);
return esVersion.onOrAfter(Version.CURRENT.minimumCompatibilityVersion());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Cannot parse [" + version + "] as a transport version identifier", e);
}
}

public static Build current() {
return CurrentHolder.CURRENT;
}
Expand Down
19 changes: 18 additions & 1 deletion server/src/main/java/org/elasticsearch/env/NodeMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void verifyUpgradeToCurrentVersion() {
assert (nodeVersion.equals(Version.V_EMPTY) == false) || (Version.CURRENT.major <= Version.V_7_0_0.major + 1)
: "version is required in the node metadata from v9 onwards";

if (Build.isNodeVersionWireCompatible(nodeVersion.toString()) == false) {
if (NodeMetadata.isNodeVersionWireCompatible(nodeVersion.toString()) == false) {
throw new IllegalStateException(
"cannot upgrade a node from version ["
+ nodeVersion
Expand Down Expand Up @@ -220,4 +220,21 @@ public NodeMetadata fromXContent(XContentParser parser) throws IOException {
}

public static final MetadataStateFormat<NodeMetadata> FORMAT = new NodeMetadataStateFormat(false);

/**
* Check whether a node version is compatible with the current minimum transport version.
* @param version A version identifier as a string
* @throws IllegalArgumentException if version is not a valid transport version identifier
* @return true if the version is compatible, false otherwise
*/
// visible for testing
static boolean isNodeVersionWireCompatible(String version) {
try {
Version esVersion = Version.fromString(version);
return esVersion.onOrAfter(Version.CURRENT.minimumCompatibilityVersion());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Cannot parse [" + version + "] as a transport version identifier", e);
}
}

}
18 changes: 0 additions & 18 deletions server/src/test/java/org/elasticsearch/BuildTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -121,20 +119,4 @@ public void testStrictTypeParsing() {
assertThat(e, hasToString(containsString("unexpected distribution type [" + displayName + "]; your distribution is broken")));
}

public void testIsNodeVersionWireCompatible() {
String nodeVersion = VersionUtils.randomCompatibleVersion(random(), Version.CURRENT).toString();
assertTrue(Build.isNodeVersionWireCompatible(nodeVersion));
nodeVersion = VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()).toString();
assertFalse(Build.isNodeVersionWireCompatible(nodeVersion));

String transportVersion = TransportVersionUtils.randomCompatibleVersion(random()).toString();
IllegalArgumentException e1 = expectThrows(
IllegalArgumentException.class,
() -> Build.isNodeVersionWireCompatible(transportVersion)
);
assertThat(e1.getMessage(), equalTo("Cannot parse [" + transportVersion + "] as a transport version identifier"));

IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, () -> Build.isNodeVersionWireCompatible("x.y.z"));
assertThat(e2.getMessage(), equalTo("Cannot parse [x.y.z] as a transport version identifier"));
}
}
18 changes: 18 additions & 0 deletions server/src/test/java/org/elasticsearch/env/NodeMetadataTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.test.TransportVersionUtils;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.index.IndexVersionUtils;

Expand Down Expand Up @@ -158,6 +159,23 @@ public void testUpgradeMarksPreviousVersion() {
assertThat(nodeMetadata.previousNodeVersion(), equalTo(version));
}

public void testIsNodeVersionWireCompatible() {
String nodeVersion = VersionUtils.randomCompatibleVersion(random(), Version.CURRENT).toString();
assertTrue(NodeMetadata.isNodeVersionWireCompatible(nodeVersion));
nodeVersion = VersionUtils.getPreviousVersion(Version.CURRENT.minimumCompatibilityVersion()).toString();
assertFalse(NodeMetadata.isNodeVersionWireCompatible(nodeVersion));

String transportVersion = TransportVersionUtils.randomCompatibleVersion(random()).toString();
IllegalArgumentException e1 = expectThrows(
IllegalArgumentException.class,
() -> NodeMetadata.isNodeVersionWireCompatible(transportVersion)
);
assertThat(e1.getMessage(), equalTo("Cannot parse [" + transportVersion + "] as a transport version identifier"));

IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, () -> NodeMetadata.isNodeVersionWireCompatible("x.y.z"));
assertThat(e2.getMessage(), equalTo("Cannot parse [x.y.z] as a transport version identifier"));
}

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

0 comments on commit 881766e

Please sign in to comment.