You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/11/09 03:27:17 UTC

[GitHub] [pulsar] codelipenghui commented on a change in pull request #8426: [Transaction]Transaction pendingack server implement patch

codelipenghui commented on a change in pull request #8426:
URL: https://github.com/apache/pulsar/pull/8426#discussion_r518639270



##########
File path: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/PositionAckSetUtil.java
##########
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.util;
+
+import com.google.common.collect.ComparisonChain;
+import org.apache.bookkeeper.mledger.impl.PositionImpl;
+import org.apache.pulsar.common.util.collections.BitSetRecyclable;
+
+public class PositionAckSetUtil {
+
+    //This method is to compare two ack set whether overlap or not
+    public static boolean isAckSetOverlap(long[] currentAckSet, long[] otherAckSet) {
+        if (currentAckSet == null || otherAckSet == null) {
+            return false;
+        }
+
+        BitSetRecyclable currentBitSet = BitSetRecyclable.valueOf(currentAckSet);
+        BitSetRecyclable otherBitSet = BitSetRecyclable.valueOf(otherAckSet);
+        currentBitSet.flip(0, currentBitSet.size());
+        otherBitSet.flip(0, otherBitSet.size());
+        currentBitSet.and(otherBitSet);
+        boolean isAckSetRepeated = !currentBitSet.isEmpty();
+        currentBitSet.recycle();
+        otherBitSet.recycle();
+        return isAckSetRepeated;
+    }
+
+    //This method is do `and` operation for position's ack set
+    public static void andAckSet(PositionImpl currentPosition, PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition == null) {
+            return;
+        }
+        BitSetRecyclable thisAckSet = BitSetRecyclable.valueOf(currentPosition.getAckSet());
+        BitSetRecyclable otherAckSet = BitSetRecyclable.valueOf(otherPosition.getAckSet());
+        thisAckSet.and(otherAckSet);
+        currentPosition.setAckSet(thisAckSet.toLongArray());
+        thisAckSet.recycle();
+        otherAckSet.recycle();
+    }
+
+    //This method is compare two position which position is bigger than another one.
+    //When the ledgerId and entryId in this position is same to another one and two position all have ack set, it will
+    //compare the ack set next bit index is bigger than another one.
+    public static int compareToWithAckSet(PositionImpl currentPosition,PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition ==null) {
+            return -1;
+        }
+        int result = ComparisonChain.start().compare(currentPosition.getLedgerId(),
+                otherPosition.getLedgerId()).compare(currentPosition.getEntryId(), otherPosition.getEntryId())
+                .result();
+        if (result == 0) {
+            if (otherPosition.getAckSet() == null) {
+                return result;
+            }

Review comment:
       Why otherPosition's ackSet is null then return the result?

##########
File path: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/PositionAckSetUtil.java
##########
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.util;
+
+import com.google.common.collect.ComparisonChain;
+import org.apache.bookkeeper.mledger.impl.PositionImpl;
+import org.apache.pulsar.common.util.collections.BitSetRecyclable;
+
+public class PositionAckSetUtil {
+
+    //This method is to compare two ack set whether overlap or not
+    public static boolean isAckSetOverlap(long[] currentAckSet, long[] otherAckSet) {
+        if (currentAckSet == null || otherAckSet == null) {
+            return false;
+        }
+
+        BitSetRecyclable currentBitSet = BitSetRecyclable.valueOf(currentAckSet);
+        BitSetRecyclable otherBitSet = BitSetRecyclable.valueOf(otherAckSet);
+        currentBitSet.flip(0, currentBitSet.size());
+        otherBitSet.flip(0, otherBitSet.size());
+        currentBitSet.and(otherBitSet);
+        boolean isAckSetRepeated = !currentBitSet.isEmpty();
+        currentBitSet.recycle();
+        otherBitSet.recycle();
+        return isAckSetRepeated;
+    }
+
+    //This method is do `and` operation for position's ack set
+    public static void andAckSet(PositionImpl currentPosition, PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition == null) {
+            return;
+        }
+        BitSetRecyclable thisAckSet = BitSetRecyclable.valueOf(currentPosition.getAckSet());
+        BitSetRecyclable otherAckSet = BitSetRecyclable.valueOf(otherPosition.getAckSet());
+        thisAckSet.and(otherAckSet);
+        currentPosition.setAckSet(thisAckSet.toLongArray());
+        thisAckSet.recycle();
+        otherAckSet.recycle();
+    }
+
+    //This method is compare two position which position is bigger than another one.
+    //When the ledgerId and entryId in this position is same to another one and two position all have ack set, it will
+    //compare the ack set next bit index is bigger than another one.
+    public static int compareToWithAckSet(PositionImpl currentPosition,PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition ==null) {
+            return -1;
+        }
+        int result = ComparisonChain.start().compare(currentPosition.getLedgerId(),
+                otherPosition.getLedgerId()).compare(currentPosition.getEntryId(), otherPosition.getEntryId())
+                .result();
+        if (result == 0) {
+            if (otherPosition.getAckSet() == null) {
+                return result;
+            }
+
+            BitSetRecyclable otherAckSet;
+            if (currentPosition.getAckSet() == null) {
+                if (otherPosition.getAckSet() != null) {
+                    otherAckSet = BitSetRecyclable.valueOf(otherPosition.getAckSet());
+                    if (otherAckSet.isEmpty()) {
+                        otherAckSet.recycle();
+                        return result;
+                    } else {
+                        otherAckSet.recycle();
+                        return 1;
+                    }
+
+                }
+                return result;
+            }
+            otherAckSet = BitSetRecyclable.valueOf(otherPosition.getAckSet());
+            BitSetRecyclable thisAckSet = BitSetRecyclable.valueOf(currentPosition.getAckSet());
+            result = thisAckSet.nextSetBit(0) - otherAckSet.nextSetBit(0);
+            otherAckSet.recycle();
+            thisAckSet.recycle();

Review comment:
       I think we can use an empty ackset to simplify these lines.

##########
File path: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/PositionAckSetUtil.java
##########
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.bookkeeper.mledger.util;
+
+import com.google.common.collect.ComparisonChain;
+import org.apache.bookkeeper.mledger.impl.PositionImpl;
+import org.apache.pulsar.common.util.collections.BitSetRecyclable;
+
+public class PositionAckSetUtil {
+
+    //This method is to compare two ack set whether overlap or not
+    public static boolean isAckSetOverlap(long[] currentAckSet, long[] otherAckSet) {
+        if (currentAckSet == null || otherAckSet == null) {
+            return false;
+        }
+
+        BitSetRecyclable currentBitSet = BitSetRecyclable.valueOf(currentAckSet);
+        BitSetRecyclable otherBitSet = BitSetRecyclable.valueOf(otherAckSet);
+        currentBitSet.flip(0, currentBitSet.size());
+        otherBitSet.flip(0, otherBitSet.size());
+        currentBitSet.and(otherBitSet);
+        boolean isAckSetRepeated = !currentBitSet.isEmpty();
+        currentBitSet.recycle();
+        otherBitSet.recycle();
+        return isAckSetRepeated;
+    }
+
+    //This method is do `and` operation for position's ack set
+    public static void andAckSet(PositionImpl currentPosition, PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition == null) {
+            return;
+        }
+        BitSetRecyclable thisAckSet = BitSetRecyclable.valueOf(currentPosition.getAckSet());
+        BitSetRecyclable otherAckSet = BitSetRecyclable.valueOf(otherPosition.getAckSet());
+        thisAckSet.and(otherAckSet);
+        currentPosition.setAckSet(thisAckSet.toLongArray());
+        thisAckSet.recycle();
+        otherAckSet.recycle();
+    }
+
+    //This method is compare two position which position is bigger than another one.
+    //When the ledgerId and entryId in this position is same to another one and two position all have ack set, it will
+    //compare the ack set next bit index is bigger than another one.
+    public static int compareToWithAckSet(PositionImpl currentPosition,PositionImpl otherPosition) {
+        if (currentPosition == null || otherPosition ==null) {
+            return -1;
+        }

Review comment:
       It's better to throw an exception here, if return -1 it means the currentPosition is lower than otherPosition

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
##########
@@ -413,49 +419,149 @@ void doUnsubscribe(final long requestId) {
                     position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
                 }
             }
-            List<Position> positionsAcked = Collections.singletonList(position);
             if (ack.hasTxnidMostBits() && ack.hasTxnidLeastBits()) {
-                return transactionAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked, AckType.Cumulative);
+                List<PositionImpl> positionsAcked = Collections.singletonList(position);
+                return transactionCumulativeAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked);
             } else {
+                List<Position> positionsAcked = Collections.singletonList(position);
                 subscription.acknowledgeMessage(positionsAcked, AckType.Cumulative, properties);
+                return CompletableFuture.completedFuture(null);
             }
         } else {
-            // Individual ack
-            List<Position> positionsAcked = new ArrayList<>();
-            for (int i = 0; i < ack.getMessageIdCount(); i++) {
-                MessageIdData msgId = ack.getMessageId(i);
-                PositionImpl position;
-                if (msgId.getAckSetCount() > 0) {
-                    position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), SafeCollectionUtils.longListToArray(msgId.getAckSetList()));
-                } else {
-                    position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
-                }
-                positionsAcked.add(position);
+            if (ack.hasTxnidLeastBits() && ack.hasTxnidMostBits()) {
+                return individualAckWithTransaction(ack);
+            } else {
+                return individualAckNormal(ack, properties);
+            }
+        }
+    }
 
