mirror of
https://gitee.com/BDWare/router-backend
synced 2025-01-25 01:04:05 +00:00
front: support startMulitipoint with args
fix: ContractClient missing arguments error update: @Router arguments format fix: ContractStatusRecorder null exception
This commit is contained in:
parent
39fe0e22f3
commit
10de4e0d5f
@ -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<String> 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) {
|
||||
}
|
||||
}
|
||||
}
|
@ -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<String, RequestCache> 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<String> 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() + "}");
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user