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


##########
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 should think about cursor persistentMarkerDeletePostion changed



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