Search in sources :

Example 6 with ByteBufPair

use of org.apache.pulsar.common.api.ByteBufPair in project incubator-pulsar by apache.

the class CommandsTest method computeChecksum.

private int computeChecksum(MessageMetadata msgMetadata, ByteBuf compressedPayload) throws IOException {
    int metadataSize = msgMetadata.getSerializedSize();
    int metadataFrameSize = 4 + metadataSize;
    ByteBuf metaPayloadFrame = PooledByteBufAllocator.DEFAULT.buffer(metadataFrameSize, metadataFrameSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(metaPayloadFrame);
    metaPayloadFrame.writeInt(metadataSize);
    msgMetadata.writeTo(outStream);
    ByteBuf payload = compressedPayload.copy();
    ByteBufPair metaPayloadBuf = ByteBufPair.get(metaPayloadFrame, payload);
    int computedChecksum = Crc32cIntChecksum.computeChecksum(ByteBufPair.coalesce(metaPayloadBuf));
    outStream.recycle();
    metaPayloadBuf.release();
    return computedChecksum;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair)

Example 7 with ByteBufPair

use of org.apache.pulsar.common.api.ByteBufPair in project incubator-pulsar by apache.

the class ProducerImpl method stripChecksum.

/**
 * Strips checksum from {@link OpSendMsg} command if present else ignore it.
 *
 * @param op
 */
private void stripChecksum(OpSendMsg op) {
    int totalMsgBufSize = op.cmd.readableBytes();
    ByteBufPair msg = op.cmd;
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        headerFrame.markReaderIndex();
        try {
            // skip [total-size]
            headerFrame.skipBytes(4);
            int cmdSize = (int) headerFrame.readUnsignedInt();
            // verify if checksum present
            headerFrame.skipBytes(cmdSize);
            if (!hasChecksum(headerFrame)) {
                return;
            }
            // [total-size] [cmd-length] [cmd-size]
            int headerSize = 4 + 4 + cmdSize;
            // [magic-number] [checksum-size]
            int checksumSize = 4 + 2;
            // [header-size] [checksum-size]
            int checksumMark = (headerSize + checksumSize);
            // metadataPayload = totalSize - checksumMark
            int metaPayloadSize = (totalMsgBufSize - checksumMark);
            // new total-size without checksum
            int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize;
            headerFrame.resetReaderIndex();
            int headerFrameSize = headerFrame.readableBytes();
            // rewrite new [total-size]
            headerFrame.setInt(0, newTotalFrameSizeLength);
            // sliced only
            ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark);
            // metadata
            // set headerFrame write-index to overwrite metadata over checksum
            headerFrame.writerIndex(headerSize);
            metadata.readBytes(headerFrame, metadata.readableBytes());
            // reduce capacity by removed checksum bytes
            headerFrame.capacity(headerFrameSize - checksumSize);
        } finally {
            headerFrame.resetReaderIndex();
        }
    } else {
        log.warn("[{}] Failed while casting {} into ByteBufPair", producerName, op.cmd.getClass().getName());
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)7 ByteBufPair (org.apache.pulsar.common.api.ByteBufPair)7 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)3 Test (org.testng.annotations.Test)2 Field (java.lang.reflect.Field)1 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)1 MessageBuilder (org.apache.pulsar.client.api.MessageBuilder)1 MessageId (org.apache.pulsar.client.api.MessageId)1 OpSendMsg (org.apache.pulsar.client.impl.ProducerImpl.OpSendMsg)1 ClientConfigurationData (org.apache.pulsar.client.impl.conf.ClientConfigurationData)1 Builder (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata.Builder)1 ByteBufCodedOutputStream (org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)1