Search in sources :

Example 6 with MessageIdData

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

the class Commands method newSendReceipt.

public static ByteBuf newSendReceipt(long producerId, long sequenceId, long ledgerId, long entryId) {
    CommandSendReceipt.Builder sendReceiptBuilder = CommandSendReceipt.newBuilder();
    sendReceiptBuilder.setProducerId(producerId);
    sendReceiptBuilder.setSequenceId(sequenceId);
    MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
    messageIdBuilder.setLedgerId(ledgerId);
    messageIdBuilder.setEntryId(entryId);
    MessageIdData messageId = messageIdBuilder.build();
    sendReceiptBuilder.setMessageId(messageId);
    CommandSendReceipt sendReceipt = sendReceiptBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.SEND_RECEIPT).setSendReceipt(sendReceipt));
    messageIdBuilder.recycle();
    messageId.recycle();
    sendReceiptBuilder.recycle();
    sendReceipt.recycle();
    return res;
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) CommandSendReceipt(org.apache.pulsar.common.api.proto.PulsarApi.CommandSendReceipt) ByteBuf(io.netty.buffer.ByteBuf)

Example 7 with MessageIdData

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

the class CompactedTopicImpl method readOneMessageId.

private static CompletableFuture<MessageIdData> readOneMessageId(LedgerHandle lh, long entryId) {
    CompletableFuture<MessageIdData> promise = new CompletableFuture<>();
    lh.asyncReadEntries(entryId, entryId, (rc, _lh, seq, ctx) -> {
        if (rc != BKException.Code.OK) {
            promise.completeExceptionally(BKException.create(rc));
        } else {
            try (RawMessage m = RawMessageImpl.deserializeFrom(seq.nextElement().getEntryBuffer())) {
                promise.complete(m.getMessageIdData());
            } catch (NoSuchElementException e) {
                log.error("No such entry {} in ledger {}", entryId, lh.getId());
                promise.completeExceptionally(e);
            }
        }
    }, null);
    return promise;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) RawMessage(org.apache.pulsar.client.api.RawMessage) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with MessageIdData

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

the class ConsumerImpl method redeliverUnacknowledgedMessages.

@Override
public void redeliverUnacknowledgedMessages(Set<MessageId> messageIds) {
    checkArgument(messageIds.stream().findFirst().get() instanceof MessageIdImpl);
    if (conf.getSubscriptionType() != SubscriptionType.Shared) {
        // We cannot redeliver single messages if subscription type is not Shared
        redeliverUnacknowledgedMessages();
        return;
    }
    ClientCnx cnx = cnx();
    if (isConnected() && cnx.getRemoteEndpointProtocolVersion() >= ProtocolVersion.v2.getNumber()) {
        int messagesFromQueue = removeExpiredMessagesFromQueue(messageIds);
        Iterable<List<MessageIdImpl>> batches = Iterables.partition(messageIds.stream().map(messageId -> (MessageIdImpl) messageId).collect(Collectors.toSet()), MAX_REDELIVER_UNACKNOWLEDGED);
        MessageIdData.Builder builder = MessageIdData.newBuilder();
        batches.forEach(ids -> {
            List<MessageIdData> messageIdDatas = ids.stream().map(messageId -> {
                // attempt to remove message from batchMessageAckTracker
                builder.setPartition(messageId.getPartitionIndex());
                builder.setLedgerId(messageId.getLedgerId());
                builder.setEntryId(messageId.getEntryId());
                return builder.build();
            }).collect(Collectors.toList());
            ByteBuf cmd = Commands.newRedeliverUnacknowledgedMessages(consumerId, messageIdDatas);
            cnx.ctx().writeAndFlush(cmd, cnx.ctx().voidPromise());
            messageIdDatas.forEach(MessageIdData::recycle);
        });
        if (messagesFromQueue > 0) {
            increaseAvailablePermits(cnx, messagesFromQueue);
        }
        builder.recycle();
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] [{}] Redeliver unacked messages and increase {} permits", subscription, topic, consumerName, messagesFromQueue);
        }
        return;
    }
    if (cnx == null || (getState() == State.Connecting)) {
        log.warn("[{}] Client Connection needs to be establised for redelivery of unacknowledged messages", this);
    } else {
        log.warn("[{}] Reconnecting the client to redeliver the messages.", this);
        cnx.ctx().close();
    }
}
Also used : PulsarApi(org.apache.pulsar.common.api.proto.PulsarApi) Commands.hasChecksum(org.apache.pulsar.common.api.Commands.hasChecksum) LoggerFactory(org.slf4j.LoggerFactory) AckType(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck.AckType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Crc32cIntChecksum.computeChecksum(com.scurrilous.circe.checksum.Crc32cIntChecksum.computeChecksum) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) PulsarDecoder(org.apache.pulsar.common.api.PulsarDecoder) Commands(org.apache.pulsar.common.api.Commands) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) Consumer(org.apache.pulsar.client.api.Consumer) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) ConsumerCryptoFailureAction(org.apache.pulsar.client.api.ConsumerCryptoFailureAction) Iterables(com.google.common.collect.Iterables) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ProtocolVersion(org.apache.pulsar.common.api.proto.PulsarApi.ProtocolVersion) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Message(org.apache.pulsar.client.api.Message) SubscriptionInitialPosition(org.apache.pulsar.client.api.SubscriptionInitialPosition) ArrayList(java.util.ArrayList) ConsumerStats(org.apache.pulsar.client.api.ConsumerStats) ByteBuf(io.netty.buffer.ByteBuf) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) ExecutorService(java.util.concurrent.ExecutorService) Timeout(io.netty.util.Timeout) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) IOException(java.io.IOException) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Schema(org.apache.pulsar.client.api.Schema) Commands.readChecksum(org.apache.pulsar.common.api.Commands.readChecksum) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ValidationError(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck.ValidationError) AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageId(org.apache.pulsar.client.api.MessageId) CompressionType(org.apache.pulsar.common.api.proto.PulsarApi.CompressionType) InitialPosition(org.apache.pulsar.common.api.proto.PulsarApi.CommandSubscribe.InitialPosition) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) List(java.util.List) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with MessageIdData

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

