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

Format projects under :distribution:tools #51292

Merged
merged 2 commits into from Jan 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Expand Up @@ -107,6 +107,9 @@ subprojects {
// switched to an exclude list, and eventualy removed completely.
def projectPathsToFormat = [
':build-tools',
':distribution:tools:java-version-checker',
':distribution:tools:launchers',
':distribution:tools:plugin-cli',
':x-pack:plugin:autoscaling',
':x-pack:plugin:enrich'
]
Expand Down
Expand Up @@ -27,8 +27,7 @@
*/
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.
Expand All @@ -42,17 +41,19 @@ public static void main(final String[] args) {
}
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_8) < 0) {
final String message = String.format(
Locale.ROOT,
"the minimum required Java version is 8; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home"));
Locale.ROOT,
"the minimum required Java version is 8; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home")
);
errPrintln(message);
exit(1);
}
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_11) < 0) {
final String message = String.format(
Locale.ROOT,
"future versions of Elasticsearch will require Java 11; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home"));
Locale.ROOT,
"future versions of Elasticsearch will require Java 11; your Java version from [%s] does not meet this requirement",
System.getProperty("java.home")
);
errPrintln(message);
}
exit(0);
Expand Down
Expand Up @@ -78,14 +78,18 @@ static List<String> choose(final List<String> userDefinedJvmOptions) throws Inte
return ergonomicChoices;
}

private static final Pattern OPTION =
Pattern.compile("^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}(\\s+\\{[^}]+})?");

