[RocksJava] Fix native library loader
Summary: Prior to this the native library loader instance didn`t care about a state. So if library loading was called multiple times, multiple copies of the shared object were put into the tmp folder and loaded into the JVM. This changed within this commit to the following behavior: - library loading is now synchronized - library is loaded within the first call - if loading was successful the library loaded sets a flag - every subsequent call checks for a boolean flag indicating if there was already a successful attempt Test Plan: - Execute example and watch tmp folder while the example is running - After this patch only one shared object will be in the tmp folder Usual tests: - make rocksdbjava jtest - mvn -f rocksjni.pom package Reviewers: adamretter, ankgup87, yhchiang Subscribers: dhruba Differential Revision: https://reviews.facebook.net/D32133
This commit is contained in:
parent
26b50783d3
commit
e61f38e5a0
@ -13,6 +13,7 @@ import org.rocksdb.util.Environment;
|
|||||||
public class NativeLibraryLoader {
|
public class NativeLibraryLoader {
|
||||||
//singleton
|
//singleton
|
||||||
private static final NativeLibraryLoader instance = new NativeLibraryLoader();
|
private static final NativeLibraryLoader instance = new NativeLibraryLoader();
|
||||||
|
private static boolean initialized = false;
|
||||||
|
|
||||||
private static final String sharedLibraryName = Environment.getJniLibraryName("rocksdb");
|
private static final String sharedLibraryName = Environment.getJniLibraryName("rocksdb");
|
||||||
private static final String tempFilePrefix = "librocksdbjni";
|
private static final String tempFilePrefix = "librocksdbjni";
|
||||||
@ -41,10 +42,11 @@ public class NativeLibraryLoader {
|
|||||||
*
|
*
|
||||||
* @throws java.io.IOException if a filesystem operation fails.
|
* @throws java.io.IOException if a filesystem operation fails.
|
||||||
*/
|
*/
|
||||||
public void loadLibraryFromJar(final String tmpDir)
|
public synchronized void loadLibraryFromJar(final String tmpDir)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
if (!initialized) {
|
||||||
final File temp;
|
final File temp;
|
||||||
if(tmpDir == null || tmpDir.equals("")) {
|
if (tmpDir == null || tmpDir.equals("")) {
|
||||||
temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
|
temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
|
||||||
} else {
|
} else {
|
||||||
temp = new File(tmpDir, sharedLibraryName);
|
temp = new File(tmpDir, sharedLibraryName);
|
||||||
@ -57,7 +59,8 @@ public class NativeLibraryLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// attempt to copy the library from the Jar file to the temp destination
|
// attempt to copy the library from the Jar file to the temp destination
|
||||||
try(final InputStream is = getClass().getClassLoader().getResourceAsStream(sharedLibraryName)) {
|
try (final InputStream is = getClass().getClassLoader().
|
||||||
|
getResourceAsStream(sharedLibraryName)) {
|
||||||
if (is == null) {
|
if (is == null) {
|
||||||
throw new RuntimeException(sharedLibraryName + " was not found inside JAR.");
|
throw new RuntimeException(sharedLibraryName + " was not found inside JAR.");
|
||||||
} else {
|
} else {
|
||||||
@ -66,6 +69,8 @@ public class NativeLibraryLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
System.load(temp.getAbsolutePath());
|
System.load(temp.getAbsolutePath());
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Private constructor to disallow instantiation
|
* Private constructor to disallow instantiation
|
||||||
|
Loading…
Reference in New Issue
Block a user