Search in sources :

Example 1 with DirectBufferOutputStream

use of io.streamnative.pulsar.handlers.kop.format.DirectBufferOutputStream in project starlight-for-kafka by datastax.

the class ByteBufUtils method decodePulsarEntryToKafkaRecords.

public static DecodeResult decodePulsarEntryToKafkaRecords(final MessageMetadata metadata, final ByteBuf payload, final long baseOffset, final byte magic) throws IOException {
    if (metadata.hasMarkerType()) {
        ControlRecordType controlRecordType;
        switch(metadata.getMarkerType()) {
            case MarkerType.TXN_COMMIT_VALUE:
                controlRecordType = ControlRecordType.COMMIT;
                break;
            case MarkerType.TXN_ABORT_VALUE:
                controlRecordType = ControlRecordType.ABORT;
                break;
            default:
                controlRecordType = ControlRecordType.UNKNOWN;
                break;
        }
        return DecodeResult.get(MemoryRecords.withEndTransactionMarker(baseOffset, metadata.getPublishTime(), 0, metadata.getTxnidMostBits(), (short) metadata.getTxnidLeastBits(), new EndTransactionMarker(controlRecordType, 0)));
    }
    long startConversionNanos = MathUtils.nowInNano();
    final int uncompressedSize = metadata.getUncompressedSize();
    final CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression());
    final ByteBuf uncompressedPayload = codec.decode(payload, uncompressedSize);
    final DirectBufferOutputStream directBufferOutputStream = new DirectBufferOutputStream(DEFAULT_BUFFER_SIZE);
    final MemoryRecordsBuilder builder = new MemoryRecordsBuilder(directBufferOutputStream, magic, CompressionType.NONE, TimestampType.CREATE_TIME, baseOffset, metadata.getPublishTime(), RecordBatch.NO_PRODUCER_ID, RecordBatch.NO_PRODUCER_EPOCH, RecordBatch.NO_SEQUENCE, metadata.hasTxnidMostBits() && metadata.hasTxnidLeastBits(), false, RecordBatch.NO_PARTITION_LEADER_EPOCH, MAX_RECORDS_BUFFER_SIZE);
    if (metadata.hasTxnidMostBits()) {
        builder.setProducerState(metadata.getTxnidMostBits(), (short) metadata.getTxnidLeastBits(), 0, true);
    }
    int conversionCount = 0;
    if (metadata.hasNumMessagesInBatch()) {
        final int numMessages = metadata.getNumMessagesInBatch();
        conversionCount += numMessages;
        for (int i = 0; i < numMessages; i++) {
            final SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
            final ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, i, numMessages);
            final long timestamp = (metadata.getEventTime() > 0) ? metadata.getEventTime() : metadata.getPublishTime();
            final ByteBuffer value = singleMessageMetadata.isNullValue() ? null : getNioBuffer(singleMessagePayload);
            if (magic >= RecordBatch.MAGIC_VALUE_V2) {
                final Header[] headers = getHeadersFromMetadata(singleMessageMetadata.getPropertiesList());
                builder.appendWithOffset(baseOffset + i, timestamp, getKeyByteBuffer(singleMessageMetadata), value, headers);
            } else {
                // record less than magic=2, no header attribute
                builder.appendWithOffset(baseOffset + i, timestamp, getKeyByteBuffer(singleMessageMetadata), value);
            }
            singleMessagePayload.release();
        }
    } else {
        conversionCount += 1;
        final long timestamp = (metadata.getEventTime() > 0) ? metadata.getEventTime() : metadata.getPublishTime();
        if (magic >= RecordBatch.MAGIC_VALUE_V2) {
            final Header[] headers = getHeadersFromMetadata(metadata.getPropertiesList());
            builder.appendWithOffset(baseOffset, timestamp, getKeyByteBuffer(metadata), getNioBuffer(uncompressedPayload), headers);
        } else {
            builder.appendWithOffset(baseOffset, timestamp, getKeyByteBuffer(metadata), getNioBuffer(uncompressedPayload));
        }
    }
    final MemoryRecords records = builder.build();
    uncompressedPayload.release();
    return DecodeResult.get(records, directBufferOutputStream.getByteBuf(), conversionCount, MathUtils.elapsedNanos(startConversionNanos));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DirectBufferOutputStream(io.streamnative.pulsar.handlers.kop.format.DirectBufferOutputStream) EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) ControlRecordType(org.apache.kafka.common.record.ControlRecordType) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Example 2 with DirectBufferOutputStream

use of io.streamnative.pulsar.handlers.kop.format.DirectBufferOutputStream in project kop by streamnative.

the class ByteBufUtils method decodePulsarEntryToKafkaRecords.

