.../java/org/apache/hadoop/hbase/ipc/IPCUtil.java | 24 +++++++++++ .../org/apache/hadoop/hbase/ipc/RpcServer.java | 49 ++++++++++++++++++---- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/IPCUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/IPCUtil.java index 734227c..f9abad6 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/IPCUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/IPCUtil.java @@ -309,4 +309,28 @@ public class IPCUtil { Preconditions.checkArgument(totalSize < Integer.MAX_VALUE); return totalSize; } + + /** + * Convert an int value and write it to CodedOutputStream. Big-endian. Same as + * what DataOutputStream.writeInt does. + * + * @param val the value to be written + * @param cos the CodedOutputStream to which the val has to be written + * @throws IOException + */ + public static void toBytes(int val, CodedOutputStream cos) throws IOException { + // Specifically done this way to avoid byte[] object creation + byte b1, b2, b3; + b3 = (byte) val; + val >>>= 8; + b2 = (byte) val; + val >>>= 8; + b1 = (byte) val; + val >>>= 8; + // Write back the bytes the way Bytes#toInt() does + cos.writeRawByte((byte) val); + cos.writeRawByte(b1); + cos.writeRawByte(b2); + cos.writeRawByte(b3); + } } 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 a9c64a3..5b20a8c 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 @@ -131,6 +131,7 @@ import org.codehaus.jackson.map.ObjectMapper; import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.protobuf.BlockingService; import com.google.protobuf.CodedInputStream; +import com.google.protobuf.CodedOutputStream; import com.google.protobuf.Descriptors.MethodDescriptor; import com.google.protobuf.Message; import com.google.protobuf.ServiceException; @@ -427,14 +428,10 @@ public class RpcServer implements RpcServerInterface, ConfigurationObserver { } Message header = headerBuilder.build(); - // Organize the response as a set of bytebuffers rather than collect it all together inside - // one big byte array; save on allocations. - ByteBuffer bbHeader = IPCUtil.getDelimitedMessageAsByteBuffer(header); - ByteBuffer bbResult = IPCUtil.getDelimitedMessageAsByteBuffer(result); - int totalSize = bbHeader.capacity() + (bbResult == null? 0: bbResult.limit()) + - (this.cellBlock == null? 0: this.cellBlock.limit()); - ByteBuffer bbTotalSize = ByteBuffer.wrap(Bytes.toBytes(totalSize)); - bc = new BufferChain(bbTotalSize, bbHeader, bbResult, this.cellBlock); + byte[] b = createHeaderAndMessageBytes(result, header); + + bc = new BufferChain(ByteBuffer.wrap(b), this.cellBlock); + if (connection.useWrap) { bc = wrapWithSasl(bc); } @@ -454,6 +451,42 @@ public class RpcServer implements RpcServerInterface, ConfigurationObserver { } } + private byte[] createHeaderAndMessageBytes(Message result, Message header) + throws IOException { + // Organize the response as a set of bytebuffers rather than collect it all together inside + // one big byte array; save on allocations. + int headerSerializedSize = 0, resultSerializedSize = 0, headerVintSize = 0, + resultVintSize = 0; + if (header != null) { + headerSerializedSize = header.getSerializedSize(); + headerVintSize = CodedOutputStream.computeRawVarint32Size(headerSerializedSize); + } + if (result != null) { + resultSerializedSize = result.getSerializedSize(); + resultVintSize = CodedOutputStream.computeRawVarint32Size(resultSerializedSize); + } + // calculate the total size + int totalSize = headerSerializedSize + headerVintSize + + (result == null ? 0 : (resultSerializedSize + resultVintSize)) + + (this.cellBlock == null ? 0 : this.cellBlock.limit()); + byte[] b = new byte[headerSerializedSize + headerVintSize + resultSerializedSize + + resultVintSize + Bytes.SIZEOF_INT]; + CodedOutputStream cos = CodedOutputStream.newInstance(b); + // The RpcClient expects the int to be in a format that code be decoded by + // the DataInputStream#readInt(). Hence going with the Bytes.toBytes(int) + // form of writing int. + IPCUtil.toBytes(totalSize, cos); + if (header != null) { + cos.writeMessageNoTag(header); + } + if (result != null) { + cos.writeMessageNoTag(result); + } + cos.flush(); + cos.checkNoSpaceLeft(); + return b; + } + private BufferChain wrapWithSasl(BufferChain bc) throws IOException { if (!this.connection.useSasl) return bc;