Search in sources :

Example 11 with MessageIdData

use of org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData in project incubator-pulsar by apache.

the class Commands method newSeek.

public static ByteBuf newSeek(long consumerId, long requestId, long ledgerId, long entryId) {
    CommandSeek.Builder seekBuilder = CommandSeek.newBuilder();
    seekBuilder.setConsumerId(consumerId);
    seekBuilder.setRequestId(requestId);
    MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
    messageIdBuilder.setLedgerId(ledgerId);
    messageIdBuilder.setEntryId(entryId);
    MessageIdData messageId = messageIdBuilder.build();
    seekBuilder.setMessageId(messageId);
    CommandSeek seek = seekBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.SEEK).setSeek(seek));
    messageId.recycle();
    messageIdBuilder.recycle();
    seekBuilder.recycle();
    seek.recycle();
    return res;
}
Also used : CommandSeek(org.apache.pulsar.common.api.proto.PulsarApi.CommandSeek) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with MessageIdData

use of org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData in project incubator-pulsar by apache.

the class RawMessageSerDeserTest method testSerializationAndDeserialization.

@Test
public void testSerializationAndDeserialization() throws Exception {
    int payload = 0xbeefcafe;
    ByteBuf headersAndPayload = Unpooled.buffer(4);
    headersAndPayload.writeInt(payload);
    MessageIdData id = MessageIdData.newBuilder().setLedgerId(0xf00).setEntryId(0xbaa).setPartition(10).setBatchIndex(20).build();
    @Cleanup RawMessage m = new RawMessageImpl(id, headersAndPayload);
    ByteBuf serialized = m.serialize();
    byte[] bytes = new byte[serialized.readableBytes()];
    serialized.readBytes(bytes);
    RawMessage m2 = RawMessageImpl.deserializeFrom(Unpooled.wrappedBuffer(bytes));
    Assert.assertEquals(m2.getMessageIdData().getLedgerId(), m.getMessageIdData().getLedgerId());
    Assert.assertEquals(m2.getMessageIdData().getEntryId(), m.getMessageIdData().getEntryId());
    Assert.assertEquals(m2.getMessageIdData().getPartition(), m.getMessageIdData().getPartition());
    Assert.assertEquals(m2.getMessageIdData().getBatchIndex(), m.getMessageIdData().getBatchIndex());
    Assert.assertEquals(m2.getHeadersAndPayload(), m.getHeadersAndPayload());
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteBuf(io.netty.buffer.ByteBuf) RawMessage(org.apache.pulsar.client.api.RawMessage) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 13 with MessageIdData

use of org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData in project incubator-pulsar by apache.

the class Consumer method sendMessages.

/**
 * Dispatch a list of entries to the consumer. <br/>
 * <b>It is also responsible to release entries data and recycle entries object.</b>
 *
 * @return a promise that can be use to track when all the data has been written into the socket
 */
public SendMessageInfo sendMessages(final List<Entry> entries) {
    final ChannelHandlerContext ctx = cnx.ctx();
    final SendMessageInfo sentMessages = new SendMessageInfo();
    final ChannelPromise writePromise = ctx.newPromise();
    sentMessages.channelPromse = writePromise;
    if (entries.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}-{}] List of messages is empty, triggering write future immediately for consumerId {}", topicName, subscription, consumerId);
        }
        writePromise.setSuccess();
        sentMessages.totalSentMessages = 0;
        sentMessages.totalSentMessageBytes = 0;
        return sentMessages;
    }
    try {
        updatePermitsAndPendingAcks(entries, sentMessages);
    } catch (PulsarServerException pe) {
        log.warn("[{}] [{}] consumer doesn't support batch-message {}", subscription, consumerId, cnx.getRemoteEndpointProtocolVersion());
        subscription.markTopicWithBatchMessagePublished();
        sentMessages.totalSentMessages = 0;
        sentMessages.totalSentMessageBytes = 0;
        // disconnect consumer: it will update dispatcher's availablePermits and resend pendingAck-messages of this
        // consumer to other consumer
        disconnect();
        return sentMessages;
    }
    ctx.channel().eventLoop().execute(() -> {
        for (int i = 0; i < entries.size(); i++) {
            Entry entry = entries.get(i);
            PositionImpl pos = (PositionImpl) entry.getPosition();
            MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
            MessageIdData messageId = messageIdBuilder.setLedgerId(pos.getLedgerId()).setEntryId(pos.getEntryId()).build();
            ByteBuf metadataAndPayload = entry.getDataBuffer();
            // increment ref-count of data and release at the end of process: so, we can get chance to call entry.release
            metadataAndPayload.retain();
            // skip checksum by incrementing reader-index if consumer-client doesn't support checksum verification
            if (cnx.getRemoteEndpointProtocolVersion() < ProtocolVersion.v11.getNumber()) {
                Commands.skipChecksumIfPresent(metadataAndPayload);
            }
            if (log.isDebugEnabled()) {
                log.debug("[{}-{}] Sending message to consumerId {}, entry id {}", topicName, subscription, consumerId, pos.getEntryId());
            }
            // We only want to pass the "real" promise on the last entry written
            ChannelPromise promise = ctx.voidPromise();
            if (i == (entries.size() - 1)) {
                promise = writePromise;
            }
            ctx.write(Commands.newMessage(consumerId, messageId, metadataAndPayload), promise);
            messageId.recycle();
            messageIdBuilder.recycle();
            entry.release();
        }
        ctx.flush();
    });
    return sentMessages;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Entry(org.apache.bookkeeper.mledger.Entry) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 14 with MessageIdData

