From bb811da0704301b4737ceb354b314e196fc3be47 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Thu, 11 Nov 2021 12:53:21 +0800 Subject: [PATCH] feat: modify exportedFunctions to support quick seekFunction modify exportedFunctions from List to Map to support quick seekFunction --- .../java/org/bdware/sc/ContractClient.java | 22 +-- .../java/org/bdware/sc/ContractManager.java | 149 +++++++++--------- src/main/java/org/bdware/sc/ContractMeta.java | 30 ++-- 3 files changed, 104 insertions(+), 97 deletions(-) diff --git a/src/main/java/org/bdware/sc/ContractClient.java b/src/main/java/org/bdware/sc/ContractClient.java index 8328a57..8495f71 100644 --- a/src/main/java/org/bdware/sc/ContractClient.java +++ b/src/main/java/org/bdware/sc/ContractClient.java @@ -139,20 +139,22 @@ public class ContractClient { strEvent, new TypeToken>() { }.getType()); // LOGGER.info("initProps ---- position-----3"); - contractMeta.exportedFunctions = - JsonUtil.fromJson( - get.syncGet("", "getExportedFunctions", ""), - new TypeToken>() { - }.getType()); contractMeta.dependentContracts = JsonUtil.fromJson( get.syncGet("", "getDependentContracts", ""), new TypeToken>() { }.getType()); // LOGGER.info("initProps ---- position-----4"); + contractMeta.exportedFunctions = new HashMap<>(); + List exportedFunctions = + JsonUtil.fromJson( + get.syncGet("", "getExportedFunctions", ""), + new TypeToken>() { + }.getType()); contractMeta.logDetail = new HashMap<>(); - for (FunctionDesp desp : contractMeta.exportedFunctions) { + for (FunctionDesp func : exportedFunctions) { + contractMeta.exportedFunctions.put(func.functionName, func); StringBuilder str = new StringBuilder(); - for (AnnotationNode anno : desp.annotations) { + for (AnnotationNode anno : func.annotations) { if (anno.getType().equals("LogType")) { for (String logType : anno.getArgs()) { str.append(logType.replaceAll("\"", "")).append(";"); @@ -172,7 +174,7 @@ public class ContractClient { } } } - contractMeta.logDetail.put(desp.functionName, str.toString()); + contractMeta.logDetail.put(func.functionName, str.toString()); } // LOGGER.info("initProps ---- position-----5"); try { @@ -372,8 +374,8 @@ public class ContractClient { return contractMeta.declaredEvents; } - public List getExportedFunctions() { - return contractMeta.exportedFunctions; + public Collection getExportedFunctions() { + return contractMeta.getExportedFunctions(); } public String getIdentifier() { diff --git a/src/main/java/org/bdware/sc/ContractManager.java b/src/main/java/org/bdware/sc/ContractManager.java index 18e4139..8bff915 100644 --- a/src/main/java/org/bdware/sc/ContractManager.java +++ b/src/main/java/org/bdware/sc/ContractManager.java @@ -74,9 +74,7 @@ public class ContractManager { new SynchronousQueue<>()); public static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10); - public static DoipServiceInfoConfigurer doipConfigurer; - public static int logStage = 0; public static Sigar sigar = null; // 获取network等资源什么 @@ -99,8 +97,8 @@ public class ContractManager { ManagerHandler handler; ServiceServer server; Map reqCache = new ConcurrentHashMap<>(); // key is requestID - private ContractClient analysisClient; // private long expiredTime; + private ContractClient analysisClient; public ContractManager() { instance = this; @@ -117,20 +115,6 @@ public class ContractManager { projectRecoder = new ProjectRecorder("./ContractManagerDB"); } - private static String convertToBytes(long traffic) { - String[] unit = new String[]{"B", "KB", "MB", "GB", "TB"}; - double d = traffic; - int i; - for (i = 0; i < unit.length - 1; i++) { - if (d > 1024.0) { - d /= 1024.0; - } else { - break; - } - } - return String.format("%.2f %s", d, unit[i]); - } - // public static void updateContractHandleRecord(Contract c, ResultCallback resultCallback) // throws Exception { // @@ -205,6 +189,20 @@ public class ContractManager { // */ // } + private static String convertToBytes(long traffic) { + String[] unit = new String[]{"B", "KB", "MB", "GB", "TB"}; + double d = traffic; + int i; + for (i = 0; i < unit.length - 1; i++) { + if (d > 1024.0) { + d /= 1024.0; + } else { + break; + } + } + return String.format("%.2f %s", d, unit[i]); + } + public static byte[] fileToByteArray(String filename) throws IOException { File f = new File(filename); if (!f.exists()) { @@ -432,16 +430,16 @@ public class ContractManager { return null; } + // public void setExpiredTime(long time) { + // expiredTime = time; + // } + public String getContractNameByID(String id) { ContractMeta meta = statusRecorder.getContractMeta(id); if (meta != null) return meta.name; return null; } - // public void setExpiredTime(long time) { - // expiredTime = time; - // } - public void reconnectContractProcess() { RecoverMechTimeRecorder.startReconnectCP = System.currentTimeMillis(); LOGGER.info("reconnectContractProcess"); @@ -546,25 +544,6 @@ public class ContractManager { return null; } - public String changeDumpPeriod(String contractName, String dumpPeriod) { - ContractResult r; - ContractClient client = getByName(contractName); - ProjectConfig config = projectRecoder.getProjectConfig(contractName); - config.setDumpPeriod(dumpPeriod); - if (null == client) { - r = new ContractResult(Status.Error, new JsonPrimitive("contract process not found")); - return JsonUtil.toJson(r); - } - loadProjectConfig(client); - - ContractMeta meta = client.contractMeta; - addLocalContractLog( - "changeDumpPeriod", meta.contract.getID(), meta.name, meta.contract.getOwner()); - r = new ContractResult(Status.Success, new JsonPrimitive("change dump period finished")); - - return JsonUtil.toJson(r); - } - // public String resumeStartContractAndRedirect(Contract c, PrintStream ps, String alias) { // long freeMemory = getFreeMemory(); // if (freeMemory < memoryLimit) { @@ -732,19 +711,23 @@ public class ContractManager { // return resumeStartContractAndRedirect(c, System.out, null); // } - public long getFreeMemory() { - try { - if (null == sigar) { - sigar = new Sigar(); - } - Mem mem = sigar.getMem(); - LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree()); - return mem.getFree(); - } catch (Throwable e) { - e.printStackTrace(System.err); - e.printStackTrace(); + public String changeDumpPeriod(String contractName, String dumpPeriod) { + ContractResult r; + ContractClient client = getByName(contractName); + ProjectConfig config = projectRecoder.getProjectConfig(contractName); + config.setDumpPeriod(dumpPeriod); + if (null == client) { + r = new ContractResult(Status.Error, new JsonPrimitive("contract process not found")); + return JsonUtil.toJson(r); } - return memoryLimit + 1; + loadProjectConfig(client); + + ContractMeta meta = client.contractMeta; + addLocalContractLog( + "changeDumpPeriod", meta.contract.getID(), meta.name, meta.contract.getOwner()); + r = new ContractResult(Status.Success, new JsonPrimitive("change dump period finished")); + + return JsonUtil.toJson(r); } // private void hangUpKillContract(String contractID) { @@ -784,6 +767,21 @@ public class ContractManager { // } // } + public long getFreeMemory() { + try { + if (null == sigar) { + sigar = new Sigar(); + } + Mem mem = sigar.getMem(); + LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree()); + return mem.getFree(); + } catch (Throwable e) { + e.printStackTrace(System.err); + e.printStackTrace(); + } + return memoryLimit + 1; + } + public void addLocalContractLog(String action, ContractClient client) { ContractMeta meta = client.contractMeta; addLocalContractLog(action, meta.contract.getID(), meta.name, meta.contract.getOwner()); @@ -1391,6 +1389,10 @@ public class ContractManager { long startTime) { try { ContractResult cr = JsonUtil.fromJson(result, ContractResult.class); + if (null != client.contractMeta.seekFunction(request.getAction()) && + client.contractMeta.seekFunction(request.getAction()).isView) { + cr.events = null; + } if (null != cr.events && !cr.events.isEmpty()) { List msgList = cr.events; cr.events = null; @@ -1474,6 +1476,11 @@ public class ContractManager { case RequestAllResponseAll: case RequestAllResponseFirst: case RequestAllResponseHalf: + case SelfAdaptiveSharding: + if (null != client.contractMeta.seekFunction(cr.getAction()) && + client.contractMeta.exportedFunctions.get(cr.getAction()).isView) { + executeLocallyAsync(cr, rcb, hcb); + } if (multiMeta != null && multiMeta.isMaster()) { masterStub.executeByMaster(client, rcb, cr); } else { @@ -1491,23 +1498,22 @@ public class ContractManager { if (null != nodeCenterConn && null != masterStub) { String pubKey = nodeCenterConn.routeContract(cr.getContractID()); // TODO 如果此时master正在选举中,先缓存请求,有必要吗? - // if(pubKey == null ||pubKey.equals("")){ - // logger.info("Master正在崩溃选举中,先将请求缓存!"); - // if(MasterClientRecoverMechAction.requestsToMaster == null){ - // MasterClientRecoverMechAction.requestsToMaster = new - // ConcurrentHashMap<>(); - // } - // - // if(!MasterClientRecoverMechAction.requestsToMaster.containsKey(c.getContractID())){ - // - // MasterClientRecoverMechAction.requestsToMaster.put(c.getContractID(),new - // Queue<>()); - // } - // - // MasterClientRecoverMechAction.requestsToMaster.get(c.getContractID()).add(new - // RequestToMaster(c)); - // return; - // } +// if (null == pubKey || pubKey.equals("")) { +// LOGGER.info("Master正在崩溃选举中,先将请求缓存!"); +// if (null == MasterClientRecoverMechAction.requestsToMaster) { +// MasterClientRecoverMechAction.requestsToMaster = +// new ConcurrentHashMap<>(); +// } +// +// if (!MasterClientRecoverMechAction.requestsToMaster.containsKey(c.getContractID())) { +// MasterClientRecoverMechAction.requestsToMaster +// .put(c.getContractID(), new Queue<>()); +// } +// +// MasterClientRecoverMechAction.requestsToMaster.get(c.getContractID()) +// .add(new RequestToMaster(c)); +// return; +// } // LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); @@ -1862,7 +1868,7 @@ public class ContractManager { desc.contractID = meta.id; desc.contractName = meta.name; desc.events = meta.declaredEvents; - desc.exportedFunctions = meta.exportedFunctions; + desc.exportedFunctions = meta.getExportedFunctions(); desc.type = meta.contract.getType(); desc.annotations = meta.annotations; desc.yjsType = meta.getYjsType(); @@ -2208,7 +2214,6 @@ public class ContractManager { static class StrCollector extends ResultCallback { String strRet = "{\"data\":\"Timeout\"}"; boolean hasResult = false; - long start = System.currentTimeMillis(); @Override public void onResult(String str) { @@ -2302,7 +2307,7 @@ public class ContractManager { // 合约状态,调用次数,流量统计,内存占用 String traffic, storage; long times; - List exportedFunctions; + Collection exportedFunctions; Map events; ContractStatusEnum contractStatus; String contractPermission; diff --git a/src/main/java/org/bdware/sc/ContractMeta.java b/src/main/java/org/bdware/sc/ContractMeta.java index d8c8c8d..3cc6b52 100644 --- a/src/main/java/org/bdware/sc/ContractMeta.java +++ b/src/main/java/org/bdware/sc/ContractMeta.java @@ -7,10 +7,7 @@ import org.bdware.sc.event.REvent; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.YjsType; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class ContractMeta implements IDSerializable { public Contract contract; @@ -19,7 +16,7 @@ public class ContractMeta implements IDSerializable { String id; boolean isDebug; Map declaredEvents; - List exportedFunctions; + Map exportedFunctions; Map logDetail; List annotations; Set dependentContracts; @@ -57,8 +54,8 @@ public class ContractMeta implements IDSerializable { return annotations; } - public List getExportedFunctions() { - return exportedFunctions; + public Collection getExportedFunctions() { + return null == exportedFunctions ? null : exportedFunctions.values(); } public Set getDependentContracts() { @@ -96,19 +93,22 @@ public class ContractMeta implements IDSerializable { } public FunctionDesp getExportedFunction(String action) { - if (funCache == null) funCache = new HashMap<>(); + if (funCache == null) { + funCache = new HashMap<>(); + } FunctionDesp desp = funCache.get(action); - if (desp != null) return desp; + if (desp != null) { + return desp; + } desp = seekFunction(action); - if (desp != null) funCache.put(action, desp); + if (null != desp) { + funCache.put(action, desp); + } return desp; } - private FunctionDesp seekFunction(String action) { - for (FunctionDesp desp : exportedFunctions) { - if (desp != null && desp.functionName.equals(action)) return desp; - } - return null; + public FunctionDesp seekFunction(String action) { + return null == exportedFunctions ? null : exportedFunctions.get(action); } public void setContract(Contract c) {