Search in sources :

Example 1 with ByteBufCodedInputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream in project pulsar by yahoo.

the class MessageIdImpl method fromByteArray.

// / Serialization
public static MessageId fromByteArray(byte[] data) throws IOException {
    checkNotNull(data);
    ByteBufCodedInputStream inputStream = ByteBufCodedInputStream.get(Unpooled.wrappedBuffer(data, 0, data.length));
    PulsarApi.MessageIdData.Builder builder = PulsarApi.MessageIdData.newBuilder();
    PulsarApi.MessageIdData idData;
    try {
        idData = builder.mergeFrom(inputStream, null).build();
    } catch (UninitializedMessageException e) {
        throw new IOException(e);
    }
    MessageIdImpl messageId;
    if (idData.hasBatchIndex()) {
        messageId = new BatchMessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition(), idData.getBatchIndex());
    } else {
        messageId = new MessageIdImpl(idData.getLedgerId(), idData.getEntryId(), idData.getPartition());
    }
    inputStream.recycle();
    builder.recycle();
    idData.recycle();
    return messageId;
}
Also used : UninitializedMessageException(com.google.protobuf.UninitializedMessageException) MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteBufCodedInputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream) PulsarApi(com.yahoo.pulsar.common.api.proto.PulsarApi) IOException(java.io.IOException)

Example 2 with ByteBufCodedInputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream in project pulsar by yahoo.

the class Commands method parseMessageMetadata.

public static MessageMetadata parseMessageMetadata(ByteBuf buffer) {
    try {
        // initially reader-index may point to start_of_checksum : increment reader-index to start_of_metadata to parse
        // metadata
        readChecksum(buffer);
        int metadataSize = (int) buffer.readUnsignedInt();
        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + metadataSize);
        ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
        MessageMetadata.Builder messageMetadataBuilder = MessageMetadata.newBuilder();
        MessageMetadata res = messageMetadataBuilder.mergeFrom(stream, null).build();
        buffer.writerIndex(writerIndex);
        messageMetadataBuilder.recycle();
        stream.recycle();
        return res;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : MessageMetadata(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata) ByteBufCodedInputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream) IOException(java.io.IOException)

Example 3 with ByteBufCodedInputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream in project pulsar by yahoo.

the class Commands method deSerializeSingleMessageInBatch.

