sashapolo commented on code in PR #5741:
URL: https://github.com/apache/ignite-3/pull/5741#discussion_r2073421549


##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;

Review Comment:
   Please add some javadocs about what these method handles correspond to



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD
+
+                    e.addSuppressed(exFallback);
+
+                    throw e; // Fallback to shared secrets failed.
+                }
+
+                if (nioAccessObj == null || directBufMtd == null) {
+                    throw e;
+                }
+            }
+        }
+
+        DIRECT_BUF_MTD = directBufMtd;
+        DIRECT_BUF_CTOR_INT = directBufCtorWithIntLen;
+        DIRECT_BUF_CTOR_LONG = directBufCtorWithLongLen;
+
+        JAVA_NIO_ACCESS_OBJ = nioAccessObj;
+    }
+
     /**
      * Wraps a pointer to unmanaged memory into a direct byte buffer.
      *
      * @param ptr Pointer to wrap.
      * @param len Memory location length.
      * @return Byte buffer wrapping the given memory.
      */
-    ByteBuffer wrapPointer(long ptr, int len);
+    static ByteBuffer wrapPointer(long ptr, int len) {
+        if (DIRECT_BUF_MTD != null && JAVA_NIO_ACCESS_OBJ != null) {
+            return wrapPointerJavaNio(ptr, len);
+        } else if (DIRECT_BUF_CTOR_INT != null) {
+            return wrapPointerDirectBufferConstructor(ptr, len);
+        } else if (DIRECT_BUF_CTOR_LONG != null) {
+            return wrapPointerDirectBufferConstructor(ptr, (long) len);
+        } else {
+            throw new RuntimeException(
+                    "All alternatives for a new DirectByteBuffer() creation 
failed: " + FeatureChecker.JAVA_STARTUP_PARAMS_WARN);
+        }
+    }
+
+    /**
+     * 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() {
+        Class<?> cls;
+        try {
+            cls = Class.forName("jdk.internal.access.SharedSecrets");
+        } catch (ClassNotFoundException e) {
+            try {
+                cls = Class.forName("jdk.internal.misc.SharedSecrets");
+            } catch (ClassNotFoundException e1) {
+                throw new RuntimeException("Neither 
jdk.internal.access.SharedSecrets nor jdk.internal.misc.SharedSecrets are 
unavailable."
+                        + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+            }
+        }
+        try {
+            Method mth = cls.getMethod("getJavaNioAccess");
+
+            return mth.invoke(null);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(cls.getName() + " class is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+    /**
+     * Returns reference to {@code JavaNioAccess.newDirectByteBuffer} method 
from private API for corresponding Java version.
+     *
+     * @param nioAccessObj Java NIO access object.
+     * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
+     * @throws RuntimeException If getting access to the private API is failed.
+     */
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
+        try {
+            Class<?> cls = nioAccessObj.getClass();
+
+            Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
+
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>) () -> 
{
+                mtd.setAccessible(true);
+
+                return null;
+            });
+
+            MethodType mtdType = MethodType.methodType(
+                    ByteBuffer.class,
+                    Object.class,
+                    long.class,
+                    int.class,
+                    Object.class
+            );
+
+            return MethodHandles.lookup()
+                    .unreflect(mtd)
+                    .asType(mtdType);
+        } catch (ReflectiveOperationException | PrivilegedActionException e) {
+            throw new RuntimeException(nioAccessObj.getClass().getName() + 
"#newDirectByteBuffer() method is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+
+    /**
+     * Creates and tests contructor for Direct ByteBuffer. Test is wrapping 
one-byte unsafe memory into a buffer.
+     *
+     * @param lengthType Type of the length parameter.
+     * @return constructor for creating direct ByteBuffers.
+     */
+    private static MethodHandle createAndTestNewDirectBufferCtor(Class<?> 
lengthType) {
+        assert lengthType == int.class || lengthType == long.class : 
"Unsupported type of length: " + lengthType;
+
+        MethodHandle ctorCandidate = createNewDirectBufferCtor(lengthType);
+
+        int l = 1;
+        long ptr = UNSAFE.allocateMemory(l);
+
+        try {
+            ByteBuffer buf = lengthType == int.class
+                    ? (ByteBuffer) ctorCandidate.invokeExact(ptr, l)
+                    : (ByteBuffer) ctorCandidate.invokeExact(ptr, (long) l);
+
+            if (!buf.isDirect()) {
+                throw new IllegalArgumentException("Buffer expected to be 
direct, internal error during #wrapPointerDirectBufCtor()");
+            }
+        } catch (Throwable t) {
+            throw new RuntimeException(t);
+        } finally {
+            UNSAFE.freeMemory(ptr);
+        }
+
+        return ctorCandidate;
+    }
+
+
+    /**
+     * Simply create some instance of direct Byte Buffer and try to get it's 
class declared constructor.
+     *
+     * @param lengthType Type of the length parameter.
+     * @return constructor for creating direct ByteBuffers.
+     */
+    private static MethodHandle createNewDirectBufferCtor(Class<?> lengthType) 
{
+        try {
+            ByteBuffer buf = 
ByteBuffer.allocateDirect(1).order(NATIVE_BYTE_ORDER);
+
+            Constructor<?> ctor = 
buf.getClass().getDeclaredConstructor(long.class, lengthType);
+
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>) () -> 
{
+                ctor.setAccessible(true);
+
+                return null;
+            });
+
+            MethodType mtdType = MethodType.methodType(
+                    ByteBuffer.class,
+                    long.class,
+                    lengthType
+            );
+
+            return MethodHandles.lookup()
+                    .unreflectConstructor(ctor)
+                    .asType(mtdType);
+        } catch (ReflectiveOperationException | PrivilegedActionException e) {
+            throw new RuntimeException("Unable to set up byte buffer creation 
using reflection :" + e.getMessage(), e);
+        }
+    }
+
+

Review Comment:
   ```suggestion
   ```



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD

Review Comment:
   Why do we need this?



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD
+
+                    e.addSuppressed(exFallback);
+
+                    throw e; // Fallback to shared secrets failed.
+                }
+
+                if (nioAccessObj == null || directBufMtd == null) {
+                    throw e;
+                }
+            }
+        }
+
+        DIRECT_BUF_MTD = directBufMtd;
+        DIRECT_BUF_CTOR_INT = directBufCtorWithIntLen;
+        DIRECT_BUF_CTOR_LONG = directBufCtorWithLongLen;
+
+        JAVA_NIO_ACCESS_OBJ = nioAccessObj;
+    }
+
     /**
      * Wraps a pointer to unmanaged memory into a direct byte buffer.
      *
      * @param ptr Pointer to wrap.
      * @param len Memory location length.
      * @return Byte buffer wrapping the given memory.
      */
