congbobo184 commented on code in PR #15137:
URL: https://github.com/apache/pulsar/pull/15137#discussion_r850171905


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/pendingack/impl/MLPendingAckStore.java:
##########
@@ -222,59 +229,29 @@ public void addComplete(Position position, ByteBuf 
entryData, Object ctx) {
                 // store the persistent position in to memory
                 if (pendingAckMetadataEntry.getPendingAckOp() != 
PendingAckOp.ABORT
                         && pendingAckMetadataEntry.getPendingAckOp() != 
PendingAckOp.COMMIT) {
+                    logAppendTimes.incrementAndGet();
                     Optional<PendingAckMetadata> optional = 
pendingAckMetadataEntry.getPendingAckMetadatasList()
                             .stream().max((o1, o2) -> 
ComparisonChain.start().compare(o1.getLedgerId(),
                                     o2.getLedgerId()).compare(o1.getEntryId(), 
o2.getEntryId()).result());
-                    optional.ifPresent(pendingAckMetadata ->
-                            metadataPositions.compute((PositionImpl) position, 
(thisPosition, otherPosition) -> {
-                                PositionImpl nowPosition = 
PositionImpl.get(pendingAckMetadata.getLedgerId(),
-                                        pendingAckMetadata.getEntryId());
-                                if (otherPosition == null) {
-                                    return nowPosition;
-                                } else {
-                                    return 
nowPosition.compareTo(otherPosition) > 0 ? nowPosition : otherPosition;
-                                }
-                    }));
+                    optional.ifPresent(pendingAckMetadata -> {
+                        PositionImpl nowPosition = 
PositionImpl.get(pendingAckMetadata.getLedgerId(),
+                                pendingAckMetadata.getEntryId());
+                        if (nowPosition.compareTo(maxAckPosition) > 0) {
+                            maxAckPosition = nowPosition;
+                        }

Review Comment:
   if maxAckPosition don't change, should change the last log position in the 
pendingAckLogIndex. and if commit or abort op also can change the last log 
position in the pendingAckLogIndex if the last ack position in the map = 
maxAckPosition



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/LogIndexBackoff.java:
##########
@@ -0,0 +1,52 @@
+/**
+ * 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.pulsar.broker.transaction;

Review Comment:
   move it to transaction.util package is better



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckPersistentTest.java:
##########
@@ -345,4 +350,103 @@ private void 
testDeleteTopicThenDeletePendingAckManagedLedger() throws Exception
         
assertFalse(topics.contains(MLPendingAckStore.getTransactionPendingAckStoreSuffix(topic,
 subName2)));
         assertFalse(topics.contains(topic));
     }
+
+    @Test
+    public void testDeleteUselessLogDataWhenSubCursorMoved() throws Exception {
+        
getPulsarServiceList().get(0).getConfig().setTransactionPendingAckLogIndexMinLag(5);
+        
getPulsarServiceList().get(0).getConfiguration().setManagedLedgerDefaultMarkDeleteRateLimit(5);
+        String subName = "test-log-delete";
+        String topic = TopicName.get(TopicDomain.persistent.toString(),
+                NamespaceName.get(NAMESPACE1), "test-log-delete").toString();
+
+        @Cleanup
+        Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName(subName)
+                .subscribe();
+        @Cleanup
+        Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .sendTimeout(0, TimeUnit.SECONDS)
+                .enableBatching(false)
+                .create();
+
+        for (int i = 0; i < 20; i++) {
+            producer.newMessage().send();
+        }
+        // init
+        Message<byte[]> message = consumer.receive(5, TimeUnit.SECONDS);
+        Transaction transaction = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.SECONDS)
+                .build()
+                .get();
+        consumer.acknowledgeAsync(message.getMessageId(), transaction);
+        transaction.commit().get();
+
+        PersistentTopic persistentTopic = (PersistentTopic) 
getPulsarServiceList().get(0)
+                .getBrokerService().getTopic(topic, false).get().get();
+
+        PersistentSubscription persistentSubscription = 
persistentTopic.getSubscription(subName);
+        Field field = 
PersistentSubscription.class.getDeclaredField("pendingAckHandle");
+        field.setAccessible(true);
+        PendingAckHandleImpl pendingAckHandle = (PendingAckHandleImpl) 
field.get(persistentSubscription);
+        Field field1 = 
PendingAckHandleImpl.class.getDeclaredField("pendingAckStoreFuture");
+        field1.setAccessible(true);
+        PendingAckStore pendingAckStore = 
((CompletableFuture<PendingAckStore>) field1.get(pendingAckHandle)).get();
+
+        Field field3 = 
MLPendingAckStore.class.getDeclaredField("pendingAckLogIndex");
+        Field field4 = 
MLPendingAckStore.class.getDeclaredField("upperLimitOfLogAppendTimes");
+
+        field3.setAccessible(true);
+        field4.setAccessible(true);
+
+        ConcurrentSkipListMap<PositionImpl, PositionImpl> pendingAckLogIndex =
+                (ConcurrentSkipListMap<PositionImpl, PositionImpl>) 
field3.get(pendingAckStore);
+        long upperLimitOfLogAppendTimes = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(pendingAckLogIndex.size(), 1);
+        Assert.assertEquals(upperLimitOfLogAppendTimes, 5);
+
+        Awaitility.await().untilAsserted(() ->
+                
Assert.assertEquals(persistentSubscription.getCursor().getPersistentMarkDeletedPosition().getEntryId(),
+                        ((MessageIdImpl)message.getMessageId()).getEntryId()));
+        // 7 more acks. Will find that there are still only two records in the 
map.
+        Transaction transaction1 = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.SECONDS)
+                .build()
+                .get();
+        //remove previous index
+        Message<byte[]> message0 = consumer.receive(5, TimeUnit.SECONDS);
+        consumer.acknowledgeAsync(message0.getMessageId(), transaction1).get();
+        Assert.assertEquals(pendingAckLogIndex.size(), 0);
+        upperLimitOfLogAppendTimes = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(upperLimitOfLogAppendTimes, 5);

Review Comment:
   also think about cursor persistentMarkerDeletePostion changed



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckPersistentTest.java:
##########
@@ -345,4 +350,103 @@ private void 
testDeleteTopicThenDeletePendingAckManagedLedger() throws Exception
         
assertFalse(topics.contains(MLPendingAckStore.getTransactionPendingAckStoreSuffix(topic,
 subName2)));
         assertFalse(topics.contains(topic));
     }
+
+    @Test
+    public void testDeleteUselessLogDataWhenSubCursorMoved() throws Exception {
+        
getPulsarServiceList().get(0).getConfig().setTransactionPendingAckLogIndexMinLag(5);
+        
getPulsarServiceList().get(0).getConfiguration().setManagedLedgerDefaultMarkDeleteRateLimit(5);
+        String subName = "test-log-delete";
+        String topic = TopicName.get(TopicDomain.persistent.toString(),
+                NamespaceName.get(NAMESPACE1), "test-log-delete").toString();
+
+        @Cleanup
+        Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topic)
+                .subscriptionName(subName)
+                .subscribe();
+        @Cleanup
+        Producer<byte[]> producer = pulsarClient.newProducer()
+                .topic(topic)
+                .sendTimeout(0, TimeUnit.SECONDS)
+                .enableBatching(false)
+                .create();
+
+        for (int i = 0; i < 20; i++) {
+            producer.newMessage().send();
+        }
+        // init
+        Message<byte[]> message = consumer.receive(5, TimeUnit.SECONDS);
+        Transaction transaction = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.SECONDS)
+                .build()
+                .get();
+        consumer.acknowledgeAsync(message.getMessageId(), transaction);
+        transaction.commit().get();
+
+        PersistentTopic persistentTopic = (PersistentTopic) 
getPulsarServiceList().get(0)
+                .getBrokerService().getTopic(topic, false).get().get();
+
+        PersistentSubscription persistentSubscription = 
persistentTopic.getSubscription(subName);
+        Field field = 
PersistentSubscription.class.getDeclaredField("pendingAckHandle");
+        field.setAccessible(true);
+        PendingAckHandleImpl pendingAckHandle = (PendingAckHandleImpl) 
field.get(persistentSubscription);
+        Field field1 = 
PendingAckHandleImpl.class.getDeclaredField("pendingAckStoreFuture");
+        field1.setAccessible(true);
+        PendingAckStore pendingAckStore = 
((CompletableFuture<PendingAckStore>) field1.get(pendingAckHandle)).get();
+
+        Field field3 = 
MLPendingAckStore.class.getDeclaredField("pendingAckLogIndex");
+        Field field4 = 
MLPendingAckStore.class.getDeclaredField("upperLimitOfLogAppendTimes");
+
+        field3.setAccessible(true);
+        field4.setAccessible(true);
+
+        ConcurrentSkipListMap<PositionImpl, PositionImpl> pendingAckLogIndex =
+                (ConcurrentSkipListMap<PositionImpl, PositionImpl>) 
field3.get(pendingAckStore);
+        long upperLimitOfLogAppendTimes = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(pendingAckLogIndex.size(), 1);

Review Comment:
   It will make the test unstable, if cursor persistentMarkerDeletePostion 
changed, this test will fail. So I suggest don't commit the txn and check the 
size and upperLimitOfLogAppendTimes



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/pendingack/impl/MLPendingAckStore.java:
##########
@@ -292,6 +269,42 @@ public void addFailed(ManagedLedgerException exception, 
Object ctx) {
         return completableFuture;
     }
 
+    private void clearUselessLogData() {
+        if (!pendingAckLogIndex.isEmpty()) {
+            PositionImpl deletePosition = null;
+            while (!pendingAckLogIndex.isEmpty()
+                    && pendingAckLogIndex.firstKey() != null
+                    && subManagedCursor.getPersistentMarkDeletedPosition() != 
null
+                    && pendingAckLogIndex.firstEntry().getKey()
+                    .compareTo((PositionImpl) 
subManagedCursor.getPersistentMarkDeletedPosition()) <= 0) {
+                deletePosition = pendingAckLogIndex.firstEntry().getValue();
+                pendingAckLogIndex.remove(pendingAckLogIndex.firstKey());
+            }
+            upperLimitOfLogAppendTimes = 
logIndexBackoff.next(pendingAckLogIndex.size());

Review Comment:
   ```
   if (deletePosition != null) {
   upperLimitOfLogAppendTimes = logIndexBackoff.next(pendingAckLogIndex.size());
   }
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/LogIndexBackoff.java:
##########
@@ -0,0 +1,52 @@
+/**
+ * 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.pulsar.broker.transaction;
+
+import org.apache.pulsar.client.api.RedeliveryBackoff;
+
+public class LogIndexBackoff implements RedeliveryBackoff {
+
+    private final long minLag;
+    private final long maxLag;
+
+    public LogIndexBackoff(long minLag, long maxLag) {
+        this.minLag = minLag;
+        this.maxLag = maxLag;
+    }
+
+    public long getMinLag() {
+        return this.minLag;
+    }
+
+    public long getMaxLag() {
+        return this.maxLag;
+    }
+
+    @Override
+    public long next(int indexCount) {
+        if (indexCount <= 0 || minLag <= 0) {
+            return this.minLag;

Review Comment:
   may this should return 0



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/pendingack/impl/MLPendingAckStore.java:
##########
@@ -323,26 +336,30 @@ public void run() {
                         // store the max position of this entry retain
                         if (pendingAckMetadataEntry.getPendingAckOp() != 
PendingAckOp.ABORT
                                 && pendingAckMetadataEntry.getPendingAckOp() 
!= PendingAckOp.COMMIT) {
+                            logAppendTimes.incrementAndGet();
                             Optional<PendingAckMetadata> optional = 
pendingAckMetadataEntry.getPendingAckMetadatasList()
                                     .stream().max((o1, o2) -> 
ComparisonChain.start().compare(o1.getLedgerId(),
                                             
o2.getLedgerId()).compare(o1.getEntryId(), o2.getEntryId()).result());
 
-                            optional.ifPresent(pendingAckMetadata ->
-                                    
metadataPositions.compute(PositionImpl.get(entry.getLedgerId(), 
entry.getEntryId()),
-                                            (thisPosition, otherPosition) -> {
-                                                PositionImpl nowPosition = 
PositionImpl
-                                                        
.get(pendingAckMetadata.getLedgerId(),
-                                                                
pendingAckMetadata.getEntryId());
-                                                if (otherPosition == null) {
-                                                    return nowPosition;
-                                                } else {
-                                                    return 
nowPosition.compareTo(otherPosition) > 0 ? nowPosition
-                                                            : otherPosition;
-                                                }
-                                            }));
+                            optional.ifPresent(pendingAckMetadata -> {
+                                PositionImpl logPosition = 
PositionImpl.get(entry.getLedgerId(), entry.getEntryId());
+                                PositionImpl nowPosition = 
PositionImpl.get(pendingAckMetadata.getLedgerId(),
+                                                
pendingAckMetadata.getEntryId());
+
+                                if (nowPosition.compareTo(maxAckPosition) > 0) 
{
+                                    maxAckPosition = nowPosition;
+                                }
+                                if (logAppendTimes.get() > 
upperLimitOfLogAppendTimes) {
+                                    pendingAckLogIndex.compute(maxAckPosition,
+                                            (thisPosition, otherPosition) -> 
logPosition);
+                                    upperLimitOfLogAppendTimes = 
logIndexBackoff.next(pendingAckLogIndex.size());
+                                    logAppendTimes.set(0);
+                                }

Review Comment:
   These codes are very similar to appendCommon



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/LogIndexBackoff.java:
##########
@@ -0,0 +1,52 @@
+/**
+ * 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.pulsar.broker.transaction;
+
+import org.apache.pulsar.client.api.RedeliveryBackoff;
+
+public class LogIndexBackoff implements RedeliveryBackoff {
+
+    private final long minLag;
+    private final long maxLag;
+
+    public LogIndexBackoff(long minLag, long maxLag) {

Review Comment:
   Add a double coefficient to make it more variable



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to