Restoring alpha/beta/rc version semantics (#1112)

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
This commit is contained in:
Andriy Redko 2021-09-01 19:22:20 -04:00 committed by GitHub
parent 652cdbd5af
commit 6e199d223b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 21 deletions

View File

@ -288,4 +288,29 @@ public class LegacyESVersion extends Version {
protected int maskId(final int id) { protected int maskId(final int id) {
return id; return id;
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(major).append('.').append(minor).append('.').append(revision);
if (isAlpha()) {
sb.append("-alpha");
sb.append(build);
} else if (isBeta()) {
if (major >= 2) {
sb.append("-beta");
} else {
sb.append(".Beta");
}
sb.append(major < 5 ? build : build-25);
} else if (build < 99) {
if (major >= 2) {
sb.append("-rc");
} else {
sb.append(".RC");
}
sb.append(build - 50);
}
return sb.toString();
}
} }

View File

@ -177,19 +177,35 @@ public class Version implements Comparable<Version>, ToXContentFragment {
throw new IllegalArgumentException("illegal version format - snapshot labels are not supported"); throw new IllegalArgumentException("illegal version format - snapshot labels are not supported");
} }
String[] parts = version.split("[.-]"); String[] parts = version.split("[.-]");
// todo: add back optional build number if (parts.length < 3 || parts.length > 4) {
if (parts.length != 3) { throw new IllegalArgumentException(
throw new IllegalArgumentException("the version needs to contain major, minor, and revision: " + version); "the version needs to contain major, minor, and revision, and optionally the build: " + version);
} }
try { try {
final int rawMajor = Integer.parseInt(parts[0]); final int rawMajor = Integer.parseInt(parts[0]);
final int betaOffset = 25; // 0 - 24 is taking by alpha builds
//we reverse the version id calculation based on some assumption as we can't reliably reverse the modulo //we reverse the version id calculation based on some assumption as we can't reliably reverse the modulo
final int major = rawMajor * 1000000; final int major = rawMajor * 1000000;
final int minor = Integer.parseInt(parts[1]) * 10000; final int minor = Integer.parseInt(parts[1]) * 10000;
final int revision = Integer.parseInt(parts[2]) * 100; final int revision = Integer.parseInt(parts[2]) * 100;
int build = 99; int build = 99;
if (parts.length == 4) {
String buildStr = parts[3];
if (buildStr.startsWith("alpha")) {
build = Integer.parseInt(buildStr.substring(5));
assert build < 25 : "expected a alpha build but " + build + " >= 25";
} else if (buildStr.startsWith("Beta") || buildStr.startsWith("beta")) {
build = betaOffset + Integer.parseInt(buildStr.substring(4));
assert build < 50 : "expected a beta build but " + build + " >= 50";
} else if (buildStr.startsWith("RC") || buildStr.startsWith("rc")) {
build = Integer.parseInt(buildStr.substring(2)) + 50;
} else {
throw new IllegalArgumentException("unable to parse version " + version);
}
}
return fromId((major + minor + revision + build) ^ MASK); return fromId((major + minor + revision + build) ^ MASK);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new IllegalArgumentException("unable to parse version " + version, e); throw new IllegalArgumentException("unable to parse version " + version, e);
@ -398,18 +414,10 @@ public class Version implements Comparable<Version>, ToXContentFragment {
sb.append("-alpha"); sb.append("-alpha");
sb.append(build); sb.append(build);
} else if (isBeta()) { } else if (isBeta()) {
if (major >= 2) { sb.append("-beta");
sb.append("-beta"); sb.append(build - 25);
} else {
sb.append(".Beta");
}
sb.append(major < 5 ? build : build-25);
} else if (build < 99) { } else if (build < 99) {
if (major >= 2) { sb.append("-rc");
sb.append("-rc");
} else {
sb.append(".RC");
}
sb.append(build - 50); sb.append(build - 50);
} }
return sb.toString(); return sb.toString();

View File

@ -265,12 +265,36 @@ public class VersionTests extends OpenSearchTestCase {
assertEquals("5.0.0-alpha1", LegacyESVersion.fromString("5.0.0-alpha1").toString()); assertEquals("5.0.0-alpha1", LegacyESVersion.fromString("5.0.0-alpha1").toString());
} }
public void testIsRc() {
assertTrue(LegacyESVersion.fromString("2.0.0-rc1").isRC());
assertTrue(LegacyESVersion.fromString("1.0.0.RC1").isRC());
assertTrue(Version.fromString("1.0.0-rc1").isRC());
assertTrue(Version.fromString("2.0.0.RC1").isRC());
for (int i = 0 ; i < 25; i++) {
assertEquals(LegacyESVersion.fromString("5.0.0-rc" + i).id, LegacyESVersion.fromId(5000000 + i + 50).id);
assertEquals("5.0.0-rc" + i, LegacyESVersion.fromId(5000000 + i + 50).toString());
assertEquals(Version.fromString("1.0.0-rc" + i).id, Version.fromId(135217728 + i + 50).id);
assertEquals("1.0.0-rc" + i, Version.fromId(135217728 + i + 50).toString());
}
}
public void testIsBeta() { public void testIsBeta() {
assertTrue(LegacyESVersion.fromString("2.0.0-beta1").isBeta()); assertTrue(LegacyESVersion.fromString("2.0.0-beta1").isBeta());
assertTrue(LegacyESVersion.fromString("1.0.0.Beta1").isBeta()); assertTrue(LegacyESVersion.fromString("1.0.0.Beta1").isBeta());
assertTrue(LegacyESVersion.fromString("0.90.0.Beta1").isBeta()); assertTrue(LegacyESVersion.fromString("0.90.0.Beta1").isBeta());
} assertTrue(Version.fromString("1.0.0.Beta1").isBeta());
assertTrue(Version.fromString("2.0.0.beta1").isBeta());
for (int i = 0 ; i < 25; i++) {
assertEquals(LegacyESVersion.fromString("5.0.0-beta" + i).id, LegacyESVersion.fromId(5000000 + i + 25).id);
assertEquals("5.0.0-beta" + i, LegacyESVersion.fromId(5000000 + i + 25).toString());
assertEquals(Version.fromString("1.0.0-beta" + i).id, Version.fromId(135217728 + i + 25).id);
assertEquals("1.0.0-beta" + i, Version.fromId(135217728 + i + 25).toString());
}
}
public void testIsAlpha() { public void testIsAlpha() {
assertTrue(new LegacyESVersion(5000001, org.apache.lucene.util.Version.LUCENE_7_0_0).isAlpha()); assertTrue(new LegacyESVersion(5000001, org.apache.lucene.util.Version.LUCENE_7_0_0).isAlpha());
@ -279,15 +303,16 @@ public class VersionTests extends OpenSearchTestCase {
assertTrue(LegacyESVersion.fromString("5.0.0-alpha14").isAlpha()); assertTrue(LegacyESVersion.fromString("5.0.0-alpha14").isAlpha());
assertEquals(5000014, LegacyESVersion.fromString("5.0.0-alpha14").id); assertEquals(5000014, LegacyESVersion.fromString("5.0.0-alpha14").id);
assertTrue(LegacyESVersion.fromId(5000015).isAlpha()); assertTrue(LegacyESVersion.fromId(5000015).isAlpha());
assertEquals(135217742, Version.fromString("1.0.0-alpha14").id);
assertTrue(Version.fromString("1.0.0-alpha14").isAlpha());
for (int i = 0 ; i < 25; i++) { for (int i = 0 ; i < 25; i++) {
assertEquals(LegacyESVersion.fromString("5.0.0-alpha" + i).id, LegacyESVersion.fromId(5000000 + i).id); assertEquals(LegacyESVersion.fromString("5.0.0-alpha" + i).id, LegacyESVersion.fromId(5000000 + i).id);
assertEquals("5.0.0-alpha" + i, LegacyESVersion.fromId(5000000 + i).toString()); assertEquals("5.0.0-alpha" + i, LegacyESVersion.fromId(5000000 + i).toString());
}
assertEquals(Version.fromString("1.0.0-alpha" + i).id, Version.fromId(135217728 + i).id);
for (int i = 0 ; i < 25; i++) { assertEquals("1.0.0-alpha" + i, Version.fromId(135217728 + i).toString());
assertEquals(LegacyESVersion.fromString("5.0.0-beta" + i).id, LegacyESVersion.fromId(5000000 + i + 25).id);
assertEquals("5.0.0-beta" + i, LegacyESVersion.fromId(5000000 + i + 25).toString());
} }
} }