static Map<String, Optional<String>> finalJvmOptions(
final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
return Collections.unmodifiableMap(flagsFinal(userDefinedJvmOptions).stream()
.map(OPTION::matcher).filter(Matcher::matches)
.collect(Collectors.toMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value")))));
private static final Pattern OPTION = Pattern.compile(
"^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}(\\s+\\{[^}]+})?"
);

static Map<String, Optional<String>> finalJvmOptions(final List<String> userDefinedJvmOptions) throws InterruptedException,
IOException {
return Collections.unmodifiableMap(
flagsFinal(userDefinedJvmOptions).stream()
.map(OPTION::matcher)
.filter(Matcher::matches)
.collect(Collectors.toMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value"))))
);
}

private static List<String> flagsFinal(final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
Expand All @@ -98,32 +102,32 @@ private static List<String> flagsFinal(final List<String> userDefinedJvmOptions)
* without having to implement our own JVM option parsing logic.
*/
final String java = Paths.get(System.getProperty("java.home"), "bin", "java").toString();
final List<String> command =
Collections.unmodifiableList(
Stream.of(Stream.of(java), userDefinedJvmOptions.stream(), Stream.of("-XX:+PrintFlagsFinal"), Stream.of("-version"))
.reduce(Stream::concat)
.get()
.collect(Collectors.toList()));
final List<String> command = Collections.unmodifiableList(
Stream.of(Stream.of(java), userDefinedJvmOptions.stream(), Stream.of("-XX:+PrintFlagsFinal"), Stream.of("-version"))
.reduce(Stream::concat)
.get()
.collect(Collectors.toList())
);
final Process process = new ProcessBuilder().command(command).start();
final List<String> output = readLinesFromInputStream(process.getInputStream());
final List<String> error = readLinesFromInputStream(process.getErrorStream());
final int status = process.waitFor();
if (status != 0) {
final String message = String.format(
Locale.ROOT,
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
status,
String.join("\n", output),
String.join("\n", error));
Locale.ROOT,
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
status,
String.join("\n", output),
String.join("\n", error)
);
throw new RuntimeException(message);
} else {
return output;
}
}

private static List<String> readLinesFromInputStream(final InputStream is) throws IOException {
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)) {
return Collections.unmodifiableList(br.lines().collect(Collectors.toList()));
}
}
Expand Down
Expand Up @@ -59,40 +59,41 @@ public static void main(final String[] args) throws InterruptedException, IOExce
}
final List<String> jvmOptions = new ArrayList<>();
final SortedMap<Integer, String> invalidLines = new TreeMap<>();
try (InputStream is = Files.newInputStream(Paths.get(args[0]));
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)) {
parse(
JavaVersion.majorVersion(JavaVersion.CURRENT),
br,
new JvmOptionConsumer() {
@Override
public void accept(final String jvmOption) {
jvmOptions.add(jvmOption);
}
},
new InvalidLineConsumer() {
@Override
public void accept(final int lineNumber, final String line) {
invalidLines.put(lineNumber, line);
}
});
try (
InputStream is = Files.newInputStream(Paths.get(args[0]));
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(reader)
) {
parse(JavaVersion.majorVersion(JavaVersion.CURRENT), br, new JvmOptionConsumer() {
@Override
public void accept(final String jvmOption) {
jvmOptions.add(jvmOption);
}
}, new InvalidLineConsumer() {
@Override
public void accept(final int lineNumber, final String line) {
invalidLines.put(lineNumber, line);
}
});
}

if (invalidLines.isEmpty()) {
// now append the JVM options from ES_JAVA_OPTS
final String environmentJvmOptions = System.getenv("ES_JAVA_OPTS");
if (environmentJvmOptions != null) {
jvmOptions.addAll(Arrays.stream(environmentJvmOptions.split("\\s+"))
.filter(s -> s.trim().isEmpty() == false)
.collect(Collectors.toList()));
jvmOptions.addAll(
Arrays.stream(environmentJvmOptions.split("\\s+")).filter(s -> s.trim().isEmpty() == false).collect(Collectors.toList())
);
}
final List<String> substitutedJvmOptions =
substitutePlaceholders(jvmOptions, Collections.singletonMap("ES_TMPDIR", System.getenv("ES_TMPDIR")));
final List<String> substitutedJvmOptions = substitutePlaceholders(
jvmOptions,
Collections.singletonMap("ES_TMPDIR", System.getenv("ES_TMPDIR"))
);
final List<String> ergonomicJvmOptions = JvmErgonomics.choose(substitutedJvmOptions);
final List<String> systemJvmOptions = SystemJvmOptions.systemJvmOptions();
final List<String> finalJvmOptions =
new ArrayList<>(systemJvmOptions.size() + substitutedJvmOptions.size() + ergonomicJvmOptions.size());
final List<String> finalJvmOptions = new ArrayList<>(
systemJvmOptions.size() + substitutedJvmOptions.size() + ergonomicJvmOptions.size()
);
finalJvmOptions.addAll(systemJvmOptions); // add the system JVM options first so that they can be overridden
finalJvmOptions.addAll(substitutedJvmOptions);
finalJvmOptions.addAll(ergonomicJvmOptions);
Expand All @@ -101,43 +102,43 @@ public void accept(final int lineNumber, final String line) {
Launchers.exit(0);
} else {
final String errorMessage = String.format(
Locale.ROOT,
"encountered [%d] error%s parsing [%s]",
invalidLines.size(),
invalidLines.size() == 1 ? "" : "s",
args[0]);
Locale.ROOT,
"encountered [%d] error%s parsing [%s]",
invalidLines.size(),
invalidLines.size() == 1 ? "" : "s",
args[0]
);
Launchers.errPrintln(errorMessage);
int count = 0;
for (final Map.Entry<Integer, String> entry : invalidLines.entrySet()) {
count++;
final String message = String.format(
Locale.ROOT,
"[%d]: encountered improperly formatted JVM option line [%s] on line number [%d]",
count,
entry.getValue(),
entry.getKey());
Locale.ROOT,
"[%d]: encountered improperly formatted JVM option line [%s] on line number [%d]",
count,
entry.getValue(),
entry.getKey()
);
Launchers.errPrintln(message);
}
Launchers.exit(1);
}
}

