Skip to content

Commit ed4b701

Browse files
authored
Replace immediate task creations by using task avoidance api (#60071) (#60504)
- Replace immediate task creations by using task avoidance api - One step closer to #56610 - Still many tasks are created during configuration phase. Tackled in separate steps
1 parent a721d6d commit ed4b701

File tree

57 files changed

+518
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+518
-484
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/doc/DocsTestPlugin.groovy

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.elasticsearch.gradle.VersionProperties
2424
import org.gradle.api.Plugin
2525
import org.gradle.api.Project
2626
import org.gradle.api.Task
27+
import org.gradle.api.tasks.TaskProvider
2728

2829
/**
2930
* Sets up tests for documentation.
@@ -42,7 +43,7 @@ class DocsTestPlugin implements Plugin<Project> {
4243
project.testClusters.integTest.nameCustomization = { it.replace("integTest", "node") }
4344
// Docs are published separately so no need to assemble
4445
project.tasks.assemble.enabled = false
45-
Map<String, String> defaultSubstitutions = [
46+
Map<String, String> commonDefaultSubstitutions = [
4647
/* These match up with the asciidoc syntax for substitutions but
4748
* the values may differ. In particular {version} needs to resolve
4849
* to the version being built for testing but needs to resolve to
@@ -53,26 +54,26 @@ class DocsTestPlugin implements Plugin<Project> {
5354
'\\{build_flavor\\}' : distribution,
5455
'\\{build_type\\}' : OS.conditionalString().onWindows({"zip"}).onUnix({"tar"}).supply(),
5556
]
56-
Task listSnippets = project.tasks.create('listSnippets', SnippetsTask)
57-
listSnippets.group 'Docs'
58-
listSnippets.description 'List each snippet'
59-
listSnippets.defaultSubstitutions = defaultSubstitutions
60-
listSnippets.perSnippet { println(it.toString()) }
61-
62-
Task listConsoleCandidates = project.tasks.create(
63-
'listConsoleCandidates', SnippetsTask)
64-
listConsoleCandidates.group 'Docs'
65-
listConsoleCandidates.description
66-
'List snippets that probably should be marked // CONSOLE'
67-
listConsoleCandidates.defaultSubstitutions = defaultSubstitutions
68-
listConsoleCandidates.perSnippet {
69-
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
70-
println(it.toString())
57+
project.tasks.register('listSnippets', SnippetsTask) {
58+
group 'Docs'
59+
description 'List each snippet'
60+
defaultSubstitutions = commonDefaultSubstitutions
61+
perSnippet { println(it.toString()) }
62+
}
63+
project.tasks.register('listConsoleCandidates', SnippetsTask) {
64+
group 'Docs'
65+
description
66+
'List snippets that probably should be marked // CONSOLE'
67+
defaultSubstitutions = commonDefaultSubstitutions
68+
perSnippet {
69+
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
70+
println(it.toString())
71+
}
7172
}
7273
}
7374

74-
Task buildRestTests = project.tasks.create(
75-
'buildRestTests', RestTestsFromSnippetsTask)
76-
buildRestTests.defaultSubstitutions = defaultSubstitutions
75+
project.tasks.register('buildRestTests', RestTestsFromSnippetsTask) {
76+
defaultSubstitutions = commonDefaultSubstitutions
77+
}
7778
}
7879
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/plugin/PluginBuildPlugin.groovy

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ class PluginBuildPlugin implements Plugin<Project> {
6767

6868
createIntegTestTask(project)
6969
createBundleTasks(project, extension)
70-
project.tasks.integTest.dependsOn(project.tasks.bundlePlugin)
70+
project.tasks.named("integTest").configure {
71+
it.dependsOn(project.tasks.named("bundlePlugin"))
72+
}
7173
if (isModule) {
7274
project.testClusters.integTest.module(project.tasks.bundlePlugin.archiveFile)
7375
} else {
@@ -80,7 +82,7 @@ class PluginBuildPlugin implements Plugin<Project> {
8082
if (project.findProject(":modules:${pluginName}") != null) {
8183
project.integTest.dependsOn(project.project(":modules:${pluginName}").tasks.bundlePlugin)
8284
project.testClusters.integTest.module(
83-
project.project(":modules:${pluginName}").tasks.bundlePlugin.archiveFile
85+
project.project(":modules:${pluginName}").tasks.bundlePlugin.archiveFile
8486
)
8587
}
8688
}
@@ -101,15 +103,15 @@ class PluginBuildPlugin implements Plugin<Project> {
101103
}
102104

103105
Map<String, String> properties = [
104-
'name' : extension1.name,
105-
'description' : extension1.description,
106-
'version' : extension1.version,
107-
'elasticsearchVersion': Version.fromString(VersionProperties.elasticsearch).toString(),
108-
'javaVersion' : project.targetCompatibility as String,
109-
'classname' : extension1.classname,
110-
'extendedPlugins' : extension1.extendedPlugins.join(','),
111-
'hasNativeController' : extension1.hasNativeController,
112-
'requiresKeystore' : extension1.requiresKeystore
106+
'name' : extension1.name,
107+
'description' : extension1.description,
108+
'version' : extension1.version,
109+
'elasticsearchVersion': Version.fromString(VersionProperties.elasticsearch).toString(),
110+
'javaVersion' : project.targetCompatibility as String,
111+
'classname' : extension1.classname,
112+
'extendedPlugins' : extension1.extendedPlugins.join(','),
113+
'hasNativeController' : extension1.hasNativeController,
114+
'requiresKeystore' : extension1.requiresKeystore
113115
]
114116
project.tasks.named('pluginProperties').configure {
115117
expand(properties)
@@ -142,7 +144,7 @@ class PluginBuildPlugin implements Plugin<Project> {
142144
}
143145
}
144146
project.configurations.getByName('default')
145-
.extendsFrom(project.configurations.getByName('runtimeClasspath'))
147+
.extendsFrom(project.configurations.getByName('runtimeClasspath'))
146148
// allow running ES with this plugin in the foreground of a build
147149
project.tasks.register('run', RunTask) {
148150
dependsOn(project.tasks.bundlePlugin)
@@ -235,7 +237,7 @@ class PluginBuildPlugin implements Plugin<Project> {
235237
*/
236238
from { project.plugins.hasPlugin(ShadowPlugin) ? project.shadowJar : project.jar }
237239
from project.configurations.runtimeClasspath - project.configurations.getByName(
238-
CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME
240+
CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME
239241
)
240242
// extra files for the plugin to go into the zip
241243
from('src/main/packaging') // TODO: move all config/bin/_size/etc into packaging

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/StandaloneRestTestPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StandaloneRestTestPlugin implements Plugin<Project> {
6262
project.pluginManager.apply(TestClustersPlugin)
6363
project.pluginManager.apply(RepositoriesSetupPlugin)
6464

65-
project.getTasks().create("buildResources", ExportElasticsearchBuildResourcesTask)
65+
project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)
6666
ElasticsearchJavaPlugin.configureTestTasks(project)
6767
ElasticsearchJavaPlugin.configureInputNormalization(project)
6868
ElasticsearchJavaPlugin.configureCompile(project)

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersPlugin.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ private NamedDomainObjectContainer<ElasticsearchCluster> createTestClustersConta
114114
}
115115

