Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve testcluster distribution artifact handling #38933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier;
import org.gradle.api.execution.TaskActionListener;
import org.gradle.api.execution.TaskExecutionListener;
import org.gradle.api.file.FileTree;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.tasks.Sync;
import org.gradle.api.tasks.TaskState;

import java.io.File;
Expand All @@ -39,6 +42,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -87,6 +91,20 @@ public void apply(Project project) {
"Internal helper configuration used by cluster configuration to download " +
"ES distributions and plugins."
);
helperConfiguration.getIncoming().afterResolve(resolvableDependencies -> {
Set<ComponentArtifactIdentifier> nonZipComponents = resolvableDependencies.getArtifacts()
.getArtifacts()
.stream()
.filter(artifact -> artifact.getFile().getName().endsWith(".zip") == false)
.map(artifact -> artifact.getId())
.collect(Collectors.toSet());

if(nonZipComponents.isEmpty() == false) {
throw new IllegalStateException("Dependencies with non-zip artifacts found in configuration '" +
TestClustersPlugin.HELPER_CONFIGURATION_NAME + "': " + nonZipComponents
);
}
});

// When running in the Daemon it's possible for this to hold references to past
usedClusters.clear();
Expand All @@ -98,7 +116,15 @@ public void apply(Project project) {
// the clusters will look for artifacts there based on the naming conventions.
// Tasks that use a cluster will add this as a dependency automatically so it's guaranteed to run early in
// the build.
rootProject.getTasks().create(SYNC_ARTIFACTS_TASK_NAME, SyncTestClustersConfiguration.class);
rootProject.getTasks().create(SYNC_ARTIFACTS_TASK_NAME, Sync.class, sync -> {
sync.from((Callable<List<FileTree>>) () ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering why the cast is needed here

Copy link
Contributor Author

@mark-vieira mark-vieira Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopySpec.from() simply takes Object... as an argument. Each item passed can be any of the types listed here. We can't just pass a raw lambda, as Java has now idea what SAM type to cast it to. So we have to explicitly cast it as one of the supported types. We don't have this problem in Groovy as { } returns an instance of Closure.

There's actually no real need to include the generic type arguments here, Gradle will figure this out at runtime, but its more explicit and folks reading the code immediately see what that lambda returns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, if it's not obvious, we wrap all this in a Callable because we are iterating over a Configuration here so we can return a FileTree from each resolved artifact. Doing so causes the Configuration to be resolved (and files to be downloaded) so we want to defer this until task execution. Or in this case more specifically, during task graph calculation.

helperConfiguration.getFiles()
.stream()
.map(project::zipTree)
.collect(Collectors.toList())
);
sync.into(new File(getTestClustersConfigurationExtractDir(project), "zip"));
});

// When we know what tasks will run, we claim the clusters of those task to differentiate between clusters
// that are defined in the build script and the ones that will actually be used in this invocation of gradle
Expand Down Expand Up @@ -129,7 +155,7 @@ private NamedDomainObjectContainer<ElasticsearchNode> createTestClustersContaine
project.getPath(),
name,
GradleServicesAdapter.getInstance(project),
SyncTestClustersConfiguration.getTestClustersConfigurationExtractDir(project),
getTestClustersConfigurationExtractDir(project),
new File(project.getBuildDir(), "testclusters")
)
);
Expand Down Expand Up @@ -249,8 +275,8 @@ public void beforeExecute(Task task) {}
);
}

static File getTestClustersBuildDir(Project project) {
return new File(project.getRootProject().getBuildDir(), "testclusters");
static File getTestClustersConfigurationExtractDir(Project project) {
return new File(project.getRootProject().getBuildDir(), "testclusters/extract");
}

/**
Expand Down