From 2b2f57b7cf4eba5ea650063a7bd416753f80d092 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Sun, 26 Jun 2022 17:54:30 +0800 Subject: [PATCH] front: support startMulitipoint with args fix: ContractClient missing arguments error update: @Router arguments format fix: ContractStatusRecorder null exception --- .../java/org/bdware/sc/ContractClient.java | 162 +++++++++--------- .../java/org/bdware/sc/ContractManager.java | 9 +- .../org/bdware/sc/ContractStatusRecorder.java | 2 + 3 files changed, 92 insertions(+), 81 deletions(-) diff --git a/src/main/java/org/bdware/sc/ContractClient.java b/src/main/java/org/bdware/sc/ContractClient.java index 67caa51..20bf304 100644 --- a/src/main/java/org/bdware/sc/ContractClient.java +++ b/src/main/java/org/bdware/sc/ContractClient.java @@ -18,9 +18,9 @@ import org.bdware.sc.encrypt.HardwareInfo.OSType; import org.bdware.sc.event.REvent.REventSemantics; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.YjsType; +import org.bdware.sc.units.MultiContractMeta; import org.bdware.sc.util.JsonUtil; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; import java.io.PrintStream; @@ -218,7 +218,7 @@ public class ContractClient { // LOGGER.debug("======= registerPort:" + ret + "-->" + ContractManager.startPort); } - public String startProcess(PrintStream ps) { + public String startProcess(PrintStream ps) throws Exception { isRunning = false; port = -1; @@ -259,103 +259,109 @@ public class ContractClient { // "org.bdware.sc.ContractProcess", // "-port=" + cPort.getPort()); int startPort = ContractManager.cPort.getPortAndInc(); + //(isDebug ? "-Dlog4j.configurationFile=./log4j2.debug.properties" : ""), ProcessBuilder builder = new ProcessBuilder( "java", "-Dfile.encoding=UTF-8", - darg, (isDebug ? "-Dlog4j.configurationFile=./log4j2.cp.properties" : ""), + darg, "-jar", classpath, "-port=" + startPort, "-cmi=" + cmi, // cmi 区分不同CM的cp (isDebug ? "-debug" : "")); - File directory = new File(""); + File directory = new File("./"); LOGGER.debug("[CMD] path: " + directory.getAbsolutePath()); LOGGER.debug(JsonUtil.toPrettyJson(builder.command())); Map map = builder.environment(); map.put("java.library.path", jniPath.getAbsolutePath() + osJni); - builder.directory(new File("./")); + builder.directory(directory); LOGGER.debug("start process:"); - try { - process = builder.start(); - this.pid = getPid(process); - LOGGER.info("[CP PPID ] " + pid); - PrintStream printStream = new PrintStream(process.getOutputStream()); - printStream.println("CP PID:" + pid); - printStream.close(); - InputStream processInputStream = process.getInputStream(); - Scanner sc = new Scanner(processInputStream); - String status = null; - while (sc.hasNext()) { - status = sc.nextLine(); - LOGGER.info("[CP] " + status); - if (status.contains("mainPort")) { - try { - // Set contractPort to max(mainPort, contractPort) - int portIndex = status.indexOf("mainPort"); - int port = - Integer.parseInt( - status.substring(portIndex + 9, portIndex + 14) - .replaceAll("\\s+", "")); - if (port != startPort) { - ContractManager.cPort.reSetPort(port + 1); - } - } catch (Exception ignored) { + process = builder.start(); + + this.pid = getPid(process); + LOGGER.info("[CP PPID] " + pid); + PrintStream printStream = new PrintStream(process.getOutputStream()); + printStream.println("CP PID:" + pid); + printStream.close(); + InputStream processInputStream = process.getInputStream(); + Scanner sc = new Scanner(processInputStream); + String status = null; + while (sc.hasNext()) { + status = sc.nextLine(); + LOGGER.info("[CP] " + status); + if (status.contains("mainPort")) { + try { + // Set contractPort to max(mainPort, contractPort) + int portIndex = status.indexOf("mainPort"); + int port = + Integer.parseInt( + status.substring(portIndex + 9, portIndex + 14) + .replaceAll("\\s+", "")); + if (port != startPort) { + 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() { diff --git a/src/main/java/org/bdware/sc/ContractManager.java b/src/main/java/org/bdware/sc/ContractManager.java index 6674e8c..0791ed2 100644 --- a/src/main/java/org/bdware/sc/ContractManager.java +++ b/src/main/java/org/bdware/sc/ContractManager.java @@ -423,7 +423,11 @@ public class ContractManager { c.setOwner(pair.getPublicKeyStr()); c.setScript("contract analysis_client{}"); analysisClient = new ContractClient(c); - analysisClient.startProcess(System.out); + try { + analysisClient.startProcess(System.out); + } catch (Exception e) { + e.printStackTrace(); + } } public String getContractIDByName(String name) { @@ -742,7 +746,6 @@ public class ContractManager { LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree()); return mem.getFree(); } catch (Throwable e) { - e.printStackTrace(System.err); e.printStackTrace(); } return memoryLimit + 1; @@ -929,7 +932,7 @@ public class ContractManager { return "contract manager can't support:" + c.getType(); } } catch (Exception e) { - e.printStackTrace(); + statusRecorder.killContract(c.getID()); r = new ContractResult(Status.Error, new JsonPrimitive("exception occurs: " + e.getMessage())); return JsonUtil.toJson(r); } diff --git a/src/main/java/org/bdware/sc/ContractStatusRecorder.java b/src/main/java/org/bdware/sc/ContractStatusRecorder.java index 537b50b..88210fa 100644 --- a/src/main/java/org/bdware/sc/ContractStatusRecorder.java +++ b/src/main/java/org/bdware/sc/ContractStatusRecorder.java @@ -184,6 +184,8 @@ public class ContractStatusRecorder extends StatusRecorder { } } 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())) { return cc; }