static List<String> substitutePlaceholders(final List<String> jvmOptions, final Map<String, String> substitutions) {
final Map<String, String> placeholderSubstitutions =
substitutions.entrySet().stream().collect(Collectors.toMap(e -> "${" + e.getKey() + "}", Map.Entry::getValue));
return jvmOptions.stream()
.map(
jvmOption -> {
String actualJvmOption = jvmOption;
int start = jvmOption.indexOf("${");
if (start >= 0 && jvmOption.indexOf('}', start) > 0) {
for (final Map.Entry<String, String> placeholderSubstitution : placeholderSubstitutions.entrySet()) {
actualJvmOption = actualJvmOption.replace(placeholderSubstitution.getKey(), placeholderSubstitution.getValue());
}
}
return actualJvmOption;
})
.collect(Collectors.toList());
final Map<String, String> placeholderSubstitutions = substitutions.entrySet()
.stream()
.collect(Collectors.toMap(e -> "${" + e.getKey() + "}", Map.Entry::getValue));
return jvmOptions.stream().map(jvmOption -> {
String actualJvmOption = jvmOption;
int start = jvmOption.indexOf("${");
if (start >= 0 && jvmOption.indexOf('}', start) > 0) {
for (final Map.Entry<String, String> placeholderSubstitution : placeholderSubstitutions.entrySet()) {
actualJvmOption = actualJvmOption.replace(placeholderSubstitution.getKey(), placeholderSubstitution.getValue());
}
}
return actualJvmOption;
}).collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -223,10 +224,11 @@ interface InvalidLineConsumer {
* @throws IOException if an I/O exception occurs reading from the buffered reader
*/
static void parse(
final int javaMajorVersion,
final BufferedReader br,
final JvmOptionConsumer jvmOptionConsumer,
final InvalidLineConsumer invalidLineConsumer) throws IOException {
final int javaMajorVersion,
final BufferedReader br,
final JvmOptionConsumer jvmOptionConsumer,
final InvalidLineConsumer invalidLineConsumer
) throws IOException {
int lineNumber = 0;
while (true) {
final String line = br.readLine();
Expand Down
Expand Up @@ -28,42 +28,45 @@
final class SystemJvmOptions {

static List<String> systemJvmOptions() {
return Collections.unmodifiableList(Arrays.asList(
/*
* Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property networkaddress.cache.ttl;
* can be set to -1 to cache forever.
*/
"-Des.networkaddress.cache.ttl=60",
/*
* Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property
* networkaddress.cache.negative ttl; set to -1 to cache forever.
*/
"-Des.networkaddress.cache.negative.ttl=10",
// pre-touch JVM emory pages during initialization
"-XX:+AlwaysPreTouch",
// explicitly set the stack size
"-Xss1m",
// set to headless, just in case,
"-Djava.awt.headless=true",
// ensure UTF-8 encoding by default (e.g., filenames)
"-Dfile.encoding=UTF-8",
// use our provided JNA always versus the system one
"-Djna.nosys=true",
/*
* Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for
* debugging.
*/
"-XX:-OmitStackTraceInFastThrow",
// flags to configure Netty
"-Dio.netty.noUnsafe=true",
"-Dio.netty.noKeySetOptimization=true",
"-Dio.netty.recycler.maxCapacityPerThread=0",
"-Dio.netty.allocator.numDirectArenas=0",
// log4j 2
"-Dlog4j.shutdownHookEnabled=false",
"-Dlog4j2.disable.jmx=true",
return Collections.unmodifiableList(
Arrays.asList(
/*
* Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property
* networkaddress.cache.ttl; can be set to -1 to cache forever.
*/
"-Des.networkaddress.cache.ttl=60",
/*
* Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property
* networkaddress.cache.negative ttl; set to -1 to cache forever.
*/
"-Des.networkaddress.cache.negative.ttl=10",
// pre-touch JVM emory pages during initialization
"-XX:+AlwaysPreTouch",
// explicitly set the stack size
"-Xss1m",
// set to headless, just in case,
"-Djava.awt.headless=true",
// ensure UTF-8 encoding by default (e.g., filenames)
"-Dfile.encoding=UTF-8",
// use our provided JNA always versus the system one
"-Djna.nosys=true",
/*
* Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for
* debugging.
*/
"-XX:-OmitStackTraceInFastThrow",
// flags to configure Netty
"-Dio.netty.noUnsafe=true",
"-Dio.netty.noKeySetOptimization=true",
"-Dio.netty.recycler.maxCapacityPerThread=0",
"-Dio.netty.allocator.numDirectArenas=0",
// log4j 2
"-Dlog4j.shutdownHookEnabled=false",
"-Dlog4j2.disable.jmx=true",

javaLocaleProviders()));
javaLocaleProviders()
)
);
}

private static String javaLocaleProviders() {
Expand All @@ -77,7 +80,7 @@ private static String javaLocaleProviders() {
* //TODO COMPAT will be deprecated in jdk14 https://bugs.openjdk.java.net/browse/JDK-8232906
* See also: documentation in <code>server/org.elasticsearch.common.time.IsoCalendarDataProvider</code>
*/
if(JavaVersion.majorVersion(JavaVersion.CURRENT) == 8){
if (JavaVersion.majorVersion(JavaVersion.CURRENT) == 8) {
return "-Djava.locale.providers=SPI,JRE";
} else {
return "-Djava.locale.providers=SPI,COMPAT";
Expand Down