From f7b8f0b2f43e962fb337192ae14eaa7982ad6713 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 12 May 2020 14:30:45 -0400 Subject: [PATCH] Adjust warning for heap size bootstrap check (#56565) Today the heap size check warns the user about two issues why they might care about the heap size check: resize pauses, and if memory locking is enabled. Yet, we unconditionally make mention of the memory locking reason, even if memory locking is not enabled. This can confuse some users, so we adjust the warning about memory locking to only display if memory locking is enabled. --- .../bootstrap/BootstrapChecks.java | 18 ++++++++++++++++-- .../bootstrap/BootstrapChecksTests.java | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java b/server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java index e62d118c65b..c34d84d7883 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/BootstrapChecks.java @@ -226,12 +226,22 @@ final class BootstrapChecks { final long initialHeapSize = getInitialHeapSize(); final long maxHeapSize = getMaxHeapSize(); if (initialHeapSize != 0 && maxHeapSize != 0 && initialHeapSize != maxHeapSize) { - final String message = String.format( + final String message; + if (isMemoryLocked()) { + message = String.format( Locale.ROOT, "initial heap size [%d] not equal to maximum heap size [%d]; " + - "this can cause resize pauses and prevents mlockall from locking the entire heap", + "this can cause resize pauses and prevents memory locking from locking the entire heap", getInitialHeapSize(), getMaxHeapSize()); + } else { + message = String.format( + Locale.ROOT, + "initial heap size [%d] not equal to maximum heap size [%d]; " + + "this can cause resize pauses", + getInitialHeapSize(), + getMaxHeapSize()); + } return BootstrapCheckResult.failure(message); } else { return BootstrapCheckResult.success(); @@ -248,6 +258,10 @@ final class BootstrapChecks { return JvmInfo.jvmInfo().getConfiguredMaxHeapSize(); } + boolean isMemoryLocked() { + return Natives.isMemoryLocked(); + } + } static class OsXFileDescriptorCheck extends FileDescriptorCheck { diff --git a/server/src/test/java/org/elasticsearch/bootstrap/BootstrapChecksTests.java b/server/src/test/java/org/elasticsearch/bootstrap/BootstrapChecksTests.java index f6c2f236b0e..0bcef6ebc81 100644 --- a/server/src/test/java/org/elasticsearch/bootstrap/BootstrapChecksTests.java +++ b/server/src/test/java/org/elasticsearch/bootstrap/BootstrapChecksTests.java @@ -32,6 +32,7 @@ import org.elasticsearch.discovery.SettingsBasedSeedHostsProvider; import org.elasticsearch.monitor.jvm.JvmInfo; import org.elasticsearch.node.NodeValidationException; import org.elasticsearch.test.AbstractBootstrapCheckTestCase; +import org.hamcrest.Matcher; import java.net.InetAddress; import java.util.ArrayList; @@ -51,6 +52,7 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.not; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -150,8 +152,10 @@ public class BootstrapChecksTests extends AbstractBootstrapCheckTestCase { final int max = randomIntBetween(initial + 1, Integer.MAX_VALUE); final AtomicLong initialHeapSize = new AtomicLong(initial); final AtomicLong maxHeapSize = new AtomicLong(max); + final boolean isMemoryLocked = randomBoolean(); final BootstrapChecks.HeapSizeCheck check = new BootstrapChecks.HeapSizeCheck() { + @Override long getInitialHeapSize() { return initialHeapSize.get(); @@ -161,6 +165,12 @@ public class BootstrapChecksTests extends AbstractBootstrapCheckTestCase { long getMaxHeapSize() { return maxHeapSize.get(); } + + @Override + boolean isMemoryLocked() { + return isMemoryLocked; + } + }; final NodeValidationException e = @@ -171,6 +181,14 @@ public class BootstrapChecksTests extends AbstractBootstrapCheckTestCase { e.getMessage(), containsString("initial heap size [" + initialHeapSize.get() + "] " + "not equal to maximum heap size [" + maxHeapSize.get() + "]")); + final String memoryLockingMessage = "and prevents memory locking from locking the entire heap"; + final Matcher memoryLockingMatcher; + if (isMemoryLocked) { + memoryLockingMatcher = containsString(memoryLockingMessage); + } else { + memoryLockingMatcher = not(containsString(memoryLockingMessage)); + } + assertThat(e.getMessage(), memoryLockingMatcher); initialHeapSize.set(maxHeapSize.get());