final short MAX_RETRIES = 5; while (true) { setupConnection(); InputStream inStream = NetUtils.getInputStream(socket); writeConnectionHeaderPreamble(outStream); if (useSasl) { final InputStream in2 = inStream; final OutputStream out2 = outStream; UserGroupInformation ticket = getUGI(); boolean continueSasl; if (ticket == null) { } catch (Exception ex) { ExceptionUtil.rethrowIfInterrupt(ex); handleSaslConnectionFailure(numRetries++, MAX_RETRIES, ex, ticket); continue; this.out = new DataOutputStream(new BufferedOutputStream(outStream)); writeConnectionHeader(); processResponseForConnectionHeader(); closeSocket(); IOException e = ExceptionUtil.asInterrupt(t); if (e == null) {
/** * Creates a connection. Can be overridden by a subclass for testing. * @param remoteId - the ConnectionId to use for the connection creation. */ @Override protected BlockingRpcConnection createConnection(ConnectionId remoteId) throws IOException { return new BlockingRpcConnection(this, remoteId); }
setupIOstreams(); closeConn(e); return;
BlockingRpcConnection(BlockingRpcClient rpcClient, ConnectionId remoteId) throws IOException { super(rpcClient.conf, AbstractRpcClient.WHEEL_TIMER, remoteId, rpcClient.clusterId, rpcClient.userProvider.isHBaseSecurityEnabled(), rpcClient.codec, rpcClient.compressor); this.rpcClient = rpcClient; if (remoteId.getAddress().isUnresolved()) { throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName()); } this.connectionHeaderPreamble = getConnectionHeaderPreamble(); ConnectionHeader header = getConnectionHeader(); ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + header.getSerializedSize()); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(header.getSerializedSize()); header.writeTo(dos); assert baos.size() == 4 + header.getSerializedSize(); this.connectionHeaderWithLength = baos.getBuffer(); UserGroupInformation ticket = remoteId.ticket.getUGI(); this.threadName = "IPC Client (" + this.rpcClient.socketFactory.hashCode() + ") connection to " + remoteId.getAddress().toString() + ((ticket == null) ? " from an unknown user" : (" from " + ticket.getUserName())); if (this.rpcClient.conf.getBoolean(BlockingRpcClient.SPECIFIC_WRITE_THREAD, false)) { callSender = new CallSender(threadName, this.rpcClient.conf); callSender.start(); } else { callSender = null; } }
@Override public synchronized void shutdown() { closed = true; if (callSender != null) { callSender.interrupt(); } closeConn(new IOException("connection to " + remoteId.address + " closed")); }
final Exception ex, final UserGroupInformation user) throws IOException, InterruptedException { closeSocket(); user.doAs(new PrivilegedExceptionAction<Object>() { @Override
@Override protected synchronized void setupConnection() throws IOException { super.setupConnection(); synchronized (savedSockets) { savedSockets.add(socket); } throw new IOException("Sample exception for " + "verifying socket closure in case of exceptions."); } };
private void processResponseForConnectionHeader() throws IOException { // if no response excepted, return if (!waitingConnectionHeaderResponse) return; try { // read the ConnectionHeaderResponse from server int len = this.in.readInt(); byte[] buff = new byte[len]; int readSize = this.in.read(buff); if (LOG.isDebugEnabled()) { LOG.debug("Length of response for connection header:" + readSize); } RPCProtos.ConnectionHeaderResponse connectionHeaderResponse = RPCProtos.ConnectionHeaderResponse.parseFrom(buff); // Get the CryptoCipherMeta, update the HBaseSaslRpcClient for Crypto Cipher if (connectionHeaderResponse.hasCryptoCipherMeta()) { negotiateCryptoAes(connectionHeaderResponse.getCryptoCipherMeta()); } waitingConnectionHeaderResponse = false; } catch (SocketTimeoutException ste) { LOG.error(HBaseMarkers.FATAL, "Can't get the connection header response for rpc timeout, " + "please check if server has the correct configuration to support the additional " + "function.", ste); // timeout when waiting the connection header response, ignore the additional function throw new IOException("Timeout while waiting connection header response", ste); } }
StringUtils.stringifyException(toe)); handleConnectionFailure(timeoutFailures++, this.rpcClient.maxRetries, toe); } catch (IOException ie) { if (LOG.isDebugEnabled()) { StringUtils.stringifyException(ie)); handleConnectionFailure(ioFailures++, this.rpcClient.maxRetries, ie);
BlockingRpcConnection(BlockingRpcClient rpcClient, ConnectionId remoteId) throws IOException { super(rpcClient.conf, AbstractRpcClient.WHEEL_TIMER, remoteId, rpcClient.clusterId, rpcClient.userProvider.isHBaseSecurityEnabled(), rpcClient.codec, rpcClient.compressor); this.rpcClient = rpcClient; if (remoteId.getAddress().isUnresolved()) { throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName()); } this.connectionHeaderPreamble = getConnectionHeaderPreamble(); ConnectionHeader header = getConnectionHeader(); ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + header.getSerializedSize()); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(header.getSerializedSize()); header.writeTo(dos); assert baos.size() == 4 + header.getSerializedSize(); this.connectionHeaderWithLength = baos.getBuffer(); UserGroupInformation ticket = remoteId.ticket.getUGI(); this.threadName = "IPC Client (" + this.rpcClient.socketFactory.hashCode() + ") connection to " + remoteId.getAddress().toString() + ((ticket == null) ? " from an unknown user" : (" from " + ticket.getUserName())); if (this.rpcClient.conf.getBoolean(BlockingRpcClient.SPECIFIC_WRITE_THREAD, false)) { callSender = new CallSender(threadName, this.rpcClient.conf); callSender.start(); } else { callSender = null; } }
private synchronized boolean waitForWork() { // beware of the concurrent access to the calls list: we can add calls, but as well // remove them. long waitUntil = EnvironmentEdgeManager.currentTime() + this.rpcClient.minIdleTimeBeforeClose; for (;;) { if (thread == null) { return false; } if (!calls.isEmpty()) { return true; } if (EnvironmentEdgeManager.currentTime() >= waitUntil) { closeConn( new IOException("idle connection closed with " + calls.size() + " pending request(s)")); return false; } try { wait(Math.min(this.rpcClient.minIdleTimeBeforeClose, 1000)); } catch (InterruptedException e) { } } }
private void closeConn(IOException e) { if (thread == null) { return; } thread.interrupt(); thread = null; closeSocket(); if (callSender != null) { callSender.cleanup(e); } for (Call call : calls.values()) { call.setException(e); } calls.clear(); }
@Override protected synchronized void setupConnection() throws IOException { super.setupConnection(); synchronized (savedSockets) { savedSockets.add(socket); } throw new IOException("Sample exception for " + "verifying socket closure in case of exceptions."); } };
private void processResponseForConnectionHeader() throws IOException { // if no response excepted, return if (!waitingConnectionHeaderResponse) return; try { // read the ConnectionHeaderResponse from server int len = this.in.readInt(); byte[] buff = new byte[len]; int readSize = this.in.read(buff); if (LOG.isDebugEnabled()) { LOG.debug("Length of response for connection header:" + readSize); } RPCProtos.ConnectionHeaderResponse connectionHeaderResponse = RPCProtos.ConnectionHeaderResponse.parseFrom(buff); // Get the CryptoCipherMeta, update the HBaseSaslRpcClient for Crypto Cipher if (connectionHeaderResponse.hasCryptoCipherMeta()) { negotiateCryptoAes(connectionHeaderResponse.getCryptoCipherMeta()); } waitingConnectionHeaderResponse = false; } catch (SocketTimeoutException ste) { LOG.error(HBaseMarkers.FATAL, "Can't get the connection header response for rpc timeout, " + "please check if server has the correct configuration to support the additional " + "function.", ste); // timeout when waiting the connection header response, ignore the additional function throw new IOException("Timeout while waiting connection header response", ste); } }
StringUtils.stringifyException(toe)); handleConnectionFailure(timeoutFailures++, this.rpcClient.maxRetries, toe); } catch (IOException ie) { if (LOG.isDebugEnabled()) { StringUtils.stringifyException(ie)); handleConnectionFailure(ioFailures++, this.rpcClient.maxRetries, ie);
final short MAX_RETRIES = 5; while (true) { setupConnection(); InputStream inStream = NetUtils.getInputStream(socket); writeConnectionHeaderPreamble(outStream); if (useSasl) { final InputStream in2 = inStream; final OutputStream out2 = outStream; UserGroupInformation ticket = getUGI(); boolean continueSasl; if (ticket == null) { } catch (Exception ex) { ExceptionUtil.rethrowIfInterrupt(ex); handleSaslConnectionFailure(numRetries++, MAX_RETRIES, ex, ticket); continue; this.out = new DataOutputStream(new BufferedOutputStream(outStream)); writeConnectionHeader(); processResponseForConnectionHeader(); closeSocket(); IOException e = ExceptionUtil.asInterrupt(t); if (e == null) {
BlockingRpcConnection(BlockingRpcClient rpcClient, ConnectionId remoteId) throws IOException { super(rpcClient.conf, AbstractRpcClient.WHEEL_TIMER, remoteId, rpcClient.clusterId, rpcClient.userProvider.isHBaseSecurityEnabled(), rpcClient.codec, rpcClient.compressor); this.rpcClient = rpcClient; if (remoteId.getAddress().isUnresolved()) { throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName()); } this.connectionHeaderPreamble = getConnectionHeaderPreamble(); ConnectionHeader header = getConnectionHeader(); ByteArrayOutputStream baos = new ByteArrayOutputStream(4 + header.getSerializedSize()); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(header.getSerializedSize()); header.writeTo(dos); assert baos.size() == 4 + header.getSerializedSize(); this.connectionHeaderWithLength = baos.getBuffer(); UserGroupInformation ticket = remoteId.ticket.getUGI(); this.threadName = "IPC Client (" + this.rpcClient.socketFactory.hashCode() + ") connection to " + remoteId.getAddress().toString() + ((ticket == null) ? " from an unknown user" : (" from " + ticket.getUserName())); if (this.rpcClient.conf.getBoolean(BlockingRpcClient.SPECIFIC_WRITE_THREAD, false)) { callSender = new CallSender(threadName, this.rpcClient.conf); callSender.start(); } else { callSender = null; } }
setupIOstreams(); closeConn(e); return;
/** * Handle connection failures 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. 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 { closeSocket(); // throw the exception if the maximum number of retries is reached if (curRetries >= maxRetries || ExceptionUtil.isInterrupt(ioe)) { throw ioe; } // otherwise back off and retry try { Thread.sleep(this.rpcClient.failureSleep); } catch (InterruptedException ie) { ExceptionUtil.rethrowIfInterrupt(ie); } if (LOG.isInfoEnabled()) { LOG.info("Retrying connect to server: " + remoteId.getAddress() + " after sleeping " + this.rpcClient.failureSleep + "ms. Already tried " + curRetries + " time(s)."); } }