Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown RestServer and GremlinServer when stop #554

Merged
merged 2 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ public static synchronized ServerOptions instance() {
256
);

public static final ConfigOption<Integer> 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<Integer> 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<String> GREMLIN_SERVER_URL =
new ConfigOption<>(
"gremlinserver.url",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -68,14 +69,26 @@ private HttpServer configHttpServer(URI uri, ResourceConfig rc) {
Collection<NetworkListener> 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().setIdleTimeoutInSeconds(idleTimeout);
listener.getKeepAlive().setMaxRequestsCount(maxRequests);
return server;
}

public Future<HttpServer> shutdown() {
E.checkNotNull(this.httpServer, "http server");
return this.httpServer.shutdown();
}

public void shutdownNow() {
E.checkNotNull(this.httpServer, "http server");
this.httpServer.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class HugeGraph implements GremlinGraph {
strategies);

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("HugeGraph is shutting down");
HugeGraph.shutdown(30L);
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,75 @@

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;
}

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");
}));
}
}