You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "nodece (via GitHub)" <gi...@apache.org> on 2026/09/01 03:56:47 UTC
Re: [PR] [fix][broker] Clear delayed delivery state before resetting the cursor [pulsar]
nodece commented on code in PR #26420:
URL: https://github.com/apache/pulsar/pull/26420#discussion_r3900643365
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -983,88 +983,83 @@ private CompletableFuture<Void> resetCursorInternal(Position finalPosition, Comp
}
}
- disconnectFuture.whenComplete((aVoid, throwable) -> {
- if (dispatcher != null) {
- dispatcher.resetCloseFuture();
- }
-
- if (throwable != null) {
- log.error()
- .exception(throwable)
- .log("Failed to disconnect consumer from subscription");
- IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
- inProgressResetCursorFuture = null;
- future.completeExceptionally(
- new SubscriptionBusyException("Failed to disconnect consumers from subscription"));
- return;
- }
-
- log.info()
- .log("Successfully disconnected consumers from subscription, proceeding with cursor reset");
-
- CompletableFuture<Boolean> forceReset = new CompletableFuture<>();
- if (topic.getTopicCompactionService() == null) {
- forceReset.complete(false);
- } else {
- topic.getTopicCompactionService().getLastCompactedPosition().thenAccept(lastCompactedPosition -> {
- Position resetTo = finalPosition;
- if (lastCompactedPosition != null && resetTo.compareTo(lastCompactedPosition.getLedgerId(),
- lastCompactedPosition.getEntryId()) <= 0) {
- forceReset.complete(true);
- } else {
+ disconnectFuture
+ .thenCompose(__ -> {
+ log.info()
+ .log("Successfully disconnected consumers from subscription, proceeding with cursor reset");
+ if (dispatcher != null) {
+ dispatcher.resetCloseFuture();
+ return dispatcher.clearDelayedMessages();
+ }
+ return CompletableFuture.completedFuture(null);
Review Comment:
Fixed in f16e705c120. I was originally fine with the skip because re-added messages dedup through the index bitmap, but that argument only holds for reset-to-earliest: getScheduledMessages filters by the ledger GC boundary, not by the reset position, so with a reset to a later position the recovered snapshots would still hand out the skipped messages. The no-dispatcher branch now runs cleanResidualSnapshots(cursor) behind the same guard as the unsubscribe path, and testResetCursorWithoutDispatcherCleansResidualBucketSnapshots covers it (close the consumer, unload the topic, reset, assert the properties are gone).
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -983,88 +983,83 @@ private CompletableFuture<Void> resetCursorInternal(Position finalPosition, Comp
}
}
- disconnectFuture.whenComplete((aVoid, throwable) -> {
- if (dispatcher != null) {
- dispatcher.resetCloseFuture();
- }
-
- if (throwable != null) {
- log.error()
- .exception(throwable)
- .log("Failed to disconnect consumer from subscription");
- IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
- inProgressResetCursorFuture = null;
- future.completeExceptionally(
- new SubscriptionBusyException("Failed to disconnect consumers from subscription"));
- return;
- }
-
- log.info()
- .log("Successfully disconnected consumers from subscription, proceeding with cursor reset");
-
- CompletableFuture<Boolean> forceReset = new CompletableFuture<>();
- if (topic.getTopicCompactionService() == null) {
- forceReset.complete(false);
- } else {
- topic.getTopicCompactionService().getLastCompactedPosition().thenAccept(lastCompactedPosition -> {
- Position resetTo = finalPosition;
- if (lastCompactedPosition != null && resetTo.compareTo(lastCompactedPosition.getLedgerId(),
- lastCompactedPosition.getEntryId()) <= 0) {
- forceReset.complete(true);
- } else {
+ disconnectFuture
+ .thenCompose(__ -> {
+ log.info()
+ .log("Successfully disconnected consumers from subscription, proceeding with cursor reset");
+ if (dispatcher != null) {
+ dispatcher.resetCloseFuture();
+ return dispatcher.clearDelayedMessages();
Review Comment:
Right, and it actually bit me while testing the reconnect fix in the test below. With the consumer closed before the reset, the full BucketDelayedDeliveryTest suite fails for me every single time: the property removal from the bucket delete still races asyncResetCursor persistence and the reset fails with BadVersion ("unable to persist readPosition for cursor reset"). Same pre-existing clear() gap you describe here. #26401 (still open) is where this gets fixed - picking 7f22e565373 on top of this branch makes the suite pass again, verified locally. I would rather not duplicate that work in this PR, so the plan is to rebase onto #26401 once it lands.
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/BucketDelayedDeliveryTest.java:
##########
@@ -128,6 +128,51 @@ public void testBucketDelayedDeliveryWithAllConsumersDisconnecting() throws Exce
Assert.assertEquals(bucketKeys, bucketKeys2);
}
+ @Test
+ public void testResetCursorClearsDelayedMessages() throws Exception {
+ String topic = BrokerTestUtil.newUniqueName("persistent://public/default/testResetClearsDelayed");
+
+ @Cleanup
+ Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
+ .topic(topic)
+ .subscriptionName("sub")
+ .subscriptionType(SubscriptionType.Shared)
+ .subscribe();
+
+ @Cleanup
+ Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
+ .topic(topic)
+ .create();
+
+ for (int i = 0; i < 100; i++) {
+ producer.newMessage()
+ .value("msg")
+ .deliverAfter(1, TimeUnit.HOURS)
+ .send();
+ }
+
+ Dispatcher dispatcher = pulsar.getBrokerService().getTopicReference(topic)
+ .get().getSubscription("sub").getDispatcher();
+ Awaitility.await().untilAsserted(() ->
+ Assert.assertEquals(dispatcher.getNumberOfDelayedMessages(), 100));
+ List<String> bucketKeys =
+ ((AbstractPersistentDispatcherMultipleConsumers) dispatcher).getCursor().getCursorProperties()
+ .keySet().stream().filter(x -> x.startsWith(CURSOR_INTERNAL_PROPERTY_PREFIX)).toList();
+ assertFalse(bucketKeys.isEmpty());
+
+ // Resetting the cursor disconnects the consumer, so nothing gets re-tracked while we
+ // observe the post-reset state.
+ admin.topics().resetCursor(topic, "sub", MessageId.earliest);
+
+ assertEquals(dispatcher.getNumberOfDelayedMessages(), 0,
Review Comment:
Done in f16e705c120, consumer.close() before the reset. The dispatcher survives the disconnect, so the reset still clears the tracker it populated and the test still fails on master without the fix. Closing the consumer first is also what flushed out the clear() race from the comment above, so the ordering matters beyond the assertions.
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -983,88 +983,83 @@ private CompletableFuture<Void> resetCursorInternal(Position finalPosition, Comp
}
}
- disconnectFuture.whenComplete((aVoid, throwable) -> {
- if (dispatcher != null) {
- dispatcher.resetCloseFuture();
- }
-
- if (throwable != null) {
- log.error()
- .exception(throwable)
- .log("Failed to disconnect consumer from subscription");
- IS_FENCED_UPDATER.set(PersistentSubscription.this, FALSE);
- inProgressResetCursorFuture = null;
- future.completeExceptionally(
- new SubscriptionBusyException("Failed to disconnect consumers from subscription"));
- return;
- }
-
- log.info()
- .log("Successfully disconnected consumers from subscription, proceeding with cursor reset");
-
- CompletableFuture<Boolean> forceReset = new CompletableFuture<>();
- if (topic.getTopicCompactionService() == null) {
- forceReset.complete(false);
- } else {
- topic.getTopicCompactionService().getLastCompactedPosition().thenAccept(lastCompactedPosition -> {
- Position resetTo = finalPosition;
- if (lastCompactedPosition != null && resetTo.compareTo(lastCompactedPosition.getLedgerId(),
- lastCompactedPosition.getEntryId()) <= 0) {
- forceReset.complete(true);
- } else {
+ disconnectFuture
+ .thenCompose(__ -> {
Review Comment:
I dropped it on purpose since disconnectActiveConsumers can not really fail (closeFuture is only ever completed with complete(null)), but keeping the old behavior costs nothing. f16e705c120 restores it: a handle() in front of the chain does resetCloseFuture() and fails with SubscriptionBusyException when the disconnect fails. The terminal exceptionally() also unwraps the CompletionException now and lets BrokerServiceException types through instead of wrapping them again.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org