-    ByteBuffer wrapPointer(long ptr, int len);
+    static ByteBuffer wrapPointer(long ptr, int len) {

Review Comment:
   Can we implement this as a reference to a method that will be computed in 
the static block? Or will it bring the same performance penalties?



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD
+
+                    e.addSuppressed(exFallback);
+
+                    throw e; // Fallback to shared secrets failed.
+                }
+
+                if (nioAccessObj == null || directBufMtd == null) {
+                    throw e;
+                }
+            }
+        }
+
+        DIRECT_BUF_MTD = directBufMtd;
+        DIRECT_BUF_CTOR_INT = directBufCtorWithIntLen;
+        DIRECT_BUF_CTOR_LONG = directBufCtorWithLongLen;
+
+        JAVA_NIO_ACCESS_OBJ = nioAccessObj;
+    }
+
     /**
      * Wraps a pointer to unmanaged memory into a direct byte buffer.
      *
      * @param ptr Pointer to wrap.
      * @param len Memory location length.
      * @return Byte buffer wrapping the given memory.
      */
-    ByteBuffer wrapPointer(long ptr, int len);
+    static ByteBuffer wrapPointer(long ptr, int len) {
+        if (DIRECT_BUF_MTD != null && JAVA_NIO_ACCESS_OBJ != null) {
+            return wrapPointerJavaNio(ptr, len);
+        } else if (DIRECT_BUF_CTOR_INT != null) {
+            return wrapPointerDirectBufferConstructor(ptr, len);
+        } else if (DIRECT_BUF_CTOR_LONG != null) {
+            return wrapPointerDirectBufferConstructor(ptr, (long) len);
+        } else {
+            throw new RuntimeException(
+                    "All alternatives for a new DirectByteBuffer() creation 
failed: " + FeatureChecker.JAVA_STARTUP_PARAMS_WARN);
+        }
+    }
+
+    /**
+     * 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() {
+        Class<?> cls;
+        try {
+            cls = Class.forName("jdk.internal.access.SharedSecrets");
+        } catch (ClassNotFoundException e) {
+            try {
+                cls = Class.forName("jdk.internal.misc.SharedSecrets");
+            } catch (ClassNotFoundException e1) {
+                throw new RuntimeException("Neither 
jdk.internal.access.SharedSecrets nor jdk.internal.misc.SharedSecrets are 
unavailable."
+                        + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+            }
+        }
+        try {
+            Method mth = cls.getMethod("getJavaNioAccess");
+
+            return mth.invoke(null);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(cls.getName() + " class is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+    /**
+     * Returns reference to {@code JavaNioAccess.newDirectByteBuffer} method 
from private API for corresponding Java version.
+     *
+     * @param nioAccessObj Java NIO access object.
+     * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
+     * @throws RuntimeException If getting access to the private API is failed.
+     */
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
+        try {
+            Class<?> cls = nioAccessObj.getClass();
+
+            Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
+
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>) () -> 
{
+                mtd.setAccessible(true);
+
+                return null;
+            });
+
+            MethodType mtdType = MethodType.methodType(
+                    ByteBuffer.class,
+                    Object.class,
+                    long.class,
+                    int.class,
+                    Object.class
+            );
+
+            return MethodHandles.lookup()
+                    .unreflect(mtd)
+                    .asType(mtdType);
+        } catch (ReflectiveOperationException | PrivilegedActionException e) {
+            throw new RuntimeException(nioAccessObj.getClass().getName() + 
"#newDirectByteBuffer() method is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+
+    /**
+     * Creates and tests contructor for Direct ByteBuffer. Test is wrapping 
one-byte unsafe memory into a buffer.
+     *
+     * @param lengthType Type of the length parameter.
+     * @return constructor for creating direct ByteBuffers.
+     */
+    private static MethodHandle createAndTestNewDirectBufferCtor(Class<?> 
lengthType) {
+        assert lengthType == int.class || lengthType == long.class : 
"Unsupported type of length: " + lengthType;
+
+        MethodHandle ctorCandidate = createNewDirectBufferCtor(lengthType);
+
+        int l = 1;
+        long ptr = UNSAFE.allocateMemory(l);
+
+        try {
+            ByteBuffer buf = lengthType == int.class
+                    ? (ByteBuffer) ctorCandidate.invokeExact(ptr, l)
+                    : (ByteBuffer) ctorCandidate.invokeExact(ptr, (long) l);
+
+            if (!buf.isDirect()) {
+                throw new IllegalArgumentException("Buffer expected to be 
direct, internal error during #wrapPointerDirectBufCtor()");
+            }
+        } catch (Throwable t) {
+            throw new RuntimeException(t);
+        } finally {
+            UNSAFE.freeMemory(ptr);
+        }
+
+        return ctorCandidate;
+    }
+
+

Review Comment:
   ```suggestion
   ```



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD
+
+                    e.addSuppressed(exFallback);
+
+                    throw e; // Fallback to shared secrets failed.
+                }
+
+                if (nioAccessObj == null || directBufMtd == null) {
+                    throw e;
+                }
+            }
+        }
+
+        DIRECT_BUF_MTD = directBufMtd;
+        DIRECT_BUF_CTOR_INT = directBufCtorWithIntLen;
+        DIRECT_BUF_CTOR_LONG = directBufCtorWithLongLen;
+
+        JAVA_NIO_ACCESS_OBJ = nioAccessObj;
+    }
+
     /**
      * Wraps a pointer to unmanaged memory into a direct byte buffer.
      *
      * @param ptr Pointer to wrap.
      * @param len Memory location length.
      * @return Byte buffer wrapping the given memory.
      */