public static DecodeResult decodePulsarEntryToKafkaRecords(final MessageMetadata metadata, final ByteBuf payload, final long baseOffset, final byte magic) throws IOException {
    if (metadata.hasMarkerType()) {
        ControlRecordType controlRecordType;
        switch(metadata.getMarkerType()) {
            case MarkerType.TXN_COMMIT_VALUE:
                controlRecordType = ControlRecordType.COMMIT;
                break;
            case MarkerType.TXN_ABORT_VALUE:
                controlRecordType = ControlRecordType.ABORT;
                break;
            default:
                controlRecordType = ControlRecordType.UNKNOWN;
                break;
        }
        return DecodeResult.get(MemoryRecords.withEndTransactionMarker(baseOffset, metadata.getPublishTime(), 0, metadata.getTxnidMostBits(), (short) metadata.getTxnidLeastBits(), new EndTransactionMarker(controlRecordType, 0)));
    }
    long startConversionNanos = MathUtils.nowInNano();
    final int uncompressedSize = metadata.getUncompressedSize();
    final CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression());
    final ByteBuf uncompressedPayload = codec.decode(payload, uncompressedSize);
    final DirectBufferOutputStream directBufferOutputStream = new DirectBufferOutputStream(DEFAULT_BUFFER_SIZE);
    final MemoryRecordsBuilder builder = new MemoryRecordsBuilder(directBufferOutputStream, magic, CompressionType.NONE, TimestampType.CREATE_TIME, baseOffset, metadata.getPublishTime(), RecordBatch.NO_PRODUCER_ID, RecordBatch.NO_PRODUCER_EPOCH, RecordBatch.NO_SEQUENCE, metadata.hasTxnidMostBits() && metadata.hasTxnidLeastBits(), false, RecordBatch.NO_PARTITION_LEADER_EPOCH, MAX_RECORDS_BUFFER_SIZE);
    if (metadata.hasTxnidMostBits()) {
        builder.setProducerState(metadata.getTxnidMostBits(), (short) metadata.getTxnidLeastBits(), 0, true);
    }
    int conversionCount = 0;
    if (metadata.hasNumMessagesInBatch()) {
        final int numMessages = metadata.getNumMessagesInBatch();
        conversionCount += numMessages;
        for (int i = 0; i < numMessages; i++) {
            final SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata();
            final ByteBuf singleMessagePayload = Commands.deSerializeSingleMessageInBatch(uncompressedPayload, singleMessageMetadata, i, numMessages);
            final long timestamp = (metadata.getEventTime() > 0) ? metadata.getEventTime() : metadata.getPublishTime();
            final ByteBuffer value = singleMessageMetadata.isNullValue() ? null : getNioBuffer(singleMessagePayload);
            if (magic >= RecordBatch.MAGIC_VALUE_V2) {
                final Header[] headers = getHeadersFromMetadata(singleMessageMetadata.getPropertiesList());
                builder.appendWithOffset(baseOffset + i, timestamp, getKeyByteBuffer(singleMessageMetadata), value, headers);
            } else {
                // record less than magic=2, no header attribute
                builder.appendWithOffset(baseOffset + i, timestamp, getKeyByteBuffer(singleMessageMetadata), value);
            }
            singleMessagePayload.release();
        }
    } else {
        conversionCount += 1;
        final long timestamp = (metadata.getEventTime() > 0) ? metadata.getEventTime() : metadata.getPublishTime();
        if (magic >= RecordBatch.MAGIC_VALUE_V2) {
            final Header[] headers = getHeadersFromMetadata(metadata.getPropertiesList());
            builder.appendWithOffset(baseOffset, timestamp, getKeyByteBuffer(metadata), getNioBuffer(uncompressedPayload), headers);
        } else {
            builder.appendWithOffset(baseOffset, timestamp, getKeyByteBuffer(metadata), getNioBuffer(uncompressedPayload));
        }
    }
    final MemoryRecords records = builder.build();
    uncompressedPayload.release();
    return DecodeResult.get(records, directBufferOutputStream.getByteBuf(), conversionCount, MathUtils.elapsedNanos(startConversionNanos));
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) DirectBufferOutputStream(io.streamnative.pulsar.handlers.kop.format.DirectBufferOutputStream) EndTransactionMarker(org.apache.kafka.common.record.EndTransactionMarker) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) SingleMessageMetadata(org.apache.pulsar.common.api.proto.SingleMessageMetadata) MemoryRecordsBuilder(org.apache.kafka.common.record.MemoryRecordsBuilder) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) ControlRecordType(org.apache.kafka.common.record.ControlRecordType) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)2 DirectBufferOutputStream (io.streamnative.pulsar.handlers.kop.format.DirectBufferOutputStream)2 ByteBuffer (java.nio.ByteBuffer)2 Header (org.apache.kafka.common.header.Header)2 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)2 ControlRecordType (org.apache.kafka.common.record.ControlRecordType)2 EndTransactionMarker (org.apache.kafka.common.record.EndTransactionMarker)2 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)2 MemoryRecordsBuilder (org.apache.kafka.common.record.MemoryRecordsBuilder)2 SingleMessageMetadata (org.apache.pulsar.common.api.proto.SingleMessageMetadata)2 CompressionCodec (org.apache.pulsar.common.compression.CompressionCodec)2