You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2014/02/13 22:58:05 UTC

git commit: Require JNA by default patch by Clément Lardeur; reviewed by dbrosius and Josh McKenzie for CASSANDRA-6575

Updated Branches:
  refs/heads/trunk de01d07a0 -> e9f8fc716


Require JNA by default
patch by Clément Lardeur; reviewed by dbrosius and Josh McKenzie for CASSANDRA-6575


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

Branch: refs/heads/trunk
Commit: e9f8fc7164aac0c7a746b74911afda9e0f02dbd8
Parents: de01d07
Author: Jonathan Ellis <jb...@apache.org>
Authored: Thu Feb 13 15:56:55 2014 -0600
Committer: Jonathan Ellis <jb...@apache.org>
Committed: Thu Feb 13 15:56:55 2014 -0600

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../cassandra/service/CassandraDaemon.java      | 14 ++++
 .../org/apache/cassandra/utils/CLibrary.java    | 68 ++++++++++++--------
 3 files changed, 58 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/e9f8fc71/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 0af5555..dc9cff5 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.1
+ * Require JNA by default (CASSANDRA-6575)
  * add listsnapshots command to nodetool (CASSANDRA-5742)
  * Introduce AtomicBTreeColumns (CASSANDRA-6271)
  * Multithreaded commitlog (CASSANDRA-3578)
@@ -31,6 +32,7 @@
  * Avoid repairing already repaired data (CASSANDRA-5351)
  * Reject counter updates with USING TTL/TIMESTAMP (CASSANDRA-6649)
 
+
 2.0.6
  * Add compatibility for Hadoop 0.2.x (CASSANDRA-5201)
  * Fix EstimatedHistogram races (CASSANDRA-6682)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/e9f8fc71/src/java/org/apache/cassandra/service/CassandraDaemon.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/service/CassandraDaemon.java b/src/java/org/apache/cassandra/service/CassandraDaemon.java
index b69ac10..732f962 100644
--- a/src/java/org/apache/cassandra/service/CassandraDaemon.java
+++ b/src/java/org/apache/cassandra/service/CassandraDaemon.java
@@ -141,6 +141,20 @@ public class CassandraDaemon
         for(MemoryPoolMXBean pool: ManagementFactory.getMemoryPoolMXBeans())
             logger.info("{} {}: {}", pool.getName(), pool.getType(), pool.getPeakUsage());
         logger.info("Classpath: {}", System.getProperty("java.class.path"));
+
+        // Fail-fast if JNA is not available or failing to initialize properly
+        // except with -Dcassandra.boot_without_jna=true. See CASSANDRA-6575.
+        if (!CLibrary.jnaAvailable())
+        {
+            boolean jnaRequired = !Boolean.getBoolean("cassandra.boot_without_jna");
+
+            if (jnaRequired)
+            {
+                logger.error("JNA failing to initialize properly. Use -Dcassandra.boot_without_jna=true to bootstrap even so.");
+                System.exit(3);
+            }
+        }
+
         CLibrary.tryMlockall();
 
         Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()

http://git-wip-us.apache.org/repos/asf/cassandra/blob/e9f8fc71/src/java/org/apache/cassandra/utils/CLibrary.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/CLibrary.java b/src/java/org/apache/cassandra/utils/CLibrary.java
index a7ff815..ac9f863 100644
--- a/src/java/org/apache/cassandra/utils/CLibrary.java
+++ b/src/java/org/apache/cassandra/utils/CLibrary.java
@@ -47,8 +47,8 @@ public final class CLibrary
     private static final int POSIX_FADV_WILLNEED   = 3; /* fadvise.h */
     private static final int POSIX_FADV_DONTNEED   = 4; /* fadvise.h */
     private static final int POSIX_FADV_NOREUSE    = 5; /* fadvise.h */
-    
-    static boolean jnaAvailable = false;
+
+    static boolean jnaAvailable = true;
     static boolean jnaLockable = false;
 
     static
@@ -56,35 +56,31 @@ public final class CLibrary
         try
         {
             Native.register("c");
-            jnaAvailable = true;
         }
         catch (NoClassDefFoundError e)
         {
-            logger.info("JNA not found. Native methods will be disabled.");
+            logger.warn("JNA not found. Native methods will be disabled.");
+            jnaAvailable = false;
         }
         catch (UnsatisfiedLinkError e)
         {
-            logger.info("JNA link failure, one or more native method will be unavailable.");
+            logger.warn("JNA link failure, one or more native method will be unavailable.");
             logger.debug("JNA link failure details: {}", e.getMessage());
         }
         catch (NoSuchMethodError e)
         {
             logger.warn("Obsolete version of JNA present; unable to register C library. Upgrade to JNA 3.2.7 or later");
+            jnaAvailable = false;
         }
     }
 
     private static native int mlockall(int flags) throws LastErrorException;
     private static native int munlockall() throws LastErrorException;
