diff --git a/server/src/main/java/org/opensearch/Version.java b/server/src/main/java/org/opensearch/Version.java index 53ecd472c3630..7ccf4449436cd 100644 --- a/server/src/main/java/org/opensearch/Version.java +++ b/server/src/main/java/org/opensearch/Version.java @@ -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 @@ -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))); } @@ -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; diff --git a/server/src/test/java/org/opensearch/LegacyESVersionTests.java b/server/src/test/java/org/opensearch/LegacyESVersionTests.java index 8fb3636dd8b2c..d59f5e38a4ed7 100644 --- a/server/src/test/java/org/opensearch/LegacyESVersionTests.java +++ b/server/src/test/java/org/opensearch/LegacyESVersionTests.java @@ -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() { diff --git a/server/src/test/java/org/opensearch/VersionTests.java b/server/src/test/java/org/opensearch/VersionTests.java index 7e5f1caea7fda..1f1c56d462c6f 100644 --- a/server/src/test/java/org/opensearch/VersionTests.java +++ b/server/src/test/java/org/opensearch/VersionTests.java @@ -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); diff --git a/test/framework/src/main/java/org/opensearch/test/VersionUtils.java b/test/framework/src/main/java/org/opensearch/test/VersionUtils.java index 93fd7c10b80ae..0f8525285fd08 100644 --- a/test/framework/src/main/java/org/opensearch/test/VersionUtils.java +++ b/test/framework/src/main/java/org/opensearch/test/VersionUtils.java @@ -210,11 +210,11 @@ public static List 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; } @@ -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);