Skip to content

Commit 99ac7be

Browse files
authored
Teach the build about betas and rcs (#26066)
The build was ignoring suffixes like "beta1" and "rc1" on the version numbers which was causing the backwards compatibility packaging tests to fail because they expected to be upgrading from 6.0.0 even though they were actually upgrading from 6.0.0-beta1. This adds the suffixes to the information that the build scrapes from Version.java. It then uses those suffixes when it resolves artifacts build from the bwc branch and for testing. Closes #26017
1 parent 9c372e5 commit 99ac7be

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

build.gradle

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ configure(subprojects.findAll { it.projectDir.toPath().startsWith(rootPath) }) {
6464

6565
/* Introspect all versions of ES that may be tested agains for backwards
6666
* compatibility. It is *super* important that this logic is the same as the
67-
* logic in VersionUtils.java, modulo alphas, betas, and rcs which are ignored
68-
* in gradle because they don't have any backwards compatibility guarantees
69-
* but are not ignored in VersionUtils.java because the tests expect them not
70-
* to be. */
67+
* logic in VersionUtils.java, throwing out alphas because they don't have any
68+
* backwards compatibility guarantees and only keeping the latest beta or rc
69+
* in a branch if there are only betas and rcs in the branch so we have
70+
* *something* to test against. */
7171
Version currentVersion = Version.fromString(VersionProperties.elasticsearch.minus('-SNAPSHOT'))
7272
int prevMajor = currentVersion.major - 1
7373
File versionFile = file('core/src/main/java/org/elasticsearch/Version.java')
@@ -84,11 +84,20 @@ for (String line : versionLines) {
8484
int major = Integer.parseInt(match.group(1))
8585
int minor = Integer.parseInt(match.group(2))
8686
int bugfix = Integer.parseInt(match.group(3))
87-
Version foundVersion = new Version(major, minor, bugfix, false)
87+
String suffix = (match.group(4) ?: '').replace('_', '-')
88+
Version foundVersion = new Version(major, minor, bugfix, suffix, false)
8889
if (currentVersion != foundVersion
89-
&& (major == prevMajor || major == currentVersion.major)
90-
&& (versions.isEmpty() || versions.last() != foundVersion)) {
91-
versions.add(foundVersion)
90+
&& (major == prevMajor || major == currentVersion.major)) {
91+
if (versions.isEmpty() || versions.last() != foundVersion) {
92+
versions.add(foundVersion)
93+
} else {
94+
// Replace the earlier betas with later ones
95+
Version last = versions.set(versions.size() - 1, foundVersion)
96+
if (last.suffix == '') {
97+
throw new InvalidUserDataException("Found two equal versions but"
98+
+ " the first one [$last] wasn't a beta.")
99+
}
100+
}
92101
if (major == prevMajor && minor > lastPrevMinor) {
93102
prevMinorIndex = versions.size() - 1
94103
lastPrevMinor = minor
@@ -106,10 +115,10 @@ if (currentVersion.bugfix == 0) {
106115
// unreleased version of closest branch. So for those cases, the version includes -SNAPSHOT,
107116
// and the bwc distribution will checkout and build that version.
108117
Version last = versions[-1]
109-
versions[-1] = new Version(last.major, last.minor, last.bugfix, true)
118+
versions[-1] = new Version(last.major, last.minor, last.bugfix, last.suffix, true)
110119
if (last.bugfix == 0) {
111120
versions[-2] = new Version(
112-
versions[-2].major, versions[-2].minor, versions[-2].bugfix, true)
121+
versions[-2].major, versions[-2].minor, versions[-2].bugfix, versions[-2].suffix, true)
113122
}
114123
}
115124

buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.gradle
2121

2222
import groovy.transform.Sortable
23+
import java.util.regex.Matcher
24+
import org.gradle.api.InvalidUserDataException
2325

2426
/**
2527
* Encapsulates comparison and printing logic for an x.y.z version.
@@ -32,32 +34,37 @@ public class Version {
3234
final int bugfix
3335
final int id
3436
final boolean snapshot
37+
/**
38+
* Suffix on the version name. Unlike Version.java the build does not
39+
* consider alphas and betas different versions, it just preserves the
40+
* suffix that the version was declared with in Version.java.
41+
*/
42+
final String suffix
3543

36-
public Version(int major, int minor, int bugfix, boolean snapshot) {
44+
public Version(int major, int minor, int bugfix,
45+
String suffix, boolean snapshot) {
3746
this.major = major
3847
this.minor = minor
3948
this.bugfix = bugfix
4049
this.snapshot = snapshot
50+
this.suffix = suffix
4151
this.id = major * 100000 + minor * 1000 + bugfix * 10 +
4252
(snapshot ? 1 : 0)
4353
}
4454

4555
public static Version fromString(String s) {
46-
String[] parts = s.split('\\.')
47-
String bugfix = parts[2]
48-
boolean snapshot = false
49-
if (bugfix.contains('-')) {
50-
snapshot = bugfix.endsWith('-SNAPSHOT')
51-
bugfix = bugfix.split('-')[0]
56+
Matcher m = s =~ /(\d+)\.(\d+)\.(\d+)(-alpha\d+|-beta\d+|-rc\d+)?(-SNAPSHOT)?/
57+
if (m.matches() == false) {
58+
throw new InvalidUserDataException("Invalid version [${s}]")
5259
}
53-
return new Version(parts[0] as int, parts[1] as int, bugfix as int,
54-
snapshot)
60+
return new Version(m.group(1) as int, m.group(2) as int,
61+
m.group(3) as int, m.group(4) ?: '', m.group(5) != null)
5562
}
5663

5764
@Override
5865
public String toString() {
5966
String snapshotStr = snapshot ? '-SNAPSHOT' : ''
60-
return "${major}.${minor}.${bugfix}${snapshotStr}"
67+
return "${major}.${minor}.${bugfix}${suffix}${snapshotStr}"
6168
}
6269

6370
public boolean before(String compareTo) {

distribution/bwc/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ if (enabled) {
119119
dependsOn checkoutBwcBranch
120120
dir = checkoutDir
121121
tasks = [':distribution:deb:assemble', ':distribution:rpm:assemble', ':distribution:zip:assemble']
122+
doLast {
123+
List missing = [bwcDeb, bwcRpm, bwcZip].grep { file ->
124+
false == file.exists() }
125+
if (false == missing.empty) {
126+
throw new InvalidUserDataException(
127+
"Building bwc version didn't generate expected files ${missing}")
128+
}
129+
}
122130
}
123131

124132

test/framework/src/test/java/org/elasticsearch/test/VersionUtilsTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,15 @@ public void testGradleVersionsMatchVersionUtils() {
181181
// First check the index compatible versions
182182
VersionsFromProperty indexCompatible = new VersionsFromProperty("tests.gradle_index_compat_versions");
183183
List<Version> released = VersionUtils.allReleasedVersions().stream()
184-
/* We skip alphas, betas, and the like in gradle because they don't have
185-
* backwards compatibility guarantees even though they are technically
186-
* released. */
187-
.filter(v -> v.isRelease() && (v.major == Version.CURRENT.major || v.major == Version.CURRENT.major - 1))
184+
// Java lists some non-index compatible versions but gradle does not include them.
185+
.filter(v -> v.major == Version.CURRENT.major || v.major == Version.CURRENT.major - 1)
186+
/* Gradle will never include *released* alphas or betas because it will prefer
187+
* the unreleased branch head. Gradle is willing to use branch heads that are
188+
* beta or rc so that we have *something* to test against even though we
189+
* do not offer backwards compatibility for alphas, betas, or rcs. */
190+
.filter(Version::isRelease)
188191
.collect(toList());
192+
189193
List<String> releasedIndexCompatible = released.stream()
190194
.map(Object::toString)
191195
.collect(toList());
@@ -195,7 +199,15 @@ public void testGradleVersionsMatchVersionUtils() {
195199
/* Gradle skips the current version because being backwards compatible
196200
* with yourself is implied. Java lists the version because it is useful. */
197201
.filter(v -> v != Version.CURRENT)
198-
.map(v -> v.major + "." + v.minor + "." + v.revision)
202+
/* Note that gradle skips alphas because they don't have any backwards
203+
* compatibility guarantees but keeps the last beta and rc in a branch
204+
* on when there are only betas an RCs in that branch so that we have
205+
* *something* to test that branch against. There is no need to recreate
206+
* that logic here because allUnreleasedVersions already only contains
207+
* the heads of branches so it should be good enough to just keep all
208+
* the non-alphas.*/
209+
.filter(v -> false == v.isAlpha())
210+
.map(Object::toString)
199211
.collect(toCollection(LinkedHashSet::new)));
200212
assertEquals(unreleasedIndexCompatible, indexCompatible.unreleased);
201213

0 commit comments

Comments
 (0)