Skip to content

Commit 98178d1

Browse files
committed
Fixed for connection failure logging
1 parent e54b227 commit 98178d1

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

convex-peer/src/main/java/convex/api/ConvexRemote.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static ConvexRemote connect(InetSocketAddress peerAddress) throws IOExcep
7575
return convex;
7676
}
7777

78-
public static ConvexRemote connectNetty(InetSocketAddress sa) throws InterruptedException {
78+
public static ConvexRemote connectNetty(InetSocketAddress sa) throws InterruptedException, IOException {
7979
ConvexRemote convex=new ConvexRemote(null,null);
8080
convex.remoteAddress=sa;
8181
convex.setConnection(NettyConnection.connect(sa, convex.returnMessageHandler));

convex-peer/src/main/java/convex/net/impl/netty/NettyConnection.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package convex.net.impl.netty;
22

3+
import java.io.IOException;
34
import java.net.InetSocketAddress;
45
import java.net.SocketAddress;
56
import java.util.function.Consumer;
67

8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
711
import convex.core.data.Vectors;
812
import convex.core.message.Message;
913
import convex.core.message.MessageType;
@@ -22,6 +26,8 @@
2226

2327
public class NettyConnection extends AConnection {
2428

29+
static final Logger log = LoggerFactory.getLogger(NettyConnection.class.getName());
30+
2531
/**
2632
* Static client connection worker
2733
*/
@@ -80,10 +86,15 @@ public void initChannel(SocketChannel ch) throws Exception {
8086
return clientBootstrap;
8187
}
8288
}
83-
84-
public static NettyConnection connect(SocketAddress sa, Consumer<Message> receiveAction) throws InterruptedException {
89+
90+
public static NettyConnection connect(SocketAddress sa, Consumer<Message> receiveAction) throws InterruptedException, IOException {
8591
Bootstrap b = getClientBootstrap();
86-
ChannelFuture f = b.connect(sa).sync(); // (5)
92+
ChannelFuture f = b.connect(sa);
93+
f.await(); // Wait until done
94+
95+
if (!f.isSuccess()) {
96+
throw new IOException("Failed to connect to peer",f.cause());
97+
}
8798

8899
Channel chan = f.channel();
89100
NettyInboundHandler inbound=new NettyInboundHandler(receiveAction,null);

convex-peer/src/main/java/convex/peer/API.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.List;
77
import java.util.Map;
8+
import java.util.concurrent.TimeoutException;
89

910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
@@ -176,16 +177,20 @@ public static List<Server> launchLocalPeers(List<AKeyPair> keyPairs, State genes
176177

177178
genesisServer.setHostname("localhost:"+genesisServer.getPort());
178179

179-
for (int i = 1; i < count; i++) {
180-
Server server=serverList.get(i);
181-
182-
// Join each additional Server to the Peer #0
183-
ConnectionManager cm=server.getConnectionManager();
184-
cm.connectToPeer(genesisServer.getHostAddress());
185-
186-
// Join server #0 to this server
187-
genesisServer.getConnectionManager().connectToPeer(server.getHostAddress());
188-
server.setHostname("localhost:"+server.getPort());
180+
try {
181+
for (int i = 1; i < count; i++) {
182+
Server server=serverList.get(i);
183+
184+
// Join each additional Server to the Peer #0
185+
ConnectionManager cm=server.getConnectionManager();
186+
cm.connectToPeer(genesisServer.getHostAddress());
187+
188+
// Join server #0 to this server
189+
genesisServer.getConnectionManager().connectToPeer(server.getHostAddress());
190+
server.setHostname("localhost:"+server.getPort());
191+
}
192+
} catch (IOException|TimeoutException e) {
193+
throw new LaunchException("Error setting up peer connections",e);
189194
}
190195

191196
return serverList;

convex-peer/src/main/java/convex/peer/ConnectionManager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ private void tryRandomConnect(State s) throws InterruptedException {
235235

236236
if (target!=null) {
237237
// Try to connect to Peer. If it fails, no worry, will retry another peer next time
238-
boolean success=connectToPeer(target) != null;
239-
if (!success) {
240-
log.warn("Failed to connect to Peer at "+target);
238+
try {
239+
connectToPeer(target);
240+
} catch (IOException|TimeoutException e) {
241+
log.debug("Failed to connect to Peer at "+target,e);
241242
}
242243
}
243244
}
@@ -581,10 +582,12 @@ private synchronized HashMap<AccountKey, Convex> getCurrentConnections() {
581582
/**
582583
* Connects explicitly to a Peer at the given host address
583584
* @param hostAddress Address to connect to
584-
* @return new Connection, or null if attempt fails
585+
* @return new Convex connection, or null if attempt fails
585586
* @throws InterruptedException
587+
* @throws TimeoutException
588+
* @throws IOException
586589
*/
587-
public Convex connectToPeer(InetSocketAddress hostAddress) throws InterruptedException {
590+
public Convex connectToPeer(InetSocketAddress hostAddress) throws InterruptedException, IOException, TimeoutException {
588591
try {
589592
Convex convex=Convex.connect(hostAddress);
590593
Result result = convex.requestStatusSync(Config.DEFAULT_CLIENT_TIMEOUT);
@@ -611,9 +614,6 @@ public Convex connectToPeer(InetSocketAddress hostAddress) throws InterruptedExc
611614
addConnection(peerKey, convex);
612615
}
613616
return convex;
614-
} catch (IOException | TimeoutException e) {
615-
log.warn("Error connecting to peer: ",e);
616-
return null;
617617
} catch (UnresolvedAddressException e) {
618618
log.info("Unable to resolve host address: "+hostAddress);
619619
return null;

0 commit comments

Comments
 (0)