codelipenghui commented on code in PR #22245:
URL: https://github.com/apache/pulsar/pull/22245#discussion_r1542603523


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/KeySharedSubscriptionTest.java:
##########
@@ -1630,4 +1637,263 @@ public void 
testContinueDispatchMessagesWhenMessageDelayed() throws Exception {
         log.info("Got {} other messages...", sum);
         Assert.assertEquals(sum, delayedMessages + messages);
     }
+
+    private AtomicInteger injectReplayReadCounter(String topicName, String 
cursorName) throws Exception {
+        PersistentTopic persistentTopic =
+                (PersistentTopic) 
pulsar.getBrokerService().getTopic(topicName, false).join().get();
+        ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) 
persistentTopic.getManagedLedger();
+        ManagedCursorImpl cursor = (ManagedCursorImpl) 
managedLedger.openCursor(cursorName);
+        managedLedger.getCursors().removeCursor(cursor.getName());
+        managedLedger.getActiveCursors().removeCursor(cursor.getName());
+        ManagedCursorImpl spyCursor = Mockito.spy(cursor);
+        managedLedger.getCursors().add(spyCursor, PositionImpl.EARLIEST);
+        managedLedger.getActiveCursors().add(spyCursor, PositionImpl.EARLIEST);
+        AtomicInteger replyReadCounter = new AtomicInteger();
+        Mockito.doAnswer(invocation -> {
+            if 
(!String.valueOf(invocation.getArguments()[2]).equals("Normal")) {
+                replyReadCounter.incrementAndGet();
+            }
+            return invocation.callRealMethod();
+        }).when(spyCursor).asyncReplayEntries(Mockito.anySet(), Mockito.any(), 
Mockito.any());
+        Mockito.doAnswer(invocation -> {
+            if 
(!String.valueOf(invocation.getArguments()[2]).equals("Normal")) {
+                replyReadCounter.incrementAndGet();
+            }
+            return invocation.callRealMethod();
+        }).when(spyCursor).asyncReplayEntries(Mockito.anySet(), Mockito.any(), 
Mockito.any(), Mockito.anyBoolean());
+        admin.topics().createSubscription(topicName, cursorName, 
MessageId.earliest);
+        return replyReadCounter;
+    }
+
+    @Test
+    public void testNoRepeatedReadAndDiscard() throws Exception {
+        int delayedMessages = 100;
+        final String topic = 
BrokerTestUtil.newUniqueName("persistent://public/default/tp");
+        final String subName = "my-sub";
+        admin.topics().createNonPartitionedTopic(topic);
+        AtomicInteger replyReadCounter = injectReplayReadCounter(topic, 
subName);
+
+        // Send messages.
+        @Cleanup
+        Producer<Integer> producer = 
pulsarClient.newProducer(Schema.INT32).topic(topic).enableBatching(false).create();
+        for (int i = 0; i < delayedMessages; i++) {
+            MessageId messageId = producer.newMessage()
+                    .key(String.valueOf(random.nextInt(NUMBER_OF_KEYS)))
+                    .value(100 + i)
+                    .send();
+            log.info("Published delayed message :{}", messageId);
+        }
+        producer.close();
+
+        // Make ack holes.
+        Consumer<Integer> consumer1 = pulsarClient.newConsumer(Schema.INT32)
+                .topic(topic)
+                .subscriptionName(subName)
+                .receiverQueueSize(10)
+                .subscriptionType(SubscriptionType.Key_Shared)
+                .subscribe();
+        Consumer<Integer> consumer2 = pulsarClient.newConsumer(Schema.INT32)
+                .topic(topic)
+                .subscriptionName(subName)
+                .receiverQueueSize(10)
+                .subscriptionType(SubscriptionType.Key_Shared)
+                .subscribe();
+        List<Message> msgList1 = new ArrayList<>();
+        List<Message> msgList2 = new ArrayList<>();
+        for (int i = 0; i < 10; i++) {
+            Message msg1 = consumer1.receive(1, TimeUnit.SECONDS);
+            if (msg1 != null) {
+                msgList1.add(msg1);
+            }
+            Message msg2 = consumer2.receive(1, TimeUnit.SECONDS);
+            if (msg2 != null) {
+                msgList2.add(msg2);
+            }
+        }
+        Consumer<Integer> redeliverConsumer = null;
+        if (!msgList1.isEmpty()) {
+            msgList1.forEach(msg -> consumer1.acknowledgeAsync(msg));
+            redeliverConsumer = consumer2;
+        } else {
+            msgList2.forEach(msg -> consumer2.acknowledgeAsync(msg));
+            redeliverConsumer = consumer1;
+        }

Review Comment:
   Actually, it can't ensure the ack hole is introduced. The second consumer 
might not able to get any messages until the first consumer acked all the 
messages.



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