Skip to content

Commit

Permalink
improve server exit logic and upgrade version
Browse files Browse the repository at this point in the history
Change-Id: I277228b91dbb25381e6b49551944d32fd9edadc6
  • Loading branch information
Linary committed Sep 4, 2019
1 parent 1add465 commit a53503c
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 55 deletions.
2 changes: 1 addition & 1 deletion hugegraph-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import com.baidu.hugegraph.auth.HugeAuthenticator;
import com.baidu.hugegraph.auth.HugeFactoryAuthProxy;
import com.baidu.hugegraph.auth.HugeGraphAuthProxy;
import com.baidu.hugegraph.backend.BackendException;
import com.baidu.hugegraph.backend.cache.Cache;
import com.baidu.hugegraph.backend.cache.CacheManager;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.license.LicenseVerifier;
import com.baidu.hugegraph.metrics.MetricsUtil;
import com.baidu.hugegraph.metrics.ServerReporter;
import com.baidu.hugegraph.serializer.JsonSerializer;
Expand All @@ -70,6 +72,7 @@ public GraphManager(HugeConfig conf) {
}

this.loadGraphs(conf.getMap(ServerOptions.GRAPHS));
this.installLicense(conf);
this.checkBackendVersionOrExit();
this.restoreUncompletedTasks();
this.addMetrics(conf);
Expand Down Expand Up @@ -135,6 +138,10 @@ public void commit(final Set<String> graphSourceNamesToCloseTxOn) {
closeTx(graphSourceNamesToCloseTxOn, Transaction.Status.COMMIT);
}

private void installLicense(HugeConfig config) {
LicenseVerifier.instance().install(config, this);
}

