incomingNodes = resActiveNodes.getNodes();
for (INode incomingNode : incomingNodes) {
- if (nodeMgr.tempNodesSize() >= this.mgr.getMaxTempNodes()) { return; }
+ if (nodeMgr.tempNodesSize() >= this.mgr.getMaxTempNodes()) {
+ return;
+ }
+
if (this.mgr.validateNode(incomingNode)) {
nodeMgr.addTempNode(incomingNode);
}
@@ -484,15 +508,15 @@ private void handleP2pMsg(final SelectionKey _sk, byte _act, final byte[] _msgBy
* @param _netId int
* @param _port int
* @param _revision byte[]
- * Construct node info after handshake request success
+ *
Construct node info after handshake request success
*/
private void handleReqHandshake(
- final ChannelBuffer _buffer,
- int _channelHash,
- final byte[] _nodeId,
- int _netId,
- int _port,
- final byte[] _revision) {
+ final ChannelBuffer _buffer,
+ int _channelHash,
+ final byte[] _nodeId,
+ int _netId,
+ int _port,
+ final byte[] _revision) {
INode node = nodeMgr.getInboundNode(_channelHash);
if (node != null && node.getPeerMetric().notBan()) {
if (handshakeRuleCheck(_netId)) {
@@ -512,15 +536,17 @@ private void handleReqHandshake(
node.setBinaryVersion(binaryVersion);
nodeMgr.moveInboundToActive(_channelHash, this.mgr);
this.sendMsgQue.offer(
- new MsgOut(
- node.getIdHash(),
- node.getIdShort(),
- this.cachedResHandshake1,
- Dest.ACTIVE));
+ new MsgOut(
+ node.getIdHash(),
+ node.getIdShort(),
+ this.cachedResHandshake1,
+ Dest.ACTIVE));
}
} else {
- if (this.mgr.isShowLog()) { System.out.println(""); }
+ if (this.mgr.isShowLog()) {
+ System.out.println("");
+ }
}
}
}
@@ -549,16 +575,16 @@ private void handleKernelMsg(int _nodeIdHash, int _route, final byte[] _msgBytes
}
}
- /** @return boolean TODO: implementation */
+ /**
+ * @return boolean TODO: implementation
+ */
private boolean handshakeRuleCheck(int netId) {
// check net id
- if (netId != this.mgr.getSelfNetId()) { return false; }
- // check supported protocol versions
- return true;
+ return netId == this.mgr.getSelfNetId();
}
private String getReadOverflowMsg(int prevCnt, int cnt) {
- return "IO read overflow! suppose read:" + prevCnt + " real left:" + cnt;
+ return "IO readBuffer overflow! suppose readBuffer:" + prevCnt + " real left:" + cnt;
}
private String getRouteMsg(short ver, byte ctrl, byte act, int count, String idStr) {
diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java
index b5af5e5e0d..14d20b358b 100644
--- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java
+++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskReceive.java
@@ -29,16 +29,17 @@
import org.aion.p2p.Handler;
public class TaskReceive implements Runnable {
+
private final AtomicBoolean start;
private final BlockingQueue receiveMsgQue;
private final Map> handlers;
private final boolean showLog;
public TaskReceive(
- AtomicBoolean _start,
- BlockingQueue _receiveMsgQue,
- Map> _handlers,
- boolean _showLog) {
+ final AtomicBoolean _start,
+ final BlockingQueue _receiveMsgQue,
+ final Map> _handlers,
+ final boolean _showLog) {
this.start = _start;
this.receiveMsgQue = _receiveMsgQue;
this.handlers = _handlers;
@@ -52,21 +53,31 @@ public void run() {
MsgIn mi = this.receiveMsgQue.take();
List hs = this.handlers.get(mi.getRoute());
- if (hs == null) { continue; }
+ if (hs == null) {
+ continue;
+ }
for (Handler hlr : hs) {
- if (hlr == null) { continue; }
+ if (hlr == null) {
+ continue;
+ }
try {
hlr.receive(mi.getNodeId(), mi.getDisplayId(), mi.getMsg());
} catch (Exception e) {
- if (this.showLog) { e.printStackTrace(); }
+ if (this.showLog) {
+ e.printStackTrace();
+ }
}
}
} catch (InterruptedException e) {
- if (this.showLog) { System.out.println(""); }
+ if (this.showLog) {
+ System.out.println("");
+ }
return;
} catch (Exception e) {
- if (this.showLog) { e.printStackTrace(); }
+ if (this.showLog) {
+ e.printStackTrace();
+ }
}
}
}
diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java
index 904467b305..3329ac2ae1 100644
--- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java
+++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskSend.java
@@ -25,6 +25,11 @@
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.aion.p2p.INode;
import org.aion.p2p.INodeMgr;
@@ -32,7 +37,10 @@
import org.aion.p2p.P2pConstant;
public class TaskSend implements Runnable {
- public static final int TOTAL_LANE = (1 << 5) - 1;
+
+ private static final int TOTAL_LANE = Math
+ .min(Runtime.getRuntime().availableProcessors() << 1, 32);
+ private static final int THREAD_Q_LIMIT = 20000;
private final IP2pMgr mgr;
private final AtomicBoolean start;
@@ -41,13 +49,15 @@ public class TaskSend implements Runnable {
private final Selector selector;
private final int lane;
+ private static ThreadPoolExecutor tpe;
+
public TaskSend(
- IP2pMgr _mgr,
- int _lane,
- BlockingQueue _sendMsgQue,
- AtomicBoolean _start,
- INodeMgr _nodeMgr,
- Selector _selector) {
+ final IP2pMgr _mgr,
+ final int _lane,
+ final BlockingQueue _sendMsgQue,
+ final AtomicBoolean _start,
+ final INodeMgr _nodeMgr,
+ final Selector _selector) {
this.mgr = _mgr;
this.lane = _lane;
@@ -55,6 +65,15 @@ public TaskSend(
this.start = _start;
this.nodeMgr = _nodeMgr;
this.selector = _selector;
+
+ if (tpe == null) {
+ tpe = new ThreadPoolExecutor(TOTAL_LANE
+ , TOTAL_LANE
+ , 0
+ , TimeUnit.MILLISECONDS
+ , new LinkedBlockingQueue<>(THREAD_Q_LIMIT)
+ , Executors.defaultThreadFactory());
+ }
}
@Override
@@ -62,6 +81,7 @@ public void run() {
while (start.get()) {
try {
MsgOut mo = sendMsgQue.take();
+
// if timeout , throw away this msg.
long now = System.currentTimeMillis();
if (now - mo.getTimestamp() > P2pConstant.WRITE_MSG_TIMEOUT) {
@@ -72,8 +92,8 @@ public void run() {
}
// if not belong to current lane, put it back.
- int targetLane = hash2Lane(mo.getNodeId());
- if (targetLane != lane) {
+ long t1 = System.nanoTime();
+ if (mo.getLane() != lane) {
sendMsgQue.offer(mo);
continue;
}
@@ -96,15 +116,13 @@ public void run() {
if (sk != null) {
Object attachment = sk.attachment();
if (attachment != null) {
- TaskWrite tw =
- new TaskWrite(
- this.mgr.isShowLog(),
- node.getIdShort(),
- node.getChannel(),
- mo.getMsg(),
- (ChannelBuffer) attachment,
- this.mgr);
- tw.run();
+ tpe.execute(new TaskWrite(
+ this.mgr.isShowLog(),
+ node.getIdShort(),
+ node.getChannel(),
+ mo.getMsg(),
+ (ChannelBuffer) attachment,
+ this.mgr));
}
}
} else {
@@ -114,22 +132,30 @@ public void run() {
}
}
} catch (InterruptedException e) {
- if (this.mgr.isShowLog()) { System.out.println(""); }
+ if (this.mgr.isShowLog()) {
+ System.out.println("");
+ }
return;
+ } catch (RejectedExecutionException e) {
+ if (this.mgr.isShowLog()) {
+ System.out.println("");
+ }
} catch (Exception e) {
- if (this.mgr.isShowLog()) { e.printStackTrace(); }
+ if (this.mgr.isShowLog()) {
+ e.printStackTrace();
+ }
}
}
}
// hash mapping channel id to write thread.
- private int hash2Lane(int in) {
+ static int hash2Lane(int in) {
in ^= in >> (32 - 5);
in ^= in >> (32 - 10);
in ^= in >> (32 - 15);
in ^= in >> (32 - 20);
in ^= in >> (32 - 25);
- return in & 0b11111;
+ return (in & 0b11111) * TOTAL_LANE / 32;
}
private String getTimeoutMsg(String id, long now) {
diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskStatus.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskStatus.java
index 38a16ea850..b62a4dae20 100644
--- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskStatus.java
+++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskStatus.java
@@ -26,16 +26,17 @@
import org.aion.p2p.INodeMgr;
public class TaskStatus implements Runnable {
+
private final INodeMgr nodeMgr;
private final String selfShortId;
- private BlockingQueue sendMsgQue;
- private BlockingQueue receiveMsgQue;
+ private final BlockingQueue sendMsgQue;
+ private final BlockingQueue receiveMsgQue;
public TaskStatus(
- INodeMgr _nodeMgr,
- String _selfShortId,
- BlockingQueue _sendMsgQue,
- BlockingQueue _receiveMsgQue) {
+ final INodeMgr _nodeMgr,
+ final String _selfShortId,
+ final BlockingQueue _sendMsgQue,
+ final BlockingQueue _receiveMsgQue) {
this.nodeMgr = _nodeMgr;
this.selfShortId = _selfShortId;
this.sendMsgQue = _sendMsgQue;
@@ -48,13 +49,13 @@ public void run() {
String status = this.nodeMgr.dumpNodeInfo(this.selfShortId);
System.out.println(status);
System.out.println("--------------------------------------------------------------------" +
- "-------------------------------------------------------------------------------" +
- "-----------------");
+ "-------------------------------------------------------------------------------" +
+ "-----------------");
System.out.println(
- "recv queue ["
- + this.receiveMsgQue.size()
- + "] send queue ["
- + this.sendMsgQue.size()
- + "]\n");
+ "recv queue ["
+ + this.receiveMsgQue.size()
+ + "] send queue ["
+ + this.sendMsgQue.size()
+ + "]\n");
}
}
diff --git a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java
index 38e3dacbd4..729027c448 100644
--- a/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java
+++ b/modP2pImpl/src/org/aion/p2p/impl1/tasks/TaskWrite.java
@@ -30,23 +30,25 @@
import org.aion.p2p.IP2pMgr;
import org.aion.p2p.Msg;
-/** @author chris */
+/**
+ * @author chris
+ */
public class TaskWrite implements Runnable {
- private boolean showLog;
- private String nodeShortId;
- private SocketChannel sc;
- private Msg msg;
- private ChannelBuffer channelBuffer;
- private IP2pMgr p2pMgr;
+ private final boolean showLog;
+ private final String nodeShortId;
+ private final SocketChannel sc;
+ private final Msg msg;
+ private final ChannelBuffer channelBuffer;
+ private final IP2pMgr p2pMgr;
TaskWrite(
- boolean _showLog,
- String _nodeShortId,
- final SocketChannel _sc,
- final Msg _msg,
- final ChannelBuffer _cb,
- final IP2pMgr _p2pMgr) {
+ final boolean _showLog,
+ final String _nodeShortId,
+ final SocketChannel _sc,
+ final Msg _msg,
+ final ChannelBuffer _cb,
+ final IP2pMgr _p2pMgr) {
this.showLog = _showLog;
this.nodeShortId = _nodeShortId;
this.sc = _sc;
@@ -81,7 +83,9 @@ public void run() {
// System.out.println("write " + h.getVer() + "-" + h.getCtrl() + "-" + h.getAction());
ByteBuffer buf = ByteBuffer.allocate(headerBytes.length + bodyLen);
buf.put(headerBytes);
- if (bodyBytes != null) { buf.put(bodyBytes); }
+ if (bodyBytes != null) {
+ buf.put(bodyBytes);
+ }
buf.flip();
try {
@@ -94,18 +98,18 @@ public void run() {
} catch (ClosedChannelException ex1) {
if (showLog) {
System.out.println(
- "");
+ "");
}
channelBuffer.isClosed.set(true);
} catch (IOException ex2) {
String reason = ex2.getMessage();
if (showLog) {
System.out.println(
- "");
+ "");
}
if (reason.equals("Broken pipe")) {
channelBuffer.isClosed.set(true);
diff --git a/modP2pImpl/test/org/aion/p2p/impl/Test.java b/modP2pImpl/test/org/aion/p2p/impl/Test.java
index 8bedcba68b..293061c8d8 100644
--- a/modP2pImpl/test/org/aion/p2p/impl/Test.java
+++ b/modP2pImpl/test/org/aion/p2p/impl/Test.java
@@ -1,11 +1,11 @@
package org.aion.p2p.impl;
-import java.util.HashSet;
-import java.util.Set;
-
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
+import java.util.HashSet;
+import java.util.Set;
+
public class Test {
@org.junit.Test