Improvements
- Massively reduced logging, especially at debug level - Optimizations to reduce system load - Use bootstrapping to find stable nodes
This commit is contained in:
@ -235,10 +235,9 @@ public class Connection {
|
||||
ObjectMessage objectMessage = (ObjectMessage) messagePayload;
|
||||
try {
|
||||
if (ctx.getInventory().contains(objectMessage)) {
|
||||
LOG.debug("Received object " + objectMessage.getInventoryVector() + " - already in inventory");
|
||||
LOG.trace("Received object " + objectMessage.getInventoryVector() + " - already in inventory");
|
||||
break;
|
||||
}
|
||||
LOG.debug("Received object " + objectMessage.getInventoryVector());
|
||||
security().checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes());
|
||||
listener.receive(objectMessage);
|
||||
ctx.getInventory().storeObject(objectMessage);
|
||||
@ -294,7 +293,6 @@ public class Connection {
|
||||
}
|
||||
|
||||
public void offer(InventoryVector iv) {
|
||||
LOG.debug("Offering " + iv + " to node " + node.toString());
|
||||
sendingQueue.offer(new Inv.Builder()
|
||||
.addInventoryVector(iv)
|
||||
.build());
|
||||
@ -321,14 +319,7 @@ public class Connection {
|
||||
private synchronized void initSocket(Socket socket) throws IOException {
|
||||
if (!socketInitialized) {
|
||||
if (!socket.isConnected()) {
|
||||
LOG.debug("Trying to connect to node " + node);
|
||||
socket.connect(new InetSocketAddress(node.toInetAddress(), node.getPort()), CONNECT_TIMEOUT);
|
||||
}
|
||||
socket.setSoTimeout(READ_TIMEOUT);
|
||||
in = socket.getInputStream();
|
||||
out = socket.getOutputStream();
|
||||
if (!socket.isConnected()) {
|
||||
LOG.debug("Trying to connect to node " + node);
|
||||
LOG.trace("Trying to connect to node " + node);
|
||||
socket.connect(new InetSocketAddress(node.toInetAddress(), node.getPort()), CONNECT_TIMEOUT);
|
||||
}
|
||||
socket.setSoTimeout(READ_TIMEOUT);
|
||||
@ -359,6 +350,7 @@ public class Connection {
|
||||
send(new Version.Builder().defaults().addrFrom(host).addrRecv(node).build());
|
||||
}
|
||||
while (state != DISCONNECTED) {
|
||||
Thread.sleep(100);
|
||||
try {
|
||||
NetworkMessage msg = Factory.getNetworkMessage(version, in);
|
||||
if (msg == null)
|
||||
@ -413,15 +405,13 @@ public class Connection {
|
||||
if (syncFinished(null)) disconnect();
|
||||
}
|
||||
}
|
||||
Thread.yield();
|
||||
}
|
||||
} catch (IOException | NodeException e) {
|
||||
disconnect();
|
||||
LOG.debug("Reader disconnected from node " + node + ": " + e.getMessage());
|
||||
} catch (InterruptedException | IOException | NodeException e) {
|
||||
LOG.trace("Reader disconnected from node " + node + ": " + e.getMessage());
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debug("Reader disconnecting from node " + node + " due to error: " + e.getMessage(), e);
|
||||
disconnect();
|
||||
LOG.trace("Reader disconnecting from node " + node + " due to error: " + e.getMessage(), e);
|
||||
} finally {
|
||||
disconnect();
|
||||
try {
|
||||
socket.close();
|
||||
} catch (Exception e) {
|
||||
@ -438,14 +428,13 @@ public class Connection {
|
||||
initSocket(socket);
|
||||
while (state != DISCONNECTED) {
|
||||
if (sendingQueue.size() > 0) {
|
||||
LOG.debug("Sending " + sendingQueue.size() + " messages to node " + node);
|
||||
send(sendingQueue.poll());
|
||||
} else {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
LOG.debug("Writer disconnected from node " + node + ": " + e.getMessage());
|
||||
LOG.trace("Writer disconnected from node " + node + ": " + e.getMessage());
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,7 @@ import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static ch.dissem.bitmessage.networking.Connection.Mode.CLIENT;
|
||||
import static ch.dissem.bitmessage.networking.Connection.Mode.SERVER;
|
||||
@ -58,7 +55,14 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
private ConcurrentMap<InventoryVector, Long> requestedObjects = new ConcurrentHashMap<>();
|
||||
|
||||
public DefaultNetworkHandler() {
|
||||
pool = Executors.newCachedThreadPool();
|
||||
pool = Executors.newCachedThreadPool(new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread thread = Executors.defaultThreadFactory().newThread(r);
|
||||
thread.setPriority(Thread.MIN_PRIORITY);
|
||||
return thread;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -67,14 +71,12 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread synchronize(InetAddress trustedHost, int port, MessageListener listener, long timeoutInSeconds) {
|
||||
public Future<?> synchronize(InetAddress trustedHost, int port, MessageListener listener, long timeoutInSeconds) {
|
||||
try {
|
||||
Connection connection = Connection.sync(ctx, trustedHost, port, listener, timeoutInSeconds);
|
||||
Thread tr = new Thread(connection.getReader());
|
||||
Thread tw = new Thread(connection.getWriter());
|
||||
tr.start();
|
||||
tw.start();
|
||||
return tr;
|
||||
Future<?> reader = pool.submit(connection.getReader());
|
||||
pool.execute(connection.getWriter());
|
||||
return reader;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@ -143,8 +145,10 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
for (NetworkAddress address : addresses) {
|
||||
startConnection(new Connection(ctx, CLIENT, address, listener, requestedObjects));
|
||||
}
|
||||
Thread.sleep(10000);
|
||||
} else {
|
||||
Thread.sleep(30000);
|
||||
}
|
||||
Thread.sleep(30000);
|
||||
} catch (InterruptedException e) {
|
||||
running = false;
|
||||
} catch (Exception e) {
|
||||
@ -204,7 +208,6 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG.debug(target.size() + " connections available to offer " + iv);
|
||||
List<Connection> randomSubset = Collections.selectRandom(NETWORK_MAGIC_NUMBER, target);
|
||||
for (Connection connection : randomSubset) {
|
||||
connection.offer(iv);
|
||||
|
@ -29,6 +29,7 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -116,10 +117,10 @@ public class NetworkHandlerTest {
|
||||
"V1Msg.payload"
|
||||
);
|
||||
|
||||
Thread t = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
Future<?> future = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
t.join();
|
||||
future.get();
|
||||
assertInventorySize(3, nodeInventory);
|
||||
assertInventorySize(3, peerInventory);
|
||||
}
|
||||
@ -133,10 +134,10 @@ public class NetworkHandlerTest {
|
||||
|
||||
nodeInventory.init();
|
||||
|
||||
Thread t = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
Future<?> future = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
t.join();
|
||||
future.get();
|
||||
assertInventorySize(2, nodeInventory);
|
||||
assertInventorySize(2, peerInventory);
|
||||
}
|
||||
@ -149,10 +150,10 @@ public class NetworkHandlerTest {
|
||||
"V1Msg.payload"
|
||||
);
|
||||
|
||||
Thread t = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
Future<?> future = networkHandler.synchronize(InetAddress.getLocalHost(), 6001,
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
t.join();
|
||||
future.get();
|
||||
assertInventorySize(1, nodeInventory);
|
||||
assertInventorySize(1, peerInventory);
|
||||
}
|
||||
|
Reference in New Issue
Block a user