Merge branch 'master' into Development

Conflicts:
	seednode/src/main/java/io/bitsquare/seednode/SeedNodeMain.java
This commit is contained in:
Manfred Karrer 2016-07-22 16:33:35 +02:00
commit e3b3342901
8 changed files with 133 additions and 9 deletions

View File

@ -24,16 +24,15 @@ public class Profiler {
private static final Logger log = LoggerFactory.getLogger(Profiler.class); private static final Logger log = LoggerFactory.getLogger(Profiler.class);
public static void printSystemLoad(Logger log) { public static void printSystemLoad(Logger log) {
String msg = printSystemLoadString(); log.warn(printSystemLoadString());
log.info(msg);
} }
public static String printSystemLoadString() { public static String printSystemLoadString() {
long used = getUsedMemory(); long used = getUsedMemoryInMB();
return "System load (nr. threads/used memory (MB)): " + Thread.activeCount() + "/" + used; return "System load: Memory (MB)): " + used + " / Nr. of threads: " + Thread.activeCount();
} }
public static long getUsedMemory() { public static long getUsedMemoryInMB() {
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long free = runtime.freeMemory() / 1024 / 1024; long free = runtime.freeMemory() / 1024 / 1024;
long total = runtime.totalMemory() / 1024 / 1024; long total = runtime.totalMemory() / 1024 / 1024;

View File

@ -0,0 +1,64 @@
package io.bitsquare.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;
// Borrowed from: https://dzone.com/articles/programmatically-restart-java
public class RestartUtil {
private static final Logger log = LoggerFactory.getLogger(RestartUtil.class);
/**
* Sun property pointing the main class and its arguments.
* Might not be defined on non Hotspot VM implementations.
*/
public static final String SUN_JAVA_COMMAND = "sun.java.command";
public static void restartApplication(String logPath) throws IOException {
try {
String java = System.getProperty("java.home") + "/bin/java";
List<String> vmArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
StringBuffer vmArgsOneLine = new StringBuffer();
for (String arg : vmArguments) {
// if it's the agent argument : we ignore it otherwise the
// address of the old application and the new one will be in conflict
if (!arg.contains("-agentlib")) {
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
// init the command to execute, add the vm args
final StringBuffer cmd = new StringBuffer(java + " " + vmArgsOneLine);
// program main and program arguments
String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");
// program main is a jar
if (mainCommand[0].endsWith(".jar")) {
// if it's a jar, add -jar mainJar
cmd.append("-jar " + new File(mainCommand[0]).getPath());
} else {
// else it's a .class, add the classpath and mainClass
cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
}
// finally add program arguments
for (int i = 1; i < mainCommand.length; i++) {
cmd.append(" ");
cmd.append(mainCommand[i]);
}
try {
final String command = "nohup " + cmd.toString() + " >/dev/null 2>" + logPath + " &";
log.warn("Executing cmd for restart:\n" + command);
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
throw new IOException("Error while trying to restart the application", e);
}
}
}

View File

@ -80,6 +80,7 @@ public class BitsquareEnvironment extends StandardEnvironment {
private final String appName; private final String appName;
private final String userDataDir; private final String userDataDir;
private final String appDataDir; private final String appDataDir;
private final String btcNetworkDir; private final String btcNetworkDir;
private final String logLevel; private final String logLevel;
@ -120,6 +121,10 @@ public class BitsquareEnvironment extends StandardEnvironment {
} }
} }
public String getAppDataDir() {
return appDataDir;
}
protected BitsquareEnvironment(PropertySource commandLineProperties) { protected BitsquareEnvironment(PropertySource commandLineProperties) {
userDataDir = commandLineProperties.containsProperty(USER_DATA_DIR_KEY) ? userDataDir = commandLineProperties.containsProperty(USER_DATA_DIR_KEY) ?
(String) commandLineProperties.getProperty(USER_DATA_DIR_KEY) : (String) commandLineProperties.getProperty(USER_DATA_DIR_KEY) :

View File

@ -104,6 +104,10 @@ public class GetDataRequestHandler {
}); });
} }
public void stop() {
cleanup();
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Private // Private
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -175,6 +175,11 @@ public class RequestDataHandler implements MessageListener {
} }
} }
public void stop() {
cleanup();
}
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Private // Private
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -23,6 +23,7 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
private static final Logger log = LoggerFactory.getLogger(RequestDataManager.class); private static final Logger log = LoggerFactory.getLogger(RequestDataManager.class);
private static final long RETRY_DELAY_SEC = 10; private static final long RETRY_DELAY_SEC = 10;
private static final long CLEANUP_TIMER = 120;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -216,7 +217,16 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
getDataRequestHandlers.put(uid, getDataRequestHandler); getDataRequestHandlers.put(uid, getDataRequestHandler);
getDataRequestHandler.handle((GetDataRequest) message, connection); getDataRequestHandler.handle((GetDataRequest) message, connection);
} else { } else {
log.warn("We have already a GetDataRequestHandler for that connection started"); log.warn("We have already a GetDataRequestHandler for that connection started. " +
"We start a cleanup timer if the handler has not closed by itself in between 2 minutes.");
UserThread.runAfter(() -> {
if (getDataRequestHandlers.containsKey(uid)) {
GetDataRequestHandler handler = getDataRequestHandlers.get(uid);
handler.stop();
getDataRequestHandlers.remove(uid);
}
}, CLEANUP_TIMER);
} }
} else { } else {
log.warn("We have stopped already. We ignore that onMessage call."); log.warn("We have stopped already. We ignore that onMessage call.");
@ -293,7 +303,16 @@ public class RequestDataManager implements MessageListener, ConnectionListener,
handlerMap.put(nodeAddress, requestDataHandler); handlerMap.put(nodeAddress, requestDataHandler);
requestDataHandler.requestData(nodeAddress); requestDataHandler.requestData(nodeAddress);
} else { } else {
log.warn("We have started already a requestDataHandshake to peer. nodeAddress=" + nodeAddress); log.warn("We have started already a requestDataHandshake to peer. nodeAddress=" + nodeAddress + "\n" +
"We start a cleanup timer if the handler has not closed by itself in between 2 minutes.");
UserThread.runAfter(() -> {
if (handlerMap.containsKey(nodeAddress)) {
RequestDataHandler handler = handlerMap.get(nodeAddress);
handler.stop();
handlerMap.remove(nodeAddress);
}
}, CLEANUP_TIMER);
} }
} else { } else {
log.warn("We have stopped already. We ignore that requestData call."); log.warn("We have stopped already. We ignore that requestData call.");

View File

@ -125,7 +125,7 @@ public class SeedNode {
}); });
} }
private void gracefulShutDown(ResultHandler resultHandler) { public void gracefulShutDown(ResultHandler resultHandler) {
log.debug("gracefulShutDown"); log.debug("gracefulShutDown");
try { try {
if (injector != null) { if (injector != null) {

View File

@ -21,12 +21,15 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.bitsquare.app.BitsquareEnvironment; import io.bitsquare.app.BitsquareEnvironment;
import io.bitsquare.app.BitsquareExecutable; import io.bitsquare.app.BitsquareExecutable;
import io.bitsquare.common.UserThread; import io.bitsquare.common.UserThread;
import io.bitsquare.common.util.Profiler;
import io.bitsquare.common.util.RestartUtil;
import joptsimple.OptionException; import joptsimple.OptionException;
import joptsimple.OptionParser; import joptsimple.OptionParser;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
@ -34,7 +37,10 @@ import static io.bitsquare.app.BitsquareEnvironment.*;
public class SeedNodeMain extends BitsquareExecutable { public class SeedNodeMain extends BitsquareExecutable {
private static final Logger log = LoggerFactory.getLogger(SeedNodeMain.class); private static final Logger log = LoggerFactory.getLogger(SeedNodeMain.class);
private static final long MAX_MEMORY_MB = 800;
private static final long CHECK_MEMORY_PERIOD_SEC = 60;
private SeedNode seedNode; private SeedNode seedNode;
private volatile boolean stopped;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
final ThreadFactory threadFactory = new ThreadFactoryBuilder() final ThreadFactory threadFactory = new ThreadFactoryBuilder()
@ -77,9 +83,31 @@ public class SeedNodeMain extends BitsquareExecutable {
@Override @Override
protected void doExecute(OptionSet options) { protected void doExecute(OptionSet options) {
SeedNode.setEnvironment(new BitsquareEnvironment(options)); final BitsquareEnvironment environment = new BitsquareEnvironment(options);
SeedNode.setEnvironment(environment);
UserThread.execute(() -> seedNode = new SeedNode()); UserThread.execute(() -> seedNode = new SeedNode());
UserThread.runPeriodically(() -> {
Profiler.printSystemLoad(log);
if (!stopped && Profiler.getUsedMemoryInMB() > MAX_MEMORY_MB) {
stopped = true;
seedNode.gracefulShutDown(() -> {
try {
final String[] tokens = environment.getAppDataDir().split("_");
String logPath = "error_" + (tokens.length > 1 ? tokens[tokens.length - 2] : "") + ".log";
RestartUtil.restartApplication(logPath);
} catch (IOException e) {
log.error(e.toString());
e.printStackTrace();
} finally {
log.warn("Shutdown complete");
System.exit(0);
}
});
}
}, CHECK_MEMORY_PERIOD_SEC);
while (true) { while (true) {
try { try {
Thread.sleep(Long.MAX_VALUE); Thread.sleep(Long.MAX_VALUE);