From afa8885ec0ab9ff709a4187fbaadaec573074340 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Mon, 11 Jun 2018 09:56:44 +0300 Subject: [PATCH 1/6] 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 plugins in the backround. --- buildSrc/build.gradle | 9 ++ ...ClusterFormationTaskExecutionListener.java | 47 ++++++++ .../ClusterFormationTaskExtension.java | 59 ++++++++++ .../ClusterformationPlugin.java | 71 ++++++++++++ .../ElasticsearchCluster.java | 105 ++++++++++++++++++ 5 files changed, 291 insertions(+) create mode 100644 buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 9ae86a661cea2..878d2cc6ffc78 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -25,6 +25,15 @@ plugins { id 'groovy' } +gradlePlugin { + plugins { + simplePlugin { + id = 'elasticsearch.clusterformation' + implementationClass = 'org.elasticsearch.clusterformation.ClusterformationPlugin' + } + } +} + group = 'org.elasticsearch.gradle' if (GradleVersion.current() < GradleVersion.version('3.3')) { diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java new file mode 100644 index 0000000000000..6f0158bc6855b --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/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.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(ElasticsearchCluster::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(ElasticsearchCluster::start); + } + + @Override + public void beforeExecute(Task task) { + } + + @Override + public void afterActions(Task task) { + } +} diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java new file mode 100644 index 0000000000000..3d2b612593723 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java @@ -0,0 +1,59 @@ +/* + * 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.clusterformation; + +import org.gradle.api.Task; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +public class ClusterFormationTaskExtension { + + private final Task task; + + private final List claimedClusters = new ArrayList<>(); + + public ClusterFormationTaskExtension(Task task) { + this.task = task; + } + + public void use(ElasticsearchCluster 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)) { + task.getLogger().warn("{} already claimed cluster {} will not claim it again", + task.getPath(), cluster.getName() + ); + return; + } + claimedClusters.add(cluster); + } + task.getLogger().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/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java new file mode 100644 index 0000000000000..f82d4fb914dc6 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java @@ -0,0 +1,71 @@ +/* + * 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.clusterformation; + +import org.gradle.api.NamedDomainObjectContainer; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.Task; + +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"; + + @Override + public void apply(Project project) { + NamedDomainObjectContainer container = project.container( + ElasticsearchCluster.class, + (name) -> new ElasticsearchCluster(name, project.getLogger()) + ); + 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((ElasticsearchCluster cluster) -> + project.getLogger().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(ElasticsearchCluster::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/clusterformation/ElasticsearchCluster.java b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java new file mode 100644 index 0000000000000..80fd7320c508a --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java @@ -0,0 +1,105 @@ +/* + * 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.clusterformation; + +import org.gradle.api.logging.Logger; + +import javax.inject.Inject; +import java.io.File; +import java.util.Objects; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +public class ElasticsearchCluster { + + private final String name; + + private File distribution; + + private final AtomicInteger noOfClaims = new AtomicInteger(); + private final AtomicBoolean started = new AtomicBoolean(false); + + private final Logger logger; + + public ElasticsearchCluster(String name, Logger logger) { + this.name = name; + this.logger = logger; + } + + public String getName() { + return name; + } + + public File getDistribution() { + return distribution; + } + + public void setDistribution(File distribution) { + this.distribution = distribution; + } + + 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 + */ + 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. + */ + public void unClaimAndStop() { + int decrementedClaims = noOfClaims.decrementAndGet(); + if (decrementedClaims > 0) { + logger.lifecycle("Asked to unClaimAndStop {}, since cluster still has {} claim it will not be stopped", + name, decrementedClaims + ); + return; + } + if (started.get() == false) { + logger.lifecycle("Asked to unClaimAndStop, but cluster was not running: {}", name); + return; + } + logger.lifecycle("Stopping {}, since no of claims is {}", name, decrementedClaims); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ElasticsearchCluster that = (ElasticsearchCluster) o; + return Objects.equals(name, that.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} From 5b448cb169f0d385cccd0fde859ae0e697c1e2d3 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Mon, 11 Jun 2018 10:58:37 +0300 Subject: [PATCH 2/6] Add integration test for cluster formation --- .../ElasticsearchCluster.java | 6 +- .../ClusterformationPluginIT.java | 135 ++++++++++++++++++ .../test/GradleIntegrationTestCase.java | 11 ++ .../src/testKit/clusterformation/build.gradle | 41 ++++++ 4 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java create mode 100644 buildSrc/src/testKit/clusterformation/build.gradle diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java index 80fd7320c508a..8124af862b807 100644 --- a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java +++ b/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java @@ -78,16 +78,14 @@ public Future start() { public void unClaimAndStop() { int decrementedClaims = noOfClaims.decrementAndGet(); if (decrementedClaims > 0) { - logger.lifecycle("Asked to unClaimAndStop {}, since cluster still has {} claim it will not be stopped", - name, decrementedClaims - ); + 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 {}, since no of claims is {}", name, decrementedClaims); + logger.lifecycle("Stopping {}, number of claims is {}", name, decrementedClaims); } @Override diff --git a/buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java b/buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java new file mode 100644 index 0000000000000..35d270751b847 --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java @@ -0,0 +1,135 @@ +/* + * 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.clusterformation; + +import org.elasticsearch.gradle.GradleIntegrationTests; +import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.GradleRunner; +import org.gradle.testkit.runner.TaskOutcome; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ClusterformationPluginIT extends GradleIntegrationTests { + + @Test + public void listClusters() { + BuildResult result = GradleRunner.create() + .withProjectDir(getProjectDir("clusterformation")) + .withArguments("listElasticSearchClusters", "-s") + .withPluginClasspath() + .build(); + + assertEquals(TaskOutcome.SUCCESS, result.task(":listElasticSearchClusters").getOutcome()); + assertOutputContains( + result.getOutput(), + " * myTestCluster:" + ); + + } + + @Test + public void useClusterByOne() { + 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" + ); + } + + @Test + public void useClusterByTwo() { + 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" + ); + } + + @Test + public void useClusterByUpToDateTask() { + 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"); + } + + @Test + public void useClusterBySkippedTask() { + 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"); + } + + @Test + public void useClusterBySkippedAndWorkingTask() { + 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..c91a87853b642 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) { diff --git a/buildSrc/src/testKit/clusterformation/build.gradle b/buildSrc/src/testKit/clusterformation/build.gradle new file mode 100644 index 0000000000000..6d6e741de5935 --- /dev/null +++ b/buildSrc/src/testKit/clusterformation/build.gradle @@ -0,0 +1,41 @@ +plugins { + id 'elasticsearch.clusterformation' +} + +elasticSearchClusters { + myTestCluster { + distribution = file('foo') + } +} + +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 ed9b150894ab157fe5de61973fdfdc8b7dc44e50 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Tue, 26 Jun 2018 17:19:18 +0300 Subject: [PATCH 3/6] Add dry run test, reorg packages --- buildSrc/build.gradle | 2 +- ...ClusterFormationTaskExecutionListener.java | 2 +- .../ClusterFormationTaskExtension.java | 5 +-- .../ClusterformationPlugin.java | 2 +- .../ElasticsearchCluster.java | 3 +- .../ClusterformationPluginIT.java | 43 +++++++++++-------- .../test/GradleIntegrationTestCase.java | 33 ++++++++++++++ 7 files changed, 64 insertions(+), 26 deletions(-) rename buildSrc/src/main/java/org/elasticsearch/{ => gradle}/clusterformation/ClusterFormationTaskExecutionListener.java (97%) rename buildSrc/src/main/java/org/elasticsearch/{ => gradle}/clusterformation/ClusterFormationTaskExtension.java (93%) rename buildSrc/src/main/java/org/elasticsearch/{ => gradle}/clusterformation/ClusterformationPlugin.java (98%) rename buildSrc/src/main/java/org/elasticsearch/{ => gradle}/clusterformation/ElasticsearchCluster.java (97%) rename buildSrc/src/test/java/org/elasticsearch/{ => gradle}/clusterformation/ClusterformationPluginIT.java (82%) diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 878d2cc6ffc78..1aab0c8399e60 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -29,7 +29,7 @@ gradlePlugin { plugins { simplePlugin { id = 'elasticsearch.clusterformation' - implementationClass = 'org.elasticsearch.clusterformation.ClusterformationPlugin' + implementationClass = 'org.elasticsearch.gradle.clusterformation.ClusterformationPlugin' } } } diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java similarity index 97% rename from buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java index 6f0158bc6855b..458c9da2310c1 100644 --- a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExecutionListener.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.clusterformation; +package org.elasticsearch.gradle.clusterformation; import org.gradle.api.Task; import org.gradle.api.execution.TaskActionListener; diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java similarity index 93% rename from buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java index 3d2b612593723..19f2f7c0b3a91 100644 --- a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterFormationTaskExtension.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java @@ -16,16 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.clusterformation; +package org.elasticsearch.gradle.clusterformation; import org.gradle.api.Task; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; public class ClusterFormationTaskExtension { diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java similarity index 98% rename from buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java index f82d4fb914dc6..daf057f8a6f97 100644 --- a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.clusterformation; +package org.elasticsearch.gradle.clusterformation; import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Plugin; diff --git a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchCluster.java similarity index 97% rename from buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchCluster.java index 8124af862b807..7844d2b8d74b7 100644 --- a/buildSrc/src/main/java/org/elasticsearch/clusterformation/ElasticsearchCluster.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchCluster.java @@ -16,11 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.clusterformation; +package org.elasticsearch.gradle.clusterformation; import org.gradle.api.logging.Logger; -import javax.inject.Inject; import java.io.File; import java.util.Objects; import java.util.concurrent.Future; diff --git a/buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java b/buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java similarity index 82% rename from buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java rename to buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java index 35d270751b847..c690557537dfb 100644 --- a/buildSrc/src/test/java/org/elasticsearch/clusterformation/ClusterformationPluginIT.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/clusterformation/ClusterformationPluginIT.java @@ -16,20 +16,19 @@ * specific language governing permissions and limitations * under the License. */ -package org.elasticsearch.clusterformation; +package org.elasticsearch.gradle.clusterformation; -import org.elasticsearch.gradle.GradleIntegrationTests; +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 org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -public class ClusterformationPluginIT extends GradleIntegrationTests { +public class ClusterformationPluginIT extends GradleIntegrationTestCase { - @Test - public void listClusters() { + public void testListClusters() { BuildResult result = GradleRunner.create() .withProjectDir(getProjectDir("clusterformation")) .withArguments("listElasticSearchClusters", "-s") @@ -44,8 +43,7 @@ public void listClusters() { } - @Test - public void useClusterByOne() { + public void testUseClusterByOne() { BuildResult result = GradleRunner.create() .withProjectDir(getProjectDir("clusterformation")) .withArguments("user1", "-s") @@ -60,8 +58,22 @@ public void useClusterByOne() { ); } - @Test - public void useClusterByTwo() { + 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") @@ -78,8 +90,7 @@ public void useClusterByTwo() { ); } - @Test - public void useClusterByUpToDateTask() { + public void testUseClusterByUpToDateTask() { BuildResult result = GradleRunner.create() .withProjectDir(getProjectDir("clusterformation")) .withArguments("upToDate1", "upToDate2", "-s") @@ -96,8 +107,7 @@ public void useClusterByUpToDateTask() { assertOutputDoesNotContain(result.getOutput(), "Starting cluster: myTestCluster"); } - @Test - public void useClusterBySkippedTask() { + public void testUseClusterBySkippedTask() { BuildResult result = GradleRunner.create() .withProjectDir(getProjectDir("clusterformation")) .withArguments("skipped1", "skipped2", "-s") @@ -114,8 +124,7 @@ public void useClusterBySkippedTask() { assertOutputDoesNotContain(result.getOutput(), "Starting cluster: myTestCluster"); } - @Test - public void useClusterBySkippedAndWorkingTask() { + public void tetUseClusterBySkippedAndWorkingTask() { BuildResult result = GradleRunner.create() .withProjectDir(getProjectDir("clusterformation")) .withArguments("skipped1", "user1", "-s") 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 c91a87853b642..0627f8d732603 100644 --- a/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java +++ b/buildSrc/src/test/java/org/elasticsearch/gradle/test/GradleIntegrationTestCase.java @@ -24,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); + } + } + } From a21aa6fe87a977e6e7f7b3e630af1a16896e0593 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Wed, 27 Jun 2018 17:20:56 +0300 Subject: [PATCH 4/6] Move Version to Java, preserver behavior Use the version to configure cluster formation with Version and Distribution for convenience --- build.gradle | 6 +- .../elasticsearch/gradle/BuildPlugin.groovy | 2 +- .../org/elasticsearch/gradle/Version.groovy | 147 -------------- .../gradle/VersionCollection.groovy | 1 + .../gradle/VersionProperties.java | 2 + .../gradle/test/ClusterConfiguration.groovy | 2 +- .../gradle/test/ClusterFormationTasks.groovy | 2 +- .../elasticsearch/gradle/test/NodeInfo.groovy | 4 +- .../vagrant/VagrantPropertiesExtension.groovy | 2 +- .../gradle/vagrant/VagrantTestPlugin.groovy | 2 +- .../elasticsearch/GradleServicesAdapter.java | 68 +++++++ ...ClusterFormationTaskExecutionListener.java | 4 +- .../ClusterFormationTaskExtension.java | 14 +- .../ClusterformationPlugin.java | 17 +- .../ElasticsearchConfiguration.java | 46 +++++ ...rchCluster.java => ElasticsearchNode.java} | 52 +++-- .../org/elasticsearch/model/Distribution.java | 36 ++++ .../java/org/elasticsearch/model/Version.java | 181 +++++++++++++++++ .../gradle/VersionCollectionTests.groovy | 4 +- .../org/elasticsearch/model/VersionTest.java | 185 ++++++++++++++++++ .../src/testKit/clusterformation/build.gradle | 2 +- distribution/bwc/build.gradle | 2 +- qa/full-cluster-restart/build.gradle | 4 +- qa/mixed-cluster/build.gradle | 2 +- qa/rolling-upgrade/build.gradle | 2 +- qa/verify-version-constants/build.gradle | 3 +- settings.gradle | 2 - x-pack/build.gradle | 2 - x-pack/qa/full-cluster-restart/build.gradle | 4 +- x-pack/qa/rolling-upgrade-basic/build.gradle | 5 +- x-pack/qa/rolling-upgrade/build.gradle | 2 +- 31 files changed, 604 insertions(+), 203 deletions(-) delete mode 100644 buildSrc/src/main/groovy/org/elasticsearch/gradle/Version.groovy create mode 100644 buildSrc/src/main/java/org/elasticsearch/GradleServicesAdapter.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchConfiguration.java rename buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/{ElasticsearchCluster.java => ElasticsearchNode.java} (68%) create mode 100644 buildSrc/src/main/java/org/elasticsearch/model/Distribution.java create mode 100644 buildSrc/src/main/java/org/elasticsearch/model/Version.java create mode 100644 buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java diff --git a/build.gradle b/build.gradle index 862943b50f5eb..5eea9d87aa717 100644 --- a/build.gradle +++ b/build.gradle @@ -22,9 +22,9 @@ import org.apache.tools.ant.taskdefs.condition.Os import org.apache.tools.ant.filters.ReplaceTokens import org.elasticsearch.gradle.BuildPlugin import org.elasticsearch.gradle.LoggedExec -import org.elasticsearch.gradle.Version import org.elasticsearch.gradle.VersionCollection import org.elasticsearch.gradle.VersionProperties +import org.elasticsearch.model.Version import org.gradle.plugins.ide.eclipse.model.SourceFolder import org.gradle.api.tasks.wrapper.Wrapper @@ -145,7 +145,9 @@ task verifyVersions { new URL('https://repo1.maven.org/maven2/org/elasticsearch/elasticsearch/maven-metadata.xml').openStream().withStream { s -> xml = new XmlParser().parse(s) } - Set knownVersions = new TreeSet<>(xml.versioning.versions.version.collect { it.text() }.findAll { it ==~ /\d+\.\d+\.\d+/ }.collect { Version.fromString(it) }) + Set knownVersions = new TreeSet<>(xml.versioning.versions.version.collect { it.text() } + .findAll { it ==~ /\d+\.\d+\.\d+/ } + .collect { Version.fromString(it) }) // Limit the known versions to those that should be index compatible, and are not future versions knownVersions = knownVersions.findAll { it.major >= bwcVersions.currentVersion.major - 1 && it.before(VersionProperties.elasticsearch) } diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index 8a2b1b798e163..cfd3da249f0e6 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -19,11 +19,11 @@ package org.elasticsearch.gradle import com.carrotsearch.gradle.junit4.RandomizedTestingTask -import nebula.plugin.extraconfigurations.ProvidedBasePlugin import org.apache.tools.ant.taskdefs.condition.Os import org.eclipse.jgit.lib.Constants import org.eclipse.jgit.lib.RepositoryBuilder import org.elasticsearch.gradle.precommit.PrecommitTasks +import org.elasticsearch.model.Version import org.gradle.api.GradleException import org.gradle.api.InvalidUserDataException import org.gradle.api.JavaVersion 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/groovy/org/elasticsearch/gradle/VersionCollection.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionCollection.groovy index 7d5b793254fe4..dd1104642030e 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionCollection.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionCollection.groovy @@ -19,6 +19,7 @@ package org.elasticsearch.gradle +import org.elasticsearch.model.Version import org.gradle.api.GradleException import org.gradle.api.InvalidUserDataException diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java b/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java index 9ee597eb25ad8..0d6b3c0e45534 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/VersionProperties.java @@ -1,5 +1,7 @@ package org.elasticsearch.gradle; +import org.elasticsearch.model.Version; + import java.io.IOException; import java.io.InputStream; import java.util.HashMap; diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy index 5c363ac043aff..3e593bb2cfc53 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterConfiguration.groovy @@ -18,7 +18,7 @@ */ package org.elasticsearch.gradle.test -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import org.gradle.api.GradleException import org.gradle.api.Project import org.gradle.api.tasks.Input 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 be0fb3a07c699..c00d1b26d7120 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy @@ -22,7 +22,7 @@ import org.apache.tools.ant.DefaultLogger 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.model.Version import org.elasticsearch.gradle.VersionProperties import org.elasticsearch.gradle.plugin.PluginBuildPlugin diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/NodeInfo.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/NodeInfo.groovy index 5e67dfa55cfd4..b65739c0f13ac 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/NodeInfo.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/test/NodeInfo.groovy @@ -22,7 +22,7 @@ package org.elasticsearch.gradle.test import com.sun.jna.Native import com.sun.jna.WString import org.apache.tools.ant.taskdefs.condition.Os -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import org.gradle.api.InvalidUserDataException import org.gradle.api.Project @@ -30,8 +30,6 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths -import static org.elasticsearch.gradle.BuildPlugin.getJavaHome - /** * A container for the files and configuration associated with a single node in a test cluster. */ diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy index e9b664a5a31b7..fbdb2288d8da5 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantPropertiesExtension.groovy @@ -18,7 +18,7 @@ */ package org.elasticsearch.gradle.vagrant -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import org.gradle.api.tasks.Input class VagrantPropertiesExtension { 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 455d30f95db32..1308b7fcb1beb 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantTestPlugin.groovy @@ -3,7 +3,7 @@ package org.elasticsearch.gradle.vagrant 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.model.Version import org.gradle.api.* import org.gradle.api.artifacts.dsl.RepositoryHandler import org.gradle.api.execution.TaskExecutionAdapter 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/clusterformation/ClusterFormationTaskExecutionListener.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExecutionListener.java index 458c9da2310c1..4cddd1d470cda 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(ElasticsearchCluster::unClaimAndStop); + 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(ElasticsearchCluster::start); + ClusterformationPlugin.getTaskExtension(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 19f2f7c0b3a91..356f16c40939f 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterFormationTaskExtension.java @@ -19,6 +19,8 @@ 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; @@ -28,27 +30,29 @@ public class ClusterFormationTaskExtension { private final Task task; - private final List claimedClusters = new ArrayList<>(); + private final List claimedClusters = new ArrayList<>(); + + private final Logger logger = Logging.getLogger(ClusterFormationTaskExtension.class); public ClusterFormationTaskExtension(Task task) { this.task = task; } - public void use(ElasticsearchCluster cluster) { + 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)) { - task.getLogger().warn("{} already claimed cluster {} will not claim it again", + logger.warn("{} already claimed cluster {} will not claim it again", task.getPath(), cluster.getName() ); return; } claimedClusters.add(cluster); } - task.getLogger().info("CF: the {} task will use cluster: {}", task.getName(), cluster.getName()); + logger.info("CF: the {} task will use cluster: {}", task.getName(), cluster.getName()); } - public List getClaimedClusters() { + 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 index daf057f8a6f97..c0c34535bc3c2 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ClusterformationPlugin.java @@ -18,10 +18,13 @@ */ 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 { @@ -29,11 +32,13 @@ public class ClusterformationPlugin implements Plugin { 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( - ElasticsearchCluster.class, - (name) -> new ElasticsearchCluster(name, project.getLogger()) + NamedDomainObjectContainer container = project.container( + ElasticsearchNode.class, + (name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project)) ); project.getExtensions().add(EXTENSION_NAME, container); @@ -41,8 +46,8 @@ public void apply(Project project) { listTask.setGroup("ES cluster formation"); listTask.setDescription("Lists all ES clusters configured for this project"); listTask.doLast((Task task) -> - container.forEach((ElasticsearchCluster cluster) -> - project.getLogger().lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution()) + container.forEach((ElasticsearchConfiguration cluster) -> + logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution()) ) ); @@ -55,7 +60,7 @@ public void apply(Project project) { // 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(ElasticsearchCluster::claim) + getTaskExtension(task).getClaimedClusters().forEach(ElasticsearchConfiguration::claim) ) ); 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..0edcea1ce7ab6 --- /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.model.Distribution; +import org.elasticsearch.model.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/ElasticsearchCluster.java b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java similarity index 68% rename from buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchCluster.java rename to buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java index 7844d2b8d74b7..59d0beafa5414 100644 --- a/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchCluster.java +++ b/buildSrc/src/main/java/org/elasticsearch/gradle/clusterformation/ElasticsearchNode.java @@ -18,50 +18,71 @@ */ package org.elasticsearch.gradle.clusterformation; +import org.elasticsearch.GradleServicesAdapter; +import org.elasticsearch.model.Distribution; +import org.elasticsearch.model.Version; import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; -import java.io.File; import java.util.Objects; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -public class ElasticsearchCluster { +public class ElasticsearchNode implements ElasticsearchConfiguration { private final String name; - - private File distribution; - + 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 final Logger logger; + private Distribution distribution; + private Version version; - public ElasticsearchCluster(String name, Logger logger) { + public ElasticsearchNode(String name, GradleServicesAdapter services) { this.name = name; - this.logger = logger; + this.services = services; } + @Override public String getName() { return name; } - public File getDistribution() { + @Override + public Version getVersion() { + return version; + } + + @Override + public void setVersion(Version version) { + checkNotRunning(); + this.version = version; + } + + @Override + public Distribution getDistribution() { return distribution; } - public void setDistribution(File 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); @@ -74,10 +95,11 @@ public Future start() { /** * 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); + logger.lifecycle("Not stopping {}, since cluster still has {} claim(s)", name, decrementedClaims); return; } if (started.get() == false) { @@ -87,11 +109,17 @@ public void unClaimAndStop() { 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; - ElasticsearchCluster that = (ElasticsearchCluster) o; + ElasticsearchNode that = (ElasticsearchNode) o; return Objects.equals(name, that.name); } diff --git a/buildSrc/src/main/java/org/elasticsearch/model/Distribution.java b/buildSrc/src/main/java/org/elasticsearch/model/Distribution.java new file mode 100644 index 0000000000000..48fe3cce6a7d0 --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/model/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.model; + +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/model/Version.java b/buildSrc/src/main/java/org/elasticsearch/model/Version.java new file mode 100644 index 0000000000000..f9ac7d82d221d --- /dev/null +++ b/buildSrc/src/main/java/org/elasticsearch/model/Version.java @@ -0,0 +1,181 @@ +package org.elasticsearch.model; + +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/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy index 2901acf65220a..f8e7efc2af356 100644 --- a/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy +++ b/buildSrc/src/test/groovy/org/elasticsearch/gradle/VersionCollectionTests.groovy @@ -1,6 +1,7 @@ package org.elasticsearch.gradle import org.elasticsearch.gradle.test.GradleUnitTestCase +import org.elasticsearch.model.Version import org.junit.Test class VersionCollectionTests extends GradleUnitTestCase { @@ -223,7 +224,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/model/VersionTest.java b/buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java new file mode 100644 index 0000000000000..b484673f23e08 --- /dev/null +++ b/buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java @@ -0,0 +1,185 @@ +package org.elasticsearch.model; + +/* + * 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.test.GradleUnitTestCase; +import org.junit.Rule; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class VersionTest 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()); + } + +} diff --git a/buildSrc/src/testKit/clusterformation/build.gradle b/buildSrc/src/testKit/clusterformation/build.gradle index 6d6e741de5935..50faca17832a4 100644 --- a/buildSrc/src/testKit/clusterformation/build.gradle +++ b/buildSrc/src/testKit/clusterformation/build.gradle @@ -4,7 +4,7 @@ plugins { elasticSearchClusters { myTestCluster { - distribution = file('foo') + distribution = 'ZIP' } } diff --git a/distribution/bwc/build.gradle b/distribution/bwc/build.gradle index 42412c6230fa4..4815856856d50 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.model.Version import static org.elasticsearch.gradle.BuildPlugin.getJavaHome diff --git a/qa/full-cluster-restart/build.gradle b/qa/full-cluster-restart/build.gradle index ca8371e30e7ac..23aa2e3aa749e 100644 --- a/qa/full-cluster-restart/build.gradle +++ b/qa/full-cluster-restart/build.gradle @@ -18,9 +18,9 @@ */ -import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.VersionCollection + import org.elasticsearch.gradle.test.RestIntegTestTask +import org.elasticsearch.model.Version apply plugin: 'elasticsearch.standalone-test' diff --git a/qa/mixed-cluster/build.gradle b/qa/mixed-cluster/build.gradle index ac57d51def7c6..4775bce2c423d 100644 --- a/qa/mixed-cluster/build.gradle +++ b/qa/mixed-cluster/build.gradle @@ -18,7 +18,7 @@ */ import org.elasticsearch.gradle.test.RestIntegTestTask -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version apply plugin: 'elasticsearch.standalone-test' diff --git a/qa/rolling-upgrade/build.gradle b/qa/rolling-upgrade/build.gradle index bfd37863cc246..7ce1c01cdac51 100644 --- a/qa/rolling-upgrade/build.gradle +++ b/qa/rolling-upgrade/build.gradle @@ -18,7 +18,7 @@ */ import org.elasticsearch.gradle.test.RestIntegTestTask -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version apply plugin: 'elasticsearch.standalone-test' diff --git a/qa/verify-version-constants/build.gradle b/qa/verify-version-constants/build.gradle index 30c879ec6146e..168e7e4f8e5de 100644 --- a/qa/verify-version-constants/build.gradle +++ b/qa/verify-version-constants/build.gradle @@ -17,8 +17,7 @@ * under the License. */ -import java.util.Locale -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import org.elasticsearch.gradle.VersionProperties import org.elasticsearch.gradle.test.RestIntegTestTask diff --git a/settings.gradle b/settings.gradle index 7a72baf1c4195..759a16fa511f7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,3 @@ -import org.elasticsearch.gradle.Version - String dirName = rootProject.projectDir.name rootProject.name = dirName diff --git a/x-pack/build.gradle b/x-pack/build.gradle index 6a064ff5b7c64..f92ff93a4ed0f 100644 --- a/x-pack/build.gradle +++ b/x-pack/build.gradle @@ -1,6 +1,4 @@ -import org.elasticsearch.gradle.BuildPlugin import org.elasticsearch.gradle.plugin.PluginBuildPlugin -import org.elasticsearch.gradle.Version import org.elasticsearch.gradle.precommit.LicenseHeadersTask Project xpackRootProject = project diff --git a/x-pack/qa/full-cluster-restart/build.gradle b/x-pack/qa/full-cluster-restart/build.gradle index 78ac1436fd8bc..932279a369451 100644 --- a/x-pack/qa/full-cluster-restart/build.gradle +++ b/x-pack/qa/full-cluster-restart/build.gradle @@ -1,11 +1,9 @@ import org.elasticsearch.gradle.test.NodeInfo import org.elasticsearch.gradle.test.RestIntegTestTask -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import java.nio.charset.StandardCharsets -import java.nio.file.Paths import java.util.regex.Matcher - // Apply the java plugin to this project so the sources can be edited in an IDE apply plugin: 'elasticsearch.build' test.enabled = false diff --git a/x-pack/qa/rolling-upgrade-basic/build.gradle b/x-pack/qa/rolling-upgrade-basic/build.gradle index 6d5b250b460b7..993a7ab0d0e2f 100644 --- a/x-pack/qa/rolling-upgrade-basic/build.gradle +++ b/x-pack/qa/rolling-upgrade-basic/build.gradle @@ -1,8 +1,5 @@ -import org.elasticsearch.gradle.Version -import org.elasticsearch.gradle.test.NodeInfo import org.elasticsearch.gradle.test.RestIntegTestTask - -import java.nio.charset.StandardCharsets +import org.elasticsearch.model.Version apply plugin: 'elasticsearch.standalone-test' diff --git a/x-pack/qa/rolling-upgrade/build.gradle b/x-pack/qa/rolling-upgrade/build.gradle index 351f33b941227..210b77e165843 100644 --- a/x-pack/qa/rolling-upgrade/build.gradle +++ b/x-pack/qa/rolling-upgrade/build.gradle @@ -1,6 +1,6 @@ import org.elasticsearch.gradle.test.NodeInfo import org.elasticsearch.gradle.test.RestIntegTestTask -import org.elasticsearch.gradle.Version +import org.elasticsearch.model.Version import java.nio.charset.StandardCharsets import java.util.regex.Matcher From 59526394615ff7f0e3ae103f98cb43a43aafe9eb Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Thu, 28 Jun 2018 16:03:55 +0300 Subject: [PATCH 5/6] Remove oversigth adter rebase --- .../gradle/test/GradleIntegrationTestCase.java | 10 +--------- 1 file changed, 1 insertion(+), 9 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..fa3a36fafcf2e 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 d9112fd10741fc37e066b16c5866180069ba10e3 Mon Sep 17 00:00:00 2001 From: Alpar Torok Date: Thu, 28 Jun 2018 16:58:14 +0300 Subject: [PATCH 6/6] Fix precommit checks --- buildSrc/src/main/java/org/elasticsearch/model/Version.java | 4 +++- .../model/{VersionTest.java => VersionTests.java} | 6 +----- 2 files changed, 4 insertions(+), 6 deletions(-) rename buildSrc/src/test/java/org/elasticsearch/model/{VersionTest.java => VersionTests.java} (97%) diff --git a/buildSrc/src/main/java/org/elasticsearch/model/Version.java b/buildSrc/src/main/java/org/elasticsearch/model/Version.java index f9ac7d82d221d..85355226c4aa4 100644 --- a/buildSrc/src/main/java/org/elasticsearch/model/Version.java +++ b/buildSrc/src/main/java/org/elasticsearch/model/Version.java @@ -63,7 +63,9 @@ 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]"); + throw new IllegalArgumentException( + "Invalid version format: '" + s + "'. Should be major.minor.revision[-(alpha|beta|rc)Number][-SNAPSHOT]" + ); } return new Version( diff --git a/buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java b/buildSrc/src/test/java/org/elasticsearch/model/VersionTests.java similarity index 97% rename from buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java rename to buildSrc/src/test/java/org/elasticsearch/model/VersionTests.java index b484673f23e08..b36e3b48f3c4c 100644 --- a/buildSrc/src/test/java/org/elasticsearch/model/VersionTest.java +++ b/buildSrc/src/test/java/org/elasticsearch/model/VersionTests.java @@ -27,11 +27,7 @@ import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class VersionTest extends GradleUnitTestCase { +public class VersionTests extends GradleUnitTestCase { @Rule public ExpectedException expectedEx = ExpectedException.none();