feat: modify exportedFunctions to support quick seekFunction

modify exportedFunctions from List to Map to support quick seekFunction
This commit is contained in:
Frank.R.Wu 2021-11-11 12:53:21 +08:00
parent 08f86a445f
commit bb811da070
3 changed files with 104 additions and 97 deletions

View File

@ -139,20 +139,22 @@ public class ContractClient {
strEvent, new TypeToken<Map<String, REventSemantics>>() { strEvent, new TypeToken<Map<String, REventSemantics>>() {
}.getType()); }.getType());
// LOGGER.info("initProps ---- position-----3"); // LOGGER.info("initProps ---- position-----3");
contractMeta.exportedFunctions =
JsonUtil.fromJson(
get.syncGet("", "getExportedFunctions", ""),
new TypeToken<List<FunctionDesp>>() {
}.getType());
contractMeta.dependentContracts = JsonUtil.fromJson( contractMeta.dependentContracts = JsonUtil.fromJson(
get.syncGet("", "getDependentContracts", ""), get.syncGet("", "getDependentContracts", ""),
new TypeToken<Set<String>>() { new TypeToken<Set<String>>() {
}.getType()); }.getType());
// LOGGER.info("initProps ---- position-----4"); // LOGGER.info("initProps ---- position-----4");
contractMeta.exportedFunctions = new HashMap<>();
List<FunctionDesp> exportedFunctions =
JsonUtil.fromJson(
get.syncGet("", "getExportedFunctions", ""),
new TypeToken<List<FunctionDesp>>() {
}.getType());
contractMeta.logDetail = new HashMap<>(); contractMeta.logDetail = new HashMap<>();
for (FunctionDesp desp : contractMeta.exportedFunctions) { for (FunctionDesp func : exportedFunctions) {
contractMeta.exportedFunctions.put(func.functionName, func);
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
for (AnnotationNode anno : desp.annotations) { for (AnnotationNode anno : func.annotations) {
if (anno.getType().equals("LogType")) { if (anno.getType().equals("LogType")) {
for (String logType : anno.getArgs()) { for (String logType : anno.getArgs()) {
str.append(logType.replaceAll("\"", "")).append(";"); 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"); // LOGGER.info("initProps ---- position-----5");
try { try {
@ -372,8 +374,8 @@ public class ContractClient {
return contractMeta.declaredEvents; return contractMeta.declaredEvents;
} }
public List<FunctionDesp> getExportedFunctions() { public Collection<FunctionDesp> getExportedFunctions() {
return contractMeta.exportedFunctions; return contractMeta.getExportedFunctions();
} }
public String getIdentifier() { public String getIdentifier() {

View File

@ -74,9 +74,7 @@ public class ContractManager {
new SynchronousQueue<>()); new SynchronousQueue<>());
public static ScheduledExecutorService scheduledThreadPool = public static ScheduledExecutorService scheduledThreadPool =
Executors.newScheduledThreadPool(10); Executors.newScheduledThreadPool(10);
public static DoipServiceInfoConfigurer doipConfigurer; public static DoipServiceInfoConfigurer doipConfigurer;
public static int logStage = 0; public static int logStage = 0;
public static Sigar sigar = null; // 获取network等资源什么 public static Sigar sigar = null; // 获取network等资源什么
@ -99,8 +97,8 @@ public class ContractManager {
ManagerHandler handler; ManagerHandler handler;
ServiceServer server; ServiceServer server;
Map<String, RespCache> reqCache = new ConcurrentHashMap<>(); // key is requestID Map<String, RespCache> reqCache = new ConcurrentHashMap<>(); // key is requestID
private ContractClient analysisClient;
// private long expiredTime; // private long expiredTime;
private ContractClient analysisClient;
public ContractManager() { public ContractManager() {
instance = this; instance = this;
@ -117,20 +115,6 @@ public class ContractManager {
projectRecoder = new ProjectRecorder("./ContractManagerDB"); 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) // public static void updateContractHandleRecord(Contract c, ResultCallback resultCallback)
// throws Exception { // 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 { public static byte[] fileToByteArray(String filename) throws IOException {
File f = new File(filename); File f = new File(filename);
if (!f.exists()) { if (!f.exists()) {
@ -432,16 +430,16 @@ public class ContractManager {
return null; return null;
} }
// public void setExpiredTime(long time) {
// expiredTime = time;
// }
public String getContractNameByID(String id) { public String getContractNameByID(String id) {
ContractMeta meta = statusRecorder.getContractMeta(id); ContractMeta meta = statusRecorder.getContractMeta(id);
if (meta != null) return meta.name; if (meta != null) return meta.name;
return null; return null;
} }
// public void setExpiredTime(long time) {
// expiredTime = time;
// }
public void reconnectContractProcess() { public void reconnectContractProcess() {
RecoverMechTimeRecorder.startReconnectCP = System.currentTimeMillis(); RecoverMechTimeRecorder.startReconnectCP = System.currentTimeMillis();
LOGGER.info("reconnectContractProcess"); LOGGER.info("reconnectContractProcess");
@ -546,25 +544,6 @@ public class ContractManager {
return null; 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) { // public String resumeStartContractAndRedirect(Contract c, PrintStream ps, String alias) {
// long freeMemory = getFreeMemory(); // long freeMemory = getFreeMemory();
// if (freeMemory < memoryLimit) { // if (freeMemory < memoryLimit) {
@ -732,19 +711,23 @@ public class ContractManager {
// return resumeStartContractAndRedirect(c, System.out, null); // return resumeStartContractAndRedirect(c, System.out, null);
// } // }
public long getFreeMemory() { public String changeDumpPeriod(String contractName, String dumpPeriod) {
try { ContractResult r;
if (null == sigar) { ContractClient client = getByName(contractName);
sigar = new Sigar(); ProjectConfig config = projectRecoder.getProjectConfig(contractName);
} config.setDumpPeriod(dumpPeriod);
Mem mem = sigar.getMem(); if (null == client) {
LOGGER.debug("[free memory] " + mem.getFree() + " " + mem.getActualFree()); r = new ContractResult(Status.Error, new JsonPrimitive("contract process not found"));
return mem.getFree(); return JsonUtil.toJson(r);
} catch (Throwable e) {
e.printStackTrace(System.err);
e.printStackTrace();
} }
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) { // 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) { public void addLocalContractLog(String action, ContractClient client) {
ContractMeta meta = client.contractMeta; ContractMeta meta = client.contractMeta;
addLocalContractLog(action, meta.contract.getID(), meta.name, meta.contract.getOwner()); addLocalContractLog(action, meta.contract.getID(), meta.name, meta.contract.getOwner());
@ -1391,6 +1389,10 @@ public class ContractManager {
long startTime) { long startTime) {
try { try {
ContractResult cr = JsonUtil.fromJson(result, ContractResult.class); 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()) { if (null != cr.events && !cr.events.isEmpty()) {
List<REvent> msgList = cr.events; List<REvent> msgList = cr.events;
cr.events = null; cr.events = null;
@ -1474,6 +1476,11 @@ public class ContractManager {
case RequestAllResponseAll: case RequestAllResponseAll:
case RequestAllResponseFirst: case RequestAllResponseFirst:
case RequestAllResponseHalf: 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()) { if (multiMeta != null && multiMeta.isMaster()) {
masterStub.executeByMaster(client, rcb, cr); masterStub.executeByMaster(client, rcb, cr);
} else { } else {
@ -1491,23 +1498,22 @@ public class ContractManager {
if (null != nodeCenterConn && null != masterStub) { if (null != nodeCenterConn && null != masterStub) {
String pubKey = nodeCenterConn.routeContract(cr.getContractID()); String pubKey = nodeCenterConn.routeContract(cr.getContractID());
// TODO 如果此时master正在选举中先缓存请求有必要吗 // TODO 如果此时master正在选举中先缓存请求有必要吗
// if(pubKey == null ||pubKey.equals("")){ // if (null == pubKey || pubKey.equals("")) {
// logger.info("Master正在崩溃选举中,先将请求缓存!"); // LOGGER.info("Master正在崩溃选举中,先将请求缓存!");
// if(MasterClientRecoverMechAction.requestsToMaster == null){ // if (null == MasterClientRecoverMechAction.requestsToMaster) {
// MasterClientRecoverMechAction.requestsToMaster = new // MasterClientRecoverMechAction.requestsToMaster =
// ConcurrentHashMap<>(); // new ConcurrentHashMap<>();
// } // }
// //
// if(!MasterClientRecoverMechAction.requestsToMaster.containsKey(c.getContractID())){ // if (!MasterClientRecoverMechAction.requestsToMaster.containsKey(c.getContractID())) {
// // MasterClientRecoverMechAction.requestsToMaster
// MasterClientRecoverMechAction.requestsToMaster.put(c.getContractID(),new // .put(c.getContractID(), new Queue<>());
// Queue<>()); // }
// } //
// // MasterClientRecoverMechAction.requestsToMaster.get(c.getContractID())
// MasterClientRecoverMechAction.requestsToMaster.get(c.getContractID()).add(new // .add(new RequestToMaster(c));
// RequestToMaster(c)); // return;
// return; // }
// }
// LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey); // LOGGER.info("查看合约 " + cr.getContractID() + " 的master为 " + pubKey);
@ -1862,7 +1868,7 @@ public class ContractManager {
desc.contractID = meta.id; desc.contractID = meta.id;
desc.contractName = meta.name; desc.contractName = meta.name;
desc.events = meta.declaredEvents; desc.events = meta.declaredEvents;
desc.exportedFunctions = meta.exportedFunctions; desc.exportedFunctions = meta.getExportedFunctions();
desc.type = meta.contract.getType(); desc.type = meta.contract.getType();
desc.annotations = meta.annotations; desc.annotations = meta.annotations;
desc.yjsType = meta.getYjsType(); desc.yjsType = meta.getYjsType();
@ -2208,7 +2214,6 @@ public class ContractManager {
static class StrCollector extends ResultCallback { static class StrCollector extends ResultCallback {
String strRet = "{\"data\":\"Timeout\"}"; String strRet = "{\"data\":\"Timeout\"}";
boolean hasResult = false; boolean hasResult = false;
long start = System.currentTimeMillis();
@Override @Override
public void onResult(String str) { public void onResult(String str) {
@ -2302,7 +2307,7 @@ public class ContractManager {
// 合约状态调用次数流量统计内存占用 // 合约状态调用次数流量统计内存占用
String traffic, storage; String traffic, storage;
long times; long times;
List<FunctionDesp> exportedFunctions; Collection<FunctionDesp> exportedFunctions;
Map<String, REventSemantics> events; Map<String, REventSemantics> events;
ContractStatusEnum contractStatus; ContractStatusEnum contractStatus;
String contractPermission; String contractPermission;

View File

@ -7,10 +7,7 @@ import org.bdware.sc.event.REvent;
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 java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ContractMeta implements IDSerializable { public class ContractMeta implements IDSerializable {
public Contract contract; public Contract contract;
@ -19,7 +16,7 @@ public class ContractMeta implements IDSerializable {
String id; String id;
boolean isDebug; boolean isDebug;
Map<String, REvent.REventSemantics> declaredEvents; Map<String, REvent.REventSemantics> declaredEvents;
List<FunctionDesp> exportedFunctions; Map<String, FunctionDesp> exportedFunctions;
Map<String, String> logDetail; Map<String, String> logDetail;
List<AnnotationNode> annotations; List<AnnotationNode> annotations;
Set<String> dependentContracts; Set<String> dependentContracts;
@ -57,8 +54,8 @@ public class ContractMeta implements IDSerializable {
return annotations; return annotations;
} }
public List<FunctionDesp> getExportedFunctions() { public Collection<FunctionDesp> getExportedFunctions() {
return exportedFunctions; return null == exportedFunctions ? null : exportedFunctions.values();
} }
public Set<String> getDependentContracts() { public Set<String> getDependentContracts() {
@ -96,19 +93,22 @@ public class ContractMeta implements IDSerializable {
} }
public FunctionDesp getExportedFunction(String action) { public FunctionDesp getExportedFunction(String action) {
if (funCache == null) funCache = new HashMap<>(); if (funCache == null) {
funCache = new HashMap<>();
}
FunctionDesp desp = funCache.get(action); FunctionDesp desp = funCache.get(action);
if (desp != null) return desp; if (desp != null) {
return desp;
}
desp = seekFunction(action); desp = seekFunction(action);
if (desp != null) funCache.put(action, desp); if (null != desp) {
funCache.put(action, desp);
}
return desp; return desp;
} }
private FunctionDesp seekFunction(String action) { public FunctionDesp seekFunction(String action) {
for (FunctionDesp desp : exportedFunctions) { return null == exportedFunctions ? null : exportedFunctions.get(action);
if (desp != null && desp.functionName.equals(action)) return desp;
}
return null;
} }
public void setContract(Contract c) { public void setContract(Contract c) {