Merge pull request #2635 from daniilguit/master

Java Binding: add option to not close network thread on shutdown hook
This commit is contained in:
A.J. Beamon 2020-02-15 10:43:18 -08:00 committed by GitHub
commit a3ba22b8a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 21 deletions

View File

@ -368,9 +368,11 @@ struct JVM {
{ { "send", "(JZ)V", reinterpret_cast<void*>(&promiseSend) } }); { { "send", "(JZ)V", reinterpret_cast<void*>(&promiseSend) } });
auto fdbClass = getClass("com/apple/foundationdb/FDB"); auto fdbClass = getClass("com/apple/foundationdb/FDB");
jmethodID selectMethod = jmethodID selectMethod =
env->GetStaticMethodID(fdbClass, "selectAPIVersion", "(IZ)Lcom/apple/foundationdb/FDB;"); env->GetStaticMethodID(fdbClass, "selectAPIVersion", "(I)Lcom/apple/foundationdb/FDB;");
checkException(); checkException();
env->CallStaticObjectMethod(fdbClass, selectMethod, jint(700), jboolean(false)); auto fdbInstance = env->CallStaticObjectMethod(fdbClass, selectMethod, jint(700));
checkException();
env->CallObjectMethod(fdbInstance, getMethod(fdbClass, "disableShutdownHook", "()V"));
checkException(); checkException();
} }

View File

@ -85,6 +85,8 @@ public class FDB {
private volatile boolean netStarted = false; private volatile boolean netStarted = false;
private volatile boolean netStopped = false; private volatile boolean netStopped = false;
volatile boolean warnOnUnclosed = true; volatile boolean warnOnUnclosed = true;
private boolean useShutdownHook = true;
private Thread shutdownHook;
private final Semaphore netRunning = new Semaphore(1); private final Semaphore netRunning = new Semaphore(1);
private final NetworkOptions options; private final NetworkOptions options;
@ -104,15 +106,8 @@ public class FDB {
* Called only once to create the FDB singleton. * Called only once to create the FDB singleton.
*/ */
private FDB(int apiVersion) { private FDB(int apiVersion) {
this(apiVersion, true);
}
private FDB(int apiVersion, boolean controlRuntime) {
this.apiVersion = apiVersion; this.apiVersion = apiVersion;
options = new NetworkOptions(this::Network_setOption); options = new NetworkOptions(this::Network_setOption);
if (controlRuntime) {
Runtime.getRuntime().addShutdownHook(new Thread(this::stopNetwork));
}
} }
/** /**
@ -177,13 +172,6 @@ public class FDB {
* @return the FoundationDB API object * @return the FoundationDB API object
*/ */
public static FDB selectAPIVersion(final int version) throws FDBException { public static FDB selectAPIVersion(final int version) throws FDBException {
return selectAPIVersion(version, true);
}
/**
This function is called from C++ if the VM is controlled directly from FDB
*/
private static synchronized FDB selectAPIVersion(final int version, boolean controlRuntime) throws FDBException {
if(singleton != null) { if(singleton != null) {
if(version != singleton.getAPIVersion()) { if(version != singleton.getAPIVersion()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -197,9 +185,26 @@ public class FDB {
throw new IllegalArgumentException("API version not supported (maximum 700)"); throw new IllegalArgumentException("API version not supported (maximum 700)");
Select_API_version(version); Select_API_version(version);
FDB fdb = new FDB(version, controlRuntime); singleton = new FDB(version);
return singleton = fdb; return singleton;
}
/**
* Disables shutdown hook that stops network thread upon process shutdown. This is useful if you need to run
* your own shutdown hook that uses the FDB instance and you need to avoid race conditions
* with the default shutdown hook. Replacement shutdown hook should stop the network thread manually
* by calling {@link #stopNetwork}.
*/
public synchronized void disableShutdownHook() {
useShutdownHook = false;
if(shutdownHook != null) {
// If this method was called after network thread started and shutdown hook was installed,
// remove this hook
Runtime.getRuntime().removeShutdownHook(shutdownHook);
// Release thread reference for GC
shutdownHook = null;
}
} }
/** /**
@ -405,6 +410,11 @@ public class FDB {
if(netStarted) { if(netStarted) {
return; return;
} }
if(useShutdownHook) {
// Register shutdown hook that stops network thread if user did not opt out
shutdownHook = new Thread(this::stopNetwork, "fdb-shutdown-hook");
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
Network_setup(); Network_setup();
netStarted = true; netStarted = true;