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 03a5937276..31f8d6b57b 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 @@ -19,16 +19,12 @@ package com.baidu.hugegraph.dist; -import java.util.ServiceLoader; - +import org.apache.tinkerpop.gremlin.server.GremlinServer; import org.slf4j.Logger; import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.HugeGraph; -import com.baidu.hugegraph.plugin.HugeGraphPlugin; import com.baidu.hugegraph.util.Log; -import com.baidu.hugegraph.util.VersionUtil; -import com.baidu.hugegraph.version.CoreVersion; public class HugeGraphServer { @@ -41,52 +37,32 @@ public class HugeGraphServer { } 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; try { - RegisterUtil.registerBackends(); - // Scan the jars in plugins directory and load them - registerPlugins(); // Start GremlinServer - HugeGremlinServer.start(args[0]); - // Start HugeRestServer - HugeRestServer.start(args[1]); + gremlinServer = HugeGremlinServer.start(args[0]); } catch (Exception e) { - LOG.error("HugeGraphServer error:", e); + LOG.error("HugeGremlinServer start error: ", e); HugeGraph.shutdown(30L); throw e; } - } - - private static void registerPlugins() { - ServiceLoader plugins = ServiceLoader.load( - HugeGraphPlugin.class); - for (HugeGraphPlugin plugin : plugins) { - LOG.info("Loading plugin {}({})", - plugin.name(), plugin.getClass().getCanonicalName()); - String minVersion = plugin.supportsMinVersion(); - String maxVersion = plugin.supportsMaxVersion(); - if (!VersionUtil.match(CoreVersion.VERSION, minVersion, - maxVersion)) { - LOG.warn("Skip loading plugin '{}' due to the version range " + - "'[{}, {})' that it's supported doesn't cover " + - "current core version '{}'", plugin.name(), - minVersion, maxVersion, CoreVersion.VERSION.get()); - continue; - } - try { - plugin.register(); - LOG.info("Loaded plugin '{}'", plugin.name()); - } catch (Exception e) { - throw new HugeException("Failed to load plugin '%s'", - plugin.name(), e); - } + try { + // Start HugeRestServer + HugeRestServer.start(args[1]); + } catch (Exception e) { + LOG.error("HugeRestServer start error: ", e); + gremlinServer.stop(); + HugeGraph.shutdown(30L); + throw e; } } } diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGremlinServer.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGremlinServer.java index dbfecf8be6..c72447e6a9 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGremlinServer.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeGremlinServer.java @@ -34,43 +34,44 @@ public class HugeGremlinServer { private static final String G_PREFIX = "__g_"; public static void main(String[] args) throws Exception { - if (args.length != 1) { String msg = "HugeGremlinServer can only accept one config files"; LOG.error(msg); throw new HugeException(msg); } + register(); + try { - RegisterUtil.registerBackends(); start(args[0]); } catch (Exception e) { LOG.error("HugeGremlinServer error:", e); throw e; } LOG.info("HugeGremlinServer started"); + } - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - LOG.info("HugeGremlinServer stopped"); - })); + public static void register() { + RegisterUtil.registerBackends(); + RegisterUtil.registerPlugins(); } - public static void start(String conf) throws Exception { + public static GremlinServer start(String conf) throws Exception { // Start GremlinServer with inject traversal source - startWithInjectTraversal(conf); + return startWithInjectTraversal(conf); } - private static void startWithInjectTraversal(String conf) - throws Exception { + private static ContextGremlinServer startWithInjectTraversal(String conf) + throws Exception { LOG.info(GremlinServer.getHeader()); final Settings settings; try { settings = Settings.read(conf); - } catch (Exception ex) { + } catch (Exception e) { LOG.error("Can't found the configuration file at {} or " + - "being parsed properly. [{}]", conf, ex.getMessage()); - return; + "being parsed properly. [{}]", conf, e.getMessage()); + throw e; } LOG.info("Configuring Gremlin Server from {}", conf); @@ -85,5 +86,7 @@ private static void startWithInjectTraversal(String conf) server.stop().join(); throw new HugeException("Failed to start Gremlin Server"); }).join(); + + return server; } } diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeRestServer.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeRestServer.java index 820a409061..83dd7d5a41 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeRestServer.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/HugeRestServer.java @@ -37,7 +37,6 @@ public static void main(String[] args) throws Exception { } try { - RegisterUtil.registerBackends(); // Start HugeRestServer start(args[0]); } catch (Exception e) { @@ -47,10 +46,15 @@ public static void main(String[] args) throws Exception { LOG.info("HugeRestServer stopped"); } - public static void start(String conf) throws Exception { + public static void register() { + RegisterUtil.registerBackends(); + RegisterUtil.registerPlugins(); + } + + public static RestServer start(String conf) throws Exception { RegisterUtil.registerServer(); // Start RestServer - RestServer.start(conf); + return RestServer.start(conf); } } diff --git a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/RegisterUtil.java b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/RegisterUtil.java index 4ad02d93cf..a93746b829 100644 --- a/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/RegisterUtil.java +++ b/hugegraph-dist/src/main/java/com/baidu/hugegraph/dist/RegisterUtil.java @@ -21,9 +21,11 @@ import java.io.InputStream; import java.util.List; +import java.util.ServiceLoader; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; +import org.slf4j.Logger; import com.baidu.hugegraph.HugeException; import com.baidu.hugegraph.backend.serializer.SerializerFactory; @@ -31,10 +33,16 @@ import com.baidu.hugegraph.config.CoreOptions; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.OptionSpace; +import com.baidu.hugegraph.plugin.HugeGraphPlugin; import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.Log; +import com.baidu.hugegraph.util.VersionUtil; +import com.baidu.hugegraph.version.CoreVersion; public class RegisterUtil { + private static final Logger LOG = Log.logger(RegisterUtil.class); + static { OptionSpace.register("core", CoreOptions.instance()); OptionSpace.register("dist", DistOptions.instance()); @@ -160,4 +168,34 @@ public static void registerPalo() { public static void registerServer() { OptionSpace.register("server", "com.baidu.hugegraph.config.ServerOptions"); } + + /** + * Scan the jars in plugins directory and load them + */ + public static void registerPlugins() { + ServiceLoader plugins = ServiceLoader.load( + HugeGraphPlugin.class); + for (HugeGraphPlugin plugin : plugins) { + LOG.info("Loading plugin {}({})", + plugin.name(), plugin.getClass().getCanonicalName()); + String minVersion = plugin.supportsMinVersion(); + String maxVersion = plugin.supportsMaxVersion(); + + if (!VersionUtil.match(CoreVersion.VERSION, minVersion, + maxVersion)) { + LOG.warn("Skip loading plugin '{}' due to the version range " + + "'[{}, {})' that it's supported doesn't cover " + + "current core version '{}'", plugin.name(), + minVersion, maxVersion, CoreVersion.VERSION.get()); + continue; + } + try { + plugin.register(); + LOG.info("Loaded plugin '{}'", plugin.name()); + } catch (Exception e) { + throw new HugeException("Failed to load plugin '%s'", + plugin.name(), e); + } + } + } }