Remove JavaVersion in favour of standard Runtime.Version (java-version-checker) (#3027)

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
This commit is contained in:
Andriy Redko 2022-04-21 17:21:51 -04:00 committed by GitHub
parent 0801a9c18a
commit 0cbd47c799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 9 additions and 127 deletions

View File

@ -1,83 +0,0 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.tools.java_version_checker;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class JavaVersion {
public static final List<Integer> CURRENT = parse(System.getProperty("java.specification.version"));
public static final List<Integer> JAVA_8 = parse("1.8");
public static final List<Integer> JAVA_11 = parse("11");
static List<Integer> parse(final String value) {
if (!value.matches("^0*[0-9]+(\\.[0-9]+)*$")) {
throw new IllegalArgumentException(value);
}
final List<Integer> version = new ArrayList<Integer>();
final String[] components = value.split("\\.");
for (final String component : components) {
version.add(Integer.valueOf(component));
}
return version;
}
public static int majorVersion(final List<Integer> javaVersion) {
Objects.requireNonNull(javaVersion);
if (javaVersion.get(0) > 1) {
return javaVersion.get(0);
} else {
return javaVersion.get(1);
}
}
static int compare(final List<Integer> left, final List<Integer> right) {
// lexicographically compare two lists, treating missing entries as zeros
final int len = Math.max(left.size(), right.size());
for (int i = 0; i < len; i++) {
final int l = (i < left.size()) ? left.get(i) : 0;
final int r = (i < right.size()) ? right.get(i) : 0;
if (l < r) {
return -1;
}
if (r < l) {
return 1;
}
}
return 0;
}
}

View File

@ -32,18 +32,19 @@
package org.opensearch.tools.java_version_checker; package org.opensearch.tools.java_version_checker;
import java.lang.Runtime.Version;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
/** /**
* Simple program that checks if the runtime Java version is at least 1.8. * Simple program that checks if the runtime Java version is at least 11
*/ */
final class JavaVersionChecker { final class JavaVersionChecker {
private JavaVersionChecker() {} private JavaVersionChecker() {}
/** /**
* The main entry point. The exit code is 0 if the Java version is at least 1.8, otherwise the exit code is 1. * The main entry point. The exit code is 0 if the Java version is at least 11, otherwise the exit code is 1.
* *
* @param args the args to the program which are rejected if not empty * @param args the args to the program which are rejected if not empty
*/ */
@ -52,23 +53,15 @@ final class JavaVersionChecker {
if (args.length != 0) { if (args.length != 0) {
throw new IllegalArgumentException("expected zero arguments but was " + Arrays.toString(args)); throw new IllegalArgumentException("expected zero arguments but was " + Arrays.toString(args));
} }
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_8) < 0) { if (Runtime.version().compareTo(Version.parse("11")) < 0) {
final String message = String.format( final String message = String.format(
Locale.ROOT, Locale.ROOT,
"the minimum required Java version is 8; your Java version from [%s] does not meet this requirement", "OpenSearch requires Java 11; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home") System.getProperty("java.home")
); );
errPrintln(message); errPrintln(message);
exit(1); exit(1);
} }
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_11) < 0) {
final String message = String.format(
Locale.ROOT,
"future versions of OpenSearch will require Java 11; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home")
);
errPrintln(message);
}
exit(0); exit(0);
} }

View File

@ -32,8 +32,6 @@
package org.opensearch.tools.launchers; package org.opensearch.tools.launchers;
import org.opensearch.tools.java_version_checker.JavaVersion;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -73,19 +71,7 @@ final class JvmErgonomics {
final long heapSize = extractHeapSize(finalJvmOptions); final long heapSize = extractHeapSize(finalJvmOptions);
final long maxDirectMemorySize = extractMaxDirectMemorySize(finalJvmOptions); final long maxDirectMemorySize = extractMaxDirectMemorySize(finalJvmOptions);
if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
Launchers.errPrintln("Warning: with JDK 8 on Windows, OpenSearch may be unable to derive correct");
Launchers.errPrintln(" ergonomic settings due to a JDK issue (JDK-8074459). Please use a newer");
Launchers.errPrintln(" version of Java.");
}
if (maxDirectMemorySize == 0 && userDefinedJvmOptions.stream().noneMatch(s -> s.startsWith("-XX:MaxDirectMemorySize"))) { if (maxDirectMemorySize == 0 && userDefinedJvmOptions.stream().noneMatch(s -> s.startsWith("-XX:MaxDirectMemorySize"))) {
if (System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
Launchers.errPrintln("Warning: MaxDirectMemorySize may have been miscalculated due to JDK-8074459.");
Launchers.errPrintln(" Please use a newer version of Java or set MaxDirectMemorySize explicitly.");
}
ergonomicChoices.add("-XX:MaxDirectMemorySize=" + heapSize / 2); ergonomicChoices.add("-XX:MaxDirectMemorySize=" + heapSize / 2);
} }
return ergonomicChoices; return ergonomicChoices;