public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload, PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize) throws IOException {
    int singleMetaSize = (int) uncompressedPayload.readUnsignedInt();
    int writerIndex = uncompressedPayload.writerIndex();
    int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize;
    uncompressedPayload.writerIndex(beginIndex);
    ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload);
    PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null).build();
    int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize();
    uncompressedPayload.markReaderIndex();
    ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(), singleMessagePayloadSize);
    singleMessagePayload.retain();
    uncompressedPayload.writerIndex(writerIndex);
    uncompressedPayload.resetReaderIndex();
    // reader now points to beginning of payload read; so move it past message payload just read
    if (index < batchSize) {
        uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize);
    }
    return singleMessagePayload;
}
Also used : ByteBufCodedInputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream) PulsarApi(com.yahoo.pulsar.common.api.proto.PulsarApi) UnpooledHeapByteBuf(io.netty.buffer.UnpooledHeapByteBuf) RecyclableDuplicateByteBuf(io.netty.buffer.RecyclableDuplicateByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with ByteBufCodedInputStream

use of com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream in project pulsar by yahoo.

the class PulsarDecoder method channelRead.

@Override
public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Get a buffer that contains the full frame
    ByteBuf buffer = (ByteBuf) msg;
    BaseCommand cmd = null;
    BaseCommand.Builder cmdBuilder = null;
    try {
        // De-serialize the command
        int cmdSize = (int) buffer.readUnsignedInt();
        int writerIndex = buffer.writerIndex();
        buffer.writerIndex(buffer.readerIndex() + cmdSize);
        ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer);
        cmdBuilder = BaseCommand.newBuilder();
        cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build();
        buffer.writerIndex(writerIndex);
        cmdInputStream.recycle();
        if (log.isDebugEnabled()) {
            log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType());
        }
        messageReceived();
        switch(cmd.getType()) {
            case PARTITIONED_METADATA:
                checkArgument(cmd.hasPartitionMetadata());
                handlePartitionMetadataRequest(cmd.getPartitionMetadata());
                cmd.getPartitionMetadata().recycle();
                break;
            case PARTITIONED_METADATA_RESPONSE:
                checkArgument(cmd.hasPartitionMetadataResponse());
                handlePartitionResponse(cmd.getPartitionMetadataResponse());
                cmd.getPartitionMetadataResponse().recycle();
                break;
            case LOOKUP:
                checkArgument(cmd.hasLookupTopic());
                handleLookup(cmd.getLookupTopic());
                cmd.getLookupTopic().recycle();
                break;
            case LOOKUP_RESPONSE:
                checkArgument(cmd.hasLookupTopicResponse());
                handleLookupResponse(cmd.getLookupTopicResponse());
                cmd.getLookupTopicResponse().recycle();
                break;
            case ACK:
                checkArgument(cmd.hasAck());
                handleAck(cmd.getAck());
                cmd.getAck().getMessageId().recycle();
                cmd.getAck().recycle();
                break;
            case CLOSE_CONSUMER:
                checkArgument(cmd.hasCloseConsumer());
                handleCloseConsumer(cmd.getCloseConsumer());
                cmd.getCloseConsumer().recycle();
                break;
            case CLOSE_PRODUCER:
                checkArgument(cmd.hasCloseProducer());
                handleCloseProducer(cmd.getCloseProducer());
                cmd.getCloseProducer().recycle();
                break;
            case CONNECT:
                checkArgument(cmd.hasConnect());
                handleConnect(cmd.getConnect());
                cmd.getConnect().recycle();
                break;
            case CONNECTED:
                checkArgument(cmd.hasConnected());
                handleConnected(cmd.getConnected());
                cmd.getConnected().recycle();
                break;
            case ERROR:
                checkArgument(cmd.hasError());
                handleError(cmd.getError());
                cmd.getError().recycle();
                break;
            case FLOW:
                checkArgument(cmd.hasFlow());
                handleFlow(cmd.getFlow());
                cmd.getFlow().recycle();
                break;
            case MESSAGE:
                {
                    checkArgument(cmd.hasMessage());
                    handleMessage(cmd.getMessage(), buffer);
                    cmd.getMessage().recycle();
                    break;
                }
            case PRODUCER:
                checkArgument(cmd.hasProducer());
                handleProducer(cmd.getProducer());
                cmd.getProducer().recycle();
                break;
            case SEND:
                {
                    checkArgument(cmd.hasSend());
                    // Store a buffer marking the content + headers
                    ByteBuf headersAndPayload = buffer.markReaderIndex();
                    handleSend(cmd.getSend(), headersAndPayload);
                    cmd.getSend().recycle();
                    break;
                }
            case SEND_ERROR:
                checkArgument(cmd.hasSendError());
                handleSendError(cmd.getSendError());
                cmd.getSendError().recycle();
                break;
            case SEND_RECEIPT:
                checkArgument(cmd.hasSendReceipt());
                handleSendReceipt(cmd.getSendReceipt());
                cmd.getSendReceipt().recycle();
                break;
            case SUBSCRIBE:
                checkArgument(cmd.hasSubscribe());
                handleSubscribe(cmd.getSubscribe());
                cmd.getSubscribe().recycle();
                break;
            case SUCCESS:
                checkArgument(cmd.hasSuccess());
                handleSuccess(cmd.getSuccess());
                cmd.getSuccess().recycle();
                break;
            case PRODUCER_SUCCESS:
                checkArgument(cmd.hasProducerSuccess());
                handleProducerSuccess(cmd.getProducerSuccess());
                cmd.getProducerSuccess().recycle();
                break;
            case UNSUBSCRIBE:
                checkArgument(cmd.hasUnsubscribe());
                handleUnsubscribe(cmd.getUnsubscribe());
                cmd.getUnsubscribe().recycle();
                break;
            case PING:
                checkArgument(cmd.hasPing());
                handlePing(cmd.getPing());
                cmd.getPing().recycle();
                break;
            case PONG:
                checkArgument(cmd.hasPong());
                handlePong(cmd.getPong());
                cmd.getPong().recycle();
                break;
            case REDELIVER_UNACKNOWLEDGED_MESSAGES:
                checkArgument(cmd.hasRedeliverUnacknowledgedMessages());
                handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages());
                cmd.getRedeliverUnacknowledgedMessages().recycle();
                break;
            case CONSUMER_STATS:
                checkArgument(cmd.hasConsumerStats());
                handleConsumerStats(cmd.getConsumerStats());
                cmd.getConsumerStats().recycle();
                break;
            case CONSUMER_STATS_RESPONSE:
                checkArgument(cmd.hasConsumerStatsResponse());
                handleConsumerStatsResponse(cmd.getConsumerStatsResponse());
                cmd.getConsumerStatsResponse().recycle();
                break;
        }
    } finally {
        if (cmdBuilder != null) {
            cmdBuilder.recycle();
        }
        if (cmd != null) {
            cmd.recycle();
        }
        buffer.release();
    }
}
Also used : BaseCommand(com.yahoo.pulsar.common.api.proto.PulsarApi.BaseCommand) ByteBufCodedInputStream(com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufCodedInputStream (com.yahoo.pulsar.common.util.protobuf.ByteBufCodedInputStream)4 PulsarApi (com.yahoo.pulsar.common.api.proto.PulsarApi)2 ByteBuf (io.netty.buffer.ByteBuf)2 IOException (java.io.IOException)2 UninitializedMessageException (com.google.protobuf.UninitializedMessageException)1 BaseCommand (com.yahoo.pulsar.common.api.proto.PulsarApi.BaseCommand)1 MessageIdData (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData)1 MessageMetadata (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata)1 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)1 UnpooledHeapByteBuf (io.netty.buffer.UnpooledHeapByteBuf)1