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


##########
pulsar-broker/src/test/java/org/apache/pulsar/utils/LogIndexLagBackOffTest.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.utils;
+
+import org.apache.pulsar.broker.transaction.util.LogIndexLagBackoff;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test(groups = "utils")
+public class LogIndexLagBackOffTest {
+    @Test
+    public void testGenerateNextLogIndexLag() {
+        LogIndexLagBackoff logIndexLagBackoff = new LogIndexLagBackoff(1, 10, 
1);
+        Assert.assertEquals(logIndexLagBackoff.next(0), 1);
+        Assert.assertEquals(logIndexLagBackoff.next(6), 6);
+
+        Assert.assertEquals(logIndexLagBackoff.next(77), 10);
+
+        logIndexLagBackoff = new LogIndexLagBackoff(1, 10, 2);
+        Assert.assertEquals(logIndexLagBackoff.next(3), 9);
+
+        try {
+            new LogIndexLagBackoff(- 1, 2, 3);

Review Comment:
   ```suggestion
               new LogIndexLagBackoff(- 1, 2, 3);
   ```
               new LogIndexLagBackoff(-1, 2, 3);



##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/pendingack/PendingAckPersistentTest.java:
##########
@@ -345,4 +350,104 @@ 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).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("maxIndexLag");
+
+        field3.setAccessible(true);
+        field4.setAccessible(true);
+
+        ConcurrentSkipListMap<PositionImpl, PositionImpl> pendingAckLogIndex =
+                (ConcurrentSkipListMap<PositionImpl, PositionImpl>) 
field3.get(pendingAckStore);
+        long maxIndexLag = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(pendingAckLogIndex.size(), 0);
+        Assert.assertEquals(maxIndexLag, 5);
+        transaction.commit().get();
+
+        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();
+        Message<byte[]> message0 = null;
+        //remove previous index
+        for (int i = 0; i < 4; i++) {
+            message0 = consumer.receive(5, TimeUnit.SECONDS);
+            consumer.acknowledgeAsync(message0.getMessageId(), 
transaction1).get();
+        }
+        Assert.assertEquals(pendingAckLogIndex.size(), 1);
+        maxIndexLag = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(maxIndexLag, 5);
+        //add new index
+        for (int i = 0; i < 9; i++) {
+            message0= consumer.receive(5, TimeUnit.SECONDS);
+            consumer.acknowledgeAsync(message0.getMessageId(), 
transaction1).get();
+        }
+
+        Assert.assertEquals(pendingAckLogIndex.size(), 2);
+        maxIndexLag = (long) field4.get(pendingAckStore);
+        Assert.assertEquals(maxIndexLag, 10);
+
+        transaction1.commit().get();
+        Message<byte[]> message1 = message0;
+        Awaitility.await().untilAsserted(() ->
+                
Assert.assertEquals(persistentSubscription.getCursor().getPersistentMarkDeletedPosition().getEntryId(),
+                        
((MessageIdImpl)message1.getMessageId()).getEntryId()));
+
+        Transaction transaction2 = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.SECONDS)
+                .build()
+                .get();
+        Message<byte[]> message2 = consumer.receive(5, TimeUnit.SECONDS);
+        consumer.acknowledgeAsync(message2.getMessageId(), transaction2).get();
+
+        Assert.assertEquals(pendingAckLogIndex.size(), 0);

Review Comment:
   should check the pendingAck cursor markDelete



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