MarvinCai commented on a change in pull request #4265: 
[transaction][acknowledge] Introduce PENDING_ACK state in acknowledgement path
URL: https://github.com/apache/pulsar/pull/4265#discussion_r287214377
 
 

 ##########
 File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java
 ##########
 @@ -209,6 +276,87 @@ public void acknowledgeMessage(List<Position> positions, 
AckType ackType, Map<St
         }
     }
 
+    /**
+     * Acknowledge message(s) for an ongoing transaction.
+     * <p>
+     * It can be of {@link AckType#Individual} or {@link AckType#Cumulative}. 
Single messages acked by ongoing
+     * transaction will be put in pending_ack state and only marked as deleted 
after transaction is committed.
+     * <p>
+     * Only one transaction is allowed to do cumulative ack on a subscription 
at a given time.
+     * If a transaction do multiple cumulative ack, only the one with largest 
position determined by
+     * {@link PositionImpl#compareTo(PositionImpl)} will be kept as it cover 
all position smaller than it.
+     * <p>
+     * If an ongoing transaction cumulative acked a message and then try to 
ack single message which is
+     * smaller than that one it cumulative acked, it'll succeed.
+     * <p>
+     * If transaction is aborted all messages acked by it will be put back to 
pending state.
+     *
+     * @param txnID                  TransactionID of an ongoing transaction 
trying to sck message.
+     * @param positions              {@link Position}(s) it try to ack.
+     * @param ackType                {@link AckType}.
+     * @throws TransactionConflictException
+     * @throws IllegalArgumentException
+     */
+    public synchronized void acknowledgeMessage(TxnID txnID, List<Position> 
positions, AckType ackType) throws TransactionConflictException {
+        checkArgument(txnID != null, "TransactionID can not be null.");
+        if (AckType.Cumulative == ackType) {
+            // Check if another transaction is already using cumulative ack on 
this subscription.
+            if (this.txnID != null && this.txnID != txnID) {
+                String errorMsg = "[" + topicName + "][" + subName + "] 
Transaction:" + txnID.getMostSigBits() +
+                        txnID.getLeastSigBits() + " try to cumulative ack 
message while transaction:"
+                        + this.txnID.getMostSigBits() + 
this.txnID.getLeastSigBits()
+                        + " already cumulative acked messages.";
+                log.error(errorMsg);
+                throw new TransactionConflictException(errorMsg);
+            }
+
+            if (positions.size() != 1) {
+                String errorMsg = "[" + topicName + "][" + subName + "] 
Transaction:" + txnID.getMostSigBits() +
+                txnID.getLeastSigBits() + " invalid cumulative ack received 
with multiple message ids.";
+                log.error(errorMsg);
+                throw new IllegalArgumentException(errorMsg);
+            }
+
+            Position position = positions.get(0);
+
+            if (log.isDebugEnabled()) {
+                log.debug("[{}][{}] TxnID:[{}{}] Cumulative ack on {}.", 
topicName, subName, txnID.getMostSigBits(),
+                        txnID.getLeastSigBits(), position);
+            }
+
+             if (this.txnID == null) {
+                // Only set txnID if no transaction is doing cumulative ack.
+                TXNID_UPDATER.set(this, txnID);
+                POSITION_UPDATER.set(this, position);
+            } else if 
(((PositionImpl)position).compareTo((PositionImpl)this.pendingCumulativeAckMessage)
 > 0) {
+                // If new cumulative ack position is greater than current one, 
update it.
+                POSITION_UPDATER.set(this, position);
+            }
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("[{}][{}] TxnID:[{}{}] Individual acks on {}", 
topicName, subName, txnID.getMostSigBits(),
+                        txnID.getLeastSigBits(), positions);
+            }
+
+            ConcurrentOpenHashSet<Position> pendingAckMessage =
 
 Review comment:
   renamed to pendingAckMessageForCurrentTxn.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to