Skip to content

Commit

Permalink
Use proper Java version for BWC builds (#29493)
Browse files Browse the repository at this point in the history
Today we have JAVA_HOME for the compiler Java home and RUNTIME_JAVA_HOME
for the test Java home. However, when we compile BWC nodes and run them,
neither of these Java homes might be the version that was suitable for
that BWC node (e.g., 5.6 requires JDK 8 to compile and to run). This
commit adds support for the environment variables JAVA\d+_HOME and uses
the appropriate Java home based on the version of the node being
started. We even do this for reindex-from-old which requires JDK 7 for
these very old nodes. Note that these environment variables are not
required if not running BWC tests, and they are strictly required if
running BWC tests.
  • Loading branch information
jasontedor authored Apr 13, 2018
1 parent 485d5d1 commit 27fafa2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class BuildPlugin implements Plugin<Project> {
String compilerJavaHome = findCompilerJavaHome()
String runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome)
File gradleJavaHome = Jvm.current().javaHome

final Map<Integer, String> javaVersions = [:]
for (int version = 7; version <= Integer.parseInt(minimumCompilerVersion.majorVersion); version++) {
javaVersions.put(version, findJavaHome(version));
}

String javaVendor = System.getProperty('java.vendor')
String javaVersion = System.getProperty('java.version')
String gradleJavaVersionDetails = "${javaVendor} ${javaVersion}" +
Expand Down Expand Up @@ -158,10 +164,32 @@ class BuildPlugin implements Plugin<Project> {
throw new GradleException(message)
}

for (final Map.Entry<Integer, String> javaVersionEntry : javaVersions.entrySet()) {
final String javaHome = javaVersionEntry.getValue()
if (javaHome == null) {
continue
}
JavaVersion javaVersionEnum = JavaVersion.toVersion(findJavaSpecificationVersion(project, javaHome))
final JavaVersion expectedJavaVersionEnum
final int version = javaVersionEntry.getKey()
if (version < 9) {
expectedJavaVersionEnum = JavaVersion.toVersion("1." + version)
} else {
expectedJavaVersionEnum = JavaVersion.toVersion(Integer.toString(version))
}
if (javaVersionEnum != expectedJavaVersionEnum) {
final String message =
"the environment variable JAVA" + version + "_HOME must be set to a JDK installation directory for Java" +
" ${expectedJavaVersionEnum} but is [${javaHome}] corresponding to [${javaVersionEnum}]"
throw new GradleException(message)
}
}

project.rootProject.ext.compilerJavaHome = compilerJavaHome
project.rootProject.ext.runtimeJavaHome = runtimeJavaHome
project.rootProject.ext.compilerJavaVersion = compilerJavaVersionEnum
project.rootProject.ext.runtimeJavaVersion = runtimeJavaVersionEnum
project.rootProject.ext.javaVersions = javaVersions
project.rootProject.ext.buildChecksDone = true
}

Expand All @@ -173,6 +201,7 @@ class BuildPlugin implements Plugin<Project> {
project.ext.runtimeJavaHome = project.rootProject.ext.runtimeJavaHome
project.ext.compilerJavaVersion = project.rootProject.ext.compilerJavaVersion
project.ext.runtimeJavaVersion = project.rootProject.ext.runtimeJavaVersion
project.ext.javaVersions = project.rootProject.ext.javaVersions
}

