Browse Source

HADOOP-12955. Fix bugs in the initialization of the ISA-L library JNI bindings (Kai Zheng via cmccabe)

Colin Patrick Mccabe 9 years ago
parent
commit
19639785f5

+ 4 - 4
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/NativeLibraryChecker.java

@@ -95,12 +95,12 @@ public class NativeLibraryChecker {
         snappyLibraryName = SnappyCodec.getLibraryName();
       }
 
-      try {
-        isalDetail = ErasureCodeNative.getLoadingFailureReason();
+      isalDetail = ErasureCodeNative.getLoadingFailureReason();
+      if (isalDetail != null) {
+        isalLoaded = false;
+      } else {
         isalDetail = ErasureCodeNative.getLibraryName();
         isalLoaded = true;
-      } catch (UnsatisfiedLinkError e) {
-        isalLoaded = false;
       }
 
       openSslDetail = OpensslCipher.getLoadingFailureReason();

+ 1 - 0
hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/erasurecode/erasure_coder.c

@@ -19,6 +19,7 @@
 #include "erasure_code.h"
 #include "gf_util.h"
 #include "erasure_coder.h"
+#include "dump.h"
 
 #include <stdio.h>
 #include <stdlib.h>

+ 21 - 25
hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/erasurecode/isal_load.c

@@ -78,6 +78,12 @@ static const char* load_functions() {
 
 void load_erasurecode_lib(char* err, size_t err_len) {
   const char* errMsg;
+  const char* library = NULL;
+#ifdef UNIX
+  Dl_info dl_info;
+#else
+  LPTSTR filename = NULL;
+#endif
 
   err[0] = '\0';
 
@@ -111,38 +117,28 @@ void load_erasurecode_lib(char* err, size_t err_len) {
   if (errMsg != NULL) {
     snprintf(err, err_len, "Loading functions from ISA-L failed: %s", errMsg);
   }
-}
 
-int build_support_erasurecode() {
-#ifdef HADOOP_ISAL_LIBRARY
-  return 1;
-#else
-  return 0;
-#endif
-}
-
-const char* get_library_name() {
 #ifdef UNIX
-  Dl_info dl_info;
-
-  if (isaLoader->ec_encode_data == NULL) {
-    return HADOOP_ISAL_LIBRARY;
-  }
-
   if(dladdr(isaLoader->ec_encode_data, &dl_info)) {
-    return dl_info.dli_fname;
+    library = dl_info.dli_fname;
   }
 #else
-  LPTSTR filename = NULL;
-
-  if (isaLoader->libec == NULL) {
-    return HADOOP_ISAL_LIBRARY;
-  }
-
   if (GetModuleFileName(isaLoader->libec, filename, 256) > 0) {
-    return filename;
+    library = filename;
   }
 #endif
 
-  return NULL;
+  if (library == NULL) {
+    library = HADOOP_ISAL_LIBRARY;
+  }
+
+  isaLoader->libname = strdup(library);
 }
+
+int build_support_erasurecode() {
+#ifdef HADOOP_ISAL_LIBRARY
+  return 1;
+#else
+  return 0;
+#endif
+}

+ 1 - 5
hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/erasurecode/isal_load.h

@@ -78,6 +78,7 @@ typedef void (__cdecl *__d_ec_encode_data_update)(int, int, int, int, unsigned c
 typedef struct __IsaLibLoader {
   // The loaded library handle
   void* libec;
+  char* libname;
 
   __d_gf_mul gf_mul;
   __d_gf_inv gf_inv;
@@ -133,11 +134,6 @@ static FARPROC WINAPI myDlsym(HMODULE handle, LPCSTR symbol) {
  */
 int build_support_erasurecode();
 
-/**
- * Get the library name possibly of full path.
- */
-const char* get_library_name();
-
 /**
  * Initialize and load erasure code library, returning error message if any.
  *

+ 7 - 4
hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/erasurecode/jni_erasure_code_native.c

@@ -22,6 +22,7 @@
 
 #include "org_apache_hadoop.h"
 #include "jni_common.h"
+#include "isal_load.h"
 #include "org_apache_hadoop_io_erasurecode_ErasureCodeNative.h"
 
 #ifdef UNIX
@@ -37,9 +38,11 @@ Java_org_apache_hadoop_io_erasurecode_ErasureCodeNative_loadLibrary
 JNIEXPORT jstring JNICALL
 Java_org_apache_hadoop_io_erasurecode_ErasureCodeNative_getLibraryName
 (JNIEnv *env, jclass myclass) {
-  char* libName = get_library_name();
-  if (libName == NULL) {
-    libName = "Unavailable";
+  if (isaLoader == NULL) {
+    THROW(env, "java/lang/UnsatisfiedLinkError",
+                             "Unavailable: library not loaded yet");
+    return (jstring)NULL;
   }
-  return (*env)->NewStringUTF(env, libName);
+
+  return (*env)->NewStringUTF(env, isaLoader->libname);
 }