-                if (Subscription.isIndividualAckMode(subType) && msgId.getAckSetCount() == 0) {
-                    removePendingAcks(position);
+    //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<>();
+        List<PositionImpl> checkBatchPositions = null;
+        if (isTransactionEnabled) {
+            checkBatchPositions = new ArrayList<>();
+        }
+        for (int i = 0; i < ack.getMessageIdCount(); i++) {
+            MessageIdData msgId = ack.getMessageId(i);
+            PositionImpl position;
+            if (msgId.getAckSetCount() > 0) {
+                position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(),
+                        SafeCollectionUtils.longListToArray(msgId.getAckSetList()));
+                if (isTransactionEnabled) {
+                    //sync the batch position bit set point, in order to delete the position in pending acks
+                    checkBatchPositions.add(position);
+                    LongPair batchSizePair = this.pendingAcks.get(msgId.getLedgerId(), msgId.getEntryId());
+                    if (batchSizePair == null) {
+                        String error = "Batch position [" + position + "] could not find " +
+                                "it's batch size from consumer pendingAcks!";
+                        log.warn(error);
+                        return FutureUtil.failedFuture(
+                                new TransactionConflictException(error));

Review comment:
       If this for normal ack, shall we need to return TransactionConflictException?

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
##########
@@ -413,49 +419,149 @@ void doUnsubscribe(final long requestId) {
                     position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
                 }
             }
-            List<Position> positionsAcked = Collections.singletonList(position);
             if (ack.hasTxnidMostBits() && ack.hasTxnidLeastBits()) {
-                return transactionAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked, AckType.Cumulative);
+                List<PositionImpl> positionsAcked = Collections.singletonList(position);
+                return transactionCumulativeAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked);
             } else {
+                List<Position> positionsAcked = Collections.singletonList(position);
                 subscription.acknowledgeMessage(positionsAcked, AckType.Cumulative, properties);
+                return CompletableFuture.completedFuture(null);
             }
         } else {
-            // Individual ack
-            List<Position> positionsAcked = new ArrayList<>();
-            for (int i = 0; i < ack.getMessageIdCount(); i++) {
-                MessageIdData msgId = ack.getMessageId(i);
-                PositionImpl position;
-                if (msgId.getAckSetCount() > 0) {
-                    position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(), SafeCollectionUtils.longListToArray(msgId.getAckSetList()));
-                } else {
-                    position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
-                }
-                positionsAcked.add(position);
+            if (ack.hasTxnidLeastBits() && ack.hasTxnidMostBits()) {
+                return individualAckWithTransaction(ack);
+            } else {
+                return individualAckNormal(ack, properties);
+            }
+        }
+    }
 
