Skip to content

Commit 98da0ff

Browse files
authored
Format projects under :distribution:tools (#51295)
Backport of #51226. Opt-in the sub-projects of :distribution:tools for automatic formatting.
1 parent 83ffe96 commit 98da0ff

File tree

16 files changed

+838
-606
lines changed

16 files changed

+838
-606
lines changed

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ subprojects {
107107
// switched to an exclude list, and eventualy removed completely.
108108
def projectPathsToFormat = [
109109
':build-tools',
110+
':distribution:tools:java-version-checker',
111+
':distribution:tools:launchers',
112+
':distribution:tools:plugin-cli',
110113
':x-pack:plugin:enrich'
111114
]
112115

distribution/tools/java-version-checker/src/main/java/org/elasticsearch/tools/java_version_checker/JavaVersionChecker.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828
final class JavaVersionChecker {
2929

30-
private JavaVersionChecker() {
31-
}
30+
private JavaVersionChecker() {}
3231

3332
/**
3433
* The main entry point. The exit code is 0 if the Java version is at least 1.8, otherwise the exit code is 1.
@@ -42,17 +41,19 @@ public static void main(final String[] args) {
4241
}
4342
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_8) < 0) {
4443
final String message = String.format(
45-
Locale.ROOT,
46-
"the minimum required Java version is 8; your Java version from [%s] does not meet this requirement",
47-
System.getProperty("java.home"));
44+
Locale.ROOT,
45+
"the minimum required Java version is 8; your Java version from [%s] does not meet this requirement",
46+
System.getProperty("java.home")
47+
);
4848
errPrintln(message);
4949
exit(1);
5050
}
5151
if (JavaVersion.compare(JavaVersion.CURRENT, JavaVersion.JAVA_11) < 0) {
5252
final String message = String.format(
53-
Locale.ROOT,
54-
"future versions of Elasticsearch will require Java 11; your Java version from [%s] does not meet this requirement",
55-
System.getProperty("java.home"));
53+
Locale.ROOT,
54+
"future versions of Elasticsearch will require Java 11; your Java version from [%s] does not meet this requirement",
55+
System.getProperty("java.home")
56+
);
5657
errPrintln(message);
5758
}
5859
exit(0);

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java

+25-21
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ static List<String> choose(final List<String> userDefinedJvmOptions) throws Inte
7878
return ergonomicChoices;
7979
}
8080

81-
private static final Pattern OPTION =
82-
Pattern.compile("^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}(\\s+\\{[^}]+})?");
83-
84-
static Map<String, Optional<String>> finalJvmOptions(
85-
final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
86-
return Collections.unmodifiableMap(flagsFinal(userDefinedJvmOptions).stream()
87-
.map(OPTION::matcher).filter(Matcher::matches)
88-
.collect(Collectors.toMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value")))));
81+
private static final Pattern OPTION = Pattern.compile(
82+
"^\\s*\\S+\\s+(?<flag>\\S+)\\s+:?=\\s+(?<value>\\S+)?\\s+\\{[^}]+?\\}(\\s+\\{[^}]+})?"
83+
);
84+
85+
static Map<String, Optional<String>> finalJvmOptions(final List<String> userDefinedJvmOptions) throws InterruptedException,
86+
IOException {
87+
return Collections.unmodifiableMap(
88+
flagsFinal(userDefinedJvmOptions).stream()
89+
.map(OPTION::matcher)
90+
.filter(Matcher::matches)
91+
.collect(Collectors.toMap(m -> m.group("flag"), m -> Optional.ofNullable(m.group("value"))))
92+
);
8993
}
9094

9195
private static List<String> flagsFinal(final List<String> userDefinedJvmOptions) throws InterruptedException, IOException {
@@ -98,32 +102,32 @@ private static List<String> flagsFinal(final List<String> userDefinedJvmOptions)
98102
* without having to implement our own JVM option parsing logic.
99103
*/
100104
final String java = Paths.get(System.getProperty("java.home"), "bin", "java").toString();
101-
final List<String> command =
102-
Collections.unmodifiableList(
103-
Stream.of(Stream.of(java), userDefinedJvmOptions.stream(), Stream.of("-XX:+PrintFlagsFinal"), Stream.of("-version"))
104-
.reduce(Stream::concat)
105-
.get()
106-
.collect(Collectors.toList()));
105+
final List<String> command = Collections.unmodifiableList(
106+
Stream.of(Stream.of(java), userDefinedJvmOptions.stream(), Stream.of("-XX:+PrintFlagsFinal"), Stream.of("-version"))
107+
.reduce(Stream::concat)
108+
.get()
109+
.collect(Collectors.toList())
110+
);
107111
final Process process = new ProcessBuilder().command(command).start();
108112
final List<String> output = readLinesFromInputStream(process.getInputStream());
109113
final List<String> error = readLinesFromInputStream(process.getErrorStream());
110114
final int status = process.waitFor();
111115
if (status != 0) {
112116
final String message = String.format(
113-
Locale.ROOT,
114-
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
115-
status,
116-
String.join("\n", output),
117-
String.join("\n", error));
117+
Locale.ROOT,
118+
"starting java failed with [%d]\noutput:\n%s\nerror:\n%s",
119+
status,
120+
String.join("\n", output),
121+
String.join("\n", error)
122+
);
118123
throw new RuntimeException(message);
119124
} else {
120125
return output;
121126
}
122127
}
123128

124129
private static List<String> readLinesFromInputStream(final InputStream is) throws IOException {
125-
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
126-
BufferedReader br = new BufferedReader(isr)) {
130+
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader br = new BufferedReader(isr)) {
127131
return Collections.unmodifiableList(br.lines().collect(Collectors.toList()));
128132
}
129133
}

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmOptionsParser.java