-    ByteBuffer wrapPointer(long ptr, int len);
+    static ByteBuffer wrapPointer(long ptr, int len) {
+        if (DIRECT_BUF_MTD != null && JAVA_NIO_ACCESS_OBJ != null) {
+            return wrapPointerJavaNio(ptr, len);
+        } else if (DIRECT_BUF_CTOR_INT != null) {
+            return wrapPointerDirectBufferConstructor(ptr, len);
+        } else if (DIRECT_BUF_CTOR_LONG != null) {
+            return wrapPointerDirectBufferConstructor(ptr, (long) len);
+        } else {
+            throw new RuntimeException(
+                    "All alternatives for a new DirectByteBuffer() creation 
failed: " + FeatureChecker.JAVA_STARTUP_PARAMS_WARN);
+        }
+    }
+
+    /**
+     * 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() {
+        Class<?> cls;
+        try {
+            cls = Class.forName("jdk.internal.access.SharedSecrets");
+        } catch (ClassNotFoundException e) {
+            try {
+                cls = Class.forName("jdk.internal.misc.SharedSecrets");
+            } catch (ClassNotFoundException e1) {
+                throw new RuntimeException("Neither 
jdk.internal.access.SharedSecrets nor jdk.internal.misc.SharedSecrets are 
unavailable."
+                        + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+            }
+        }
+        try {
+            Method mth = cls.getMethod("getJavaNioAccess");
+
+            return mth.invoke(null);
+        } catch (ReflectiveOperationException e) {
+            throw new RuntimeException(cls.getName() + " class is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+    /**
+     * Returns reference to {@code JavaNioAccess.newDirectByteBuffer} method 
from private API for corresponding Java version.
+     *
+     * @param nioAccessObj Java NIO access object.
+     * @return Reference to {@code JavaNioAccess.newDirectByteBuffer} method
+     * @throws RuntimeException If getting access to the private API is failed.
+     */
+    private static MethodHandle newDirectBufferMethodHandle(Object 
nioAccessObj) {
+        try {
+            Class<?> cls = nioAccessObj.getClass();
+
+            Method mtd = cls.getMethod("newDirectByteBuffer", long.class, 
int.class, Object.class);
+
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>) () -> 
{
+                mtd.setAccessible(true);
+
+                return null;
+            });
+
+            MethodType mtdType = MethodType.methodType(
+                    ByteBuffer.class,
+                    Object.class,
+                    long.class,
+                    int.class,
+                    Object.class
+            );
+
+            return MethodHandles.lookup()
+                    .unreflect(mtd)
+                    .asType(mtdType);
+        } catch (ReflectiveOperationException | PrivilegedActionException e) {
+            throw new RuntimeException(nioAccessObj.getClass().getName() + 
"#newDirectByteBuffer() method is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+
+    /**
+     * Creates and tests contructor for Direct ByteBuffer. Test is wrapping 
one-byte unsafe memory into a buffer.
+     *
+     * @param lengthType Type of the length parameter.
+     * @return constructor for creating direct ByteBuffers.
+     */
+    private static MethodHandle createAndTestNewDirectBufferCtor(Class<?> 
lengthType) {
+        assert lengthType == int.class || lengthType == long.class : 
"Unsupported type of length: " + lengthType;
+
+        MethodHandle ctorCandidate = createNewDirectBufferCtor(lengthType);
+
+        int l = 1;
+        long ptr = UNSAFE.allocateMemory(l);
+
+        try {
+            ByteBuffer buf = lengthType == int.class
+                    ? (ByteBuffer) ctorCandidate.invokeExact(ptr, l)
+                    : (ByteBuffer) ctorCandidate.invokeExact(ptr, (long) l);
+
+            if (!buf.isDirect()) {
+                throw new IllegalArgumentException("Buffer expected to be 
direct, internal error during #wrapPointerDirectBufCtor()");
+            }
+        } catch (Throwable t) {
+            throw new RuntimeException(t);
+        } finally {
+            UNSAFE.freeMemory(ptr);
+        }
+
+        return ctorCandidate;
+    }
+
+
+    /**
+     * Simply create some instance of direct Byte Buffer and try to get it's 
class declared constructor.
+     *
+     * @param lengthType Type of the length parameter.
+     * @return constructor for creating direct ByteBuffers.
+     */
+    private static MethodHandle createNewDirectBufferCtor(Class<?> lengthType) 
{
+        try {
+            ByteBuffer buf = 
ByteBuffer.allocateDirect(1).order(NATIVE_BYTE_ORDER);
+
+            Constructor<?> ctor = 
buf.getClass().getDeclaredConstructor(long.class, lengthType);
+
+            AccessController.doPrivileged((PrivilegedExceptionAction<?>) () -> 
{
+                ctor.setAccessible(true);
+
+                return null;
+            });
+
+            MethodType mtdType = MethodType.methodType(
+                    ByteBuffer.class,
+                    long.class,
+                    lengthType
+            );
+
+            return MethodHandles.lookup()
+                    .unreflectConstructor(ctor)
+                    .asType(mtdType);
+        } catch (ReflectiveOperationException | PrivilegedActionException e) {
+            throw new RuntimeException("Unable to set up byte buffer creation 
using reflection :" + e.getMessage(), e);
+        }
+    }
+
+
+    private static ByteBuffer wrapPointerJavaNio(long ptr, int len) {
+        try {
+            ByteBuffer buf = (ByteBuffer) 
DIRECT_BUF_MTD.invokeExact(JAVA_NIO_ACCESS_OBJ, ptr, len, NULL_OBJ);
+
+            assert buf.isDirect() : "ptr=" + ptr + ", len=" + len;
+
+            buf.order(NATIVE_BYTE_ORDER);
+
+            return buf;
+        } catch (Throwable e) {
+            throw new RuntimeException("JavaNioAccess#newDirectByteBuffer() 
method is unavailable."
+                    + FeatureChecker.JAVA_STARTUP_PARAMS_WARN, e);
+        }
+    }
+
+    /**
+     * Wraps a pointer to unmanaged memory into a direct byte buffer. Uses the 
constructor of the direct byte buffer.
+     *
+     * @param ptr         Pointer to wrap.

Review Comment:
   Please remove the alignments



##########
modules/core/src/main/java/org/apache/ignite/internal/util/PointerWrapping.java:
##########
@@ -17,19 +17,268 @@
 
 package org.apache.ignite.internal.util;
 
+import static org.apache.ignite.internal.util.GridUnsafe.NATIVE_BYTE_ORDER;
+import static org.apache.ignite.internal.util.GridUnsafe.UNSAFE;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 /**
  * Wraps a pointer to unmanaged memory into a direct byte buffer.
  */
-@SuppressWarnings("InterfaceMayBeAnnotatedFunctional")
-interface PointerWrapping {
+abstract class PointerWrapping {
+    /** Null object. */
+    private static final Object NULL_OBJ = null;
+
+    private static final MethodHandle DIRECT_BUF_MTD;
+    private static final MethodHandle DIRECT_BUF_CTOR_INT;
+    private static final MethodHandle DIRECT_BUF_CTOR_LONG;
+
+    private static final Object JAVA_NIO_ACCESS_OBJ;
+
+    static {
+        Object nioAccessObj = null;
+
+        MethodHandle directBufMtd = null;
+        MethodHandle directBufCtorWithIntLen = null;
+        MethodHandle directBufCtorWithLongLen = null;
+
+        try {
+            directBufCtorWithIntLen = 
createAndTestNewDirectBufferCtor(int.class);
+        } catch (Exception e) {
+            try {
+                directBufCtorWithLongLen = 
createAndTestNewDirectBufferCtor(long.class);
+            } catch (Exception e2) {
+                try {
+                    nioAccessObj = javaNioAccessObject();
+                    directBufMtd = newDirectBufferMethodHandle(nioAccessObj);
+                } catch (Exception exFallback) {
+                    //noinspection CallToPrintStackTrace
+                    exFallback.printStackTrace(); // NOPMD
+
+                    e.addSuppressed(exFallback);
+
+                    throw e; // Fallback to shared secrets failed.

Review Comment:
   This comment is weird, why do we need it?



-- 
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.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to