Skip to content

Commit 045dd4a

Browse files
authored
Introduce multi-release JAR
This commit introduces the ability for the core Elasticsearch JAR to be a multi-release JAR containing code that is compiled for JDK 8 and code that is compiled for JDK 9. At runtime, a JDK 8 JVM will ignore the JDK 9 compiled classfiles, and a JDK 9 JVM will use the JDK 9 compiled classfiles instead of the JDK 8 compiled classfiles. With this work, we utilize the new JDK 9 API for obtaining the PID of the running JVM, instead of relying on a hack. For now, we want to keep IDEs on JDK 8 so when the build is in an IDE we ignore the JDK 9 source set (as otherwise the IDE would give compilation errors). However, with this change, running Gradle from the command-line now requires JAVA_HOME and JAVA_9_HOME to be set. This will require follow-up work in our CI infrastructure and our release builds to accommodate this change. Relates #28051
1 parent e5a6984 commit 045dd4a

File tree

5 files changed

+113
-14
lines changed

5 files changed

+113
-14
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ class BuildPlugin implements Plugin<Project> {
158158
project.rootProject.ext.runtimeJavaVersion = runtimeJavaVersionEnum
159159
project.rootProject.ext.buildChecksDone = true
160160
}
161+
161162
project.targetCompatibility = minimumRuntimeVersion
162163
project.sourceCompatibility = minimumRuntimeVersion
164+
163165
// set java home for each project, so they dont have to find it in the root project
164166
project.ext.compilerJavaHome = project.rootProject.ext.compilerJavaHome
165167
project.ext.runtimeJavaHome = project.rootProject.ext.runtimeJavaHome
@@ -421,12 +423,12 @@ class BuildPlugin implements Plugin<Project> {
421423
}
422424
project.afterEvaluate {
423425
project.tasks.withType(JavaCompile) {
424-
File gradleJavaHome = Jvm.current().javaHome
426+
final JavaVersion targetCompatibilityVersion = JavaVersion.toVersion(it.targetCompatibility)
425427
// we fork because compiling lots of different classes in a shared jvm can eventually trigger GC overhead limitations
426428
options.fork = true
427429
options.forkOptions.javaHome = new File(project.compilerJavaHome)
428430
options.forkOptions.memoryMaximumSize = "1g"
429-
if (project.targetCompatibility >= JavaVersion.VERSION_1_8) {
431+
if (targetCompatibilityVersion == JavaVersion.VERSION_1_8) {
430432
// compile with compact 3 profile by default
431433
// NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
432434
if (project.compactProfile != 'full') {
@@ -449,8 +451,9 @@ class BuildPlugin implements Plugin<Project> {
449451

450452
options.encoding = 'UTF-8'
451453
options.incremental = true
454+
452455
// TODO: use native Gradle support for --release when available (cf. https://github.com/gradle/gradle/issues/2510)
453-
options.compilerArgs << '--release' << project.targetCompatibility.majorVersion
456+
options.compilerArgs << '--release' << targetCompatibilityVersion.majorVersion
454457
}
455458
}
456459
}

server/build.gradle

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ publishing {
3636

3737
archivesBaseName = 'elasticsearch'
3838

39+
// we want to keep the JDKs in our IDEs set to JDK 8 until minimum JDK is bumped to 9 so we do not include this source set in our IDEs
40+
if (!isEclipse && !isIdea) {
41+
sourceSets {
42+
java9 {
43+
java {
44+
srcDirs = ['src/main/java9']
45+
}
46+
}
47+
}
48+
49+
compileJava9Java {
50+
sourceCompatibility = 9
51+
targetCompatibility = 9
52+
}
53+
54+
jar {
55+
into('META-INF/versions/9') {
56+
from sourceSets.java9.output
57+
}
58+
manifest.attributes('Multi-Release': 'true')
59+
}
60+
}
61+
3962
dependencies {
4063

4164
compile "org.elasticsearch:elasticsearch-core:${version}"
@@ -94,6 +117,10 @@ dependencies {
94117
// repackaged jna with native bits linked against all elastic supported platforms
95118
compile "org.elasticsearch:jna:${versions.jna}"
96119

120+
if (!isEclipse && !isIdea) {
121+
java9Compile sourceSets.main.output
122+
}
123+
97124
if (isEclipse == false || project.path == ":server-tests") {
98125
testCompile("org.elasticsearch.test:framework:${version}") {
99126
// tests use the locally compiled version of server

server/src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ public class JvmInfo implements Writeable, ToXContentFragment {
4747
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
4848
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
4949

50-
// returns the <process id>@<host>
51-
long pid;
52-
String xPid = runtimeMXBean.getName();
53-
try {
54-
xPid = xPid.split("@")[0];
55-
pid = Long.parseLong(xPid);
56-
} catch (Exception e) {
57-
pid = -1;
58-
}
59-
6050
long heapInit = memoryMXBean.getHeapMemoryUsage().getInit() < 0 ? 0 : memoryMXBean.getHeapMemoryUsage().getInit();
6151
long heapMax = memoryMXBean.getHeapMemoryUsage().getMax() < 0 ? 0 : memoryMXBean.getHeapMemoryUsage().getMax();
6252
long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit() < 0 ? 0 : memoryMXBean.getNonHeapMemoryUsage().getInit();
@@ -160,7 +150,7 @@ public class JvmInfo implements Writeable, ToXContentFragment {
160150

161151
}
162152

163-
INSTANCE = new JvmInfo(pid, System.getProperty("java.version"), runtimeMXBean.getVmName(), runtimeMXBean.getVmVersion(),
153+
INSTANCE = new JvmInfo(JvmPid.getPid(), System.getProperty("java.version"), runtimeMXBean.getVmName(), runtimeMXBean.getVmVersion(),
164154
runtimeMXBean.getVmVendor(), runtimeMXBean.getStartTime(), configuredInitialHeapSize, configuredMaxHeapSize,
165155
mem, inputArguments, bootClassPath, classPath, systemProperties, gcCollectors, memoryPools, onError, onOutOfMemoryError,
166156
useCompressedOops, useG1GC, useSerialGC);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
20+
package org.elasticsearch.monitor.jvm;
21+
22+
import org.apache.logging.log4j.message.ParameterizedMessage;
23+
import org.elasticsearch.common.logging.Loggers;
24+
25+
import java.lang.management.ManagementFactory;
26+
27+
class JvmPid {
28+
29+
private static final long PID;
30+
31+
static long getPid() {
32+
return PID;
33+
}
34+
35+
static {
36+
PID = initializePid();
37+
}
38+
39+
private static long initializePid() {
40+
final String name = ManagementFactory.getRuntimeMXBean().getName();
41+
try {
42+
return Long.parseLong(name.split("@")[0]);
43+
} catch (final NumberFormatException e) {
44+
Loggers.getLogger(JvmPid.class).debug(new ParameterizedMessage("failed parsing PID from [{}]", name), e);
45+
return -1;
46+
}
47+
}
48+
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
20+
package org.elasticsearch.monitor.jvm;
21+
22+
import java.lang.ProcessHandle;
23+
24+
class JvmPid {
25+
26+
static long getPid() {
27+
return ProcessHandle.current().pid();
28+
}
29+
30+
}

0 commit comments

Comments
 (0)