mirror of
https://gitee.com/BDWare/cm
synced 2025-01-10 01:44:04 +00:00
feat: modify exportedFunctions to support quick seekFunction
modify exportedFunctions from List to Map to support quick seekFunction
This commit is contained in:
parent
08f86a445f
commit
bb811da070
@ -139,20 +139,22 @@ public class ContractClient {
|
||||
strEvent, new TypeToken<Map<String, REventSemantics>>() {
|
||||
}.getType());
|
||||
// LOGGER.info("initProps ---- position-----3");
|
||||
contractMeta.exportedFunctions =
|
||||
JsonUtil.fromJson(
|
||||
get.syncGet("", "getExportedFunctions", ""),
|
||||
new TypeToken<List<FunctionDesp>>() {
|
||||
}.getType());
|
||||
contractMeta.dependentContracts = JsonUtil.fromJson(
|
||||
get.syncGet("", "getDependentContracts", ""),
|
||||
new TypeToken<Set<String>>() {
|
||||
}.getType());
|
||||
// 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<>();
|
||||
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<FunctionDesp> getExportedFunctions() {
|
||||
return contractMeta.exportedFunctions;
|
||||
public Collection<FunctionDesp> getExportedFunctions() {
|
||||
return contractMeta.getExportedFunctions();
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
|
@ -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<String, RespCache> 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<REvent> 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<FunctionDesp> exportedFunctions;
|
||||
Collection<FunctionDesp> exportedFunctions;
|
||||
Map<String, REventSemantics> events;
|
||||
ContractStatusEnum contractStatus;
|
||||
String contractPermission;
|
||||
|
@ -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<String, REvent.REventSemantics> declaredEvents;
|
||||
List<FunctionDesp> exportedFunctions;
|
||||
Map<String, FunctionDesp> exportedFunctions;
|
||||
Map<String, String> logDetail;
|
||||
List<AnnotationNode> annotations;
|
||||
Set<String> dependentContracts;
|
||||
@ -57,8 +54,8 @@ public class ContractMeta implements IDSerializable {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
public List<FunctionDesp> getExportedFunctions() {
|
||||
return exportedFunctions;
|
||||
public Collection<FunctionDesp> getExportedFunctions() {
|
||||
return null == exportedFunctions ? null : exportedFunctions.values();
|
||||
}
|
||||
|
||||
public Set<String> 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user