diff --git a/build.gradle b/build.gradle index 3fc6064346b..cc21b94ec2e 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin import org.apache.tools.ant.taskdefs.condition.Os import org.elasticsearch.gradle.BuildPlugin import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.VersionCollection +import org.elasticsearch.gradle.BwcVersions import org.elasticsearch.gradle.VersionProperties import org.elasticsearch.gradle.plugin.PluginBuildPlugin import org.gradle.util.GradleVersion @@ -105,7 +105,7 @@ subprojects { * backwards compatibility guarantees and only keeping the latest beta or rc * in a branch if there are only betas and rcs in the branch so we have * *something* to test against. */ -VersionCollection versions = new VersionCollection(file('server/src/main/java/org/elasticsearch/Version.java').readLines('UTF-8')) +BwcVersions versions = new BwcVersions(file('server/src/main/java/org/elasticsearch/Version.java').readLines('UTF-8')) // build metadata from previous build, contains eg hashes for bwc builds String buildMetadataValue = System.getenv('BUILD_METADATA') diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy index df86b7a2650..0505455720c 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy @@ -23,7 +23,7 @@ import org.apache.tools.ant.taskdefs.condition.Os import org.elasticsearch.gradle.BuildPlugin import org.elasticsearch.gradle.LoggedExec import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.VersionCollection +import org.elasticsearch.gradle.BwcVersions import org.elasticsearch.gradle.VersionProperties import org.elasticsearch.gradle.plugin.PluginBuildPlugin import org.elasticsearch.gradle.plugin.PluginPropertiesExtension @@ -210,7 +210,7 @@ class ClusterFormationTasks { snapshotProject = "oss-" + snapshotProject } boolean internalBuild = project.hasProperty('bwcVersions') - VersionCollection.UnreleasedVersionInfo unreleasedInfo = null + BwcVersions.UnreleasedVersionInfo unreleasedInfo = null if (project.hasProperty('bwcVersions')) { // NOTE: leniency is needed for external plugin authors using build-tools. maybe build the version compat info into build-tools? unreleasedInfo = project.bwcVersions.unreleasedInfo(version) diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy index fe3ba216f2b..7d8b78385a3 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy @@ -4,7 +4,7 @@ import org.apache.tools.ant.taskdefs.condition.Os import org.elasticsearch.gradle.FileContentsTask import org.elasticsearch.gradle.LoggedExec import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.VersionCollection +import org.elasticsearch.gradle.BwcVersions import org.gradle.api.* import org.gradle.api.artifacts.dsl.RepositoryHandler import org.gradle.api.execution.TaskExecutionAdapter @@ -194,7 +194,7 @@ class VagrantTestPlugin implements Plugin { if (project.ext.bwc_tests_enabled) { // The version of elasticsearch that we upgrade *from* // we only add them as dependencies if the bwc tests are enabled, so we don't trigger builds otherwise - VersionCollection.UnreleasedVersionInfo unreleasedInfo = project.bwcVersions.unreleasedInfo(upgradeFromVersion) + BwcVersions.UnreleasedVersionInfo unreleasedInfo = project.bwcVersions.unreleasedInfo(upgradeFromVersion) if (unreleasedInfo != null) { // handle snapshots pointing to bwc build UPGRADE_FROM_ARCHIVES.each { diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/VersionCollection.java b/buildSrc/src/main/java/org/elasticsearch/gradle/BwcVersions.java similarity index 95% rename from buildSrc/src/main/java/org/elasticsearch/gradle/VersionCollection.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/BwcVersions.java index 4dd904158b6..b1b4831e0ec 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/VersionCollection.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/BwcVersions.java @@ -26,6 +26,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -80,7 +82,7 @@ import static java.util.Collections.unmodifiableList; * We are then able to map the unreleased version to branches in git and Gradle projects that are capable of checking * out and building them, so we can include these in the testing plan as well. */ -public class VersionCollection { +public class BwcVersions { private static final Pattern LINE_PATTERN = Pattern.compile( "\\W+public static final Version V_(\\d+)_(\\d+)_(\\d+)(_alpha\\d+|_beta\\d+|_rc\\d+)? .*" @@ -102,12 +104,12 @@ public class VersionCollection { } } - public VersionCollection(List versionLines) { + public BwcVersions(List versionLines) { this(versionLines, Version.fromString(VersionProperties.getElasticsearch())); } - protected VersionCollection(List versionLines, Version currentVersionProperty) { - groupByMajor = versionLines.stream() + protected BwcVersions(List versionLines, Version currentVersionProperty) { + SortedSet allVersions = versionLines.stream() .map(LINE_PATTERN::matcher) .filter(Matcher::matches) .map(match -> new Version( @@ -115,19 +117,19 @@ public class VersionCollection { Integer.parseInt(match.group(2)), Integer.parseInt(match.group(3)) )) - .sorted() - .distinct() - .collect(Collectors.groupingBy(Version::getMajor, Collectors.toList())); + .collect(Collectors.toCollection(TreeSet::new)); - if (groupByMajor.isEmpty()) { + if (allVersions.isEmpty()) { throw new IllegalArgumentException("Could not parse any versions"); } - currentVersion = getLatestVersionByKey( - groupByMajor, - groupByMajor.keySet().stream().max(Integer::compareTo) - .orElseThrow(() -> new IllegalStateException("Unexpected number of versions in collection")) - ); + currentVersion = allVersions.last(); + + groupByMajor = allVersions.stream() + // We only care about the last 2 majors when it comes to BWC. + // It might take us time to remove the older ones from versionLines, so we allow them to exist. + .filter(version -> version.getMajor() > currentVersion.getMajor() - 2) + .collect(Collectors.groupingBy(Version::getMajor, Collectors.toList())); assertCurrentVersionMatchesParsed(currentVersionProperty); diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/VersionCollectionTests.java b/buildSrc/src/test/java/org/elasticsearch/gradle/BwcVersionsTests.java similarity index 95% rename from buildSrc/src/test/java/org/elasticsearch/gradle/VersionCollectionTests.java rename to buildSrc/src/test/java/org/elasticsearch/gradle/BwcVersionsTests.java index 85f683c80b9..219632feebf 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/VersionCollectionTests.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/BwcVersionsTests.java @@ -33,7 +33,7 @@ import static java.util.Collections.singletonList; * specific language governing permissions and limitations * under the License. */ -public class VersionCollectionTests extends GradleUnitTestCase { +public class BwcVersionsTests extends GradleUnitTestCase { private static final Map> sampleVersions = new HashMap<>(); @@ -88,17 +88,17 @@ public class VersionCollectionTests extends GradleUnitTestCase { @Test(expected = IllegalArgumentException.class) public void testExceptionOnEmpty() { - new VersionCollection(asList("foo", "bar"), Version.fromString("7.0.0")); + new BwcVersions(asList("foo", "bar"), Version.fromString("7.0.0")); } @Test(expected = IllegalStateException.class) public void testExceptionOnNonCurrent() { - new VersionCollection(singletonList(formatVersionToLine("6.5.0")), Version.fromString("7.0.0")); + new BwcVersions(singletonList(formatVersionToLine("6.5.0")), Version.fromString("7.0.0")); } @Test(expected = IllegalStateException.class) public void testExceptionOnTooManyMajors() { - new VersionCollection( + new BwcVersions( asList( formatVersionToLine("5.6.12"), formatVersionToLine("6.5.0"), @@ -348,7 +348,7 @@ public class VersionCollectionTests extends GradleUnitTestCase { .map(Version::fromString) .collect(Collectors.toList()); - VersionCollection vc = new VersionCollection( + BwcVersions vc = new BwcVersions( listOfVersions.stream() .map(this::formatVersionToLine) .collect(Collectors.toList()), @@ -363,7 +363,7 @@ public class VersionCollectionTests extends GradleUnitTestCase { .map(Version::fromString) .collect(Collectors.toList()); - VersionCollection vc = new VersionCollection( + BwcVersions vc = new BwcVersions( listOfVersions.stream() .map(this::formatVersionToLine) .collect(Collectors.toList()), @@ -379,7 +379,7 @@ public class VersionCollectionTests extends GradleUnitTestCase { List authoritativeReleasedVersions = Stream.of("7.0.0", "7.0.1") .map(Version::fromString) .collect(Collectors.toList()); - VersionCollection vc = new VersionCollection( + BwcVersions vc = new BwcVersions( listOfVersions.stream() .map(this::formatVersionToLine) .collect(Collectors.toList()), @@ -390,17 +390,17 @@ public class VersionCollectionTests extends GradleUnitTestCase { vc.compareToAuthoritative(authoritativeReleasedVersions); } - private void assertUnreleasedGradleProjectPaths(List expectedNAmes, VersionCollection versionCollection) { + private void assertUnreleasedGradleProjectPaths(List expectedNAmes, BwcVersions bwcVersions) { List actualNames = new ArrayList<>(); - versionCollection.forPreviousUnreleased(unreleasedVersion -> + bwcVersions.forPreviousUnreleased(unreleasedVersion -> actualNames.add(unreleasedVersion.gradleProjectPath) ); assertEquals(expectedNAmes, actualNames); } - private void assertUnreleasedBranchNames(List expectedBranches, VersionCollection versionCollection) { + private void assertUnreleasedBranchNames(List expectedBranches, BwcVersions bwcVersions) { List actualBranches = new ArrayList<>(); - versionCollection.forPreviousUnreleased(unreleasedVersionInfo -> + bwcVersions.forPreviousUnreleased(unreleasedVersionInfo -> actualBranches.add(unreleasedVersionInfo.branch) ); assertEquals(expectedBranches, actualBranches); @@ -419,8 +419,8 @@ public class VersionCollectionTests extends GradleUnitTestCase { ); } - private VersionCollection getVersionCollection(String currentVersion) { - return new VersionCollection( + private BwcVersions getVersionCollection(String currentVersion) { + return new BwcVersions( sampleVersions.get(currentVersion).stream() .map(this::formatVersionToLine) .collect(Collectors.toList()), diff --git a/distribution/bwc/build.gradle b/distribution/bwc/build.gradle index 8459257ddd3..0481115d95c 100644 --- a/distribution/bwc/build.gradle +++ b/distribution/bwc/build.gradle @@ -20,7 +20,7 @@ import org.apache.tools.ant.taskdefs.condition.Os import org.elasticsearch.gradle.LoggedExec import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.VersionCollection +import org.elasticsearch.gradle.BwcVersions import java.nio.charset.StandardCharsets @@ -33,7 +33,7 @@ import static org.elasticsearch.gradle.BuildPlugin.getJavaHome * unreleased versions are when Gradle projects are set up, so we use "build-unreleased-version-*" as placeholders * and configure them to build various versions here. */ -bwcVersions.forPreviousUnreleased { VersionCollection.UnreleasedVersionInfo unreleasedVersion -> project("${unreleasedVersion.gradleProjectPath}") { +bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInfo unreleasedVersion -> project("${unreleasedVersion.gradleProjectPath}") { Version bwcVersion = unreleasedVersion.version String bwcBranch = unreleasedVersion.branch apply plugin: 'distribution'