From 2aedb93cf4b4bb19091a25d95c0b39a9796f98f6 Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Tue, 29 Mar 2022 17:22:35 +0800 Subject: [PATCH 1/5] fix: close exception --- .../java/com/baidu/hugegraph/auth/ContextGremlinServer.java | 4 ++++ .../main/java/com/baidu/hugegraph/task/HugeServerInfo.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java index b4f221d31d..9e319b4fe9 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java @@ -53,6 +53,10 @@ public class ContextGremlinServer extends GremlinServer { private final EventHub eventHub; + static { + HugeGraphAuthProxy.setContext(Context.admin()); + } + public ContextGremlinServer(final Settings settings, EventHub eventHub) { /* * pass custom Executor https://github.com/apache/tinkerpop/pull/813 diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeServerInfo.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeServerInfo.java index 340fff2245..f43bf9ec02 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeServerInfo.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeServerInfo.java @@ -51,7 +51,7 @@ public class HugeServerInfo { // Unit millisecond private static final long EXPIRED_INTERVAL = - TaskManager.SCHEDULE_PERIOD * 1000L * 3; + TaskManager.SCHEDULE_PERIOD * 10; private Id id; private NodeRole role; From f1414477bbd63945e5ef20327e0ae50ae063935e Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Wed, 30 Mar 2022 13:17:25 +0800 Subject: [PATCH 2/5] fix: eventHub notify after shutdown --- .../java/com/baidu/hugegraph/HugeFactory.java | 21 +++++++++++++++---- .../baidu/hugegraph/dist/HugeGraphServer.java | 6 ++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java index de73284598..5276c8a742 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java @@ -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; @@ -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 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); @@ -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"); @@ -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); + } } diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java index 8d5d5d5853..c7d06fd673 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java @@ -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(); } } From 5a851d72ba448fc18af2f05dc33d8e535c1e76bc Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Thu, 31 Mar 2022 16:23:59 +0800 Subject: [PATCH 3/5] improve code --- .../hugegraph/auth/ContextGremlinServer.java | 2 - .../baidu/hugegraph/core/GraphManager.java | 41 +++++++++++-------- .../java/com/baidu/hugegraph/HugeFactory.java | 2 + .../baidu/hugegraph/dist/HugeGraphServer.java | 17 +++++--- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java index 9e319b4fe9..3aaf7a409b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/ContextGremlinServer.java @@ -98,8 +98,6 @@ public synchronized CompletableFuture stop() { } public void injectAuthGraph() { - HugeGraphAuthProxy.setContext(Context.admin()); - GraphManager manager = this.getServerGremlinExecutor() .getGraphManager(); for (String name : manager.getGraphNames()) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index be36b1af4d..3e61929007 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -337,28 +337,33 @@ private void checkBackendVersionOrExit(HugeConfig config) { for (String graph : this.graphs()) { // TODO: close tx from main thread HugeGraph hugegraph = this.graph(graph); - if (!hugegraph.backendStoreFeatures().supportsPersistence()) { - hugegraph.initBackend(); - if (this.requireAuthentication()) { - String token = config.get(ServerOptions.AUTH_ADMIN_TOKEN); - try { - this.authenticator.initAdminUser(token); - } catch (Exception e) { - throw new BackendException( - "The backend store of '%s' can't " + - "initialize admin user", hugegraph.name()); + try { + if (!hugegraph.backendStoreFeatures().supportsPersistence()) { + hugegraph.initBackend(); + if (this.requireAuthentication()) { + String token = config.get( + ServerOptions.AUTH_ADMIN_TOKEN); + try { + this.authenticator.initAdminUser(token); + } catch (Exception e) { + throw new BackendException( + "The backend store of '%s' can't " + + "initialize admin user", hugegraph.name()); + } } } - } - BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo(); - if (!info.exists()) { - throw new BackendException( + BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo(); + if (!info.exists()) { + throw new BackendException( "The backend store of '%s' has not been initialized", hugegraph.name()); - } - if (!info.checkVersion()) { - throw new BackendException( - "The backend store version is inconsistent"); + } + if (!info.checkVersion()) { + throw new BackendException( + "The backend store version is inconsistent"); + } + } finally { + hugegraph.tx().close(); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java index 5276c8a742..56ee22e84b 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java @@ -157,6 +157,8 @@ public static void shutdown(long timeout) { shutdown.compareAndSet(true, false); throw new HugeException("Failed to shutdown", e); } + + LOG.info("HugeFactory shutdown"); } public static void removeShutdownHook() { diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java index c7d06fd673..77c5854774 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java @@ -31,6 +31,8 @@ import com.baidu.hugegraph.util.ConfigUtil; import com.baidu.hugegraph.util.Log; +import java.util.concurrent.CompletableFuture; + public class HugeGraphServer { private static final Logger LOG = Log.logger(HugeGraphServer.class); @@ -117,16 +119,19 @@ public static void main(String[] args) throws Exception { HugeGraphServer.register(); HugeGraphServer server = new HugeGraphServer(args[0], args[1]); + /* + * HugeFactory.shutdown hook may be invoked before server stop, + * causes event-hub can't execute notification events for another + * shutdown executor such as gremling-stop-shutdown + */ + HugeFactory.removeShutdownHook(); + CompletableFuture serverStopped = new CompletableFuture<>(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { LOG.info("HugeGraphServer stopping"); server.stop(); LOG.info("HugeGraphServer stopped"); + serverStopped.complete(null); }, "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(); + serverStopped.get(); } } From daec7fd7bb7ff344a35350ce7e8afacf4a738a1a Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Wed, 6 Apr 2022 21:41:30 +0800 Subject: [PATCH 4/5] revert close --- .../baidu/hugegraph/core/GraphManager.java | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 3e61929007..be36b1af4d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -337,33 +337,28 @@ private void checkBackendVersionOrExit(HugeConfig config) { for (String graph : this.graphs()) { // TODO: close tx from main thread HugeGraph hugegraph = this.graph(graph); - try { - if (!hugegraph.backendStoreFeatures().supportsPersistence()) { - hugegraph.initBackend(); - if (this.requireAuthentication()) { - String token = config.get( - ServerOptions.AUTH_ADMIN_TOKEN); - try { - this.authenticator.initAdminUser(token); - } catch (Exception e) { - throw new BackendException( - "The backend store of '%s' can't " + - "initialize admin user", hugegraph.name()); - } + if (!hugegraph.backendStoreFeatures().supportsPersistence()) { + hugegraph.initBackend(); + if (this.requireAuthentication()) { + String token = config.get(ServerOptions.AUTH_ADMIN_TOKEN); + try { + this.authenticator.initAdminUser(token); + } catch (Exception e) { + throw new BackendException( + "The backend store of '%s' can't " + + "initialize admin user", hugegraph.name()); } } - BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo(); - if (!info.exists()) { - throw new BackendException( + } + BackendStoreSystemInfo info = hugegraph.backendStoreSystemInfo(); + if (!info.exists()) { + throw new BackendException( "The backend store of '%s' has not been initialized", hugegraph.name()); - } - if (!info.checkVersion()) { - throw new BackendException( - "The backend store version is inconsistent"); - } - } finally { - hugegraph.tx().close(); + } + if (!info.checkVersion()) { + throw new BackendException( + "The backend store version is inconsistent"); } } } From 5209065efbc8a585425b3e361d9265d0b3af217a Mon Sep 17 00:00:00 2001 From: zyxxoo <1318247699@qq.com> Date: Wed, 6 Apr 2022 21:44:33 +0800 Subject: [PATCH 5/5] improve code --- .../src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java index 77c5854774..476e5420ea 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGraphServer.java @@ -119,12 +119,14 @@ public static void main(String[] args) throws Exception { HugeGraphServer.register(); HugeGraphServer server = new HugeGraphServer(args[0], args[1]); + /* * HugeFactory.shutdown hook may be invoked before server stop, * causes event-hub can't execute notification events for another * shutdown executor such as gremling-stop-shutdown */ HugeFactory.removeShutdownHook(); + CompletableFuture serverStopped = new CompletableFuture<>(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { LOG.info("HugeGraphServer stopping");