From 4b532c08c2b80abef1f0faa45781ec763f51f96d Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Thu, 12 Jul 2018 16:59:04 +0300 Subject: [PATCH 1/8] Implement Version in java - This allows to move all all .java files from .groovy. - Will prevent eclipse from tangling up in this setup - make it possible to use Version from Java --- .../org/elasticsearch/gradle/LoggedExec.java | 41 ---- .../org/elasticsearch/gradle/Version.groovy | 147 -------------- .../org/elasticsearch/gradle/LoggedExec.java | 44 +++++ .../org/elasticsearch/gradle/Version.java | 183 ++++++++++++++++++ .../gradle/VersionProperties.java | 0 .../precommit/NamingConventionsTask.java | 78 ++++---- .../test/NamingConventionsCheck.java | 4 + .../gradle/VersionCollectionTests.groovy | 3 +- .../elasticsearch/gradle/VersionTests.java | 182 +++++++++++++++++ 9 files changed, 453 insertions(+), 229 deletions(-) delete mode 100644 buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggedExec.java delete mode 100644 buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/LoggedExec.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/Version.java rename buildSrc/src/main/{groovy => java}/org/elasticsearch/gradle/VersionProperties.java (100%) rename buildSrc/src/main/{groovy => java}/org/elasticsearch/gradle/precommit/NamingConventionsTask.java (65%) create mode 100644 buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggedExec.java b/buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggedExec.java deleted file mode 100644 index 7f51c4fb3987d..0000000000000 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggedExec.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.elasticsearch.gradle; - -import groovy.lang.Closure; -import org.gradle.api.GradleException; -import org.gradle.api.Task; -import org.gradle.api.tasks.Exec; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.stream.Collectors; - -/** - * A wrapper around gradle's Exec task to capture output and log on error. - */ -public class LoggedExec extends Exec { - - protected ByteArrayOutputStream output = new ByteArrayOutputStream(); - - public LoggedExec() { - if (getLogger().isInfoEnabled() == false) { - setStandardOutput(output); - setErrorOutput(output); - setIgnoreExitValue(true); - doLast(new Closure(this, this) { - public void doCall(Task it) throws IOException { - if (getExecResult().getExitValue() != 0) { - for (String line : output.toString("UTF-8").split("\\R")) { - getLogger().error(line); - } - throw new GradleException( - "Process \'" + getExecutable() + " " + - getArgs().stream().collect(Collectors.joining(" "))+ - "\' finished with non-zero exit value " + - String.valueOf(getExecResult().getExitValue()) - ); - } - } - }); - } - } -} diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy deleted file mode 100644 index c28738d7695eb..0000000000000 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.gradle - -import groovy.transform.Sortable -import java.util.regex.Matcher -import org.gradle.api.InvalidUserDataException - -/** - * Encapsulates comparison and printing logic for an x.y.z version. - */ -@Sortable(includes=['id']) -public class Version { - - final int major - final int minor - final int revision - final int id - final boolean snapshot - /** - * Suffix on the version name. - */ - final String suffix - - public Version(int major, int minor, int revision, - String suffix, boolean snapshot) { - this.major = major - this.minor = minor - this.revision = revision - this.snapshot = snapshot - this.suffix = suffix - - int suffixOffset = 0 - if (suffix.contains("alpha")) { - suffixOffset += Integer.parseInt(suffix.substring(6)) - } else if (suffix.contains("beta")) { - suffixOffset += 25 + Integer.parseInt(suffix.substring(5)) - } else if (suffix.contains("rc")) { - suffixOffset += 50 + Integer.parseInt(suffix.substring(3)); - } - - this.id = major * 1000000 + minor * 10000 + revision * 100 + suffixOffset - } - - public static Version fromString(String s) { - Matcher m = s =~ /(\d+)\.(\d+)\.(\d+)(-alpha\d+|-beta\d+|-rc\d+)?(-SNAPSHOT)?/ - if (m.matches() == false) { - throw new InvalidUserDataException("Invalid version [${s}]") - } - return new Version(m.group(1) as int, m.group(2) as int, - m.group(3) as int, m.group(4) ?: '', m.group(5) != null) - } - - @Override - public String toString() { - String snapshotStr = snapshot ? '-SNAPSHOT' : '' - return "${major}.${minor}.${revision}${suffix}${snapshotStr}" - } - - public boolean before(Version compareTo) { - return id < compareTo.id - } - - public boolean before(String compareTo) { - return before(fromString(compareTo)) - } - - public boolean onOrBefore(Version compareTo) { - return id <= compareTo.id - } - - public boolean onOrBefore(String compareTo) { - return onOrBefore(fromString(compareTo)) - } - - public boolean onOrAfter(Version compareTo) { - return id >= compareTo.id - } - - public boolean onOrAfter(String compareTo) { - return onOrAfter(fromString(compareTo)) - } - - public boolean after(Version compareTo) { - return id > compareTo.id - } - - public boolean after(String compareTo) { - return after(fromString(compareTo)) - } - - public boolean onOrBeforeIncludingSuffix(Version otherVersion) { - if (id != otherVersion.id) { - return id < otherVersion.id - } - - if (suffix == '') { - return otherVersion.suffix == '' - } - - return otherVersion.suffix == '' || suffix < otherVersion.suffix - } - - boolean equals(o) { - if (this.is(o)) return true - if (getClass() != o.class) return false - - Version version = (Version) o - - if (id != version.id) return false - if (major != version.major) return false - if (minor != version.minor) return false - if (revision != version.revision) return false - if (snapshot != version.snapshot) return false - if (suffix != version.suffix) return false - - return true - } - - int hashCode() { - int result - result = major - result = 31 * result + minor - result = 31 * result + revision - result = 31 * result + id - result = 31 * result + (snapshot ? 1 : 0) - result = 31 * result + (suffix != null ? suffix.hashCode() : 0) - return result - } -} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/LoggedExec.java b/buildSrc/src/main/java/org/elasticsearch/gradle/LoggedExec.java new file mode 100644 index 0000000000000..4eab1232ceb78 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/LoggedExec.java @@ -0,0 +1,44 @@ +package org.elasticsearch.gradle; + +import org.gradle.api.GradleException; +import org.gradle.api.tasks.Exec; + +import java.io.ByteArrayOutputStream; +import java.io.UnsupportedEncodingException; + +/** + * A wrapper around gradle's Exec task to capture output and log on error. + */ +@SuppressWarnings("unchecked") +public class LoggedExec extends Exec { + + protected ByteArrayOutputStream output = new ByteArrayOutputStream(); + + public LoggedExec() { + if (getLogger().isInfoEnabled() == false) { + setStandardOutput(output); + setErrorOutput(output); + setIgnoreExitValue(true); + doLast((unused) -> { + if (getExecResult().getExitValue() != 0) { + try { + for (String line : output.toString("UTF-8").split("\\R")) { + getLogger().error(line); + } + } catch (UnsupportedEncodingException e) { + throw new GradleException("Failed to read exec output", e); + } + throw new GradleException( + String.format( + "Process '%s %s' finished with non-zero exit value %d", + getExecutable(), + getArgs(), + getExecResult().getExitValue() + ) + ); + } + } + ); + } + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java b/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java new file mode 100644 index 0000000000000..115da5bc5a054 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java @@ -0,0 +1,183 @@ +package org.elasticsearch.gradle; + +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Encapsulates comparison and printing logic for an x.y.z version. + */ +public class Version implements Comparable { + private final int major; + private final int minor; + private final int revision; + private final int id; + private final boolean snapshot; + /** + * Suffix on the version name. + */ + private final String suffix; + + private static final Pattern pattern = + Pattern.compile("(\\d)+\\.(\\d+)\\.(\\d+)(-alpha\\d+|-beta\\d+|-rc\\d+)?(-SNAPSHOT)?"); + + public Version(int major, int minor, int revision, String suffix, boolean snapshot) { + Objects.requireNonNull(major, "major version can't be null"); + Objects.requireNonNull(minor, "minor version can't be null"); + Objects.requireNonNull(revision, "revision version can't be null"); + this.major = major; + this.minor = minor; + this.revision = revision; + this.snapshot = snapshot; + this.suffix = suffix == null ? "" : suffix; + + int suffixOffset = 0; + if (this.suffix == null || this.suffix.isEmpty()) { + // no suffix will be considered smaller, uncomment to change that + // suffixOffset = 100; + } else { + if (this.suffix.contains("alpha")) { + suffixOffset += parseSuffixNumber(this.suffix.substring(6)); + } else if (this.suffix.contains("beta")) { + suffixOffset += 25 + parseSuffixNumber(this.suffix.substring(5)); + } else if (this.suffix.contains("rc")) { + suffixOffset += 50 + parseSuffixNumber(this.suffix.substring(3)); + } + else { + throw new IllegalArgumentException("Suffix must contain one of: alpha, beta or rc"); + } + } + + // currently snapshot is not taken into account + this.id = major * 10000000 + minor * 100000 + revision * 1000 + suffixOffset * 10 /*+ (snapshot ? 1 : 0)*/; + } + + private static int parseSuffixNumber(String substring) { + if (substring.isEmpty()) { + throw new IllegalArgumentException("Invalid suffix, must contain a number e.x. alpha2"); + } + return Integer.parseInt(substring); + } + + public static Version fromString(final String s) { + Objects.requireNonNull(s); + Matcher matcher = pattern.matcher(s); + if (matcher.matches() == false) { + throw new IllegalArgumentException( + "Invalid version format: '" + s + "'. Should be major.minor.revision[-(alpha|beta|rc)Number][-SNAPSHOT]" + ); + } + + return new Version( + Integer.parseInt(matcher.group(1)), + parseSuffixNumber(matcher.group(2)), + parseSuffixNumber(matcher.group(3)), + matcher.group(4), + matcher.group(5) != null + ); + } + + @Override + public String toString() { + final String snapshotStr = snapshot ? "-SNAPSHOT" : ""; + return String.valueOf(getMajor()) + "." + String.valueOf(getMinor()) + "." + String.valueOf(getRevision()) + + (suffix == null ? "" : suffix) + snapshotStr; + } + + public boolean before(Version compareTo) { + return id < compareTo.getId(); + } + + public boolean before(String compareTo) { + return before(fromString(compareTo)); + } + + public boolean onOrBefore(Version compareTo) { + return id <= compareTo.getId(); + } + + public boolean onOrBefore(String compareTo) { + return onOrBefore(fromString(compareTo)); + } + + public boolean onOrAfter(Version compareTo) { + return id >= compareTo.getId(); + } + + public boolean onOrAfter(String compareTo) { + return onOrAfter(fromString(compareTo)); + } + + public boolean after(Version compareTo) { + return id > compareTo.getId(); + } + + public boolean after(String compareTo) { + return after(fromString(compareTo)); + } + + public boolean onOrBeforeIncludingSuffix(Version otherVersion) { + if (id != otherVersion.getId()) { + return id < otherVersion.getId(); + } + + if (suffix.equals("")) { + return otherVersion.getSuffix().equals(""); + } + + + return otherVersion.getSuffix().equals("") || suffix.compareTo(otherVersion.getSuffix()) < 0; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Version version = (Version) o; + return major == version.major && + minor == version.minor && + revision == version.revision && + id == version.id && + snapshot == version.snapshot && + Objects.equals(suffix, version.suffix); + } + + @Override + public int hashCode() { + + return Objects.hash(major, minor, revision, id, snapshot, suffix); + } + + public final int getMajor() { + return major; + } + + public final int getMinor() { + return minor; + } + + public final int getRevision() { + return revision; + } + + protected final int getId() { + return id; + } + + public final boolean getSnapshot() { + return snapshot; + } + + public final boolean isSnapshot() { + return snapshot; + } + + public final String getSuffix() { + return suffix; + } + + @Override + public int compareTo(Version other) { + return Integer.compare(getId(), other.getId()); + } +} diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java b/buildSrc/src/main/java/org/elasticsearch/gradle/VersionProperties.java similarity index 100% rename from buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/VersionProperties.java diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/NamingConventionsTask.java b/buildSrc/src/main/java/org/elasticsearch/gradle/precommit/NamingConventionsTask.java similarity index 65% rename from buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/NamingConventionsTask.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/precommit/NamingConventionsTask.java index 7b63899de31ee..cfbb75456bc6c 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/NamingConventionsTask.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/precommit/NamingConventionsTask.java @@ -1,7 +1,6 @@ package org.elasticsearch.gradle.precommit; import groovy.lang.Closure; -import org.codehaus.groovy.runtime.ResourceGroovyMethods; import org.elasticsearch.gradle.LoggedExec; import org.elasticsearch.test.NamingConventionsCheck; import org.gradle.api.GradleException; @@ -10,12 +9,12 @@ import org.gradle.api.file.FileCollection; import org.gradle.api.plugins.ExtraPropertiesExtension; import org.gradle.api.plugins.JavaPluginConvention; -import org.gradle.api.tasks.AbstractExecTask; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.SourceSetContainer; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.Objects; @@ -24,6 +23,7 @@ * tests are named according to our conventions so they'll be picked up by * gradle. Read the Javadoc for NamingConventionsCheck to learn more. */ +@SuppressWarnings("unchecked") public class NamingConventionsTask extends LoggedExec { public NamingConventionsTask() { setDescription("Tests that test classes aren't misnamed or misplaced"); @@ -31,23 +31,23 @@ public NamingConventionsTask() { SourceSetContainer sourceSets = getJavaSourceSets(); final FileCollection classpath = project.files( - // This works because the class only depends on one class from junit that will be available from the - // tests compile classpath. It's the most straight forward way of telling Java where to find the main - // class. - NamingConventionsCheck.class.getProtectionDomain().getCodeSource().getLocation().getPath(), - // the tests to be loaded - checkForTestsInMain ? sourceSets.getByName("main").getRuntimeClasspath() : project.files(), - sourceSets.getByName("test").getCompileClasspath(), - sourceSets.getByName("test").getOutput() + // This works because the class only depends on one class from junit that will be available from the + // tests compile classpath. It's the most straight forward way of telling Java where to find the main + // class. + NamingConventionsCheck.class.getProtectionDomain().getCodeSource().getLocation().getPath(), + // the tests to be loaded + checkForTestsInMain ? sourceSets.getByName("main").getRuntimeClasspath() : project.files(), + sourceSets.getByName("test").getCompileClasspath(), + sourceSets.getByName("test").getOutput() ); dependsOn(project.getTasks().matching(it -> "testCompileClasspath".equals(it.getName()))); getInputs().files(classpath); setExecutable(new File( - Objects.requireNonNull( - project.getExtensions().getByType(ExtraPropertiesExtension.class).get("runtimeJavaHome") - ).toString(), - "bin/java") + Objects.requireNonNull( + project.getExtensions().getByType(ExtraPropertiesExtension.class).get("runtimeJavaHome") + ).toString(), + "bin/java") ); if (checkForTestsInMain == false) { @@ -61,36 +61,34 @@ public NamingConventionsTask() { * We build the arguments in a funny afterEvaluate/doFirst closure so that we can wait for the classpath to be * ready for us. Strangely neither one on their own are good enough. */ - project.afterEvaluate(new Closure(this, this) { - public Task doCall(Project it) { - return doFirst(new Closure(NamingConventionsTask.this, NamingConventionsTask.this) { - public AbstractExecTask doCall(Task it) { - args("-Djna.nosys=true"); - args("-cp", classpath.getAsPath(), "org.elasticsearch.test.NamingConventionsCheck"); - args("--test-class", getTestClass()); - if (skipIntegTestInDisguise) { - args("--skip-integ-tests-in-disguise"); - } else { - args("--integ-test-class", getIntegTestClass()); - } - if (getCheckForTestsInMain()) { - args("--main"); - args("--"); - } else { - args("--"); - } - return args(getExistingClassesDirs().getAsPath()); + project.afterEvaluate(new Closure(this, this) { + public void doCall(Project it) { + doFirst(unused -> { + args("-Djna.nosys=true"); + args("-cp", classpath.getAsPath(), "org.elasticsearch.test.NamingConventionsCheck"); + args("--test-class", getTestClass()); + if (skipIntegTestInDisguise) { + args("--skip-integ-tests-in-disguise"); + } else { + args("--integ-test-class", getIntegTestClass()); } + if (getCheckForTestsInMain()) { + args("--main"); + args("--"); + } else { + args("--"); + } + args(getExistingClassesDirs().getAsPath()); }); } }); - doLast(new Closure(this, this) { - public void doCall(Task it) { - try { - ResourceGroovyMethods.setText(getSuccessMarker(), "", "UTF-8"); - } catch (IOException e) { - throw new GradleException("io exception", e); + doLast((Task it) -> { + try { + try (FileWriter fw = new FileWriter(getSuccessMarker())) { + fw.write(""); } + } catch (IOException e) { + throw new GradleException("io exception", e); } }); } @@ -101,7 +99,7 @@ private SourceSetContainer getJavaSourceSets() { public FileCollection getExistingClassesDirs() { FileCollection classesDirs = getJavaSourceSets().getByName(checkForTestsInMain ? "main" : "test") - .getOutput().getClassesDirs(); + .getOutput().getClassesDirs(); return classesDirs.filter(it -> it.exists()); } diff --git a/buildSrc/src/main/java/org/elasticsearch/test/NamingConventionsCheck.java b/buildSrc/src/main/java/org/elasticsearch/test/NamingConventionsCheck.java index 58e95cfc00232..17d885e21bcc2 100644 --- a/buildSrc/src/main/java/org/elasticsearch/test/NamingConventionsCheck.java +++ b/buildSrc/src/main/java/org/elasticsearch/test/NamingConventionsCheck.java @@ -69,6 +69,10 @@ public static void main(String[] args) throws IOException { fail("unsupported argument '" + arg + "'"); } } + if (rootPathList == null) { + fail("No paths provided"); + return; + } NamingConventionsCheck check = new NamingConventionsCheck(testClass, integTestClass); for (String rootDir : rootPathList.split(Pattern.quote(File.pathSeparator))) { diff --git a/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy index 2901acf65220a..dc77cecac2a88 100644 --- a/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy +++ b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy @@ -223,7 +223,8 @@ class VersionCollectionTests extends GradleUnitTestCase { Version.fromString("5.1.1"), Version.fromString("5.2.0"), Version.fromString("5.2.1"), Version.fromString("5.3.0"), Version.fromString("5.3.1")] - assertTrue(wireCompatList.containsAll(vc.wireCompatible)) + def compatible = vc.wireCompatible + assertTrue(wireCompatList.containsAll(compatible)) assertTrue(vc.wireCompatible.containsAll(wireCompatList)) assertEquals(vc.snapshotsIndexCompatible.size(), 1) diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java b/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java new file mode 100644 index 0000000000000..d75692335ddb8 --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java @@ -0,0 +1,182 @@ +package org.elasticsearch.gradle; + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.elasticsearch.gradle.Version; +import org.elasticsearch.gradle.test.GradleUnitTestCase; +import org.junit.Rule; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class VersionTests extends GradleUnitTestCase { + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + public void testVersionParsing() { + assertVersionEquals("7.0.1", 7, 0, 1, "", false); + assertVersionEquals("7.0.1-alpha2", 7, 0, 1, "-alpha2", false); + assertVersionEquals("5.1.2-rc3", 5, 1, 2, "-rc3", false); + assertVersionEquals("6.1.2-SNAPSHOT", 6, 1, 2, "", true); + assertVersionEquals("6.1.2-beta1-SNAPSHOT", 6, 1, 2, "-beta1", true); + } + + public void testCompareWithStringVersions() { + assertTrue("1.10.20 is not interpreted as before 2.0.0", + Version.fromString("1.10.20").before("2.0.0") + ); + assertTrue("7.0.0-alpha1 is not interpreted as before 7.0.0-alpha2", + Version.fromString("7.0.0-alpha1").before("7.0.0-alpha2") + ); + assertTrue("7.0.0-alpha1 should be equal to 7.0.0-alpha1", + Version.fromString("7.0.0-alpha1").equals(Version.fromString("7.0.0-alpha1")) + ); + assertTrue("7.0.0-SNAPSHOT should be equal to 7.0.0-SNAPSHOT", + Version.fromString("7.0.0-SNAPSHOT").equals(Version.fromString("7.0.0-SNAPSHOT")) + ); + assertEquals(Version.fromString("5.2.1-SNAPSHOT"), Version.fromString("5.2.1-SNAPSHOT")); + } + + public void testCollections() { + assertTrue( + Arrays.asList( + Version.fromString("5.2.0"), Version.fromString("5.2.1-SNAPSHOT"), Version.fromString("6.0.0"), + Version.fromString("6.0.1"), Version.fromString("6.1.0") + ).containsAll(Arrays.asList( + Version.fromString("6.0.1"), Version.fromString("5.2.1-SNAPSHOT") + )) + ); + Set versions = new HashSet<>(); + versions.addAll(Arrays.asList( + Version.fromString("5.2.0"), Version.fromString("5.2.1-SNAPSHOT"), Version.fromString("6.0.0"), + Version.fromString("6.0.1"), Version.fromString("6.1.0") + )); + Set subset = new HashSet<>(); + subset.addAll(Arrays.asList( + Version.fromString("6.0.1"), Version.fromString("5.2.1-SNAPSHOT") + )); + assertTrue(versions.containsAll(subset)); + } + + public void testToString() { + assertEquals("7.0.1", new Version(7, 0, 1, null, false).toString()); + } + + public void testCompareVersions() { + assertEquals(0, new Version(7, 0, 0, null, true).compareTo( + new Version(7, 0, 0, null, true) + )); + assertEquals(0, new Version(7, 0, 0, null, true).compareTo( + new Version(7, 0, 0, "", true) + )); + + // snapshot is not taken into account TODO inconsistent with equals + assertEquals( + 0, + new Version(7, 0, 0, "", false).compareTo( + new Version(7, 0, 0, null, true)) + ); + // without sufix is smaller than with TODO + assertOrder( + new Version(7, 0, 0, null, false), + new Version(7, 0, 0, "-alpha1", false) + ); + // numbered sufix + assertOrder( + new Version(7, 0, 0, "-alpha1", false), + new Version(7, 0, 0, "-alpha2", false) + ); + // ranked sufix + assertOrder( + new Version(7, 0, 0, "-alpha8", false), + new Version(7, 0, 0, "-rc1", false) + ); + // ranked sufix + assertOrder( + new Version(7, 0, 0, "-alpha8", false), + new Version(7, 0, 0, "-beta1", false) + ); + // ranked sufix + assertOrder( + new Version(7, 0, 0, "-beta8", false), + new Version(7, 0, 0, "-rc1", false) + ); + // major takes precedence + assertOrder( + new Version(6, 10, 10, "-alpha8", true), + new Version(7, 0, 0, "-alpha2", false) + ); + // then minor + assertOrder( + new Version(7, 0, 10, "-alpha8", true), + new Version(7, 1, 0, "-alpha2", false) + ); + // then revision + assertOrder( + new Version(7, 1, 0, "-alpha8", true), + new Version(7, 1, 10, "-alpha2", false) + ); + } + + public void testExceptionEmpty() { + expectedEx.expect(IllegalArgumentException.class); + expectedEx.expectMessage("Invalid version format"); + Version.fromString(""); + } + + public void testExceptionSyntax() { + expectedEx.expect(IllegalArgumentException.class); + expectedEx.expectMessage("Invalid version format"); + Version.fromString("foo.bar.baz"); + } + + public void testExceptionSuffixNumber() { + expectedEx.expect(IllegalArgumentException.class); + expectedEx.expectMessage("Invalid suffix"); + new Version(7, 1, 1, "-alpha", true); + } + + public void testExceptionSuffix() { + expectedEx.expect(IllegalArgumentException.class); + expectedEx.expectMessage("Suffix must contain one of:"); + new Version(7, 1, 1, "foo1", true); + } + + private void assertOrder(Version smaller, Version bigger) { + assertEquals(smaller + " should be smaller than " + bigger, -1, smaller.compareTo(bigger)); + } + + private void assertVersionEquals(String stringVersion, int major, int minor, int revision, String sufix, boolean snapshot) { + Version version = Version.fromString(stringVersion); + assertEquals(major, version.getMajor()); + assertEquals(minor, version.getMinor()); + assertEquals(revision, version.getRevision()); + if (snapshot) { + assertTrue("Expected version to be a snapshot but it was not", version.getSnapshot()); + } else { + assertFalse("Expected version not to be a snapshot but it was", version.getSnapshot()); + } + assertEquals(sufix, version.getSuffix()); + } + +} From 952af44eb15dc5d5d92f5c4a960a1442ecd5d069 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Fri, 13 Jul 2018 08:19:58 +0300 Subject: [PATCH 2/8] PR review comments --- .../org/elasticsearch/gradle/Version.java | 20 ++++++++----------- .../gradle/VersionCollectionTests.groovy | 4 ++-- .../elasticsearch/gradle/VersionTests.java | 5 ++--- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java b/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java index 115da5bc5a054..53855716840dd 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/Version.java @@ -7,7 +7,7 @@ /** * Encapsulates comparison and printing logic for an x.y.z version. */ -public class Version implements Comparable { +public final class Version implements Comparable { private final int major; private final int minor; private final int revision; @@ -32,7 +32,7 @@ public Version(int major, int minor, int revision, String suffix, boolean snapsh this.suffix = suffix == null ? "" : suffix; int suffixOffset = 0; - if (this.suffix == null || this.suffix.isEmpty()) { + if (this.suffix.isEmpty()) { // no suffix will be considered smaller, uncomment to change that // suffixOffset = 100; } else { @@ -148,31 +148,27 @@ public int hashCode() { return Objects.hash(major, minor, revision, id, snapshot, suffix); } - public final int getMajor() { + public int getMajor() { return major; } - public final int getMinor() { + public int getMinor() { return minor; } - public final int getRevision() { + public int getRevision() { return revision; } - protected final int getId() { + protected int getId() { return id; } - public final boolean getSnapshot() { + public boolean isSnapshot() { return snapshot; } - public final boolean isSnapshot() { - return snapshot; - } - - public final String getSuffix() { + public String getSuffix() { return suffix; } diff --git a/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy index dc77cecac2a88..ad36c84078398 100644 --- a/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy +++ b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy @@ -8,7 +8,7 @@ class VersionCollectionTests extends GradleUnitTestCase { String formatVersion(String version) { return " public static final Version V_${version.replaceAll("\\.", "_")} " } - def allVersions = [formatVersion('5.0.0'), formatVersion('5.0.0_alpha1'), formatVersion('5.0.0_alpha2'), formatVersion('5.0.0_beta1'), + List allVersions = [formatVersion('5.0.0'), formatVersion('5.0.0_alpha1'), formatVersion('5.0.0_alpha2'), formatVersion('5.0.0_beta1'), formatVersion('5.0.0_rc1'),formatVersion('5.0.0_rc2'),formatVersion('5.0.1'), formatVersion('5.0.2'), formatVersion('5.1.1'), formatVersion('5.1.2'), formatVersion('5.2.0'), formatVersion('5.2.1'), formatVersion('6.0.0'), formatVersion('6.0.1'), formatVersion('6.1.0'), formatVersion('6.1.1'), formatVersion('6.2.0'), formatVersion('6.3.0'), @@ -223,7 +223,7 @@ class VersionCollectionTests extends GradleUnitTestCase { Version.fromString("5.1.1"), Version.fromString("5.2.0"), Version.fromString("5.2.1"), Version.fromString("5.3.0"), Version.fromString("5.3.1")] - def compatible = vc.wireCompatible + List compatible = vc.wireCompatible assertTrue(wireCompatList.containsAll(compatible)) assertTrue(vc.wireCompatible.containsAll(wireCompatList)) diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java b/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java index d75692335ddb8..d3c3b4a43cb41 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/VersionTests.java @@ -19,7 +19,6 @@ * under the License. */ -import org.elasticsearch.gradle.Version; import org.elasticsearch.gradle.test.GradleUnitTestCase; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -172,9 +171,9 @@ private void assertVersionEquals(String stringVersion, int major, int minor, int assertEquals(minor, version.getMinor()); assertEquals(revision, version.getRevision()); if (snapshot) { - assertTrue("Expected version to be a snapshot but it was not", version.getSnapshot()); + assertTrue("Expected version to be a snapshot but it was not", version.isSnapshot()); } else { - assertFalse("Expected version not to be a snapshot but it was", version.getSnapshot()); + assertFalse("Expected version not to be a snapshot but it was", version.isSnapshot()); } assertEquals(sufix, version.getSuffix()); } From cddb550281d153eed102c1d6c3bf8bbb58e91e61 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Mon, 11 Jun 2018 09:56:44 +0300 Subject: [PATCH 3/8] Cluster formation plugin with reference counting ``` > Task :plugins:ingest-user-agent:listElasticSearchClusters Starting cluster: myTestCluster * myTestCluster: /home/alpar/work/elastic/elasticsearch/plugins/ingest-user-agent/foo Asked to unClaimAndStop myTestCluster, since cluster still has 1 claim it will not be stopped > Task :plugins:ingest-user-agent:testme UP-TO-DATE Stopping myTestCluster, since no of claims is 0 ``` - Meant to auto manage the clusters lifecycle - Add integration test for cluster formation --- buildSrc/build.gradle | 9 ++ .../elasticsearch/GradleServicesAdapter.java | 68 +++++++++ .../elasticsearch/gradle/Distribution.java | 36 +++++ ...ClusterFormationTaskExecutionListener.java | 47 ++++++ .../ClusterFormationTaskExtension.java | 60 ++++++++ .../ClusterformationPlugin.java | 76 +++++++++ .../ElasticsearchConfiguration.java | 46 ++++++ .../clusterformation/ElasticsearchNode.java | 130 ++++++++++++++++ .../ClusterformationPluginIT.java | 144 ++++++++++++++++++ .../test/GradleIntegrationTestCase.java | 44 ++++++ .../src/testKit/clusterformation/build.gradle | 41 +++++ 11 files changed, 701 insertions(+) create mode 100644 buildSrc/src/main/java/org/elasticsearch/GradleServicesAdapter.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/Distribution.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchConfiguration.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java create mode 100644 buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java create mode 100644 buildSrc/src/testKit/clusterformation/build.gradle diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 3d100daf7d65f..0faa738ce8679 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -23,6 +23,15 @@ plugins { id 'groovy' } +gradlePlugin { + plugins { + simplePlugin { + id = 'elasticsearch.clusterformation' + implementationClass = 'org.elasticsearch.gradle.clusterformation.ClusterformationPlugin' + } + } +} + group = 'org.elasticsearch.gradle' if (GradleVersion.current() < GradleVersion.version('3.3')) { diff --git a/buildSrc/src/main/java/org/elasticsearch/GradleServicesAdapter.java b/buildSrc/src/main/java/org/elasticsearch/GradleServicesAdapter.java new file mode 100644 index 0000000000000..6d256ba044971 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/GradleServicesAdapter.java @@ -0,0 +1,68 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch; + +import org.gradle.api.Action; +import org.gradle.api.Project; +import org.gradle.api.file.CopySpec; +import org.gradle.api.file.FileTree; +import org.gradle.api.tasks.WorkResult; +import org.gradle.process.ExecResult; +import org.gradle.process.JavaExecSpec; + +import java.io.File; + +/** + * Facilitate access to Gradle services without a direct dependency on Project. + * + * In a future release Gradle will offer service injection, this adapter plays that role until that time. + * It exposes the service methods that are part of the public API as the classes implementing them are not. + * Today service injection is not available for + * extensions. + * + * Everything exposed here must be thread safe. That is the very reason why project is not passed in directly. + */ +public class GradleServicesAdapter { + + public final Project project; + + public GradleServicesAdapter(Project project) { + this.project = project; + } + + public static GradleServicesAdapter getInstance(Project project) { + return new GradleServicesAdapter(project); + } + + public WorkResult copy(Action action) { + return project.copy(action); + } + + public WorkResult sync(Action action) { + return project.sync(action); + } + + public ExecResult javaexec(Action action) { + return project.javaexec(action); + } + + public FileTree zipTree(File zipPath) { + return project.zipTree(zipPath); + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/Distribution.java b/buildSrc/src/main/java/org/elasticsearch/gradle/Distribution.java new file mode 100644 index 0000000000000..c926e70b3f765 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/Distribution.java @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle; + +public enum Distribution { + + INTEG_TEST("integ-test-zip"), + ZIP("zip"), + ZIP_OSS("zip-oss"); + + private final String name; + + Distribution(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java new file mode 100644 index 0000000000000..4cddd1d470cda --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java @@ -0,0 +1,47 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.gradle.api.Task; +import org.gradle.api.execution.TaskActionListener; +import org.gradle.api.execution.TaskExecutionListener; +import org.gradle.api.tasks.TaskState; + +public class ClusterFormationTaskExecutionListener implements TaskExecutionListener, TaskActionListener { + @Override + public void afterExecute(Task task, TaskState state) { + // always unclaim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the + // cluster to start. + ClusterformationPlugin.getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::unClaimAndStop); + } + + @Override + public void beforeActions(Task task) { + // we only start the cluster before the actions, so we'll not start it if the task is up-to-date + ClusterformationPlugin.getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::start); + } + + @Override + public void beforeExecute(Task task) { + } + + @Override + public void afterActions(Task task) { + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java new file mode 100644 index 0000000000000..356f16c40939f --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java @@ -0,0 +1,60 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.gradle.api.Task; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ClusterFormationTaskExtension { + + private final Task task; + + private final List claimedClusters = new ArrayList<>(); + + private final Logger logger = Logging.getLogger(ClusterFormationTaskExtension.class); + + public ClusterFormationTaskExtension(Task task) { + this.task = task; + } + + public void use(ElasticsearchConfiguration cluster) { + // not likely to configure the same task from multiple threads as of Gradle 4.7, but it's the right thing to do + synchronized (claimedClusters) { + if (claimedClusters.contains(cluster)) { + logger.warn("{} already claimed cluster {} will not claim it again", + task.getPath(), cluster.getName() + ); + return; + } + claimedClusters.add(cluster); + } + logger.info("CF: the {} task will use cluster: {}", task.getName(), cluster.getName()); + } + + public List getClaimedClusters() { + synchronized (claimedClusters) { + return Collections.unmodifiableList(claimedClusters); + } + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java new file mode 100644 index 0000000000000..c0c34535bc3c2 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -0,0 +1,76 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.elasticsearch.GradleServicesAdapter; +import org.gradle.api.NamedDomainObjectContainer; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; + +public class ClusterformationPlugin implements Plugin { + + public static final String LIST_TASK_NAME = "listElasticSearchClusters"; + public static final String EXTENSION_NAME = "elasticSearchClusters"; + public static final String TASK_EXTENSION_NAME = "clusterFormation"; + + private final Logger logger = Logging.getLogger(ClusterformationPlugin.class); + + @Override + public void apply(Project project) { + NamedDomainObjectContainer container = project.container( + ElasticsearchNode.class, + (name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project)) + ); + project.getExtensions().add(EXTENSION_NAME, container); + + Task listTask = project.getTasks().create(LIST_TASK_NAME); + listTask.setGroup("ES cluster formation"); + listTask.setDescription("Lists all ES clusters configured for this project"); + listTask.doLast((Task task) -> + container.forEach((ElasticsearchConfiguration cluster) -> + logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution()) + ) + ); + + // register an extension for all current and future tasks, so that any task can declare that it wants to use a + // specific cluster. + project.getTasks().all((Task task) -> + task.getExtensions().create(TASK_EXTENSION_NAME, ClusterFormationTaskExtension.class, task) + ); + + // Make sure we only claim the clusters for the tasks that will actually execute + project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> + taskExecutionGraph.getAllTasks().forEach(task -> + getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::claim) + ) + ); + + // create the listener to start the clusters on-demand and terminate when no longer claimed. + // we need to use a task execution listener, as tasl + project.getGradle().addListener(new ClusterFormationTaskExecutionListener()); + } + + static ClusterFormationTaskExtension getTaskExtension(Task task) { + return task.getExtensions().getByType(ClusterFormationTaskExtension.class); + } + +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchConfiguration.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchConfiguration.java new file mode 100644 index 0000000000000..913d88e9fa11b --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchConfiguration.java @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.elasticsearch.gradle.Distribution; +import org.elasticsearch.gradle.Version; + +import java.util.concurrent.Future; + +public interface ElasticsearchConfiguration { + String getName(); + + Version getVersion(); + + void setVersion(Version version); + + default void setVersion(String version) { + setVersion(Version.fromString(version)); + } + + Distribution getDistribution(); + + void setDistribution(Distribution distribution); + + void claim(); + + Future start(); + + void unClaimAndStop(); +} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java new file mode 100644 index 0000000000000..8b78fc2b627cb --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java @@ -0,0 +1,130 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.elasticsearch.GradleServicesAdapter; +import org.elasticsearch.gradle.Distribution; +import org.elasticsearch.gradle.Version; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; + +import java.util.Objects; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +public class ElasticsearchNode implements ElasticsearchConfiguration { + + private final String name; + private final GradleServicesAdapter services; + private final AtomicInteger noOfClaims = new AtomicInteger(); + private final AtomicBoolean started = new AtomicBoolean(false); + private final Logger logger = Logging.getLogger(ElasticsearchNode.class); + + private Distribution distribution; + private Version version; + + public ElasticsearchNode(String name, GradleServicesAdapter services) { + this.name = name; + this.services = services; + } + + @Override + public String getName() { + return name; + } + + @Override + public Version getVersion() { + return version; + } + + @Override + public void setVersion(Version version) { + checkNotRunning(); + this.version = version; + } + + @Override + public Distribution getDistribution() { + return distribution; + } + + @Override + public void setDistribution(Distribution distribution) { + checkNotRunning(); + this.distribution = distribution; + } + + @Override + public void claim() { + noOfClaims.incrementAndGet(); + } + + /** + * Start the cluster if not running. Does nothing if the cluster is already running. + * + * @return future of thread running in the background + */ + @Override + public Future start() { + if (started.getAndSet(true)) { + logger.lifecycle("Already started cluster: {}", name); + } else { + logger.lifecycle("Starting cluster: {}", name); + } + return null; + } + + /** + * Stops a running cluster if it's not claimed. Does nothing otherwise. + */ + @Override + public void unClaimAndStop() { + int decrementedClaims = noOfClaims.decrementAndGet(); + if (decrementedClaims > 0) { + logger.lifecycle("Not stopping {}, since cluster still has {} claim(s)", name, decrementedClaims); + return; + } + if (started.get() == false) { + logger.lifecycle("Asked to unClaimAndStop, but cluster was not running: {}", name); + return; + } + logger.lifecycle("Stopping {}, number of claims is {}", name, decrementedClaims); + } + + private void checkNotRunning() { + if (started.get()) { + throw new IllegalStateException("Configuration can not be altered while running "); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ElasticsearchNode that = (ElasticsearchNode) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java b/buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java new file mode 100644 index 0000000000000..c690557537dfb --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java @@ -0,0 +1,144 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +import org.elasticsearch.gradle.test.GradleIntegrationTestCase; +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.TaskOutcome; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class ClusterformationPluginIT extends GradleIntegrationTestCase { + + public void testListClusters() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("listElasticSearchClusters", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SUCCESS, result.task(":listElasticSearchClusters").getOutcome()); + assertOutputContains( + result.getOutput(), + " * myTestCluster:" + ); + + } + + public void testUseClusterByOne() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("user1", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SUCCESS, result.task(":user1").getOutcome()); + assertOutputContains( + result.getOutput(), + "Starting cluster: myTestCluster", + "Stopping myTestCluster, number of claims is 0" + ); + } + + public void testUseClusterByOneWithDryRun() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("user1", "-s", "--dry-run") + .withPluginClasspath() + .build(); + + assertNull(result.task(":user1")); + assertOutputDoesNotContain( + result.getOutput(), + "Starting cluster: myTestCluster", + "Stopping myTestCluster, number of claims is 0" + ); + } + + public void testUseClusterByTwo() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("user1", "user2", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SUCCESS, result.task(":user1").getOutcome()); + assertEquals(TaskOutcome.SUCCESS, result.task(":user2").getOutcome()); + assertOutputContains( + result.getOutput(), + "Starting cluster: myTestCluster", + "Not stopping myTestCluster, since cluster still has 1 claim(s)", + "Stopping myTestCluster, number of claims is 0" + ); + } + + public void testUseClusterByUpToDateTask() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("upToDate1", "upToDate2", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.UP_TO_DATE, result.task(":upToDate1").getOutcome()); + assertEquals(TaskOutcome.UP_TO_DATE, result.task(":upToDate2").getOutcome()); + assertOutputContains( + result.getOutput(), + "Not stopping myTestCluster, since cluster still has 1 claim(s)", + "cluster was not running: myTestCluster" + ); + assertOutputDoesNotContain(result.getOutput(), "Starting cluster: myTestCluster"); + } + + public void testUseClusterBySkippedTask() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("skipped1", "skipped2", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SKIPPED, result.task(":skipped1").getOutcome()); + assertEquals(TaskOutcome.SKIPPED, result.task(":skipped2").getOutcome()); + assertOutputContains( + result.getOutput(), + "Not stopping myTestCluster, since cluster still has 1 claim(s)", + "cluster was not running: myTestCluster" + ); + assertOutputDoesNotContain(result.getOutput(), "Starting cluster: myTestCluster"); + } + + public void tetUseClusterBySkippedAndWorkingTask() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("skipped1", "user1", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SKIPPED, result.task(":skipped1").getOutcome()); + assertEquals(TaskOutcome.SUCCESS, result.task(":user1").getOutcome()); + assertOutputContains( + result.getOutput(), + "> Task :user1", + "Starting cluster: myTestCluster", + "Stopping myTestCluster, number of claims is 0" + ); + } + +} diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java index 26da663182f7c..0627f8d732603 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java @@ -1,9 +1,20 @@ package org.elasticsearch.gradle.test; import java.io.File; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public abstract class GradleIntegrationTestCase extends GradleUnitTestCase { + @Test + public void pass() { + } + protected File getProjectDir(String name) { File root = new File("src/testKit/"); if (root.exists() == false) { @@ -13,4 +24,37 @@ protected File getProjectDir(String name) { return new File(root, name); } + protected void assertOutputContains(String output, String... lines) { + for (String line : lines) { + assertOutputContains(output, line); + } + List index = Stream.of(lines).map(line -> output.indexOf(line)).collect(Collectors.toList()); + if (index.equals(index.stream().sorted().collect(Collectors.toList())) == false) { + fail("Expected the following lines to appear in this order:\n" + + Stream.of(lines).map(line -> " - `" + line + "`").collect(Collectors.joining("\n")) + + "\nBut they did not. Output is:\n\n```" + output + "\n```\n" + ); + } + } + + protected void assertOutputContains(String output, String line) { + assertTrue( + "Expected the following line in output:\n\n" + line + "\n\nOutput is:\n" + output, + output.contains(line) + ); + } + + protected void assertOutputDoesNotContain(String output, String line) { + assertFalse( + "Expected the following line not to be in output:\n\n" + line + "\n\nOutput is:\n" + output, + output.contains(line) + ); + } + + protected void assertOutputDoesNotContain(String output, String... lines) { + for (String line : lines) { + assertOutputDoesNotContain(line); + } + } + } diff --git a/buildSrc/src/testKit/clusterformation/build.gradle b/buildSrc/src/testKit/clusterformation/build.gradle new file mode 100644 index 0000000000000..50faca17832a4 --- /dev/null +++ b/buildSrc/src/testKit/clusterformation/build.gradle @@ -0,0 +1,41 @@ +plugins { + id 'elasticsearch.clusterformation' +} + +elasticSearchClusters { + myTestCluster { + distribution = 'ZIP' + } +} + +task user1 { + clusterFormation.use elasticSearchClusters.myTestCluster + doLast { + println "user1 executing" + } +} + +task user2 { + clusterFormation.use elasticSearchClusters.myTestCluster + doLast { + println "user2 executing" + } +} + +task upToDate1 { + clusterFormation.use elasticSearchClusters.myTestCluster +} + +task upToDate2 { + clusterFormation.use elasticSearchClusters.myTestCluster +} + +task skipped1 { + enabled = false + clusterFormation.use elasticSearchClusters.myTestCluster +} + +task skipped2 { + enabled = false + clusterFormation.use elasticSearchClusters.myTestCluster +} \ No newline at end of file From dfc38a19428b5aec3a8dfa2d598f60cefe3be0c4 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Fri, 13 Jul 2018 11:53:55 +0300 Subject: [PATCH 4/8] Fix rebase --- .../gradle/test/GradleIntegrationTestCase.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java index 0627f8d732603..b5fadf214e81f 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java @@ -5,16 +5,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - public abstract class GradleIntegrationTestCase extends GradleUnitTestCase { - @Test - public void pass() { - } - protected File getProjectDir(String name) { File root = new File("src/testKit/"); if (root.exists() == false) { From f2fb4504504912b539b69e71340b5f7aa9df5572 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Wed, 15 Aug 2018 19:13:55 +0300 Subject: [PATCH 5/8] Change to `useCluster` method on task --- ...ClusterFormationTaskExecutionListener.java | 4 +-- .../ClusterFormationTaskExtension.java | 6 +++- .../ClusterformationPlugin.java | 33 +++++++++++++------ .../src/testKit/clusterformation/build.gradle | 14 ++++---- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java index 4cddd1d470cda..e4a50d77599e4 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java @@ -28,13 +28,13 @@ public class ClusterFormationTaskExecutionListener implements TaskExecutionListe public void afterExecute(Task task, TaskState state) { // always unclaim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the // cluster to start. - ClusterformationPlugin.getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::unClaimAndStop); + ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach(ElasticsearchConfiguration::unClaimAndStop); } @Override public void beforeActions(Task task) { // we only start the cluster before the actions, so we'll not start it if the task is up-to-date - ClusterformationPlugin.getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::start); + ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach(ElasticsearchConfiguration::start); } @Override diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java index 356f16c40939f..0cafd2d4bebef 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java @@ -38,7 +38,7 @@ public ClusterFormationTaskExtension(Task task) { this.task = task; } - public void use(ElasticsearchConfiguration cluster) { + public void call(ElasticsearchConfiguration cluster) { // not likely to configure the same task from multiple threads as of Gradle 4.7, but it's the right thing to do synchronized (claimedClusters) { if (claimedClusters.contains(cluster)) { @@ -57,4 +57,8 @@ public List getClaimedClusters() { return Collections.unmodifiableList(claimedClusters); } } + + static ClusterFormationTaskExtension getForTask(Task task) { + return task.getExtensions().getByType(ClusterFormationTaskExtension.class); + } } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java index c0c34535bc3c2..e79f67c0fd539 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.gradle.clusterformation; +import groovy.lang.Closure; import org.elasticsearch.GradleServicesAdapter; import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Plugin; @@ -25,12 +26,13 @@ import org.gradle.api.Task; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; +import org.gradle.api.plugins.ExtraPropertiesExtension; public class ClusterformationPlugin implements Plugin { public static final String LIST_TASK_NAME = "listElasticSearchClusters"; public static final String EXTENSION_NAME = "elasticSearchClusters"; - public static final String TASK_EXTENSION_NAME = "clusterFormation"; + public static final String TASK_EXTENSION_NAME = "useClusterExt"; private final Logger logger = Logging.getLogger(ClusterformationPlugin.class); @@ -53,14 +55,29 @@ public void apply(Project project) { // register an extension for all current and future tasks, so that any task can declare that it wants to use a // specific cluster. - project.getTasks().all((Task task) -> - task.getExtensions().create(TASK_EXTENSION_NAME, ClusterFormationTaskExtension.class, task) - ); + project.getTasks().all((Task task) -> { + ClusterFormationTaskExtension taskExtension = task.getExtensions().create( + TASK_EXTENSION_NAME, ClusterFormationTaskExtension.class, task + ); + // Gradle doesn't look for extensions that might implement `call` and instead only looks for task methods + // work around by creating a closure in the extra properties extensions which does work + task.getExtensions().findByType(ExtraPropertiesExtension.class) + .set( + "useCluster", + new Closure(this, this) { + public void doCall(ElasticsearchConfiguration conf) { + taskExtension.call(conf); + } + } + ); + }); // Make sure we only claim the clusters for the tasks that will actually execute project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> - taskExecutionGraph.getAllTasks().forEach(task -> - getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::claim) + taskExecutionGraph.getAllTasks().forEach( + task -> ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach( + ElasticsearchConfiguration::claim + ) ) ); @@ -69,8 +86,4 @@ public void apply(Project project) { project.getGradle().addListener(new ClusterFormationTaskExecutionListener()); } - static ClusterFormationTaskExtension getTaskExtension(Task task) { - return task.getExtensions().getByType(ClusterFormationTaskExtension.class); - } - } diff --git a/buildSrc/src/testKit/clusterformation/build.gradle b/buildSrc/src/testKit/clusterformation/build.gradle index 50faca17832a4..ae9dd8a2c335c 100644 --- a/buildSrc/src/testKit/clusterformation/build.gradle +++ b/buildSrc/src/testKit/clusterformation/build.gradle @@ -9,33 +9,33 @@ elasticSearchClusters { } task user1 { - clusterFormation.use elasticSearchClusters.myTestCluster + useCluster elasticSearchClusters.myTestCluster doLast { println "user1 executing" } } task user2 { - clusterFormation.use elasticSearchClusters.myTestCluster + useCluster elasticSearchClusters.myTestCluster doLast { println "user2 executing" } } task upToDate1 { - clusterFormation.use elasticSearchClusters.myTestCluster + useCluster elasticSearchClusters.myTestCluster } task upToDate2 { - clusterFormation.use elasticSearchClusters.myTestCluster + useCluster elasticSearchClusters.myTestCluster } task skipped1 { enabled = false - clusterFormation.use elasticSearchClusters.myTestCluster + useCluster elasticSearchClusters.myTestCluster } task skipped2 { enabled = false - clusterFormation.use elasticSearchClusters.myTestCluster -} \ No newline at end of file + useCluster elasticSearchClusters.myTestCluster +} From 2d0e4781a2c7497c87c6c0403124be2a7e7dcbe7 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Thu, 16 Aug 2018 17:13:24 +0300 Subject: [PATCH 6/8] remove task extension --- ...ClusterFormationTaskExecutionListener.java | 20 +++++- .../ClusterFormationTaskExtension.java | 64 ------------------- .../ClusterformationPlugin.java | 49 +++++++------- .../clusterformation/UseClusterAction.java | 26 ++++++++ 4 files changed, 68 insertions(+), 91 deletions(-) delete mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java index e4a50d77599e4..f37699ee92a05 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java @@ -23,18 +23,28 @@ import org.gradle.api.execution.TaskExecutionListener; import org.gradle.api.tasks.TaskState; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + public class ClusterFormationTaskExecutionListener implements TaskExecutionListener, TaskActionListener { + private final Map> taskToCluster; + + public ClusterFormationTaskExecutionListener(Map> taskToCluster) { + this.taskToCluster = taskToCluster; + } + @Override public void afterExecute(Task task, TaskState state) { - // always unclaim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the + // always un-claim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the // cluster to start. - ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach(ElasticsearchConfiguration::unClaimAndStop); + clustersForTask(task).forEach(ElasticsearchConfiguration::unClaimAndStop); } @Override public void beforeActions(Task task) { // we only start the cluster before the actions, so we'll not start it if the task is up-to-date - ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach(ElasticsearchConfiguration::start); + clustersForTask(task).forEach(ElasticsearchConfiguration::start); } @Override @@ -44,4 +54,8 @@ public void beforeExecute(Task task) { @Override public void afterActions(Task task) { } + + private List clustersForTask(Task task) { + return taskToCluster.getOrDefault(task, new ArrayList<>()); + } } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java deleted file mode 100644 index 0cafd2d4bebef..0000000000000 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.gradle.clusterformation; - -import org.gradle.api.Task; -import org.gradle.api.logging.Logger; -import org.gradle.api.logging.Logging; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ClusterFormationTaskExtension { - - private final Task task; - - private final List claimedClusters = new ArrayList<>(); - - private final Logger logger = Logging.getLogger(ClusterFormationTaskExtension.class); - - public ClusterFormationTaskExtension(Task task) { - this.task = task; - } - - public void call(ElasticsearchConfiguration cluster) { - // not likely to configure the same task from multiple threads as of Gradle 4.7, but it's the right thing to do - synchronized (claimedClusters) { - if (claimedClusters.contains(cluster)) { - logger.warn("{} already claimed cluster {} will not claim it again", - task.getPath(), cluster.getName() - ); - return; - } - claimedClusters.add(cluster); - } - logger.info("CF: the {} task will use cluster: {}", task.getName(), cluster.getName()); - } - - public List getClaimedClusters() { - synchronized (claimedClusters) { - return Collections.unmodifiableList(claimedClusters); - } - } - - static ClusterFormationTaskExtension getForTask(Task task) { - return task.getExtensions().getByType(ClusterFormationTaskExtension.class); - } -} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java index e79f67c0fd539..49e048b7ed17a 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -28,11 +28,16 @@ import org.gradle.api.logging.Logging; import org.gradle.api.plugins.ExtraPropertiesExtension; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class ClusterformationPlugin implements Plugin { public static final String LIST_TASK_NAME = "listElasticSearchClusters"; public static final String EXTENSION_NAME = "elasticSearchClusters"; - public static final String TASK_EXTENSION_NAME = "useClusterExt"; private final Logger logger = Logging.getLogger(ClusterformationPlugin.class); @@ -53,37 +58,33 @@ public void apply(Project project) { ) ); + Map> taskToCluster = new HashMap<>(); + // register an extension for all current and future tasks, so that any task can declare that it wants to use a // specific cluster. - project.getTasks().all((Task task) -> { - ClusterFormationTaskExtension taskExtension = task.getExtensions().create( - TASK_EXTENSION_NAME, ClusterFormationTaskExtension.class, task - ); - // Gradle doesn't look for extensions that might implement `call` and instead only looks for task methods - // work around by creating a closure in the extra properties extensions which does work - task.getExtensions().findByType(ExtraPropertiesExtension.class) - .set( - "useCluster", - new Closure(this, this) { - public void doCall(ElasticsearchConfiguration conf) { - taskExtension.call(conf); - } - } - ); - }); + project.getTasks().all((Task task) -> + task.getExtensions().findByType(ExtraPropertiesExtension.class) + .set( + "useCluster", + new Closure(this, this) { + public void doCall(ElasticsearchConfiguration conf) { + taskToCluster.computeIfAbsent(task, k -> new ArrayList<>()).add(conf); + } + }) + ); - // Make sure we only claim the clusters for the tasks that will actually execute + // we need to claim all the clusters before starting executing project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> - taskExecutionGraph.getAllTasks().forEach( - task -> ClusterFormationTaskExtension.getForTask(task).getClaimedClusters().forEach( - ElasticsearchConfiguration::claim + taskExecutionGraph.getAllTasks() + .forEach(task -> + taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::claim) ) - ) ); // create the listener to start the clusters on-demand and terminate when no longer claimed. - // we need to use a task execution listener, as tasl - project.getGradle().addListener(new ClusterFormationTaskExecutionListener()); + project.getGradle().addListener( + new ClusterFormationTaskExecutionListener(Collections.unmodifiableMap(taskToCluster)) + ); } } diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java new file mode 100644 index 0000000000000..d75bafa0b9611 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java @@ -0,0 +1,26 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.gradle.clusterformation; + +@FunctionalInterface +public interface UseClusterAction { + + void call(ElasticsearchConfiguration conf); + +} From 938ef5320f6cde3d9f3f6da4715ab368429ed6bf Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Fri, 17 Aug 2018 10:36:57 +0300 Subject: [PATCH 7/8] address PR --- .../ClusterformationPlugin.java | 2 +- .../clusterformation/UseClusterAction.java | 26 ------------------- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java index 49e048b7ed17a..92860704ef3d5 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -77,7 +77,7 @@ public void doCall(ElasticsearchConfiguration conf) { project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> taskExecutionGraph.getAllTasks() .forEach(task -> - taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::claim) + taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchConfiguration::claim) ) ); diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java deleted file mode 100644 index d75bafa0b9611..0000000000000 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/UseClusterAction.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.gradle.clusterformation; - -@FunctionalInterface -public interface UseClusterAction { - - void call(ElasticsearchConfiguration conf); - -} From a611747270c5f93b99a018d6ef875fa2ef7b5813 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Tue, 21 Aug 2018 11:47:51 +0300 Subject: [PATCH 8/8] Listener in anonymous class --- ...ClusterFormationTaskExecutionListener.java | 61 ------------------- .../ClusterformationPlugin.java | 28 +++++++-- 2 files changed, 24 insertions(+), 65 deletions(-) delete mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java deleted file mode 100644 index f37699ee92a05..0000000000000 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.gradle.clusterformation; - -import org.gradle.api.Task; -import org.gradle.api.execution.TaskActionListener; -import org.gradle.api.execution.TaskExecutionListener; -import org.gradle.api.tasks.TaskState; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class ClusterFormationTaskExecutionListener implements TaskExecutionListener, TaskActionListener { - private final Map> taskToCluster; - - public ClusterFormationTaskExecutionListener(Map> taskToCluster) { - this.taskToCluster = taskToCluster; - } - - @Override - public void afterExecute(Task task, TaskState state) { - // always un-claim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the - // cluster to start. - clustersForTask(task).forEach(ElasticsearchConfiguration::unClaimAndStop); - } - - @Override - public void beforeActions(Task task) { - // we only start the cluster before the actions, so we'll not start it if the task is up-to-date - clustersForTask(task).forEach(ElasticsearchConfiguration::start); - } - - @Override - public void beforeExecute(Task task) { - } - - @Override - public void afterActions(Task task) { - } - - private List clustersForTask(Task task) { - return taskToCluster.getOrDefault(task, new ArrayList<>()); - } -} diff --git a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java index 92860704ef3d5..779e7b61ed9ce 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -24,9 +24,12 @@ import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; +import org.gradle.api.execution.TaskActionListener; +import org.gradle.api.execution.TaskExecutionListener; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.plugins.ExtraPropertiesExtension; +import org.gradle.api.tasks.TaskState; import java.util.ArrayList; import java.util.Collections; @@ -73,17 +76,34 @@ public void doCall(ElasticsearchConfiguration conf) { }) ); - // we need to claim all the clusters before starting executing project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> taskExecutionGraph.getAllTasks() .forEach(task -> taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchConfiguration::claim) ) ); - - // create the listener to start the clusters on-demand and terminate when no longer claimed. project.getGradle().addListener( - new ClusterFormationTaskExecutionListener(Collections.unmodifiableMap(taskToCluster)) + new TaskActionListener() { + @Override + public void beforeActions(Task task) { + // we only start the cluster before the actions, so we'll not start it if the task is up-to-date + taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::start); + } + @Override + public void afterActions(Task task) {} + } + ); + project.getGradle().addListener( + new TaskExecutionListener() { + @Override + public void afterExecute(Task task, TaskState state) { + // always un-claim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the + // cluster to start. + taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::unClaimAndStop); + } + @Override + public void beforeExecute(Task task) {} + } ); }