-                if (Subscription.isIndividualAckMode(subType) && msgId.getAckSetCount() == 0) {
-                    removePendingAcks(position);
+    //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<>();
+        List<PositionImpl> checkBatchPositions = null;
+        if (isTransactionEnabled) {
+            checkBatchPositions = new ArrayList<>();
+        }
+        for (int i = 0; i < ack.getMessageIdCount(); i++) {
+            MessageIdData msgId = ack.getMessageId(i);
+            PositionImpl position;
+            if (msgId.getAckSetCount() > 0) {
+                position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId(),
+                        SafeCollectionUtils.longListToArray(msgId.getAckSetList()));
+                if (isTransactionEnabled) {
+                    //sync the batch position bit set point, in order to delete the position in pending acks
+                    checkBatchPositions.add(position);
+                    LongPair batchSizePair = this.pendingAcks.get(msgId.getLedgerId(), msgId.getEntryId());
+                    if (batchSizePair == null) {
+                        String error = "Batch position [" + position + "] could not find " +
+                                "it's batch size from consumer pendingAcks!";
+                        log.warn(error);
+                        return FutureUtil.failedFuture(
+                                new TransactionConflictException(error));
+                    }
+                    ((PersistentSubscription) subscription)
+                            .syncBatchPositionBitSetForPendingAck(new MutablePair<>(position, batchSizePair.first));
+                    //check if the position can remove from the consumer pending acks.
+                    // the bit set is empty in pending ack handle.
+                    if (((PersistentSubscription) subscription).checkIsCanDeleteConsumerPendingAck(position)) {
+                        removePendingAcks(position);
+                    }
                 }
+            } else {
+                position = PositionImpl.get(msgId.getLedgerId(), msgId.getEntryId());
+            }
+            positionsAcked.add(position);
+            if (Subscription.isIndividualAckMode(subType) && msgId.getAckSetCount() == 0) {
+                removePendingAcks(position);
+            }
 
-                if (ack.hasValidationError()) {
-                    log.error("[{}] [{}] Received ack for corrupted message at {} - Reason: {}", subscription,
-                            consumerId, position, ack.getValidationError());
-                }
+            if (ack.hasValidationError()) {
+                log.error("[{}] [{}] Received ack for corrupted message at {} - Reason: {}", subscription,
+                        consumerId, position, ack.getValidationError());
             }
-            if (ack.hasTxnidMostBits() && ack.hasTxnidLeastBits()) {
-                return transactionAcknowledge(ack.getTxnidMostBits(), ack.getTxnidLeastBits(), positionsAcked, AckType.Individual);
+        }
+        subscription.acknowledgeMessage(positionsAcked, AckType.Individual, properties);
+        return CompletableFuture.completedFuture(null);
+    }
+
+
+    //this method is for individual ack carry the transaction
+    private CompletableFuture<Void> individualAckWithTransaction(CommandAck ack) {

Review comment:
       Looks there is much duplicate code with the `individualAckNormal`, is it possible to refine it?

##########
File path: pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
##########
@@ -131,12 +132,13 @@
     private static final double avgPercent = 0.9;
     private boolean preciseDispatcherFlowControl;
     private PositionImpl readPositionWhenJoining;
+    private final boolean isTransactionEnabled;

Review comment:
       Move the isTransactionEnabled to the topic or subscription?  We cannot treat consumers differently in transaction.




----------------------------------------------------------------
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:
users@infra.apache.org