+56-54
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,41 @@ public static void main(final String[] args) throws InterruptedException, IOExce
5959
}
6060
final List<String> jvmOptions = new ArrayList<>();
6161
final SortedMap<Integer, String> invalidLines = new TreeMap<>();
62-
try (InputStream is = Files.newInputStream(Paths.get(args[0]));
63-
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
64-
BufferedReader br = new BufferedReader(reader)) {
65-
parse(
66-
JavaVersion.majorVersion(JavaVersion.CURRENT),
67-
br,
68-
new JvmOptionConsumer() {
69-
@Override
70-
public void accept(final String jvmOption) {
71-
jvmOptions.add(jvmOption);
72-
}
73-
},
74-
new InvalidLineConsumer() {
75-
@Override
76-
public void accept(final int lineNumber, final String line) {
77-
invalidLines.put(lineNumber, line);
78-
}
79-
});
62+
try (
63+
InputStream is = Files.newInputStream(Paths.get(args[0]));
64+
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
65+
BufferedReader br = new BufferedReader(reader)
66+
) {
67+
parse(JavaVersion.majorVersion(JavaVersion.CURRENT), br, new JvmOptionConsumer() {
68+
@Override
69+
public void accept(final String jvmOption) {
70+
jvmOptions.add(jvmOption);
71+
}
72+
}, new InvalidLineConsumer() {
73+
@Override
74+
public void accept(final int lineNumber, final String line) {
75+
invalidLines.put(lineNumber, line);
76+
}
77+
});
8078
}
8179

8280
if (invalidLines.isEmpty()) {
8381
// now append the JVM options from ES_JAVA_OPTS
8482
final String environmentJvmOptions = System.getenv("ES_JAVA_OPTS");
8583
if (environmentJvmOptions != null) {
86-
jvmOptions.addAll(Arrays.stream(environmentJvmOptions.split("\\s+"))
87-
.filter(s -> s.trim().isEmpty() == false)
88-
.collect(Collectors.toList()));
84+
jvmOptions.addAll(
85+
Arrays.stream(environmentJvmOptions.split("\\s+")).filter(s -> s.trim().isEmpty() == false).collect(Collectors.toList())
86+
);
8987
}
90-
final List<String> substitutedJvmOptions =
91-
substitutePlaceholders(jvmOptions, Collections.singletonMap("ES_TMPDIR", System.getenv("ES_TMPDIR")));
88+
final List<String> substitutedJvmOptions = substitutePlaceholders(
89+
jvmOptions,
90+
Collections.singletonMap("ES_TMPDIR", System.getenv("ES_TMPDIR"))
91+
);
9292
final List<String> ergonomicJvmOptions = JvmErgonomics.choose(substitutedJvmOptions);
9393
final List<String> systemJvmOptions = SystemJvmOptions.systemJvmOptions();
94-
final List<String> finalJvmOptions =
95-
new ArrayList<>(systemJvmOptions.size() + substitutedJvmOptions.size() + ergonomicJvmOptions.size());
94+
final List<String> finalJvmOptions = new ArrayList<>(
95+
systemJvmOptions.size() + substitutedJvmOptions.size() + ergonomicJvmOptions.size()
96+
);
9697
finalJvmOptions.addAll(systemJvmOptions); // add the system JVM options first so that they can be overridden
9798
finalJvmOptions.addAll(substitutedJvmOptions);
9899
finalJvmOptions.addAll(ergonomicJvmOptions);
@@ -101,43 +102,43 @@ public void accept(final int lineNumber, final String line) {
101102
Launchers.exit(0);
102103
} else {
103104
final String errorMessage = String.format(
104-
Locale.ROOT,
105-
"encountered [%d] error%s parsing [%s]",
106-
invalidLines.size(),
107-
invalidLines.size() == 1 ? "" : "s",
108-
args[0]);
105+
Locale.ROOT,
106+
"encountered [%d] error%s parsing [%s]",
107+
invalidLines.size(),
108+
invalidLines.size() == 1 ? "" : "s",
109+
args[0]
110+
);
109111
Launchers.errPrintln(errorMessage);
110112
int count = 0;
111113
for (final Map.Entry<Integer, String> entry : invalidLines.entrySet()) {
112114
count++;
113115
final String message = String.format(
114-
Locale.ROOT,
115-
"[%d]: encountered improperly formatted JVM option line [%s] on line number [%d]",
116-
count,
117-
entry.getValue(),
118-
entry.getKey());
116+
Locale.ROOT,
117+
"[%d]: encountered improperly formatted JVM option line [%s] on line number [%d]",
118+
count,
119+
entry.getValue(),
120+
entry.getKey()
121+
);
119122
Launchers.errPrintln(message);
120123
}
121124
Launchers.exit(1);
122125
}
123126
}
124127

