You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ag...@apache.org on 2018/01/11 11:30:11 UTC

ignite git commit: ignite-7352 sun.misc.SharedSecrets and sun.misc.JavaNioAccess direct usages are removed

Repository: ignite
Updated Branches:
  refs/heads/master 8f9be7188 -> 43c7ddad8


ignite-7352 sun.misc.SharedSecrets and sun.misc.JavaNioAccess direct usages are removed


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/43c7ddad
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/43c7ddad
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/43c7ddad

Branch: refs/heads/master
Commit: 43c7ddad85edd687a62032b732b33593b8322572
Parents: 8f9be71
Author: Andrey Gura <ag...@apache.org>
Authored: Wed Jan 10 17:51:51 2018 +0300
Committer: Andrey Gura <ag...@apache.org>
Committed: Thu Jan 11 14:19:13 2018 +0300

----------------------------------------------------------------------
 .../persistence/pagemem/PageMemoryImpl.java     | 22 +-----
 .../apache/ignite/internal/util/GridUnsafe.java | 76 +++++++++++++++++---
 2 files changed, 68 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/43c7ddad/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/pagemem/PageMemoryImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/pagemem/PageMemoryImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/pagemem/PageMemoryImpl.java
index fc8ae11..623bf54 100755
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/pagemem/PageMemoryImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/pagemem/PageMemoryImpl.java
@@ -18,7 +18,6 @@
 package org.apache.ignite.internal.processors.cache.persistence.pagemem;
 
 import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -78,11 +77,10 @@ import org.apache.ignite.internal.util.offheap.GridOffHeapOutOfMemoryException;
 import org.apache.ignite.internal.util.typedef.T2;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteBiTuple;
-import sun.misc.JavaNioAccess;
-import sun.misc.SharedSecrets;
 
 import static java.lang.Boolean.FALSE;
 import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.internal.util.GridUnsafe.wrapPointer;
 
 /**
  * Page header structure is described by the following diagram.
@@ -198,9 +196,6 @@ public class PageMemoryImpl implements PageMemoryEx {
     /** */
     private IgniteWriteAheadLogManager walMgr;
 
-    /** Direct byte buffer factory. */
-    private JavaNioAccess nioAccess;
-
     /** */
     private final IgniteLogger log;
 
@@ -296,8 +291,6 @@ public class PageMemoryImpl implements PageMemoryEx {
             regions.add(reg);
         }
 
-        nioAccess = SharedSecrets.getJavaNioAccess();
-
         int regs = regions.size();
 
         segments = new Segment[regs - 1];
@@ -1382,19 +1375,6 @@ public class PageMemoryImpl implements PageMemoryEx {
     }
 
     /**
-     * @param ptr Pointer to wrap.
-     * @param len Memory location length.
-     * @return Wrapped buffer.
-     */
-    ByteBuffer wrapPointer(long ptr, int len) {
-        ByteBuffer buf = nioAccess.newDirectByteBuffer(ptr, len, null);
-
-        buf.order(ByteOrder.nativeOrder());
-
-        return buf;
-    }
-
-    /**
      * @param pageId Page ID to check if it was added to the checkpoint list.
      * @return {@code True} if it was added to the checkpoint list.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/43c7ddad/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
index f4c5e80..8dce02f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUnsafe.java
@@ -28,8 +28,7 @@ import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import org.apache.ignite.IgniteSystemProperties;
-import sun.misc.JavaNioAccess;
-import sun.misc.SharedSecrets;
+import org.jetbrains.annotations.NotNull;
 import sun.misc.Unsafe;
 
 import static org.apache.ignite.internal.util.IgniteUtils.jdkVersion;
@@ -57,9 +56,6 @@ public abstract class GridUnsafe {
     /** */
     public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();
 
-    /** Direct byte buffer factory. */
-    private static final JavaNioAccess nioAccess = SharedSecrets.getJavaNioAccess();
-
     /** Unsafe. */
     private static final Unsafe UNSAFE = unsafe();
 
@@ -118,6 +114,12 @@ public abstract class GridUnsafe {
             ? new LegacyBase64Encoder()
             : new Base64EncoderImpl();
 
+    /** JavaNioAccess object. */
+    private static final Object JAVA_NIO_ACCESS_OBJ = javaNioAccessObject();
+
+    /** JavaNioAccess#newDirectByteBuffer method. */
+    private static final Method NEW_DIRECT_BUF_MTD = newDirectBufferMethod();
+
     /**
      * Ensure singleton.
      */
@@ -131,13 +133,18 @@ public abstract class GridUnsafe {
      * @return Byte buffer wrapping the given memory.
      */
     public static ByteBuffer wrapPointer(long ptr, int len) {
-        ByteBuffer buf = nioAccess.newDirectByteBuffer(ptr, len, null);
+        try {
+            ByteBuffer buf = (ByteBuffer)NEW_DIRECT_BUF_MTD.invoke(JAVA_NIO_ACCESS_OBJ, ptr, len, null);
 
-        assert buf.isDirect();
+            assert buf.isDirect();
 
-        buf.order(NATIVE_BYTE_ORDER);
+            buf.order(NATIVE_BYTE_ORDER);
 
-        return buf;
+            return buf;
+        }
+        catch (ReflectiveOperationException e) {
+            throw new RuntimeException("JavaNioAccess#newDirectByteBuffer() method is unavailable.", e);
+        }
     }
 
     /**
@@ -1451,6 +1458,57 @@ public abstract class GridUnsafe {
     }
 
     /**
+     * Returns {@code JavaNioAccess} instance from private API for corresponding Java version.
+     *
+     * @return {@code JavaNioAccess} instance for corresponding Java version.
+     * @throws RuntimeException If getting access to the private API is failed.
+     */
+    private static Object javaNioAccessObject() {
+        String pkgName = miscPackage();
+
+        try {
+            Class<?> cls = Class.forName(pkgName + ".misc.SharedSecrets");
+
+            Method mth = cls.getMethod("getJavaNioAccess");
+
+            return mth.invoke(null);
+        }
+        catch (ReflectiveOperationException e) {
+            throw new RuntimeException(pkgName + ".misc.JavaNioAccess class is unavailable.", e);
+        }
+    }
+
+    /**
+     * Returns reference to {@code JavaNioAccess.newDirectByteBuffer} method
+     * from private API for corresponding Java version.
+     *
+     * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
+     * @throws RuntimeException If getting access to the private API is failed.
+     */
+    private static Method newDirectBufferMethod() {
+
+        try {
+            Class<?> cls = JAVA_NIO_ACCESS_OBJ.getClass();
+
+            Method mtd = cls.getMethod("newDirectByteBuffer", long.class, int.class, Object.class);
+
+            mtd.setAccessible(true);
+
+            return mtd;
+        }
+        catch (ReflectiveOperationException e) {
+            throw new RuntimeException(miscPackage() + ".JavaNioAccess#newDirectByteBuffer() method is unavailable.", e);
+        }
+    }
+
+    /** */
+    @NotNull private static String miscPackage() {
+        int javaVer = majorJavaVersion(jdkVersion());
+
+        return javaVer < 9 ? "sun" : "jdk.internal";
+    }
+
+    /**
      * @param obj Object.
      * @param off Offset.
      * @param bigEndian Order of value bytes in memory. If {@code true} - big-endian, otherwise little-endian.