You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by "wenbingshen (via GitHub)" <gi...@apache.org> on 2023/03/02 08:22:36 UTC

[GitHub] [bookkeeper] wenbingshen commented on a diff in pull request #3824: Use JNI directly for posix_fadvise

wenbingshen commented on code in PR #3824:
URL: https://github.com/apache/bookkeeper/pull/3824#discussion_r1122739003


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/util/PageCacheUtil.java:
##########
@@ -18,41 +18,37 @@
 
 package org.apache.bookkeeper.util;
 
-import com.sun.jna.LastErrorException;
-import com.sun.jna.Native;
 import java.io.FileDescriptor;
 import java.lang.reflect.Field;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.common.util.nativeio.NativeIO;
+import org.apache.bookkeeper.common.util.nativeio.NativeIOImpl;
 
 /**
  * Native I/O operations.
  */
-public final class NativeIO {
-    private static final Logger LOG = LoggerFactory.getLogger(NativeIO.class);
+@UtilityClass
+@Slf4j
+public final class PageCacheUtil {
 
     private static final int POSIX_FADV_DONTNEED = 4; /* fadvise.h */
 
-    private static boolean initialized = false;
     private static boolean fadvisePossible = true;
 
+    private static final NativeIO NATIVE_IO;
+
     static {
+        NativeIO nativeIO = null;
         try {
-            Native.register("c");
-            initialized = true;
-        } catch (NoClassDefFoundError e) {
-            LOG.info("JNA not found. Native methods will be disabled.");
-        } catch (UnsatisfiedLinkError e) {
-            LOG.info("Unable to link C library. Native methods will be disabled.");
-        } catch (NoSuchMethodError e) {
-            LOG.warn("Obsolete version of JNA present; unable to register C library");
+            nativeIO = new NativeIOImpl();

Review Comment:
   @horizonzy I also found this when I run unit tests on mac os.
   
   We don't need to check whether nativeIO.posix_fdavise is valid, because in use, once an error occurs, we will close posix_fdavise, so we can just check whether the library is normal, this is the error I encountered.
   
   ```java
       public static void bestEffortRemoveFromPageCache(int fd, long offset, long len) {
           if (!fadvisePossible || fd < 0) {
               return;
           }
           try {
               NATIVE_IO.posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
           } catch (Exception e) {
               log.warn("Failed to perform posix_fadvise: {}", e.getMessage());
               fadvisePossible = false;
           }
       }
   ```



-- 
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: commits-unsubscribe@bookkeeper.apache.org

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