use of org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData in project incubator-pulsar by apache.

the class ServerCnx method handleGetLastMessageId.

@Override
protected void handleGetLastMessageId(CommandGetLastMessageId getLastMessageId) {
    checkArgument(state == State.Connected);
    CompletableFuture<Consumer> consumerFuture = consumers.get(getLastMessageId.getConsumerId());
    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        Consumer consumer = consumerFuture.getNow(null);
        long requestId = getLastMessageId.getRequestId();
        Topic topic = consumer.getSubscription().getTopic();
        Position position = topic.getLastMessageId();
        int partitionIndex = TopicName.getPartitionIndex(topic.getName());
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}][{}] Get LastMessageId {} partitionIndex {}", remoteAddress, topic.getName(), consumer.getSubscription().getName(), position, partitionIndex);
        }
        MessageIdData messageId = MessageIdData.newBuilder().setLedgerId(((PositionImpl) position).getLedgerId()).setEntryId(((PositionImpl) position).getEntryId()).setPartition(partitionIndex).build();
        ctx.writeAndFlush(Commands.newGetLastMessageIdResponse(requestId, messageId));
    } else {
        ctx.writeAndFlush(Commands.newError(getLastMessageId.getRequestId(), ServerError.MetadataError, "Consumer not found"));
    }
}
Also used : CommandCloseConsumer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CommandLookupTopic(org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopic)

Example 15 with MessageIdData

use of org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData in project incubator-pulsar by apache.

the class ServerCnx method handleSeek.

@Override
protected void handleSeek(CommandSeek seek) {
    checkArgument(state == State.Connected);
    final long requestId = seek.getRequestId();
    CompletableFuture<Consumer> consumerFuture = consumers.get(seek.getConsumerId());
    // Currently only seeking on a message id is supported
    if (!seek.hasMessageId()) {
        ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, "Message id was not present"));
        return;
    }
    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        Consumer consumer = consumerFuture.getNow(null);
        Subscription subscription = consumer.getSubscription();
        MessageIdData msgIdData = seek.getMessageId();
        Position position = new PositionImpl(msgIdData.getLedgerId(), msgIdData.getEntryId());
        subscription.resetCursor(position).thenRun(() -> {
            log.info("[{}] [{}][{}] Reset subscription to message id {}", remoteAddress, subscription.getTopic().getName(), subscription.getName(), position);
            ctx.writeAndFlush(Commands.newSuccess(requestId));
        }).exceptionally(ex -> {
            log.warn("[{}][{}] Failed to reset subscription: {}", remoteAddress, subscription, ex.getMessage(), ex);
            ctx.writeAndFlush(Commands.newError(requestId, ServerError.UnknownError, "Error when resetting subscription: " + ex.getCause().getMessage()));
            return null;
        });
    } else {
        ctx.writeAndFlush(Commands.newError(requestId, ServerError.MetadataError, "Consumer not found"));
    }
}
Also used : CommandCloseConsumer(org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) Position(org.apache.bookkeeper.mledger.Position) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl)

Aggregations

MessageIdData (org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData)16 ByteBuf (io.netty.buffer.ByteBuf)10 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 InitialPosition (org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition)3 Collections (java.util.Collections)2 Map (java.util.Map)2 Cleanup (lombok.Cleanup)2 BookKeeper (org.apache.bookkeeper.client.BookKeeper)2 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)2 Position (org.apache.bookkeeper.mledger.Position)2 Pair (org.apache.commons.lang3.tuple.Pair)2 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 RawMessage (org.apache.pulsar.client.api.RawMessage)2 CommandCloseConsumer (org.apache.pulsar.common.api.proto.PulsarApi.CommandCloseConsumer)2 Test (org.testng.annotations.Test)2