You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2026/10/19 22:34:19 UTC
(pulsar) branch master updated: [fix][broker] Don't dispatch a replay read that was pending when all consumers of a Shared subscription disconnected (#27007)
This is an automated email from the ASF dual-hosted git repository.
merlimat pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new dfdc781edbca [fix][broker] Don't dispatch a replay read that was pending when all consumers of a Shared subscription disconnected (#27007)
dfdc781edbca is described below
commit dfdc781edbcab7245e8c333d804bb542d1ee862f
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Mon Oct 19 15:34:11 2026 -0700
[fix][broker] Don't dispatch a replay read that was pending when all consumers of a Shared subscription disconnected (#27007)
---
.../PersistentDispatcherMultipleConsumers.java | 25 +++--
...ntStickyKeyDispatcherMultipleConsumersTest.java | 111 +++++++++++++++++++++
2 files changed, 126 insertions(+), 10 deletions(-)
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
index 88e9577f7c4e..6592e169fde1 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java
@@ -388,6 +388,16 @@ public class PersistentDispatcherMultipleConsumers extends AbstractPersistentDis
log.debug("Skipping read due to sending in-progress");
return;
}
+ if (shouldRewindBeforeReadingOrReplaying) {
+ if (havePendingRead || havePendingReplayRead) {
+ // All consumers disconnected while this read was pending: its entries are discarded when it ends,
+ // and the cursor is rewound only then, so that it can't dispatch what the rewind reads again
+ log.debug("Skipping read until the read pending before all consumers disconnected ends");
+ return;
+ }
+ cursor.rewind();
+ shouldRewindBeforeReadingOrReplaying = false;
+ }
if (shouldPauseDeliveryForDelayTracker()) {
log.debug("Skipping read due to pause delivery for delay tracker");
return;
@@ -759,12 +769,12 @@ public class PersistentDispatcherMultipleConsumers extends AbstractPersistentDis
readFailureBackoff.reduceToHalf();
- if (shouldRewindBeforeReadingOrReplaying && readType == ReadType.Normal) {
- // All consumers got disconnected before the completion of the read operation
+ if (shouldRewindBeforeReadingOrReplaying) {
+ // All consumers got disconnected before the completion of the read operation. A replay read's entries
+ // are discarded too: the redelivery queue was cleared, and the rewind reads them again. The next read
+ // rewinds the cursor once no read is pending.
entries.forEach(Entry::release);
- onReadEnded(ReadType.Normal);
- cursor.rewind();
- shouldRewindBeforeReadingOrReplaying = false;
+ onReadEnded(readType);
readMoreEntriesAsync();
return;
}
@@ -1108,11 +1118,6 @@ public class PersistentDispatcherMultipleConsumers extends AbstractPersistentDis
.log("Error reading entries - Retrying to read");
}
- if (shouldRewindBeforeReadingOrReplaying) {
- shouldRewindBeforeReadingOrReplaying = false;
- cursor.rewind();
- }
-
onReadEnded(readType);
if (readType == ReadType.Normal) {
havePendingRead = false;
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumersTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumersTest.java
index cac885b7c424..6a4575ac6284 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumersTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentStickyKeyDispatcherMultipleConsumersTest.java
@@ -97,6 +97,7 @@ import org.apache.pulsar.common.protocol.Commands;
import org.apache.pulsar.common.protocol.Markers;
import org.awaitility.Awaitility;
import org.mockito.ArgumentCaptor;
+import org.mockito.stubbing.Answer;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
@@ -1469,6 +1470,116 @@ public class PersistentStickyKeyDispatcherMultipleConsumersTest {
}
}
+ /**
+ * A replay read is in flight when the subscription's last consumer disconnects and a new one connects. The
+ * cursor is rewound for the new consumer, so the replay read's entry must not be dispatched as well.
+ */
+ @Test(dataProvider = "writableSubscriptionTypes", timeOut = 30000)
+ public void testReplayReadPendingWhenAllConsumersDisconnectIsNotDispatchedAgain(boolean keyShared)
+ throws Exception {
+ persistentDispatcher.close();
+ PersistentDispatcherMultipleConsumers dispatcher = createWritableTestDispatcher(keyShared);
+ doReturn(PositionFactory.create(1, 0)).when(cursorMock).getMarkDeletedPosition();
+ Entry redelivered = createEntry(1, 1, "message1", 1);
+ Entry published = createEntry(1, 2, "message2", 2);
+
+ // The cursor's entries and read position: a normal read gets the entries past the read position, or waits
+ // for the next one, and a rewind moves the read position back to the first entry.
+ List<Entry> ledgerEntries = new ArrayList<>(List.of(redelivered));
+ AtomicInteger readIndex = new AtomicInteger();
+ List<Runnable> waitingReads = new ArrayList<>();
+ doAnswer(invocation -> {
+ AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(2);
+ Object ctx = invocation.getArgument(3);
+ List<Entry> entries;
+ synchronized (ledgerEntries) {
+ if (readIndex.get() == ledgerEntries.size()) {
+ waitingReads.add(() -> {
+ List<Entry> newEntries;
+ synchronized (ledgerEntries) {
+ newEntries = copyEntries(ledgerEntries.subList(readIndex.get(), ledgerEntries.size()));
+ readIndex.set(ledgerEntries.size());
+ }
+ callback.readEntriesComplete(newEntries, ctx);
+ });
+ return null;
+ }
+ entries = copyEntries(ledgerEntries.subList(readIndex.get(), ledgerEntries.size()));
+ readIndex.set(ledgerEntries.size());
+ }
+ callback.readEntriesComplete(entries, ctx);
+ return null;
+ }).when(cursorMock).asyncReadEntriesWithSkipOrWait(anyInt(), anyLong(), any(), any(), any(), any());
+ doAnswer(invocation -> {
+ synchronized (ledgerEntries) {
+ readIndex.set(0);
+ }
+ return null;
+ }).when(cursorMock).rewind();
+ doAnswer(invocation -> {
+ synchronized (ledgerEntries) {
+ boolean cancelled = !waitingReads.isEmpty();
+ waitingReads.clear();
+ return cancelled;
+ }
+ }).when(cursorMock).cancelPendingReadRequest();
+
+ // the replay read stays in flight until the test completes it
+ CompletableFuture<Runnable> replayRead = new CompletableFuture<>();
+ Answer<Set<Position>> replay = invocation -> {
+ AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
+ Object ctx = invocation.getArgument(2);
+ replayRead.complete(() -> callback.readEntriesComplete(copyEntries(List.of(redelivered)), ctx));
+ return Collections.emptySet();
+ };
+ doAnswer(replay).when(cursorMock).asyncReplayEntries(anySet(), any(), any());
+ doAnswer(replay).when(cursorMock).asyncReplayEntries(anySet(), any(), any(), anyBoolean());
+
+ Consumer consumerA = createMockConsumer();
+ doReturn("a").when(consumerA).consumerName();
+ doReturn(true).when(consumerA).isWritable();
+ doReturn(1000).when(consumerA).getAvailablePermits();
+ mockSendMessages(consumerA, null);
+ Consumer consumerB = createMockConsumer();
+ doReturn("b").when(consumerB).consumerName();
+ doReturn(true).when(consumerB).isWritable();
+ doReturn(1000).when(consumerB).getAvailablePermits();
+ List<Position> receivedByB = new CopyOnWriteArrayList<>();
+ mockSendMessages(consumerB, entries -> entries.forEach(entry -> receivedByB.add(entry.getPosition())));
+
+ try {
+ dispatcher.addConsumer(consumerA).join();
+ // a has already received the entry when it asks for its redelivery
+ synchronized (ledgerEntries) {
+ readIndex.set(1);
+ }
+ dispatcher.redeliverUnacknowledgedMessages(consumerA, List.of(redelivered.getPosition()));
+ Runnable completeReplayRead = replayRead.get(5, TimeUnit.SECONDS);
+
+ // a disconnects and b connects while the replay read is in flight
+ dispatcher.removeConsumer(consumerA);
+ dispatcher.addConsumer(consumerB).join();
+ completeReplayRead.run();
+
+ // a new entry completes the normal read that waits for one
+ List<Runnable> readsToComplete;
+ synchronized (ledgerEntries) {
+ ledgerEntries.add(published);
+ readsToComplete = new ArrayList<>(waitingReads);
+ waitingReads.clear();
+ }
+ readsToComplete.forEach(Runnable::run);
+
+ Awaitility.await().untilAsserted(() -> assertThat(receivedByB).contains(published.getPosition()));
+ assertThat(receivedByB).as("each entry once, from the rewound cursor")
+ .containsExactly(redelivered.getPosition(), published.getPosition());
+ } finally {
+ dispatcher.close();
+ redelivered.release();
+ published.release();
+ }
+ }
+
@Test(dataProvider = "writableSubscriptionTypes", timeOut = 30000)
public void testUnwritableReadCompletionWaitsForNotification(boolean keyShared) throws Exception {
persistentDispatcher.close();