front: support startMulitipoint with args

fix: ContractClient missing arguments error
update: @Router arguments format
fix: ContractStatusRecorder null exception
This commit is contained in:
CaiHQ 2022-06-26 17:54:30 +08:00
parent 2f874832b8
commit 2b2f57b7cf
3 changed files with 92 additions and 81 deletions

View File

@ -18,9 +18,9 @@ import org.bdware.sc.encrypt.HardwareInfo.OSType;
import org.bdware.sc.event.REvent.REventSemantics; import org.bdware.sc.event.REvent.REventSemantics;
import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.YjsType; import org.bdware.sc.node.YjsType;
import org.bdware.sc.units.MultiContractMeta;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
@ -218,7 +218,7 @@ public class ContractClient {
// LOGGER.debug("======= registerPort:" + ret + "-->" + ContractManager.startPort); // LOGGER.debug("======= registerPort:" + ret + "-->" + ContractManager.startPort);
} }
public String startProcess(PrintStream ps) { public String startProcess(PrintStream ps) throws Exception {
isRunning = false; isRunning = false;
port = -1; port = -1;
@ -259,103 +259,109 @@ public class ContractClient {
// "org.bdware.sc.ContractProcess", // "org.bdware.sc.ContractProcess",
// "-port=" + cPort.getPort()); // "-port=" + cPort.getPort());
int startPort = ContractManager.cPort.getPortAndInc(); int startPort = ContractManager.cPort.getPortAndInc();
//(isDebug ? "-Dlog4j.configurationFile=./log4j2.debug.properties" : ""),
ProcessBuilder builder = ProcessBuilder builder =
new ProcessBuilder( new ProcessBuilder(
"java", "java",
"-Dfile.encoding=UTF-8", "-Dfile.encoding=UTF-8",
darg, (isDebug ? "-Dlog4j.configurationFile=./log4j2.cp.properties" : ""), darg,
"-jar", "-jar",
classpath, classpath,
"-port=" + startPort, "-port=" + startPort,
"-cmi=" + cmi, // cmi 区分不同CM的cp "-cmi=" + cmi, // cmi 区分不同CM的cp
(isDebug ? "-debug" : "")); (isDebug ? "-debug" : ""));
File directory = new File(""); File directory = new File("./");
LOGGER.debug("[CMD] path: " + directory.getAbsolutePath()); LOGGER.debug("[CMD] path: " + directory.getAbsolutePath());
LOGGER.debug(JsonUtil.toPrettyJson(builder.command())); LOGGER.debug(JsonUtil.toPrettyJson(builder.command()));
Map<String, String> map = builder.environment(); Map<String, String> map = builder.environment();
map.put("java.library.path", jniPath.getAbsolutePath() + osJni); map.put("java.library.path", jniPath.getAbsolutePath() + osJni);
builder.directory(new File("./")); builder.directory(directory);
LOGGER.debug("start process:"); LOGGER.debug("start process:");
try {
process = builder.start();
this.pid = getPid(process); process = builder.start();
LOGGER.info("[CP PPID ] " + pid);
PrintStream printStream = new PrintStream(process.getOutputStream()); this.pid = getPid(process);
printStream.println("CP PID:" + pid); LOGGER.info("[CP PPID] " + pid);
printStream.close(); PrintStream printStream = new PrintStream(process.getOutputStream());
InputStream processInputStream = process.getInputStream(); printStream.println("CP PID:" + pid);
Scanner sc = new Scanner(processInputStream); printStream.close();
String status = null; InputStream processInputStream = process.getInputStream();
while (sc.hasNext()) { Scanner sc = new Scanner(processInputStream);
status = sc.nextLine(); String status = null;
LOGGER.info("[CP] " + status); while (sc.hasNext()) {
if (status.contains("mainPort")) { status = sc.nextLine();
try { LOGGER.info("[CP] " + status);
// Set contractPort to max(mainPort, contractPort) if (status.contains("mainPort")) {
int portIndex = status.indexOf("mainPort"); try {
int port = // Set contractPort to max(mainPort, contractPort)
Integer.parseInt( int portIndex = status.indexOf("mainPort");
status.substring(portIndex + 9, portIndex + 14) int port =
.replaceAll("\\s+", "")); Integer.parseInt(
if (port != startPort) { status.substring(portIndex + 9, portIndex + 14)
ContractManager.cPort.reSetPort(port + 1); .replaceAll("\\s+", ""));
} if (port != startPort) {
} catch (Exception ignored) { ContractManager.cPort.reSetPort(port + 1);
} }
break; } catch (Exception ignored) {
} }
break;
} }
if (status != null) {
status = status.replaceAll(".*mainPort ", "");
}
assert status != null;
port = Integer.parseInt(status.split(" ")[0]);
LOGGER.debug("port=" + port);
ContractManager.cPort.updateDb(port, true);
get = new SocketGet("127.0.0.1", port);
get.syncGet("", "setDBInfo", ContractManager.dbPath);
if (isDebug) {
String tagA = (ps == System.out ? "[Contract_" + port + "_out] " : "");
String tagB = (ps == System.out ? "[Contract_" + port + "_err] " : "");
outputTracer.track(process, sc, tagA, ps);
errorTracer.track(process, new Scanner(process.getErrorStream()), tagB, ps);
} else {
// 关闭流否则缓冲区打满会阻塞进程
processInputStream.close();
sc.close();
}
get.syncGet("", "registerMangerPort", String.valueOf(ContractManager.cPort.getCMPort()));
if (isBundlePath(contractMeta.contract.getScriptStr())) {
status =
get.syncGet(
"", "setContractBundle", JsonUtil.toJson(contractMeta.contract));
} else {
status = get.syncGet("", "setContract", JsonUtil.toJson(contractMeta.contract));
}
LOGGER.debug("port:" + port + " status:" + status);
ContractResult r = JsonUtil.fromJson(status, ContractResult.class);
if (r.status == Status.Success) {
initProps();
get.syncGet("", "setPID", pid);
} else if (r.status == null) {
r.status = Status.Error;
r.result = new JsonPrimitive(status);
status = JsonUtil.toJson(r);
contractMeta.name = get.syncGet("", "getContractName", "");
}
return status;
} catch (Exception e) {
e.printStackTrace();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
ContractResult r =
new ContractResult(Status.Exception, new JsonPrimitive(bo.toString()));
return JsonUtil.toJson(r);
} }
if (status != null) {
status = status.replaceAll(".*mainPort ", "");
}
assert status != null;
port = Integer.parseInt(status.split(" ")[0]);
LOGGER.debug("port=" + port);
ContractManager.cPort.updateDb(port, true);
get = new SocketGet("127.0.0.1", port);
get.syncGet("", "setDBInfo", ContractManager.dbPath);
if (isDebug || get != null) {
String tagA = (ps == System.out ? "[Contract_" + port + "_out] " : "");
String tagB = (ps == System.out ? "[Contract_" + port + "_err] " : "");
outputTracer.track(process, sc, tagA, ps);
errorTracer.track(process, new Scanner(process.getErrorStream()), tagB, ps);
} else {
// 关闭流否则缓冲区打满会阻塞进程
processInputStream.close();
sc.close();
}
get.syncGet("", "registerMangerPort", String.valueOf(ContractManager.cPort.getCMPort()));
MultiContractMeta multiContractMeta =
ContractManager.instance.multiContractRecorder.getMultiContractMeta(contractMeta.getID());
if (multiContractMeta != null && multiContractMeta.getMembers() != null) {
String setMemberResult = get.syncGet(
"", "setMembers", JsonUtil.toJson(multiContractMeta.getMembers()));
LOGGER.debug("setMember:" + setMemberResult);
}
if (isBundlePath(contractMeta.contract.getScriptStr())) {
status =
get.syncGet(
"", "setContractBundle", JsonUtil.toJson(contractMeta.contract));
} else {
status = get.syncGet("", "setContract", JsonUtil.toJson(contractMeta.contract));
}
LOGGER.debug("port:" + port + " status:" + status);
ContractResult r = JsonUtil.fromJson(status, ContractResult.class);
if (r.status == Status.Success) {
initProps();
get.syncGet("", "setPID", pid);
} else if (r.status == null) {
r.status = Status.Error;
r.result = new JsonPrimitive(status);
status = JsonUtil.toJson(r);
contractMeta.name = get.syncGet("", "getContractName", "");
}
if (r.status != Status.Success) {
killProcess();
throw new IllegalStateException(r.result.getAsString());
}
return status;
} }
public void updateMemory() { public void updateMemory() {

View File

@ -423,7 +423,11 @@ public class ContractManager {
c.setOwner(pair.getPublicKeyStr()); c.setOwner(pair.getPublicKeyStr());
c.setScript("contract analysis_client{}"); c.setScript("contract analysis_client{}");
analysisClient = new ContractClient(c); analysisClient = new ContractClient(c);
analysisClient.startProcess(System.out); try {
analysisClient.startProcess(System.out);
} catch (Exception e) {
e.printStackTrace();
}
} }
public String getContractIDByName(String name) { public String getContractIDByName(String name) {
@ -742,7 +746,6 @@ public class ContractManager {
LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree()); LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree());
return mem.getFree(); return mem.getFree();
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(System.err);
e.printStackTrace(); e.printStackTrace();
} }
return memoryLimit + 1; return memoryLimit + 1;
@ -929,7 +932,7 @@ public class ContractManager {
return "contract manager can't support:" + c.getType(); return "contract manager can't support:" + c.getType();
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); statusRecorder.killContract(c.getID());
r = new ContractResult(Status.Error, new JsonPrimitive("exception occurs: " + e.getMessage())); r = new ContractResult(Status.Error, new JsonPrimitive("exception occurs: " + e.getMessage()));
return JsonUtil.toJson(r); return JsonUtil.toJson(r);
} }

View File

@ -184,6 +184,8 @@ public class ContractStatusRecorder extends StatusRecorder<ContractMeta> {
} }
} }
for (ContractMeta cc : getStatus().values()) { for (ContractMeta cc : getStatus().values()) {
if (cc.name == null) continue;
if (cc.contract == null) continue;
if (idOrNameOrDOI.equals(cc.name) || idOrNameOrDOI.equals(cc.contract.getDOI())) { if (idOrNameOrDOI.equals(cc.name) || idOrNameOrDOI.equals(cc.contract.getDOI())) {
return cc; return cc;
} }