125128
static List<String> substitutePlaceholders(final List<String> jvmOptions, final Map<String, String> substitutions) {
126-
final Map<String, String> placeholderSubstitutions =
127-
substitutions.entrySet().stream().collect(Collectors.toMap(e -> "${" + e.getKey() + "}", Map.Entry::getValue));
128-
return jvmOptions.stream()
129-
.map(
130-
jvmOption -> {
131-
String actualJvmOption = jvmOption;
132-
int start = jvmOption.indexOf("${");
133-
if (start >= 0 && jvmOption.indexOf('}', start) > 0) {
134-
for (final Map.Entry<String, String> placeholderSubstitution : placeholderSubstitutions.entrySet()) {
135-
actualJvmOption = actualJvmOption.replace(placeholderSubstitution.getKey(), placeholderSubstitution.getValue());
136-
}
137-
}
138-
return actualJvmOption;
139-
})
140-
.collect(Collectors.toList());
129+
final Map<String, String> placeholderSubstitutions = substitutions.entrySet()
130+
.stream()
131+
.collect(Collectors.toMap(e -> "${" + e.getKey() + "}", Map.Entry::getValue));
132+
return jvmOptions.stream().map(jvmOption -> {
133+
String actualJvmOption = jvmOption;
134+
int start = jvmOption.indexOf("${");
135+
if (start >= 0 && jvmOption.indexOf('}', start) > 0) {
136+
for (final Map.Entry<String, String> placeholderSubstitution : placeholderSubstitutions.entrySet()) {
137+
actualJvmOption = actualJvmOption.replace(placeholderSubstitution.getKey(), placeholderSubstitution.getValue());
138+
}
139+
}
140+
return actualJvmOption;
141+
}).collect(Collectors.toList());
141142
}
142143

