From 7173bb71b56710e8421ac322a3179404b1a3a9c5 Mon Sep 17 00:00:00 2001 From: Christoph Sturm Date: Fri, 6 Sep 2019 11:14:19 +0200 Subject: [PATCH 1/3] Don't build debug strings when debug is not enabled. --- .../bisq/network/p2p/network/NetworkNode.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java index ac1ff69256b..74adfaf3995 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java @@ -98,7 +98,8 @@ public abstract class NetworkNode implements MessageListener { public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddress, NetworkEnvelope networkEnvelope) { - log.debug("sendMessage: peersNodeAddress=" + peersNodeAddress + "\n\tmessage=" + Utilities.toTruncatedString(networkEnvelope)); + if (log.isDebugEnabled()) + log.debug("sendMessage: peersNodeAddress=" + peersNodeAddress + "\n\tmessage=" + Utilities.toTruncatedString(networkEnvelope)); checkNotNull(peersNodeAddress, "peerAddress must not be null"); Connection connection = getOutboundConnection(peersNodeAddress); @@ -123,11 +124,13 @@ public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddr try { // can take a while when using tor long startTs = System.currentTimeMillis(); - log.debug("Start create socket to peersNodeAddress {}", peersNodeAddress.getFullAddress()); + if (log.isDebugEnabled()) + log.debug("Start create socket to peersNodeAddress {}", peersNodeAddress.getFullAddress()); Socket socket = createSocket(peersNodeAddress); long duration = System.currentTimeMillis() - startTs; - log.debug("Socket creation to peersNodeAddress {} took {} ms", peersNodeAddress.getFullAddress(), - duration); + if (log.isDebugEnabled()) + log.debug("Socket creation to peersNodeAddress {} took {} ms", peersNodeAddress.getFullAddress(), + duration); if (duration > CREATE_SOCKET_TIMEOUT) throw new TimeoutException("A timeout occurred when creating a socket."); @@ -139,7 +142,8 @@ public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddr existingConnection = getOutboundConnection(peersNodeAddress); if (existingConnection != null) { - log.debug("We found in the meantime a connection for peersNodeAddress {}, " + + if (log.isDebugEnabled()) + log.debug("We found in the meantime a connection for peersNodeAddress {}, " + "so we use that for sending the message.\n" + "That can happen if Tor needs long for creating a new outbound connection.\n" + "We might have got a new inbound or outbound connection.", @@ -184,13 +188,14 @@ public void onError(Throwable throwable) { peersNodeAddress, networkProtoResolver); - log.debug("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" + - "NetworkNode created new outbound connection:" - + "\nmyNodeAddress=" + getNodeAddress() - + "\npeersNodeAddress=" + peersNodeAddress - + "\nuid=" + outboundConnection.getUid() - + "\nmessage=" + networkEnvelope - + "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"); + if (log.isDebugEnabled()) + log.debug("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" + + "NetworkNode created new outbound connection:" + + "\nmyNodeAddress=" + getNodeAddress() + + "\npeersNodeAddress=" + peersNodeAddress + + "\nuid=" + outboundConnection.getUid() + + "\nmessage=" + networkEnvelope + + "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"); // can take a while when using tor outboundConnection.sendMessage(networkEnvelope); From a3bf6ab4cb7246351fb46ecc12d3c59d07706971 Mon Sep 17 00:00:00 2001 From: Christoph Sturm Date: Fri, 6 Sep 2019 11:24:35 +0200 Subject: [PATCH 2/3] Don't build debug strings when debug is not enabled. --- .../bisq/network/p2p/network/Connection.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/Connection.java b/p2p/src/main/java/bisq/network/p2p/network/Connection.java index d3db7d72657..b6f49e7adbd 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Connection.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Connection.java @@ -704,9 +704,10 @@ public void run() { long now = System.currentTimeMillis(); long elapsed = now - lastReadTimeStamp; if (elapsed < 10) { - log.debug("We got 2 network_messages received in less than 10 ms. We set the thread to sleep " + - "for 20 ms to avoid getting flooded by our peer. lastReadTimeStamp={}, now={}, elapsed={}", - lastReadTimeStamp, now, elapsed); + if (log.isDebugEnabled()) + log.debug("We got 2 network_messages received in less than 10 ms. We set the thread to sleep " + + "for 20 ms to avoid getting flooded by our peer. lastReadTimeStamp={}, now={}, elapsed={}", + lastReadTimeStamp, now, elapsed); Thread.sleep(20); } @@ -724,7 +725,8 @@ public void run() { NetworkEnvelope networkEnvelope = networkProtoResolver.fromProto(proto); lastReadTimeStamp = now; - log.debug("<< Received networkEnvelope of type: " + networkEnvelope.getClass().getSimpleName()); + if (log.isDebugEnabled()) + log.debug("<< Received networkEnvelope of type: " + networkEnvelope.getClass().getSimpleName()); int size = proto.getSerializedSize(); // We comment out that part as only debug and trace log level is used. For debugging purposes @@ -761,7 +763,8 @@ public void run() { boolean exceeds; if (networkEnvelope instanceof ExtendedDataSizePermission) { exceeds = size > MAX_PERMITTED_MESSAGE_SIZE; - log.debug("size={}; object={}", size, Utilities.toTruncatedString(proto, 100)); + if (log.isDebugEnabled()) + log.debug("size={}; object={}", size, Utilities.toTruncatedString(proto, 100)); } else { exceeds = size > PERMITTED_MESSAGE_SIZE; } @@ -819,7 +822,8 @@ && reportInvalidRequest(RuleViolation.WRONG_NETWORK_ID)) { if (networkEnvelope instanceof CloseConnectionMessage) { // If we get a CloseConnectionMessage we shut down - log.debug("CloseConnectionMessage received. Reason={}\n\t" + + if (log.isDebugEnabled()) + log.debug("CloseConnectionMessage received. Reason={}\n\t" + "connection={}", proto.getCloseConnectionMessage().getReason(), this); if (CloseConnectionReason.PEER_BANNED.name().equals(proto.getCloseConnectionMessage().getReason())) { log.warn("We got shut down because we are banned by the other peer. (InputHandler.run CloseConnectionMessage)"); From ecaf592a6040e367925b6c798242581971895a22 Mon Sep 17 00:00:00 2001 From: Christoph Sturm Date: Fri, 6 Sep 2019 11:57:15 +0200 Subject: [PATCH 3/3] Implement reviewer suggestions also in NetworkNode --- .../bisq/network/p2p/network/Connection.java | 19 +++++++------- .../bisq/network/p2p/network/NetworkNode.java | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/Connection.java b/p2p/src/main/java/bisq/network/p2p/network/Connection.java index b6f49e7adbd..ebfd14e9a00 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/Connection.java +++ b/p2p/src/main/java/bisq/network/p2p/network/Connection.java @@ -704,10 +704,9 @@ public void run() { long now = System.currentTimeMillis(); long elapsed = now - lastReadTimeStamp; if (elapsed < 10) { - if (log.isDebugEnabled()) - log.debug("We got 2 network_messages received in less than 10 ms. We set the thread to sleep " + - "for 20 ms to avoid getting flooded by our peer. lastReadTimeStamp={}, now={}, elapsed={}", - lastReadTimeStamp, now, elapsed); + log.debug("We got 2 network_messages received in less than 10 ms. We set the thread to sleep " + + "for 20 ms to avoid getting flooded by our peer. lastReadTimeStamp={}, now={}, elapsed={}", + lastReadTimeStamp, now, elapsed); Thread.sleep(20); } @@ -725,9 +724,7 @@ public void run() { NetworkEnvelope networkEnvelope = networkProtoResolver.fromProto(proto); lastReadTimeStamp = now; - if (log.isDebugEnabled()) - log.debug("<< Received networkEnvelope of type: " + networkEnvelope.getClass().getSimpleName()); - + log.debug("<< Received networkEnvelope of type: {}", networkEnvelope.getClass().getSimpleName()); int size = proto.getSerializedSize(); // We comment out that part as only debug and trace log level is used. For debugging purposes // we leave the code though. @@ -763,8 +760,9 @@ public void run() { boolean exceeds; if (networkEnvelope instanceof ExtendedDataSizePermission) { exceeds = size > MAX_PERMITTED_MESSAGE_SIZE; - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("size={}; object={}", size, Utilities.toTruncatedString(proto, 100)); + } } else { exceeds = size > PERMITTED_MESSAGE_SIZE; } @@ -822,9 +820,10 @@ && reportInvalidRequest(RuleViolation.WRONG_NETWORK_ID)) { if (networkEnvelope instanceof CloseConnectionMessage) { // If we get a CloseConnectionMessage we shut down - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("CloseConnectionMessage received. Reason={}\n\t" + - "connection={}", proto.getCloseConnectionMessage().getReason(), this); + "connection={}", proto.getCloseConnectionMessage().getReason(), this); + } if (CloseConnectionReason.PEER_BANNED.name().equals(proto.getCloseConnectionMessage().getReason())) { log.warn("We got shut down because we are banned by the other peer. (InputHandler.run CloseConnectionMessage)"); shutDown(CloseConnectionReason.PEER_BANNED); diff --git a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java index 74adfaf3995..94262d8f7ff 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java @@ -98,8 +98,9 @@ public abstract class NetworkNode implements MessageListener { public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddress, NetworkEnvelope networkEnvelope) { - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("sendMessage: peersNodeAddress=" + peersNodeAddress + "\n\tmessage=" + Utilities.toTruncatedString(networkEnvelope)); + } checkNotNull(peersNodeAddress, "peerAddress must not be null"); Connection connection = getOutboundConnection(peersNodeAddress); @@ -124,13 +125,15 @@ public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddr try { // can take a while when using tor long startTs = System.currentTimeMillis(); - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("Start create socket to peersNodeAddress {}", peersNodeAddress.getFullAddress()); + } Socket socket = createSocket(peersNodeAddress); long duration = System.currentTimeMillis() - startTs; - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("Socket creation to peersNodeAddress {} took {} ms", peersNodeAddress.getFullAddress(), duration); + } if (duration > CREATE_SOCKET_TIMEOUT) throw new TimeoutException("A timeout occurred when creating a socket."); @@ -142,12 +145,14 @@ public SettableFuture sendMessage(@NotNull NodeAddress peersNodeAddr existingConnection = getOutboundConnection(peersNodeAddress); if (existingConnection != null) { - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("We found in the meantime a connection for peersNodeAddress {}, " + - "so we use that for sending the message.\n" + - "That can happen if Tor needs long for creating a new outbound connection.\n" + - "We might have got a new inbound or outbound connection.", - peersNodeAddress.getFullAddress()); + "so we use that for sending the message.\n" + + "That can happen if Tor needs long for creating a new outbound connection.\n" + + "We might have got a new inbound or outbound connection.", + peersNodeAddress.getFullAddress()); + + } try { socket.close(); } catch (Throwable throwable) { @@ -188,7 +193,7 @@ public void onError(Throwable throwable) { peersNodeAddress, networkProtoResolver); - if (log.isDebugEnabled()) + if (log.isDebugEnabled()) { log.debug("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" + "NetworkNode created new outbound connection:" + "\nmyNodeAddress=" + getNodeAddress() @@ -196,7 +201,7 @@ public void onError(Throwable throwable) { + "\nuid=" + outboundConnection.getUid() + "\nmessage=" + networkEnvelope + "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"); - + } // can take a while when using tor outboundConnection.sendMessage(networkEnvelope); return outboundConnection;