Skip to content

Commit

Permalink
Fix: config(rest-server.properties) error but HugeGraphServer still up
Browse files Browse the repository at this point in the history
Fix #110

Change-Id: Ib5da6e6c71f2fb6d523b13b75e803b257f0b7035
  • Loading branch information
Linary committed Nov 9, 2018
1 parent 1d0e128 commit 2de3976
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<HugeGraphPlugin> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -85,5 +86,7 @@ private static void startWithInjectTraversal(String conf)
server.stop().join();
throw new HugeException("Failed to start Gremlin Server");
}).join();

return server;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static void main(String[] args) throws Exception {
}

try {
RegisterUtil.registerBackends();
// Start HugeRestServer
start(args[0]);
} catch (Exception e) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@

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;
import com.baidu.hugegraph.backend.store.BackendProviderFactory;
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());
Expand Down Expand Up @@ -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<HugeGraphPlugin> 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);
}
}
}
}

0 comments on commit 2de3976

Please sign in to comment.