Skip to content

Commit 62d94f2

Browse files
authored
Remove released vs unreleased distinction from VersionUtils (#118108)
1 parent fd81c51 commit 62d94f2

File tree

4 files changed

+30
-424
lines changed

4 files changed

+30
-424
lines changed

server/src/test/java/org/elasticsearch/VersionTests.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ public void testParseVersion() {
179179
}
180180

181181
public void testAllVersionsMatchId() throws Exception {
182-
final Set<Version> releasedVersions = new HashSet<>(VersionUtils.allReleasedVersions());
183-
final Set<Version> unreleasedVersions = new HashSet<>(VersionUtils.allUnreleasedVersions());
182+
final Set<Version> versions = new HashSet<>(VersionUtils.allVersions());
184183
Map<String, Version> maxBranchVersions = new HashMap<>();
185184
for (java.lang.reflect.Field field : Version.class.getFields()) {
186185
if (field.getName().matches("_ID")) {
@@ -195,43 +194,15 @@ public void testAllVersionsMatchId() throws Exception {
195194

196195
Version v = (Version) versionConstant.get(null);
197196
logger.debug("Checking {}", v);
198-
if (field.getName().endsWith("_UNRELEASED")) {
199-
assertTrue(unreleasedVersions.contains(v));
200-
} else {
201-
assertTrue(releasedVersions.contains(v));
202-
}
197+
assertTrue(versions.contains(v));
203198
assertEquals("Version id " + field.getName() + " does not point to " + constantName, v, Version.fromId(versionId));
204199
assertEquals("Version " + constantName + " does not have correct id", versionId, v.id);
205200
String number = v.toString();
206201
assertEquals("V_" + number.replace('.', '_'), constantName);
207-
208-
// only the latest version for a branch should be a snapshot (ie unreleased)
209-
String branchName = "" + v.major + "." + v.minor;
210-
Version maxBranchVersion = maxBranchVersions.get(branchName);
211-
if (maxBranchVersion == null) {
212-
maxBranchVersions.put(branchName, v);
213-
} else if (v.after(maxBranchVersion)) {
214-
if (v == Version.CURRENT) {
215-
// Current is weird - it counts as released even though it shouldn't.
216-
continue;
217-
}
218-
assertFalse(
219-
"Version " + maxBranchVersion + " cannot be a snapshot because version " + v + " exists",
220-
VersionUtils.allUnreleasedVersions().contains(maxBranchVersion)
221-
);
222-
maxBranchVersions.put(branchName, v);
223-
}
224202
}
225203
}
226204
}
227205

228-
public static void assertUnknownVersion(Version version) {
229-
assertFalse(
230-
"Version " + version + " has been releaed don't use a new instance of this version",
231-
VersionUtils.allReleasedVersions().contains(version)
232-
);
233-
}
234-
235206
public void testIsCompatible() {
236207
assertTrue(isCompatible(Version.CURRENT, Version.CURRENT.minimumCompatibilityVersion()));
237208
assertFalse(isCompatible(Version.V_7_0_0, Version.V_8_0_0));
@@ -279,14 +250,6 @@ public boolean isCompatible(Version left, Version right) {
279250
return result;
280251
}
281252

282-
// This exists because 5.1.0 was never released due to a mistake in the release process.
283-
// This verifies that we never declare the version as "released" accidentally.
284-
// It would never pass qa tests later on, but those come very far in the build and this is quick to check now.
285-
public void testUnreleasedVersion() {
286-
Version VERSION_5_1_0_UNRELEASED = Version.fromString("5.1.0");
287-
VersionTests.assertUnknownVersion(VERSION_5_1_0_UNRELEASED);
288-
}
289-
290253
public void testIllegalMinorAndPatchNumbers() {
291254
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Version.fromString("8.2.999"));
292255
assertThat(

test/framework/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ tasks.named("thirdPartyAudit").configure {
8686
tasks.named("test").configure {
8787
systemProperty 'tests.gradle_index_compat_versions', buildParams.bwcVersions.indexCompatible.join(',')
8888
systemProperty 'tests.gradle_wire_compat_versions', buildParams.bwcVersions.wireCompatible.join(',')
89-
systemProperty 'tests.gradle_unreleased_versions', buildParams.bwcVersions.unreleased.join(',')
9089
}
9190

9291
tasks.register("integTest", Test) {

test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java

Lines changed: 11 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -12,132 +12,15 @@
1212
import org.elasticsearch.Build;
1313
import org.elasticsearch.Version;
1414
import org.elasticsearch.core.Nullable;
15-
import org.elasticsearch.core.Tuple;
1615

17-
import java.util.ArrayList;
18-
import java.util.Collections;
1916
import java.util.List;
20-
import java.util.Map;
2117
import java.util.Optional;
2218
import java.util.Random;
23-
import java.util.stream.Collectors;
24-
import java.util.stream.Stream;
2519

2620
/** Utilities for selecting versions in tests */
2721
public class VersionUtils {
2822

29-
/**
30-
* Sort versions that have backwards compatibility guarantees from
31-
* those that don't. Doesn't actually check whether or not the versions
32-
* are released, instead it relies on gradle to have already checked
33-
* this which it does in {@code :core:verifyVersions}. So long as the
34-
* rules here match up with the rules in gradle then this should
35-
* produce sensible results.
36-
* @return a tuple containing versions with backwards compatibility
37-
* guarantees in v1 and versions without the guranteees in v2
38-
*/
39-
static Tuple<List<Version>, List<Version>> resolveReleasedVersions(Version current, Class<?> versionClass) {
40-
// group versions into major version
41-
Map<Integer, List<Version>> majorVersions = Version.getDeclaredVersions(versionClass)
42-
.stream()
43-
.collect(Collectors.groupingBy(v -> (int) v.major));
44-
// this breaks b/c 5.x is still in version list but master doesn't care about it!
45-
// assert majorVersions.size() == 2;
46-
// TODO: remove oldVersions, we should only ever have 2 majors in Version
47-
List<List<Version>> oldVersions = splitByMinor(majorVersions.getOrDefault((int) current.major - 2, Collections.emptyList()));
48-
List<List<Version>> previousMajor = splitByMinor(majorVersions.get((int) current.major - 1));
49-
List<List<Version>> currentMajor = splitByMinor(majorVersions.get((int) current.major));
50-
51-
List<Version> unreleasedVersions = new ArrayList<>();
52-
final List<List<Version>> stableVersions;
53-
if (currentMajor.size() == 1) {
54-
// on master branch
55-
stableVersions = previousMajor;
56-
// remove current
57-
moveLastToUnreleased(currentMajor, unreleasedVersions);
58-
} else {
59-
// on a stable or release branch, ie N.x
60-
stableVersions = currentMajor;
61-
// remove the next maintenance bugfix
62-
moveLastToUnreleased(previousMajor, unreleasedVersions);
63-
}
64-
65-
// remove next minor
66-
Version lastMinor = moveLastToUnreleased(stableVersions, unreleasedVersions);
67-
if (lastMinor.revision == 0) {
68-
if (stableVersions.get(stableVersions.size() - 1).size() == 1) {
69-
// a minor is being staged, which is also unreleased
70-
moveLastToUnreleased(stableVersions, unreleasedVersions);
71-
}
72-
// remove the next bugfix
73-
if (stableVersions.isEmpty() == false) {
74-
moveLastToUnreleased(stableVersions, unreleasedVersions);
75-
}
76-
}
77-
78-
// If none of the previous major was released, then the last minor and bugfix of the old version was not released either.
79-
if (previousMajor.isEmpty()) {
80-
assert currentMajor.isEmpty() : currentMajor;
81-
// minor of the old version is being staged
82-
moveLastToUnreleased(oldVersions, unreleasedVersions);
83-
// bugix of the old version is also being staged
84-
moveLastToUnreleased(oldVersions, unreleasedVersions);
85-
}
86-
List<Version> releasedVersions = Stream.of(oldVersions, previousMajor, currentMajor)
87-
.flatMap(List::stream)
88-
.flatMap(List::stream)
89-
.collect(Collectors.toList());
90-
Collections.sort(unreleasedVersions); // we add unreleased out of order, so need to sort here
91-
return new Tuple<>(Collections.unmodifiableList(releasedVersions), Collections.unmodifiableList(unreleasedVersions));
92-
}
93-
94-
// split the given versions into sub lists grouped by minor version
95-
private static List<List<Version>> splitByMinor(List<Version> versions) {
96-
Map<Integer, List<Version>> byMinor = versions.stream().collect(Collectors.groupingBy(v -> (int) v.minor));
97-
return byMinor.entrySet().stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getValue).collect(Collectors.toList());
98-
}
99-
100-
// move the last version of the last minor in versions to the unreleased versions
101-
private static Version moveLastToUnreleased(List<List<Version>> versions, List<Version> unreleasedVersions) {
102-
List<Version> lastMinor = new ArrayList<>(versions.get(versions.size() - 1));
103-
Version lastVersion = lastMinor.remove(lastMinor.size() - 1);
104-
if (lastMinor.isEmpty()) {
105-
versions.remove(versions.size() - 1);
106-
} else {
107-
versions.set(versions.size() - 1, lastMinor);
108-
}
109-
unreleasedVersions.add(lastVersion);
110-
return lastVersion;
111-
}
112-
113-
private static final List<Version> RELEASED_VERSIONS;
114-
private static final List<Version> UNRELEASED_VERSIONS;
115-
private static final List<Version> ALL_VERSIONS;
116-
117-
static {
118-
Tuple<List<Version>, List<Version>> versions = resolveReleasedVersions(Version.CURRENT, Version.class);
119-
RELEASED_VERSIONS = versions.v1();
120-
UNRELEASED_VERSIONS = versions.v2();
121-
List<Version> allVersions = new ArrayList<>(RELEASED_VERSIONS.size() + UNRELEASED_VERSIONS.size());
122-
allVersions.addAll(RELEASED_VERSIONS);
123-
allVersions.addAll(UNRELEASED_VERSIONS);
124-
Collections.sort(allVersions);
125-
ALL_VERSIONS = Collections.unmodifiableList(allVersions);
126-
}
127-
128-
/**
129-
* Returns an immutable, sorted list containing all released versions.
130-
*/
131-
public static List<Version> allReleasedVersions() {
132-
return RELEASED_VERSIONS;
133-
}
134-
135-
/**
136-
* Returns an immutable, sorted list containing all unreleased versions.
137-
*/
138-
public static List<Version> allUnreleasedVersions() {
139-
return UNRELEASED_VERSIONS;
140-
}
23+
private static final List<Version> ALL_VERSIONS = Version.getDeclaredVersions(Version.class);
14124

14225
/**
14326
* Returns an immutable, sorted list containing all versions, both released and unreleased.
@@ -147,16 +30,16 @@ public static List<Version> allVersions() {
14730
}
14831

14932
/**
150-
* Get the released version before {@code version}.
33+
* Get the version before {@code version}.
15134
*/
15235
public static Version getPreviousVersion(Version version) {
153-
for (int i = RELEASED_VERSIONS.size() - 1; i >= 0; i--) {
154-
Version v = RELEASED_VERSIONS.get(i);
36+
for (int i = ALL_VERSIONS.size() - 1; i >= 0; i--) {
37+
Version v = ALL_VERSIONS.get(i);
15538
if (v.before(version)) {
15639
return v;
15740
}
15841
}
159-
throw new IllegalArgumentException("couldn't find any released versions before [" + version + "]");
42+
throw new IllegalArgumentException("couldn't find any versions before [" + version + "]");
16043
}
16144

16245
/**
@@ -169,22 +52,22 @@ public static Version getPreviousVersion() {
16952
}
17053

17154
/**
172-
* Returns the released {@link Version} before the {@link Version#CURRENT}
55+
* Returns the {@link Version} before the {@link Version#CURRENT}
17356
* where the minor version is less than the currents minor version.
17457
*/
17558
public static Version getPreviousMinorVersion() {
176-
for (int i = RELEASED_VERSIONS.size() - 1; i >= 0; i--) {
177-
Version v = RELEASED_VERSIONS.get(i);
59+
for (int i = ALL_VERSIONS.size() - 1; i >= 0; i--) {
60+
Version v = ALL_VERSIONS.get(i);
17861
if (v.minor < Version.CURRENT.minor || v.major < Version.CURRENT.major) {
17962
return v;
18063
}
18164
}
182-
throw new IllegalArgumentException("couldn't find any released versions of the minor before [" + Build.current().version() + "]");
65+
throw new IllegalArgumentException("couldn't find any versions of the minor before [" + Build.current().version() + "]");
18366
}
18467

185-
/** Returns the oldest released {@link Version} */
68+
/** Returns the oldest {@link Version} */
18669
public static Version getFirstVersion() {
187-
return RELEASED_VERSIONS.get(0);
70+
return ALL_VERSIONS.get(0);
18871
}
18972

19073
/** Returns a random {@link Version} from all available versions. */

0 commit comments

Comments
 (0)