Skip to content

Commit 39edbf3

Browse files
rhubneralanpaxton
authored andcommitted
Address PR comments.
1 parent 24597af commit 39edbf3

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

java/rocksjni/event_listener.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jlong Java_org_rocksdb_AbstractEventListener_createNewEventListener(
3434
env, jobj, enabled_event_callbacks));
3535
return GET_CPLUSPLUS_POINTER(sptr_event_listener);
3636
} catch (ROCKSDB_NAMESPACE::JniException&) {
37-
// We always throw JniException with Java Exception
37+
// JNIException indicates that a Java Exception has been thrown in the env
3838
return 0;
3939
}
4040
}

java/rocksjni/portal.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,14 @@ class StatusJni
489489
return construct(env, status, jclazz, mid);
490490
}
491491

492-
// StatusJni can't cache jclass because in descructor we need JniUtil,
493-
// JniUtil need RocksDBExceptionJni, and RocksDBExceptionJni need StatusJni.
494-
// So I can't even change the order of the classes in portal.h
492+
/**
493+
* This method is used in situations when we need to create
494+
* org.rocksdb.Status in C++ rocksDB callback. In situation when
495+
* Java code was loaded by a custom class loader,
496+
* env->FindClass will fail, because on the JVM attached thread we have only
497+
* system class loader. For more info :
498+
* https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#FindClass
499+
*/
495500
static jobject construct(JNIEnv* env, const Status& status, jclass jclazz,
496501
jmethodID mid) {
497502
// convert the Status state for Java
@@ -2513,7 +2518,7 @@ class JniUtil {
25132518
auto status = env->GetJavaVM(vm);
25142519
if (status != 0) {
25152520
ROCKSDB_NAMESPACE::JniException::ThrowNew(
2516-
env, "Unable to retrieve instance ov JavaVM");
2521+
env, "Unable to retrieve instance of JavaVM");
25172522
}
25182523
}
25192524
};

java/src/test/java/org/rocksdb/EventListenerClassloaderTest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,36 @@
1818
public class EventListenerClassloaderTest {
1919
@Test
2020
public void testCallback() throws Exception {
21-
try {
22-
this.getClass().getClassLoader().loadClass("org.rocksdb.RocksDB");
23-
fail("It looks like RocksDB is on classpath. This test must load RocksDB via custom "
24-
+ "classLoader"
25-
+ " to verify that callback cache all class instances.");
26-
} catch (ClassNotFoundException e) {
27-
;
28-
}
21+
assertThat(hasRocksDBonPath())
22+
.as("It looks like RocksDB is on classpath. "
23+
+ "This test must load RocksDB via custom "
24+
+ "classLoader to verify that callback cache all class instances.")
25+
.isFalse();
2926

3027
String jarPath = System.getProperty("rocks-jar");
31-
assertThat(jarPath).isNotNull().as("Java property 'rocks-jar' was not setup properly");
28+
assertThat(jarPath).as("Java property 'rocks-jar' was not setup properly").isNotNull();
3229

3330
Path classesDir = Paths.get(jarPath);
34-
ClassLoader cl = new URLClassLoader(new URL[] {classesDir.toAbsolutePath().toUri().toURL()});
31+
try (URLClassLoader cl =
32+
new URLClassLoader(new URL[] {classesDir.toAbsolutePath().toUri().toURL()})) {
33+
Class<?> rocksDBclazz = cl.loadClass("org.rocksdb.RocksDB");
34+
Method loadLibrary = rocksDBclazz.getMethod("loadLibrary");
35+
loadLibrary.invoke(null);
3536

36-
Class rocksDBclazz = cl.loadClass("org.rocksdb.RocksDB");
37-
Method loadLibrary = rocksDBclazz.getMethod("loadLibrary");
38-
loadLibrary.invoke(null);
37+
Class<?> testableEventListenerClazz = cl.loadClass("org.rocksdb.test.TestableEventListener");
38+
Method invokeAllCallbacksInThread =
39+
testableEventListenerClazz.getMethod("invokeAllCallbacksInThread");
40+
Object instance = testableEventListenerClazz.getDeclaredConstructor().newInstance();
41+
invokeAllCallbacksInThread.invoke(instance);
42+
}
43+
}
3944

40-
Class testableEventListenerClazz = cl.loadClass("org.rocksdb.test.TestableEventListener");
41-
Method invokeAllCallbacksInThread =
42-
testableEventListenerClazz.getMethod("invokeAllCallbacksInThread");
43-
Object instance = testableEventListenerClazz.getDeclaredConstructor().newInstance();
44-
invokeAllCallbacksInThread.invoke(instance);
45+
private boolean hasRocksDBonPath() {
46+
try {
47+
this.getClass().getClassLoader().loadClass("org.rocksdb.RocksDB");
48+
return true;
49+
} catch (ClassNotFoundException e) {
50+
return false;
51+
}
4552
}
4653
}

0 commit comments

Comments
 (0)