From 16350d3a78c3d7dfe0a2e863d0585f9a26fa5865 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Thu, 15 Jun 2023 11:07:56 +0800 Subject: [PATCH] build: config spotless plugin and reformat code --- build.gradle | 2 + .../java/org/bdware/sc/udp/UDPMessage.java | 131 ++- .../org/bdware/sc/udp/UDPMessageType.java | 2 +- src/main/java/org/bdware/sc/udp/UDPNode.java | 20 +- .../org/bdware/server/NodeCenterServer.java | 73 +- .../server/action/DistributeCallback.java | 33 +- .../server/action/RequestAllExecutor.java | 132 ++- .../server/action/RequestOnceExecutor.java | 28 +- .../server/action/ResponseOnceExecutor.java | 49 +- .../org/bdware/server/nodecenter/CMNode.java | 30 +- .../nodecenter/CheckAgentAliveTimer.java | 11 +- .../server/nodecenter/ContractExecutor.java | 2 +- .../nodecenter/ElectMasterTimeRecorder.java | 10 +- .../bdware/server/nodecenter/FileActions.java | 110 +-- .../bdware/server/nodecenter/LogActions.java | 40 +- .../server/nodecenter/MasterActions.java | 427 ++++---- .../server/nodecenter/MetaIndexAction.java | 237 +++-- .../nodecenter/MultiPointContractInfo.java | 4 +- .../server/nodecenter/NCClientActions.java | 9 +- .../server/nodecenter/NCClientHandler.java | 26 +- .../server/nodecenter/NCElectMasterUtil.java | 78 +- .../server/nodecenter/NCHttpHandler.java | 80 +- .../server/nodecenter/NCManagerAction.java | 95 +- .../bdware/server/nodecenter/NCTables.java | 14 +- .../bdware/server/nodecenter/NCUDPRunner.java | 35 +- .../bdware/server/nodecenter/NameAndID.java | 2 +- .../server/nodecenter/NodeCenterActions.java | 932 ++++++++---------- .../nodecenter/NodeCenterFrameHandler.java | 92 +- .../nodecenter/NodeCenterWSFrameHandler.java | 122 ++- .../server/nodecenter/OtherNCProxy.java | 63 +- .../server/nodecenter/TracingAction.java | 5 +- .../bdware/server/nodecenter/UnitActions.java | 509 +++++----- .../org/bdware/bdserver/PermissionHelper.java | 38 +- .../java/org/bdware/bdserver/TestServer.java | 3 +- .../org/bdware/bdserver/test/TestADSP.java | 385 ++++---- .../org/bdware/bdserver/test/TestSearch.java | 40 +- 36 files changed, 1775 insertions(+), 2094 deletions(-) diff --git a/build.gradle b/build.gradle index c70334b..a762a73 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,8 @@ plugins { id 'application' } +apply from: '../spotless.gradle' + mainClassName = 'org.bdware.server.NodeCenterServer' application { diff --git a/src/main/java/org/bdware/sc/udp/UDPMessage.java b/src/main/java/org/bdware/sc/udp/UDPMessage.java index 99ea432..b107095 100644 --- a/src/main/java/org/bdware/sc/udp/UDPMessage.java +++ b/src/main/java/org/bdware/sc/udp/UDPMessage.java @@ -2,85 +2,84 @@ package org.bdware.sc.udp; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; - import java.io.ObjectInputStream; +import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; - + public class UDPMessage implements Serializable { - private static final long serialVersionUID = -8103830946473687985L; - int isPart = 0; - public UDPMessageType type; - public int id; // 要标记sender - int order; - int requestID; - byte[] content; + private static final long serialVersionUID = -8103830946473687985L; + int isPart = 0; + public UDPMessageType type; + public int id; // 要标记sender + int order; + int requestID; + byte[] content; - public UDPMessage() { - } + public UDPMessage() {} - public void setIsPart(boolean isPart) { - this.isPart = isPart ? 1 : 0; - } + public void setIsPart(boolean isPart) { + this.isPart = isPart ? 1 : 0; + } - public void setContent(byte[] content) { - this.content = content; - } + public void setContent(byte[] content) { + this.content = content; + } - public byte[] getContent() { - return content; - } + public byte[] getContent() { + return content; + } - static int Len = 6000;// 1000 + static int Len = 6000;// 1000 - public List split() { - List splited = new ArrayList<>(); -// System.out.println("[UDPMessage split] content.length" + content.length); - if (content.length < Len) { - splited.add(this); - return splited; - } - for (int i = 0; i < content.length; i += Len) { - UDPMessage msg = new UDPMessage(); - msg.isPart = 1; - msg.id = this.id; - msg.order = i / Len; - if (i + Len > content.length) { - msg.content = new byte[content.length - i]; - System.arraycopy(content, i, msg.content, 0, content.length - i); - } else { - msg.content = new byte[Len]; - System.arraycopy(content, i, msg.content, 0, Len); - } - splited.add(msg); - } - return splited; - } + public List split() { + List splited = new ArrayList<>(); + // System.out.println("[UDPMessage split] content.length" + content.length); + if (content.length < Len) { + splited.add(this); + return splited; + } + for (int i = 0; i < content.length; i += Len) { + UDPMessage msg = new UDPMessage(); + msg.isPart = 1; + msg.id = this.id; + msg.order = i / Len; + if (i + Len > content.length) { + msg.content = new byte[content.length - i]; + System.arraycopy(content, i, msg.content, 0, content.length - i); + } else { + msg.content = new byte[Len]; + System.arraycopy(content, i, msg.content, 0, Len); + } + splited.add(msg); + } + return splited; + } - public byte[] toByteArray() { - try { - ByteArrayOutputStream bo = new ByteArrayOutputStream(); - ObjectOutputStream output; - output = new ObjectOutputStream(bo); - output.writeObject(this); - return bo.toByteArray(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } + public byte[] toByteArray() { + try { + ByteArrayOutputStream bo = new ByteArrayOutputStream(); + ObjectOutputStream output; + output = new ObjectOutputStream(bo); + output.writeObject(this); + return bo.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } - public static UDPMessage parse(byte[] data, int len) { - try { - ByteArrayInputStream bi = new ByteArrayInputStream(data); - ObjectInputStream input = new ObjectInputStream(bi); - return (UDPMessage) input.readObject(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } + public static UDPMessage parse(byte[] data, int len) { + try { + ByteArrayInputStream bi = new ByteArrayInputStream(data); + ObjectInputStream input = new ObjectInputStream(bi); + return (UDPMessage) input.readObject(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/org/bdware/sc/udp/UDPMessageType.java b/src/main/java/org/bdware/sc/udp/UDPMessageType.java index bf130c4..f0ef44d 100644 --- a/src/main/java/org/bdware/sc/udp/UDPMessageType.java +++ b/src/main/java/org/bdware/sc/udp/UDPMessageType.java @@ -3,5 +3,5 @@ package org.bdware.sc.udp; import java.io.Serializable; public enum UDPMessageType implements Serializable { - shakeHand, request, reply + shakeHand, request, reply } diff --git a/src/main/java/org/bdware/sc/udp/UDPNode.java b/src/main/java/org/bdware/sc/udp/UDPNode.java index cc119c5..2a1d16b 100644 --- a/src/main/java/org/bdware/sc/udp/UDPNode.java +++ b/src/main/java/org/bdware/sc/udp/UDPNode.java @@ -5,16 +5,16 @@ import java.net.SocketAddress; import org.bdware.sc.conn.Node; public class UDPNode extends Node { - public UDPNode(SocketAddress addr) { - this.addr = addr; - } + public UDPNode(SocketAddress addr) { + this.addr = addr; + } - public int id; - public long lastUpdatedTime; - public SocketAddress addr; + public int id; + public long lastUpdatedTime; + public SocketAddress addr; - public String getAddr() { - InetSocketAddress inet4 = (InetSocketAddress) addr; - return inet4.getHostString() + ":" + inet4.getPort(); - } + public String getAddr() { + InetSocketAddress inet4 = (InetSocketAddress) addr; + return inet4.getHostString() + ":" + inet4.getPort(); + } } diff --git a/src/main/java/org/bdware/server/NodeCenterServer.java b/src/main/java/org/bdware/server/NodeCenterServer.java index 052c66c..c0a3d6d 100644 --- a/src/main/java/org/bdware/server/NodeCenterServer.java +++ b/src/main/java/org/bdware/server/NodeCenterServer.java @@ -53,17 +53,15 @@ public class NodeCenterServer { Executors.newScheduledThreadPool(8); static SslContext sslContext = null; public static URLClassLoader pluginLoader; -// static byte[] delimiter = "wonbifoodie".getBytes(); + // static byte[] delimiter = "wonbifoodie".getBytes(); static { KeyValueDBUtil.setupNC(); -// TimeDBUtil.setupNC(); + // TimeDBUtil.setupNC(); TimeDBUtil.setupNC(); - Configurator.setLevel( - "io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder", + Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder", Level.OFF); - Configurator.setLevel( - "io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder", + Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder", Level.OFF); } @@ -102,7 +100,7 @@ public class NodeCenterServer { private static String[] parseStrAsList(String str) { if (str == null) { - return new String[]{}; + return new String[] {}; } return str.split(","); } @@ -125,18 +123,18 @@ public class NodeCenterServer { try { BufferedReader br = new BufferedReader(new FileReader(keyFile)); String pubKey = br.readLine(); - String nowManager = - KeyValueDBUtil.instance.getValue( - NCTables.ConfigDB.toString(), NCManagerAction.centerManger); + String nowManager = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), + NCManagerAction.centerManger); // manager.key is used when node manager isn' set if (null == nowManager || nowManager.isEmpty()) { - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), NCManagerAction.centerManger, pubKey); - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), NCManagerAction.clusterName, "clusterName_" + pubKey.substring(0, 5)); - KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, Role.CenterManager.toString()); - KeyValueDBUtil.instance.setValue( - NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), + NCManagerAction.centerManger, pubKey); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), + NCManagerAction.clusterName, "clusterName_" + pubKey.substring(0, 5)); + KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, + Role.CenterManager.toString()); + KeyValueDBUtil.instance.setValue(NCTables.NodeTime.toString(), pubKey, + Long.toString(new Date().getTime())); LOGGER.info("set node manager from manager.key"); } } catch (IOException ignored) { @@ -148,7 +146,7 @@ public class NodeCenterServer { OtherNCProxy.instance.init(); startHttp(cmf.servicePort); - //System.out.println("PORT"+cmf.servicePort); + // System.out.println("PORT"+cmf.servicePort); } public static void listenSocket(int port) throws Exception { @@ -157,25 +155,21 @@ public class NodeCenterServer { EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .option(ChannelOption.SO_BACKLOG, 100) - .localAddress(port) - .childHandler( - new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel arg0) { - arg0.pipeline() - .addLast(new DelimiterCodec()) - .addLast(new NodeCenterFrameHandler()); - } - }); + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .option(ChannelOption.SO_BACKLOG, 100).localAddress(port) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel arg0) { + arg0.pipeline().addLast(new DelimiterCodec()) + .addLast(new NodeCenterFrameHandler()); + } + }); b.bind(port).sync().channel(); } public static void startHttp(int port) { - File[] pluginJar = new File("./pluginLib/") - .listFiles(pathname -> pathname.getName().endsWith(".jar")); + File[] pluginJar = + new File("./pluginLib/").listFiles(pathname -> pathname.getName().endsWith(".jar")); URL[] urls; if (pluginJar != null && pluginJar.length > 0) { urls = new URL[pluginJar.length]; @@ -188,7 +182,7 @@ public class NodeCenterServer { } } } else { - urls = new URL[]{}; + urls = new URL[] {}; } pluginLoader = new URLClassLoader(urls, NodeCenterServer.class.getClassLoader()); LOGGER.info("start at: " + port); @@ -199,9 +193,7 @@ public class NodeCenterServer { ServerBootstrap b = new ServerBootstrap(); ControlledChannelInitializer initializer = new ControlledChannelInitializer(); b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); - b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) - .localAddress(port) + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(initializer); final Channel ch = b.bind(port).sync().channel(); ch.closeFuture().sync(); @@ -233,12 +225,9 @@ public class NodeCenterServer { } else { LOGGER.warn("disable ssl"); } - arg0.pipeline() - .addLast(new HttpServerCodec()) - .addLast(new HttpObjectAggregator(65536)) + arg0.pipeline().addLast(new HttpServerCodec()).addLast(new HttpObjectAggregator(65536)) .addLast(new WebSocketServerProtocolHandler(PATH, null, true)) - .addLast(new ChunkedWriteHandler()) - .addLast(handler) + .addLast(new ChunkedWriteHandler()).addLast(handler) .addLast(new NodeCenterWSFrameHandler()); } } diff --git a/src/main/java/org/bdware/server/action/DistributeCallback.java b/src/main/java/org/bdware/server/action/DistributeCallback.java index 7cd0833..a6f2c46 100644 --- a/src/main/java/org/bdware/server/action/DistributeCallback.java +++ b/src/main/java/org/bdware/server/action/DistributeCallback.java @@ -16,15 +16,16 @@ public class DistributeCallback extends ResultCallback { private static final Logger LOGGER = LogManager.getLogger(DistributeCallback.class); String sponsorPubkey;// 发起节点的 Map nodes; - String pubKey; //发起节点的用户公钥 + String pubKey; // 发起节点的用户公钥 String signature; // 发起节点的 int count; // 集群中几个节点已成功启动 ResultCallback res; // 返回给Node String fileName; // ypk的文件名 int index; - String distributeID; //发起节点的分发id + String distributeID; // 发起节点的分发id - public DistributeCallback(String ss, String sponID, Map n, ResultCallback re, String p, String s) { + public DistributeCallback(String ss, String sponID, Map n, ResultCallback re, + String p, String s) { distributeID = ss; count = 0; index = 1; @@ -67,14 +68,10 @@ public class DistributeCallback extends ResultCallback { public void onDistribute(String progress, String nodeIP) { Map ret2 = new HashMap<>(); ret2.put("action", "onDistributeContract"); - ret2.put( - "progress", - "NC is sending ypk to NO." - + index - + " node in the units,progress is " - + progress - + "%."); - if (progress.equals("100.00")) index++; + ret2.put("progress", "NC is sending ypk to NO." + index + " node in the units,progress is " + + progress + "%."); + if (progress.equals("100.00")) + index++; Map map = new HashMap<>(); map.put("action", "onDistribute"); @@ -111,7 +108,7 @@ public class DistributeCallback extends ResultCallback { map_send.put("nodeIP", nodeIP); map_send.put("content", JsonUtil.toJson(ret2)); res.onResult(JsonUtil.toJson(map_send)); - //NC delete file + // NC delete file File file = new File("./temp/stateFiles/" + fileName); if (file.exists() && file.isFile()) file.delete(); @@ -123,27 +120,27 @@ public class DistributeCallback extends ResultCallback { public void onResult(String str) { LOGGER.info("[DistributeCallback] str=" + str); - Map map = JsonUtil.fromJson(str, new TypeToken>() { - }.getType()); + Map map = + JsonUtil.fromJson(str, new TypeToken>() {}.getType()); NodeCenterActions.sync.sleepWithTimeout(map.get("requestID"), this, 60); String operation = map.get("operation"); switch (operation) { - //上传到nodecenter + // 上传到nodecenter case "NCreceive": NCreceive(map.get("progress")); break; - //开始分发给各个节点 + // 开始分发给各个节点 case "distribute": distributeContractProject(map.get("receiveFileName"), map.get("isPrivate")); break; - //分发给各个节点 + // 分发给各个节点 case "onDistribute": onDistribute(map.get("progress"), map.get("nodeIP")); break; - //各个节点返回收到 + // 各个节点返回收到 case "onReceive": onReceive(map); break; diff --git a/src/main/java/org/bdware/server/action/RequestAllExecutor.java b/src/main/java/org/bdware/server/action/RequestAllExecutor.java index 1ac3550..ec62508 100644 --- a/src/main/java/org/bdware/server/action/RequestAllExecutor.java +++ b/src/main/java/org/bdware/server/action/RequestAllExecutor.java @@ -25,27 +25,26 @@ public class RequestAllExecutor implements ContractExecutor { resultCount = count; } - public ResultCallback createResultCallback( - final String requestID, final ResultCallback originalCb, int count, String contractID) { + public ResultCallback createResultCallback(final String requestID, + final ResultCallback originalCb, int count, String contractID) { return new Collector(requestID, originalCb, count, contractID); } - //only for test ADSP -// public ResultCallback createResultCallback( -// final String requestID, final ResultCallback originalCb, int count,String contractID,String seq) { -// ResultCallback collector = new Collector(requestID, originalCb, count,contractID,seq); -// return collector; -// } + // only for test ADSP + // public ResultCallback createResultCallback( + // final String requestID, final ResultCallback originalCb, int count,String contractID,String + // seq) { + // ResultCallback collector = new Collector(requestID, originalCb, count,contractID,seq); + // return collector; + // } public void sendRequest(String req, ResultCallback collector) { List nodes = info.members; for (String node : nodes) { CMNode cmNode = NodeCenterActions.nodeInfos.get(node); if (cmNode == null) { - collector.onResult( - "{\"status\":\"Error\",\"result\":\"node " - + node - + " offline\",\"action\":\"onExecuteContractTrustfully\"}"); + collector.onResult("{\"status\":\"Error\",\"result\":\"node " + node + + " offline\",\"action\":\"onExecuteContractTrustfully\"}"); } else { cmNode.connection.controller.sendMsg(req); } @@ -57,12 +56,13 @@ public class RequestAllExecutor implements ContractExecutor { JsonObject jo2 = JsonParser.parseString(req).getAsJsonObject(); String id = jo2.get("contractID").getAsString(); - //only for test ADSP -// ResultCallback collector = createResultCallback(requestID, rc, resultCount,id,jo2.get("seq").getAsString()); + // only for test ADSP + // ResultCallback collector = createResultCallback(requestID, rc, + // resultCount,id,jo2.get("seq").getAsString()); ResultCallback collector = createResultCallback(requestID, rc, resultCount, id); - //TODO NC.sync?有问题? + // TODO NC.sync?有问题? NodeCenterActions.sync.sleep(requestID, collector); sendRequest(req, collector); } @@ -74,20 +74,20 @@ public class RequestAllExecutor implements ContractExecutor { AtomicInteger count = new AtomicInteger(0); - //only for testADSP -// boolean flag = true; //true表示还没在文件中记录这次调用结果 -// String seq; -// Map res = new ConcurrentHashMap(); + // only for testADSP + // boolean flag = true; //true表示还没在文件中记录这次调用结果 + // String seq; + // Map res = new ConcurrentHashMap(); - //only for test ADSP -// public Collector(String reqID, ResultCallback callback, int c,String id,String seq2) { -// total = c; -// requestID = reqID; -// commiter = callback; -// contractID = id; -// seq = seq2; -// } + // only for test ADSP + // public Collector(String reqID, ResultCallback callback, int c,String id,String seq2) { + // total = c; + // requestID = reqID; + // commiter = callback; + // contractID = id; + // seq = seq2; + // } ResultCallback committer; public Collector(String reqID, ResultCallback callback, int c, String id) { @@ -105,7 +105,7 @@ public class RequestAllExecutor implements ContractExecutor { obj.addProperty("seqNum", count.getAndIncrement()); obj.addProperty("total", total); - //NodeCenterActions.recoverMap.get(nodePubKey).get(contractID).lastExeSeq++; + // NodeCenterActions.recoverMap.get(nodePubKey).get(contractID).lastExeSeq++; if (committer != null) committer.onResult(obj.toString()); @@ -125,47 +125,45 @@ public class RequestAllExecutor implements ContractExecutor { String data = obj.get("data").getAsString(); ContractResult cr = JsonUtil.fromJson(data, ContractResult.class); - //only for testADSP -// if(TestADSP.getFlag()){ -// String r = cr.result; -// if(!res.containsKey(r)){ -// res.put(r,0); -// } -// int former = res.get(r); -// res.put(r,++former); -// for(String s : res.keySet()){ -// if(res.get(s) > total / 2 && flag){ -// //写入文件 -// File file = new File(TestADSP.resultPath); -// synchronized (file){ -// try { -// FileWriter fw = new FileWriter(file, true); -// PrintWriter pw = new PrintWriter(fw); -// pw.println(seq + " " + r); -// pw.flush(); -// fw.flush(); -// pw.close(); -// fw.close(); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// flag = false; -// } -// } -// } + // only for testADSP + // if(TestADSP.getFlag()){ + // String r = cr.result; + // if(!res.containsKey(r)){ + // res.put(r,0); + // } + // int former = res.get(r); + // res.put(r,++former); + // for(String s : res.keySet()){ + // if(res.get(s) > total / 2 && flag){ + // //写入文件 + // File file = new File(TestADSP.resultPath); + // synchronized (file){ + // try { + // FileWriter fw = new FileWriter(file, true); + // PrintWriter pw = new PrintWriter(fw); + // pw.println(seq + " " + r); + // pw.flush(); + // fw.flush(); + // pw.close(); + // fw.close(); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // } + // flag = false; + // } + // } + // } -/* if (cr.status == ContractResult.Status.Error) { //TODO 规范Status的使用 改成 ==Statuc.Error才恢复 - String nodePubKey = obj.get("nodeID").getAsString(); - if(NodeCenterActions.recoverMap.containsKey(nodePubKey)){ - ContractRecord record = NodeCenterActions.recoverMap.get(nodePubKey).get(contractID); - if(record.recoverFlag == RecoverFlag.Fine){ - record.recoverFlag = RecoverFlag.ToRecover; - MasterActions.restartContracts(nodePubKey); - } - } - }*/ + /* + * if (cr.status == ContractResult.Status.Error) { //TODO 规范Status的使用 改成 + * ==Statuc.Error才恢复 String nodePubKey = obj.get("nodeID").getAsString(); + * if(NodeCenterActions.recoverMap.containsKey(nodePubKey)){ ContractRecord record = + * NodeCenterActions.recoverMap.get(nodePubKey).get(contractID); + * if(record.recoverFlag == RecoverFlag.Fine){ record.recoverFlag = + * RecoverFlag.ToRecover; MasterActions.restartContracts(nodePubKey); } } } + */ } } } diff --git a/src/main/java/org/bdware/server/action/RequestOnceExecutor.java b/src/main/java/org/bdware/server/action/RequestOnceExecutor.java index d9b8a30..011a98a 100644 --- a/src/main/java/org/bdware/server/action/RequestOnceExecutor.java +++ b/src/main/java/org/bdware/server/action/RequestOnceExecutor.java @@ -26,18 +26,17 @@ public class RequestOnceExecutor implements ContractExecutor { JsonObject jo2 = (new JsonParser()).parse(req).getAsJsonObject(); String contractID = jo2.get("contractID").getAsString(); - ResultCallback cb = - new ResultCallback() { - @Override - public void onResult(String str) { - LOGGER.debug(str); - JsonObject jo = (new JsonParser()).parse(str).getAsJsonObject(); - jo.remove("action"); - jo.addProperty("action", "onExecuteResult"); - LOGGER.debug(jo.toString()); - rc.onResult(jo.toString()); - } - }; + ResultCallback cb = new ResultCallback() { + @Override + public void onResult(String str) { + LOGGER.debug(str); + JsonObject jo = (new JsonParser()).parse(str).getAsJsonObject(); + jo.remove("action"); + jo.addProperty("action", "onExecuteResult"); + LOGGER.debug(jo.toString()); + rc.onResult(jo.toString()); + } + }; NodeCenterActions.sync.sleep(requestID, cb); for (int i = 0; i < info.members.size(); i++) { int size = info.members.size(); @@ -49,8 +48,7 @@ public class RequestOnceExecutor implements ContractExecutor { return; } } - rc.onResult( - "{\"status\":\"Error\",\"result\":\"all nodes " - + " offline\",\"action\":\"onExecuteContract\"}"); + rc.onResult("{\"status\":\"Error\",\"result\":\"all nodes " + + " offline\",\"action\":\"onExecuteContract\"}"); } } diff --git a/src/main/java/org/bdware/server/action/ResponseOnceExecutor.java b/src/main/java/org/bdware/server/action/ResponseOnceExecutor.java index c195414..fdba177 100644 --- a/src/main/java/org/bdware/server/action/ResponseOnceExecutor.java +++ b/src/main/java/org/bdware/server/action/ResponseOnceExecutor.java @@ -32,31 +32,30 @@ public class ResponseOnceExecutor implements ContractExecutor { JsonObject jo2 = (new JsonParser()).parse(req).getAsJsonObject(); String contractID = jo2.get("contractID").getAsString(); // TODO 标注失效节点,是否选择重新迁移?? - ResultCallback cb = - new ResultCallback() { - @Override - public void onResult(String str) { - LOGGER.debug(str); - JsonObject jo = (new JsonParser()).parse(str).getAsJsonObject(); - jo.remove("action"); - jo.addProperty("action", "onExecuteResult"); - LOGGER.debug(jo.toString()); - if (jo.has("data")) { - String data = jo.get("data").getAsString(); - ContractResult cr = JsonUtil.fromJson(data, ContractResult.class); - if (cr.status != ContractResult.Status.Success && count > 0) { - executeInternal(requestID, rc, req, count - 1); - } else - rc.onResult(jo.toString()); - } else { - JsonObject jo2 = new JsonObject(); - jo2.addProperty("action", "onExecuteResult"); - jo.remove("action"); - jo2.addProperty("data", jo.toString()); - rc.onResult(jo2.toString()); - } - } - }; + ResultCallback cb = new ResultCallback() { + @Override + public void onResult(String str) { + LOGGER.debug(str); + JsonObject jo = (new JsonParser()).parse(str).getAsJsonObject(); + jo.remove("action"); + jo.addProperty("action", "onExecuteResult"); + LOGGER.debug(jo.toString()); + if (jo.has("data")) { + String data = jo.get("data").getAsString(); + ContractResult cr = JsonUtil.fromJson(data, ContractResult.class); + if (cr.status != ContractResult.Status.Success && count > 0) { + executeInternal(requestID, rc, req, count - 1); + } else + rc.onResult(jo.toString()); + } else { + JsonObject jo2 = new JsonObject(); + jo2.addProperty("action", "onExecuteResult"); + jo.remove("action"); + jo2.addProperty("data", jo.toString()); + rc.onResult(jo2.toString()); + } + } + }; NodeCenterActions.sync.sleepWithTimeout(requestID, cb, 5); if (!sendOnce(req)) rc.onResult( diff --git a/src/main/java/org/bdware/server/nodecenter/CMNode.java b/src/main/java/org/bdware/server/nodecenter/CMNode.java index aea1042..9b47a30 100644 --- a/src/main/java/org/bdware/server/nodecenter/CMNode.java +++ b/src/main/java/org/bdware/server/nodecenter/CMNode.java @@ -32,8 +32,8 @@ public class CMNode { contracts = cons; contractVersion = version; for (ContractDesp desp : cons) { - KeyValueDBUtil.instance.setValue( - NCTables.ContractMeta.toString(), desp.contractID, desp.contractName); + KeyValueDBUtil.instance.setValue(NCTables.ContractMeta.toString(), desp.contractID, + desp.contractName); } } @@ -53,7 +53,8 @@ public class CMNode { if (null != contracts) { for (ContractDesp desp : contracts) { if (desp.contractID.equals(contractIDOrName) - || desp.contractName.equals(contractIDOrName)) return true; + || desp.contractName.equals(contractIDOrName)) + return true; } } return false; @@ -79,14 +80,15 @@ public class CMNode { } try { if (!connection.controller.isOpen()) { - LOGGER.info("node " + nodeName + "(" + pubKey.substring(0, 5) + ") may be offline!"); -// System.out.println( -// new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") -// .format(new Date(System.currentTimeMillis())) -// + "[ADSP][CMNode] isAlive : " -// + nodeName -// + " not open!" -// + "\n"); + LOGGER.info( + "node " + nodeName + "(" + pubKey.substring(0, 5) + ") may be offline!"); + // System.out.println( + // new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + // .format(new Date(System.currentTimeMillis())) + // + "[ADSP][CMNode] isAlive : " + // + nodeName + // + " not open!" + // + "\n"); return doubleCheckAlive(); } else { return true; @@ -116,10 +118,8 @@ public class CMNode { LOGGER.debug("removeCIManager" + string); int start = this.cimanager.indexOf(string); if (start > 0) { - this.cimanager = - this.cimanager - .substring(0, start) - .concat(this.cimanager.substring(start + 130)); + this.cimanager = this.cimanager.substring(0, start) + .concat(this.cimanager.substring(start + 130)); } } diff --git a/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java b/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java index 3cbf5cf..0b61133 100644 --- a/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java +++ b/src/main/java/org/bdware/server/nodecenter/CheckAgentAliveTimer.java @@ -34,14 +34,9 @@ public class CheckAgentAliveTimer extends TimerTask { 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); + // 注意 选举不通过NC的机制触发。 + LOGGER.info("checkAlive---- 设置节点 " + info.pubKey.substring(0, 5) + " 的合约 " + + cd.contractID + " isMaster=" + false); } } } diff --git a/src/main/java/org/bdware/server/nodecenter/ContractExecutor.java b/src/main/java/org/bdware/server/nodecenter/ContractExecutor.java index 86a20d8..05487fe 100644 --- a/src/main/java/org/bdware/server/nodecenter/ContractExecutor.java +++ b/src/main/java/org/bdware/server/nodecenter/ContractExecutor.java @@ -1,6 +1,6 @@ package org.bdware.server.nodecenter; - import org.bdware.sc.conn.ResultCallback; +import org.bdware.sc.conn.ResultCallback; public interface ContractExecutor { public void execute(String requestID, ResultCallback rc, String req); diff --git a/src/main/java/org/bdware/server/nodecenter/ElectMasterTimeRecorder.java b/src/main/java/org/bdware/server/nodecenter/ElectMasterTimeRecorder.java index 4b19b3e..42162f5 100644 --- a/src/main/java/org/bdware/server/nodecenter/ElectMasterTimeRecorder.java +++ b/src/main/java/org/bdware/server/nodecenter/ElectMasterTimeRecorder.java @@ -4,12 +4,12 @@ import java.util.HashMap; import java.util.Map; -//NC +// NC public class ElectMasterTimeRecorder { - public static Long startElect; //NC开始选举 - public static Long findNewMaster; //NC选出新master + public static Long startElect; // NC开始选举 + public static Long findNewMaster; // NC选出新master public static String newMaster; - public static Long changeMasterFinish; //新master在NC上更新路由信息 + public static Long changeMasterFinish; // 新master在NC上更新路由信息 - public static Map nodeFindMasterCrash = new HashMap(); //nodeID,electMaster调用时间 + public static Map nodeFindMasterCrash = new HashMap(); // nodeID,electMaster调用时间 } diff --git a/src/main/java/org/bdware/server/nodecenter/FileActions.java b/src/main/java/org/bdware/server/nodecenter/FileActions.java index 10acf30..ca1cd11 100644 --- a/src/main/java/org/bdware/server/nodecenter/FileActions.java +++ b/src/main/java/org/bdware/server/nodecenter/FileActions.java @@ -49,9 +49,7 @@ public class FileActions { return ret != null && ret.equals("locked"); } - @URIPath( - method = org.bdware.server.http.HttpMethod.POST, - value = {"/upload"}) + @URIPath(method = org.bdware.server.http.HttpMethod.POST, value = {"/upload"}) public static void handleUploadRequest(ChannelHandlerContext ctx, FullHttpRequest request) { LOGGER.info("[FileActions] handleUploadRequest : "); // Upload method is POST @@ -65,18 +63,12 @@ public class FileActions { } } - if (!transformedParam.containsKey("fileName") - || !transformedParam.containsKey("order") - || !transformedParam.containsKey("count") - || !transformedParam.containsKey("pubKey") + if (!transformedParam.containsKey("fileName") || !transformedParam.containsKey("order") + || !transformedParam.containsKey("count") || !transformedParam.containsKey("pubKey") || !transformedParam.containsKey("sign")) { - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), - OK, - Unpooled.wrappedBuffer( - "{\"status\":\"false\",\"data\":\"Missing argument!\"}" - .getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer( + "{\"status\":\"false\",\"data\":\"Missing argument!\"}".getBytes())); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); @@ -84,10 +76,8 @@ public class FileActions { } // 验签 - // HttpMethod method = request.method(); - String uri = - URLDecoder.decode(request.uri()) - .split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey,否则验签失败 + // HttpMethod method = request.method(); + String uri = URLDecoder.decode(request.uri()).split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey,否则验签失败 int index = uri.lastIndexOf('&'); String str = uri.substring(0, index); @@ -115,9 +105,8 @@ public class FileActions { Method mm; Action a = null; try { - mm = - FileActions.class.getDeclaredMethod( - "uploadFile", JsonObject.class, ResultCallback.class); + mm = FileActions.class.getDeclaredMethod("uploadFile", JsonObject.class, + ResultCallback.class); a = mm.getAnnotation(Action.class); } catch (SecurityException | NoSuchMethodException e1) { // TODO Auto-generated catch block @@ -147,28 +136,17 @@ public class FileActions { status = "accept"; } - TimeDBUtil.instance.put( - NCTables.NodeHttpLog.toString(), - "{\"action\":\"" - + action - + "\",\"pubKey\":\"" - + transformedParam.get("pubKey") - + "\",\"status\":\"" - + status - + "\",\"date\":" - + System.currentTimeMillis() + TimeDBUtil.instance.put(NCTables.NodeHttpLog.toString(), + "{\"action\":\"" + action + "\",\"pubKey\":\"" + transformedParam.get("pubKey") + + "\",\"status\":\"" + status + "\",\"date\":" + System.currentTimeMillis() + "}"); // logger.info("[CMHttpHandler] flag = " + flag + " flag2 = " + flag2); if (!flag || !flag2) { - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), - OK, - Unpooled.wrappedBuffer( - "{\"status\":\"false\",\"data\":\"Permission denied!\"}" - .getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer( + "{\"status\":\"false\",\"data\":\"Permission denied!\"}".getBytes())); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); @@ -191,27 +169,24 @@ public class FileActions { } File target = new File(dir, fileName); if (fileName.contains("..")) { - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), - OK, - Unpooled.wrappedBuffer( - "{\"status\":\"false\",\"data\":\"FileName illegal!\"}" - .getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer( + "{\"status\":\"false\",\"data\":\"FileName illegal!\"}".getBytes())); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); return; } - // LOGGER.info(request.refCnt()); - // httpDecoder.offer(request); - // LOGGER.info(request.refCnt()); + // LOGGER.info(request.refCnt()); + // httpDecoder.offer(request); + // LOGGER.info(request.refCnt()); FileOutputStream fout; if (order == 0) { try { LOGGER.debug("Path:" + target.getAbsolutePath()); - if (!target.getParentFile().exists()) target.getParentFile().mkdirs(); + if (!target.getParentFile().exists()) + target.getParentFile().mkdirs(); fout = new FileOutputStream(target, false); fileMap.put(target.getAbsolutePath(), fout); } catch (FileNotFoundException e) { @@ -232,22 +207,21 @@ public class FileActions { } fileMap.remove(target.getAbsolutePath()); updateTime.remove(target.getAbsolutePath()); -// String doi = unzipIfYpk(target, dir); + // String doi = unzipIfYpk(target, dir); String doi = null; if (null != doi) { retStr = retStr.replaceFirst("null", doi); } } - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), OK, Unpooled.wrappedBuffer(retStr.getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer(retStr.getBytes())); fullResponse.headers().add("Access-Control-Allow-Origin", "*"); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); } - private static void writeChunk( - ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder, File file) { + private static void writeChunk(ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder, + File file) { try { if (!file.exists()) { file.createNewFile(); @@ -274,19 +248,14 @@ public class FileActions { @URIPath(method = HttpMethod.OPTIONS) public static void crossOrigin(ChannelHandlerContext ctx, FullHttpRequest request) { - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), - OK, - Unpooled.wrappedBuffer("success".getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer("success".getBytes())); fullResponse.headers().remove("Access-Control-Allow-Origin"); fullResponse.headers().remove("Access-Control-Allow-Headers"); fullResponse.headers().add("Access-Control-Allow-Origin", "*"); - fullResponse - .headers() - .add("Access-Control-Allow-Headers", - "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + - "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection"); + fullResponse.headers().add("Access-Control-Allow-Headers", + "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + + "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection"); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); LOGGER.info("[OOOOOOOOption] received!"); @@ -338,10 +307,8 @@ public class FileActions { // 文本文件 if (!path.contains("..") && isTextFile(path)) { LOGGER.debug("[FileActions] 上传文本文件类型 : "); - BufferedWriter bw = - new BufferedWriter( - new FileWriter( - target.getAbsolutePath() + "/" + fileName, isAppend)); + BufferedWriter bw = new BufferedWriter( + new FileWriter(target.getAbsolutePath() + "/" + fileName, isAppend)); bw.write(content); bw.close(); } else { // 其他类型文件 @@ -426,10 +393,7 @@ public class FileActions { File f = new File(parPath + "/" + oldFile); if (!oldFile.contains("..") && f.exists()) { LOGGER.info( - "[FileController] delete:" - + f.getAbsolutePath() - + " exists:" - + f.exists()); + "[FileController] delete:" + f.getAbsolutePath() + " exists:" + f.exists()); f.delete(); } response.data = "success"; diff --git a/src/main/java/org/bdware/server/nodecenter/LogActions.java b/src/main/java/org/bdware/server/nodecenter/LogActions.java index 526d5fd..dbbce38 100644 --- a/src/main/java/org/bdware/server/nodecenter/LogActions.java +++ b/src/main/java/org/bdware/server/nodecenter/LogActions.java @@ -7,7 +7,6 @@ import org.apache.logging.log4j.Logger; import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.MultiIndexTimeDBUtilIntf; -import org.bdware.sc.util.JsonUtil; import org.bdware.server.NodeCenterServer; import org.bdware.server.action.Action; @@ -33,10 +32,7 @@ public class LogActions { resultCallback.onResult(jsonResult); } - private void queryInternal( - String actResp, - MultiIndexTimeDBUtilIntf db, - JsonObject json, + private void queryInternal(String actResp, MultiIndexTimeDBUtilIntf db, JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); if (!json.has("start")) { @@ -48,26 +44,25 @@ public class LogActions { if (json.has("end")) { endTime = json.get("end").getAsLong(); } - String[] category = new String[]{null}; + String[] category = new String[] {null}; if (json.has("category")) { category = json.get("category").getAsString().split(","); } Map data = new HashMap<>(); for (String str : category) { List array = db.queryByDateAsJson(str, startTime, endTime); - if (str == null) data.put("primary", array); - else data.put(str, array); + if (str == null) + data.put("primary", array); + else + data.put(str, array); } simpleReply(resultCallback, actResp, data); long end = System.currentTimeMillis(); LOGGER.debug("[queryInternal:time]" + (end - start)); } - private void countLogByCategoryInternal( - String actResp, - MultiIndexTimeDBUtilIntf db, - JsonObject json, - ResultCallback resultCallback) { + private void countLogByCategoryInternal(String actResp, MultiIndexTimeDBUtilIntf db, + JsonObject json, ResultCallback resultCallback) { if (!json.has("start")) { simpleReply(resultCallback, actResp, new ArrayList<>()); return; @@ -78,15 +73,17 @@ public class LogActions { endTime = json.get("end").getAsLong(); } long interval = json.get("interval").getAsLong(); - String[] category = new String[]{null}; + String[] category = new String[] {null}; if (json.has("category")) { category = json.get("category").getAsString().split(","); } JsonObject data = new JsonObject(); for (String str : category) { JsonArray array = db.countInInterval(str, startTime, interval, endTime); - if (str == null) data.add("primary", array); - else data.add(str, array); + if (str == null) + data.add("primary", array); + else + data.add(str, array); } simpleReply(resultCallback, actResp, data); } @@ -98,8 +95,8 @@ public class LogActions { @Action(userPermission = 1 << 8, async = true) public void countActionLogByCategory(JsonObject json, ResultCallback resultCallback) { - countLogByCategoryInternal( - "onCountActionLogByCategory", NodeCenterServer.nodeHttpLogDB, json, resultCallback); + countLogByCategoryInternal("onCountActionLogByCategory", NodeCenterServer.nodeHttpLogDB, + json, resultCallback); } @Action(userPermission = 1 << 8, async = true) @@ -109,8 +106,8 @@ public class LogActions { @Action(userPermission = 1 << 8, async = true) public void countCMLogByCategory(JsonObject json, ResultCallback resultCallback) { - countLogByCategoryInternal( - "onCountCMLogByCategory", NodeCenterServer.nodeTcpLogDB, json, resultCallback); + countLogByCategoryInternal("onCountCMLogByCategory", NodeCenterServer.nodeTcpLogDB, json, + resultCallback); } @Action(userPermission = 1 << 9) @@ -138,8 +135,7 @@ public class LogActions { List onlineNodes = new ArrayList<>(); Map ret = new HashMap<>(); - if (KeyValueDBUtil.instance - .getValue(NCTables.ConfigDB.toString(), "__CenterManager__") + if (KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), "__CenterManager__") .contains(pubKey)) { LOGGER.debug("is center manager"); LOGGER.debug("dbnodes " + dbnodes.toString()); diff --git a/src/main/java/org/bdware/server/nodecenter/MasterActions.java b/src/main/java/org/bdware/server/nodecenter/MasterActions.java index 3e7a133..b976c96 100644 --- a/src/main/java/org/bdware/server/nodecenter/MasterActions.java +++ b/src/main/java/org/bdware/server/nodecenter/MasterActions.java @@ -11,25 +11,25 @@ import java.util.concurrent.TimeUnit; public class MasterActions { private static final Logger LOGGER = LogManager.getLogger(MasterActions.class); - // TODO 定期清缓存 - public static Map requestCache = - new ConcurrentHashMap<>(); // key is contractID,only for requestAll type contract + // TODO 定期清缓存 + public static Map requestCache = new ConcurrentHashMap<>(); // key is + // contractID,only + // for + // requestAll + // type + // contract static { - 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); + 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); } public NodeCenterFrameHandler controller; @@ -39,213 +39,184 @@ public class MasterActions { } // judge the just online node whwther need to restart contracts,send the contracts'info - /* public static void restartContracts(String nodePubKey){ - if(!NodeCenterActions.recoverMap.containsKey(nodePubKey)){ - return; - } - - System.out.println("开始恢复节点" + NodeCenterActions.nodeinfos.get(nodePubKey).nodeName); - - - // 恢复该节点的每一个集群运行的合约 - for (ContractRecord record : NodeCenterActions.recoverMap.get(nodePubKey).values()) { - String contractID = record.contractID; - if (record.recoverFlag != RecoverFlag.ToRecover) - continue; - - Map req = new HashMap(); - req.put("action", "queryUnitStatus"); - req.put("contractID", contractID); - CMNode cmNode = NodeCenterActions.nodeinfos.get(nodePubKey); - cmNode.connection.controller.sendMsg(gson.toJson(req)); - } - }*/ - - /* @Action(async = true) - public void onQueryUnitStatus(Map args, final ResultCallback rc) { - String mode = args.get("mode"); - String nodeID = args.get("nodeID"); - String contractID = args.get("contractID"); - ContractRecord record = NodeCenterActions.recoverMap.get(nodeID).get(contractID); - logger.debug("节点" + NodeCenterActions.nodeinfos.get(args.get("nodeID")).nodeName + "崩溃前模式为" + mode); - if(mode.equals(ContractUnitStatus.CommonMode.toString())){ - restartFromCommonMode(nodeID,record); - }else if(mode.equals(ContractUnitStatus.StableMode.toString())){ - restartFromStableMode(nodeID,record); - } - }*/ - - // 当StableMode的节点的lastExeSeq和集群相差超过10时,通过CommonNode的恢复方式恢复 - /* @Action(async = true) - public void restartByCommonNode(Map args, final ResultCallback rc){ - String nodeID = args.get("nodeID"); - String contractID = args.get("contractID"); - ContractRecord record = NodeCenterActions.recoverMap.get(nodeID).get(contractID); - restartFromCommonMode(nodeID,record); - }*/ + /* + * public static void restartContracts(String nodePubKey){ + * if(!NodeCenterActions.recoverMap.containsKey(nodePubKey)){ return; } + * + * System.out.println("开始恢复节点" + NodeCenterActions.nodeinfos.get(nodePubKey).nodeName); + * + * + * // 恢复该节点的每一个集群运行的合约 for (ContractRecord record : + * NodeCenterActions.recoverMap.get(nodePubKey).values()) { String contractID = + * record.contractID; if (record.recoverFlag != RecoverFlag.ToRecover) continue; + * + * Map req = new HashMap(); req.put("action", + * "queryUnitStatus"); req.put("contractID", contractID); CMNode cmNode = + * NodeCenterActions.nodeinfos.get(nodePubKey); + * cmNode.connection.controller.sendMsg(gson.toJson(req)); } } + */ /* - public static void restartFromCommonMode(String nodePubKey,ContractRecord record){ - logger.debug("从CommonMode中恢复:"); + * @Action(async = true) public void onQueryUnitStatus(Map args, final + * ResultCallback rc) { String mode = args.get("mode"); String nodeID = args.get("nodeID"); + * String contractID = args.get("contractID"); ContractRecord record = + * NodeCenterActions.recoverMap.get(nodeID).get(contractID); logger.debug("节点" + + * NodeCenterActions.nodeinfos.get(args.get("nodeID")).nodeName + "崩溃前模式为" + mode); + * if(mode.equals(ContractUnitStatus.CommonMode.toString())){ + * restartFromCommonMode(nodeID,record); }else + * if(mode.equals(ContractUnitStatus.StableMode.toString())){ + * restartFromStableMode(nodeID,record); } } + */ - String contractID = record.contractID; + // 当StableMode的节点的lastExeSeq和集群相差超过10时,通过CommonNode的恢复方式恢复 + /* + * @Action(async = true) public void restartByCommonNode(Map args, final + * ResultCallback rc){ String nodeID = args.get("nodeID"); String contractID = + * args.get("contractID"); ContractRecord record = + * NodeCenterActions.recoverMap.get(nodeID).get(contractID); + * restartFromCommonMode(nodeID,record); } + */ - record.recoverFlag = RecoverFlag.Recovering; - //先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests - Map request = new HashMap<>(); - request.put("action", "setRecovering"); - request.put("contractID",contractID); - CMNode node = NodeCenterActions.nodeinfos.get(nodePubKey); - node.connection.controller.sendMsg(gson.toJson(request)); - - //System.out.println("第一步 : [NodeCeterActions] restartContracts 开始处理合约 contractID=" + contractID); - - if (NodeCenterActions.contractID2Members.containsKey(contractID)) { - - // 在nodeinfos中找节点,该节点的pubKey在pubKeys中 - MultiPointContractInfo info = NodeCenterActions.contractID2Members.get(contractID); - CMNode cmNode = null; - for (int i = 0; i < info.members.size(); i++) { - int size = info.members.size(); - String tempNodeID = info.members.get(record.order.incrementAndGet() % size); - - if(NodeCenterActions.nodeinfos.containsKey(tempNodeID)) - cmNode = NodeCenterActions.nodeinfos.get(tempNodeID); - else continue; - - //System.out.println("查询节点 " + cmNode.nodeName); - - if (cmNode != null && !cmNode.pubKey.equals(nodePubKey)) { - //System.out.println("第二步 : [NodeCenterActions] 找到一个依赖恢复节点,其节点名为 " + cmNode.nodeName); - - Map req = new HashMap(); - req.put("action", "dumpCurrentState"); - req.put("contractID", contractID); - req.put("targetNodePubkey", nodePubKey); - - // NC向该节点发送请求,让其存储自身当前状态并发给NC - cmNode.connection.controller.sendMsg(gson.toJson(req)); - - return; - } - } - - if(cmNode == null){ - logger.debug("[NodeCenterActions] Can't find a recover rely node!"); - } - } - } - */ + /* + * public static void restartFromCommonMode(String nodePubKey,ContractRecord record){ + * logger.debug("从CommonMode中恢复:"); + * + * String contractID = record.contractID; + * + * record.recoverFlag = RecoverFlag.Recovering; //先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests + * Map request = new HashMap<>(); request.put("action", "setRecovering"); + * request.put("contractID",contractID); CMNode node = + * NodeCenterActions.nodeinfos.get(nodePubKey); + * node.connection.controller.sendMsg(gson.toJson(request)); + * + * //System.out.println("第一步 : [NodeCeterActions] restartContracts 开始处理合约 contractID=" + + * contractID); + * + * if (NodeCenterActions.contractID2Members.containsKey(contractID)) { + * + * // 在nodeinfos中找节点,该节点的pubKey在pubKeys中 MultiPointContractInfo info = + * NodeCenterActions.contractID2Members.get(contractID); CMNode cmNode = null; for (int i = 0; i + * < info.members.size(); i++) { int size = info.members.size(); String tempNodeID = + * info.members.get(record.order.incrementAndGet() % size); + * + * if(NodeCenterActions.nodeinfos.containsKey(tempNodeID)) cmNode = + * NodeCenterActions.nodeinfos.get(tempNodeID); else continue; + * + * //System.out.println("查询节点 " + cmNode.nodeName); + * + * if (cmNode != null && !cmNode.pubKey.equals(nodePubKey)) { + * //System.out.println("第二步 : [NodeCenterActions] 找到一个依赖恢复节点,其节点名为 " + cmNode.nodeName); + * + * Map req = new HashMap(); req.put("action", + * "dumpCurrentState"); req.put("contractID", contractID); req.put("targetNodePubkey", + * nodePubKey); + * + * // NC向该节点发送请求,让其存储自身当前状态并发给NC cmNode.connection.controller.sendMsg(gson.toJson(req)); + * + * return; } } + * + * if(cmNode == null){ logger.debug("[NodeCenterActions] Can't find a recover rely node!"); } } + * } + */ // TODO - /* public static void restartFromStableMode(String nodePubkey,ContractRecord record){ - logger.debug("从StableMode中恢复:"); + /* + * public static void restartFromStableMode(String nodePubkey,ContractRecord record){ + * logger.debug("从StableMode中恢复:"); + * + * String contractID = record.contractID; record.recoverFlag = RecoverFlag.Recovering; + * //先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests Map request = new HashMap<>(); + * request.put("action", "setRecovering"); request.put("contractID",contractID); + * request.put("mode", ContractUnitStatus.StableMode.toString()); int lastExeSeq = + * NodeCenterActions.contractID2Members.get(contractID).ai.get() - 1; + * request.put("lastExeSeq",lastExeSeq + ""); CMNode node = + * NodeCenterActions.nodeinfos.get(nodePubkey); + * node.connection.controller.sendMsg(gson.toJson(request)); } + */ - String contractID = record.contractID; - record.recoverFlag = RecoverFlag.Recovering; - //先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests - Map request = new HashMap<>(); - request.put("action", "setRecovering"); - request.put("contractID",contractID); - request.put("mode", ContractUnitStatus.StableMode.toString()); - int lastExeSeq = NodeCenterActions.contractID2Members.get(contractID).ai.get() - 1; - request.put("lastExeSeq",lastExeSeq + ""); - CMNode node = NodeCenterActions.nodeinfos.get(nodePubkey); - node.connection.controller.sendMsg(gson.toJson(request)); - }*/ + /* + * public static void unitModeCheck(String contractID){ MultiPointContractInfo mpci = + * NodeCenterActions.contractID2Members.get(contractID); int total = 0,online = 0; for(String + * nodeId : mpci.members){ if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ online++; } + * total++; } + * + * logger.debug("合约" + contractID + "的集群,上线节点有" + online + "个,总共节点有" + total + + * "个. Math.ceil(total / 2)=" + Math.ceil((double)total / 2) + + * " online > Math.ceil(total / 2)" + (online > Math.ceil(total / 2)) + " mpci.unitStatus=" + + * mpci.unitStatus); if(online > Math.ceil((double)total / 2) && mpci.unitStatus == + * ContractUnitStatus.StableMode){ logger.debug("合约" + contractID + "的集群更改模式为" + + * ContractUnitStatus.CommonMode.toString()); + * + * Map req = new HashMap(); req.put("action", + * "changeUnitStatus"); req.put("contractID", contractID); + * req.put("mode",ContractUnitStatus.CommonMode.toString()); for(String nodeId : mpci.members){ + * if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ CMNode cmNode = + * NodeCenterActions.nodeinfos.get(nodeId); logger.debug("发消息给节点 " + cmNode.nodeName + " 设置合约" + + * contractID + "的集群模式为CommonMode"); cmNode.connection.controller.sendMsg(gson.toJson(req)); } } + * + * mpci.unitStatus = ContractUnitStatus.CommonMode; }else if(online <= Math.ceil((double)total / + * 2) && mpci.unitStatus == ContractUnitStatus.CommonMode){ logger.debug("合约" + contractID + + * "的集群更改模式为" + ContractUnitStatus.StableMode.toString()); + * + * Map req = new HashMap(); req.put("action", + * "changeUnitStatus"); req.put("contractID", contractID); + * req.put("mode",ContractUnitStatus.StableMode.toString()); for(String nodeId : mpci.members){ + * if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ CMNode cmNode = + * NodeCenterActions.nodeinfos.get(nodeId); Map map = + * NodeCenterActions.recoverMap.get(nodeId).get(contractID).members; + * req.put("membersStr",JsonUtil.toJson(map)); //ContractRecord's members logger.debug("发消息给节点 " + * + cmNode.nodeName + " 设置合约" + contractID + "的集群模式为StableMode"); + * cmNode.connection.controller.sendMsg(gson.toJson(req)); } } + * + * mpci.unitStatus = ContractUnitStatus.StableMode; + * + * //将ContractRecord中members发给集群中节点 + * + * } } + */ - /* public static void unitModeCheck(String contractID){ - MultiPointContractInfo mpci = NodeCenterActions.contractID2Members.get(contractID); - int total = 0,online = 0; - for(String nodeId : mpci.members){ - if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ - online++; - } - total++; - } - - logger.debug("合约" + contractID + "的集群,上线节点有" + online + "个,总共节点有" + total + "个. Math.ceil(total / 2)=" + Math.ceil((double)total / 2) + " online > Math.ceil(total / 2)" + (online > Math.ceil(total / 2)) + " mpci.unitStatus=" + mpci.unitStatus); - if(online > Math.ceil((double)total / 2) && mpci.unitStatus == ContractUnitStatus.StableMode){ - logger.debug("合约" + contractID + "的集群更改模式为" + ContractUnitStatus.CommonMode.toString()); - - Map req = new HashMap(); - req.put("action", "changeUnitStatus"); - req.put("contractID", contractID); - req.put("mode",ContractUnitStatus.CommonMode.toString()); - for(String nodeId : mpci.members){ - if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ - CMNode cmNode = NodeCenterActions.nodeinfos.get(nodeId); - logger.debug("发消息给节点 " + cmNode.nodeName + " 设置合约" + contractID + "的集群模式为CommonMode"); - cmNode.connection.controller.sendMsg(gson.toJson(req)); - } - } - - mpci.unitStatus = ContractUnitStatus.CommonMode; - }else if(online <= Math.ceil((double)total / 2) && mpci.unitStatus == ContractUnitStatus.CommonMode){ - logger.debug("合约" + contractID + "的集群更改模式为" + ContractUnitStatus.StableMode.toString()); - - Map req = new HashMap(); - req.put("action", "changeUnitStatus"); - req.put("contractID", contractID); - req.put("mode",ContractUnitStatus.StableMode.toString()); - for(String nodeId : mpci.members){ - if(NodeCenterActions.nodeinfos.containsKey(nodeId)){ - CMNode cmNode = NodeCenterActions.nodeinfos.get(nodeId); - Map map = NodeCenterActions.recoverMap.get(nodeId).get(contractID).members; - req.put("membersStr",JsonUtil.toJson(map)); //ContractRecord's members - logger.debug("发消息给节点 " + cmNode.nodeName + " 设置合约" + contractID + "的集群模式为StableMode"); - cmNode.connection.controller.sendMsg(gson.toJson(req)); - } - } - - mpci.unitStatus = ContractUnitStatus.StableMode; - - //将ContractRecord中members发给集群中节点 - - } - }*/ - - /* @Action(async = true) - public void recoverFinish(Map args, final ResultCallback rc) { - ContractRecord cr = NodeCenterActions.recoverMap.get(args.get("nodeID")).get(args.get("contractID")); - logger.debug("节点" + NodeCenterActions.nodeinfos.get(args.get("nodeID")).nodeName + "恢复完成!"); - if(cr.recoverFlag == RecoverFlag.Recovering) - cr.recoverFlag = RecoverFlag.Fine; - - logger.debug("恢复完成,需要检查合约" + args.get("contractID") + "的集群运行模式"); - unitModeCheck(args.get("contractID")); - }*/ + /* + * @Action(async = true) public void recoverFinish(Map args, final + * ResultCallback rc) { ContractRecord cr = + * NodeCenterActions.recoverMap.get(args.get("nodeID")).get(args.get("contractID")); + * logger.debug("节点" + NodeCenterActions.nodeinfos.get(args.get("nodeID")).nodeName + "恢复完成!"); + * if(cr.recoverFlag == RecoverFlag.Recovering) cr.recoverFlag = RecoverFlag.Fine; + * + * logger.debug("恢复完成,需要检查合约" + args.get("contractID") + "的集群运行模式"); + * unitModeCheck(args.get("contractID")); } + */ static boolean clearCache() { - if (requestCache.isEmpty()) return true; + if (requestCache.isEmpty()) + return true; - // final long time = System.currentTimeMillis() - 60000L; //60s - // requestCache.entrySet() - // .removeIf( - // entry -> { - // RequestCache cache = entry.getValue(); - // if (cache == null) return true; - // return cache.getTime() < time; - // }); + // final long time = System.currentTimeMillis() - 60000L; //60s + // requestCache.entrySet() + // .removeIf( + // entry -> { + // RequestCache cache = entry.getValue(); + // if (cache == null) return true; + // return cache.getTime() < time; + // }); // - // return false; + // return false; // 对每个合约,只保存最近RESERVED个请求 - // for(RequestCache rc : requestCache.values()){ - // int delete = rc.size() - RequestCache.RESERVED,count = 0; - // if(delete > 0){ - // synchronized (rc){ - // for(Iterator iterator = rc.requests.keySet().iterator(); + // for(RequestCache rc : requestCache.values()){ + // int delete = rc.size() - RequestCache.RESERVED,count = 0; + // if(delete > 0){ + // synchronized (rc){ + // for(Iterator iterator = rc.requests.keySet().iterator(); // iterator.hasNext(); ) { - // Integer key = iterator.next(); - // if(count < delete){ - // iterator.remove(); - // count++; - // } - // } - // } - // } - // } + // Integer key = iterator.next(); + // if(count < delete){ + // iterator.remove(); + // count++; + // } + // } + // } + // } + // } return false; } @@ -253,7 +224,8 @@ public class MasterActions { static RequestCache getCache(String contractID) { if (contractID != null) { RequestCache cache = requestCache.get(contractID); - if (cache != null) return cache; + if (cache != null) + return cache; else { LOGGER.debug("[NodeCenterActions] create requestcache:" + contractID); RequestCache reqc = new RequestCache(); @@ -264,26 +236,17 @@ public class MasterActions { return null; } - /* @Action(async = true) - public void sendCachedRequests(Map args, final ResultCallback rc) { - String contractID = args.get("contractID"); - int start = Integer.parseInt(args.get("start")); - int end = Integer.parseInt(args.get("end")); - - RequestCache cache = getCache(contractID); - for(int i = start + 1;i < end;i++){ - if(cache == null || !cache.containsKey(i)){ - //this node crash - String nodeID = args.get("nodeID"); - MasterActions.restartContracts(nodeID); - } - JsonObject jo = cache.get(i); - if(jo == null){ - logger.debug("在NC中第 " + i + "个请求已经被清,无法发给恢复节点!"); - }else - controller.sendMsg(JsonUtil.toJson(jo)); - - logger.debug("NC发送第 " + i + " " + jo.get("seq").getAsString() + " 个请求给Node"); - } - }*/ + /* + * @Action(async = true) public void sendCachedRequests(Map args, final + * ResultCallback rc) { String contractID = args.get("contractID"); int start = + * Integer.parseInt(args.get("start")); int end = Integer.parseInt(args.get("end")); + * + * RequestCache cache = getCache(contractID); for(int i = start + 1;i < end;i++){ if(cache == + * null || !cache.containsKey(i)){ //this node crash String nodeID = args.get("nodeID"); + * MasterActions.restartContracts(nodeID); } JsonObject jo = cache.get(i); if(jo == null){ + * logger.debug("在NC中第 " + i + "个请求已经被清,无法发给恢复节点!"); }else + * controller.sendMsg(JsonUtil.toJson(jo)); + * + * logger.debug("NC发送第 " + i + " " + jo.get("seq").getAsString() + " 个请求给Node"); } } + */ } diff --git a/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java b/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java index fbb5561..f0d0496 100644 --- a/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java +++ b/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java @@ -49,9 +49,9 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public static Integer page; public static boolean isEmpty = false; public static NodeCenterFrameHandler controller; - // public MetaIndexAction(NodeCenterFrameHandler nodeCenterFrameHandler) { - // controller = nodeCenterFrameHandler; - // } + // public MetaIndexAction(NodeCenterFrameHandler nodeCenterFrameHandler) { + // controller = nodeCenterFrameHandler; + // } private static IndexWriter indexWriter; static { @@ -90,7 +90,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; Query query = new TermQuery(new Term("contractID", thisDesp.contractID)); TopDocs docs = indexSearcher.search(query, 10); LOGGER.debug(docs.scoreDocs); - if (null != thisDesp.contractName && (docs.scoreDocs == null || docs.scoreDocs.length == 0)) { + if (null != thisDesp.contractName + && (docs.scoreDocs == null || docs.scoreDocs.length == 0)) { req.addProperty("action", "requestReadMe"); req.addProperty("contractID", thisDesp.contractID); rc.onResult(req); @@ -101,8 +102,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; LOGGER.warn("getting index failed! " + e.getMessage()); } } - // req.addProperty("action", "requestReadMe"); - // req.addProperty("contractID", thisDesp.contractID); + // req.addProperty("action", "requestReadMe"); + // req.addProperty("contractID", thisDesp.contractID); // rc.onResult(req); LOGGER.info("contract " + thisDesp.contractName + " --> actually to index"); } @@ -128,7 +129,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public static void delIndexbyCID(JsonObject jo) { try { - if (!jo.has("contractID")) return; + if (!jo.has("contractID")) + return; String contractID = jo.get("contractID").getAsString(); LOGGER.info("contractID:" + contractID + "-->actually to delete"); indexWriter.deleteDocuments(new Term("contractID", contractID)); @@ -140,7 +142,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public static void delIndexbyOwner(JsonObject jo) { try { - if (!jo.has("owner")) return; + if (!jo.has("owner")) + return; String owner = jo.get("owner").getAsString(); LOGGER.info("owner:" + owner + "-->actually to delete"); indexWriter.deleteDocuments(new Term("owner", owner)); @@ -167,18 +170,18 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; default: return "no such search type"; } - // if (searchReq != null) { - // logger.info(searchReq.get("pubkey").getAsString() + "---->start + // if (searchReq != null) { + // logger.info(searchReq.get("pubkey").getAsString() + "---->start // search"); - // DirectoryReader indexReader = DirectoryReader.open(indexDir); - // IndexSearcher indexSearcher = new IndexSearcher(indexReader); - // BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); - // reandler(searchReq, queryBuilder); - // BooleanQuery searchQuery = queryBuilder.build(); - // TopDocs docs = indexSearcher.search(searchQuery, 10); - // return docs.scoreDocs.length + ""; - // } - // else return "0"; + // DirectoryReader indexReader = DirectoryReader.open(indexDir); + // IndexSearcher indexSearcher = new IndexSearcher(indexReader); + // BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder(); + // reandler(searchReq, queryBuilder); + // BooleanQuery searchQuery = queryBuilder.build(); + // TopDocs docs = indexSearcher.search(searchQuery, 10); + // return docs.scoreDocs.length + ""; + // } + // else return "0"; } catch (Exception e) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); @@ -188,12 +191,17 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public static String getMetabyReadme(JsonObject jo) { try { - if (!jo.has("keyword")) return "missing arguments: keyword"; + if (!jo.has("keyword")) + return "missing arguments: keyword"; - if (!jo.has("page")) page = 1; - else page = jo.get("page").getAsInt(); - if (page <= 0) page = 1; - if (jo.has("pageSize")) PAGE_SIZE = jo.get("pageSize").getAsInt(); + if (!jo.has("page")) + page = 1; + else + page = jo.get("page").getAsInt(); + if (page <= 0) + page = 1; + if (jo.has("pageSize")) + PAGE_SIZE = jo.get("pageSize").getAsInt(); String keyword = jo.get("keyword").getAsString(); // Analyzer analyzer = new StandardAnalyzer(); Analyzer analyzer = new IKAnalyzer(); @@ -203,17 +211,17 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TopDocs docs = indexSearcher.search(rmQuery, 1000); - // Document document = null ; - // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { - // document = indexSearcher.doc(docs.scoreDocs[0].doc); - // ContractMeta contractMeta = new ContractMeta(); - // contractMeta.setContractID(document.get("contractID")); - // contractMeta.setOwner(document.get("owner")); - // contractMeta.setPubkey(document.get("pubkey")); - // contractMeta.setReadmeStr(document.get("readmeStr")); - // String rs = JsonUtil.toJson(contractMeta); - // return rs; - // } + // Document document = null ; + // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { + // document = indexSearcher.doc(docs.scoreDocs[0].doc); + // ContractMeta contractMeta = new ContractMeta(); + // contractMeta.setContractID(document.get("contractID")); + // contractMeta.setOwner(document.get("owner")); + // contractMeta.setPubkey(document.get("pubkey")); + // contractMeta.setReadmeStr(document.get("readmeStr")); + // String rs = JsonUtil.toJson(contractMeta); + // return rs; + // } ResultModel resultModel = null; if (docs.scoreDocs != null && docs.scoreDocs.length > 0) resultModel = paginate(docs, indexReader); @@ -231,28 +239,32 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; private static String getMetabyPubkey(JsonObject jo) { try { - if (!jo.has("pubkey")) return "missing arguments: pubkey"; + if (!jo.has("pubkey")) + return "missing arguments: pubkey"; - if (!jo.has("page")) page = 1; - else page = jo.get("page").getAsInt(); - if (page <= 0) page = 1; + if (!jo.has("page")) + page = 1; + else + page = jo.get("page").getAsInt(); + if (page <= 0) + page = 1; String pubkey = jo.get("pubkey").getAsString(); DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Query query = new TermQuery(new Term("pubkey", pubkey)); TopDocs docs = indexSearcher.search(query, 10); - // Document document = null; - // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { - // document = indexSearcher.doc(docs.scoreDocs[0].doc); - // ContractMeta contractMeta = new ContractMeta(); - // contractMeta.setContractID(document.get("contractID")); - // contractMeta.setOwner(document.get("owner")); - // contractMeta.setPubkey(document.get("pubkey")); - // contractMeta.setReadmeStr(document.get("readmeStr")); - // String rs = JsonUtil.toJson(contractMeta); - // return rs; - // } + // Document document = null; + // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { + // document = indexSearcher.doc(docs.scoreDocs[0].doc); + // ContractMeta contractMeta = new ContractMeta(); + // contractMeta.setContractID(document.get("contractID")); + // contractMeta.setOwner(document.get("owner")); + // contractMeta.setPubkey(document.get("pubkey")); + // contractMeta.setReadmeStr(document.get("readmeStr")); + // String rs = JsonUtil.toJson(contractMeta); + // return rs; + // } ResultModel resultModel = null; if (docs.scoreDocs != null && docs.scoreDocs.length > 0) resultModel = paginate(docs, indexReader); @@ -271,28 +283,32 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; private static String getMetabyCID(JsonObject jo) { System.out.println("getMetabyCID" + jo.toString()); try { - if (!jo.has("contractID")) return "missing arguments: contractID"; + if (!jo.has("contractID")) + return "missing arguments: contractID"; - if (!jo.has("page")) page = 1; - else page = jo.get("page").getAsInt(); - if (page <= 0) page = 1; + if (!jo.has("page")) + page = 1; + else + page = jo.get("page").getAsInt(); + if (page <= 0) + page = 1; String contractID = jo.get("contractID").getAsString(); DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Query query = new TermQuery(new Term("contractID", contractID)); TopDocs docs = indexSearcher.search(query, 10); - // Document document = null; - // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { - // document = indexSearcher.doc(docs.scoreDocs[0].doc); - // ContractMeta contractMeta = new ContractMeta(); - // contractMeta.setContractID(document.get("contractID")); - // contractMeta.setOwner(document.get("owner")); - // contractMeta.setPubkey(document.get("pubkey")); - // contractMeta.setReadmeStr(document.get("readmeStr")); - // String rs = JsonUtil.toJson(contractMeta); - // return rs; - // } + // Document document = null; + // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { + // document = indexSearcher.doc(docs.scoreDocs[0].doc); + // ContractMeta contractMeta = new ContractMeta(); + // contractMeta.setContractID(document.get("contractID")); + // contractMeta.setOwner(document.get("owner")); + // contractMeta.setPubkey(document.get("pubkey")); + // contractMeta.setReadmeStr(document.get("readmeStr")); + // String rs = JsonUtil.toJson(contractMeta); + // return rs; + // } ResultModel resultModel = null; if (docs.scoreDocs != null && docs.scoreDocs.length > 0) resultModel = paginate(docs, indexReader); @@ -310,28 +326,33 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; private static String getMetabyOwner(JsonObject jo) { try { - if (!jo.has("owner")) return "missing arguments: owner"; + if (!jo.has("owner")) + return "missing arguments: owner"; - if (!jo.has("page")) page = 1; - else page = jo.get("page").getAsInt(); - if (page <= 0) page = 1; - if (jo.has("pageSize")) PAGE_SIZE = jo.get("pageSize").getAsInt(); + if (!jo.has("page")) + page = 1; + else + page = jo.get("page").getAsInt(); + if (page <= 0) + page = 1; + if (jo.has("pageSize")) + PAGE_SIZE = jo.get("pageSize").getAsInt(); String owner = jo.get("owner").getAsString(); DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Query query = new TermQuery(new Term("owner", owner)); TopDocs docs = indexSearcher.search(query, 1000); - // Document document = null; - // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { - // document = indexSearcher.doc(docs.scoreDocs[0].doc); - // ContractMeta contractMeta = new ContractMeta(); - // contractMeta.setContractID(document.get("contractID")); - // contractMeta.setOwner(document.get("owner")); - // contractMeta.setPubkey(document.get("pubkey")); - // contractMeta.setReadmeStr(document.get("readmeStr")); - // String rs = JsonUtil.toJson(contractMeta); - // return rs; - // } + // Document document = null; + // if (docs.scoreDocs!=null&& docs.scoreDocs.length>0) { + // document = indexSearcher.doc(docs.scoreDocs[0].doc); + // ContractMeta contractMeta = new ContractMeta(); + // contractMeta.setContractID(document.get("contractID")); + // contractMeta.setOwner(document.get("owner")); + // contractMeta.setPubkey(document.get("pubkey")); + // contractMeta.setReadmeStr(document.get("readmeStr")); + // String rs = JsonUtil.toJson(contractMeta); + // return rs; + // } ResultModel resultModel = null; if (docs.scoreDocs != null && docs.scoreDocs.length > 0) resultModel = paginate(docs, indexReader); @@ -350,18 +371,15 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public static void reqHandler(JsonObject req, BooleanQuery.Builder query) throws ParseException { if (req.get("contractID").getAsString() != null) { - query.add( - new FuzzyQuery(new Term("contractID", req.get("contractID").getAsString())), + query.add(new FuzzyQuery(new Term("contractID", req.get("contractID").getAsString())), BooleanClause.Occur.SHOULD); } if (req.get("owner").getAsString() != null) { - query.add( - new FuzzyQuery(new Term("owner", req.get("owner").getAsString())), + query.add(new FuzzyQuery(new Term("owner", req.get("owner").getAsString())), BooleanClause.Occur.SHOULD); } if (req.get("pubkey").getAsString() != null) { - query.add( - new PrefixQuery(new Term("pubkey", req.get("pubkey").getAsString())), + query.add(new PrefixQuery(new Term("pubkey", req.get("pubkey").getAsString())), BooleanClause.Occur.SHOULD); } if (req.get("readmeStr").getAsString() != null) { @@ -400,8 +418,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; resultModel.setContractMetaList(contractMetaList); resultModel.setCurPage(page); Long pageCount = - docs.totalHits.value % PAGE_SIZE > 0 - ? (docs.totalHits.value) / PAGE_SIZE + 1 + docs.totalHits.value % PAGE_SIZE > 0 ? (docs.totalHits.value) / PAGE_SIZE + 1 : (docs.totalHits.value) / PAGE_SIZE; resultModel.setPageCount(pageCount); return resultModel; @@ -410,23 +427,19 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; @Action(async = true, userPermission = 1L) public synchronized void onRequestReadMe(JsonObject json, ResultCallback rc) throws IOException { - // try { - // Thread.sleep(2000); - // } catch (InterruptedException e) { - // e.printStackTrace(); - // } + // try { + // Thread.sleep(2000); + // } catch (InterruptedException e) { + // e.printStackTrace(); + // } CMNode node = NodeCenterActions.nodeInfos.get(controller.pubKey); String nodeAddr = getNodeAddr(node.masterAddress); String name = json.get("name").getAsString(); // /DOIP/Hello1/assets/logo.png // String nodeAddr="127.0.0.1:21030"; String pngUrl = "http://" + nodeAddr + "/DOIP/" + name + "/assets/logo.png"; - System.out.println( - "node.masterAddress: " - + node.masterAddress - + "==========" - + "nodeAddr: " - + nodeAddr); + System.out.println("node.masterAddress: " + node.masterAddress + "==========" + "nodeAddr: " + + nodeAddr); System.out.println("name: " + name + "pngUrl: " + pngUrl); String contractID = json.get("contractID").getAsString(); String status = json.get("status").getAsString(); @@ -465,7 +478,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public void getMetabyReadme(JsonObject json, ResultCallback rc) { String result = getMetabyReadme(json); JsonObject object = new JsonObject(); - if (json.has("requestID")) object.add("responseID", json.get("requestID")); + if (json.has("requestID")) + object.add("responseID", json.get("requestID")); System.out.println("zzzResult" + result); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyReadme"); @@ -474,15 +488,19 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; @Action(async = true, userPermission = 1L) public void segmentWord(JsonObject json, ResultCallback rc) throws IOException { - //Analyzer analyzer = new IKAnalyzer(true); - CharArraySet stopWords = CharArraySet.unmodifiableSet(WordlistLoader.getWordSet(new InputStreamReader(Objects.requireNonNull(MetaIndexAction.class.getClassLoader().getResourceAsStream( - "org/bdware/server/stopwords.txt")), StandardCharsets.UTF_8))); + // Analyzer analyzer = new IKAnalyzer(true); + CharArraySet stopWords = + CharArraySet.unmodifiableSet(WordlistLoader.getWordSet(new InputStreamReader( + Objects.requireNonNull(MetaIndexAction.class.getClassLoader() + .getResourceAsStream("org/bdware/server/stopwords.txt")), + StandardCharsets.UTF_8))); Analyzer analyzer = new SmartChineseAnalyzer(stopWords); JsonObject object = new JsonObject(); String words = "我喜欢区块链,我想试用一下这个,我是做大数据处理的,我是科技局的管理员"; // 从json拿 TokenStream stream = null; - if (json.has("requestID")) object.add("responseID", json.get("requestID")); + if (json.has("requestID")) + object.add("responseID", json.get("requestID")); try { stream = analyzer.tokenStream("myfield", words); CharTermAttribute charTermAtt = stream.addAttribute(CharTermAttribute.class); @@ -511,7 +529,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public void getMetabyCID(JsonObject json, ResultCallback rc) { String result = getMetabyCID(json); JsonObject object = new JsonObject(); - if (json.has("requestID")) object.add("responseID", json.get("requestID")); + if (json.has("requestID")) + object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyCID"); rc.onResult(object); @@ -521,7 +540,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public void getMetabyOwner(JsonObject json, ResultCallback rc) { String result = getMetabyOwner(json); JsonObject object = new JsonObject(); - if (json.has("requestID")) object.add("responseID", json.get("requestID")); + if (json.has("requestID")) + object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyOwner"); rc.onResult(object); @@ -531,7 +551,8 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; public void getMetabyPubkey(JsonObject json, ResultCallback rc) { String result = getMetabyPubkey(json); JsonObject object = new JsonObject(); - if (json.has("requestID")) object.add("responseID", json.get("requestID")); + if (json.has("requestID")) + object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyPubkey"); rc.onResult(object); diff --git a/src/main/java/org/bdware/server/nodecenter/MultiPointContractInfo.java b/src/main/java/org/bdware/server/nodecenter/MultiPointContractInfo.java index b26a440..667e478 100644 --- a/src/main/java/org/bdware/server/nodecenter/MultiPointContractInfo.java +++ b/src/main/java/org/bdware/server/nodecenter/MultiPointContractInfo.java @@ -6,13 +6,13 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class MultiPointContractInfo { - public List members; //pubKey + public List members; // pubKey public ContractExecType type; AtomicInteger ai = new AtomicInteger(); public transient ContractExecutor rcf; - public String getNextSeq(){ + public String getNextSeq() { System.out.println("MultiPointContractInfo获得下一个序号" + (ai.get() + 1)); return ai.getAndIncrement() + ""; } diff --git a/src/main/java/org/bdware/server/nodecenter/NCClientActions.java b/src/main/java/org/bdware/server/nodecenter/NCClientActions.java index 115fa96..7fa5f7e 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCClientActions.java +++ b/src/main/java/org/bdware/server/nodecenter/NCClientActions.java @@ -35,8 +35,9 @@ public class NCClientActions { map.put("id", OtherNCProxy.instance.sm2.getPublicKeyStr()); byte[] signature = "no signature".getBytes(); try { - signature = SM2Util.sign(OtherNCProxy.instance.sm2.getPrivateKeyParameter(), (OtherNCProxy.instance.sm2.getPublicKeyStr() + json.get("session").getAsString()) - .getBytes()); + signature = SM2Util.sign(OtherNCProxy.instance.sm2.getPrivateKeyParameter(), + (OtherNCProxy.instance.sm2.getPublicKeyStr() + + json.get("session").getAsString()).getBytes()); } catch (CryptoException e) { e.printStackTrace(); } @@ -62,8 +63,8 @@ public class NCClientActions { if (json.has("empty")) { LOGGER.info("receive from NC ,do not have any cp."); } else { - info = JsonUtil.fromJson(json.get("contracts").getAsString(), new TypeToken>() { - }.getType()); + info = JsonUtil.fromJson(json.get("contracts").getAsString(), + new TypeToken>() {}.getType()); } LOGGER.info(JsonUtil.toJson(info)); diff --git a/src/main/java/org/bdware/server/nodecenter/NCClientHandler.java b/src/main/java/org/bdware/server/nodecenter/NCClientHandler.java index 0797fab..3be6c7e 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCClientHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/NCClientHandler.java @@ -23,7 +23,7 @@ import java.util.concurrent.Executors; public class NCClientHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(NCClientHandler.class); static ExecutorService executorService = Executors.newFixedThreadPool(5); - public boolean hasPermission; //是否在目标NC上有权限 + public boolean hasPermission; // 是否在目标NC上有权限 public NCClientActions actions; Channel channel; ActionExecutor ae; @@ -67,21 +67,16 @@ public class NCClientHandler extends SimpleChannelInboundHandler { protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf bb = (ByteBuf) msg; try { - final JsonObject arg = - new JsonParser() - .parse(new InputStreamReader(new ByteBufInputStream(bb))) - .getAsJsonObject(); + final JsonObject arg = new JsonParser() + .parse(new InputStreamReader(new ByteBufInputStream(bb))).getAsJsonObject(); if (arg.has("action")) { final String action = arg.get("action").getAsString(); - ae.handle( - action, - arg, - new ResultCallback() { - @Override - public void onResult(String str) { - sendMsg(str); - } - }); + ae.handle(action, arg, new ResultCallback() { + @Override + public void onResult(String str) { + sendMsg(str); + } + }); } } catch (java.lang.IllegalArgumentException e) { @@ -100,7 +95,8 @@ public class NCClientHandler extends SimpleChannelInboundHandler { public void close() { try { isConnected = false; - if (channel != null) channel.close(); + if (channel != null) + channel.close(); } catch (Exception e) { e.printStackTrace(); } finally { diff --git a/src/main/java/org/bdware/server/nodecenter/NCElectMasterUtil.java b/src/main/java/org/bdware/server/nodecenter/NCElectMasterUtil.java index aca46e1..38260bb 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCElectMasterUtil.java +++ b/src/main/java/org/bdware/server/nodecenter/NCElectMasterUtil.java @@ -15,7 +15,8 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; public class NCElectMasterUtil { - public static final Map electInfos = new ConcurrentHashMap<>(); //key is contractID + public static final Map electInfos = new ConcurrentHashMap<>(); // key is + // contractID private static final Logger LOGGER = LogManager.getLogger(NCElectMasterUtil.class); public static class ElectInfo { @@ -24,10 +25,10 @@ public class NCElectMasterUtil { String formerMaster; String uniNumber; long lastTime = System.currentTimeMillis(); - int onlineNum; //除旧的master之外的在线节点数 + int onlineNum; // 除旧的master之外的在线节点数 String contractID; - String mems; //执行这个合约的所有节点的pubKey - private Map nodeID2LastExe = new ConcurrentHashMap<>(); //key is nodeID + String mems; // 执行这个合约的所有节点的pubKey + private Map nodeID2LastExe = new ConcurrentHashMap<>(); // key is nodeID private Timer timer; @@ -38,11 +39,10 @@ public class NCElectMasterUtil { uniNumber = uni; String[] mem = members.split(","); -/* try { - Thread.sleep(2000); //让NC发现崩溃节点 - } catch (InterruptedException e) { - e.printStackTrace(); - }*/ + /* + * try { Thread.sleep(2000); //让NC发现崩溃节点 } catch (InterruptedException e) { + * e.printStackTrace(); } + */ for (String memID : mem) { if (memID == null || memID.length() == 0) @@ -52,26 +52,22 @@ public class NCElectMasterUtil { onlineNum++; } } - NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay( - () -> { - // cancel the election if no nodes find the master's crash in delay + 2 seconds - SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss.SSS"); - if (System.currentTimeMillis() - lastTime > (delay + 15000)) { - LOGGER.info("lastTime=" + df.format(lastTime) + " cancel the election"); - cancel(); - } - // start timeout election -// if (electInfos.containsKey(contractID) && nodeID2LastExe.size() == onlineNum) { -// elect(); -// } - }, - delay + 1500, - delay + 1500, - TimeUnit.MILLISECONDS); - //timer.schedule(task, dealy + 2000); + NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay(() -> { + // cancel the election if no nodes find the master's crash in delay + 2 seconds + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd.HH:mm:ss.SSS"); + if (System.currentTimeMillis() - lastTime > (delay + 15000)) { + LOGGER.info("lastTime=" + df.format(lastTime) + " cancel the election"); + cancel(); + } + // start timeout election + // if (electInfos.containsKey(contractID) && nodeID2LastExe.size() == onlineNum) { + // elect(); + // } + }, delay + 1500, delay + 1500, TimeUnit.MILLISECONDS); + // timer.schedule(task, dealy + 2000); - LOGGER.info("new election of contract " + contractID + " is added to electInfos, " + - onlineNum + " node is online"); + LOGGER.info("new election of contract " + contractID + " is added to electInfos, " + + onlineNum + " node is online"); } public void cancel() { @@ -84,10 +80,11 @@ public class NCElectMasterUtil { } } - public synchronized void put(String nodeID, int lastExe, String master, String mem2, String uniNum) { + public synchronized void put(String nodeID, int lastExe, String master, String mem2, + String uniNum) { LOGGER.info("put nodeID=" + nodeID); - //确保该合约某时只能有一个选举,其他的选举请求被忽略 + // 确保该合约某时只能有一个选举,其他的选举请求被忽略 if (!master.equals(formerMaster)) { LOGGER.debug("[NCElectMasterUtil] master error!"); return; @@ -105,13 +102,14 @@ public class NCElectMasterUtil { } long now = System.currentTimeMillis(); - //认为是一个新的选举,之前的作废 + // 认为是一个新的选举,之前的作废 if (now - lastTime > delay) { LOGGER.info("[NCElectMasterUtil] time error!"); cancel(); synchronized (electInfos) { - //electInfos.remove(contractID); - NCElectMasterUtil.electInfos.put(contractID, new ElectInfo(master, contractID, mem2, uniNum)); + // electInfos.remove(contractID); + NCElectMasterUtil.electInfos.put(contractID, + new ElectInfo(master, contractID, mem2, uniNum)); ElectInfo eleInfo = electInfos.get(contractID); eleInfo.put(nodeID, lastExe, master, mem2, uniNum); } @@ -137,15 +135,17 @@ public class NCElectMasterUtil { startElect.set(true); } - //更新路由信息,这个合约的master暂时为null + // 更新路由信息,这个合约的master暂时为null if (NodeCenterActions.nodeInfos.containsKey(formerMaster)) { CMNode node = NodeCenterActions.nodeInfos.get(formerMaster); if (node != null) { synchronized (node) { for (ContractDesp cd : node.contracts) { - if (cd.contractID.equals(contractID) || cd.contractName.equals(contractID)) { + if (cd.contractID.equals(contractID) + || cd.contractName.equals(contractID)) { cd.setIsMaster(false); - LOGGER.debug("设置节点 " + node.pubKey.substring(0, 5) + " 的合约 " + contractID + " isMaster=" + false); + LOGGER.debug("设置节点 " + node.pubKey.substring(0, 5) + " 的合约 " + + contractID + " isMaster=" + false); break; } } @@ -164,16 +164,16 @@ public class NCElectMasterUtil { LOGGER.info("[NCElectMasterUtil] 选举出新的master " + newMaster); cancel(); - //electInfos.remove(contractID); + // electInfos.remove(contractID); - //通知新的master让它发起重连操作,并在所有节点重连成功之后开启master的恢复 + // 通知新的master让它发起重连操作,并在所有节点重连成功之后开启master的恢复 try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } LOGGER.info("开始计算需要连接新master的都有哪些节点:"); - StringBuilder onlineMems = new StringBuilder(); //不包含旧的master + StringBuilder onlineMems = new StringBuilder(); // 不包含旧的master for (String memID : nodeID2LastExe.keySet()) { if (memID == null || memID.length() == 0) continue; diff --git a/src/main/java/org/bdware/server/nodecenter/NCHttpHandler.java b/src/main/java/org/bdware/server/nodecenter/NCHttpHandler.java index b9d3678..b2729e5 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCHttpHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/NCHttpHandler.java @@ -47,10 +47,8 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { static LogActions logActions = new LogActions(managerAction); static UnitActions unitActions = new UnitActions(managerAction); private static final ActionExecutor actionExecutor = - new ActionExecutor( - NodeCenterFrameHandler.executorService, - new NodeCenterActions(null), - managerAction, logActions, unitActions) { + new ActionExecutor(NodeCenterFrameHandler.executorService, + new NodeCenterActions(null), managerAction, logActions, unitActions) { @Override public boolean checkPermission(Action a, JsonObject arg, long per) { @@ -78,21 +76,14 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { if (arg.has("pubKey")) { pubkey = arg.get("pubKey").getAsString(); } - NodeCenterServer.nodeHttpLogDB.put( - action, - "{\"action\":\"" - + action - + "\",\"pubKey\":\"" - + pubkey - + "\",\"date\":" - + System.currentTimeMillis() - + "}"); + NodeCenterServer.nodeHttpLogDB.put(action, + "{\"action\":\"" + action + "\",\"pubKey\":\"" + pubkey + "\",\"date\":" + + System.currentTimeMillis() + "}"); LOGGER.debug("[NCHttpHandler] flag = " + flag + " flag2 = " + flag2); return flag && flag2; } }; - static TypeToken> token = new TypeToken>() { - }; + static TypeToken> token = new TypeToken>() {}; private final HttpFileHandleAdapter fileAdapter; // public static ExecutorService executor = Executors.newFixedThreadPool(5); public AtomicInteger counter = new AtomicInteger(0); @@ -149,20 +140,15 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { @URIPath(method = HttpMethod.OPTIONS) public void crossOrigin(ChannelHandlerContext ctx, FullHttpRequest request) { - DefaultFullHttpResponse fullResponse = - new DefaultFullHttpResponse( - request.protocolVersion(), - OK, - Unpooled.wrappedBuffer("success".getBytes())); + DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse( + request.protocolVersion(), OK, Unpooled.wrappedBuffer("success".getBytes())); fullResponse.headers().remove("Access-Control-Allow-Origin"); fullResponse.headers().remove("Access-Control-Allow-Headers"); fullResponse.headers().add("Access-Control-Allow-Origin", "*"); fullResponse.headers().add("Access-Control-Allow-Methods", "*"); - fullResponse - .headers() - .add("Access-Control-Allow-Headers", - "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + - "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection"); + fullResponse.headers().add("Access-Control-Allow-Headers", + "Content-Type, Cookie, Accept-Encoding, User-Agent, Host, Referer, " + + "X-Requested-With, Accept, Accept-Language, Cache-Control, Connection"); ChannelFuture f = ctx.write(fullResponse); f.addListener(ChannelFutureListener.CLOSE); LOGGER.info("[Option] received!"); @@ -189,14 +175,13 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { JsonObject transformedParam = new JsonObject(); for (String key : parameters.keySet()) { List val = parameters.get(key); - if (val != null) transformedParam.addProperty(key, val.get(0)); + if (val != null) + transformedParam.addProperty(key, val.get(0)); } return transformedParam; } - @URIPath( - method = org.bdware.server.http.HttpMethod.GET, - value = {"/doip"}) + @URIPath(method = org.bdware.server.http.HttpMethod.GET, value = {"/doip"}) public void handleDOIP(ChannelHandlerContext ctx, FullHttpRequest req) { ByteBuf content = req.content(); JsonObject map = parseArgInQuery(req); @@ -209,11 +194,8 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { } injectPermission(map, str); - DefaultFullHttpResponse response = - new DefaultFullHttpResponse( - HttpVersion.HTTP_1_1, - OK, - Unpooled.wrappedBuffer(MetaIndexAction.search(map).getBytes())); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, + Unpooled.wrappedBuffer(MetaIndexAction.search(map).getBytes())); response.headers().add("Access-Control-Allow-Origin", "*"); response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); @@ -224,8 +206,7 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { @Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) { - LOGGER.debug("[NCHttpHandler] TID:" + Thread.currentThread().getId() + - msg.toString()); + LOGGER.debug("[NCHttpHandler] TID:" + Thread.currentThread().getId() + msg.toString()); if (msg instanceof FullHttpRequest) { FullHttpRequest request = (FullHttpRequest) msg; handler.handle(ctx, request); @@ -234,9 +215,8 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { public static String getRole(String pubKey) { try { - String ret = - KeyValueDBUtil.instance.getValue( - NCTables.ConfigDB.toString(), "__CenterManager__"); + String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), + "__CenterManager__"); if (ret != null && ret.length() > 0) { boolean isCenterManager = (ret.equals(pubKey)); // 表示此节点是否是平台管理员 ret = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); @@ -313,7 +293,8 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { if (map.has("callback")) cb = new HttpResultCallback(ctx, map.get("callback").getAsString()); - else cb = new HttpResultCallback(ctx, null); + else + cb = new HttpResultCallback(ctx, null); cb.addHeader("Access-Control-Allow-Origin", "*"); cb.addHeader("Access-Control-Allow-Methods", "*"); cb.addHeader("charset", "utf-8"); @@ -321,20 +302,16 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { } actionExecutor.handle(action, map, cb); } else { - DefaultFullHttpResponse response = - new DefaultFullHttpResponse( - HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(ret.getBytes())); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, + OK, Unpooled.wrappedBuffer(ret.getBytes())); response.headers().add("Access-Control-Allow-Origin", "*"); response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); f.addListener(ChannelFutureListener.CLOSE); } } catch (IllegalArgumentException e) { - DefaultFullHttpResponse response = - new DefaultFullHttpResponse( - HttpVersion.HTTP_1_1, - OK, - Unpooled.wrappedBuffer(e.getMessage().getBytes())); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, + Unpooled.wrappedBuffer(e.getMessage().getBytes())); response.headers().add("Access-Control-Allow-Origin", "*"); response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); @@ -345,11 +322,8 @@ public class NCHttpHandler extends SimpleChannelInboundHandler { ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); ret.put("msg", bo.toString()); - DefaultFullHttpResponse response = - new DefaultFullHttpResponse( - HttpVersion.HTTP_1_1, - OK, - Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes())); + DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, + Unpooled.wrappedBuffer(JsonUtil.toJson(ret).getBytes())); response.headers().add("Access-Control-Allow-Origin", "*"); response.headers().add("Access-Control-Allow-Methods", "*"); ChannelFuture f = ctx.write(response); diff --git a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java index a938680..1fc111c 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java +++ b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java @@ -27,11 +27,9 @@ public class NCManagerAction { public static final String centerManger = "__CenterManager__"; public static final String clusterName = "__ClusterName__"; static final String Licence = "__LICENCE___"; - static final ECPublicKeyParameters licencePub = - BCECUtil.createECPublicKeyFromStrParameters( - "04844412ecb77b07d5ad7097c488ae9dff1013b310d2311f8bd84c491642011e1a6a7041bae8c2ad75da29c27e320bd430abc723cf6bcb0490afc82cc26e6d5637", - SM2Util.CURVE, - SM2Util.DOMAIN_PARAMS); + static final ECPublicKeyParameters licencePub = BCECUtil.createECPublicKeyFromStrParameters( + "04844412ecb77b07d5ad7097c488ae9dff1013b310d2311f8bd84c491642011e1a6a7041bae8c2ad75da29c27e320bd430abc723cf6bcb0490afc82cc26e6d5637", + SM2Util.CURVE, SM2Util.DOMAIN_PARAMS); private static final Logger LOGGER = LogManager.getLogger(NCManagerAction.class); String sessionID = null; private String publicKey; @@ -45,7 +43,8 @@ public class NCManagerAction { public String getPubKey(JsonObject jo) { try { - if (publicKey != null) return publicKey; + if (publicKey != null) + return publicKey; return jo.get("pubKey").getAsString(); } catch (Exception e) { } @@ -55,8 +54,7 @@ public class NCManagerAction { public static boolean isCenterManager(String pubkey) { String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), centerManger); LOGGER.debug("centerManger: " + ret); - return !StringUtil.isNullOrEmpty(ret) - && !StringUtil.isNullOrEmpty(pubkey) + return !StringUtil.isNullOrEmpty(ret) && !StringUtil.isNullOrEmpty(pubkey) && pubkey.equals(ret); } @@ -104,8 +102,8 @@ public class NCManagerAction { @Action(userPermission = 0) public void setClusterName(JsonObject json, ResultCallback resultCallback) { if (json.has("name")) { - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), clusterName, json.get("name").getAsString()); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), clusterName, + json.get("name").getAsString()); simpleReply(resultCallback, "onSetClusterName", "success"); } else { simpleReply(resultCallback, "onSetClusterName", "failed"); @@ -122,7 +120,7 @@ public class NCManagerAction { String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), centerManger); if (ret != null && ret.length() > 0) { - boolean isCenterManager = (ret.equals(pubKey)); //表示此节点是否是平台管理员 + boolean isCenterManager = (ret.equals(pubKey)); // 表示此节点是否是平台管理员 ret = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); String role = ""; if (isCenterManager) { @@ -140,10 +138,10 @@ public class NCManagerAction { handler.setPermission(Role.compoundValue(role.split(","))); simpleReply(resultCallback, "onLogin", role); } else { - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), centerManger, pubKey); - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), clusterName, "clusterName_" + pubKey.substring(0, 5)); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), centerManger, + pubKey); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), clusterName, + "clusterName_" + pubKey.substring(0, 5)); handler.setPermission(0x30000ffL); simpleReply(resultCallback, "onLogin", "CenterManager"); } @@ -185,18 +183,22 @@ public class NCManagerAction { if (str.contains(Role.CenterManager.toString())) { if (json.has("newPubKey")) { String newPubKey = json.get("newPubKey").getAsString(); - KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), centerManger, newPubKey); - KeyValueDBUtil.instance.setValue( - NCTables.NodeUser.toString(), newPubKey, Role.CenterManager.toString()); - resultCallback.onResult("{\"action\":\"onResetCenterManager\",\"data\":\"success\",\"pubKey\":\"" - + newPubKey + "\"}"); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), centerManger, + newPubKey); + KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), newPubKey, + Role.CenterManager.toString()); + resultCallback.onResult( + "{\"action\":\"onResetCenterManager\",\"data\":\"success\",\"pubKey\":\"" + + newPubKey + "\"}"); } else { - //just keep the same - resultCallback.onResult("{\"action\":\"onResetCenterManager\",\"data\":\"success\",\"pubKey\":\"" - + getPubKey(json) + "\"}"); + // just keep the same + resultCallback.onResult( + "{\"action\":\"onResetCenterManager\",\"data\":\"success\",\"pubKey\":\"" + + getPubKey(json) + "\"}"); } } else { - resultCallback.onResult("{\"action\":\"onResetCenterManager\",\"data\":\"failed, no permission\"}"); + resultCallback.onResult( + "{\"action\":\"onResetCenterManager\",\"data\":\"failed, no permission\"}"); } } @@ -225,14 +227,10 @@ public class NCManagerAction { @Action(userPermission = 1 << 6) public void addNode(JsonObject json, ResultCallback resultCallback) { try { - KeyValueDBUtil.instance.setValue( - NCTables.NodeUser.toString(), - json.get("nodePubKey").getAsString(), - Role.Node.toString()); - KeyValueDBUtil.instance.setValue( - NCTables.NodeTime.toString(), - json.get("nodePubKey").getAsString(), - Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), + json.get("nodePubKey").getAsString(), Role.Node.toString()); + KeyValueDBUtil.instance.setValue(NCTables.NodeTime.toString(), + json.get("nodePubKey").getAsString(), Long.toString(new Date().getTime())); simpleReply(resultCallback, "onAddNodes", "success"); } catch (Exception e) { e.printStackTrace(); @@ -251,14 +249,14 @@ public class NCManagerAction { } if (str == null || str.length() == 0) { KeyValueDBUtil.instance.setValue(NCTables.ApplyRole.toString(), pubKey, role.name()); - KeyValueDBUtil.instance.setValue( - NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(NCTables.ApplyTime.toString(), pubKey, + Long.toString(new Date().getTime())); } else { if (!str.contains(role.name())) { - KeyValueDBUtil.instance.setValue( - NCTables.ApplyRole.toString(), pubKey, str + "," + role.name()); - KeyValueDBUtil.instance.setValue( - NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(NCTables.ApplyRole.toString(), pubKey, + str + "," + role.name()); + KeyValueDBUtil.instance.setValue(NCTables.ApplyTime.toString(), pubKey, + Long.toString(new Date().getTime())); } } simpleReply(resultCallback, "onApplyRole", "success!"); @@ -310,8 +308,8 @@ public class NCManagerAction { already = roles; } KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, already); - KeyValueDBUtil.instance.setValue( - NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); + KeyValueDBUtil.instance.setValue(NCTables.NodeTime.toString(), pubKey, + Long.toString(new Date().getTime())); KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); simpleReply(resultCallback, "onAuthNodeManager", "success"); @@ -445,13 +443,14 @@ public class NCManagerAction { SM2Util.verify(licencePub, content.getBytes(), ByteUtils.fromHexString(sign)); if (verify) { - KeyValueDBUtil.instance.setValue( - NCTables.ConfigDB.toString(), Licence, json.toString()); + KeyValueDBUtil.instance.setValue(NCTables.ConfigDB.toString(), Licence, + json.toString()); simpleReply(resultCallback, "onUpdateLicence", "success"); long end = System.currentTimeMillis(); LOGGER.debug("[listLicence:time]" + (end - start)); return; - } else simpleReply(resultCallback, "onUpdateLicence", "failed"); + } else + simpleReply(resultCallback, "onUpdateLicence", "failed"); long end = System.currentTimeMillis(); LOGGER.debug("[updateLicence:time]" + (end - start)); } @@ -535,14 +534,8 @@ public class NCManagerAction { Map result = new HashMap<>(); result.put("action", "onTransfer"); - result.put( - "data", - "now start transfer contract " - + contractID - + " from node:" - + node1ID - + " to node:" - + node2ID); + result.put("data", "now start transfer contract " + contractID + " from node:" + node1ID + + " to node:" + node2ID); resultCallback.onResult(result); } } diff --git a/src/main/java/org/bdware/server/nodecenter/NCTables.java b/src/main/java/org/bdware/server/nodecenter/NCTables.java index 35e6a20..b6d6cd7 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCTables.java +++ b/src/main/java/org/bdware/server/nodecenter/NCTables.java @@ -1,19 +1,7 @@ package org.bdware.server.nodecenter; public enum NCTables { - NodeUser, - NodeTime, - ApplyRole, - ApplyTime, - NodeHttpLog, - ContractMeta, - ConfigDB, - NodeTcpLog, - NodesDB, - TrustUnitsDB, - OtherNCs, - NCFile, - ApplyNodeManager; + NodeUser, NodeTime, ApplyRole, ApplyTime, NodeHttpLog, ContractMeta, ConfigDB, NodeTcpLog, NodesDB, TrustUnitsDB, OtherNCs, NCFile, ApplyNodeManager; public String toString() { return "NC_" + super.toString(); diff --git a/src/main/java/org/bdware/server/nodecenter/NCUDPRunner.java b/src/main/java/org/bdware/server/nodecenter/NCUDPRunner.java index 1ad3bc4..086b93d 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCUDPRunner.java +++ b/src/main/java/org/bdware/server/nodecenter/NCUDPRunner.java @@ -33,21 +33,21 @@ public class NCUDPRunner extends Thread { } public void run() { - // TimerTask task = new TimerTask() { + // TimerTask task = new TimerTask() { // - // @Override - // public void run(Timeout arg0) throws Exception { - // Set nodes = new HashSet(); - // for (Integer i:id2IP.keySet()) { - // UDPNode node = id2IP.get(i); - // if (System.currentTimeMillis()-node.lastUpdatedTime<60*1000) { - // nodes.remove(i); - // } - // } - // for (Integer i:nodes) - // node.re - // } - // }; + // @Override + // public void run(Timeout arg0) throws Exception { + // Set nodes = new HashSet(); + // for (Integer i:id2IP.keySet()) { + // UDPNode node = id2IP.get(i); + // if (System.currentTimeMillis()-node.lastUpdatedTime<60*1000) { + // nodes.remove(i); + // } + // } + // for (Integer i:nodes) + // node.re + // } + // }; id2IP = new HashMap(); int startPort = mainPort; while (true) { @@ -82,11 +82,8 @@ public class NCUDPRunner extends Thread { private void onUDPMessage(UDPMessage msg, DatagramPacket request) { switch (msg.type) { case shakeHand: - LOGGER.debug( - "[NCUDPRunner] shakeHand:" - + request.getAddress().getHostAddress() - + ":" - + request.getPort()); + LOGGER.debug("[NCUDPRunner] shakeHand:" + request.getAddress().getHostAddress() + + ":" + request.getPort()); id2IP.put(msg.id, new UDPNode(request.getSocketAddress())); default: } diff --git a/src/main/java/org/bdware/server/nodecenter/NameAndID.java b/src/main/java/org/bdware/server/nodecenter/NameAndID.java index 7050d51..b6a0d6d 100644 --- a/src/main/java/org/bdware/server/nodecenter/NameAndID.java +++ b/src/main/java/org/bdware/server/nodecenter/NameAndID.java @@ -1,5 +1,5 @@ package org.bdware.server.nodecenter; public class NameAndID { - String name,id; + String name, id; } diff --git a/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java b/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java index 9870bb7..e2aec4d 100644 --- a/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java +++ b/src/main/java/org/bdware/server/nodecenter/NodeCenterActions.java @@ -28,20 +28,20 @@ public class NodeCenterActions { // static SyncResult syncResult = new SyncResult(); private static final Logger LOGGER = LogManager.getLogger(NodeCenterActions.class); private static final String MISSING_ARGUMENT = "Missing arguments"; - public static Map nodeInfos = - new ConcurrentHashMap<>(); // all online nodes,key是节点pubKey + public static Map nodeInfos = new ConcurrentHashMap<>(); // all online + // nodes,key是节点pubKey public static SyncResult sync = new SyncResult(); public static Map contractID2Members = new ConcurrentHashMap<>(); // contractID,list(nodes' pubKey) - // public static ConcurrentHashMap> + // public static ConcurrentHashMap> // recoverMap = - // new ConcurrentHashMap<>(); // k-nodesPubKey,v-(k-contractID),only unit contract - // TODO 定期清缓存 - // public static Map requestCache = - // new ConcurrentHashMap<>(); // key is contractID,only for requestAll type contract - // TimerTask checkAliveTask; + // new ConcurrentHashMap<>(); // k-nodesPubKey,v-(k-contractID),only unit contract + // TODO 定期清缓存 + // public static Map requestCache = + // new ConcurrentHashMap<>(); // key is contractID,only for requestAll type contract + // TimerTask checkAliveTask; static Timer checkAliveTimer = new Timer(); public NodeCenterFrameHandler controller; String nodeID; // node pubKey @@ -58,7 +58,8 @@ public class NodeCenterActions { public static ContractDesp getContractByID(String key) { for (CMNode node : nodeInfos.values()) { for (ContractDesp desp : node.contracts) { - if (desp.contractID.equals(key)) return desp; + if (desp.contractID.equals(key)) + return desp; } } return null; @@ -67,7 +68,8 @@ public class NodeCenterActions { public static ContractDesp getContractByName(String key) { for (CMNode node : nodeInfos.values()) { for (ContractDesp desp : node.contracts) { - if (desp.contractID.equals(key) || desp.contractName.equals(key)) return desp; + if (desp.contractID.equals(key) || desp.contractName.equals(key)) + return desp; } } return null; @@ -88,8 +90,7 @@ public class NodeCenterActions { String pubKey = args.get("pubKey").getAsString(); String signature = args.get("sign").getAsString(); String nonce = args.get("nonce").getAsString(); - if (StringUtil.isNullOrEmpty(pubKey) - || StringUtil.isNullOrEmpty(signature) + if (StringUtil.isNullOrEmpty(pubKey) || StringUtil.isNullOrEmpty(signature) || StringUtil.isNullOrEmpty(nonce)) { resultCallback.onResult("missing pubkey/sign/nonce"); } @@ -103,8 +104,8 @@ public class NodeCenterActions { } String uuid = ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""); - ((HttpResultCallback) resultCallback) - .addHeader("content-disposition", "attachment;filename=encodeduuid.key"); + ((HttpResultCallback) resultCallback).addHeader("content-disposition", + "attachment;filename=encodeduuid.key"); resultCallback.onResult(uuid); } else { resultCallback.onResult("permission denied, not ContractManager"); @@ -129,9 +130,9 @@ public class NodeCenterActions { @Action public void syncPing(JsonObject json, ResultCallback rc) { // logger.debug("[NodeCenterAction] syncPing"); - //checkAliveTask.cancel(); - //checkAliveTask = new CheckAgentAliveTimer(nodeID); - //checkAliveTimer.schedule(checkAliveTask, 200000L); + // checkAliveTask.cancel(); + // checkAliveTask = new CheckAgentAliveTimer(nodeID); + // checkAliveTimer.schedule(checkAliveTask, 200000L); if (json.has("data")) { String data = json.get("data").getAsString(); LOGGER.debug("[syncPing] data" + data); @@ -161,10 +162,8 @@ public class NodeCenterActions { rc.onResult("{\"action\":\"syncContractInfo\"}"); } } - rc.onResult( - "{\"action\":\"syncPong\",\"requestID\":\"" - + json.get("requestID").getAsString() - + "\"}"); + rc.onResult("{\"action\":\"syncPong\",\"requestID\":\"" + + json.get("requestID").getAsString() + "\"}"); } @Action(userPermission = 0) @@ -189,16 +188,11 @@ public class NodeCenterActions { LOGGER.info("Role=" + ret + " --> pubkey=" + pubKey); if (null == ret || ret.isEmpty()) { /* - KeyValueDBUtil.instance.setValue( - NCTables.NodeUser.toString(), - pubKey, - Role.Node.toString()); - KeyValueDBUtil.instance.setValue( - NCTables.NodeTime.toString(), - pubKey, - Long.toString(new Date().getTime())); - return Role.Node.getValue(); - */ + * KeyValueDBUtil.instance.setValue( NCTables.NodeUser.toString(), pubKey, + * Role.Node.toString()); KeyValueDBUtil.instance.setValue( + * NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); + * return Role.Node.getValue(); + */ return Role.Anonymous.getValue(); // TODO fix permission bugs. // return Role.compoundValue(new String[]{"CenterManager"}); @@ -229,8 +223,8 @@ 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); + // checkAliveTask = new CheckAgentAliveTimer(nodeID); + // checkAliveTimer.schedule(checkAliveTask, 20000L); controller.pubKey = nodeID; @@ -252,8 +246,8 @@ public class NodeCenterActions { nodeInfos.put(pubkey, node); if (json.has("nodeName")) { node.nodeName = json.get("nodeName").getAsString(); - KeyValueDBUtil.instance.setValue( - NCTables.NodesDB.toString(), pubkey, node.nodeName); + KeyValueDBUtil.instance.setValue(NCTables.NodesDB.toString(), pubkey, + node.nodeName); LOGGER.info("set node name: " + nodeShortID + " -> " + node.nodeName); } if (json.has("masterAddress")) { @@ -274,30 +268,31 @@ public class NodeCenterActions { // TODO 如果是NC挂了,其他节点重连之后需要另外考虑 // 向该合约发送请求,令其查看自己崩溃前的集群合约 -// LOGGER.info("NodeCenter tells nodes new masters for contracts created before the crash!"); -// Map request = new HashMap<>(); -// request.put("action", "queryUnitContractsID"); -// try { -//// CMNode conn = nodeInfos.get(pubkey); -//// NodeCenterActions connection = conn.connection; -//// NodeCenterFrameHandler connController = connection.controller; -//// connController.sendMsg(JsonUtil.toJson(request)); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// Map onNewNodeConnected = new HashMap<>(); -// onNewNodeConnected.put("action", "onNewNodeConnected"); -// onNewNodeConnected.put("pubKey", pubkey); -// for (CMNode node : nodeInfos.values()) { -// if (node.pubKey.equals(pubkey)) { -// continue; -// } -// try { -// // node.connection.controller.sendMsg(JsonUtil.toJson(onNewNodeConnected)); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } + // LOGGER.info("NodeCenter tells nodes new masters for contracts created before the + // crash!"); + // Map request = new HashMap<>(); + // request.put("action", "queryUnitContractsID"); + // try { + //// CMNode conn = nodeInfos.get(pubkey); + //// NodeCenterActions connection = conn.connection; + //// NodeCenterFrameHandler connController = connection.controller; + //// connController.sendMsg(JsonUtil.toJson(request)); + // } catch (Exception e) { + // e.printStackTrace(); + // } + // Map onNewNodeConnected = new HashMap<>(); + // onNewNodeConnected.put("action", "onNewNodeConnected"); + // onNewNodeConnected.put("pubKey", pubkey); + // for (CMNode node : nodeInfos.values()) { + // if (node.pubKey.equals(pubkey)) { + // continue; + // } + // try { + // // node.connection.controller.sendMsg(JsonUtil.toJson(onNewNodeConnected)); + // } catch (Exception e) { + // e.printStackTrace(); + // } + // } rc.onResult(JsonUtil.toJson(r)); } @@ -319,7 +314,8 @@ public class NodeCenterActions { if (sessionID != null && SM2Util.plainStrVerify(pubkey, pubkey + sessionID, signature)) { r.data = "success"; NCConFlag = true; - } else r.data = "failed"; + } else + r.data = "failed"; rc.onResult(JsonUtil.toJson(r)); } @@ -327,10 +323,10 @@ public class NodeCenterActions { @Action(httpAccess = true) public void listCMInfo(JsonObject json, ResultCallback rc) { // 检查是否为在线节点 - // if (!json.containsKey("pubKey") || !nodeinfos.containsKey(json.get("pubKey"))) { - // LOGGER.error("node pubKey not valid " + json.get("pubKey")); - // return; - // } + // if (!json.containsKey("pubKey") || !nodeinfos.containsKey(json.get("pubKey"))) { + // LOGGER.error("node pubKey not valid " + json.get("pubKey")); + // return; + // } Response r = listCMInfoInterval(json); rc.onResult(JsonUtil.toJson(r)); } @@ -361,12 +357,9 @@ public class NodeCenterActions { // 如果有就更改json串,并发消息给该节点将Contract的isMaster设置为false Set contractIDS = new HashSet<>(); // 旧master需要自己非master的合约 // 当前节点的合约 - List contracts = - JsonUtil.fromJson( - json.get("contracts"), - new TypeToken>() { - }.getType()); - //MetaIndexAction.updateContractsIndex(contracts, rc); + List contracts = JsonUtil.fromJson(json.get("contracts"), + new TypeToken>() {}.getType()); + // MetaIndexAction.updateContractsIndex(contracts, rc); LOGGER.debug("update contracts: " + json.get("contracts")); int version = -1; if (json.has("contractVersion")) { @@ -376,8 +369,9 @@ public class NodeCenterActions { if (json.has("events")) { node.events = json.get("events").getAsInt(); } - //just ignore recover - if (nodeInfos != null) return; + // just ignore recover + if (nodeInfos != null) + return; // 遍历所有节点 for (CMNode cmNode : nodeInfos.values()) { if (cmNode.pubKey.equals(nodeID)) { @@ -394,8 +388,8 @@ public class NodeCenterActions { // 遍历当前节点的所有合约 for (ContractDesp thisDesp : contracts) { /// logger.debug("测试位置555 节点" + cmNode.pubKey.substring(0,5) + " - // 的合约 " + desp.contractID + " " + desp.isMaster + " 与 节点 " + - // nodeID.substring(0,5) + " 的合约 " + thisDesp.contractID + " " + + // 的合约 " + desp.contractID + " " + desp.isMaster + " 与 节点 " + + // nodeID.substring(0,5) + " 的合约 " + thisDesp.contractID + " " + // thisDesp.isMaster); if (!((desp.contractID).equals(thisDesp.contractID))) { @@ -406,14 +400,9 @@ public class NodeCenterActions { } if (desp.getIsMaster() && thisDesp.getIsMaster()) { - LOGGER.info( - "合约 " - + thisDesp.contractID - + " 的旧的master " - + node.pubKey.substring(0, 5) - + "重新上线后和现在master " - + cmNode.pubKey.substring(0, 5) - + "撞车!"); + LOGGER.info("合约 " + thisDesp.contractID + " 的旧的master " + + node.pubKey.substring(0, 5) + "重新上线后和现在master " + + cmNode.pubKey.substring(0, 5) + "撞车!"); contractIDS.add(thisDesp.contractID); } } @@ -424,12 +413,8 @@ public class NodeCenterActions { for (ContractDesp cd : contracts) { if (contractIDS.contains(cd.contractID)) { cd.setIsMaster(false); - LOGGER.info( - "设置合约 " - + cd.contractID - + " 的旧master " - + node.pubKey.substring(0, 5) - + " isMaster为false!"); + LOGGER.info("设置合约 " + cd.contractID + " 的旧master " + node.pubKey.substring(0, 5) + + " isMaster为false!"); con_ids.append(",").append(cd.contractID); } } @@ -458,10 +443,8 @@ public class NodeCenterActions { return; } for (CMNode node : nodeInfos.values()) { - if (node.nodeName.equals(nodeName) - || node.pubKey.equals(nodeName) - || node.udpID.equals(nodeName) - || node.ipPort.equals(nodeName)) { + if (node.nodeName.equals(nodeName) || node.pubKey.equals(nodeName) + || node.udpID.equals(nodeName) || node.ipPort.equals(nodeName)) { String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); Map req = new HashMap<>(); req.put("requestID", requestID); @@ -490,10 +473,11 @@ public class NodeCenterActions { } String requesterID = json.get("requesterNodeID").getAsString(); CMNode node = nodeInfos.get(requesterID); - if (node != null) node.connection.sendContractResult(json); + if (node != null) + node.connection.sendContractResult(json); if (requesterID.equals("NodeCenter")) { - sync.wakeUp( - json.get("requestID").getAsString(), json.get("contractResult").getAsString()); + sync.wakeUp(json.get("requestID").getAsString(), + json.get("contractResult").getAsString()); } } @@ -506,11 +490,8 @@ public class NodeCenterActions { rc.onResult(JsonUtil.toJson(r)); return; } - LOGGER.debug( - "tid:" - + Thread.currentThread().getId() - + "executeContractOnOtherNodes\n" - + JsonUtil.toJson(json)); + LOGGER.debug("tid:" + Thread.currentThread().getId() + "executeContractOnOtherNodes\n" + + JsonUtil.toJson(json)); String requestID = json.get("requestID").getAsString(); try { CMNode node; @@ -518,7 +499,8 @@ public class NodeCenterActions { ContractRequest cr = JsonUtil.fromJson(crStr, ContractRequest.class); String requesterNodeID = nodeID; for (String nodeID : nodeInfos.keySet()) { - if (nodeID.equals(this.nodeID)) continue; + if (nodeID.equals(this.nodeID)) + continue; node = nodeInfos.get(nodeID); if (node != null && node.contracts != null) for (ContractDesp cd : node.contracts) { @@ -529,11 +511,8 @@ public class NodeCenterActions { jo.addProperty("requesterNodeID", requesterNodeID); jo.addProperty("action", "executeContractLocally"); jo.addProperty("contractRequest", crStr); - LOGGER.debug( - "tid:" - + Thread.currentThread().getId() - + "execute in node:" - + node.nodeName); + LOGGER.debug("tid:" + Thread.currentThread().getId() + + "execute in node:" + node.nodeName); node.connection.controller.sendMsg(JsonUtil.toJson(jo)); return; } @@ -543,10 +522,8 @@ public class NodeCenterActions { e.printStackTrace(); } LOGGER.debug("tid:" + Thread.currentThread().getId() + "can't locate in NodeCenter!"); - ContractResult cResult = - new ContractResult( - Status.Error, - new JsonPrimitive("can't locate the Contract in node center")); + ContractResult cResult = new ContractResult(Status.Error, + new JsonPrimitive("can't locate the Contract in node center")); JsonObject jo = new JsonObject(); jo.addProperty("action", "onReceiveContractExecution"); jo.addProperty("requestID", requestID); @@ -591,241 +568,150 @@ public class NodeCenterActions { response.put("data", "can not locate contractID: " + json.get("contractID")); cb.onResult(JsonUtil.toJson(response)); } - sync.sleep( - requestID, - new ResultCallback() { - @Override - public void onResult(String str) { - ContractResult ret = JsonUtil.fromJson(str, ContractResult.class); - Map converted = new HashMap<>(); - converted.put("status", ret.status); - try { - List> je = - JsonUtil.fromJson( - ret.result, - new TypeToken< - List>>() { - }.getType()); - converted.put("result", je); - } catch (Exception e) { - converted.put("result", ret.result); - } - String retStr = JsonUtil.toJson(converted); - cb.onResult(retStr); - } - }); + sync.sleep(requestID, new ResultCallback() { + @Override + public void onResult(String str) { + ContractResult ret = JsonUtil.fromJson(str, ContractResult.class); + Map converted = new HashMap<>(); + converted.put("status", ret.status); + try { + List> je = JsonUtil.fromJson(ret.result, + new TypeToken>>() {}.getType()); + converted.put("result", je); + } catch (Exception e) { + converted.put("result", ret.result); + } + String retStr = JsonUtil.toJson(converted); + cb.onResult(retStr); + } + }); } - /* // judge the just online node whwther need to restart contracts,send the contracts'info - public static void restartContracts(String nodePubKey) { - if (!recoverMap.containsKey(nodePubKey)) { - return; - } - - // 恢复该节点的每一个集群运行的合约 - for (ContractRecord record : recoverMap.get(nodePubKey).values()) { - String contractID = record.contractID; - if (record.recoverFlag != RecoverFlag.ToRecover) continue; - - // 先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests - Map request = new HashMap<>(); - request.put("action", "setRecovering"); - request.put("contractID", contractID); - CMNode node = nodeinfos.get(nodePubKey); - node.connection.controller.sendMsg(gson.toJson(request)); - - System.out.println( - "第一步 : [NodeCeterActions] restartContracts 开始处理合约 contractID=" + contractID); - - if (contractID2Members.containsKey(contractID)) { - - // 在nodeinfos中找节点,该节点的pubKey在pubKeys中 - MultiPointContractInfo info = contractID2Members.get(contractID); - CMNode cmNode = null; - for (int i = 0; i < info.members.size(); i++) { - int size = info.members.size(); - String tempNodeID = info.members.get(record.order.incrementAndGet() % size); - - if (nodeinfos.containsKey(tempNodeID)) - cmNode = NodeCenterActions.nodeinfos.get(tempNodeID); - else continue; - - System.out.println("查询节点 " + cmNode.nodeName); - - if (cmNode != null && !cmNode.pubKey.equals(nodePubKey)) { - System.out.println("第二步 : [NodeCenterActions] 找到一个依赖恢复节点,其节点名为 " + - cmNode.nodeName); - - Map req = new HashMap(); - req.put("action", "dumpCurrentState"); - req.put("contractID", contractID); - req.put("targetNodePubkey", nodePubKey); - - // NC向该节点发送请求,让其存储自身当前状态并发给NC - cmNode.connection.controller.sendMsg(gson.toJson(req)); - - return; - } - } - - if (cmNode == null) { - logger.debug("[NodeCenterActions] Can't find a recover rely node!"); - } - } - } - }*/ + /* + * // judge the just online node whwther need to restart contracts,send the contracts'info + * public static void restartContracts(String nodePubKey) { if + * (!recoverMap.containsKey(nodePubKey)) { return; } + * + * // 恢复该节点的每一个集群运行的合约 for (ContractRecord record : recoverMap.get(nodePubKey).values()) { + * String contractID = record.contractID; if (record.recoverFlag != RecoverFlag.ToRecover) + * continue; + * + * // 先发消息,让恢复节点的该合约收到消息后只加入队列不dealRequests Map request = new HashMap<>(); + * request.put("action", "setRecovering"); request.put("contractID", contractID); CMNode node = + * nodeinfos.get(nodePubKey); node.connection.controller.sendMsg(gson.toJson(request)); + * + * System.out.println( "第一步 : [NodeCeterActions] restartContracts 开始处理合约 contractID=" + + * contractID); + * + * if (contractID2Members.containsKey(contractID)) { + * + * // 在nodeinfos中找节点,该节点的pubKey在pubKeys中 MultiPointContractInfo info = + * contractID2Members.get(contractID); CMNode cmNode = null; for (int i = 0; i < + * info.members.size(); i++) { int size = info.members.size(); String tempNodeID = + * info.members.get(record.order.incrementAndGet() % size); + * + * if (nodeinfos.containsKey(tempNodeID)) cmNode = NodeCenterActions.nodeinfos.get(tempNodeID); + * else continue; + * + * System.out.println("查询节点 " + cmNode.nodeName); + * + * if (cmNode != null && !cmNode.pubKey.equals(nodePubKey)) { + * System.out.println("第二步 : [NodeCenterActions] 找到一个依赖恢复节点,其节点名为 " + cmNode.nodeName); + * + * Map req = new HashMap(); req.put("action", + * "dumpCurrentState"); req.put("contractID", contractID); req.put("targetNodePubkey", + * nodePubKey); + * + * // NC向该节点发送请求,让其存储自身当前状态并发给NC cmNode.connection.controller.sendMsg(gson.toJson(req)); + * + * return; } } + * + * if (cmNode == null) { logger.debug("[NodeCenterActions] Can't find a recover rely node!"); } + * } } } + */ // zyx modified - /* @Action(async = true) - public void receiveState(Map args, final ResultCallback rc) { - //System.out.println("第五步 : [NodeCenterActions] 开始从恢复依赖节点接收state"); - - String fileName = args.get("fileName") + "_NC"; - boolean isAppend = Boolean.valueOf(args.get("isAppend")); - boolean isDone = Boolean.valueOf(args.get("isDone")); - - //logger.debug("[NodeCenterActions] isAppend=" + isAppend + " isDone=" + isDone); - - String path = "./temp/stateFiles/" + fileName; - File file = new File(path); - File dir = file.getParentFile(); - if (!dir.exists()) { - dir.mkdirs(); - } - - FileOutputStream fout = null; - if (!isAppend) { - try { - fout = new FileOutputStream(file); - fileMap.put(fileName, fout); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } else { - fout = fileMap.get(fileName); - } - - if (isDone) { - if (fout != null) { - try { - fout.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - // TODO 参数可以加入文件名? - //System.out.println("第七步 : [NodeCenterActions] 从恢复依赖节点接收state完成"); - - //logger.debug("[NodeCenterActions] receive state finish."); - String receiveFileName = new File(dir, fileName).getAbsolutePath(); - - // 发送state给需要恢复的节点 - sendState(fileName, args.get("targetNodePubkey")); - } else { - String data = args.get("data"); - try { - String progress = args.get("progress"); - // TODO pulish progress - fout.write(ByteUtil.decodeBASE64(data)); - } catch (IOException e) { - e.printStackTrace(); - } - } - }*/ + /* + * @Action(async = true) public void receiveState(Map args, final ResultCallback + * rc) { //System.out.println("第五步 : [NodeCenterActions] 开始从恢复依赖节点接收state"); + * + * String fileName = args.get("fileName") + "_NC"; boolean isAppend = + * Boolean.valueOf(args.get("isAppend")); boolean isDone = Boolean.valueOf(args.get("isDone")); + * + * //logger.debug("[NodeCenterActions] isAppend=" + isAppend + " isDone=" + isDone); + * + * String path = "./temp/stateFiles/" + fileName; File file = new File(path); File dir = + * file.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } + * + * FileOutputStream fout = null; if (!isAppend) { try { fout = new FileOutputStream(file); + * fileMap.put(fileName, fout); } catch (FileNotFoundException e) { e.printStackTrace(); } } + * else { fout = fileMap.get(fileName); } + * + * if (isDone) { if (fout != null) { try { fout.close(); } catch (IOException e) { + * e.printStackTrace(); } } // TODO 参数可以加入文件名? + * //System.out.println("第七步 : [NodeCenterActions] 从恢复依赖节点接收state完成"); + * + * //logger.debug("[NodeCenterActions] receive state finish."); String receiveFileName = new + * File(dir, fileName).getAbsolutePath(); + * + * // 发送state给需要恢复的节点 sendState(fileName, args.get("targetNodePubkey")); } else { String data = + * args.get("data"); try { String progress = args.get("progress"); // TODO pulish progress + * fout.write(ByteUtil.decodeBASE64(data)); } catch (IOException e) { e.printStackTrace(); } } } + */ // The resultCallback is an Counter, it will invoke // in its // onResult // zyx modified - /* public void sendState(String path, String targetNodePubkey) { - File project = new File("./temp/stateFiles/" + path); - - // 将合约中的memberd加入ContractRecord - ContractRecord record = null; - try { - FileInputStream fileout = new FileInputStream(project); - GZIPInputStream gzin = new GZIPInputStream(fileout); - ObjectInputStream reader = new ObjectInputStream(gzin); - record = (ContractRecord) reader.readObject(); - reader.close(); - } catch (IOException | ClassNotFoundException e) { - e.printStackTrace(); - } - // record.members = contractID2MembersUDP.get(record.contractID); - //NC delete state file - if(project.isFile() && project.exists()){ - project.delete(); - } - - record.members = new HashMap<>(recoverMap.get(nodeID).get(record.contractID).members); - - File file = new File("./temp/stateFiles/" + path); - File parent = file.getParentFile(); - if (!parent.exists()) parent.mkdirs(); - ObjectOutputStream writer; - try { - FileOutputStream fileout = new FileOutputStream(file); - GZIPOutputStream out = new GZIPOutputStream(fileout); - writer = new ObjectOutputStream(out); - writer.writeObject(record); - writer.flush(); - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - //System.out.println("第八步 : [NodeCenterActions] 开始向需要恢复的节点转发State"); - //record.printContent(); - - CMNode targetNode = nodeinfos.get(targetNodePubkey); - - Map req = new HashMap<>(); - req.put("action", "receiveState"); - req.put("isAppend", "false"); - req.put("fileName", path); - req.put("isDone", "false"); - String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); - req.put("requestID", requestID); - - if (project.isFile()) { - try { - FileInputStream fin = new FileInputStream(project); - byte[] buff = new byte[30 * 1024]; - long count = 0; - long total = project.length(); - - for (int len = 0; (len = (fin.read(buff))) > 0; ) { - //logger.debug("len = " + len); - String data = ByteUtil.encodeBASE64(buff, len); - req.put("data", data); - count += len; - targetNode.connection.controller.sendMsg(gson.toJson(req)); - req.put("isAppend", "true"); - - Thread.sleep(300); - } - fin.close(); - - req.put("isDone", "true"); - req.remove("data"); - - targetNode.connection.controller.sendMsg(gson.toJson(req)); - //NC delete state file - if(file.isFile() && file.exists()){ - file.delete(); - } - - //logger.debug("[NodeCenterActions] send state project finish."); - - //System.out.println("第十步 : [NodeCenterActions] 向需要恢复的节点转发State完成"); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - }*/ + /* + * public void sendState(String path, String targetNodePubkey) { File project = new + * File("./temp/stateFiles/" + path); + * + * // 将合约中的memberd加入ContractRecord ContractRecord record = null; try { FileInputStream fileout = + * new FileInputStream(project); GZIPInputStream gzin = new GZIPInputStream(fileout); + * ObjectInputStream reader = new ObjectInputStream(gzin); record = (ContractRecord) + * reader.readObject(); reader.close(); } catch (IOException | ClassNotFoundException e) { + * e.printStackTrace(); } // record.members = contractID2MembersUDP.get(record.contractID); //NC + * delete state file if(project.isFile() && project.exists()){ project.delete(); } + * + * record.members = new HashMap<>(recoverMap.get(nodeID).get(record.contractID).members); + * + * File file = new File("./temp/stateFiles/" + path); File parent = file.getParentFile(); if + * (!parent.exists()) parent.mkdirs(); ObjectOutputStream writer; try { FileOutputStream fileout + * = new FileOutputStream(file); GZIPOutputStream out = new GZIPOutputStream(fileout); writer = + * new ObjectOutputStream(out); writer.writeObject(record); writer.flush(); writer.close(); } + * catch (IOException e) { e.printStackTrace(); } + * + * //System.out.println("第八步 : [NodeCenterActions] 开始向需要恢复的节点转发State"); + * //record.printContent(); + * + * CMNode targetNode = nodeinfos.get(targetNodePubkey); + * + * Map req = new HashMap<>(); req.put("action", "receiveState"); + * req.put("isAppend", "false"); req.put("fileName", path); req.put("isDone", "false"); String + * requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); + * req.put("requestID", requestID); + * + * if (project.isFile()) { try { FileInputStream fin = new FileInputStream(project); byte[] buff + * = new byte[30 * 1024]; long count = 0; long total = project.length(); + * + * for (int len = 0; (len = (fin.read(buff))) > 0; ) { //logger.debug("len = " + len); String + * data = ByteUtil.encodeBASE64(buff, len); req.put("data", data); count += len; + * targetNode.connection.controller.sendMsg(gson.toJson(req)); req.put("isAppend", "true"); + * + * Thread.sleep(300); } fin.close(); + * + * req.put("isDone", "true"); req.remove("data"); + * + * targetNode.connection.controller.sendMsg(gson.toJson(req)); //NC delete state file + * if(file.isFile() && file.exists()){ file.delete(); } + * + * //logger.debug("[NodeCenterActions] send state project finish."); + * + * //System.out.println("第十步 : [NodeCenterActions] 向需要恢复的节点转发State完成"); } catch (IOException e) + * { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block + * e.printStackTrace(); } } } + */ @Action public void onReceiveSyncRequest(JsonObject jo, final ResultCallback cb) { @@ -886,7 +772,8 @@ public class NodeCenterActions { boolean hasResult = false; for (String nodeID : nodeInfos.keySet()) { - if (nodeID.equals(this.nodeID)) continue; + if (nodeID.equals(this.nodeID)) + continue; CMNode node = nodeInfos.get(nodeID); if (node != null && node.contracts != null) for (ContractDesp cd : node.contracts) { @@ -898,30 +785,26 @@ public class NodeCenterActions { jo.addProperty("action", "executeContractLocally"); jo.addProperty("contractRequest", crStr); node.connection.controller.sendMsg(JsonUtil.toJson(jo)); - sync.sleep( - requestID, - new ResultCallback() { - @Override - public void onResult(String str) { - ContractResult cr = - JsonUtil.fromJson(str, ContractResult.class); - ret.add("result", cr.result); - ret.addProperty("status", cr.status + ""); - long executeTime = (System.currentTimeMillis() - start); - ret.addProperty("executeTime", executeTime + ""); - String retStr = JsonUtil.toJson(ret); - rc.onResult(retStr); - } - }); + sync.sleep(requestID, new ResultCallback() { + @Override + public void onResult(String str) { + ContractResult cr = JsonUtil.fromJson(str, ContractResult.class); + ret.add("result", cr.result); + ret.addProperty("status", cr.status + ""); + long executeTime = (System.currentTimeMillis() - start); + ret.addProperty("executeTime", executeTime + ""); + String retStr = JsonUtil.toJson(ret); + rc.onResult(retStr); + } + }); hasResult = true; break; } } } if (!hasResult) { - ContractResult result = - new ContractResult( - Status.Error, new JsonPrimitive("Can't locate contract At NodeCenter")); + ContractResult result = new ContractResult(Status.Error, + new JsonPrimitive("Can't locate contract At NodeCenter")); ret.addProperty("result", JsonUtil.toJson(result)); long executeTime = (System.currentTimeMillis() - start); ret.addProperty("executeTime", executeTime + ""); @@ -975,8 +858,8 @@ public class NodeCenterActions { } // cb will invoke sendProject in the cluster in its onResult method. - public void requestProject( - String projectName, String pubkey, boolean isPrivate, ResultCallback cb) { + public void requestProject(String projectName, String pubkey, boolean isPrivate, + ResultCallback cb) { LOGGER.debug("[NodeCenterActions] requestProject : -----------position2"); Map req = new HashMap<>(); @@ -995,8 +878,8 @@ public class NodeCenterActions { // The resultCallback is an Counter, it will invoke // in its // onResult - public void sendProject( - String filePath, String isPrivate, String pubKey, String nodeIP, ResultCallback result) { + public void sendProject(String filePath, String isPrivate, String pubKey, String nodeIP, + ResultCallback result) { LOGGER.debug("sendProject : position----6" + filePath); File project = new File(filePath); @@ -1027,7 +910,7 @@ public class NodeCenterActions { long step = total / 10; LOGGER.debug("ypk = " + total); - for (int len = 0; (len = (fin.read(buff))) > 0; ) { + for (int len = 0; (len = (fin.read(buff))) > 0;) { LOGGER.debug("len = " + len); String data = ByteUtil.encodeBASE64(buff, len); req.put("data", data); @@ -1074,39 +957,29 @@ public class NodeCenterActions { } /* - @Action(async = true) - public void startContractInEachNode(Map args,final ResultCallback rc) { - logger.debug("[NodeCenterActions] startContractInEachNode : "); - - Map req = new HashMap<>(); - req.put("action", "startContractByYpk"); - req.put("fileName", args.get("fileName")); - req.put("contractOwner",args.get("pubKey")); - req.put("contractSig",args.get("signature")); - - String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); - req.put("requestID", requestID); - sync.sleep(requestID, rc); - controller.sendMsg(gson.toJson(req)); - } - - - @Action(async = true) - public void onStartEachNode(Map args, final ResultCallback rc) { - logger.debug("[NodeCenterActions] onStartEachNode : "); - - logger.debug("---action" + args.get("action")); - logger.debug("---requestID" + args.get("requestID")); - logger.debug("---data" + args.get("data")); - logger.debug("---cid" + args.get("cid")); - logger.debug("---executeTime" + args.get("executeTime")); - - - String str = "onStartEachNode;" + args.get("data") + ";" + args.get("cid") + ";" + - args.get("executeTime"); - sync.wakeUp(args.get("requestID"),str); - } - */ + * @Action(async = true) public void startContractInEachNode(Map args,final + * ResultCallback rc) { logger.debug("[NodeCenterActions] startContractInEachNode : "); + * + * Map req = new HashMap<>(); req.put("action", "startContractByYpk"); + * req.put("fileName", args.get("fileName")); req.put("contractOwner",args.get("pubKey")); + * req.put("contractSig",args.get("signature")); + * + * String requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); + * req.put("requestID", requestID); sync.sleep(requestID, rc); + * controller.sendMsg(gson.toJson(req)); } + * + * + * @Action(async = true) public void onStartEachNode(Map args, final + * ResultCallback rc) { logger.debug("[NodeCenterActions] onStartEachNode : "); + * + * logger.debug("---action" + args.get("action")); logger.debug("---requestID" + + * args.get("requestID")); logger.debug("---data" + args.get("data")); logger.debug("---cid" + + * args.get("cid")); logger.debug("---executeTime" + args.get("executeTime")); + * + * + * String str = "onStartEachNode;" + args.get("data") + ";" + args.get("cid") + ";" + + * args.get("executeTime"); sync.wakeUp(args.get("requestID"),str); } + */ @Action(async = true) public void receiveProject(JsonObject args, final ResultCallback rc) { @@ -1211,63 +1084,57 @@ public class NodeCenterActions { } } - // static boolean clearCache() { + // static boolean clearCache() { // - // if (requestCache.isEmpty()) return true; + // if (requestCache.isEmpty()) return true; // - // final long time = System.currentTimeMillis() - 120000L; // 120s - // requestCache - // .entrySet() - // .removeIf( - // entry -> { - // RequestCache cache = entry.getValue(); - // if (cache == null) return true; - // return cache.getTime() < time; - // }); + // final long time = System.currentTimeMillis() - 120000L; // 120s + // requestCache + // .entrySet() + // .removeIf( + // entry -> { + // RequestCache cache = entry.getValue(); + // if (cache == null) return true; + // return cache.getTime() < time; + // }); // - // return false; - // } + // return false; + // } - // static RequestCache getCache(String contractID) { - // if (contractID != null) { - // RequestCache cache = requestCache.get(contractID); - // if (cache != null) return cache; - // else { - // logger.debug("[NodeCenterActions] create requestcache:" + contractID); - // RequestCache reqc = new RequestCache(); - // requestCache.put(contractID, reqc); - // return reqc; - // } - // } - // return null; - // } + // static RequestCache getCache(String contractID) { + // if (contractID != null) { + // RequestCache cache = requestCache.get(contractID); + // if (cache != null) return cache; + // else { + // logger.debug("[NodeCenterActions] create requestcache:" + contractID); + // RequestCache reqc = new RequestCache(); + // requestCache.put(contractID, reqc); + // return reqc; + // } + // } + // return null; + // } - /* @Action(async = true) - public void sendCachedRequests(Map args, final ResultCallback rc) { - String contractID = args.get("contractID"); - int start = Integer.parseInt(args.get("start")); - int end = Integer.parseInt(args.get("end")); + /* + * @Action(async = true) public void sendCachedRequests(Map args, final + * ResultCallback rc) { String contractID = args.get("contractID"); int start = + * Integer.parseInt(args.get("start")); int end = Integer.parseInt(args.get("end")); + * + * RequestCache cache = getCache(contractID); for (int i = start + 1; i < end; i++) { if (cache + * == null || !cache.containsKey(i)) { + * + * // this node crash String nodeID = args.get("nodeID"); restartContracts(nodeID); } JsonObject + * jo = cache.get(i); controller.sendMsg(JsonUtil.toJson(jo)); + * + * logger.debug("NC发送第 " + jo.get("seq").getAsString() + " 个请求给Node"); } } + */ - RequestCache cache = getCache(contractID); - for (int i = start + 1; i < end; i++) { - if (cache == null || !cache.containsKey(i)) { - - // this node crash - String nodeID = args.get("nodeID"); - restartContracts(nodeID); - } - JsonObject jo = cache.get(i); - controller.sendMsg(JsonUtil.toJson(jo)); - - logger.debug("NC发送第 " + jo.get("seq").getAsString() + " 个请求给Node"); - } - }*/ - - /* @Action(async = true) - public void recoverFinish(Map args, final ResultCallback rc) { - ContractRecord cr = recoverMap.get(args.get("nodeID")).get(args.get("contractID")); - if (cr.recoverFlag == RecoverFlag.ToRecover) cr.recoverFlag = RecoverFlag.Fine; - }*/ + /* + * @Action(async = true) public void recoverFinish(Map args, final + * ResultCallback rc) { ContractRecord cr = + * recoverMap.get(args.get("nodeID")).get(args.get("contractID")); if (cr.recoverFlag == + * RecoverFlag.ToRecover) cr.recoverFlag = RecoverFlag.Fine; } + */ @Action(async = true) public void electMaster(JsonObject args, final ResultCallback rc) { @@ -1282,31 +1149,31 @@ public class NodeCenterActions { String masterID = args.get("master").getAsString(); // 旧的master String members = args.get("members").getAsString(); // 执行这个合约所有节点的pubkey String uniNumber = null; - if (args.has("uniNumber")) uniNumber = args.get("uniNumber").getAsString(); + if (args.has("uniNumber")) + uniNumber = args.get("uniNumber").getAsString(); - // if (nodeinfos.containsKey(masterID)) { - // CMNode node = nodeinfos.get(masterID); - // synchronized (node) { - // for (ContractDesp cd : node.contracts) { - // if (cd.contractID.equals(contractID) || + // if (nodeinfos.containsKey(masterID)) { + // CMNode node = nodeinfos.get(masterID); + // synchronized (node) { + // for (ContractDesp cd : node.contracts) { + // if (cd.contractID.equals(contractID) || // cd.contractName.equals(contractID)) { - // cd.setIsMaster(false); - // LOGGER.debug( - // "选举步骤1---- 设置节点 " - // + node.pubKey.substring(0, 5) - // + " 的合约 " - // + contractID - // + " isMaster=" - // + false); - // } - // } - // } - // } + // cd.setIsMaster(false); + // LOGGER.debug( + // "选举步骤1---- 设置节点 " + // + node.pubKey.substring(0, 5) + // + " 的合约 " + // + contractID + // + " isMaster=" + // + false); + // } + // } + // } + // } synchronized (NCElectMasterUtil.electInfos) { if (!NCElectMasterUtil.electInfos.containsKey(contractID)) { - NCElectMasterUtil.electInfos.put( - contractID, + NCElectMasterUtil.electInfos.put(contractID, new NCElectMasterUtil.ElectInfo(masterID, contractID, members, uniNumber)); } NCElectMasterUtil.ElectInfo eleInfo = NCElectMasterUtil.electInfos.get(contractID); @@ -1321,7 +1188,8 @@ public class NodeCenterActions { // JsonObject for (Map.Entry stringCMNodeEntry : nodeInfos.entrySet()) { CMNode node = stringCMNodeEntry.getValue(); - if (node.contracts == null) continue; + if (node.contracts == null) + continue; for (ContractDesp desp : node.contracts) { if (desp.type == ContractExecType.Sole || desp.getIsMaster()) { array.add(extractContractRoute(node, desp)); @@ -1368,20 +1236,13 @@ public class NodeCenterActions { } for (Map.Entry stringCMNodeEntry : nodeInfos.entrySet()) { CMNode node = stringCMNodeEntry.getValue(); - if (node.contracts == null) continue; + if (node.contracts == null) + continue; for (ContractDesp desp : node.contracts) { if (contractID.equals(desp.contractID) || contractID.equals(desp.contractName)) { - LOGGER.info( - "查看合约 " - + desp.contractID - + " " - + desp.contractName - + " 节点 " - + node.pubKey.substring(0, 5) - + " " - + desp.type - + " " - + desp.getIsMaster()); + LOGGER.info("查看合约 " + desp.contractID + " " + desp.contractName + " 节点 " + + node.pubKey.substring(0, 5) + " " + desp.type + " " + + desp.getIsMaster()); if (desp.type == ContractExecType.Sole) { args.add("result", extractContractRoute(node, desp)); } else if (desp.getIsMaster()) { @@ -1393,31 +1254,25 @@ public class NodeCenterActions { } } - /* logger.debug("查看合约 " + contractID + " 的master为 " + args.get("result")); - if(!args.containsKey("result") || args.get("result") == null || args.get("result").equals("null")){ - //NC发起各个节点重选 - logger.debug("NC发现合约 " + contractID + " 的master为null,NC向该合约的各个节点发起重选请求!"); - Map req = new HashMap<>(); - req.put("action","NCStartElect"); - req.put("contractID",contractID); - for(CMNode node : nodeinfos.values()){ - node.connection.controller.sendMsg(JsonUtil.toJson(req)); - break; - } - }*/ + /* + * logger.debug("查看合约 " + contractID + " 的master为 " + args.get("result")); + * if(!args.containsKey("result") || args.get("result") == null || + * args.get("result").equals("null")){ //NC发起各个节点重选 logger.debug("NC发现合约 " + contractID + + * " 的master为null,NC向该合约的各个节点发起重选请求!"); Map req = new HashMap<>(); + * req.put("action","NCStartElect"); req.put("contractID",contractID); for(CMNode node : + * nodeinfos.values()){ node.connection.controller.sendMsg(JsonUtil.toJson(req)); break; } } + */ if (args.get("result").equals("null")) { LOGGER.info("[NodeCenterActions] query route by info from other NC"); // 查询其他节点的信息 String s = OtherNCProxy.instance.search(contractID); - LOGGER.info( - "[NodeCenterActions] query route by info from other NC " - + (s == null ? "null" : s)); + LOGGER.info("[NodeCenterActions] query route by info from other NC " + + (s == null ? "null" : s)); if (s != null) { String[] ss = s.split(";"); - args.addProperty( - "result", + args.addProperty("result", "{\"pubKey\":\"" + ss[0] + "\",\"masterAddress\":\"" + ss[1] + "\"}"); } } @@ -1430,8 +1285,7 @@ public class NodeCenterActions { String contractID = args.get("contractID").getAsString(); LOGGER.debug("查看合约 " + contractID + " 的master为 " + args.get("result")); - if (!args.has("result") - || args.get("result") == null + if (!args.has("result") || args.get("result") == null || args.get("result").getAsString().equals("null")) { // NC发起各个节点重选 LOGGER.debug("NC发现合约 " + contractID + " 的master为null,NC向该合约的各个节点发起重选请求!"); @@ -1441,40 +1295,36 @@ public class NodeCenterActions { req.put("uniNumber", System.currentTimeMillis() + "_" + new Random().nextLong()); for (CMNode node : nodeInfos.values()) { node.connection.controller.sendMsg(JsonUtil.toJson(req)); - LOGGER.info( - "[ADSP] [NodeCenterActions] 发现合约 " - + contractID - + " 的master为null 向节点 " - + node.nodeName - + "发送选举请求"); + LOGGER.info("[ADSP] [NodeCenterActions] 发现合约 " + contractID + " 的master为null 向节点 " + + node.nodeName + "发送选举请求"); } } } // 另可能仅和旧的master断开和NC连接的节点连新的master - // @Action(async = true) - // public void updateFormerMaster(JsonObject args, final ResultCallback rc) { - // String[] offlines = args.get("offlines").getAsString().split(","); - // String contractID = args.get("contractID").getAsString(); - // String master = args.get("master").getAsString(); + // @Action(async = true) + // public void updateFormerMaster(JsonObject args, final ResultCallback rc) { + // String[] offlines = args.get("offlines").getAsString().split(","); + // String contractID = args.get("contractID").getAsString(); + // String master = args.get("master").getAsString(); // - // for (String nodeID : offlines) { - // if (nodeinfos.containsKey(nodeID)) { - // CMNode node = nodeinfos.get(nodeID); - // for (ContractDesp desp : node.contracts) { - // if (desp.contractID.equals(contractID) - // || desp.contractName.equals(contractID)) { - // Map request = new HashMap<>(); - // request.put("action", "setClientNotMaster"); - // request.put("contractID", contractID); - // node.connection.controller.sendMsg(JsonUtil.toJson(request)); - // request.put("action", "queryUnitContractsID"); - // node.connection.controller.sendMsg(JsonUtil.toJson(request)); - // } - // } - // } - // } - // } + // for (String nodeID : offlines) { + // if (nodeinfos.containsKey(nodeID)) { + // CMNode node = nodeinfos.get(nodeID); + // for (ContractDesp desp : node.contracts) { + // if (desp.contractID.equals(contractID) + // || desp.contractName.equals(contractID)) { + // Map request = new HashMap<>(); + // request.put("action", "setClientNotMaster"); + // request.put("contractID", contractID); + // node.connection.controller.sendMsg(JsonUtil.toJson(request)); + // request.put("action", "queryUnitContractsID"); + // node.connection.controller.sendMsg(JsonUtil.toJson(request)); + // } + // } + // } + // } + // } @Action(async = true) public void changeMaster(JsonObject args, final ResultCallback rc) { @@ -1486,15 +1336,8 @@ public class NodeCenterActions { for (ContractDesp cd : node.contracts) { if (cd.contractID.equals(contractID) || cd.contractName.equals(contractID)) { cd.setIsMaster(true); - LOGGER.debug( - "设置节点 " - + node.pubKey.substring(0, 5) - + " 的合约 " - + cd.contractID - + " " - + cd.contractName - + " isMaster=" - + cd.getIsMaster()); + LOGGER.debug("设置节点 " + node.pubKey.substring(0, 5) + " 的合约 " + cd.contractID + + " " + cd.contractName + " isMaster=" + cd.getIsMaster()); } } } @@ -1514,11 +1357,12 @@ public class NodeCenterActions { // String sponsorPubKey = args.get("sponsorPubKey").getAsString(); String sponsorPubkey = args.get("sponsorPubkey").getAsString(); LOGGER.debug("sponsorPubkey=" + sponsorPubkey); - String[] strs = - args.get("nodeIDs").getAsString().split(","); // all nodes' pubKey in the unit + String[] strs = args.get("nodeIDs").getAsString().split(","); // all nodes' pubKey in the + // unit Set nodePubKeys = new HashSet<>(); // nodes' pubkey for (String str : strs) { - if (str != null && str.length() > 0) nodePubKeys.add(str); + if (str != null && str.length() > 0) + nodePubKeys.add(str); } Map nodes = new HashMap<>(); // node's pubKey-node'a name for (CMNode node : NodeCenterActions.nodeInfos.values()) { @@ -1538,9 +1382,8 @@ public class NodeCenterActions { // 身份验证 - boolean result = - SM2Util.plainStrVerify( - pubKey, "DistributeContract|" + projectName + "|" + pubKey, signature); + boolean result = SM2Util.plainStrVerify(pubKey, + "DistributeContract|" + projectName + "|" + pubKey, signature); LOGGER.debug("[UnitAcitons] 验证:" + result + " -> projectName:" + projectName); @@ -1631,12 +1474,8 @@ public class NodeCenterActions { JsonObject ret = new JsonObject(); ret.addProperty("action", "onQueryAEState"); if (json.has("caller")) { - ret.add( - "result", - JsonParser.parseString( - JsonUtil.toJson( - ActionExecutor.getStatistic( - json.get("caller").getAsString())))); + ret.add("result", JsonParser.parseString(JsonUtil + .toJson(ActionExecutor.getStatistic(json.get("caller").getAsString())))); } else { ret.add("result", JsonParser.parseString(JsonUtil.toJson(ActionExecutor.getAllData()))); } @@ -1655,7 +1494,8 @@ public class NodeCenterActions { public void publishEvent(JsonObject args, final ResultCallback rcb) { try { args.addProperty("action", "publishEventFromCenter"); - nodeInfos.values().iterator().next().connection.controller.sendMsg(JsonUtil.toJson(args)); + nodeInfos.values().iterator().next().connection.controller + .sendMsg(JsonUtil.toJson(args)); } catch (Exception e) { LOGGER.error("publishEvent error! " + e.getMessage()); } diff --git a/src/main/java/org/bdware/server/nodecenter/NodeCenterFrameHandler.java b/src/main/java/org/bdware/server/nodecenter/NodeCenterFrameHandler.java index 26ebd63..27b47c7 100644 --- a/src/main/java/org/bdware/server/nodecenter/NodeCenterFrameHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/NodeCenterFrameHandler.java @@ -34,40 +34,31 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler this.actions = new NodeCenterActions(this); MetaIndexAction.controller = this; // TODO 添加那个UnitAction. - ae = - new ActionExecutor( - executorService, actions, new MetaIndexAction()) { - @Override - public boolean checkPermission( - Action a, final JsonObject args, long permission) { - long val = a.userPermission(); - boolean flag; - String status = "refuse"; - String action = args.get("action").getAsString(); - if (val == 0) { - flag = true; - status = "accept"; - } else if ((permission & val) == val) { - flag = true; - status = "accept"; - } else { - flag = false; - } - if (!action.contains("checkAlive")) - NodeCenterServer.nodeTcpLogDB.put( - action, - "{\"action\":\"" - + action - + "\",\"pubKey\":\"" - + pubKey - + "\",\"status\":\"" - + status - + "\",\"date\":" - + System.currentTimeMillis() - + "}"); - return flag; - } - }; + ae = new ActionExecutor(executorService, actions, + new MetaIndexAction()) { + @Override + public boolean checkPermission(Action a, final JsonObject args, long permission) { + long val = a.userPermission(); + boolean flag; + String status = "refuse"; + String action = args.get("action").getAsString(); + if (val == 0) { + flag = true; + status = "accept"; + } else if ((permission & val) == val) { + flag = true; + status = "accept"; + } else { + flag = false; + } + if (!action.contains("checkAlive")) + NodeCenterServer.nodeTcpLogDB.put(action, + "{\"action\":\"" + action + "\",\"pubKey\":\"" + pubKey + + "\",\"status\":\"" + status + "\",\"date\":" + + System.currentTimeMillis() + "}"); + return flag; + } + }; LOGGER.info("create NodeCenterFrameHandler instance succeed!"); } @@ -80,10 +71,9 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler this.ctx = ctx; JsonObject map; try { - map = - JsonParser.parseReader( - new InputStreamReader(new ByteBufInputStream((ByteBuf) frame))) - .getAsJsonObject(); + map = JsonParser + .parseReader(new InputStreamReader(new ByteBufInputStream((ByteBuf) frame))) + .getAsJsonObject(); } catch (Exception e) { LOGGER.error("can't handle msg! " + e.getMessage()); @@ -93,15 +83,12 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler try { final String action = map.get("action").getAsString(); // System.out.println("[NodeCenterFramHandler] handle:" + action); - ae.handle( - action, - map, - new ResultCallback() { - @Override - public void onResult(String ret) { - sendMsg(ret); - } - }); + ae.handle(action, map, new ResultCallback() { + @Override + public void onResult(String ret) { + sendMsg(ret); + } + }); } catch (IllegalArgumentException e) { response = new Response(); response.action = "onException"; @@ -117,12 +104,9 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler StringBuilder ret = new StringBuilder(); int count = 0; for (String s : strs) { - if (s.contains("sun.reflect") - || s.contains("java.lang.reflect") - || s.contains("org.apache") - || s.contains("java.util") - || s.contains("java.lang") - || s.contains("io.netty")) { + if (s.contains("sun.reflect") || s.contains("java.lang.reflect") + || s.contains("org.apache") || s.contains("java.util") + || s.contains("java.lang") || s.contains("io.netty")) { continue; } ret.append(s); @@ -140,7 +124,7 @@ public class NodeCenterFrameHandler extends SimpleChannelInboundHandler if (ctx != null) { // System.out.println("[NodeCenterFrame send] TID:" + Thread.currentThread().getId() + " // isOpen:" - // + ctx.channel().isOpen() + " isActive:" + ctx.channel().isActive() + " -->" + json); + // + ctx.channel().isOpen() + " isActive:" + ctx.channel().isActive() + " -->" + json); ByteBuf buf = Unpooled.wrappedBuffer(json.getBytes()); ctx.channel().writeAndFlush(buf); } diff --git a/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java b/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java index 84efdea..338eaf8 100644 --- a/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java @@ -39,35 +39,29 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler( - executorService, managerAction, logActions, unitActions, fileActions, new MetaIndexAction(), new TracingAction()) { - @Override - public boolean checkPermission(Action a, JsonObject arg, long permission) { - // Permission userPermission = a.userPermission(); - // long val=userPermission.getValue(); - long val = a.userPermission(); - String pubKey = managerAction.getPubKey(null); - String action = arg.get("action").getAsString(); - LOGGER.debug("permission" + permission); - LOGGER.debug("userPermission" + val); - NodeCenterServer.nodeHttpLogDB.put( - action, - "{\"action\":\"" - + action - + "\",\"pubKey\":\"" - + pubKey - + "\",\"date\":" - + System.currentTimeMillis() - + "}"); + ae = new ActionExecutor(executorService, managerAction, + logActions, unitActions, fileActions, new MetaIndexAction(), new TracingAction()) { + @Override + public boolean checkPermission(Action a, JsonObject arg, long permission) { + // Permission userPermission = a.userPermission(); + // long val=userPermission.getValue(); + long val = a.userPermission(); + String pubKey = managerAction.getPubKey(null); + String action = arg.get("action").getAsString(); + LOGGER.debug("permission" + permission); + LOGGER.debug("userPermission" + val); + NodeCenterServer.nodeHttpLogDB.put(action, + "{\"action\":\"" + action + "\",\"pubKey\":\"" + pubKey + "\",\"date\":" + + System.currentTimeMillis() + "}"); - // if (val == 0) return true; - // return true; - if ((permission & val) == val) { - return true; - } else return false; - } - }; + // if (val == 0) return true; + // return true; + if ((permission & val) == val) { + return true; + } else + return false; + } + }; for (String str : wsPluginActions) { Object obj = createInstanceByClzName(str); ae.appendHandler(obj); @@ -114,13 +108,13 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler 0) { @@ -133,32 +127,29 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler 5) break; + if (count++ > 5) + break; } response.data = ret; ctx.channel().writeAndFlush(new TextWebSocketFrame(JsonUtil.toJson(response))); diff --git a/src/main/java/org/bdware/server/nodecenter/OtherNCProxy.java b/src/main/java/org/bdware/server/nodecenter/OtherNCProxy.java index 2ca3913..7ec95c0 100644 --- a/src/main/java/org/bdware/server/nodecenter/OtherNCProxy.java +++ b/src/main/java/org/bdware/server/nodecenter/OtherNCProxy.java @@ -31,13 +31,12 @@ import java.util.concurrent.TimeUnit; public class OtherNCProxy { private static final Logger LOGGER = LogManager.getLogger(OtherNCProxy.class); public static OtherNCProxy instance = new OtherNCProxy(); - private final TreeMap> otherNodeCenterInfos = new TreeMap<>(); //key为NC节点的pubkey(非一致的) - public Map connList = new ConcurrentHashMap<>(); //连接别的NC key为ip:port - public Set ncAdds = ConcurrentHashMap.newKeySet(); //其他NC的ip:port - public SM2KeyPair sm2; //NC证书 + private final TreeMap> otherNodeCenterInfos = new TreeMap<>(); // key为NC节点的pubkey(非一致的) + public Map connList = new ConcurrentHashMap<>(); // 连接别的NC key为ip:port + public Set ncAdds = ConcurrentHashMap.newKeySet(); // 其他NC的ip:port + public SM2KeyPair sm2; // NC证书 - private OtherNCProxy() { - } + private OtherNCProxy() {} // 从DB中加载OtherNC和NCFile的信息 public void init() { @@ -56,12 +55,8 @@ public class OtherNCProxy { } public void periodUpdate() { - NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay( - () -> { - }, - 0, - 30, - TimeUnit.SECONDS); + NodeCenterServer.scheduledThreadPool.scheduleWithFixedDelay(() -> { + }, 0, 30, TimeUnit.SECONDS); } public void loadOtherNCs() { @@ -134,7 +129,7 @@ public class OtherNCProxy { return KeyValueDBUtil.instance.getValue(NCTables.NCFile.toString(), "filePath"); } - //配置其他NC + // 配置其他NC public String setOtherNCs(String add) { LOGGER.debug("setOtherNC: " + add); @@ -143,7 +138,7 @@ public class OtherNCProxy { return getOtherNCs(); } - //配置NC文件 + // 配置NC文件 public String setNCFile(String fileName) { LOGGER.debug("setNCFIle: " + fileName); @@ -152,17 +147,18 @@ public class OtherNCProxy { return getNCFile(); } - //本地更新 + // 本地更新 public void updateOtherNCInfo(String nodePubkey, List list) { LOGGER.debug("updateOtherNCInfo: "); synchronized (otherNodeCenterInfos) { otherNodeCenterInfos.put(nodePubkey, list); - LOGGER.debug("update route nodePubkey=" + nodePubkey + " list=" + JsonUtil.toJson(list)); + LOGGER.debug( + "update route nodePubkey=" + nodePubkey + " list=" + JsonUtil.toJson(list)); } } - //通过合约id或name在别的nc上查询信息 + // 通过合约id或name在别的nc上查询信息 public String search(String id) { LOGGER.debug("search : " + id); synchronized (otherNodeCenterInfos) { @@ -170,7 +166,8 @@ public class OtherNCProxy { List list = otherNodeCenterInfos.get(pubKey); for (OtherNCInfo info : list) { if (info.contractID.equals(id) || info.contractName.equals(id)) { - LOGGER.debug("[OtherNCProxy] find contract " + id + "pubKey is " + pubKey + " master is " + info.master + " from other NC route info"); + LOGGER.debug("[OtherNCProxy] find contract " + id + "pubKey is " + pubKey + + " master is " + info.master + " from other NC route info"); return pubKey + ";" + info.master; } } @@ -179,7 +176,7 @@ public class OtherNCProxy { } } - //向其他NC发起同步 + // 向其他NC发起同步 public void requestUpdate() { LOGGER.debug("sendUpdate: "); for (String add : instance.ncAdds) { @@ -207,7 +204,8 @@ public class OtherNCProxy { continue; for (ContractDesp desp : node.contracts) { if (desp.type == ContractExecType.Sole || desp.getIsMaster()) { - list.add(new OtherNCInfo(desp.contractID, desp.contractName, node.masterAddress)); + list.add(new OtherNCInfo(desp.contractID, desp.contractName, + node.masterAddress)); } } } @@ -227,22 +225,18 @@ public class OtherNCProxy { b.group(group); NCConnector conn = new NCConnector(target, b, new NCClientHandler()); connList.put(target, conn); - b.channel(NioSocketChannel.class) - .handler( - new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel ch) { - ChannelPipeline p = ch.pipeline(); - p.addLast(new DelimiterCodec()).addLast(conn.handler); - } - }); + b.channel(NioSocketChannel.class).handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) { + ChannelPipeline p = ch.pipeline(); + p.addLast(new DelimiterCodec()).addLast(conn.handler); + } + }); if (!conn.handler.isOpen()) { String[] ipAndPort = target.split(":"); - b.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])) - .sync() - .channel(); + b.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync().channel(); } reconnect(target); @@ -257,10 +251,7 @@ public class OtherNCProxy { NCConnector conn = connList.get(target); if (!conn.handler.isOpen()) { String[] ipAndPort = conn.ipAndPort.split(":"); - conn.bootstrap - .connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])) - .sync() - .channel(); + conn.bootstrap.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync().channel(); } } diff --git a/src/main/java/org/bdware/server/nodecenter/TracingAction.java b/src/main/java/org/bdware/server/nodecenter/TracingAction.java index e3f4720..4b7d48e 100644 --- a/src/main/java/org/bdware/server/nodecenter/TracingAction.java +++ b/src/main/java/org/bdware/server/nodecenter/TracingAction.java @@ -11,7 +11,7 @@ import java.io.PrintStream; public class TracingAction { private static JsonObject getDependentContract(JsonObject jo) { - //考虑多层依赖 + // 考虑多层依赖 JsonObject result = new JsonObject(); JsonArray beDependent = new JsonArray(); String contractName = jo.get("contractName").getAsString(); @@ -29,7 +29,8 @@ public class TracingAction { dependencies.add(val); } } else { - if (desp.dependentContracts != null && desp.dependentContracts.contains(contractName)) { + if (desp.dependentContracts != null + && desp.dependentContracts.contains(contractName)) { beDependent.add(desp.contractName); } } diff --git a/src/main/java/org/bdware/server/nodecenter/UnitActions.java b/src/main/java/org/bdware/server/nodecenter/UnitActions.java index cb8a22f..90c2657 100644 --- a/src/main/java/org/bdware/server/nodecenter/UnitActions.java +++ b/src/main/java/org/bdware/server/nodecenter/UnitActions.java @@ -25,7 +25,8 @@ public class UnitActions { private static String convertContractName(String contractID) { ContractDesp desp = NodeCenterActions.getContractByName(contractID); - if (desp != null) return desp.contractID; + if (desp != null) + return desp.contractID; return contractID; } @@ -47,10 +48,8 @@ public class UnitActions { long start = System.currentTimeMillis(); final String pubKey = managerAction.getPubKey(json); List allunits = KeyValueDBUtil.instance.getKeyValues(NCTables.TrustUnitsDB.toString()); - if (pubKey != null - && KeyValueDBUtil.instance - .getValue(NCTables.ConfigDB.toString(), "__CenterManager__") - .contains(pubKey)) { + if (pubKey != null && KeyValueDBUtil.instance + .getValue(NCTables.ConfigDB.toString(), "__CenterManager__").contains(pubKey)) { simpleReply(resultCallback, "onListTrustUnits", allunits); LOGGER.debug("is center manager"); long end = System.currentTimeMillis(); @@ -75,10 +74,8 @@ public class UnitActions { public void createTrustUnit(JsonObject json, ResultCallback resultCallback) { final String pubKey = managerAction.getPubKey(json); LOGGER.debug("[createTrustUnit] " + json.get("data").toString()); - KeyValueDBUtil.instance.setValue( - NCTables.TrustUnitsDB.toString(), - pubKey + "_" + json.get("msg").getAsString(), - json.get("data").toString()); + KeyValueDBUtil.instance.setValue(NCTables.TrustUnitsDB.toString(), + pubKey + "_" + json.get("msg").getAsString(), json.get("data").toString()); Map ret = new HashMap<>(); ret.put("action", "onCreateTrustUnit"); ret.put("status", "Success"); @@ -87,27 +84,23 @@ public class UnitActions { @Action(userPermission = 1 << 6, async = true) public void deleteTrustUnit(JsonObject json, ResultCallback resultCallback) { - // final String pubKey = managerAction.pubKey; - LOGGER.debug( - "[deleteTrustUnit] before" - + KeyValueDBUtil.instance.getValue( - NCTables.TrustUnitsDB.toString(), json.get("data").getAsString())); - KeyValueDBUtil.instance.delete( - NCTables.TrustUnitsDB.toString(), json.get("data").getAsString()); - LOGGER.debug( - "[deleteTrustUnit] after" - + KeyValueDBUtil.instance.getValue( - NCTables.TrustUnitsDB.toString(), json.get("data").getAsString())); + // final String pubKey = managerAction.pubKey; + LOGGER.debug("[deleteTrustUnit] before" + KeyValueDBUtil.instance + .getValue(NCTables.TrustUnitsDB.toString(), json.get("data").getAsString())); + KeyValueDBUtil.instance.delete(NCTables.TrustUnitsDB.toString(), + json.get("data").getAsString()); + LOGGER.debug("[deleteTrustUnit] after" + KeyValueDBUtil.instance + .getValue(NCTables.TrustUnitsDB.toString(), json.get("data").getAsString())); Map ret = new HashMap<>(); ret.put("action", "onDeleteTrustUnit"); ret.put("status", "Success"); resultCallback.onResult(ret); } -// @Action(async = true) -// public void executeContract(JsonObject args, final ResultCallback rc) { -// executeContractTrustfully(args, rc); -// } + // @Action(async = true) + // public void executeContract(JsonObject args, final ResultCallback rc) { + // executeContractTrustfully(args, rc); + // } @Action(async = true, userPermission = 1L << 6) public void getNodeTrustUnits(JsonObject json, ResultCallback resultCallback) { @@ -124,89 +117,88 @@ public class UnitActions { simpleReply(resultCallback, "onGetNodeTrustUnits", nodeunits); } -// @Action(async = true, userPermission = 0) -// public void executeContractTrustfully(JsonObject args, final ResultCallback rc) { -// String contractID = args.get("contractID").getAsString(); -// -// contractID = convertContractName(contractID); -// -// args.remove("contractID"); -// args.addProperty("contractID", contractID); -// String requestID; -// if (!args.has("requestID")) { -// requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); -// args.addProperty("requestID", requestID); -// } else requestID = args.get("requestID").getAsString(); -// args.remove("action"); -// args.addProperty("action", "executeContractTrustfully"); -// MultiPointContractInfo mInfo = NodeCenterActions.contractID2Members.get(contractID); -// args.addProperty("seq", mInfo.getNextSeq()); -// args.addProperty("needSeq", ContractType.needSeq(mInfo.type)); -// List nodes = mInfo.members; -// logger.debug("tid:" + Thread.currentThread().getId() + " contractID:" + contractID); -// if (nodes == null) { -// rc.onResult( -// "{\"status\":\"Error\",\"result\":\"can't locate contract\",\"action\":\"onExecuteContractTrustfully\"}"); -// } else { -// mInfo.rcf.execute(requestID, rc, JsonUtil.toJson(args)); -// } -// -// // add request cache,only for requestAll type contract -// if (ContractType.needSeq(mInfo.type)) { -// RequestCache reqc = NodeCenterActions.getCache(contractID); -// reqc.put(args.get("seq").getAsString(), args); -// } -// } + // @Action(async = true, userPermission = 0) + // public void executeContractTrustfully(JsonObject args, final ResultCallback rc) { + // String contractID = args.get("contractID").getAsString(); + // + // contractID = convertContractName(contractID); + // + // args.remove("contractID"); + // args.addProperty("contractID", contractID); + // String requestID; + // if (!args.has("requestID")) { + // requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); + // args.addProperty("requestID", requestID); + // } else requestID = args.get("requestID").getAsString(); + // args.remove("action"); + // args.addProperty("action", "executeContractTrustfully"); + // MultiPointContractInfo mInfo = NodeCenterActions.contractID2Members.get(contractID); + // args.addProperty("seq", mInfo.getNextSeq()); + // args.addProperty("needSeq", ContractType.needSeq(mInfo.type)); + // List nodes = mInfo.members; + // logger.debug("tid:" + Thread.currentThread().getId() + " contractID:" + contractID); + // if (nodes == null) { + // rc.onResult( + // "{\"status\":\"Error\",\"result\":\"can't locate + // contract\",\"action\":\"onExecuteContractTrustfully\"}"); + // } else { + // mInfo.rcf.execute(requestID, rc, JsonUtil.toJson(args)); + // } + // + // // add request cache,only for requestAll type contract + // if (ContractType.needSeq(mInfo.type)) { + // RequestCache reqc = NodeCenterActions.getCache(contractID); + // reqc.put(args.get("seq").getAsString(), args); + // } + // } - //only for test ADSP -// public static void testExecuteContract(JsonObject args, final ResultCallback rc) { -// String contractID = args.get("contractID").getAsString(); -// -// contractID = convertContractName(contractID); -// -// args.remove("contractID"); -// args.addProperty("contractID", contractID); -// String requestID; -// if (!args.has("requestID")) { -// requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); -// args.addProperty("requestID", requestID); -// } else requestID = args.get("requestID").getAsString(); -// args.remove("action"); -// args.addProperty("action", "executeContractTrustfully"); -// MultiPointContractInfo mInfo = NodeCenterActions.contractID2Members.get(contractID); -// System.out.println("mInfo是空吗?" + mInfo == null); -// -// args.addProperty("seq", mInfo.getNextSeq()); -// args.addProperty("needSeq", ContractType.needSeq(mInfo.type)); -// List nodes = mInfo.members; -// logger.debug("tid:" + Thread.currentThread().getId() + " contractID:" + contractID); -// if (nodes == null) { -// rc.onResult( -// "{\"status\":\"Error\",\"result\":\"can't locate contract\",\"action\":\"onExecuteContractTrustfully\"}"); -// } else { -// mInfo.rcf.execute(requestID, rc, JsonUtil.toJson(args)); -// } -// -// //add request cache,only for requestAll type contract -// if(ContractType.needSeq(mInfo.type)){ -// RequestCache reqc = NodeCenterActions.getCache(contractID); -// reqc.put(args.get("seq").getAsString(),args); -// } -// } + // only for test ADSP + // public static void testExecuteContract(JsonObject args, final ResultCallback rc) { + // String contractID = args.get("contractID").getAsString(); + // + // contractID = convertContractName(contractID); + // + // args.remove("contractID"); + // args.addProperty("contractID", contractID); + // String requestID; + // if (!args.has("requestID")) { + // requestID = System.currentTimeMillis() + "_" + (int) (Math.random() * 10000); + // args.addProperty("requestID", requestID); + // } else requestID = args.get("requestID").getAsString(); + // args.remove("action"); + // args.addProperty("action", "executeContractTrustfully"); + // MultiPointContractInfo mInfo = NodeCenterActions.contractID2Members.get(contractID); + // System.out.println("mInfo是空吗?" + mInfo == null); + // + // args.addProperty("seq", mInfo.getNextSeq()); + // args.addProperty("needSeq", ContractType.needSeq(mInfo.type)); + // List nodes = mInfo.members; + // logger.debug("tid:" + Thread.currentThread().getId() + " contractID:" + contractID); + // if (nodes == null) { + // rc.onResult( + // "{\"status\":\"Error\",\"result\":\"can't locate + // contract\",\"action\":\"onExecuteContractTrustfully\"}"); + // } else { + // mInfo.rcf.execute(requestID, rc, JsonUtil.toJson(args)); + // } + // + // //add request cache,only for requestAll type contract + // if(ContractType.needSeq(mInfo.type)){ + // RequestCache reqc = NodeCenterActions.getCache(contractID); + // reqc.put(args.get("seq").getAsString(),args); + // } + // } @Action(async = true, userPermission = 1L << 6) public void listContractProcess(JsonObject args, final ResultCallback rc) { List info = new ArrayList<>(); - LOGGER.debug( - "[contracts] " - + JsonUtil.toPrettyJson(NodeCenterActions.nodeInfos)); - LOGGER.debug( - "[cid2Nodes]" - + JsonUtil.toPrettyJson(NodeCenterActions.contractID2Members)); + LOGGER.debug("[contracts] " + JsonUtil.toPrettyJson(NodeCenterActions.nodeInfos)); + LOGGER.debug("[cid2Nodes]" + JsonUtil.toPrettyJson(NodeCenterActions.contractID2Members)); for (String key : NodeCenterActions.contractID2Members.keySet()) { ContractDesp desp = NodeCenterActions.getContractByID(key); - if (desp != null) info.add(desp); + if (desp != null) + info.add(desp); } simpleReply(rc, "onListContractProcess", JsonUtil.toJson(info)); } @@ -214,15 +206,12 @@ public class UnitActions { @Action(userPermission = 1 << 6, async = true) public void listMultiPointContractProcess(JsonObject json, ResultCallback resultCallback) { List info = new ArrayList<>(); - LOGGER.debug( - "[contracts] " - + JsonUtil.toPrettyJson(NodeCenterActions.nodeInfos)); - LOGGER.debug( - "[cid2Nodes]" - + JsonUtil.toPrettyJson(NodeCenterActions.contractID2Members)); + LOGGER.debug("[contracts] " + JsonUtil.toPrettyJson(NodeCenterActions.nodeInfos)); + LOGGER.debug("[cid2Nodes]" + JsonUtil.toPrettyJson(NodeCenterActions.contractID2Members)); for (String key : NodeCenterActions.contractID2Members.keySet()) { ContractDesp desp = NodeCenterActions.getContractByID(key); - if (desp != null) info.add(desp); + if (desp != null) + info.add(desp); } simpleReply(resultCallback, "onListMultiPointContractProcess", JsonUtil.toJson(info)); } @@ -251,7 +240,8 @@ public class UnitActions { String[] strs = args.get("nodeIDs").getAsString().split(","); Set nodePubKeys = new HashSet<>(); // nodes' pubkey for (String str : strs) { - if (str != null && str.length() > 0) nodePubKeys.add(str); + if (str != null && str.length() > 0) + nodePubKeys.add(str); } Map nodes = new HashMap<>(); // node's pubKey-node'a name for (CMNode node : NodeCenterActions.nodeInfos.values()) { @@ -264,9 +254,8 @@ public class UnitActions { simpleReply(rc, "onDistributeYPK", "empty nodes"); } - boolean result = - SM2Util.plainStrVerify( - pubKey, "DistributeYPK|" + projectName + "|" + pubKey, signature); + boolean result = SM2Util.plainStrVerify(pubKey, + "DistributeYPK|" + projectName + "|" + pubKey, signature); LOGGER.info("[UnitAcitons] 验签:" + result + " -> projectName:" + projectName); String ypkType = projectName.split("_")[0]; @@ -292,154 +281,154 @@ public class UnitActions { } } -// @Action(async = true, userPermission = 1L << 6) -// public void startContractMultiPoint(JsonObject args, final ResultCallback rc) { -// Map request = new HashMap(); -// Contract contract = new Contract(); -// if (args.has("type")) { -// contract.setType(ContractType.valueOf(args.get("type").getAsString())); -// } -// -// // this multiPointContractInfo problem -// MultiPointContractInfo multiPointContractInfo = new MultiPointContractInfo(); -// multiPointContractInfo.type = contract.getType(); -// -// logger.debug("startContractMultiPoint:"); -// logger.debug(JsonUtil.toPrettyJson(args)); -// if (args.has("script")) contract.setScript(args.get("script").getAsString()); -// else if (args.has("path")) contract.setScript(args.get("path").getAsString()); -// else if (args.has("projectName")) -// contract.setScript("/" + args.get("projectName").getAsString()); -// // contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json"); -// -// SM2 sm2 = new SM2(); -// SM2KeyPair sm2Key = sm2.generateKeyPair(); -// contract.setID(sm2Key.getPublicKeyStr().hashCode() + ""); -// contract.setKey(sm2Key.getPrivateKey().toString(16)); -// contract.setPublicKey(sm2Key.getPublicKeyStr()); -// -// contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier); -// // 多点合约部署时,由NC预先注册一个DOI以备使用 -// if (DoConfig.openContractDORegister) { -// try { -// HandleService hs = new HandleService(HandleServiceUtils.hrRegister); -// String tmpDOI = hs.registerContract(DOIPMainServer4NC.repoIdentifier); -// if (tmpDOI == "" || tmpDOI == null) -// contract.setDOI("RegisterFailed"); -// else -// contract.setDOI(tmpDOI); -// } -// catch (Exception e) { -// e.printStackTrace(); -// contract.setDOI("RegisterFailed"); -// } -// } -// -// String[] nodeNames = -// args.get("peersID").getAsString().split(","); // all nodes' peerID in the unit -// Set nodeNameSet = new HashSet<>(); // nodes' peerID -// for (String str : nodeNames) { -// if (str != null && str.length() > 0) nodeNameSet.add(str); -// } -// -// List udpids = new ArrayList<>(); // nodes' udpID in the unit -// -// for (CMNode node : NodeCenterActions.nodeinfos.values()) { -// if (nodeNameSet.contains(node.nodeName)) { -// udpids.add(node.udpID); -// } -// } -// final long curr = System.currentTimeMillis(); -// String requestID = curr + "_" + (int) (Math.random() * 10000); -// NodeCenterActions.ResultCollector collector = -// new NodeCenterActions.ResultCollector( -// requestID, -// new ResultCallback() { -// @Override -// public void onResult(String str) { -// Map ret = new HashMap<>(); -// ret.put("action", "onStartContractTrustfullyResult"); -// ret.put("data", str); -// ret.put("executionTime", (System.currentTimeMillis() - curr) + ""); -// rc.onResult(JsonUtil.toJson(ret)); -// } -// }, -// udpids.size()); -// NodeCenterActions.sync.sleepWithTimeout(requestID, collector, 20); -// -// // ArrayList udpids=gson.fromJson(args.get("members"), -// // new TypeToken>(){}.getType()); -// Map members = new HashMap<>(); // nodes' -// for (int i = 0; i < udpids.size(); i++) { -// members.put( -// udpids.get(i), -// NodeCenterActions.runner.getUDPAddr(Integer.valueOf(udpids.get(i))) -// + ":" -// + (i == 0)); // regard the first node as the master in the unit -// } -// -// List nodes = new ArrayList<>(); // nodes' pubKey -// for (CMNode node : NodeCenterActions.nodeinfos.values()) { -// if (nodeNameSet.contains(node.nodeName)) { -// nodes.add(node.pubKey); -// } -// } -// -// request.put("memberStr", JsonUtil.toJson(members)); -// request.put("isPrivate", args.get("isPrivate").getAsString()); -// request.put("pubKey", managerAction.pubKey); -// request.put("action", "startContractTrustfully"); -// request.put("requestID", requestID); -// multiPointContractInfo.members = nodes; -// -// contract.setNumOfCopies(1); -// switch (multiPointContractInfo.type) { -// case Sole: -// logger.debug("Can't support Solo in multi-point mode"); -// return; -// case RequestOnce: -// multiPointContractInfo.rcf = new RequestOnceExecutor(multiPointContractInfo); -// break; -// case ResponseOnce: -// multiPointContractInfo.rcf = new ResponseOnceExecutor(multiPointContractInfo); -// break; -// case RequestAllResponseFirst: -// multiPointContractInfo.rcf = new RequestAllExecutor(multiPointContractInfo, 1); -// contract.setNumOfCopies(nodes.size()); -// break; -// case RequestAllResponseHalf: -// multiPointContractInfo.rcf = -// new RequestAllExecutor(multiPointContractInfo, nodes.size() / 2 + 1); -// contract.setNumOfCopies(nodes.size()); -// break; -// case RequestAllResponseAll: -// multiPointContractInfo.rcf = -// new RequestAllExecutor(multiPointContractInfo, nodes.size()); -// contract.setNumOfCopies(nodes.size()); -// break; -// } -// NodeCenterActions.contractID2Members.put(contract.getID(), multiPointContractInfo); -// -// request.put("contractStr", JsonUtil.toJson(contract)); -// String startReq = JsonUtil.toJson(request); -// logger.debug("启动合约:" + startReq); -// -// for (String nodeID : nodes) { -// CMNode node; -// node = NodeCenterActions.nodeinfos.get(nodeID); -// node.connection.controller.sendMsg(startReq); -// -// if (!NodeCenterActions.recoverMap.containsKey(nodeID)) { -// NodeCenterActions.recoverMap.put(nodeID, new ConcurrentHashMap<>()); -// } -// ContractRecord record = new ContractRecord(contract.getID()); -// record.members = new HashMap<>(members); -// NodeCenterActions.recoverMap.get(nodeID).put(contract.getID(), record); -// } -// -// rc.onResult( -// "{\"status\":\"Success\",\"result\":\"" -// + contract.getID() -// + "\",\"action\":\"onStartTrustfulContract\"}"); -// } + // @Action(async = true, userPermission = 1L << 6) + // public void startContractMultiPoint(JsonObject args, final ResultCallback rc) { + // Map request = new HashMap(); + // Contract contract = new Contract(); + // if (args.has("type")) { + // contract.setType(ContractType.valueOf(args.get("type").getAsString())); + // } + // + // // this multiPointContractInfo problem + // MultiPointContractInfo multiPointContractInfo = new MultiPointContractInfo(); + // multiPointContractInfo.type = contract.getType(); + // + // logger.debug("startContractMultiPoint:"); + // logger.debug(JsonUtil.toPrettyJson(args)); + // if (args.has("script")) contract.setScript(args.get("script").getAsString()); + // else if (args.has("path")) contract.setScript(args.get("path").getAsString()); + // else if (args.has("projectName")) + // contract.setScript("/" + args.get("projectName").getAsString()); + // // contract.setScript("/" + args.get("projectName").getAsString() + "/manifest.json"); + // + // SM2 sm2 = new SM2(); + // SM2KeyPair sm2Key = sm2.generateKeyPair(); + // contract.setID(sm2Key.getPublicKeyStr().hashCode() + ""); + // contract.setKey(sm2Key.getPrivateKey().toString(16)); + // contract.setPublicKey(sm2Key.getPublicKeyStr()); + // + // contract.setNodeCenterRepoDOI(DOIPMainServer4NC.repoIdentifier); + // // 多点合约部署时,由NC预先注册一个DOI以备使用 + // if (DoConfig.openContractDORegister) { + // try { + // HandleService hs = new HandleService(HandleServiceUtils.hrRegister); + // String tmpDOI = hs.registerContract(DOIPMainServer4NC.repoIdentifier); + // if (tmpDOI == "" || tmpDOI == null) + // contract.setDOI("RegisterFailed"); + // else + // contract.setDOI(tmpDOI); + // } + // catch (Exception e) { + // e.printStackTrace(); + // contract.setDOI("RegisterFailed"); + // } + // } + // + // String[] nodeNames = + // args.get("peersID").getAsString().split(","); // all nodes' peerID in the unit + // Set nodeNameSet = new HashSet<>(); // nodes' peerID + // for (String str : nodeNames) { + // if (str != null && str.length() > 0) nodeNameSet.add(str); + // } + // + // List udpids = new ArrayList<>(); // nodes' udpID in the unit + // + // for (CMNode node : NodeCenterActions.nodeinfos.values()) { + // if (nodeNameSet.contains(node.nodeName)) { + // udpids.add(node.udpID); + // } + // } + // final long curr = System.currentTimeMillis(); + // String requestID = curr + "_" + (int) (Math.random() * 10000); + // NodeCenterActions.ResultCollector collector = + // new NodeCenterActions.ResultCollector( + // requestID, + // new ResultCallback() { + // @Override + // public void onResult(String str) { + // Map ret = new HashMap<>(); + // ret.put("action", "onStartContractTrustfullyResult"); + // ret.put("data", str); + // ret.put("executionTime", (System.currentTimeMillis() - curr) + ""); + // rc.onResult(JsonUtil.toJson(ret)); + // } + // }, + // udpids.size()); + // NodeCenterActions.sync.sleepWithTimeout(requestID, collector, 20); + // + // // ArrayList udpids=gson.fromJson(args.get("members"), + // // new TypeToken>(){}.getType()); + // Map members = new HashMap<>(); // nodes' + // for (int i = 0; i < udpids.size(); i++) { + // members.put( + // udpids.get(i), + // NodeCenterActions.runner.getUDPAddr(Integer.valueOf(udpids.get(i))) + // + ":" + // + (i == 0)); // regard the first node as the master in the unit + // } + // + // List nodes = new ArrayList<>(); // nodes' pubKey + // for (CMNode node : NodeCenterActions.nodeinfos.values()) { + // if (nodeNameSet.contains(node.nodeName)) { + // nodes.add(node.pubKey); + // } + // } + // + // request.put("memberStr", JsonUtil.toJson(members)); + // request.put("isPrivate", args.get("isPrivate").getAsString()); + // request.put("pubKey", managerAction.pubKey); + // request.put("action", "startContractTrustfully"); + // request.put("requestID", requestID); + // multiPointContractInfo.members = nodes; + // + // contract.setNumOfCopies(1); + // switch (multiPointContractInfo.type) { + // case Sole: + // logger.debug("Can't support Solo in multi-point mode"); + // return; + // case RequestOnce: + // multiPointContractInfo.rcf = new RequestOnceExecutor(multiPointContractInfo); + // break; + // case ResponseOnce: + // multiPointContractInfo.rcf = new ResponseOnceExecutor(multiPointContractInfo); + // break; + // case RequestAllResponseFirst: + // multiPointContractInfo.rcf = new RequestAllExecutor(multiPointContractInfo, 1); + // contract.setNumOfCopies(nodes.size()); + // break; + // case RequestAllResponseHalf: + // multiPointContractInfo.rcf = + // new RequestAllExecutor(multiPointContractInfo, nodes.size() / 2 + 1); + // contract.setNumOfCopies(nodes.size()); + // break; + // case RequestAllResponseAll: + // multiPointContractInfo.rcf = + // new RequestAllExecutor(multiPointContractInfo, nodes.size()); + // contract.setNumOfCopies(nodes.size()); + // break; + // } + // NodeCenterActions.contractID2Members.put(contract.getID(), multiPointContractInfo); + // + // request.put("contractStr", JsonUtil.toJson(contract)); + // String startReq = JsonUtil.toJson(request); + // logger.debug("启动合约:" + startReq); + // + // for (String nodeID : nodes) { + // CMNode node; + // node = NodeCenterActions.nodeinfos.get(nodeID); + // node.connection.controller.sendMsg(startReq); + // + // if (!NodeCenterActions.recoverMap.containsKey(nodeID)) { + // NodeCenterActions.recoverMap.put(nodeID, new ConcurrentHashMap<>()); + // } + // ContractRecord record = new ContractRecord(contract.getID()); + // record.members = new HashMap<>(members); + // NodeCenterActions.recoverMap.get(nodeID).put(contract.getID(), record); + // } + // + // rc.onResult( + // "{\"status\":\"Success\",\"result\":\"" + // + contract.getID() + // + "\",\"action\":\"onStartTrustfulContract\"}"); + // } } diff --git a/src/test/java/org/bdware/bdserver/PermissionHelper.java b/src/test/java/org/bdware/bdserver/PermissionHelper.java index 1ccab1f..4c10f27 100644 --- a/src/test/java/org/bdware/bdserver/PermissionHelper.java +++ b/src/test/java/org/bdware/bdserver/PermissionHelper.java @@ -23,7 +23,7 @@ public class PermissionHelper { @Test public void listClusterHttpAction() { - ActionExecutor ae = NCHttpHandler.getActionExecutor(); + ActionExecutor ae = NCHttpHandler.getActionExecutor(); Map> handlers = ae.getHandlers(); EnumSet set = EnumSet.allOf(Role.class); set.remove(Role.ContractInstanceManager); @@ -44,8 +44,8 @@ public class PermissionHelper { printActions(handlers, set); } - private void printActions( - Map> handlers, EnumSet set) { + private void printActions(Map> handlers, + EnumSet set) { List lines = new ArrayList<>(); for (String str : handlers.keySet()) { Method m = handlers.get(str).first(); @@ -56,23 +56,23 @@ public class PermissionHelper { l.permission = a.userPermission(); l.roles = ""; for (Role r : set) - if ((r.getValue() & l.permission) == l.permission) l.roles += r.name() + ";"; - if (l.roles.equals("CenterManager;NodeManager;Node;Anonymous;")) l.roles = "任意角色"; + if ((r.getValue() & l.permission) == l.permission) + l.roles += r.name() + ";"; + if (l.roles.equals("CenterManager;NodeManager;Node;Anonymous;")) + l.roles = "任意角色"; } - lines.sort( - new Comparator() { - @Override - public int compare(Line o1, Line o2) { - return o1.action.compareTo(o2.action); - } - }); - lines.sort( - new Comparator() { - @Override - public int compare(Line o1, Line o2) { - return o1.roles.compareTo(o2.roles); - } - }); + lines.sort(new Comparator() { + @Override + public int compare(Line o1, Line o2) { + return o1.action.compareTo(o2.action); + } + }); + lines.sort(new Comparator() { + @Override + public int compare(Line o1, Line o2) { + return o1.roles.compareTo(o2.roles); + } + }); Gson g = new GsonBuilder().setPrettyPrinting().create(); for (Line l : lines) { diff --git a/src/test/java/org/bdware/bdserver/TestServer.java b/src/test/java/org/bdware/bdserver/TestServer.java index 8f34361..852675b 100644 --- a/src/test/java/org/bdware/bdserver/TestServer.java +++ b/src/test/java/org/bdware/bdserver/TestServer.java @@ -18,8 +18,7 @@ public class TestServer { public void channelActive(ChannelHandlerContext ctx) { LOGGER.info("active"); System.out.println("======Active======="); - ctx.channel() - .writeAndFlush("ACTIVE from Server") + ctx.channel().writeAndFlush("ACTIVE from Server") .addListener(future -> LOGGER.info("Active Send Done!!")); } diff --git a/src/test/java/org/bdware/bdserver/test/TestADSP.java b/src/test/java/org/bdware/bdserver/test/TestADSP.java index 583fa62..fd7d1ce 100644 --- a/src/test/java/org/bdware/bdserver/test/TestADSP.java +++ b/src/test/java/org/bdware/bdserver/test/TestADSP.java @@ -1,192 +1,201 @@ package org.bdware.bdserver.test; public class TestADSP { - // public static Map nodes = new HashMap(); -// -// private static volatile Boolean flag = false; -// private static int totalRequest = 10; -// private static int nodeCount = 5; -// -// public static String resultPath = "./testADSP/" + "result.txt"; -// public static String contractID = "counter"; -// public static String action = "executeContract"; -// public static String pubkey = "0480204f4ef341359a5f64fcb11baf9ca2e6706ac20cba36ca83066870cf2c1d5de6df67e24e68dde7934af9b31d94a6084281db3d32d5ce42ab8f75bf799aca05"; -// -// public static String operation = "count"; -// public static String arg = "1"; -// -// public static String priKey = "63464fa9587bbf2a022b3d655f4ed49c7d9a4249de1079d52bd5a1fec3308719"; -// public static String signature = "dfd07c00c5c89fb0f901bed132db4342f49f4560f34c15878636623d2a8716a2c93ad32eeb9ee9c4f1d905df292cb4a1a27aa0171f2856848ce07cfc8022e809"; -// -// public static List crashSeries = new ArrayList(); -// public static AtomicInteger crashIndex = new AtomicInteger(); -// -// public static ConcurrentSet crashSet = new ConcurrentSet(); -// public static ConcurrentSet recover = new ConcurrentSet(); -// -// public static TestTask tt; -// public static int period = 3000; -// -// static{ -// getSig(); -// -// nodes.put("zyx's book","0440023d13facddcefbd87f2bb622b1b2b6ca43eb84c0579b82450814233ce557bd7ad7852cd47f1d6df867c5413ecf3dd1954cdbf5a6da683e3a89095f091d83d"); -// nodes.put("localNode1","0480f783cf63294224afbd19e175e5c7ea0c5c60ee115ed9e114fe2691eb28cc1680e5fec532d64c80d2f6b737e8b43b94d5a9c73206ac6235c50ff992133e4d38"); -// nodes.put("localNode2","044e0c127e24407ee8a8abd4fe507b32b340ce9775317b8d7b5bb8e182745d6a45c57aaf867d80a5f816a7561564f9294c6aee5916f95e93c0011e16c28b9e849a"); -// nodes.put("localNode3","0485040cfd94bec672bb8ba184856188963ee4ad339d247a76e2d9819f20e61bfad24ec763b1371998583f9dc0cf55c4d53cb1a2ec84c67aed1aa5203cc84fc78f"); -// nodes.put("localNode4",""); -// nodes.put("localNode5",""); -// -// -// //生成崩溃序列 -// crashSeries.clear(); -// for(int i = 1;i <= totalRequest;i++){ -// int temp = i % nodeCount; -// if(temp == 0) -// temp = nodeCount; -// crashSeries.add("crash localNode" + temp); -// crashSeries.add("recover localNode" + temp); -// } -// -// System.out.println("崩溃序列" + crashSeries.size()); -// -// Timer timer = new Timer(); -// tt = new TestTask(); -// timer.schedule(tt, new Date(), period); -// } -// -// static class TestTask extends TimerTask { -// @Override -// public void run() { -// if(TestADSP.getFlag()){ -// int index = TestADSP.crashIndex.getAndIncrement(); -// System.out.println("[TestADSP] index=" + TestADSP.crashIndex.get()); -// -// String[] crash = TestADSP.crashSeries.get(index).split(" "); -// String pubKey = convertNameToPubkey(crash[1]); -// switch (crash[0]){ -// case "crash": -// if(pubKey != null){ -// System.out.println("[TestADSP] 令节点 " + crash[1] + "崩溃"); -// System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()))); -// crashSet.add(pubKey); -// } -// break; -// case "recover": -// if(pubKey != null){ -// if(crashSet.contains(pubKey)){ -// crashSet.remove(pubKey); -// } -// recover.add(pubKey); -// System.out.println("[TestADSP] 令节点 " + crash[1] + "上线"); -// System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()))); -// } -// break; -// default: -// break; -// } -// -// -// try { -// Thread.sleep(period / 2); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// -// -// System.out.println("[TestADSP]发起请求 " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()))); -// JsonObject jo = new JsonObject(); -// jo.addProperty("action", TestADSP.action); -// jo.addProperty("contractID", TestADSP.contractID); -// jo.addProperty("operation", TestADSP.operation); -// jo.addProperty("arg", TestADSP.arg); -// jo.addProperty("pubkey",TestADSP.pubkey); -// jo.addProperty("signature",TestADSP.signature); -// UnitActions.testExecuteContract(jo,null); -// -// if(index == (TestADSP.crashSeries.size() - 1)){ -// System.out.println("测试停止!"); -// TestADSP.setFlag(false); -// //TestADSP.check(); -// } -// }//if -// } -// } -// -// public static boolean getFlag(){ -// boolean temp; -// synchronized (flag){ -// temp = flag; -// } -// return temp; -// } -// -// public static void setFlag(boolean temp){ -// synchronized (flag){ -// flag = temp; -// System.out.println("[TestADSP]设置flag为" + flag); -// } -// } -// -// public static void getSig(){ -// String temp = contractID + "|" + operation + "|" + arg + "|" + pubkey; -// SM2 sm2 = new SM2(); -// signature = sm2.sign(temp.getBytes(),priKey).toString(); -// } -// -// //查看结果输出文件 -// public static void check() { -// tt.cancel(); -// -// int correct = 0; -// int incorrect = 0; -// int unavailable = 0; -// int fileTotal = 0; -// -// File file = new File("./front-cluster/testADSP/result.txt"); -// System.out.println(file.getAbsolutePath()); -// StringBuilder result = new StringBuilder(); -// try{ -// BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件 -// -// String s = null; -// while((s = br.readLine())!=null){//使用readLine方法,一次读一行 -// if(!s.equals("")){ -// fileTotal++; -// String str[] = s.split(" "); -// if(str[1].equals((Integer.parseInt(str[0]) + 1) + ".0")){ -// correct++; -// }else{ -// incorrect++; -// } -// } -// } -// br.close(); -// -// }catch(Exception e){ -// e.printStackTrace(); -// } -// -// unavailable = totalRequest * 2 - fileTotal; -// -// System.out.println("共有" + totalRequest * 2 + "次请求,其中" + correct + "正确," + incorrect + "不正确," + unavailable + "不可用."); -// } -// -// public static String convertNameToPubkey(String nodeName){ -// return nodes.get(nodeName); -// } -// -// public static String convertPubKeyToName(String pubkey){ -// for(String key : nodes.keySet()){ -// if(nodes.get(key).equals(pubkey)){ -// return key; -// } -// } -// return null; -// } -// -// public static void main(String[] args){ -// check(); -// } + // public static Map nodes = new HashMap(); + // + // private static volatile Boolean flag = false; + // private static int totalRequest = 10; + // private static int nodeCount = 5; + // + // public static String resultPath = "./testADSP/" + "result.txt"; + // public static String contractID = "counter"; + // public static String action = "executeContract"; + // public static String pubkey = + // "0480204f4ef341359a5f64fcb11baf9ca2e6706ac20cba36ca83066870cf2c1d5de6df67e24e68dde7934af9b31d94a6084281db3d32d5ce42ab8f75bf799aca05"; + // + // public static String operation = "count"; + // public static String arg = "1"; + // + // public static String priKey = + // "63464fa9587bbf2a022b3d655f4ed49c7d9a4249de1079d52bd5a1fec3308719"; + // public static String signature = + // "dfd07c00c5c89fb0f901bed132db4342f49f4560f34c15878636623d2a8716a2c93ad32eeb9ee9c4f1d905df292cb4a1a27aa0171f2856848ce07cfc8022e809"; + // + // public static List crashSeries = new ArrayList(); + // public static AtomicInteger crashIndex = new AtomicInteger(); + // + // public static ConcurrentSet crashSet = new ConcurrentSet(); + // public static ConcurrentSet recover = new ConcurrentSet(); + // + // public static TestTask tt; + // public static int period = 3000; + // + // static{ + // getSig(); + // + // nodes.put("zyx's + // book","0440023d13facddcefbd87f2bb622b1b2b6ca43eb84c0579b82450814233ce557bd7ad7852cd47f1d6df867c5413ecf3dd1954cdbf5a6da683e3a89095f091d83d"); + // nodes.put("localNode1","0480f783cf63294224afbd19e175e5c7ea0c5c60ee115ed9e114fe2691eb28cc1680e5fec532d64c80d2f6b737e8b43b94d5a9c73206ac6235c50ff992133e4d38"); + // nodes.put("localNode2","044e0c127e24407ee8a8abd4fe507b32b340ce9775317b8d7b5bb8e182745d6a45c57aaf867d80a5f816a7561564f9294c6aee5916f95e93c0011e16c28b9e849a"); + // nodes.put("localNode3","0485040cfd94bec672bb8ba184856188963ee4ad339d247a76e2d9819f20e61bfad24ec763b1371998583f9dc0cf55c4d53cb1a2ec84c67aed1aa5203cc84fc78f"); + // nodes.put("localNode4",""); + // nodes.put("localNode5",""); + // + // + // //生成崩溃序列 + // crashSeries.clear(); + // for(int i = 1;i <= totalRequest;i++){ + // int temp = i % nodeCount; + // if(temp == 0) + // temp = nodeCount; + // crashSeries.add("crash localNode" + temp); + // crashSeries.add("recover localNode" + temp); + // } + // + // System.out.println("崩溃序列" + crashSeries.size()); + // + // Timer timer = new Timer(); + // tt = new TestTask(); + // timer.schedule(tt, new Date(), period); + // } + // + // static class TestTask extends TimerTask { + // @Override + // public void run() { + // if(TestADSP.getFlag()){ + // int index = TestADSP.crashIndex.getAndIncrement(); + // System.out.println("[TestADSP] index=" + TestADSP.crashIndex.get()); + // + // String[] crash = TestADSP.crashSeries.get(index).split(" "); + // String pubKey = convertNameToPubkey(crash[1]); + // switch (crash[0]){ + // case "crash": + // if(pubKey != null){ + // System.out.println("[TestADSP] 令节点 " + crash[1] + "崩溃"); + // System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new + // Date(System.currentTimeMillis()))); + // crashSet.add(pubKey); + // } + // break; + // case "recover": + // if(pubKey != null){ + // if(crashSet.contains(pubKey)){ + // crashSet.remove(pubKey); + // } + // recover.add(pubKey); + // System.out.println("[TestADSP] 令节点 " + crash[1] + "上线"); + // System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new + // Date(System.currentTimeMillis()))); + // } + // break; + // default: + // break; + // } + // + // + // try { + // Thread.sleep(period / 2); + // } catch (InterruptedException e) { + // e.printStackTrace(); + // } + // + // + // + // System.out.println("[TestADSP]发起请求 " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new + // Date(System.currentTimeMillis()))); + // JsonObject jo = new JsonObject(); + // jo.addProperty("action", TestADSP.action); + // jo.addProperty("contractID", TestADSP.contractID); + // jo.addProperty("operation", TestADSP.operation); + // jo.addProperty("arg", TestADSP.arg); + // jo.addProperty("pubkey",TestADSP.pubkey); + // jo.addProperty("signature",TestADSP.signature); + // UnitActions.testExecuteContract(jo,null); + // + // if(index == (TestADSP.crashSeries.size() - 1)){ + // System.out.println("测试停止!"); + // TestADSP.setFlag(false); + // //TestADSP.check(); + // } + // }//if + // } + // } + // + // public static boolean getFlag(){ + // boolean temp; + // synchronized (flag){ + // temp = flag; + // } + // return temp; + // } + // + // public static void setFlag(boolean temp){ + // synchronized (flag){ + // flag = temp; + // System.out.println("[TestADSP]设置flag为" + flag); + // } + // } + // + // public static void getSig(){ + // String temp = contractID + "|" + operation + "|" + arg + "|" + pubkey; + // SM2 sm2 = new SM2(); + // signature = sm2.sign(temp.getBytes(),priKey).toString(); + // } + // + // //查看结果输出文件 + // public static void check() { + // tt.cancel(); + // + // int correct = 0; + // int incorrect = 0; + // int unavailable = 0; + // int fileTotal = 0; + // + // File file = new File("./front-cluster/testADSP/result.txt"); + // System.out.println(file.getAbsolutePath()); + // StringBuilder result = new StringBuilder(); + // try{ + // BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), + // "UTF-8"));//构造一个BufferedReader类来读取文件 + // + // String s = null; + // while((s = br.readLine())!=null){//使用readLine方法,一次读一行 + // if(!s.equals("")){ + // fileTotal++; + // String str[] = s.split(" "); + // if(str[1].equals((Integer.parseInt(str[0]) + 1) + ".0")){ + // correct++; + // }else{ + // incorrect++; + // } + // } + // } + // br.close(); + // + // }catch(Exception e){ + // e.printStackTrace(); + // } + // + // unavailable = totalRequest * 2 - fileTotal; + // + // System.out.println("共有" + totalRequest * 2 + "次请求,其中" + correct + "正确," + incorrect + "不正确," + // + unavailable + "不可用."); + // } + // + // public static String convertNameToPubkey(String nodeName){ + // return nodes.get(nodeName); + // } + // + // public static String convertPubKeyToName(String pubkey){ + // for(String key : nodes.keySet()){ + // if(nodes.get(key).equals(pubkey)){ + // return key; + // } + // } + // return null; + // } + // + // public static void main(String[] args){ + // check(); + // } } diff --git a/src/test/java/org/bdware/bdserver/test/TestSearch.java b/src/test/java/org/bdware/bdserver/test/TestSearch.java index 08f7bee..6d5c8f2 100644 --- a/src/test/java/org/bdware/bdserver/test/TestSearch.java +++ b/src/test/java/org/bdware/bdserver/test/TestSearch.java @@ -32,44 +32,44 @@ public class TestSearch { @Test public void testIndexSearch() throws Exception { - //1. 创建分词器(对搜索的关键词进行分词使用) - //注意: 分词器要和创建索引的时候使用的分词器一模一样 + // 1. 创建分词器(对搜索的关键词进行分词使用) + // 注意: 分词器要和创建索引的时候使用的分词器一模一样 Analyzer analyzer = new StandardAnalyzer(); - //2. 创建查询对象, - //第一个参数: 默认查询域, 如果查询的关键字中带搜索的域名, 则从指定域中查询, 如果不带域名则从, 默认搜索域中查询 - //第二个参数: 使用的分词器 + // 2. 创建查询对象, + // 第一个参数: 默认查询域, 如果查询的关键字中带搜索的域名, 则从指定域中查询, 如果不带域名则从, 默认搜索域中查询 + // 第二个参数: 使用的分词器 QueryParser queryParser = new QueryParser("name", analyzer); - //3. 设置搜索关键词 - //华 OR 为 手 机 + // 3. 设置搜索关键词 + // 华 OR 为 手 机 Query query = queryParser.parse("华为手机"); - //4. 创建Directory目录对象, 指定索引库的位置 + // 4. 创建Directory目录对象, 指定索引库的位置 Directory dir = FSDirectory.open(Paths.get("E:\\dir")); - //5. 创建输入流对象 + // 5. 创建输入流对象 IndexReader indexReader = DirectoryReader.open(dir); - //6. 创建搜索对象 + // 6. 创建搜索对象 IndexSearcher indexSearcher = new IndexSearcher(indexReader); - //7. 搜索, 并返回结果 - //第二个参数: 是返回多少条数据用于展示, 分页使用 + // 7. 搜索, 并返回结果 + // 第二个参数: 是返回多少条数据用于展示, 分页使用 TopDocs topDocs = indexSearcher.search(query, 10); - //获取查询到的结果集的总数, 打印 + // 获取查询到的结果集的总数, 打印 System.out.println("=======count=======" + topDocs.totalHits); - //8. 获取结果集 + // 8. 获取结果集 ScoreDoc[] scoreDocs = topDocs.scoreDocs; - //9. 遍历结果集 + // 9. 遍历结果集 if (scoreDocs != null) { for (ScoreDoc scoreDoc : scoreDocs) { - //获取查询到的文档唯一标识, 文档id, 这个id是lucene在创建文档的时候自动分配的 - int docID = scoreDoc.doc; - //通过文档id, 读取文档 + // 获取查询到的文档唯一标识, 文档id, 这个id是lucene在创建文档的时候自动分配的 + int docID = scoreDoc.doc; + // 通过文档id, 读取文档 Document doc = indexSearcher.doc(docID); System.out.println("=================================================="); - //通过域名, 从文档中获取域值 + // 通过域名, 从文档中获取域值 System.out.println("===id==" + doc.get("id")); System.out.println("===name==" + doc.get("name")); System.out.println("===price==" + doc.get("price")); @@ -79,6 +79,6 @@ public class TestSearch { } } - //10. 关闭流 + // 10. 关闭流 } }