the class MessageIdImpl method toByteArray.

// batchIndex is -1 if message is non-batched message and has the batchIndex for a batch message
protected byte[] toByteArray(int batchIndex) {
    MessageIdData.Builder builder = MessageIdData.newBuilder();
    builder.setLedgerId(ledgerId);
    builder.setEntryId(entryId);
    if (partitionIndex >= 0) {
        builder.setPartition(partitionIndex);
    }
    if (batchIndex != -1) {
        builder.setBatchIndex(batchIndex);
    }
    MessageIdData msgId = builder.build();
    int size = msgId.getSerializedSize();
    ByteBuf serialized = Unpooled.buffer(size, size);
    ByteBufCodedOutputStream stream = ByteBufCodedOutputStream.get(serialized);
    try {
        msgId.writeTo(stream);
    } catch (IOException e) {
        // This is in-memory serialization, should not fail
        throw new RuntimeException(e);
    }
    msgId.recycle();
    builder.recycle();
    stream.recycle();
    return serialized.array();
}
Also used : MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ByteBufCodedOutputStream(org.apache.pulsar.common.util.protobuf.ByteBufCodedOutputStream)

Example 10 with MessageIdData

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

the class Commands method newAck.

public static ByteBuf newAck(long consumerId, long ledgerId, long entryId, AckType ackType, ValidationError validationError, Map<String, Long> properties) {
    CommandAck.Builder ackBuilder = CommandAck.newBuilder();
    ackBuilder.setConsumerId(consumerId);
    ackBuilder.setAckType(ackType);
    MessageIdData.Builder messageIdDataBuilder = MessageIdData.newBuilder();
    messageIdDataBuilder.setLedgerId(ledgerId);
    messageIdDataBuilder.setEntryId(entryId);
    MessageIdData messageIdData = messageIdDataBuilder.build();
    ackBuilder.setMessageId(messageIdData);
    if (validationError != null) {
        ackBuilder.setValidationError(validationError);
    }
    for (Map.Entry<String, Long> e : properties.entrySet()) {
        ackBuilder.addProperties(PulsarApi.KeyLongValue.newBuilder().setKey(e.getKey()).setValue(e.getValue()).build());
    }
    CommandAck ack = ackBuilder.build();
    ByteBuf res = serializeWithSize(BaseCommand.newBuilder().setType(Type.ACK).setAck(ack));
    ack.recycle();
    ackBuilder.recycle();
    messageIdDataBuilder.recycle();
    messageIdData.recycle();
    return res;
}
Also used : CommandAck(org.apache.pulsar.common.api.proto.PulsarApi.CommandAck) MessageIdData(org.apache.pulsar.common.api.proto.PulsarApi.MessageIdData) ByteString(com.google.protobuf.ByteString) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map)

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