Search in sources :

Example 1 with CommandAck

use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.

the class Consumer method individualAckWithTransaction.

// this method is for individual ack carry the transaction
private CompletableFuture<Void> individualAckWithTransaction(CommandAck ack) {
    // Individual ack
    List<MutablePair<PositionImpl, Integer>> positionsAcked = new ArrayList<>();
    if (!isTransactionEnabled()) {
        return FutureUtil.failedFuture(new BrokerServiceException.NotAllowedException("Server don't support transaction ack!"));
    }
    for (int i = 0; i < ack.getMessageIdsCount(); i++) {
        MessageIdData msgId = ack.getMessageIdAt(i);
        PositionImpl position;
        long ackedCount = 0;
        long batchSize = getBatchSize(msgId);
        Consumer ackOwnerConsumer = getAckOwnerConsumer(msgId.getLedgerId(), msgId.getEntryId());
        if (msgId.getAckSetsCount() > 0) {
            long[] ackSets = new long[msgId.getAckSetsCount()];
            for (int j = 0; j < msgId.getAckSetsCount(); j++) {
                ackSets[j] = msgId.getAckSetAt(j);
            }
            position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), ackSets);
            ackedCount = getAckedCountForTransactionAck(batchSize, ackSets);
        } else {
            position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
            ackedCount = batchSize;
        }
        positionsAcked.add(new MutablePair<>(position, (int) batchSize));
        addAndGetUnAckedMsgs(ackOwnerConsumer, -(int) ackedCount);
        checkCanRemovePendingAcksAndHandle(position, msgId);
        checkAckValidationError(ack, position);
    }
    CompletableFuture<Void> completableFuture = transactionIndividualAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked);
    if (Subscription.isIndividualAckMode(subType)) {
        completableFuture.whenComplete((v, e) -> positionsAcked.forEach(positionLongMutablePair -> {
            if (positionLongMutablePair.getLeft().getAckSet() != null) {
                if (((PersistentSubscription) subscription).checkIsCanDeleteConsumerPendingAck(positionLongMutablePair.left)) {
                    removePendingAcks(positionLongMutablePair.left);
                }
            }
        }));
    }
    return completableFuture;
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) Setter(lombok.Setter) MutableInt(org.apache.commons.lang3.mutable.MutableInt) TopicName(org.apache.pulsar.common.naming.TopicName) LongPair(org.apache.pulsar.common.util.collections.ConcurrentLongLongPairHashMap.LongPair) Getter(lombok.Getter) AckType(org.apache.pulsar.common.api.proto.CommandAck.AckType) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) KeySharedMeta(org.apache.pulsar.common.api.proto.KeySharedMeta) ArrayList(java.util.ArrayList) Commands(org.apache.pulsar.common.protocol.Commands) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TxnID(org.apache.pulsar.client.api.transaction.TxnID) Lists(com.google.common.collect.Lists) KeyLongValue(org.apache.pulsar.common.api.proto.KeyLongValue) DateFormatter(org.apache.pulsar.common.util.DateFormatter) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Map(java.util.Map) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) CommandAck(org.apache.pulsar.common.api.proto.CommandAck) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) DEFAULT_CONSUMER_EPOCH(org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH) Logger(org.slf4j.Logger) Promise(io.netty.util.concurrent.Promise) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) TransactionConflictException(org.apache.pulsar.transaction.common.exception.TransactionConflictException) BitSetRecyclable(org.apache.pulsar.common.util.collections.BitSetRecyclable) MoreObjects(com.google.common.base.MoreObjects) Position(org.apache.bookkeeper.mledger.Position) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) ConcurrentLongLongPairHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongLongPairHashMap) BitSet(java.util.BitSet) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) Rate(org.apache.pulsar.common.stats.Rate) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ArrayList(java.util.ArrayList) MutablePair(org.apache.commons.lang3.tuple.MutablePair)

Example 2 with CommandAck

use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.

the class Commands method newMultiMessageAckCommon.

