Skip to content

Commit 053d8ac

Browse files
committed
[Gradle/BWC] Patch bundled OpenJdk17 with Adoptium Jdk17 for older ES Distros (elastic#135300) (elastic#135363)
Bundled OpenJDK 17 is incompatible with newer versions of Ubuntu 24.04 (Kernel 6.14.x). We fix our bwc testing on ubuntu 24.04 to explicitly use adoptium jdk 17 in cases where the bundled JDK is older than 21 which is not affected. (cherry picked from commit fec83ac) # Conflicts: # build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/RestTestBasePlugin.java # build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy # build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchCluster.java # build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java # build-tools/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersPlugin.java # test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterFactory.java
1 parent 5464b92 commit 053d8ac

File tree

7 files changed

+213
-16
lines changed

7 files changed

+213
-16
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/test/rest/RestTestBasePlugin.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@
3939
import org.gradle.api.artifacts.ProjectDependency;
4040
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
4141
import org.gradle.api.file.FileTree;
42+
import org.gradle.api.plugins.JvmToolchainsPlugin;
4243
import org.gradle.api.provider.ProviderFactory;
4344
import org.gradle.api.tasks.ClasspathNormalizer;
4445
import org.gradle.api.tasks.PathSensitivity;
4546
import org.gradle.api.tasks.util.PatternFilterable;
47+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
48+
import org.gradle.jvm.toolchain.JavaToolchainService;
49+
import org.gradle.jvm.toolchain.JvmVendorSpec;
4650

4751
import java.util.Collection;
4852
import java.util.HashSet;
@@ -52,6 +56,8 @@
5256

5357
import javax.inject.Inject;
5458

59+
import static org.elasticsearch.gradle.util.OsUtils.jdkIsIncompatibleWithOS;
60+
5561
/**
5662
* Base plugin used for wiring up build tasks to REST testing tasks using new JUnit rule-based test clusters framework.
5763
*/
@@ -85,6 +91,7 @@ public void apply(Project project) {
8591
project.getPluginManager().apply(ElasticsearchJavaPlugin.class);
8692
project.getPluginManager().apply(InternalDistributionDownloadPlugin.class);
8793
project.getPluginManager().apply(JdkDownloadPlugin.class);
94+
project.getPluginManager().apply(JvmToolchainsPlugin.class);
8895

8996
// Register integ-test and default distributions
9097
ElasticsearchDistribution defaultDistro = createDistribution(
@@ -182,6 +189,17 @@ public Void call(Object... args) {
182189
String versionString = version.toString();
183190
ElasticsearchDistribution bwcDistro = createDistribution(project, "bwc_" + versionString, versionString);
184191

192+
if (jdkIsIncompatibleWithOS(Version.fromString(versionString))) {
193+
var toolChainService = project.getExtensions().getByType(JavaToolchainService.class);
194+
var fallbackJdk17Launcher = toolChainService.launcherFor(spec -> {
195+
spec.getVendor().set(JvmVendorSpec.ADOPTIUM);
196+
spec.getLanguageVersion().set(JavaLanguageVersion.of(17));
197+
});
198+
task.environment(
199+
"ES_FALLBACK_JAVA_HOME",
200+
fallbackJdk17Launcher.get().getMetadata().getInstallationPath().getAsFile().getPath()
201+
);
202+
}
185203
task.dependsOn(bwcDistro);
186204
registerDistributionInputs(task, bwcDistro);
187205

build-tools/src/integTest/groovy/org/elasticsearch/gradle/TestClustersPluginFuncTest.groovy

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
package org.elasticsearch.gradle
1010

11-
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
12-
import org.gradle.testkit.runner.GradleRunner
1311
import spock.lang.IgnoreIf
1412
import spock.lang.Unroll
13+
import spock.util.environment.RestoreSystemProperties
14+
15+
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
16+
import org.gradle.testkit.runner.GradleRunner
1517

16-
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withChangedClasspathMockedDistributionDownload
17-
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withChangedConfigMockedDistributionDownload
18-
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withMockedDistributionDownload
18+
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.*
1919

2020
/**
2121
* We do not have coverage for the test cluster startup on windows yet.
@@ -107,8 +107,8 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
107107
def runningClosure = { GradleRunner r -> r.build() }
108108
withMockedDistributionDownload(runner, runningClosure)
109109
def result = inputProperty == "distributionClasspath" ?
110-
withChangedClasspathMockedDistributionDownload(runner, runningClosure) :
111-
withChangedConfigMockedDistributionDownload(runner, runningClosure)
110+
withChangedClasspathMockedDistributionDownload(runner, runningClosure) :
111+
withChangedConfigMockedDistributionDownload(runner, runningClosure)
112112

113113
then:
114114
result.output.contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'")
@@ -229,9 +229,51 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
229229
assertCustomDistro('myCluster')
230230
}
231231

232+
@RestoreSystemProperties
233+
def "override jdk usage via ES_JAVA_HOME for known jdk os incompatibilities"() {
234+
given:
235+
236+
settingsFile.text = """
237+
plugins {
238+
id 'org.gradle.toolchains.foojay-resolver-convention' version '1.0.0'
239+
}
240+
""" + settingsFile.text
241+
242+
buildFile << """
243+
testClusters {
244+
myCluster {
245+
testDistribution = 'default'
246+
version = '8.10.4'
247+
}
248+
}
249+
250+
// Force linux platform to trigger jdk override
251+
elasticsearch_distributions.forEach { d ->
252+
d.platform = org.elasticsearch.gradle.ElasticsearchDistribution.Platform.LINUX
253+
}
254+
255+
tasks.register('myTask', SomeClusterAwareTask) {
256+
useCluster testClusters.myCluster
257+
}
258+
"""
259+
when:
260+
def result = withMockedDistributionDownload(
261+
"8.10.4",
262+
ElasticsearchDistribution.Platform.LINUX,
263+
gradleRunner("myTask", '-Dos.name=Linux', '-Dos.version=6.14.0-1015-gcp', '-i')
264+
) {
265+
build()
266+
}
267+
268+
then:
269+
result.output.lines().anyMatch { line -> line.startsWith("Running") && line.split().find { it.startsWith("ES_JAVA_HOME=") }.contains("eclipse_adoptium-17") }
270+
}
271+
232272
boolean assertEsOutputContains(String testCluster, String expectedOutput) {
233-
assert new File(testProjectDir.root,
234-
"build/testclusters/${testCluster}-0/logs/es.out").text.contains(expectedOutput)
273+
assert new File(
274+
testProjectDir.root,
275+
"build/testclusters/${testCluster}-0/logs/es.out"
276+
).text.contains(expectedOutput)
235277
true
236278
}
237279

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchCluster.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.provider.Provider;
2424
import org.gradle.api.tasks.Internal;
2525
import org.gradle.api.tasks.Nested;
26+
import org.gradle.jvm.toolchain.JavaLauncher;
2627
import org.gradle.process.ExecOperations;
2728

2829
import java.io.File;
@@ -63,6 +64,8 @@ public class ElasticsearchCluster implements TestClusterConfiguration, Named {
6364
private final Provider<File> runtimeJava;
6465
private int nodeIndex = 0;
6566

67+
private final Provider<JavaLauncher> jdk17FallbackLauncher;
68+
6669
public ElasticsearchCluster(
6770
String clusterName,
6871
Project project,
@@ -72,7 +75,8 @@ public ElasticsearchCluster(
7275
ArchiveOperations archiveOperations,
7376
ExecOperations execOperations,
7477
Jdk bwcJdk,
75-
Provider<File> runtimeJava
78+
Provider<File> runtimeJava,
79+
Provider<JavaLauncher> jdk17FallbackLauncher
7680
) {
7781
this.path = project.getPath();
7882
this.clusterName = clusterName;
@@ -84,6 +88,7 @@ public ElasticsearchCluster(
8488
this.workingDirBase = workingDirBase;
8589
this.runtimeJava = runtimeJava;
8690
this.nodes = project.container(ElasticsearchNode.class);
91+
this.jdk17FallbackLauncher = jdk17FallbackLauncher;
8792
this.bwcJdk = bwcJdk;
8893

8994
this.nodes.add(
@@ -98,7 +103,8 @@ public ElasticsearchCluster(
98103
archiveOperations,
99104
execOperations,
100105
bwcJdk,
101-
runtimeJava
106+
runtimeJava,
107+
jdk17FallbackLauncher
102108
)
103109
);
104110

@@ -131,7 +137,8 @@ public void setNumberOfNodes(int numberOfNodes) {
131137
archiveOperations,
132138
execOperations,
133139
bwcJdk,
134-
runtimeJava
140+
runtimeJava,
141+
jdk17FallbackLauncher
135142
)
136143
);
137144
}

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.gradle.api.tasks.PathSensitive;
5252
import org.gradle.api.tasks.PathSensitivity;
5353
import org.gradle.api.tasks.util.PatternFilterable;
54+
import org.gradle.jvm.toolchain.JavaLauncher;
5455
import org.gradle.process.ExecOperations;
5556

5657
import java.io.ByteArrayInputStream;
@@ -94,6 +95,7 @@
9495

9596
import static java.util.Objects.requireNonNull;
9697
import static java.util.Optional.ofNullable;
98+
import static org.elasticsearch.gradle.util.OsUtils.jdkIsIncompatibleWithOS;
9799

98100
public class ElasticsearchNode implements TestClusterConfiguration {
99101

@@ -160,6 +162,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
160162
private final Path esInputFile;
161163
private final Path tmpDir;
162164
private final Provider<File> runtimeJava;
165+
private final Provider<JavaLauncher> jdk17FallbackLauncher;
163166

164167
private int currentDistro = 0;
165168
private TestDistribution testDistribution;
@@ -186,7 +189,8 @@ public class ElasticsearchNode implements TestClusterConfiguration {
186189
ArchiveOperations archiveOperations,
187190
ExecOperations execOperations,
188191
Jdk bwcJdk,
189-
Provider<File> runtimeJava
192+
Provider<File> runtimeJava,
193+
Provider<JavaLauncher> jdk17FallbackLauncher
190194
) {
191195
this.clusterName = clusterName;
192196
this.path = path;
@@ -198,6 +202,7 @@ public class ElasticsearchNode implements TestClusterConfiguration {
198202
this.execOperations = execOperations;
199203
this.bwcJdk = bwcJdk;
200204
this.runtimeJava = runtimeJava;
205+
this.jdk17FallbackLauncher = jdk17FallbackLauncher;
201206
workingDir = workingDirBase.toPath().resolve(safeName(name)).toAbsolutePath();
202207
confPathRepo = workingDir.resolve("repo");
203208
configFile = workingDir.resolve("config/elasticsearch.yml");
@@ -874,6 +879,15 @@ private java.util.Optional<String> getRequiredJavaHome() {
874879
return java.util.Optional.of(runtimeJava.map(File::getAbsolutePath).get());
875880
} else if (getVersion().before("7.0.0")) {
876881
return java.util.Optional.of(bwcJdk.getJavaHomePath().toString());
882+
} else if (jdkIsIncompatibleWithOS(getVersion())) {
883+
// Older distributions ship with openjdk versions that are not compatible with newer kernels of ubuntu 24.04 and later
884+
// Therefore we pass explicitly the runtime java to use the adoptium jdk that is maintained longer and compatible
885+
// with newer kernels.
886+
// 8.10.4 is the last version shipped with jdk < 21. We configure these cluster to run with jdk 17 adoptium as 17 was
887+
// the last LTS release before 21
888+
return java.util.Optional.of(
889+
jdk17FallbackLauncher.map(j -> j.getMetadata().getInstallationPath().getAsFile().getAbsolutePath()).get()
890+
);
877891
} else { // otherwise use the bundled JDK
878892
return java.util.Optional.empty();
879893
}

build-tools/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersPlugin.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@
2626
import org.gradle.api.invocation.Gradle;
2727
import org.gradle.api.logging.Logger;
2828
import org.gradle.api.logging.Logging;
29+
import org.gradle.api.plugins.JvmToolchainsPlugin;
2930
import org.gradle.api.provider.Provider;
3031
import org.gradle.api.provider.ProviderFactory;
3132
import org.gradle.api.tasks.TaskState;
3233
import org.gradle.internal.jvm.Jvm;
34+
import org.gradle.jvm.toolchain.JavaLanguageVersion;
35+
import org.gradle.jvm.toolchain.JavaLauncher;
36+
import org.gradle.jvm.toolchain.JavaToolchainService;
37+
import org.gradle.jvm.toolchain.JvmVendorSpec;
3338
import org.gradle.process.ExecOperations;
3439

3540
import java.io.File;
@@ -79,6 +84,7 @@ public void setRuntimeJava(Provider<File> runtimeJava) {
7984
public void apply(Project project) {
8085
project.getPluginManager().apply(JdkDownloadPlugin.class);
8186
project.getPlugins().apply(DistributionDownloadPlugin.class);
87+
project.getPlugins().apply(JvmToolchainsPlugin.class);
8288
project.getRootProject().getPluginManager().apply(ReaperPlugin.class);
8389
// register legacy jdk distribution for testing pre-7.0 BWC clusters
8490
Jdk bwcJdk = JdkDownloadPlugin.getContainer(project).create("bwc_jdk", jdk -> {
@@ -92,14 +98,22 @@ public void apply(Project project) {
9298
project.getGradle().getSharedServices(),
9399
ReaperPlugin.REAPER_SERVICE_NAME
94100
);
101+
102+
JavaToolchainService toolChainService = project.getExtensions().getByType(JavaToolchainService.class);
103+
Provider<JavaLauncher> fallbackJdk17Launcher = toolChainService.launcherFor(spec -> {
104+
spec.getVendor().set(JvmVendorSpec.ADOPTIUM);
105+
spec.getLanguageVersion().set(JavaLanguageVersion.of(17));
106+
});
107+
95108
runtimeJavaProvider = providerFactory.provider(
96109
() -> System.getenv("RUNTIME_JAVA_HOME") == null ? Jvm.current().getJavaHome() : new File(System.getenv("RUNTIME_JAVA_HOME"))
97110
);
98111
// enable the DSL to describe clusters
99112
NamedDomainObjectContainer<ElasticsearchCluster> container = createTestClustersContainerExtension(
100113
project,
101114
reaperServiceProvider,
102-
bwcJdk
115+
bwcJdk,
116+
fallbackJdk17Launcher
103117
);
104118

105119
// provide a task to be able to list defined clusters.
@@ -124,7 +138,8 @@ public void apply(Project project) {
124138
private NamedDomainObjectContainer<ElasticsearchCluster> createTestClustersContainerExtension(
125139
Project project,
126140
Provider<ReaperService> reaper,
127-
Jdk bwcJdk
141+
Jdk bwcJdk,
142+
Provider<JavaLauncher> fallbackJdk17Launcher
128143
) {
129144
// Create an extensions that allows describing clusters
130145
NamedDomainObjectContainer<ElasticsearchCluster> container = project.container(ElasticsearchCluster.class, name -> {
@@ -137,7 +152,8 @@ private NamedDomainObjectContainer<ElasticsearchCluster> createTestClustersConta
137152
getArchiveOperations(),
138153
getExecOperations(),
139154
bwcJdk,
140-
runtimeJavaProvider
155+
runtimeJavaProvider,
156+
fallbackJdk17Launcher
141157
);
142158
});
143159
project.getExtensions().add(EXTENSION_NAME, container);

0 commit comments

Comments
 (0)