You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "namest504 (via GitHub)" <gi...@apache.org> on 2026/10/23 06:06:11 UTC
[PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
namest504 opened a new pull request, #27062:
URL: https://github.com/apache/pulsar/pull/27062
### Motivation
`ConsumerDedupPermitsUpdateTest#testConsumerDedup` fails on master since #21928 (4060ccee89), in about half of the runs locally and in the `CI - Unit - Brokers - Client Impl` job of #27061:
```
java.lang.AssertionError: expected [new-message-0] but found [hello-0]
at org.apache.pulsar.client.impl.ConsumerDedupPermitsUpdateTest.testConsumerDedup(ConsumerDedupPermitsUpdateTest.java:102)
```
The parent of 4060ccee89 passes the class 3 times out of 3 and 6914b1d85c 4 out of 4; 4060ccee89 and current master fail it in 1 of 3 and 4 of 5 runs.
#21928 flushes the acks that waited for the subscription once the consumer is subscribed again. It placed the flush after the flow command. The broker starts dispatching as soon as it gets the permits, so it redelivers the messages whose acks are still on their way, and by the time they arrive the flush has already emptied the pending acks that `isDuplicate` checks. The client cannot tell them apart from new messages anymore and hands the duplicates to the application. The test keeps its acks pending for an hour on purpose, to check exactly this deduplication, so it shows the race most of the time.
### Modifications
- `ConsumerImpl#connectionOpened`: flush the pending acks before sending the flow command. Both go through the same connection, so the broker processes the acks before it dispatches and does not redeliver the acknowledged messages. The deduplication in the client is then not needed for them.
### Verifying this change
- [x] Make sure that the change passes the CI checks.
With the change, `ConsumerDedupPermitsUpdateTest` passes 5 runs out of 5 (before: 4 of 5 fail on master). `ConsumerAckTest`, which holds the tests added by #21928, and `AcknowledgementsGroupingTrackerTest` pass.
### Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API: no
- The schema: no
- The default values of configurations: no
- The threading model: no
- The binary protocol: no
- The REST endpoints: no
- The admin CLI options: no
- The metrics: no
- Anything that affects deployment: no
### Documentation
- [ ] `doc-required`
- [x] `doc-not-needed`
- [ ] `doc`
- [ ] `doc-complete`
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #27062:
URL: https://github.com/apache/pulsar/pull/27062#discussion_r4364447524
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -985,13 +985,14 @@ public CompletableFuture<Void> connectionOpened(final ClientCnx cnx) {
resetBackoff();
boolean firstTimeConnect = subscribeFuture.complete(this);
+ // Send the acks that waited for the subscription before the flow command, so the broker has them
+ // when it starts dispatching and does not redeliver the acknowledged messages.
+ acknowledgmentsGroupingTracker.flush();
Review Comment:
Follow-up: #27087 moves the flush right after `changeToReadyState()`, ahead of the epoch redeliver from #20032 and `consumerIsReconnectedToBroker()`.
##########
pulsar-client/src/test/java/org/apache/pulsar/client/impl/ConsumerImplTest.java:
##########
@@ -577,6 +583,46 @@ public void testConnectionOpenedAfterConsumerCloseReleasesConnection() {
verify(connectionPool).releaseConnection(clientCnx);
}
+ @Test
+ public void testReconnectSendsPendingAcksBeforeFlowPermits() {
+ // The acks grouped before the reconnection must reach the broker before the permits. The broker dispatches
+ // as soon as it has permits, and the flush empties the pending acks that the duplicate check uses, so the
+ // messages it would redeliver before the acks arrive would reach the application again.
+ consumerConf = new ConsumerConfigurationData<>();
+ consumerConf.setAcknowledgementsGroupTimeMicros(TimeUnit.HOURS.toMicros(1));
+ createConsumer(consumerConf, PERSISTENT_TOPIC, null, Schema.BYTES);
+ consumer.acknowledgeAsync(new MessageIdImpl(1, 1, -1)).join();
+
+ ClientCnx cnx = ClientTestFixtures.mockClientCnx();
+ doReturn(Commands.getCurrentProtocolVersion()).when(cnx).getRemoteEndpointProtocolVersion();
+ ChannelHandlerContext ctx = cnx.ctx();
+ List<BaseCommand.Type> written = new ArrayList<>();
+ ChannelHandlerContext recordingCtx = mock(ChannelHandlerContext.class);
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0));
+ }).when(recordingCtx).writeAndFlush(any());
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0), invocation.getArgument(1));
+ }).when(recordingCtx).writeAndFlush(any(), any());
+ doReturn(recordingCtx).when(cnx).ctx();
+
+ consumer.setState(HandlerState.State.Connecting);
+ consumer.connectionOpened(cnx).join();
+
+ assertThat(written).containsExactly(BaseCommand.Type.ACK, BaseCommand.Type.FLOW);
Review Comment:
The zero-queue variant is in #27087: `testZeroQueueReconnectSendsPendingAcksBeforeFlowPermits` records `[FLOW, ACK]` on master and `[ACK, FLOW]` with the move.
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "codelipenghui (via GitHub)" <gi...@apache.org>.
codelipenghui commented on code in PR #27062:
URL: https://github.com/apache/pulsar/pull/27062#discussion_r4361834815
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -985,13 +985,14 @@ public CompletableFuture<Void> connectionOpened(final ClientCnx cnx) {
resetBackoff();
boolean firstTimeConnect = subscribeFuture.complete(this);
+ // Send the acks that waited for the subscription before the flow command, so the broker has them
+ // when it starts dispatching and does not redeliver the acknowledged messages.
+ acknowledgmentsGroupingTracker.flush();
Review Comment:
This flush still doesn't run before every FLOW on the new connection. For a `ZeroQueueConsumerImpl`, `consumerIsReconnectedToBroker(cnx, currentSize)` (in the synchronized block above) has already sent permits for a pending `receive()`/`receiveAsync()` or an idle listener. The order there is FLOW, ACK, so the duplicate can still reach the application.
Could the flush move into the synchronized block, right after `changeToReadyState()` and before `consumerIsReconnectedToBroker()`?
```java
synchronized (ConsumerImpl.this) {
if (changeToReadyState()) {
// Send the acks that waited for the subscription before any flow permits, including the ones
// consumerIsReconnectedToBroker() sends for a zero-queue consumer.
acknowledgmentsGroupingTracker.flush();
consumerIsReconnectedToBroker(cnx, currentSize);
} else {
```
That also covers the other permit paths that take the consumer lock and can run between the switch to `Ready` and this line (e.g. `ZeroQueueConsumerImpl.internalReceiveAsync`). It doesn't add a new lock order: `closeAsync()` is synchronized and already flushes the tracker through `closeConsumerTasks()`.
##########
pulsar-client/src/test/java/org/apache/pulsar/client/impl/ConsumerImplTest.java:
##########
@@ -577,6 +583,46 @@ public void testConnectionOpenedAfterConsumerCloseReleasesConnection() {
verify(connectionPool).releaseConnection(clientCnx);
}
+ @Test
+ public void testReconnectSendsPendingAcksBeforeFlowPermits() {
+ // The acks grouped before the reconnection must reach the broker before the permits. The broker dispatches
+ // as soon as it has permits, and the flush empties the pending acks that the duplicate check uses, so the
+ // messages it would redeliver before the acks arrive would reach the application again.
+ consumerConf = new ConsumerConfigurationData<>();
+ consumerConf.setAcknowledgementsGroupTimeMicros(TimeUnit.HOURS.toMicros(1));
+ createConsumer(consumerConf, PERSISTENT_TOPIC, null, Schema.BYTES);
+ consumer.acknowledgeAsync(new MessageIdImpl(1, 1, -1)).join();
+
+ ClientCnx cnx = ClientTestFixtures.mockClientCnx();
+ doReturn(Commands.getCurrentProtocolVersion()).when(cnx).getRemoteEndpointProtocolVersion();
+ ChannelHandlerContext ctx = cnx.ctx();
+ List<BaseCommand.Type> written = new ArrayList<>();
+ ChannelHandlerContext recordingCtx = mock(ChannelHandlerContext.class);
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0));
+ }).when(recordingCtx).writeAndFlush(any());
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0), invocation.getArgument(1));
+ }).when(recordingCtx).writeAndFlush(any(), any());
+ doReturn(recordingCtx).when(cnx).ctx();
+
+ consumer.setState(HandlerState.State.Connecting);
+ consumer.connectionOpened(cnx).join();
+
+ assertThat(written).containsExactly(BaseCommand.Type.ACK, BaseCommand.Type.FLOW);
Review Comment:
Could you add a zero-queue variant of this test? Use the same setup, plus `consumerConf.setReceiverQueueSize(0)` and `consumer.pendingReceives.add(new CompletableFuture<>())` before `connectionOpened(cnx)`. On this head it records `[FLOW, ACK]`, because `ZeroQueueConsumerImpl.consumerIsReconnectedToBroker()` sends its permit before the flush.
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat merged PR #27062:
URL: https://github.com/apache/pulsar/pull/27062
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #27062:
URL: https://github.com/apache/pulsar/pull/27062#discussion_r4363321458
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -985,13 +985,14 @@ public CompletableFuture<Void> connectionOpened(final ClientCnx cnx) {
resetBackoff();
boolean firstTimeConnect = subscribeFuture.complete(this);
+ // Send the acks that waited for the subscription before the flow command, so the broker has them
+ // when it starts dispatching and does not redeliver the acknowledged messages.
+ acknowledgmentsGroupingTracker.flush();
Review Comment:
The follow-up with the flush move and the zero-queue test is #27087.
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #27062:
URL: https://github.com/apache/pulsar/pull/27062#discussion_r4363071515
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -985,13 +985,14 @@ public CompletableFuture<Void> connectionOpened(final ClientCnx cnx) {
resetBackoff();
boolean firstTimeConnect = subscribeFuture.complete(this);
+ // Send the acks that waited for the subscription before the flow command, so the broker has them
+ // when it starts dispatching and does not redeliver the acknowledged messages.
+ acknowledgmentsGroupingTracker.flush();
Review Comment:
You're right: `ZeroQueueConsumerImpl.consumerIsReconnectedToBroker()` sends its permits before this flush, so a zero-queue consumer with a pending receive or an idle listener still writes FLOW before ACK. This PR fixes the `ConsumerDedupPermitsUpdateTest` failures that every open PR is hitting in "CI - Unit - Brokers - Client Impl", so I'd like to merge it as is. I'll move the flush right after `changeToReadyState()`, as you suggest, in a follow-up PR and link it here.
##########
pulsar-client/src/test/java/org/apache/pulsar/client/impl/ConsumerImplTest.java:
##########
@@ -577,6 +583,46 @@ public void testConnectionOpenedAfterConsumerCloseReleasesConnection() {
verify(connectionPool).releaseConnection(clientCnx);
}
+ @Test
+ public void testReconnectSendsPendingAcksBeforeFlowPermits() {
+ // The acks grouped before the reconnection must reach the broker before the permits. The broker dispatches
+ // as soon as it has permits, and the flush empties the pending acks that the duplicate check uses, so the
+ // messages it would redeliver before the acks arrive would reach the application again.
+ consumerConf = new ConsumerConfigurationData<>();
+ consumerConf.setAcknowledgementsGroupTimeMicros(TimeUnit.HOURS.toMicros(1));
+ createConsumer(consumerConf, PERSISTENT_TOPIC, null, Schema.BYTES);
+ consumer.acknowledgeAsync(new MessageIdImpl(1, 1, -1)).join();
+
+ ClientCnx cnx = ClientTestFixtures.mockClientCnx();
+ doReturn(Commands.getCurrentProtocolVersion()).when(cnx).getRemoteEndpointProtocolVersion();
+ ChannelHandlerContext ctx = cnx.ctx();
+ List<BaseCommand.Type> written = new ArrayList<>();
+ ChannelHandlerContext recordingCtx = mock(ChannelHandlerContext.class);
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0));
+ }).when(recordingCtx).writeAndFlush(any());
+ doAnswer(invocation -> {
+ written.add(commandType(invocation.getArgument(0)));
+ return ctx.writeAndFlush(invocation.getArgument(0), invocation.getArgument(1));
+ }).when(recordingCtx).writeAndFlush(any(), any());
+ doReturn(recordingCtx).when(cnx).ctx();
+
+ consumer.setState(HandlerState.State.Connecting);
+ consumer.connectionOpened(cnx).join();
+
+ assertThat(written).containsExactly(BaseCommand.Type.ACK, BaseCommand.Type.FLOW);
Review Comment:
I'll add the zero-queue variant in the same follow-up, together with the flush move.
--
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
Re: [PR] [fix][client] Flush the pending acks before the flow command when the consumer reconnects [pulsar]
Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on code in PR #27062:
URL: https://github.com/apache/pulsar/pull/27062#discussion_r4363321458
##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java:
##########
@@ -985,13 +985,14 @@ public CompletableFuture<Void> connectionOpened(final ClientCnx cnx) {
resetBackoff();
boolean firstTimeConnect = subscribeFuture.complete(this);
+ // Send the acks that waited for the subscription before the flow command, so the broker has them
+ // when it starts dispatching and does not redeliver the acknowledged messages.
+ acknowledgmentsGroupingTracker.flush();
Review Comment:
The follow-up with the flush move and the zero-queue test is #27085.
--
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