private static BaseCommand newMultiMessageAckCommon(List<Triple<Long, Long, ConcurrentBitSetRecyclable>> entries) {
    BaseCommand cmd = localCmd(Type.ACK);
    CommandAck ack = cmd.setAck();
    int entriesCount = entries.size();
    for (int i = 0; i < entriesCount; i++) {
        long ledgerId = entries.get(i).getLeft();
        long entryId = entries.get(i).getMiddle();
        ConcurrentBitSetRecyclable bitSet = entries.get(i).getRight();
        MessageIdData msgId = ack.addMessageId().setLedgerId(ledgerId).setEntryId(entryId);
        if (bitSet != null) {
            long[] ackSet = bitSet.toLongArray();
            for (int j = 0; j < ackSet.length; j++) {
                msgId.addAckSet(ackSet[j]);
            }
            bitSet.recycle();
        }
    }
    return cmd;
}
Also used : CommandAck(org.apache.pulsar.common.api.proto.CommandAck) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) ConcurrentBitSetRecyclable(org.apache.pulsar.common.util.collections.ConcurrentBitSetRecyclable)

Example 3 with CommandAck

use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.

the class Consumer method individualAckNormal.

// this method is for individual ack not carry the transaction
private CompletableFuture<Void> individualAckNormal(CommandAck ack, Map<String, Long> properties) {
    List<Position> positionsAcked = new ArrayList<>();
    for (int i = 0; i < ack.getMessageIdsCount(); i++) {
        MessageIdData msgId = ack.getMessageIdAt(i);
        PositionImpl position;
        long ackedCount = 0;
        long batchSize = getBatchSize(msgId);
        Consumer ackOwnerConsumer = getAckOwnerConsumer(msgId.getLedgerId(), msgId.getEntryId());
        if (msgId.getAckSetsCount() > 0) {
            long[] ackSets = new long[msgId.getAckSetsCount()];
            for (int j = 0; j < msgId.getAckSetsCount(); j++) {
                ackSets[j] = msgId.getAckSetAt(j);
            }
            position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), ackSets);
            ackedCount = getAckedCountForBatchIndexLevelEnabled(position, batchSize, ackSets);
            if (isTransactionEnabled()) {
                // sync the batch position bit set point, in order to delete the position in pending acks
                if (Subscription.isIndividualAckMode(subType)) {
                    ((PersistentSubscription) subscription).syncBatchPositionBitSetForPendingAck(position);
                }
            }
        } else {
            position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
            ackedCount = getAckedCountForMsgIdNoAckSets(batchSize, position);
        }
        addAndGetUnAckedMsgs(ackOwnerConsumer, -(int) ackedCount);
        positionsAcked.add(position);
        checkCanRemovePendingAcksAndHandle(position, msgId);
        checkAckValidationError(ack, position);
    }
    subscription.acknowledgeMessage(positionsAcked, AckType.Individual, properties);
    CompletableFuture<Void> completableFuture = new CompletableFuture<>();
    completableFuture.complete(null);
    if (isTransactionEnabled() && Subscription.isIndividualAckMode(subType)) {
        completableFuture.whenComplete((v, e) -> positionsAcked.forEach(position -> {
            // the bit set is empty in pending ack handle.
            if (((PositionImpl) position).getAckSet() != null) {
                if (((PersistentSubscription) subscription).checkIsCanDeleteConsumerPendingAck((PositionImpl) position)) {
                    removePendingAcks((PositionImpl) position);
                }
            }
        }));
    }
    return completableFuture;
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) AtomicIntegerFieldUpdater(java.util.concurrent.atomic.AtomicIntegerFieldUpdater) Setter(lombok.Setter) MutableInt(org.apache.commons.lang3.mutable.MutableInt) TopicName(org.apache.pulsar.common.naming.TopicName) LongPair(org.apache.pulsar.common.util.collections.ConcurrentLongLongPairHashMap.LongPair) Getter(lombok.Getter) AckType(org.apache.pulsar.common.api.proto.CommandAck.AckType) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) Entry(org.apache.bookkeeper.mledger.Entry) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) KeySharedMeta(org.apache.pulsar.common.api.proto.KeySharedMeta) ArrayList(java.util.ArrayList) Commands(org.apache.pulsar.common.protocol.Commands) SubType(org.apache.pulsar.common.api.proto.CommandSubscribe.SubType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TxnID(org.apache.pulsar.client.api.transaction.TxnID) Lists(com.google.common.collect.Lists) KeyLongValue(org.apache.pulsar.common.api.proto.KeyLongValue) DateFormatter(org.apache.pulsar.common.util.DateFormatter) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Map(java.util.Map) ConsumerStatsImpl(org.apache.pulsar.common.policies.data.stats.ConsumerStatsImpl) CommandAck(org.apache.pulsar.common.api.proto.CommandAck) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) DEFAULT_CONSUMER_EPOCH(org.apache.pulsar.common.protocol.Commands.DEFAULT_CONSUMER_EPOCH) Logger(org.slf4j.Logger) Promise(io.netty.util.concurrent.Promise) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) TransactionConflictException(org.apache.pulsar.transaction.common.exception.TransactionConflictException) BitSetRecyclable(org.apache.pulsar.common.util.collections.BitSetRecyclable) MoreObjects(com.google.common.base.MoreObjects) Position(org.apache.bookkeeper.mledger.Position) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) ConcurrentLongLongPairHashMap(org.apache.pulsar.common.util.collections.ConcurrentLongLongPairHashMap) BitSet(java.util.BitSet) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) Rate(org.apache.pulsar.common.stats.Rate) InitialPosition(org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition) Position(org.apache.bookkeeper.mledger.Position) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ArrayList(java.util.ArrayList) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 4 with CommandAck

