Browse Source

Choose JVM options ergonomically

With this commit we add the possibility to define further JVM options (and
system properties) based on the current environment. As a proof of concept, it
chooses Netty's allocator ergonomically based on the maximum defined heap size.
We switch to the unpooled allocator at 1GB heap size (value determined
experimentally, see #30684 for more details). We are also explicit about the
choice of the allocator in either case.

Relates #30684
Daniel Mitterdorfer 7 years ago
parent
commit
2aefb72891

+ 108 - 0
distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmErgonomics.java

@@ -0,0 +1,108 @@
+/*
+ * 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.
+ */
+
+package org.elasticsearch.tools.launchers;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Tunes Elasticsearch JVM settings based on inspection of provided JVM options.
+ */
+final class JvmErgonomics {
+    private static final long KB = 1024L;
+
+    private static final long MB = 1024L * 1024L;
+
+    private static final long GB = 1024L * 1024L * 1024L;
+
+
+    private JvmErgonomics() {
+        throw new AssertionError("No instances intended");
+    }
+
+    /**
+     * Chooses additional JVM options for Elasticsearch.
+     *
+     * @param userDefinedJvmOptions A list of JVM options that have been defined by the user.
+     * @return A list of additional JVM options to set.
+     */
+    static List<String> choose(List<String> userDefinedJvmOptions) {
+        List<String> ergonomicChoices = new ArrayList<>();
+        Long heapSize = extractHeapSize(userDefinedJvmOptions);
+        Map<String, String> systemProperties = extractSystemProperties(userDefinedJvmOptions);
+        if (heapSize != null) {
+            if (systemProperties.containsKey("io.netty.allocator.type") == false) {
+                if (heapSize <= 1 * GB) {
+                    ergonomicChoices.add("-Dio.netty.allocator.type=unpooled");
+                } else {
+                    ergonomicChoices.add("-Dio.netty.allocator.type=pooled");
+                }
+            }
+        }
+        return ergonomicChoices;
+    }
+
+    private static final Pattern MAX_HEAP_SIZE = Pattern.compile("^(-Xmx|-XX:MaxHeapSize=)(?<size>\\d+)(?<unit>\\w)?$");
+
+    // package private for testing
+    static Long extractHeapSize(List<String> userDefinedJvmOptions) {
+        for (String jvmOption : userDefinedJvmOptions) {
+            final Matcher matcher = MAX_HEAP_SIZE.matcher(jvmOption);
+            if (matcher.matches()) {
+                final long size = Long.parseLong(matcher.group("size"));
+                final String unit = matcher.group("unit");
+                if (unit == null) {
+                    return size;
+                } else {
+                    switch (unit.toLowerCase(Locale.ROOT)) {
+                        case "k":
+                            return size * KB;
+                        case "m":
+                            return size * MB;
+                        case "g":
+                            return size * GB;
+                        default:
+                            throw new IllegalArgumentException("Unknown unit [" + unit + "] for max heap size in [" + jvmOption + "]");
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    private static final Pattern SYSTEM_PROPERTY = Pattern.compile("^-D(?<key>[\\w+].*?)=(?<value>.*)$");
+
+    // package private for testing
+    static Map<String, String> extractSystemProperties(List<String> userDefinedJvmOptions) {
+        Map<String, String> systemProperties = new HashMap<>();
+        for (String jvmOption : userDefinedJvmOptions) {
+            final Matcher matcher = SYSTEM_PROPERTY.matcher(jvmOption);
+            if (matcher.matches()) {
+                systemProperties.put(matcher.group("key"), matcher.group("value"));
+            }
+        }
+        return systemProperties;
+    }
+}

+ 2 - 0
distribution/tools/launchers/src/main/java/org/elasticsearch/tools/launchers/JvmOptionsParser.java

@@ -78,6 +78,8 @@ final class JvmOptionsParser {
         }
 
         if (invalidLines.isEmpty()) {
+            List<String> ergonomicJvmOptions = JvmErgonomics.choose(jvmOptions);
+            jvmOptions.addAll(ergonomicJvmOptions);
             final String spaceDelimitedJvmOptions = spaceDelimitJvmOptions(jvmOptions);
             Launchers.outPrintln(spaceDelimitedJvmOptions);
             Launchers.exit(0);

+ 83 - 0
distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/JvmErgonomicsTests.java

@@ -0,0 +1,83 @@
+/*
+ * 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.
+ */
+
+package org.elasticsearch.tools.launchers;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class JvmErgonomicsTests extends LaunchersTestCase {
+    public void testExtractValidHeapSize() {
+        assertEquals(Long.valueOf(1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx1024")));
+        assertEquals(Long.valueOf(2L * 1024 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx2g")));
+        assertEquals(Long.valueOf(32 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx32M")));
+        assertEquals(Long.valueOf(32 * 1024 * 1024), JvmErgonomics.extractHeapSize(Collections.singletonList("-XX:MaxHeapSize=32M")));
+    }
+
+    public void testExtractInvalidHeapSize() {
+        try {
+            JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx2T"));
+            fail("Expected IllegalArgumentException to be raised");
+        } catch (IllegalArgumentException expected) {
+            assertEquals("Unknown unit [T] for max heap size in [-Xmx2T]", expected.getMessage());
+        }
+    }
+
+    public void testExtractNoHeapSize() {
+        assertNull("No spaces allowed", JvmErgonomics.extractHeapSize(Collections.singletonList("-Xmx 1024")));
+        assertNull("JVM option is not present", JvmErgonomics.extractHeapSize(Collections.singletonList("")));
+        assertNull("Multiple JVM options per line", JvmErgonomics.extractHeapSize(Collections.singletonList("-Xms2g -Xmx2g")));
+    }
+
+    public void testExtractSystemProperties() {
+        Map<String, String> expectedSystemProperties = new HashMap<>();
+        expectedSystemProperties.put("file.encoding", "UTF-8");
+        expectedSystemProperties.put("kv.setting", "ABC=DEF");
+
+        Map<String, String> parsedSystemProperties = JvmErgonomics.extractSystemProperties(
+            Arrays.asList("-Dfile.encoding=UTF-8", "-Dkv.setting=ABC=DEF"));
+
+        assertEquals(expectedSystemProperties, parsedSystemProperties);
+    }
+
+    public void testExtractNoSystemProperties() {
+        Map<String, String> parsedSystemProperties = JvmErgonomics.extractSystemProperties(Arrays.asList("-Xms1024M", "-Xmx1024M"));
+        assertTrue(parsedSystemProperties.isEmpty());
+    }
+
+    public void testLittleMemoryErgonomicChoices() {
+        String smallHeap = randomFrom(Arrays.asList("64M", "512M", "1024M", "1G"));
+        List<String> expectedChoices = Collections.singletonList("-Dio.netty.allocator.type=unpooled");
+        assertEquals(expectedChoices, JvmErgonomics.choose(Arrays.asList("-Xms" + smallHeap, "-Xmx" + smallHeap)));
+    }
+
+    public void testPlentyMemoryErgonomicChoices() {
+        String largeHeap = randomFrom(Arrays.asList("1025M", "2048M", "2G", "8G"));
+        List<String> expectedChoices = Collections.singletonList("-Dio.netty.allocator.type=pooled");
+        assertEquals(expectedChoices, JvmErgonomics.choose(Arrays.asList("-Xms" + largeHeap, "-Xmx" + largeHeap)));
+    }
+}