-
-    // fcntl - manipulate file descriptor, `man 2 fcntl`
-    public static native int fcntl(int fd, int command, long flags) throws LastErrorException;
-
-    // fadvice
-    public static native int posix_fadvise(int fd, long offset, int len, int flag) throws LastErrorException;
-
-    public static native int open(String path, int flags) throws LastErrorException;
-    public static native int fsync(int fd) throws LastErrorException;
-    public static native int close(int fd) throws LastErrorException;
+    private static native int fcntl(int fd, int command, long flags) throws LastErrorException;
+    private static native int posix_fadvise(int fd, long offset, int len, int flag) throws LastErrorException;
+    private static native int open(String path, int flags) throws LastErrorException;
+    private static native int fsync(int fd) throws LastErrorException;
+    private static native int close(int fd) throws LastErrorException;
 
     private static int errno(RuntimeException e)
     {
@@ -101,12 +97,12 @@ public final class CLibrary
     }
 
     private CLibrary() {}
-    
+
     public static boolean jnaAvailable()
     {
         return jnaAvailable;
     }
-    
+
     public static boolean jnaMemoryLockable()
     {
         return jnaLockable;
@@ -128,11 +124,12 @@ public final class CLibrary
         {
             if (!(e instanceof LastErrorException))
                 throw e;
+
             if (errno(e) == ENOMEM && System.getProperty("os.name").toLowerCase().contains("linux"))
             {
                 logger.warn("Unable to lock JVM memory (ENOMEM)."
-                             + " This can result in part of the JVM being swapped out, especially with mmapped I/O enabled."
-                             + " Increase RLIMIT_MEMLOCK or run Cassandra as root.");
+                        + " This can result in part of the JVM being swapped out, especially with mmapped I/O enabled."
+                        + " Increase RLIMIT_MEMLOCK or run Cassandra as root.");
             }
             else if (!System.getProperty("os.name").toLowerCase().contains("mac"))
             {
@@ -159,6 +156,13 @@ public final class CLibrary
             // if JNA is unavailable just skipping Direct I/O
             // instance of this class will act like normal RandomAccessFile
         }
+        catch (RuntimeException e)
+        {
+            if (!(e instanceof LastErrorException))
+                throw e;
+
+            logger.warn(String.format("posix_fadvise(%d, %d) failed, errno (%d).", fd, offset, errno(e)));
+        }
     }
 
     public static int tryFcntl(int fd, int command, int flags)
@@ -168,15 +172,18 @@ public final class CLibrary
 
         try
         {
-            result = CLibrary.fcntl(fd, command, flags);
+            result = fcntl(fd, command, flags);
+        }
+        catch (UnsatisfiedLinkError e)
+        {
+            // if JNA is unavailable just skipping
         }
         catch (RuntimeException e)
         {
             if (!(e instanceof LastErrorException))
                 throw e;
 
-            logger.warn(String.format("fcntl(%d, %d, %d) failed, errno (%d).",
-                                      fd, command, flags, CLibrary.errno(e)));
+            logger.warn(String.format("fcntl(%d, %d, %d) failed, errno (%d).", fd, command, flags, errno(e)));
         }
 
         return result;
@@ -199,7 +206,7 @@ public final class CLibrary
             if (!(e instanceof LastErrorException))
                 throw e;
 
-            logger.warn(String.format("open(%s, O_RDONLY) failed, errno (%d).", path, CLibrary.errno(e)));
+            logger.warn(String.format("open(%s, O_RDONLY) failed, errno (%d).", path, errno(e)));
         }
 
         return fd;
@@ -223,7 +230,7 @@ public final class CLibrary
             if (!(e instanceof LastErrorException))
                 throw e;
 
-            logger.warn(String.format("fsync(%d) failed, errno (%d).", fd, CLibrary.errno(e)));
+            logger.warn(String.format("fsync(%d) failed, errno (%d).", fd, errno(e)));
         }
     }
 
@@ -245,7 +252,7 @@ public final class CLibrary
             if (!(e instanceof LastErrorException))
                 throw e;
 
-            logger.warn(String.format("close(%d) failed, errno (%d).", fd, CLibrary.errno(e)));
+            logger.warn(String.format("close(%d) failed, errno (%d).", fd, errno(e)));
         }
     }
 
@@ -290,7 +297,16 @@ public final class CLibrary
         }
         catch (UnsatisfiedLinkError e)
         {
-            return -1;
+            // JNA is unavailable just skipping
+        }
+        catch (RuntimeException e)
+        {
+            if (!(e instanceof LastErrorException))
+                throw e;
+
+            logger.warn(String.format("posix_fadvise(%d, %d) failed, errno (%d).", fd, position, errno(e)));
         }
+
+        return -1;
     }
 }