private static ByteBufPair serializeCommandMessageWithSize(BaseCommand cmd, ByteBuf metadataAndPayload) { // / Wire format // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD] // // metadataAndPayload contains from magic-number to the payload included int cmdSize = cmd.getSerializedSize(); int totalSize = 4 + cmdSize + metadataAndPayload.readableBytes(); int headersSize = 4 + 4 + cmdSize; ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize); headers.writeInt(totalSize); // External frame try { // Write cmd headers.writeInt(cmdSize); ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers); cmd.writeTo(outStream); outStream.recycle(); } catch (IOException e) { // This is in-memory serialization, should not fail throw new RuntimeException(e); } return (ByteBufPair) ByteBufPair.get(headers, metadataAndPayload); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof ByteBufPair) { ByteBufPair b = (ByteBufPair) msg; // Some handlers in the pipeline will modify the bytebufs passed in to them (i.e. SslHandler). // For these handlers, we need to pass a copy of the buffers as the source buffers may be cached // for multiple requests. try { ctx.write(b.getFirst().copy(), ctx.voidPromise()); ctx.write(b.getSecond().copy(), promise); } finally { ReferenceCountUtil.safeRelease(b); } } else { ctx.write(msg, promise); } } }
int totalMsgBufSize = op.cmd.readableBytes(); ByteBufPair msg = op.cmd; if (msg != null) { ByteBuf headerFrame = msg.getFirst(); headerFrame.markReaderIndex(); try {
public ByteBuf getMessageWithMetadata(byte[] data) throws IOException { MessageMetadata messageData = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()) .setProducerName("prod-name").setSequenceId(0).build(); ByteBuf payload = Unpooled.wrappedBuffer(data, 0, data.length); int msgMetadataSize = messageData.getSerializedSize(); int headersSize = 4 + msgMetadataSize; ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize, headersSize); ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers); headers.writeInt(msgMetadataSize); messageData.writeTo(outStream); outStream.recycle(); return ByteBufPair.coalesce(ByteBufPair.get(headers, payload)); }
/** * @return a single buffer with the content of both individual buffers */ @VisibleForTesting public static ByteBuf coalesce(ByteBufPair pair) { ByteBuf b = Unpooled.buffer(pair.readableBytes()); b.writeBytes(pair.b1, pair.b1.readerIndex(), pair.b1.readableBytes()); b.writeBytes(pair.b2, pair.b2.readerIndex(), pair.b2.readableBytes()); return b; }
stripChecksum(op); op.cmd.retain(); if (log.isDebugEnabled()) { log.debug("[{}] [{}] Re-Sending message in cnx {}, sequenceId {}", topic, producerName,
@Override protected ByteBufPair newObject(Recycler.Handle<ByteBufPair> handle) { return new ByteBufPair(handle); } };
/** * Get a new {@link ByteBufPair} from the pool and assign 2 buffers to it. * <p> * The buffers b1 and b2 lifecycles are now managed by the ByteBufPair: when the {@link ByteBufPair} is deallocated, * b1 and b2 will be released as well. * * @param b1 * @param b2 * @return */ public static ByteBufPair get(ByteBuf b1, ByteBuf b2) { ByteBufPair buf = RECYCLER.get(); buf.setRefCnt(1); buf.b1 = b1; buf.b2 = b2; return buf; }
/** * @return a single buffer with the content of both individual buffers */ @VisibleForTesting public static ByteBuf coalesce(ByteBufPair pair) { ByteBuf b = Unpooled.buffer(pair.readableBytes()); b.writeBytes(pair.b1, pair.b1.readerIndex(), pair.b1.readableBytes()); b.writeBytes(pair.b2, pair.b2.readerIndex(), pair.b2.readableBytes()); return b; }
cmd.retain(); cnx().ctx().channel().eventLoop().execute(WriteInEventLoopCallback.create(this, cnx(), op)); stats.updateNumMsgsSent(numMessagesInBatch, op.batchSizeByte);
@Override protected ByteBufPair newObject(Recycler.Handle<ByteBufPair> handle) { return new ByteBufPair(handle); } };
/** * Get a new {@link ByteBufPair} from the pool and assign 2 buffers to it. * <p> * The buffers b1 and b2 lifecycles are now managed by the ByteBufPair: when the {@link ByteBufPair} is deallocated, * b1 and b2 will be released as well. * * @param b1 * @param b2 * @return */ public static ByteBufPair get(ByteBuf b1, ByteBuf b2) { ByteBufPair buf = RECYCLER.get(); buf.setRefCnt(1); buf.b1 = b1; buf.b2 = b2; return buf; }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof ByteBufPair) { ByteBufPair b = (ByteBufPair) msg; // Write each buffer individually on the socket. The retain() here is needed to preserve the fact that // ByteBuf are automatically released after a write. If the ByteBufPair ref count is increased and it // gets written multiple times, the individual buffers refcount should be reflected as well. try { ctx.write(b.getFirst().retainedDuplicate(), ctx.voidPromise()); ctx.write(b.getSecond().retainedDuplicate(), promise); } finally { ReferenceCountUtil.safeRelease(b); } } else { ctx.write(msg, promise); } } }
int totalMsgBufSize = op.cmd.readableBytes(); ByteBufPair msg = op.cmd; if (msg != null) { ByteBuf headerFrame = msg.getFirst(); headerFrame.markReaderIndex(); try {
ByteBufPair command = ByteBufPair.get(headers, payload);
cmd.retain(); cnx.ctx().channel().eventLoop().execute(WriteInEventLoopCallback.create(this, cnx, op)); stats.updateNumMsgsSent(op.numMessagesInBatch, op.batchSizeByte);
private static ByteBufPair serializeCommandMessageWithSize(BaseCommand cmd, ByteBuf metadataAndPayload) { // / Wire format // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD] // // metadataAndPayload contains from magic-number to the payload included int cmdSize = cmd.getSerializedSize(); int totalSize = 4 + cmdSize + metadataAndPayload.readableBytes(); int headersSize = 4 + 4 + cmdSize; ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize); headers.writeInt(totalSize); // External frame try { // Write cmd headers.writeInt(cmdSize); ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers); cmd.writeTo(outStream); outStream.recycle(); } catch (IOException e) { // This is in-memory serialization, should not fail throw new RuntimeException(e); } return (ByteBufPair) ByteBufPair.get(headers, metadataAndPayload); }