Skip to content

Commit f097446

Browse files
authored
Fix/30904 cluster formation part2 (#32877)
Gradle integration for the Cluster formation plugin with ref counting
1 parent 214652d commit f097446

File tree

8 files changed

+584
-0
lines changed

8 files changed

+584
-0
lines changed

buildSrc/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ plugins {
2323
id 'groovy'
2424
}
2525

26+
gradlePlugin {
27+
plugins {
28+
simplePlugin {
29+
id = 'elasticsearch.clusterformation'
30+
implementationClass = 'org.elasticsearch.gradle.clusterformation.ClusterformationPlugin'
31+
}
32+
}
33+
}
34+
2635
group = 'org.elasticsearch.gradle'
2736

2837
String minimumGradleVersion = file('src/main/resources/minimumGradleVersion').text.trim()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch;
20+
21+
import org.gradle.api.Action;
22+
import org.gradle.api.Project;
23+
import org.gradle.api.file.CopySpec;
24+
import org.gradle.api.file.FileTree;
25+
import org.gradle.api.tasks.WorkResult;
26+
import org.gradle.process.ExecResult;
27+
import org.gradle.process.JavaExecSpec;
28+
29+
import java.io.File;
30+
31+
/**
32+
* Facilitate access to Gradle services without a direct dependency on Project.
33+
*
34+
* In a future release Gradle will offer service injection, this adapter plays that role until that time.
35+
* It exposes the service methods that are part of the public API as the classes implementing them are not.
36+
* Today service injection is <a href="https://github.com/gradle/gradle/issues/2363">not available</a> for
37+
* extensions.
38+
*
39+
* Everything exposed here must be thread safe. That is the very reason why project is not passed in directly.
40+
*/
41+
public class GradleServicesAdapter {
42+
43+
public final Project project;
44+
45+
public GradleServicesAdapter(Project project) {
46+
this.project = project;
47+
}
48+
49+
public static GradleServicesAdapter getInstance(Project project) {
50+
return new GradleServicesAdapter(project);
51+
}
52+
53+
public WorkResult copy(Action<? super CopySpec> action) {
54+
return project.copy(action);
55+
}
56+
57+
public WorkResult sync(Action<? super CopySpec> action) {
58+
return project.sync(action);
59+
}
60+
61+
public ExecResult javaexec(Action<? super JavaExecSpec> action) {
62+
return project.javaexec(action);
63+
}
64+
65+
public FileTree zipTree(File zipPath) {
66+
return project.zipTree(zipPath);
67+
}
68+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle;
20+
21+
public enum Distribution {
22+
23+
INTEG_TEST("integ-test-zip"),
24+
ZIP("zip"),
25+
ZIP_OSS("zip-oss");
26+
27+
private final String name;
28+
29+
Distribution(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle.clusterformation;
20+
21+
import groovy.lang.Closure;
22+
import org.elasticsearch.GradleServicesAdapter;
23+
import org.gradle.api.NamedDomainObjectContainer;
24+
import org.gradle.api.Plugin;
25+
import org.gradle.api.Project;
26+
import org.gradle.api.Task;
27+
import org.gradle.api.execution.TaskActionListener;
28+
import org.gradle.api.execution.TaskExecutionListener;
29+
import org.gradle.api.logging.Logger;
30+
import org.gradle.api.logging.Logging;
31+
import org.gradle.api.plugins.ExtraPropertiesExtension;
32+
import org.gradle.api.tasks.TaskState;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collections;
36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
40+
public class ClusterformationPlugin implements Plugin<Project> {
41+
42+
public static final String LIST_TASK_NAME = "listElasticSearchClusters";
43+
public static final String EXTENSION_NAME = "elasticSearchClusters";
44+
45+
private final Logger logger = Logging.getLogger(ClusterformationPlugin.class);
46+
47+
@Override
48+
public void apply(Project project) {
49+
NamedDomainObjectContainer<? extends ElasticsearchConfiguration> container = project.container(
50+
ElasticsearchNode.class,
51+
(name) -> new ElasticsearchNode(name, GradleServicesAdapter.getInstance(project))
52+
);
53+
project.getExtensions().add(EXTENSION_NAME, container);
54+
55+
Task listTask = project.getTasks().create(LIST_TASK_NAME);
56+
listTask.setGroup("ES cluster formation");
57+
listTask.setDescription("Lists all ES clusters configured for this project");
58+
listTask.doLast((Task task) ->
59+
container.forEach((ElasticsearchConfiguration cluster) ->
60+
logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getDistribution())
61+
)
62+
);
63+
64+
Map<Task, List<ElasticsearchConfiguration>> taskToCluster = new HashMap<>();
65+
66+
// register an extension for all current and future tasks, so that any task can declare that it wants to use a
67+
// specific cluster.
68+
project.getTasks().all((Task task) ->
69+
task.getExtensions().findByType(ExtraPropertiesExtension.class)
70+
.set(
71+
"useCluster",
72+
new Closure<Void>(this, this) {
73+
public void doCall(ElasticsearchConfiguration conf) {
74+
taskToCluster.computeIfAbsent(task, k -> new ArrayList<>()).add(conf);
75+
}
76+
})
77+
);
78+
79+
project.getGradle().getTaskGraph().whenReady(taskExecutionGraph ->
80+
taskExecutionGraph.getAllTasks()
81+
.forEach(task ->
82+
taskToCluster.getOrDefault(task, Collections.emptyList()).forEach(ElasticsearchConfiguration::claim)
83+
)
84+
);
85+
project.getGradle().addListener(
86+
new TaskActionListener() {
87+
@Override
88+
public void beforeActions(Task task) {
89+
// we only start the cluster before the actions, so we'll not start it if the task is up-to-date
90+
taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::start);
91+
}
92+
@Override
93+
public void afterActions(Task task) {}
94+
}
95+
);
96+
project.getGradle().addListener(
97+
new TaskExecutionListener() {
98+
@Override
99+
public void afterExecute(Task task, TaskState state) {
100+
// always un-claim the cluster, even if _this_ task is up-to-date, as others might not have been and caused the
101+
// cluster to start.
102+
taskToCluster.getOrDefault(task, new ArrayList<>()).forEach(ElasticsearchConfiguration::unClaimAndStop);
103+
}
104+
@Override
105+
public void beforeExecute(Task task) {}
106+
}
107+
);
108+
}
109+
110+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.gradle.clusterformation;
20+
21+
import org.elasticsearch.gradle.Distribution;
22+
import org.elasticsearch.gradle.Version;
23+
24+
import java.util.concurrent.Future;
25+
26+
public interface ElasticsearchConfiguration {
27+
String getName();
28+
29+
Version getVersion();
30+
31+
void setVersion(Version version);
32+
33+
default void setVersion(String version) {
34+
setVersion(Version.fromString(version));
35+
}
36+
37+
Distribution getDistribution();
38+
39+
void setDistribution(Distribution distribution);
40+
41+
void claim();
42+
43+
Future<Void> start();
44+
45+
void unClaimAndStop();
46+
}

0 commit comments

Comments
 (0)