From 5a4fdb8a13f4eb60d7cf63c040b53be81392ea45 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Thu, 21 Apr 2022 10:18:47 +0800 Subject: [PATCH] feat: support createParam feat: docker script --- cmconfig.readme.md | 31 ++++++- .../java/org/bdware/server/CMHttpServer.java | 46 ++++++---- .../org/bdware/server/CongestionControl.java | 1 - src/main/java/org/bdware/server/GRPCPool.java | 2 +- .../org/bdware/server/action/CMActions.java | 90 ++++++++++++------- .../org/bdware/server/action/FileActions.java | 1 - .../bdware/server/action/MasterWSAction.java | 2 + .../server/action/TemporyTestAction.java | 17 ---- 8 files changed, 119 insertions(+), 71 deletions(-) diff --git a/cmconfig.readme.md b/cmconfig.readme.md index 529a04e..76a98b5 100644 --- a/cmconfig.readme.md +++ b/cmconfig.readme.md @@ -19,7 +19,9 @@ "textFileSuffixes":".yjs,.json,.txt,.css,.js,.html,.md,.conf,.csv", "withBdledgerClient":"./runnable/bdledger_mac", "withBdledgerServer":false, - "consistencyPlugins": "./libs/custom-plugin.jar" + "consistencyPlugins": "./libs/custom-plugin.jar", + "startContract": [], + "datachainConf": "021.node.internetapi.cn:21121" } ``` ## consistencyPlugins 配置 @@ -35,4 +37,29 @@ 1. wsPluginActions: WS端,包括contractManagerFrameHandler和CMHttpHandler 2. clientToAgentPlugins: client -> cluster 包括MasterClientFrameHandler 3. clientToClusterPlugins:[], client -> NodeCenterClientHandler -4. tcpPlugins:tcp 包括TcpserverFrameHandler \ No newline at end of file +4. tcpPlugins:tcp 包括TcpserverFrameHandler + +## startContract配置说明 +其中startConfig.json为json数组结构,格式如下: +```json +[ + { + "path": "./BDWareProjectDir/publicCompiled/xxx.ypk", + "owner": "", + "createParam": {} + }, + { + ... + } +] +path为必填配置项。表示启动的ypk的路径。 +owner为可选配置,不填时,使用NodeManger的key作为Owner。 +createParam为可选配置。表示合约的启动参数。 +``` +## datachainConf 配置说明 +1.针对Window Docker/Mac Docker +可使用`"datachainConf":"host.docker.internal:2401"`进行配置。 +2.针对Linux Docker,可通过查看`docker 0`的网卡IP进行设置。 +如:`"datachainConf":"172.16.10.1:2401"`。 +3.如果是在同一dockercompose.yml下启动,可以使用: +`"datachainConf":"bdledger:2401"`进行配置。 \ No newline at end of file diff --git a/src/main/java/org/bdware/server/CMHttpServer.java b/src/main/java/org/bdware/server/CMHttpServer.java index 939c2a2..739c6a0 100644 --- a/src/main/java/org/bdware/server/CMHttpServer.java +++ b/src/main/java/org/bdware/server/CMHttpServer.java @@ -1,5 +1,7 @@ package org.bdware.server; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; import io.netty.channel.*; @@ -137,7 +139,6 @@ public class CMHttpServer { TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins); NodeCenterClientHandler.clientToClusterPlugins = parseStrAsList(cmdConf.clientToClusterPlugins); org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins); - if (!cmdConf.debug.isEmpty()) { try { String[] classes = cmdConf.debug.split(","); @@ -153,26 +154,41 @@ public class CMHttpServer { LOGGER.warn(e.getMessage()); } } - if (cmdConf.overwrite) { - cmdConf.write(CONFIG_PATH); - } - if (cmdConf.startContract != null && cmdConf.startContract.length() > 0) { + + if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) { ContractManager.scheduledThreadPool.schedule( () -> { - String[] paths = cmdConf.startContract.split(","); - for (String path : paths) { - File f = new File(path); - if (!f.getName().endsWith(".ypk") || !f.exists()) - continue; - Contract c = new Contract(); - c.setScript(f.getAbsolutePath()); - c.setType(ContractExecType.Sole); - c.setOwner(UserManagerAction.getNodeManager()); - ContractManager.instance.startContract(c); + try { + for (JsonElement je : cmdConf.startContract) { + JsonObject jo = je.getAsJsonObject(); + String path = jo.get("path").getAsString(); + File f = new File(path); + if (!f.getName().endsWith(".ypk") || !f.exists()) + continue; + Contract c = new Contract(); + c.setScript(f.getAbsolutePath()); + c.setType(ContractExecType.Sole); + if (jo.has("owner")) + c.setOwner(jo.get("owner").getAsString()); + else + c.setOwner(UserManagerAction.getNodeManager()); + if (jo.has("createParam")) + c.setCreateParam(jo.get("createParam")); + ContractManager.instance.startContract(c); + } + } catch (Exception e) { + e.printStackTrace(); } }, 10, TimeUnit.SECONDS); } + if (cmdConf.datachainConf!=null){ + GlobalConf.resetDataChain(cmdConf.datachainConf); + } + if (cmdConf.overwrite) { + cmdConf.write(CONFIG_PATH); + } + } private static String[] parseStrAsList(String str) { diff --git a/src/main/java/org/bdware/server/CongestionControl.java b/src/main/java/org/bdware/server/CongestionControl.java index 3e13aef..c28390e 100644 --- a/src/main/java/org/bdware/server/CongestionControl.java +++ b/src/main/java/org/bdware/server/CongestionControl.java @@ -47,7 +47,6 @@ public class CongestionControl { jo.addProperty("slaveConn", slaveCounter.get()); jo.addProperty("slaveQueue", masterProxyLoad.get()); jo.addProperty("masterQueue", masterServerLoad.get()); - jo.addProperty("maxConnCount", maxHttpConnCount); jo.addProperty("httpMaxQueue", currHttpMax); jo.addProperty("slaveMaxQueue", maxMasterProxyLoad); diff --git a/src/main/java/org/bdware/server/GRPCPool.java b/src/main/java/org/bdware/server/GRPCPool.java index 40c54ee..889b72b 100644 --- a/src/main/java/org/bdware/server/GRPCPool.java +++ b/src/main/java/org/bdware/server/GRPCPool.java @@ -815,7 +815,7 @@ public class GRPCPool implements ChainOpener { logType.put("branch", "branch log failure"); } } else if (info.equals("Result")) { - s = jo.get("result").getAsString(); + s = jo.get("result").toString(); s = JsonUtil.toJson(s); if (s.length() > 2) { s = s.substring(1, s.length() - 1); diff --git a/src/main/java/org/bdware/server/action/CMActions.java b/src/main/java/org/bdware/server/action/CMActions.java index 00523cf..d0a4dbb 100644 --- a/src/main/java/org/bdware/server/action/CMActions.java +++ b/src/main/java/org/bdware/server/action/CMActions.java @@ -733,7 +733,9 @@ public class CMActions implements OnHashCallback { c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.doSignature(GlobalConf.instance.keyPair); } - + if (args.has("createParam")) { + c.setCreateParam(args.get("createParam")); + } // if (!c.verifySignature()) { // ret.put("data", "verify failed"); // resultCallback.onResult(gson.toJson(ret)); @@ -831,6 +833,9 @@ public class CMActions implements OnHashCallback { c.setOwner(GlobalConf.instance.keyPair.getPublicKeyStr()); c.doSignature(GlobalConf.instance.keyPair); } + if (args.has("createParam")) { + c.setCreateParam(args.get("createParam")); + } // 上面那些。。有空再整理一下 c.setDebug(false); c.setOwner(args.get("verifiedPubKey").getAsString()); @@ -956,6 +961,9 @@ public class CMActions implements OnHashCallback { } c.setDebug(true); System.out.println(c.isDebug()); + if (args.has("createParam")) { + c.setCreateParam(args.get("createParam")); + } // 上面那些。。有空再整理一下 c.setOwner(args.get("verifiedPubKey").getAsString()); @@ -1266,6 +1274,53 @@ public class CMActions implements OnHashCallback { } } + @Action(userPermission = 1L << 26, async = true) + public void restartContractProcess(JsonObject args, ResultCallback resultCallback) { + LOGGER.debug("[CMActions] killContractProcess : " + args.has("verifiedPubKey")); + if (args.has("verifiedPubKey") && (args.has("id") || args.has("name"))) { + ContractRequest rc = new ContractRequest(); + long s = System.currentTimeMillis(); + if (args.has("id")) { + // stop unit contract using contract name + rc.setContractID(args.get("id").getAsString()); + } else { + // stop unit contract using contract name + rc.setContractID(args.get("name").getAsString()); + } + ContractMeta meta = manager.statusRecorder.getContractMeta(rc.getContractID()); + Contract contract = meta.contract; + killContractProcess(args, resultCallback); + manager.startContractAndRedirect(contract,System.out); + String data = manager.listContractsWithOwner( + args.get("verifiedPubKey").getAsString(), + null, + args.has("filters") ? args.get("filters").getAsInt() : 0); + ReplyUtil.simpleReply(resultCallback, "onListContractProcess", data); + }else { + ReplyUtil.simpleReply(resultCallback, "onRestartContractProcess", "Failed: Illegal parameters"); + } + } + + @Action(userPermission = 1L << 26, async = true) + public void pruneKilledContract(JsonObject args, ResultCallback resultCallback) { + Map map = CMActions.manager.statusRecorder.getStatus(); + LOGGER.info("pruneKilledContract!!!"); + Set toRemove = new HashSet<>(); + for (ContractMeta meta : map.values()) { + if (meta.getStatus().equals(ContractStatusEnum.KILLED)) + toRemove.add(meta); + } + LOGGER.info("pruneKilledContract!!! size:" + toRemove.size()); + for (ContractMeta key : toRemove) { + CMActions.manager.statusRecorder.remove(key); + MultiContractMeta multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(key.getID()); + CMActions.manager.multiContractRecorder.remove(multiMeta); + } + resultCallback.onResult("size:" + toRemove.size()); + + } + + @Action(userPermission = 1L << 26, async = true) public void queryContractInstanceDOI(JsonObject args, ResultCallback resultCallback) { long s = System.currentTimeMillis(); @@ -1356,39 +1411,6 @@ public class CMActions implements OnHashCallback { resultCallback.onResult(r); } - @Action(async = true, userPermission = 1L << 26) - public void startContractInTempZips(JsonObject args, ResultCallback resultCallback) { - long start = System.currentTimeMillis(); - Contract c = new Contract(); - // c.setType(Type.Algorithm); - c.setType(ContractExecType.Sole); - String pubkey = args.get("owner").getAsString(); - // SCManagerServlet.registerCCCallback(json.getString("requestID"), this); - Map r = new HashMap<>(); - r.put("action", "onStartContract"); - String path = args.get("path").getAsString(); - path = - new File(new File(GlobalConf.instance.projectDir, "tempZips"), path) - .getAbsolutePath(); - c.setScript(path); - c.setSignature(args.get("signature").getAsString()); - c.setOwner(pubkey); - if (!c.verifySignature()) { - r.put("data", "verify failed"); - resultCallback.onResult(r); - return; - } - r.put("data", manager.startContractAndRedirect(c, System.out)); - r.put("cid", c.getID()); - r.put("executeTime", System.currentTimeMillis() - start); - resultCallback.onResult(r); - - if (args.has("dumpPeriod")) { - LOGGER.debug("[CMActions]启动后设置dump周期" + args.get("dumpPeriod").getAsString()); - manager.changeDumpPeriod(c.getID(), args.get("dumpPeriod").getAsString()); - } - } - @Action(userPermission = 1L << 26, async = true, httpAccess = false) public void killAllContract(JsonObject args, ResultCallback resultCallback) { if (args.has("verifiedPubKey")) { diff --git a/src/main/java/org/bdware/server/action/FileActions.java b/src/main/java/org/bdware/server/action/FileActions.java index 9d4da67..610fa71 100644 --- a/src/main/java/org/bdware/server/action/FileActions.java +++ b/src/main/java/org/bdware/server/action/FileActions.java @@ -402,7 +402,6 @@ public class FileActions { } public static void startIfManifestDetected(File targetDir) { - try { String contractName = targetDir.getName(); String owner = targetDir.getParentFile().getName(); diff --git a/src/main/java/org/bdware/server/action/MasterWSAction.java b/src/main/java/org/bdware/server/action/MasterWSAction.java index d995b87..c2fc075 100644 --- a/src/main/java/org/bdware/server/action/MasterWSAction.java +++ b/src/main/java/org/bdware/server/action/MasterWSAction.java @@ -92,6 +92,8 @@ public class MasterWSAction { } else if (args.has("projectName")) { contract.setScript("/" + args.get("projectName").getAsString()); } + if (args.has("createParam")) + contract.setCreateParam(args.get("createParam")); // contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json"); // String toVerify = diff --git a/src/main/java/org/bdware/server/action/TemporyTestAction.java b/src/main/java/org/bdware/server/action/TemporyTestAction.java index a655e52..152c7ed 100644 --- a/src/main/java/org/bdware/server/action/TemporyTestAction.java +++ b/src/main/java/org/bdware/server/action/TemporyTestAction.java @@ -176,24 +176,7 @@ public class TemporyTestAction { resultCallback.onResult(jo.toString()); } - @Action(async = true) - public void pruneKilledContract(JsonObject args, ResultCallback resultCallback) { - Map map = CMActions.manager.statusRecorder.getStatus(); - LOGGER.info("pruneKilledContract!!!"); - Set toRemove = new HashSet<>(); - for (ContractMeta meta : map.values()) { - if (meta.getStatus().equals(ContractStatusEnum.KILLED)) - toRemove.add(meta); - } - LOGGER.info("pruneKilledContract!!! size:" + toRemove.size()); - for (ContractMeta key : toRemove) { - CMActions.manager.statusRecorder.remove(key); - MultiContractMeta multiMeta = CMActions.manager.multiContractRecorder.getMultiContractMeta(key.getID()); - CMActions.manager.multiContractRecorder.remove(multiMeta); - } - resultCallback.onResult("size:" + toRemove.size()); - } @Action(async = true) public void reconnectPort(JsonObject args, ResultCallback resultCallback) {