private <R> ChannelBuffer encode(final KuduRpc<R> rpc) { final int rpcid = this.rpcid.incrementAndGet(); ChannelBuffer payload; final String service = rpc.serviceName(); final String method = rpc.method(); try { final RpcHeader.RequestHeader.Builder headerBuilder = RpcHeader.RequestHeader.newBuilder() .setCallId(rpcid) .addAllRequiredFeatureFlags(rpc.getRequiredFeatures()) .setRemoteMethod( RpcHeader.RemoteMethodPB.newBuilder() .setServiceName(service) .setMethodName(method)); // If any timeout is set, find the lowest non-zero one, since this will be the deadline that // the server must respect. if (rpc.deadlineTracker.hasDeadline() || socketReadTimeoutMs > 0) { long millisBeforeDeadline = Long.MAX_VALUE; if (rpc.deadlineTracker.hasDeadline()) { millisBeforeDeadline = rpc.deadlineTracker.getMillisBeforeDeadline(); } long localRpcTimeoutMs = Long.MAX_VALUE; if (socketReadTimeoutMs > 0) { localRpcTimeoutMs = socketReadTimeoutMs; } headerBuilder.setTimeoutMillis((int) Math.min(millisBeforeDeadline, localRpcTimeoutMs)); } payload = rpc.serialize(headerBuilder.build()); } catch (Exception e) { LOG.error("Uncaught exception while serializing RPC: " + rpc, e); rpc.errback(e); // Make the RPC fail with the exception. return null; } final KuduRpc<?> oldrpc = rpcs_inflight.put(rpcid, rpc); if (oldrpc != null) { final String wtf = getPeerUuidLoggingString() + "WTF? There was already an RPC in flight with" + " rpcid=" + rpcid + ": " + oldrpc + ". This happened when sending out: " + rpc; LOG.error(wtf); // Make it fail. This isn't an expected failure mode. oldrpc.errback(new NonRecoverableException(wtf)); } if (LOG.isDebugEnabled()) { LOG.debug( getPeerUuidLoggingString() + chan + " Sending RPC #" + rpcid + ", payload=" + payload + ' ' + Bytes.pretty(payload)); } payload = secureRpcHelper.wrap(payload); return payload; }