From bf3c5c6064699ad4d0f2b572767459093c2b3c4f Mon Sep 17 00:00:00 2001 From: liningrui Date: Tue, 4 Jun 2019 19:49:12 +0800 Subject: [PATCH 1/2] Shutdown RestServer and GremlinServer when stop Change-Id: Iae5a6506dfaa0eeb6698f42f8bb3ef7f0ee73321 --- .../baidu/hugegraph/config/ServerOptions.java | 18 +++++ .../baidu/hugegraph/server/RestServer.java | 21 ++++-- .../java/com/baidu/hugegraph/HugeGraph.java | 2 + .../baidu/hugegraph/dist/HugeGraphServer.java | 70 +++++++++++++------ 4 files changed, 87 insertions(+), 24 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java index 2753477343..f78e96c6ca 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/config/ServerOptions.java @@ -66,6 +66,24 @@ public static synchronized ServerOptions instance() { 256 ); + public static final ConfigOption CONN_IDLE_TIMEOUT = + new ConfigOption<>( + "restserver.connection_idle_timeout", + "The time in seconds to keep an inactive connection " + + "alive, -1 means no timeout.", + rangeInt(-1, Integer.MAX_VALUE), + 30 + ); + + public static final ConfigOption CONN_MAX_REQUESTS = + new ConfigOption<>( + "restserver.connection_max_requests", + "The max number of HTTP requests allowed to be processed " + + "on one keep-alive connection, -1 means unlimited.", + rangeInt(-1, Integer.MAX_VALUE), + 256 + ); + public static final ConfigOption GREMLIN_SERVER_URL = new ConfigOption<>( "gremlinserver.url", diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java index 4b3570bd60..0ad8ec0838 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java @@ -23,6 +23,7 @@ import java.net.URI; import java.util.Arrays; import java.util.Collection; +import java.util.concurrent.Future; import javax.ws.rs.core.UriBuilder; @@ -68,14 +69,26 @@ private HttpServer configHttpServer(URI uri, ResourceConfig rc) { Collection listeners = server.getListeners(); E.checkState(listeners.size() > 0, "Http Server should have some listeners, but now is none"); + NetworkListener listener = listeners.iterator().next(); + // Option max_worker_threads int maxWorkerThreads = this.conf.get(ServerOptions.MAX_WORKER_THREADS); - listeners.iterator().next().getTransport() - .getWorkerThreadPoolConfig() - .setCorePoolSize(maxWorkerThreads) - .setMaxPoolSize(maxWorkerThreads); + listener.getTransport() + .getWorkerThreadPoolConfig() + .setCorePoolSize(maxWorkerThreads) + .setMaxPoolSize(maxWorkerThreads); + // Option keep_alive + int idleTimeout = this.conf.get(ServerOptions.CONN_IDLE_TIMEOUT); + int maxRequests = this.conf.get(ServerOptions.CONN_MAX_REQUESTS); + listener.getKeepAlive().setMaxRequestsCount(maxRequests); + listener.getKeepAlive().setIdleTimeoutInSeconds(idleTimeout); return server; } + public Future shutdown() { + E.checkNotNull(this.httpServer, "http server"); + return this.httpServer.shutdown(); + } + public void shutdownNow() { E.checkNotNull(this.httpServer, "http server"); this.httpServer.shutdownNow(); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index cf531c0b9e..0115ddbd55 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -92,7 +92,9 @@ public class HugeGraph implements GremlinGraph { strategies); Runtime.getRuntime().addShutdownHook(new Thread(() -> { + LOG.info("HugeGraph stopping"); HugeGraph.shutdown(30L); + LOG.info("HugeGraph stopped"); })); } 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 31f8d6b57b..1d67b25b60 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 @@ -24,32 +24,22 @@ import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.util.Log; public class HugeGraphServer { private static final Logger LOG = Log.logger(HugeGraphServer.class); - static { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - LOG.info("HugeGraphServer stopped"); - })); - } + private final GremlinServer gremlinServer; + private final RestServer restServer; - public static void main(String[] args) throws Exception { - if (args.length != 2) { - String msg = "HugeGraphServer can only accept two config files"; - LOG.error(msg); - throw new HugeException(msg); - } - - HugeRestServer.register(); - - GremlinServer gremlinServer = null; + public HugeGraphServer(String gremlinServerConf, String restServerConf) + throws Exception { try { // Start GremlinServer - gremlinServer = HugeGremlinServer.start(args[0]); - } catch (Exception e) { + this.gremlinServer = HugeGremlinServer.start(gremlinServerConf); + } catch (Throwable e) { LOG.error("HugeGremlinServer start error: ", e); HugeGraph.shutdown(30L); throw e; @@ -57,12 +47,52 @@ public static void main(String[] args) throws Exception { try { // Start HugeRestServer - HugeRestServer.start(args[1]); - } catch (Exception e) { + this.restServer = HugeRestServer.start(restServerConf); + } catch (Throwable e) { LOG.error("HugeRestServer start error: ", e); - gremlinServer.stop(); + this.gremlinServer.stop(); HugeGraph.shutdown(30L); throw e; } } + + public void stop() { + try { + this.restServer.shutdown().get(); + LOG.info("HugeRestServer stopped"); + } catch (Throwable e) { + LOG.error("HugeRestServer stop error: ", e); + } + + try { + this.gremlinServer.stop().get(); + LOG.info("HugeGremlinServer stopped"); + } catch (Throwable e) { + LOG.error("HugeGremlinServer stop error: ", e); + } + + try { + HugeGraph.shutdown(30L); + LOG.info("HugeGraph stopped"); + } catch (Throwable e) { + LOG.error("Failed to stop HugeGraph: ", e); + } + } + + public static void main(String[] args) throws Exception { + if (args.length != 2) { + String msg = "HugeGraphServer can only accept two config files"; + LOG.error(msg); + throw new HugeException(msg); + } + + HugeRestServer.register(); + HugeGraphServer server = new HugeGraphServer(args[0], args[1]); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + LOG.info("HugeGraphServer stopping"); + server.stop(); + LOG.info("HugeGraphServer stopped"); + })); + } } From a6463ea4b5e5d72e59b8e4ed6115b1513ea7520f Mon Sep 17 00:00:00 2001 From: liningrui Date: Wed, 12 Jun 2019 15:36:19 +0800 Subject: [PATCH 2/2] tiny improve Change-Id: Iae620fd4bbc26008171bdc92c4a90225b469cd6a --- .../src/main/java/com/baidu/hugegraph/server/RestServer.java | 2 +- .../src/main/java/com/baidu/hugegraph/HugeGraph.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java index 0ad8ec0838..e14c3515bd 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/server/RestServer.java @@ -79,8 +79,8 @@ private HttpServer configHttpServer(URI uri, ResourceConfig rc) { // Option keep_alive int idleTimeout = this.conf.get(ServerOptions.CONN_IDLE_TIMEOUT); int maxRequests = this.conf.get(ServerOptions.CONN_MAX_REQUESTS); - listener.getKeepAlive().setMaxRequestsCount(maxRequests); listener.getKeepAlive().setIdleTimeoutInSeconds(idleTimeout); + listener.getKeepAlive().setMaxRequestsCount(maxRequests); return server; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java index 0115ddbd55..3f00f4daf9 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/HugeGraph.java @@ -92,9 +92,8 @@ public class HugeGraph implements GremlinGraph { strategies); Runtime.getRuntime().addShutdownHook(new Thread(() -> { - LOG.info("HugeGraph stopping"); + LOG.info("HugeGraph is shutting down"); HugeGraph.shutdown(30L); - LOG.info("HugeGraph stopped"); })); }