anmolnar commented on a change in pull request #730: Zookeeper-3188: Improve resilience to network URL: https://github.com/apache/zookeeper/pull/730#discussion_r260756647
########## File path: zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Learner.java ########## @@ -249,54 +253,23 @@ protected void sockConnect(Socket sock, InetSocketAddress addr, int timeout) * @param addr - the address of the Peer to connect to. * @throws IOException - if the socket connection fails on the 5th attempt * <li>if there is an authentication failure while connecting to leader</li> - * @throws ConnectException * @throws InterruptedException */ - protected void connectToLeader(InetSocketAddress addr, String hostname) - throws IOException, InterruptedException, X509Exception { - this.sock = createSocket(); + protected void connectToLeader(MultipleAddresses addr, String hostname) + throws IOException, InterruptedException { - int initLimitTime = self.tickTime * self.initLimit; - int remainingInitLimitTime; - long startNanoTime = nanoTime(); + List<InetSocketAddress> addresses = addr.getAllAddresses(); + ExecutorService executor = Executors.newFixedThreadPool(addresses.size()); + CountDownLatch latch = new CountDownLatch(addresses.size()); + AtomicReference<Socket> socket = new AtomicReference<>(null); + addresses.stream().map(address -> new LeaderConnector(address, socket, latch)).forEach(executor::submit); - for (int tries = 0; tries < 5; tries++) { - try { - // recalculate the init limit time because retries sleep for 1000 milliseconds - remainingInitLimitTime = initLimitTime - (int)((nanoTime() - startNanoTime) / 1000000); - if (remainingInitLimitTime <= 0) { - LOG.error("initLimit exceeded on retries."); - throw new IOException("initLimit exceeded on retries."); - } + latch.await(); - sockConnect(sock, addr, Math.min(self.tickTime * self.syncLimit, remainingInitLimitTime)); - if (self.isSslQuorum()) { - ((SSLSocket) sock).startHandshake(); - } - sock.setTcpNoDelay(nodelay); - break; - } catch (IOException e) { - remainingInitLimitTime = initLimitTime - (int)((nanoTime() - startNanoTime) / 1000000); - - if (remainingInitLimitTime <= 1000) { - LOG.error("Unexpected exception, initLimit exceeded. tries=" + tries + - ", remaining init limit=" + remainingInitLimitTime + - ", connecting to " + addr,e); - throw e; - } else if (tries >= 4) { - LOG.error("Unexpected exception, retries exceeded. tries=" + tries + - ", remaining init limit=" + remainingInitLimitTime + - ", connecting to " + addr,e); - throw e; - } else { - LOG.warn("Unexpected exception, tries=" + tries + - ", remaining init limit=" + remainingInitLimitTime + - ", connecting to " + addr,e); - this.sock = createSocket(); - } - } - Thread.sleep(leaderConnectDelayDuringRetryMs); - } + if(socket.get() == null) Review comment: Code style: please use brackets ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services