diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java index 8cead2a..06c219a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/ipc/RpcServer.java @@ -1579,21 +1579,23 @@ public class RpcServer implements RpcServerInterface { } } + dataLengthBuffer.flip(); + int dataLength = dataLengthBuffer.getInt(); + if (dataLength == RpcClient.PING_CALL_ID) { + if (!useWrap) { //covers the !useSasl too + dataLengthBuffer.clear(); + return 0; //ping message + } + } + if (dataLength < 0) { // A data length of zero is legal. + throw new IllegalArgumentException("Unexpected data length " + + dataLength + "!! from " + getHostAddress()); + } + // We have read a length and we have read the preamble. It is either the connection header // or it is a request. - if (data == null) { - dataLengthBuffer.flip(); - int dataLength = dataLengthBuffer.getInt(); - if (dataLength == RpcClient.PING_CALL_ID) { - if (!useWrap) { //covers the !useSasl too - dataLengthBuffer.clear(); - return 0; //ping message - } - } - if (dataLength < 0) { // A data length of zero is legal. - throw new IllegalArgumentException("Unexpected data length " - + dataLength + "!! from " + getHostAddress()); - } + // We will reuse buffer of data unless data.capacity is not enough. + if (data == null || (data != null && data.capacity() < dataLength)) { data = ByteBuffer.allocate(dataLength); // Increment the rpc count. This counter will be decreased when we write @@ -1630,7 +1632,10 @@ public class RpcServer implements RpcServerInterface { } finally { dataLengthBuffer.clear(); // Clean for the next call - data = null; // For the GC + + // If buffer is larger we free it to save memory otherwise try to reuse it. + if (data.capacity() >= NIO_BUFFER_LIMIT / 2) data = null; + else data.clear(); } }