[Build] Simplify testclusters configuration (#34334)

Remove interface, prefer straught implementaton instead.
This commit is contained in:
Alpar Torok 2018-10-23 12:13:49 +03:00 committed by GitHub
parent 1eb76f16b1
commit 5fb4f32c91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 62 deletions

View File

@ -1,46 +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.testclusters;
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<Void> start();
void unClaimAndStop();
}

View File

@ -29,7 +29,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class ElasticsearchNode implements ElasticsearchConfiguration { public class ElasticsearchNode {
private final String name; private final String name;
private final GradleServicesAdapter services; private final GradleServicesAdapter services;
@ -45,34 +45,28 @@ public class ElasticsearchNode implements ElasticsearchConfiguration {
this.services = services; this.services = services;
} }
@Override
public String getName() { public String getName() {
return name; return name;
} }
@Override
public Version getVersion() { public Version getVersion() {
return version; return version;
} }
@Override
public void setVersion(Version version) { public void setVersion(Version version) {
checkNotRunning(); checkNotRunning();
this.version = version; this.version = version;
} }
@Override
public Distribution getDistribution() { public Distribution getDistribution() {
return distribution; return distribution;
} }
@Override
public void setDistribution(Distribution distribution) { public void setDistribution(Distribution distribution) {
checkNotRunning(); checkNotRunning();
this.distribution = distribution; this.distribution = distribution;
} }
@Override
public void claim() { public void claim() {
noOfClaims.incrementAndGet(); noOfClaims.incrementAndGet();
} }
@ -82,7 +76,6 @@ public class ElasticsearchNode implements ElasticsearchConfiguration {
* *
* @return future of thread running in the background * @return future of thread running in the background
*/ */
@Override
public Future<Void> start() { public Future<Void> start() {
if (started.getAndSet(true)) { if (started.getAndSet(true)) {
logger.lifecycle("Already started cluster: {}", name); logger.lifecycle("Already started cluster: {}", name);
@ -95,7 +88,6 @@ public class ElasticsearchNode implements ElasticsearchConfiguration {
/** /**
* Stops a running cluster if it's not claimed. Does nothing otherwise. * Stops a running cluster if it's not claimed. Does nothing otherwise.
*/ */
@Override
public void unClaimAndStop() { public void unClaimAndStop() {
int decrementedClaims = noOfClaims.decrementAndGet(); int decrementedClaims = noOfClaims.decrementAndGet();
if (decrementedClaims > 0) { if (decrementedClaims > 0) {

View File

@ -46,7 +46,7 @@ public class TestClustersPlugin implements Plugin<Project> {
@Override @Override
public void apply(Project project) { public void apply(Project project) {
NamedDomainObjectContainer<? extends ElasticsearchConfiguration> container = project.container( NamedDomainObjectContainer<? extends ElasticsearchNode> container = project.container(
ElasticsearchNode.class, ElasticsearchNode.class,
(name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project)) (name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project))
); );
@ -56,12 +56,12 @@ public class TestClustersPlugin implements Plugin<Project> {
listTask.setGroup("ES cluster formation"); listTask.setGroup("ES cluster formation");
listTask.setDescription("Lists all ES clusters configured for this project"); listTask.setDescription("Lists all ES clusters configured for this project");
listTask.doLast((Task task) -> listTask.doLast((Task task) ->
container.forEach((ElasticsearchConfiguration cluster) -> container.forEach((ElasticsearchNode cluster) ->
logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution()) logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution())
) )
); );
Map<Task, List<ElasticsearchConfiguration>> taskToCluster = new HashMap<>(); Map<Task, List<ElasticsearchNode>> taskToCluster = new HashMap<>();
// register an extension for all current and future tasks, so that any task can declare that it wants to use a // register an extension for all current and future tasks, so that any task can declare that it wants to use a
// specific cluster. // specific cluster.
@ -70,7 +70,7 @@ public class TestClustersPlugin implements Plugin<Project> {
.set( .set(
"useCluster", "useCluster",
new Closure<Void>(this, this) { new Closure<Void>(this, this) {
public void doCall(ElasticsearchConfiguration conf) { public void doCall(ElasticsearchNode conf) {
taskToCluster.computeIfAbsent(task, k -> new ArrayList<>()).add(conf); taskToCluster.computeIfAbsent(task, k -> new ArrayList<>()).add(conf);
} }
}) })
@ -79,7 +79,7 @@ public class TestClustersPlugin implements Plugin<Project> {
project.getGradle().getTaskGraph().whenReady(taskExecutionGraph -> project.getGradle().getTaskGraph().whenReady(taskExecutionGraph ->
taskExecutionGraph.getAllTasks() taskExecutionGraph.getAllTasks()
.forEach(task -> .forEach(task ->
taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchConfiguration::claim) taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchNode::claim)
) )
); );
project.getGradle().addListener( project.getGradle().addListener(
@ -87,7 +87,7 @@ public class TestClustersPlugin implements Plugin<Project> {
@Override @Override
public void beforeActions(Task task) { 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 // 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); taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchNode::start);
} }
@Override @Override
public void afterActions(Task task) {} public void afterActions(Task task) {}
@ -99,7 +99,7 @@ public class TestClustersPlugin implements Plugin<Project> {
public void afterExecute(Task task, TaskState state) { 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 // 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. // cluster to start.
taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::unClaimAndStop); taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchNode::unClaimAndStop);
} }
@Override @Override
public void beforeExecute(Task task) {} public void beforeExecute(Task task) {}