private static String findCompilerJavaHome() {
Expand All @@ -188,6 +217,10 @@ class BuildPlugin implements Plugin<Project> {
return javaHome
}

private static String findJavaHome(int version) {
return System.getenv('JAVA' + version + '_HOME')
}

private static String findRuntimeJavaHome(final String compilerJavaHome) {
assert compilerJavaHome != null
return System.getenv('RUNTIME_JAVA_HOME') ?: compilerJavaHome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import com.sun.jna.Native
import com.sun.jna.WString
import org.apache.tools.ant.taskdefs.condition.Os
import org.elasticsearch.gradle.Version
import org.gradle.api.GradleException
import org.gradle.api.InvalidUserDataException
import org.gradle.api.JavaVersion
import org.gradle.api.Project

import java.nio.file.Files
Expand Down Expand Up @@ -162,7 +164,25 @@ class NodeInfo {
args.add("${esScript}")
}

env = ['JAVA_HOME': project.runtimeJavaHome]
final String javaHome
final Map<Integer, JavaVersion> javaVersions = project.javaVersions
if (Version.fromString(nodeVersion).before("6.2.0")) {
final String java8Home = javaVersions.get(8)
if (java8Home == null) {
throw new GradleException("JAVA8_HOME must be set to run BWC tests against [" + nodeVersion + "]")
}
javaHome = java8Home
} else if (Version.fromString(nodeVersion).onOrAfter("6.2.0") && Version.fromString(nodeVersion).before("6.3.0")) {
final String java9Home = javaVersions.get(9)
if (java9Home == null) {
throw new GradleException("JAVA9_HOME must be set to run BWC tests against [" + nodeVersion + "]")
}
javaHome = java9Home
} else {
javaHome = project.compilerJavaHome
}

env = ['JAVA_HOME':javaHome]
args.addAll("-E", "node.portsfile=true")
String collectedSystemProperties = config.systemProperties.collect { key, value -> "-D${key}=${value}" }.join(" ")
String esJavaOpts = config.jvmArgs.isEmpty() ? collectedSystemProperties : collectedSystemProperties + " " + config.jvmArgs
Expand Down
19 changes: 13 additions & 6 deletions distribution/bwc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ subprojects {
task buildBwcVersion(type: Exec) {
dependsOn checkoutBwcBranch, writeBuildMetadata
workingDir = checkoutDir
if (project.rootProject.ext.runtimeJavaVersion == JavaVersion.VERSION_1_8 && ["5.6", "6.0", "6.1"].contains(bwcBranch)) {
/*
* If runtime Java home is set to JDK 8 and we are building branches that are officially built with JDK 8, push this to JAVA_HOME for
* these builds.
*/
environment('JAVA_HOME', System.getenv('RUNTIME_JAVA_HOME'))
if (["5.6", "6.0", "6.1"].contains(bwcBranch)) {
// we are building branches that are officially built with JDK 8, push JAVA8_HOME to JAVA_HOME for these builds
if (project.javaVersions.get(8) == null) {
throw new GradleException("JAVA8_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")
}
environment('JAVA_HOME', project.javaVersions.get(8))
} else if ("6.2".equals(bwcBranch)) {
if (project.javaVersions.get(9) == null) {
throw new GradleException("JAVA9_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")
}
environment('JAVA_HOME', project.javaVersions.get(9))
} else {
environment('JAVA_HOME', project.compilerJavaHome)
}
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
executable 'cmd'
Expand Down
10 changes: 6 additions & 4 deletions qa/reindex-from-old/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ dependencies {
es090 'org.elasticsearch:elasticsearch:0.90.13@zip'
}

if (project.runtimeJavaVersion >= JavaVersion.VERSION_1_9 || Os.isFamily(Os.FAMILY_WINDOWS)) {
/* We can't run the dependencies with Java 9 so for now we'll skip the whole
* thing. We can't get the pid files in windows so we skip that as well.... */
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
// we can't get the pid files in windows so we skip that
integTest.enabled = false
} else {
if (project.javaVersions.get(7) == null) {
throw new GradleException("JAVA7_HOME must be set to run reindex-from-old")
}
/* Set up tasks to unzip and run the old versions of ES before running the
* integration tests. */
for (String version : ['2', '1', '090']) {
Expand All @@ -75,7 +77,7 @@ if (project.runtimeJavaVersion >= JavaVersion.VERSION_1_9 || Os.isFamily(Os.FAMI
dependsOn unzip
executable = new File(project.runtimeJavaHome, 'bin/java')
env 'CLASSPATH', "${ -> project.configurations.oldesFixture.asPath }"
env 'JAVA_HOME', project.runtimeJavaHome
env 'JAVA_HOME', project.javaVersions.get(7)
args 'oldes.OldElasticsearch',
baseDir,
unzip.temporaryDir,
Expand Down

0 comments on commit 27fafa2

Please sign in to comment.