116116
private void createListClustersTask(Project project, NamedDomainObjectContainer<ElasticsearchCluster> container) {
117-
Task listTask = project.getTasks().create(LIST_TASK_NAME);
118-
listTask.setGroup("ES cluster formation");
119-
listTask.setDescription("Lists all ES clusters configured for this project");
120-
listTask.doLast(
121-
(Task task) -> container.forEach(cluster -> logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getNumberOfNodes()))
122-
);
117+
// Task is never up to date so we can pass an lambda for the task action
118+
project.getTasks().register(LIST_TASK_NAME, task -> {
119+
task.setGroup("ES cluster formation");
120+
task.setDescription("Lists all ES clusters configured for this project");
121+
task.doLast(
122+
(Task t) -> container.forEach(cluster -> logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getNumberOfNodes()))
123+
);
124+
});
125+
123126
}
124127

125128
static class TestClustersHookPlugin implements Plugin<Project> {

distribution/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ tasks.register("generateDependenciesReport", ConcatFilesTask) {
5454
*****************************************************************************/
5555

5656
// integ test zip only uses server, so a different notice file is needed there
57-
task buildServerNotice(type: NoticeTask)
57+
tasks.register("buildServerNotice", NoticeTask)
5858

5959
// other distributions include notices from modules as well, which are added below later
60-
task buildDefaultNotice(type: NoticeTask) {
60+
tasks.register("buildDefaultNotice", NoticeTask).configure {
6161
licensesDir new File(project(':distribution').projectDir, 'licenses')
6262
}
6363

64-
task buildOssNotice(type: NoticeTask) {
64+
tasks.register("buildOssNotice", NoticeTask).configure {
6565
licensesDir new File(project(':distribution').projectDir, 'licenses')
6666
}
6767

68-
task buildDefaultNoJdkNotice(type: NoticeTask)
68+
tasks.register("buildDefaultNoJdkNotice", NoticeTask)
6969

70-
task buildOssNoJdkNotice(type: NoticeTask)
70+
tasks.register("buildOssNoJdkNotice", NoticeTask)
7171

7272
// The :server and :libs projects belong to all distributions
73-
tasks.withType(NoticeTask) {
73+
tasks.withType(NoticeTask).configureEach {
7474
licensesDir project(':server').file('licenses')
7575
source project(':server').file('src/main/java')
7676
project(':libs').subprojects.each { Project lib ->

distribution/bwc/build.gradle

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
4242
String bwcBranch = unreleasedVersion.branch
4343
apply plugin: 'distribution'
4444
// Not published so no need to assemble
45-
assemble.enabled = false
46-
45+
tasks.named("assemble").configure {
46+
enabled = false
47+
}
4748
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")
4849

4950
final String remote = System.getProperty("bwc.remote", "elastic")
@@ -58,13 +59,13 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
5859
throw new GradleException("tests.bwc.git_fetch_latest must be [true] or [false] but was [" + gitFetchLatestProperty + "]")
5960
}
6061

61-
task createClone(type: LoggedExec) {
62+
tasks.register("createClone", LoggedExec) {
6263
onlyIf { checkoutDir.exists() == false }
6364
commandLine = ['git', 'clone', rootDir, checkoutDir]
6465
}
6566

66-
task findRemote(type: LoggedExec) {
67-
dependsOn createClone
67+
tasks.register("findRemote", LoggedExec) {
68+
dependsOn "createClone"
6869
workingDir = checkoutDir
6970
commandLine = ['git', 'remote', '-v']
7071
ByteArrayOutputStream output = new ByteArrayOutputStream()
@@ -79,16 +80,16 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
7980
}
8081
}
8182

82-
task addRemote(type: LoggedExec) {
83+
tasks.register("addRemote", LoggedExec) {
8384
dependsOn findRemote
8485
onlyIf { project.ext.remoteExists == false }
8586
workingDir = checkoutDir
8687
commandLine = ['git', 'remote', 'add', "${remote}", "https://github.com/${remote}/elasticsearch.git"]
8788
}
8889

89-
task fetchLatest(type: LoggedExec) {
90+
tasks.register("fetchLatest", LoggedExec) {
9091
onlyIf { project.gradle.startParameter.isOffline() == false && gitFetchLatest }
91-
dependsOn addRemote
92+
dependsOn("addRemote")
9293
workingDir = checkoutDir
9394
commandLine = ['git', 'fetch', '--all']
9495
}
@@ -104,8 +105,8 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
104105
return os.toString().trim()
105106
}
106107
}
107-
task checkoutBwcBranch() {
108-
dependsOn fetchLatest
108+
tasks.register("checkoutBwcBranch") {
109+
dependsOn("fetchLatest")
109110
doLast {
110111
def refspec = System.getProperty("bwc.refspec." + bwcBranch) ?: System.getProperty("tests.bwc.refspec." + bwcBranch) ?: "${remote}/${bwcBranch}"
111112
if (System.getProperty("bwc.checkout.align") != null) {
@@ -156,9 +157,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
156157
}
157158

158159

159-
Closure createRunBwcGradleTask = { name, extraConfig ->
160+
Closure<TaskProvider> createRunBwcGradleTask = { name, extraConfig ->
160161
return tasks.register("$name", LoggedExec) {
161-
dependsOn checkoutBwcBranch
162+
dependsOn "checkoutBwcBranch"
162163
spoolOutput = true
163164
workingDir = checkoutDir
164165
doFirst {
@@ -304,7 +305,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
304305
Version currentVersion = Version.fromString(version)
305306
if (currentVersion.getMinor() == 0 && currentVersion.getRevision() == 0) {
306307
// We only want to resolve dependencies for live versions of master, without cascading this to older versions
307-
resolveAllDependencies.dependsOn resolveAllBwcDependencies
308+
tasks.named("resolveAllDependencies").configure {
309+
dependsOn("resolveAllBwcDependencies")
310+
}
308311
}
309312

310313
for (e in artifactFiles) {
@@ -326,7 +329,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
326329
}
327330
}
328331
// make sure no dependencies were added to assemble; we want it to be a no-op
329-
assemble.dependsOn = []
332+
tasks.named("assemble").configure {
333+
dependsOn = []
334+
}
330335
}
331336
}
332337

distribution/docker/build.gradle

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ project.ext {
109109
}
110110

111111
void addCopyDockerContextTask(final String architecture, final boolean oss) {
112-
task(taskName("copy", architecture, oss, "DockerContext"), type: Sync) {
112+
tasks.register(taskName("copy", architecture, oss, "DockerContext"), Sync) {
113113
expansions(architecture, oss, true).findAll { it.key != 'build_date' }.each { k, v ->
114114
inputs.property(k, { v.toString() })
115115
}
@@ -143,7 +143,7 @@ def createAndSetWritable(Object... locations) {
143143
}
144144
}
145145

146-
task copyKeystore(type: Sync) {
146+
tasks.register("copyKeystore", Sync) {
147147
from project(':x-pack:plugin:core')
148148
.file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
149149
into "${buildDir}/certs"
@@ -167,9 +167,9 @@ elasticsearch_distributions {
167167
}
168168
}
169169

170-
preProcessFixture {
170+
tasks.named("preProcessFixture").configure {
171171
dependsOn elasticsearch_distributions.docker_default, elasticsearch_distributions.docker_oss
172-
dependsOn copyKeystore
172+
dependsOn "copyKeystore"
173173
doLast {
174174
// tests expect to have an empty repo
175175
project.delete(
@@ -187,21 +187,25 @@ preProcessFixture {
187187
}
188188
}
189189

190-
processTestResources {
190+
tasks.named("processTestResources").configure {
191191
from project(':x-pack:plugin:core')
192192
.file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks')
193193
}
194194

195-
task integTest(type: Test) {
195+
tasks.register("integTest", Test) {
196196
outputs.doNotCacheIf('Build cache is disabled for Docker tests') { true }
197197
maxParallelForks = '1'
198198
include '**/*IT.class'
199199
}
200200

201-
check.dependsOn integTest
201+
tasks.named("check").configure {
202+
dependsOn "integTest"
203+
}
202204

203205
void addBuildDockerImage(final String architecture, final boolean oss) {
204-
final Task buildDockerImageTask = task(taskName("build", architecture, oss, "DockerImage"), type: DockerBuildTask) {
206+
final TaskProvider<DockerBuildTask> buildDockerImageTask =
207+
tasks.register(taskName("build", architecture, oss, "DockerImage"), DockerBuildTask) {
208+
onlyIf { Architecture.current().name().toLowerCase().equals(architecture) }
205209
TaskProvider<Sync> copyContextTask = tasks.named(taskName("copy", architecture, oss, "DockerContext"))
206210
dependsOn(copyContextTask)
207211
dockerContext.fileProvider(copyContextTask.map { it.destinationDir })
@@ -220,8 +224,9 @@ void addBuildDockerImage(final String architecture, final boolean oss) {
220224
]
221225
}
222226
}
223-
buildDockerImageTask.onlyIf { Architecture.current().name().toLowerCase().equals(architecture) }
224-
assemble.dependsOn(buildDockerImageTask)
227+
tasks.named("assemble").configure {
228+
dependsOn(buildDockerImageTask)
229+
}
225230
}
226231

227232
for (final String architecture : ["aarch64", "x64"]) {
@@ -253,26 +258,27 @@ subprojects { Project subProject ->
253258
def buildTaskName = taskName("build", architecture, oss, "DockerImage")
254259
def tarFile = "${parent.projectDir}/build/elasticsearch${"aarch64".equals(architecture) ? '-aarch64' : ''}${oss ? '-oss' : ''}_test.${VersionProperties.elasticsearch}.docker.tar"
255260

256-
final Task exportDockerImageTask = task(exportTaskName, type: LoggedExec) {
261+
final TaskProvider<LoggedExec> exportDockerImageTask = tasks.register(exportTaskName, LoggedExec) {
257262
inputs.file("${parent.projectDir}/build/markers/${buildTaskName}.marker")
258263
executable 'docker'
259264
outputs.file(tarFile)
260265
args "save",
261266
"-o",
262267
tarFile,
263268
"elasticsearch${oss ? '-oss' : ''}:test"
264-
}
265269

266-
exportDockerImageTask.dependsOn(parent.tasks.getByName(buildTaskName))
267-
268-
exportDockerImageTask.onlyIf { Architecture.current().name().toLowerCase().equals(architecture) }
270+
dependsOn(parent.path + ":" + buildTaskName)
271+
onlyIf { Architecture.current().name().toLowerCase().equals(architecture) }
272+
}
269273

270274
artifacts.add('default', file(tarFile)) {
271275
type 'tar'
272276
name "elasticsearch${"aarch64".equals(architecture) ? '-aarch64' : ''}${oss ? '-oss' : ''}"
273277
builtBy exportTaskName
274278
}
275279

276-
assemble.dependsOn exportTaskName
280+
tasks.named("assemble").configure {
281+
dependsOn(exportTaskName)
282+
}
277283
}
278284
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
apply plugin: 'base'
22

3-
task buildDockerBuildContext(type: Tar) {
3+
tasks.register("buildDockerBuildContext", Tar) {
44
archiveExtension = 'tar.gz'
55
compression = Compression.GZIP
66
archiveClassifier = "docker-build-context"
@@ -10,4 +10,4 @@ task buildDockerBuildContext(type: Tar) {
1010
with dockerBuildContext("<remote>", false, false)
1111
}
1212

13-
assemble.dependsOn buildDockerBuildContext
13+
tasks.named("assemble").configure {dependsOn "buildDockerBuildContext"}

0 commit comments

Comments
 (0)