/** * Handle connection failures * * <p>If the current number of retries is equal to the max number of retries, stop retrying and * throw the exception; Otherwise backoff N seconds and try connecting again. * * <p>This Method is only called from inside setupIOstreams(), which is synchronized. Hence the * sleep is synchronized; the locks will be retained. * * @param curRetries current number of retries * @param maxRetries max number of retries allowed * @param ioe failure reason * @throws IOException if max number of retries is reached */ private void handleConnectionFailure(int curRetries, int maxRetries, IOException ioe) throws IOException { closeConnection(); // throw the exception if the maximum number of retries is reached if (curRetries >= maxRetries) { throw ioe; } // otherwise back off and retry try { Thread.sleep(failureSleep); } catch (InterruptedException ignored) { } LOG.info( "Retrying connect to server: " + remoteId.getAddress() + " after sleeping " + failureSleep + "ms. Already tried " + curRetries + " time(s)."); }
protected synchronized void setupConnection() throws IOException { short ioFailures = 0; short timeoutFailures = 0; while (true) { try { this.socket = socketFactory.createSocket(); this.socket.setTcpNoDelay(tcpNoDelay); this.socket.setKeepAlive(tcpKeepAlive); // connection time out is 20s NetUtils.connect(this.socket, remoteId.getAddress(), getSocketTimeout(conf)); if (remoteId.rpcTimeout > 0) { pingInterval = remoteId.rpcTimeout; // overwrite pingInterval } this.socket.setSoTimeout(pingInterval); return; } catch (SocketTimeoutException toe) { /* The max number of retries is 45, * which amounts to 20s*45 = 15 minutes retries. */ handleConnectionFailure(timeoutFailures++, maxRetries, toe); } catch (IOException ie) { handleConnectionFailure(ioFailures++, maxRetries, ie); } } }
/** * Connect to the server and set up the I/O streams. It then sends a header to the server and * starts the connection thread that waits for responses. * * @throws java.io.IOException e */ protected synchronized void setupIOstreams() throws IOException, InterruptedException { if (socket != null || shouldCloseConnection.get()) { return; } if (failedServers.isFailedServer(remoteId.getAddress())) { if (LOG.isDebugEnabled()) { LOG.debug( "Not trying to connect to " + remoteId.getAddress() + " this server is in the failed servers list"); } IOException e = new FailedServerException( "This server is in the failed servers list: " + remoteId.getAddress()); markClosed(e); close(); throw e; } try { if (LOG.isDebugEnabled()) { LOG.debug("Connecting to " + remoteId); } setupConnection(); this.in = new DataInputStream( new BufferedInputStream(new PingInputStream(NetUtils.getInputStream(socket)))); this.out = new DataOutputStream(new BufferedOutputStream(NetUtils.getOutputStream(socket))); writeHeader(); // update last activity time touch(); // start the receiver thread after the socket connection has been set up start(); } catch (IOException e) { failedServers.addToFailedServers(remoteId.address); markClosed(e); close(); throw e; } }
Connection(ConnectionId remoteId) throws IOException { if (remoteId.getAddress().isUnresolved()) { throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName()); } this.remoteId = remoteId; User ticket = remoteId.getTicket(); Class<? extends VersionedProtocol> protocol = remoteId.getProtocol(); header = new ConnectionHeader(protocol == null ? null : protocol.getName(), ticket); this.setName( "IPC Client (" + socketFactory.hashCode() + ") connection to " + remoteId.getAddress().toString() + ((ticket == null) ? " from an unknown user" : (" from " + ticket.getName()))); this.setDaemon(true); }
public InetSocketAddress getRemoteAddress() { return remoteId.getAddress(); }