Skip to content

[fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions (streaming dispatcher) - #17163

Merged
eolivelli merged 2 commits into
apache:masterfrom
nicoloboschi:fix-dispatcher-replay-twice
Aug 20, 2022
Merged

[fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions (streaming dispatcher) #17163
eolivelli merged 2 commits into
apache:masterfrom
nicoloboschi:fix-dispatcher-replay-twice

Conversation

@nicoloboschi

@nicoloboschi nicoloboschi commented Aug 18, 2022

Copy link
Copy Markdown
Contributor

Motivation

There's a test failing constantly in the flaky suite:
#16795

While investigating, I found out a case when it's possible that the dispatcher replays twice the same message to the consumer. This may happens because the message's replay is async and it's done while reading entries.

I believe that it may be related to recent changes to the dispatcher:

Modifications

  • Added the same guard inside readMoreEntries for the streaming dispatcher

The proof that this fix works is that now the test SimpleProducerConsumerTest.testSharedSamePriorityConsumer doesn't fail anymore.

Fix #16795

  • doc-not-needed

@eolivelli eolivelli added this to the 2.11.0 milestone Aug 18, 2022
@eolivelli eolivelli added the release/blocker Indicate the PR or issue that should block the release until it gets resolved label Aug 18, 2022

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch !

we need more eyes on this patch

@Technoboy- @codelipenghui @hangc0276 @merlimat @lhotari PTAL

@nicoloboschi

Copy link
Copy Markdown
Contributor Author

@eolivelli thanks for the review. The PersistentStickyKeyDispatcherMultipleConsumers had the same issue. I fixed it and added a test

@mattisonchao

Copy link
Copy Markdown
Member

I think we have to know the root cause.

@codelipenghui

Copy link
Copy Markdown
Contributor

I think we have to know the root cause.

+1, we should handle this PR with extra care. It will filter out some messages,
This may cause the message to not be delivered correctly to the client under some conditions.

@mattisonchao

Copy link
Copy Markdown
Member

Hi, @nicoloboschi

The proof that this fix works is that now the test SimpleProducerConsumerTest.testSharedSamePriorityConsumer doesn't fail anymore.

I think it should be SimpleProducerConsumerTestStreamingDispatcherTest.testSharedSamePriorityConsumer

By the way, I want to collect some tests that need to move out of the flaky-test group... Because in the current time we may lose some test coverage.
See #17145

@mattisonchao

mattisonchao commented Aug 19, 2022

Copy link
Copy Markdown
Member

After the test, I think some race conditions in multi-thread should cause it.
I try to disable this configuration:
dispatcherDispatchMessagesInSubscriptionThread run the test 30 times, everything got fine.

image

Controlled experiment:

image

@codelipenghui

Copy link
Copy Markdown
Contributor

Thanks @mattisonchao

I think we should point out the race condition or revert dispatcherDispatchMessagesInSubscriptionThread in the 2.11.0 release. It will be a potential risk, and there may be other issues just haven't break the test.

@nicoloboschi

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews @codelipenghui and @mattisonchao.
I did additional testing and I still think this solution is correct.

Basically the case I'm seeing is the following:

  1. After - normally - reading an entry, the dispatcher calls sendMessagesToConsumers.
  2. The consumers are busy (actually the test properly verify the busy consumers) and so no permits are available for any consumer. The message is added to the redeliveryMessages structure.
    3.a In the next read cycle (readMoreEntries) the replay read is triggered asynchronously.
    3.b At the same time a consumer is ready to receive a new message and then consumerFlow is triggered. And, again, readMoreEntries.
  3. Since the cursor read and replay read points the same position, the entry is read twice. (That could be another unwanted issue and may impact performance)
  4. After the read is complete, the send is triggered. The send method doesn't check if in case of replay the message is still on the redeliveryMessages structure. So the message is sent twice (sequentially)

After the recent work (see mentioned pull in the description) the methods sendMessagesToConsumers and readMoreEntries are no more executed in the same lock and that's why this situation wouldn't have happened before.

The point 4. could be a problem and a new behaviour that will be introduced in 2.11. With this fix we prefer consistency (no duplicated sent to the consumers) over duplicated-reading. We read the same entry twice and perhaps we could optimize this edge-case in another pull. IMO still think this whole work of using different threads is worth.

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@mattisonchao

mattisonchao commented Aug 19, 2022

Copy link
Copy Markdown
Member

Hi, @nicoloboschi

3.a In the next read cycle (readMoreEntries) the replay read is triggered asynchronously.
3.b At the same time a consumer is ready to receive a new message and then consumerFlow is triggered. And, again,

I'm still wondering why invoking readMoreEntires concurrently will got race conditions. I find we may lose the important state sendInProgress when invoking PersistentStreamingDispatcherMultipleConsumers#readMoreEntires method, After add this state control. the test never failed.

image

Controlled experiment:

image

@nicoloboschi
nicoloboschi force-pushed the fix-dispatcher-replay-twice branch from f16e157 to ddabc6e Compare August 19, 2022 14:47
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Aug 19, 2022
@nicoloboschi nicoloboschi changed the title [fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions [fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions (streaming dispatcher) Aug 19, 2022
@nicoloboschi

Copy link
Copy Markdown
Contributor Author

@mattisonchao nice catch!! you're right, that is the missing check
That also explains why only the streaming test fails.

Thanks, I've updated the pull with your suggestion

@mattisonchao

mattisonchao commented Aug 19, 2022

Copy link
Copy Markdown
Member

I think we have to check the relative logic, to avoid another problem. Because I think this kind of logic is very important.

@mattisonchao mattisonchao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move the test out of the flaky group to avoid regression?

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry.
I misread the PR.
I thought we were in another class.

+1
Nice team work @nicoloboschi @mattisonchao

@eolivelli
eolivelli merged commit 7f69727 into apache:master Aug 20, 2022
@eolivelli

Copy link
Copy Markdown
Contributor

@Technoboy- this change must go into 2.11 as well

@mattisonchao mattisonchao removed the release/blocker Indicate the PR or issue that should block the release until it gets resolved label Aug 20, 2022
Technoboy- pushed a commit that referenced this pull request Aug 21, 2022
…bscriptions (streaming dispatcher) (#17163)

* [fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions

* move test to broker-api
nicoloboschi added a commit to datastax/pulsar that referenced this pull request Aug 22, 2022
…bscriptions (streaming dispatcher) (apache#17163)

* [fix][broker] Avoid messages being repeatedly replayed with SHARED subscriptions

* move test to broker-api

(cherry picked from commit 7f69727)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-picked/branch-2.11 doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky-test: SimpleProducerConsumerTest.testSharedSamePriorityConsumer

5 participants