use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.

the class Commands method newAck.

public static ByteBuf newAck(long consumerId, long ledgerId, long entryId, BitSetRecyclable ackSet, AckType ackType, ValidationError validationError, Map<String, Long> properties, long txnIdLeastBits, long txnIdMostBits, long requestId, int batchSize) {
    BaseCommand cmd = localCmd(Type.ACK);
    CommandAck ack = cmd.setAck().setConsumerId(consumerId).setAckType(ackType);
    MessageIdData messageIdData = ack.addMessageId().setLedgerId(ledgerId).setEntryId(entryId);
    if (ackSet != null) {
        long[] as = ackSet.toLongArray();
        for (int i = 0; i < as.length; i++) {
            messageIdData.addAckSet(as[i]);
        }
    }
    if (batchSize >= 0) {
        messageIdData.setBatchSize(batchSize);
    }
    if (validationError != null) {
        ack.setValidationError(validationError);
    }
    if (txnIdMostBits >= 0) {
        ack.setTxnidMostBits(txnIdMostBits);
    }
    if (txnIdLeastBits >= 0) {
        ack.setTxnidLeastBits(txnIdLeastBits);
    }
    if (requestId >= 0) {
        ack.setRequestId(requestId);
    }
    if (!properties.isEmpty()) {
        properties.forEach((k, v) -> {
            ack.addProperty().setKey(k).setValue(v);
        });
    }
    return serializeWithSize(cmd);
}
Also used : CommandAck(org.apache.pulsar.common.api.proto.CommandAck) MessageIdData(org.apache.pulsar.common.api.proto.MessageIdData) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand)

Example 5 with CommandAck

use of org.apache.pulsar.common.api.proto.CommandAck in project pulsar by apache.

the class BrokerInterceptorWithClassLoaderTest method testClassLoaderSwitcher.

