Skip to content

Commit

Permalink
fix: eventHub notify after shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxxoo committed Mar 31, 2022
1 parent f621ea7 commit 2eb6c9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
21 changes: 17 additions & 4 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
Expand All @@ -43,20 +44,24 @@ public class HugeFactory {

private static final Logger LOG = Log.logger(HugeGraph.class);

private static final Thread shutdownHook = new Thread(() -> {
LOG.info("HugeGraph is shutting down");
HugeFactory.shutdown(30L);
}, "hugegraph-shutdown");

static {
SerialEnum.registerInternalEnums();
HugeGraph.registerTraversalStrategies(StandardHugeGraph.class);

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("HugeGraph is shutting down");
HugeFactory.shutdown(30L);
}, "hugegraph-shutdown"));
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

private static final String NAME_REGEX = "^[A-Za-z][A-Za-z0-9_]{0,47}$";

private static final Map<String, HugeGraph> graphs = new HashMap<>();

private static final AtomicBoolean shutdown = new AtomicBoolean(false);

public static synchronized HugeGraph open(Configuration config) {
HugeConfig conf = config instanceof HugeConfig ?
(HugeConfig) config : new HugeConfig(config);
Expand Down Expand Up @@ -138,6 +143,9 @@ public static PropertiesConfiguration getRemoteConfig(URL url) {
* @param timeout seconds
*/
public static void shutdown(long timeout) {
if (!shutdown.compareAndSet(false, true)) {
return;
}
try {
if (!EventHub.destroy(timeout)) {
throw new TimeoutException(timeout + "s");
Expand All @@ -146,7 +154,12 @@ public static void shutdown(long timeout) {
OltpTraverser.destroy();
} catch (Throwable e) {
LOG.error("Error while shutdown", e);
shutdown.compareAndSet(true, false);
throw new HugeException("Failed to shutdown", e);
}
}

public static void removeShutdownHook() {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,11 @@ public static void main(String[] args) throws Exception {
server.stop();
LOG.info("HugeGraphServer stopped");
}, "hugegraph-server-shutdown"));
/*
* HugeFactory shutdown hook be invoked before server stop, so that
* eventHub execute notify event on a shutdown executor, so here
* remove the HugeFactory shutdown hook.
*/
HugeFactory.removeShutdownHook();
}
}

0 comments on commit 2eb6c9b

Please sign in to comment.