Skip to content

Commit

Permalink
change version comparison to only translate versions for opensearch 1…
Browse files Browse the repository at this point in the history
….x and 2.x

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize committed Apr 4, 2022
1 parent fba94e0 commit e8d8e80
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
28 changes: 18 additions & 10 deletions server/src/main/java/org/opensearch/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,16 @@ public boolean onOrBefore(Version version) {
return version.id >= id;
}

// LegacyESVersion major 7 is equivalent to Version major 1
public int compareMajor(Version other) {
int m = major == 1 ? 7 : major == 2 ? 8 : major == 3 ? 9 : major;
int om = other.major == 1 ? 7 : other.major == 2 ? 8 : major == 3 ? 9 : other.major;
return Integer.compare(m, om);
// comparing Legacy 7x for bwc
// todo: remove the following when removing legacy support in 3.0.0
if (major == 7 || other.major == 7) {
// opensearch v1.x and v2.x need major translation to compare w/ legacy versions
int m = major == 1 ? 7 : major == 2 ? 8 : major;
int om = other.major == 1 ? 7 : other.major == 2 ? 8 : other.major;
return Integer.compare(m, om);
}
return Integer.compare(major, other.major);
}

@Override
Expand Down Expand Up @@ -392,10 +397,10 @@ private Version computeMinIndexCompatVersion() {
bwcMajor = major - 1;
}
final int bwcMinor = 0;
// todo remove when LegacyESVersion is removed
if (major == 3) {
return Version.min(this, fromId((bwcMajor * 1000000 + bwcMinor * 10000 + 99) ^ MASK));
}
// todo remove below when LegacyESVersion is removed in 3.0
return Version.min(this, fromId((bwcMajor * 1000000 + bwcMinor * 10000 + 99)));
}

Expand All @@ -409,12 +414,15 @@ public boolean isCompatible(Version version) {
// OpenSearch version 2 is the functional equivalent of predecessor unreleased version "8"
// todo refactor this logic after removing deprecated features
int a = major;
if (major <= 3) {
a += 6; // for legacy compatibility up to version 3.0 (to compare minCompat)
}
int b = version.major;
if (version.major <= 3) {
b += 6;

if (a == 7 || b == 7) {
if (major <= 2) {
a += 6; // for legacy compatibility up to version 2.x (to compare minCompat)
}
if (version.major <= 2) {
b += 6; // for legacy compatibility up to version 2.x (to compare minCompat)
}
}

assert compatible == false || Math.max(a, b) - Math.min(a, b) <= 1;
Expand Down
4 changes: 2 additions & 2 deletions server/src/test/java/org/opensearch/LegacyESVersionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public void testVersionComparison() {

// compare opensearch version to LegacyESVersion
assertThat(Version.V_1_0_0.compareMajor(LegacyESVersion.V_7_0_0), is(0));
assertThat(Version.V_1_0_0.compareMajor(LegacyESVersion.fromString("6.3.0")), is(1));
assertThat(LegacyESVersion.fromString("6.3.0").compareMajor(Version.V_1_0_0), is(-1));
assertThat(Version.V_2_0_0.compareMajor(LegacyESVersion.fromString("7.3.0")), is(1));
assertThat(LegacyESVersion.fromString("7.3.0").compareMajor(Version.V_2_0_0), is(-1));
}

public void testMin() {
Expand Down
3 changes: 1 addition & 2 deletions server/src/test/java/org/opensearch/VersionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ public void testOpenSearchPreLegacyRemoval() {
Version expectedMinIndexCompat = VersionUtils.getFirstVersionOfMajor(candidates, opensearchMajor - 1);
Version actualMinIndexCompat = opensearchVersion.minimumIndexCompatibilityVersion();

Version expectedMinCompat = VersionUtils.getPreviousVersion(opensearchVersion);
expectedMinCompat = Version.fromId((expectedMinCompat.major * 1000000 + expectedMinCompat.minor * 10000 + 99) ^ MASK);
Version expectedMinCompat = VersionUtils.lastFirstReleasedMinorFromMajor(VersionUtils.allOpenSearchVersions(), opensearchMajor - 1);
Version actualMinCompat = opensearchVersion.minimumCompatibilityVersion();
// since some legacy versions still support build (alpha, beta, RC) we check major minor revision only
assertEquals(expectedMinIndexCompat.major, actualMinIndexCompat.major);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ public static List<Version> allLegacyVersions() {
}

/**
* Get the version before {@code version}.
* Get the released version before {@code version}.
*/
public static Version getPreviousVersion(Version version) {
for (int i = ALL_VERSIONS.size() - 1; i >= 0; i--) {
Version v = ALL_VERSIONS.get(i);
for (int i = RELEASED_VERSIONS.size() - 1; i >= 0; i--) {
Version v = RELEASED_VERSIONS.get(i);
if (v.before(version)) {
return v;
}
Expand All @@ -223,7 +223,7 @@ public static Version getPreviousVersion(Version version) {
}

/**
* Get the version before {@link Version#CURRENT}.
* Get the released version before {@link Version#CURRENT}.
*/
public static Version getPreviousVersion() {
Version version = getPreviousVersion(Version.CURRENT);
Expand Down

0 comments on commit e8d8e80

Please sign in to comment.