From 10de4e0d5f7543a611989f713d2741c1003d1f6a Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Sun, 26 Jun 2022 17:54:29 +0800 Subject: [PATCH] front: support startMulitipoint with args fix: ContractClient missing arguments error update: @Router arguments format fix: ContractStatusRecorder null exception --- .../nodecenter/CheckAgentAliveTimer.java | 55 +++++++ .../server/nodecenter/NodeCenterActions.java | 142 ++---------------- 2 files changed, 66 insertions(+), 131 deletions(-) create mode 100644 src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java diff --git a/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java b/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java new file mode 100644 index 0000000..3cbf5cf --- /dev/null +++ b/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java @@ -0,0 +1,55 @@ +package org.bdware.server.nodecenter; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bdware.sc.bean.ContractDesp; + +import java.util.HashSet; +import java.util.Set; +import java.util.TimerTask; + +public class CheckAgentAliveTimer extends TimerTask { + private final String pubkey; + static Logger LOGGER = LogManager.getLogger(CheckAgentAliveTimer.class); + + public CheckAgentAliveTimer(String pubKey) { + this.pubkey = pubKey; + } + + @Override + public void run() { + try { + checkAlive(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void checkAlive() { + Set toRemove = new HashSet<>(); + CMNode info; + info = NodeCenterActions.nodeInfos.get(pubkey); + if (!info.checkAlive()) { + synchronized (info) { + if (null != info.contracts && !info.contracts.isEmpty()) { + for (ContractDesp cd : info.contracts) { + cd.setIsMaster(false); + //注意 选举不通过NC的机制触发。 + LOGGER.info( + "checkAlive---- 设置节点 " + + info.pubKey.substring(0, 5) + + " 的合约 " + + cd.contractID + + " isMaster=" + + false); + } + } + } + } + NodeCenterActions.nodeInfos.remove(pubkey); + try { + info.connection.controller.ctx.close(); + } catch (Exception ignored) { + } + } +} diff --git a/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java b/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java index aa2d727..d11f35b 100644 --- a/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java +++ b/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java @@ -2,8 +2,6 @@ package org.bdware.server.nodecenter; import com.google.gson.*; import com.google.gson.reflect.TypeToken; -import io.netty.util.Timeout; -import io.netty.util.TimerTask; import io.netty.util.internal.StringUtil; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,7 +23,6 @@ import org.zz.gmhelper.SM2Util; import java.io.*; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; public class NodeCenterActions { // static SyncResult syncResult = new SyncResult(); @@ -44,104 +41,8 @@ public class NodeCenterActions { // TODO 定期清缓存 // public static Map requestCache = // new ConcurrentHashMap<>(); // key is contractID,only for requestAll type contract - static TimerTask checkAliveTask; - static Timeout checkAliveTimeout; - - static { - int delay = 1000; - checkAliveTask = - new TimerTask() { - @Override - public void run(Timeout arg0) { - try { - if (!arg0.isCancelled()) { - checkAlive(); - } - } catch (Exception e) { - e.printStackTrace(); - } - if (!arg0.isCancelled()) { - checkAliveTimeout = - SyncResult.timer.newTimeout(this, delay, TimeUnit.MILLISECONDS); - } - } - - // heartbeat?(zyx) - private void checkAlive() { - Set toRemove = new HashSet<>(); - for (String key : nodeInfos.keySet()) { - if (!nodeInfos.get(key).checkAlive()) { - LOGGER.info("Node " + key.substring(0, 5) + " is offline!"); - // LOGGER.info( - // "[NodeCenterActions] 发现节点 - // " - // + key.substring(0, - // 5) - // + " not alive!"); - toRemove.add(key); - } - } - - for (String key : toRemove) { - CMNode info = nodeInfos.get(key); - LOGGER.debug("Heart beat: " + info.nodeName + " is offline!"); - // System.out.println("[ADSP]NC心跳机制发现节点 " + - // info.nodeName + " 下线!"); - - synchronized (info) { - if (null != info.contracts && !info.contracts.isEmpty()) { - for (ContractDesp cd : info.contracts) { - cd.setIsMaster(false); - LOGGER.info( - "checkAlive---- 设置节点 " - + info.pubKey.substring(0, 5) - + " 的合约 " - + cd.contractID - + " isMaster=" - + false); - } - } - } - - nodeInfos.remove(key); - try { - info.connection.controller.ctx.close(); - - } catch (Exception ignored) { - } - - /* for(String contractID : recoverMap.get(info.pubKey).keySet()){ - MasterActions.unitModeCheck(contractID); - ContractRecord record = recoverMap.get(info.pubKey).get(contractID); - synchronized (record) { - if (record.recoverFlag == RecoverFlag.Fine) - record.recoverFlag = RecoverFlag.ToRecover; - } - }*/ - } - } - }; // checkAliveTask - checkAliveTimeout = - SyncResult.timer.newTimeout(checkAliveTask, delay, TimeUnit.MILLISECONDS); - - // checkAliveTimeout = SyncResult.timer.newTimeout(checkAliveTask, 20, TimeUnit.SECONDS); - - // NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay( - // () -> { - // boolean flag = clearCache(); - // if (flag) { - // try { - // Thread.sleep(10000L); - // } catch (InterruptedException e) { - // LOGGER.error("sleeping is interrupted! " + e.getMessage()); - // } - // } - // }, - // 0, - // 0, - // TimeUnit.SECONDS); - } - + TimerTask checkAliveTask; + static Timer checkAliveTimer = new Timer(); public NodeCenterFrameHandler controller; String nodeID; // node pubKey boolean NCConFlag = false; // true表示这个是别的NC连过来的 @@ -151,6 +52,7 @@ public class NodeCenterActions { public NodeCenterActions(NodeCenterFrameHandler nodeCenterFrameHandler) { controller = nodeCenterFrameHandler; + } public static ContractDesp getContractByID(String key) { @@ -218,42 +120,18 @@ public class NodeCenterActions { } return r; } - // - // @Action(userPermission = 0) - // public void login(JsonObject json, ResultCallback resultCallback) { - // long start = System.currentTimeMillis(); - // if (sessionID == null) - // resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"failed\"}"); - // String signature = json.get("signature").getAsString(); - // String pubKey = json.get("pubKey").getAsString(); - // SM2 sm2 = new SM2(); - // boolean result = - // sm2.verify( - // (sessionID), - // Signature.loadFromString(signature), - // "", - // SM2KeyPair.publicKeyStr2ECPoint(pubKey)); - // logger.debug("session:" + (sessionID)); - // if (result) { - // this.nodeManagerPubkey = pubKey; - // logger.debug("设置公钥" + pubKey); - // resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"success\"}"); - // } else { - // resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"failed\"}"); - // } - // long l = getPermission(nodeManagerPubkey); - // controller.setPermission(l); - // long end = System.currentTimeMillis(); - // logger.debug("time:" + (end - start)); - // } private void sendContractResult(JsonObject json) { controller.sendMsg(json.toString()); } + @Action public void syncPing(JsonObject json, ResultCallback rc) { // logger.debug("[NodeCenterAction] syncPing"); + checkAliveTask.cancel(); + checkAliveTask = new CheckAgentAliveTimer(nodeID); + checkAliveTimer.schedule(checkAliveTask, 20000L); if (json.has("data")) { String data = json.get("data").getAsString(); LOGGER.debug("[syncPing] data" + data); @@ -351,6 +229,10 @@ public class NodeCenterActions { r.data = "failed"; if (sessionID != null && SM2Util.plainStrVerify(pubkey, pubkey + sessionID, signature)) { nodeID = pubkey; + checkAliveTask = new CheckAgentAliveTimer(nodeID); + checkAliveTimer.schedule(checkAliveTask, 20000L); + + controller.pubKey = nodeID; CMNode node = new CMNode(this, pubkey); String nodeShortID = node.pubKey.substring(0, 5); @@ -563,8 +445,6 @@ public class NodeCenterActions { @Action(async = true, userPermission = 1) public void checkAlive(JsonObject json, ResultCallback rc) { - checkAliveTimeout.cancel(); - checkAliveTimeout = SyncResult.timer.newTimeout(checkAliveTask, 0, TimeUnit.SECONDS); rc.onResult("{\"action\":\"pong\",\"data\":" + System.currentTimeMillis() + "}"); }