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

Fix close exception and server-info EXPIRED_INTERVAL #1804

Merged
merged 5 commits into from
Apr 7, 2022
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 @@ -53,6 +53,10 @@ public class ContextGremlinServer extends GremlinServer {

private final EventHub eventHub;

static {
HugeGraphAuthProxy.setContext(Context.admin());
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
}

public ContextGremlinServer(final Settings settings, EventHub eventHub) {
/*
* pass custom Executor https://github.com/apache/tinkerpop/pull/813
Expand Down Expand Up @@ -94,8 +98,6 @@ public synchronized CompletableFuture<Void> stop() {
}

public void injectAuthGraph() {
HugeGraphAuthProxy.setContext(Context.admin());

GraphManager manager = this.getServerGremlinExecutor()
.getGraphManager();
for (String name : manager.getGraphNames()) {
Expand Down
23 changes: 19 additions & 4 deletions hugegraph-core/src/main/java/com/baidu/hugegraph/HugeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, HugeGraph> 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);
Expand Down Expand Up @@ -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;
}
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved
try {
if (!EventHub.destroy(timeout)) {
throw new TimeoutException(timeout + "s");
Expand All @@ -146,7 +154,14 @@ 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);
}

LOG.info("HugeFactory shutdown");
}

public static void removeShutdownHook() {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -117,10 +119,21 @@ 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();
zyxxoo marked this conversation as resolved.
Show resolved Hide resolved

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"));
serverStopped.get();
}
}