@Test
public void testClassLoaderSwitcher() throws Exception {
    NarClassLoader narLoader = mock(NarClassLoader.class);
    BrokerInterceptor interceptor = new BrokerInterceptor() {

        @Override
        public void beforeSendMessage(Subscription subscription, Entry entry, long[] ackSet, MessageMetadata msgMetadata) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onConnectionCreated(ServerCnx cnx) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void producerCreated(ServerCnx cnx, Producer producer, Map<String, String> metadata) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void consumerCreated(ServerCnx cnx, Consumer consumer, Map<String, String> metadata) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void messageProduced(ServerCnx cnx, Producer producer, long startTimeNs, long ledgerId, long entryId, Topic.PublishContext publishContext) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void messageDispatched(ServerCnx cnx, Consumer consumer, long ledgerId, long entryId, ByteBuf headersAndPayload) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void messageAcked(ServerCnx cnx, Consumer consumer, CommandAck ackCmd) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onPulsarCommand(BaseCommand command, ServerCnx cnx) throws InterceptException {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onConnectionClosed(ServerCnx cnx) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onWebserviceRequest(ServletRequest request) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onWebserviceResponse(ServletRequest request, ServletResponse response) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void onFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void initialize(PulsarService pulsarService) throws Exception {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }

        @Override
        public void close() {
            assertEquals(Thread.currentThread().getContextClassLoader(), narLoader);
        }
    };
    BrokerInterceptorWithClassLoader brokerInterceptorWithClassLoader = new BrokerInterceptorWithClassLoader(interceptor, narLoader);
    ClassLoader curClassLoader = Thread.currentThread().getContextClassLoader();
    // test class loader
    assertEquals(brokerInterceptorWithClassLoader.getClassLoader(), narLoader);
    // test initialize
    brokerInterceptorWithClassLoader.initialize(mock(PulsarService.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onFilter
    brokerInterceptorWithClassLoader.onFilter(mock(ServletRequest.class), mock(ServletResponse.class), mock(FilterChain.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onWebserviceResponse
    brokerInterceptorWithClassLoader.onWebserviceResponse(mock(ServletRequest.class), mock(ServletResponse.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onWebserviceRequest
    brokerInterceptorWithClassLoader.onWebserviceRequest(mock(ServletRequest.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onConnectionClosed
    brokerInterceptorWithClassLoader.onConnectionClosed(mock(ServerCnx.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onPulsarCommand
    brokerInterceptorWithClassLoader.onPulsarCommand(null, mock(ServerCnx.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test messageAcked
    brokerInterceptorWithClassLoader.messageAcked(mock(ServerCnx.class), mock(Consumer.class), null);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test messageDispatched
    brokerInterceptorWithClassLoader.messageDispatched(mock(ServerCnx.class), mock(Consumer.class), 1, 1, null);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test messageProduced
    brokerInterceptorWithClassLoader.messageProduced(mock(ServerCnx.class), mock(Producer.class), 1, 1, 1, null);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test consumerCreated
    brokerInterceptorWithClassLoader.consumerCreated(mock(ServerCnx.class), mock(Consumer.class), Maps.newHashMap());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test producerCreated
    brokerInterceptorWithClassLoader.producerCreated(mock(ServerCnx.class), mock(Producer.class), Maps.newHashMap());
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test onConnectionCreated
    brokerInterceptorWithClassLoader.onConnectionCreated(mock(ServerCnx.class));
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test beforeSendMessage
    brokerInterceptorWithClassLoader.beforeSendMessage(mock(Subscription.class), mock(Entry.class), null, null);
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
    // test close
    brokerInterceptorWithClassLoader.close();
    assertEquals(Thread.currentThread().getContextClassLoader(), curClassLoader);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) BaseCommand(org.apache.pulsar.common.api.proto.BaseCommand) ServerCnx(org.apache.pulsar.broker.service.ServerCnx) FilterChain(javax.servlet.FilterChain) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) ByteBuf(io.netty.buffer.ByteBuf) CommandAck(org.apache.pulsar.common.api.proto.CommandAck) Entry(org.apache.bookkeeper.mledger.Entry) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Producer(org.apache.pulsar.broker.service.Producer) Consumer(org.apache.pulsar.broker.service.Consumer) PulsarService(org.apache.pulsar.broker.PulsarService) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) Subscription(org.apache.pulsar.broker.service.Subscription) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

CommandAck (org.apache.pulsar.common.api.proto.CommandAck)5 MessageIdData (org.apache.pulsar.common.api.proto.MessageIdData)4 Map (java.util.Map)3 Entry (org.apache.bookkeeper.mledger.Entry)3 BaseCommand (org.apache.pulsar.common.api.proto.BaseCommand)3 MoreObjects (com.google.common.base.MoreObjects)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Lists (com.google.common.collect.Lists)2 Future (io.netty.util.concurrent.Future)2 Promise (io.netty.util.concurrent.Promise)2 ArrayList (java.util.ArrayList)2 BitSet (java.util.BitSet)2 Collections (java.util.Collections)2 List (java.util.List)2 Objects (java.util.Objects)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicIntegerFieldUpdater (java.util.concurrent.atomic.AtomicIntegerFieldUpdater)2 LongAdder (java.util.concurrent.atomic.LongAdder)2 Collectors (java.util.stream.Collectors)2 Getter (lombok.Getter)2