View File

@ -32,8 +32,6 @@
package org.opensearch.tools.launchers; package org.opensearch.tools.launchers;
import org.opensearch.tools.java_version_checker.JavaVersion;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -183,7 +181,7 @@ final class JvmOptionsParser {
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader) BufferedReader br = new BufferedReader(reader)
) { ) {
parse(JavaVersion.majorVersion(JavaVersion.CURRENT), br, jvmOptions::add, invalidLines::put); parse(Runtime.version().feature(), br, jvmOptions::add, invalidLines::put);
} }
if (invalidLines.isEmpty() == false) { if (invalidLines.isEmpty() == false) {
throw new JvmOptionsFileParserException(jvmOptionsFile, invalidLines); throw new JvmOptionsFileParserException(jvmOptionsFile, invalidLines);

View File

@ -32,8 +32,6 @@
package org.opensearch.tools.launchers; package org.opensearch.tools.launchers;
import org.opensearch.tools.java_version_checker.JavaVersion;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -86,7 +84,7 @@ final class SystemJvmOptions {
} }
private static String maybeShowCodeDetailsInExceptionMessages() { private static String maybeShowCodeDetailsInExceptionMessages() {
if (JavaVersion.majorVersion(JavaVersion.CURRENT) >= 14) { if (Runtime.version().feature() >= 14) {
return "-XX:+ShowCodeDetailsInExceptionMessages"; return "-XX:+ShowCodeDetailsInExceptionMessages";
} else { } else {
return ""; return "";
@ -101,14 +99,10 @@ final class SystemJvmOptions {
* *
* Due to internationalization enhancements in JDK 9 OpenSearch need to set the provider to COMPAT otherwise time/date * Due to internationalization enhancements in JDK 9 OpenSearch need to set the provider to COMPAT otherwise time/date
* parsing will break in an incompatible way for some date patterns and locales. * parsing will break in an incompatible way for some date patterns and locales.
* //TODO COMPAT will be deprecated in jdk14 https://bugs.openjdk.java.net/browse/JDK-8232906 * //TODO COMPAT will be deprecated in at some point, see please https://bugs.openjdk.java.net/browse/JDK-8232906
* See also: documentation in <code>server/org.opensearch.common.time.IsoCalendarDataProvider</code> * See also: documentation in <code>server/org.opensearch.common.time.IsoCalendarDataProvider</code>
*/ */
if (JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
return "-Djava.locale.providers=SPI,JRE";
} else {
return "-Djava.locale.providers=SPI,COMPAT"; return "-Djava.locale.providers=SPI,COMPAT";
} }
}
} }

View File

@ -32,8 +32,6 @@
package org.opensearch.tools.launchers; package org.opensearch.tools.launchers;
import org.opensearch.tools.java_version_checker.JavaVersion;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -69,8 +67,6 @@ public class JvmErgonomicsTests extends LaunchersTestCase {
} }
public void testExtractValidHeapSizeNoOptionPresent() throws InterruptedException, IOException { public void testExtractValidHeapSizeNoOptionPresent() throws InterruptedException, IOException {
// Muted for jdk8/Windows, see: https://github.com/elastic/elasticsearch/issues/47384
assumeFalse(System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8);
assertThat(JvmErgonomics.extractHeapSize(JvmErgonomics.finalJvmOptions(Collections.emptyList())), greaterThan(0L)); assertThat(JvmErgonomics.extractHeapSize(JvmErgonomics.finalJvmOptions(Collections.emptyList())), greaterThan(0L));
} }
@ -141,8 +137,6 @@ public class JvmErgonomicsTests extends LaunchersTestCase {
} }
public void testMaxDirectMemorySizeChoice() throws InterruptedException, IOException { public void testMaxDirectMemorySizeChoice() throws InterruptedException, IOException {
// Muted for jdk8/Windows, see: https://github.com/elastic/elasticsearch/issues/47384
assumeFalse(System.getProperty("os.name").startsWith("Windows") && JavaVersion.majorVersion(JavaVersion.CURRENT) == 8);
final Map<String, String> heapMaxDirectMemorySize = new HashMap<>(); final Map<String, String> heapMaxDirectMemorySize = new HashMap<>();
heapMaxDirectMemorySize.put("64M", Long.toString((64L << 20) / 2)); heapMaxDirectMemorySize.put("64M", Long.toString((64L << 20) / 2));
heapMaxDirectMemorySize.put("512M", Long.toString((512L << 20) / 2)); heapMaxDirectMemorySize.put("512M", Long.toString((512L << 20) / 2));