You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/04/13 19:07:05 UTC

[GitHub] [incubator-druid] gianm commented on a change in pull request #7466: Java 9 compatible ByteBuffer unmap operation

gianm commented on a change in pull request #7466: Java 9 compatible ByteBuffer unmap operation
URL: https://github.com/apache/incubator-druid/pull/7466#discussion_r275128857
 
 

 ##########
 File path: core/src/main/java/org/apache/druid/java/util/common/ByteBufferUtils.java
 ##########
 @@ -19,16 +19,124 @@
 
 package org.apache.druid.java.util.common;
 
-import sun.misc.Cleaner;
-import sun.nio.ch.DirectBuffer;
+import org.apache.druid.utils.JvmUtils;
 
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
 
 /**
  */
 public class ByteBufferUtils
 {
+  // the following MethodHandle lookup code is adapted from Apache Kafka
+  // https://github.com/apache/kafka/blob/e554dc518eaaa0747899e708160275f95c4e525f/clients/src/main/java/org/apache/kafka/common/utils/MappedByteBuffers.java
+
+  // null if unmap is not supported
+  private static final MethodHandle UNMAP;
+
+  // null if unmap is supported
+  private static final RuntimeException UNMAP_NOT_SUPPORTED_EXCEPTION;
+
+  static {
+    Object unmap = null;
+    RuntimeException exception = null;
+    try {
+      unmap = lookupUnmapMethodHandle();
+    }
+    catch (RuntimeException e) {
+      exception = e;
+    }
+    if (unmap != null) {
+      UNMAP = (MethodHandle) unmap;
+      UNMAP_NOT_SUPPORTED_EXCEPTION = null;
+    } else {
+      UNMAP = null;
+      UNMAP_NOT_SUPPORTED_EXCEPTION = exception;
+    }
+  }
+
+  private static void clean(ByteBuffer buffer)
+  {
+    if (!buffer.isDirect()) {
+      throw new IllegalArgumentException("Unmapping only works with direct buffers");
+    }
+    if (UNMAP == null) {
+      throw UNMAP_NOT_SUPPORTED_EXCEPTION;
+    }
+
+    try {
+      UNMAP.invokeExact(buffer);
+    }
+    catch (Throwable throwable) {
+      throw new RuntimeException("Unable to unmap the mapped buffer", throwable);
+    }
+  }
+
+  private static MethodHandle lookupUnmapMethodHandle()
+  {
+    final MethodHandles.Lookup lookup = MethodHandles.lookup();
+    try {
+      if (JvmUtils.isIsJava9Compatible()) {
+        return unmapJava9(lookup);
+      } else {
+        return unmapJava7Or8(lookup);
+      }
+    }
+    catch (ReflectiveOperationException | RuntimeException e1) {
+      throw new UnsupportedOperationException("Unmapping is not supported on this platform, because internal " +
+                                              "Java APIs are not compatible with this Druid version", e1);
+    }
+  }
+
+  private static MethodHandle unmapJava7Or8(MethodHandles.Lookup lookup) throws ReflectiveOperationException
+  {
+    // "Compile" a MethodHandle that is roughly equivalent to the following lambda:
+    //
+    // (ByteBuffer buffer) -> {
+    //   sun.misc.Cleaner cleaner = ((java.nio.DirectByteBuffer) byteBuffer).cleaner();
+    //   if (nonNull(cleaner))
+    //     cleaner.clean();
+    //   else
+    //     noop(cleaner); // the noop is needed because MethodHandles#guardWithTest always needs both if and else
+    // }
+    //
+    Class<?> directBufferClass = Class.forName("java.nio.DirectByteBuffer");
+    Method m = directBufferClass.getMethod("cleaner");
+    m.setAccessible(true);
 
 Review comment:
   setAccessible only affects this `Method` instance, not the actual method itself, so it should be ok. i.e. if someone else, later on, does `Class.forName("java.nio.DirectByteBuffer").getMethod("cleaner")` then that Method will still start out non-accessible.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org