143144
/**
@@ -223,10 +224,11 @@ interface InvalidLineConsumer {
223224
* @throws IOException if an I/O exception occurs reading from the buffered reader
224225
*/
225226
static void parse(
226-
final int javaMajorVersion,
227-
final BufferedReader br,
228-
final JvmOptionConsumer jvmOptionConsumer,
229-
final InvalidLineConsumer invalidLineConsumer) throws IOException {
227+
final int javaMajorVersion,
228+
final BufferedReader br,
229+
final JvmOptionConsumer jvmOptionConsumer,
230+
final InvalidLineConsumer invalidLineConsumer
231+
) throws IOException {
230232
int lineNumber = 0;
231233
while (true) {
232234
final String line = br.readLine();

distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/SystemJvmOptions.java

+42-39
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,48 @@
2626
final class SystemJvmOptions {
2727

2828
static List<String> systemJvmOptions() {
29-
return Collections.unmodifiableList(Arrays.asList(
30-
/*
31-
* Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property networkaddress.cache.ttl;
32-
* can be set to -1 to cache forever.
33-
*/
34-
"-Des.networkaddress.cache.ttl=60",
35-
/*
36-
* Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property
37-
* networkaddress.cache.negative ttl; set to -1 to cache forever.
38-
*/
39-
"-Des.networkaddress.cache.negative.ttl=10",
40-
// pre-touch JVM emory pages during initialization
41-
"-XX:+AlwaysPreTouch",
42-
// explicitly set the stack size
43-
"-Xss1m",
44-
// set to headless, just in case,
45-
"-Djava.awt.headless=true",
46-
// ensure UTF-8 encoding by default (e.g., filenames)
47-
"-Dfile.encoding=UTF-8",
48-
// use our provided JNA always versus the system one
49-
"-Djna.nosys=true",
50-
/*
51-
* Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for
52-
* debugging.
53-
*/
54-
"-XX:-OmitStackTraceInFastThrow",
55-
// flags to configure Netty
56-
"-Dio.netty.noUnsafe=true",
57-
"-Dio.netty.noKeySetOptimization=true",
58-
"-Dio.netty.recycler.maxCapacityPerThread=0",
59-
"-Dio.netty.allocator.numDirectArenas=0",
60-
// log4j 2
61-
"-Dlog4j.shutdownHookEnabled=false",
62-
"-Dlog4j2.disable.jmx=true",
63-
/*
64-
* Due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise time/date
65-
* parsing will break in an incompatible way for some date patterns and locales.
66-
*/
67-
"-Djava.locale.providers=COMPAT"));
29+
return Collections.unmodifiableList(
30+
Arrays.asList(
31+
/*
32+
* Cache ttl in seconds for positive DNS lookups noting that this overrides the JDK security property
33+
* networkaddress.cache.ttl; can be set to -1 to cache forever.
34+
*/
35+
"-Des.networkaddress.cache.ttl=60",
36+
/*
37+
* Cache ttl in seconds for negative DNS lookups noting that this overrides the JDK security property
38+
* networkaddress.cache.negative ttl; set to -1 to cache forever.
39+
*/
40+
"-Des.networkaddress.cache.negative.ttl=10",
41+
// pre-touch JVM emory pages during initialization
42+
"-XX:+AlwaysPreTouch",
43+
// explicitly set the stack size
44+
"-Xss1m",
45+
// set to headless, just in case,
46+
"-Djava.awt.headless=true",
47+
// ensure UTF-8 encoding by default (e.g., filenames)
48+
"-Dfile.encoding=UTF-8",
49+
// use our provided JNA always versus the system one
50+
"-Djna.nosys=true",
51+
/*
52+
* Turn off a JDK optimization that throws away stack traces for common exceptions because stack traces are important for
53+
* debugging.
54+
*/
55+
"-XX:-OmitStackTraceInFastThrow",
56+
// flags to configure Netty
57+
"-Dio.netty.noUnsafe=true",
58+
"-Dio.netty.noKeySetOptimization=true",
59+
"-Dio.netty.recycler.maxCapacityPerThread=0",
60+
"-Dio.netty.allocator.numDirectArenas=0",
61+
// log4j 2
62+
"-Dlog4j.shutdownHookEnabled=false",
63+
"-Dlog4j2.disable.jmx=true",
64+
/*
65+
* Due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise time/date
66+
* parsing will break in an incompatible way for some date patterns and locales.
67+
*/
68+
"-Djava.locale.providers=COMPAT"
69+
)
70+
);
6871
}
6972

7073
}

0 commit comments

Comments
 (0)