Search in sources :

Example 1 with DoubleByteBuf

use of com.yahoo.pulsar.common.api.DoubleByteBuf in project pulsar by yahoo.

the class ProducerImpl method verifyLocalBufferIsNotCorrupted.

/**
     * Computes checksum again and verifies it against existing checksum. If checksum doesn't match it means that
     * message is corrupt.
     * 
     * @param op
     * @return returns true only if message is not modified and computed-checksum is same as previous checksum else
     *         return false that means that message is corrupted. Returns true if checksum is not present.
     */
protected boolean verifyLocalBufferIsNotCorrupted(OpSendMsg op) {
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            // skip bytes up to checksum index
            // skip [total-size]
            headerFrame.skipBytes(4);
            int cmdSize = (int) headerFrame.readUnsignedInt();
            headerFrame.skipBytes(cmdSize);
            // verify if checksum present
            if (hasChecksum(headerFrame)) {
                int checksum = readChecksum(headerFrame).intValue();
                // msg.readerIndex is already at header-payload index, Recompute checksum for headers-payload
                int metadataChecksum = computeChecksum(headerFrame);
                long computedChecksum = resumeChecksum(metadataChecksum, msg.getSecond());
                return checksum == computedChecksum;
            } else {
                log.warn("[{}] [{}] checksum is not present into message with id {}", topic, producerName, op.sequenceId);
            }
        } finally {
            headerFrame.resetReaderIndex();
            msg.resetReaderIndex();
        }
        return true;
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
        return false;
    }
}
Also used : DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf) ByteBuf(io.netty.buffer.ByteBuf) DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf)

Example 2 with DoubleByteBuf

use of com.yahoo.pulsar.common.api.DoubleByteBuf in project pulsar by yahoo.

the class ProducerImpl method stripChecksum.

/**
     * Strips checksum from {@link OpSendMsg} command if present else ignore it.
     * 
     * @param op
     */
private void stripChecksum(OpSendMsg op) {
    op.cmd.markReaderIndex();
    int totalMsgBufSize = op.cmd.readableBytes();
    DoubleByteBuf msg = getDoubleByteBuf(op.cmd);
    if (msg != null) {
        ByteBuf headerFrame = msg.getFirst();
        msg.markReaderIndex();
        headerFrame.markReaderIndex();
        try {
            // skip [total-size]
            headerFrame.skipBytes(4);
            int cmdSize = (int) headerFrame.readUnsignedInt();
            // verify if checksum present
            headerFrame.skipBytes(cmdSize);
            if (!hasChecksum(headerFrame)) {
                headerFrame.resetReaderIndex();
                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);
            headerFrame.resetReaderIndex();
        } finally {
            op.cmd.resetReaderIndex();
        }
    } else {
        log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName());
    }
}
Also used : DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf) ByteBuf(io.netty.buffer.ByteBuf) DoubleByteBuf(com.yahoo.pulsar.common.api.DoubleByteBuf)

Aggregations

DoubleByteBuf (com.yahoo.pulsar.common.api.DoubleByteBuf)2 ByteBuf (io.netty.buffer.ByteBuf)2