private void closeTx(final Set<String> graphSourceNamesToCloseTxOn,
final Transaction.Status tx) {
final Set<Graph> graphsToCloseTxOn = new HashSet<>();
Expand Down Expand Up @@ -192,13 +199,12 @@ private void checkBackendVersionOrExit() {
}
BackendStoreSystemInfo info = new BackendStoreSystemInfo(hugegraph);
if (!info.exist()) {
LOG.error("The backend store of '{}' has not been initialized",
throw new BackendException(
"The backend store of '%s' has not been initialized",
hugegraph.name());
System.exit(-1);
}
if (!info.checkVersion()) {
// Exit if versions are inconsistent
System.exit(-1);
throw new BackendException("Check backend store version failed");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.core.GraphManager;
Expand Down Expand Up @@ -64,7 +67,7 @@ public void config(HugeConfig config) {

public HugeConfig config() {
E.checkState(this.config != null,
"license verify manager has not been installed");
"License verify manager has not been installed");
return this.config;
}

Expand All @@ -74,15 +77,18 @@ public void graphManager(GraphManager graphManager) {

public GraphManager graphManager() {
E.checkState(this.graphManager != null,
"license verify manager has not been installed");
"License verify manager has not been installed");
return this.graphManager;
}

@Override
protected synchronized void validate(LicenseContent content)
throws LicenseContentException {
protected synchronized void validate(LicenseContent content) {
// call super validate firstly
super.validate(content);
try {
super.validate(content);
} catch (LicenseContentException e) {
throw new HugeException("License valiverifydate failed");
}

// Verify the customized license parameters.
List<ExtraParam> extraParams;
Expand All @@ -97,14 +103,12 @@ protected synchronized void validate(LicenseContent content)
LOG.debug("server id is {}", serverId);
ExtraParam param = this.matchParam(serverId, extraParams);
if (param == null) {
throw new LicenseContentException("The current server's id " +
"is not authorized");
throw new HugeException("The current server's id is not authorized");
}

this.checkVersion(param);
this.checkGraphs(param);
this.checkIp(param);
this.checkMac(param);
this.checkIpAndMac(param);
this.checkCpu(param);
this.checkRam(param);
this.checkThreads(param);
Expand All @@ -124,7 +128,7 @@ private ExtraParam matchParam(String id, List<ExtraParam> extraParams) {
return null;
}

private void checkVersion(ExtraParam param) throws LicenseContentException {
private void checkVersion(ExtraParam param) {
String expectVersion = param.version();
if (StringUtils.isEmpty(expectVersion)) {
return;
Expand All @@ -137,7 +141,7 @@ private void checkVersion(ExtraParam param) throws LicenseContentException {
}
}

private void checkGraphs(ExtraParam param) throws LicenseContentException {
private void checkGraphs(ExtraParam param) {
int expectGraphs = param.graphs();
if (expectGraphs == NO_LIMIT) {
return;
Expand All @@ -150,7 +154,7 @@ private void checkGraphs(ExtraParam param) throws LicenseContentException {
}
}

private void checkIp(ExtraParam param) throws LicenseContentException {
private void checkIpAndMac(ExtraParam param) {
String expectIp = param.ip();
if (StringUtils.isEmpty(expectIp)) {
return;
Expand All @@ -161,22 +165,29 @@ private void checkIp(ExtraParam param) throws LicenseContentException {
"The server's ip '%s' doesn't match the authorized '%s'",
actualIps, expectIp);
}
}

private void checkMac(ExtraParam param) throws LicenseContentException {
String expectMac = param.mac();
if (StringUtils.isEmpty(expectMac)) {
return;
}
List<String> actualMacs = this.machineInfo.getMacAddress();
if (!actualMacs.contains(expectMac)) {
String actualMac;
try {
actualMac = this.machineInfo.getMacByInetAddress(
InetAddress.getByName(expectIp));
} catch (UnknownHostException e) {
throw newLicenseException(
"Failed to get mac address for ip '%s'", expectIp);
}
String expectFormatMac = expectMac.toUpperCase().replaceAll(":", "-");
String actualFormatMac = actualMac.toUpperCase().replaceAll(":", "-");
if (!actualFormatMac.equals(expectFormatMac)) {
throw newLicenseException(
"The server's mac '%s' doesn't match the authorized '%s'",
actualMacs, expectMac);
actualMac, expectMac);
}
}

private void checkCpu(ExtraParam param) throws LicenseContentException {
private void checkCpu(ExtraParam param) {
int expectCpus = param.cpus();
if (expectCpus == NO_LIMIT) {
return;
Expand All @@ -189,7 +200,7 @@ private void checkCpu(ExtraParam param) throws LicenseContentException {
}
}

private void checkRam(ExtraParam param) throws LicenseContentException {
private void checkRam(ExtraParam param) {
// Unit MB
int expectRam = param.ram();
if (expectRam == NO_LIMIT) {
Expand All @@ -205,7 +216,7 @@ private void checkRam(ExtraParam param) throws LicenseContentException {
}
}

private void checkThreads(ExtraParam param) throws LicenseContentException {
private void checkThreads(ExtraParam param) {
int expectThreads = param.threads();
if (expectThreads == NO_LIMIT) {
return;
Expand All @@ -218,22 +229,25 @@ private void checkThreads(ExtraParam param) throws LicenseContentException {
}
}

private void checkMemory(ExtraParam param) throws LicenseContentException {
private void checkMemory(ExtraParam param) {
// Unit MB
int expectMemory = param.memory();
if (expectMemory == NO_LIMIT) {
return;
}
/*
* NOTE: this max memory will be slightly smaller than XMX,
* because only one survivor will be used
*/
long actualMemory = Runtime.getRuntime().maxMemory() / Bytes.MB;
if (actualMemory > expectMemory) {
throw newLicenseException(
"The server's max memory(MB) '%s' exceeded the " +
"The server's max heap memory(MB) '%s' exceeded the " +
"limit(MB) '%s'", actualMemory, expectMemory);
}
}

private LicenseContentException newLicenseException(String message,
Object... args) {
return new LicenseContentException(String.format(message, args));
private HugeException newLicenseException(String message, Object... args) {
return new HugeException(message, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.define.WorkLoad;
import com.baidu.hugegraph.license.LicenseVerifier;
import com.baidu.hugegraph.util.E;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jersey2.InstrumentedResourceMethodApplicationListener;
Expand Down Expand Up @@ -68,10 +67,6 @@ public ApplicationConfig(HugeConfig conf) {
register(new InstrumentedResourceMethodApplicationListener(registry));
}

private void installLicense(HugeConfig config, GraphManager manager) {
LicenseVerifier.instance().install(config, manager);
}

private class ConfFactory extends AbstractBinder
implements Factory<HugeConfig> {

Expand Down Expand Up @@ -110,8 +105,7 @@ public GraphManagerFactory(HugeConfig conf) {
@Override
public void onEvent(ApplicationEvent event) {
if (event.getType() == this.EVENT_INITED) {
GraphManagerFactory.this.manager = new GraphManager(conf);
ApplicationConfig.this.installLicense(conf, manager);
manager = new GraphManager(conf);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ public void start() throws IOException {
ResourceConfig rc = new ApplicationConfig(this.conf);

this.httpServer = this.configHttpServer(uri, rc);
this.httpServer.start();

try {
this.httpServer.start();
} catch (Throwable e) {
this.httpServer.shutdownNow();
throw e;
}
this.calcMaxWriteThreads();
}

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-cassandra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph</artifactId>
<version>0.10.2</version>
<version>0.10.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>hugegraph-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public void closeScheduler(HugeGraph graph) {
if (scheduler != null && scheduler.close()) {
this.schedulers.remove(graph);
}
this.closeTaskTx(graph);
if (!this.taskExecutor.isTerminated()) {
this.closeTaskTx(graph);
}
}

private void closeTaskTx(HugeGraph graph) {
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-dist/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>hugegraph-dist</name>
Expand Down
Binary file not shown.
1 change: 0 additions & 1 deletion hugegraph-dist/src/assembly/static/conf/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
</appenders>
<loggers>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
<logger name="org.apache.cassandra" level="INFO" additivity="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public HugeGraphServer(String gremlinServerConf, String restServerConf)
this.restServer = HugeRestServer.start(restServerConf);
} catch (Throwable e) {
LOG.error("HugeRestServer start error: ", e);
this.gremlinServer.stop();
try {
this.gremlinServer.stop().get();
} catch (Throwable t) {
LOG.error("GremlinServer stop error: ", t);
}
HugeGraph.shutdown(30L);
throw e;
}
Expand Down Expand Up @@ -87,8 +91,8 @@ public static void main(String[] args) throws Exception {
}

HugeRestServer.register();
HugeGraphServer server = new HugeGraphServer(args[0], args[1]);

HugeGraphServer server = new HugeGraphServer(args[0], args[1]);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("HugeGraphServer stopping");
server.stop();
Expand Down
2 changes: 1 addition & 1 deletion hugegraph-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-hbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-palo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hugegraph-postgresql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>hugegraph</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<version>0.10.2</version>
<version>0.10.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Loading

0 comments on commit a53503c

Please sign in to comment.