fix race (out of order) in flushing topics (wrong state is stored in changelog) #112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a race condition in writing messages to topics that resulted in a violation of the ordering guarantee (especially changelog topics).
What happened before:
The producer buffer is filled with messages.
_handle_pending is taking one message from the buffer at a time (Participant 1).
If the buffer grows flush_atmost is called which itself starts taking messages from the buffer and sends them (Participant 2).
In rare cases, both participants popped message (e.g. MSG1, then MSG2), but the messages are written in reverse order (e.g. MSG2, then MSG1) to the topic.
In case of changelog topics, this results in wrong states if both messages have the same key.
The fix:
Basically, flush simply waits until the buffer is empty.