This is an automated email from the ASF dual-hosted git repository.
gortiz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new 5b54378d2e7 Allow sun.misc.Unsafe memory access on JDK 24+ and warn
when Netty runs without it (#20244)
5b54378d2e7 is described below
commit 5b54378d2e7982648417812a0e1c1c37ab8e9d06
Author: Gonzalo Ortiz Jaureguizar <[email protected]>
AuthorDate: Wed Nov 18 10:30:44 2026 +0100
Allow sun.misc.Unsafe memory access on JDK 24+ and warn when Netty runs
without it (#20244)
---
.../pinot/core/transport/NettyInspector.java | 28 +++++-
.../apache/pinot/core/transport/NettyInstance.java | 30 ++++++
.../pinot/core/transport/NettyInspectorTest.java | 107 +++++++++++++++++++++
.../src/main/resources/appAssemblerScriptTemplate | 16 ++-
4 files changed, 178 insertions(+), 3 deletions(-)
diff --git
a/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java
b/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java
index 3bd43bbe7ea..6c0b88f9fa2 100644
---
a/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java
+++
b/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInspector.java
@@ -42,7 +42,8 @@ public class NettyInspector {
static {
CHECKS = new CopyOnWriteArrayList<>(
new Check[] {
- NettyInspector::checkDirectMemory
+ NettyInspector::checkDirectMemory,
+ NettyInspector::checkUnsafe
}
);
KNOWN_INSTANCES = new CopyOnWriteArrayList<>(new NettyInstance[] {
@@ -112,6 +113,22 @@ public class NettyInspector {
}
}
+ /// Fails when Netty does not use `sun.misc.Unsafe`.
+ ///
+ /// Netty 4.2 stops using `Unsafe` on JDK 25+ unless the JVM allows it with
+ /// `--sun-misc-unsafe-memory-access=allow`. Setting
`tryReflectionSetAccessible` is not enough on those JDKs, so
+ /// [#checkDirectMemory(NettyInstance)] can pass while Netty runs without
`Unsafe`. Without it, Netty allocates and
+ /// releases direct memory through slower paths, and
`PlatformDependent.allocateMemory` fails.
+ public static CheckResult checkUnsafe(NettyInstance instance) {
+ if (instance.hasUnsafe()) {
+ return CheckResult.SUCCESS;
+ }
+ String message = "sun.misc.Unsafe is not available on the " +
instance.getName() + " Netty instance (cause: "
+ + instance.getUnsafeUnavailabilityCause() + "). Netty will use slower
direct memory allocation and release. "
+ + "On JDK 24+, start the JVM with
--sun-misc-unsafe-memory-access=allow.";
+ return new CheckResult(message, CheckResult.Status.FAIL);
+ }
+
public interface Check extends Function<NettyInstance, CheckResult> {
/// Applies the check to the given Netty instance.
/// @param nettyInstance Netty instance to check
@@ -131,6 +148,15 @@ public class NettyInspector {
_status = status;
}
+ @Nullable
+ public String getMessage() {
+ return _message;
+ }
+
+ public Status getStatus() {
+ return _status;
+ }
+
public enum Status {
PASS,
FAIL,
diff --git
a/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInstance.java
b/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInstance.java
index e7259202516..964d8033781 100644
---
a/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInstance.java
+++
b/pinot-core/src/main/java/org/apache/pinot/core/transport/NettyInstance.java
@@ -20,6 +20,7 @@
package org.apache.pinot.core.transport;
import java.lang.reflect.Constructor;
+import javax.annotation.Nullable;
/// Represents an instance of Netty, allowing access to certain static
properties via reflection, with support for
/// shaded Netty versions.
@@ -66,6 +67,13 @@ public abstract class NettyInstance {
public abstract boolean isExplicitTryReflectionSetAccessible();
+ /// Returns whether this Netty instance uses `sun.misc.Unsafe`.
+ public abstract boolean hasUnsafe();
+
+ /// Returns why `sun.misc.Unsafe` is not available to this Netty instance,
or `null` if it is available.
+ @Nullable
+ public abstract Throwable getUnsafeUnavailabilityCause();
+
public abstract long getUsedDirectMemory();
public abstract long getMaxDirectMemory();
@@ -86,6 +94,17 @@ public abstract class NettyInstance {
return
io.netty.util.internal.ReflectionUtil.trySetAccessible(CONSTRUCTOR, true) ==
null;
}
+ @Override
+ public boolean hasUnsafe() {
+ return io.netty.util.internal.PlatformDependent.hasUnsafe();
+ }
+
+ @Nullable
+ @Override
+ public Throwable getUnsafeUnavailabilityCause() {
+ return
io.netty.util.internal.PlatformDependent.getUnsafeUnavailabilityCause();
+ }
+
@Override
public long getUsedDirectMemory() {
return io.netty.util.internal.PlatformDependent.usedDirectMemory();
@@ -113,6 +132,17 @@ public abstract class NettyInstance {
return
io.grpc.netty.shaded.io.netty.util.internal.ReflectionUtil.trySetAccessible(CONSTRUCTOR,
true) == null;
}
+ @Override
+ public boolean hasUnsafe() {
+ return
io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent.hasUnsafe();
+ }
+
+ @Nullable
+ @Override
+ public Throwable getUnsafeUnavailabilityCause() {
+ return
io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent.getUnsafeUnavailabilityCause();
+ }
+
@Override
public long getUsedDirectMemory() {
return
io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent.usedDirectMemory();
diff --git
a/pinot-core/src/test/java/org/apache/pinot/core/transport/NettyInspectorTest.java
b/pinot-core/src/test/java/org/apache/pinot/core/transport/NettyInspectorTest.java
new file mode 100644
index 00000000000..f72db9e184a
--- /dev/null
+++
b/pinot-core/src/test/java/org/apache/pinot/core/transport/NettyInspectorTest.java
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF 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.apache.pinot.core.transport;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+
+public class NettyInspectorTest {
+
+ @Test
+ public void checkUnsafePassesWhenNettyHasUnsafe() {
+ NettyInspector.CheckResult result = NettyInspector.checkUnsafe(new
FakeNettyInstance(null));
+
+ assertEquals(result.getStatus(), NettyInspector.CheckResult.Status.PASS);
+ assertNull(result.getMessage());
+ }
+
+ @Test
+ public void checkUnsafeFailsWhenNettyHasNoUnsafe() {
+ Throwable cause = new
UnsupportedOperationException("io.netty.noUnsafe=true by default on Java 25+");
+
+ NettyInspector.CheckResult result = NettyInspector.checkUnsafe(new
FakeNettyInstance(cause));
+
+ assertEquals(result.getStatus(), NettyInspector.CheckResult.Status.FAIL);
+ String message = result.getMessage();
+ assertNotNull(message);
+ assertTrue(message.contains("fake"), message);
+ assertTrue(message.contains("io.netty.noUnsafe=true by default on Java
25+"), message);
+ assertTrue(message.contains("--sun-misc-unsafe-memory-access=allow"),
message);
+ }
+
+ @Test
+ public void defaultChecksIncludeUnsafe() {
+ NettyInstance instance = new FakeNettyInstance(new
UnsupportedOperationException("no unsafe"));
+
+ boolean failed = NettyInspector.CHECKS.stream()
+ .map(check -> check.apply(instance))
+ .anyMatch(result -> result.getStatus() ==
NettyInspector.CheckResult.Status.FAIL);
+
+ assertTrue(failed);
+ }
+
+ /// A [NettyInstance] whose Unsafe availability is fixed by the test.
Reflection access is always available.
+ private static class FakeNettyInstance extends NettyInstance {
+ private final Throwable _unsafeUnavailabilityCause;
+
+ FakeNettyInstance(Throwable unsafeUnavailabilityCause) {
+ _unsafeUnavailabilityCause = unsafeUnavailabilityCause;
+ }
+
+ @Override
+ public String getName() {
+ return "fake";
+ }
+
+ @Override
+ public String getShadePrefix() {
+ return "";
+ }
+
+ @Override
+ public boolean isExplicitTryReflectionSetAccessible() {
+ return true;
+ }
+
+ @Override
+ public boolean hasUnsafe() {
+ return _unsafeUnavailabilityCause == null;
+ }
+
+ @Override
+ public Throwable getUnsafeUnavailabilityCause() {
+ return _unsafeUnavailabilityCause;
+ }
+
+ @Override
+ public long getUsedDirectMemory() {
+ return 0;
+ }
+
+ @Override
+ public long getMaxDirectMemory() {
+ return 0;
+ }
+ }
+}
diff --git a/pinot-tools/src/main/resources/appAssemblerScriptTemplate
b/pinot-tools/src/main/resources/appAssemblerScriptTemplate
index 3e17cc163d5..386e5c5637a 100644
--- a/pinot-tools/src/main/resources/appAssemblerScriptTemplate
+++ b/pinot-tools/src/main/resources/appAssemblerScriptTemplate
@@ -179,7 +179,7 @@ jdk_version() {
IFS='
'
# remove \r for Cygwin
- lines=$(java -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
+ lines=$("$JAVACMD" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
for line in $lines; do
if test -z $result && echo "$line" | grep -q 'version "'
then
@@ -203,7 +203,8 @@ else
ALL_JAVA_OPTS=$JAVA_OPTS
fi
-if [ "$(jdk_version)" -gt 11 ]; then
+JDK_VERSION=$(jdk_version)
+if [ "$JDK_VERSION" -gt 11 ]; then
ALL_JAVA_OPTS="--add-opens=java.base/java.nio=ALL-UNNAMED \
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED \
--add-opens=java.base/java.lang=ALL-UNNAMED \
@@ -215,6 +216,17 @@ if [ "$(jdk_version)" -gt 11 ]; then
$JAVA_OPTS"
fi
+# JDK 24+ warns on sun.misc.Unsafe memory access (JEP 471), and Netty 4.2
stops using Unsafe on JDK 25+ unless the
+# JVM allows it. Without Unsafe, Netty creates and closes one shared Arena per
pooled chunk, which makes allocation
+# and release much slower, and PlatformDependent.allocateMemory (used for
buffers over 2 GB) fails. Allowing it
+# also removes the JEP 471 warning from Pinot's own Unsafe-based buffers.
--enable-native-access lets JNI and FFM
+# libraries load without the JEP 472 warnings. JAVA_OPTS comes later on the
command line, so a
+# --sun-misc-unsafe-memory-access value set there overrides this one. Native
access grants add up: JAVA_OPTS cannot
+# remove the ALL-UNNAMED grant.
+if [ "$JDK_VERSION" -ge 24 ]; then
+ ALL_JAVA_OPTS="--sun-misc-unsafe-memory-access=allow
--enable-native-access=ALL-UNNAMED $ALL_JAVA_OPTS"
+fi
+
if [ -z "$PLUGINS_DIR" ] ; then
PLUGINS_DIR=$BASEDIR/plugins
fi
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]