Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(StreamEngine): graceful shutdown must wait for all events to be p… #171

Merged
merged 1 commit into from
Feb 8, 2024

Conversation

marcosschroh
Copy link
Collaborator

…rocessed before Streams are stopped. Related to #162

Copy link
Contributor

github-actions bot commented Feb 7, 2024

PR Preview Action v1.4.7
Preview removed because the pull request was closed.
2024-02-08 11:59 UTC

Copy link

codecov bot commented Feb 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ca70959) 95.75% compared to head (e013ae5) 95.53%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #171      +/-   ##
==========================================
- Coverage   95.75%   95.53%   -0.22%     
==========================================
  Files          23       23              
  Lines         849      852       +3     
==========================================
+ Hits          813      814       +1     
- Misses         36       38       +2     
Flag Coverage Δ
unittests 95.53% <100.00%> (-0.22%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@marcosschroh
Copy link
Collaborator Author

marcosschroh commented Feb 7, 2024

This PR is addressing the first part of the issue #162 . With this new code, when a Stream is stopped (either directly or indirectly by the engine) the following thing will happen:

  1. It will wait until the event in the chain (middlewares chain) has been processed
  2. More importantly no more events will arrive in the getone.
  3. The asyncio.Task that runs will be cancelled only and only when there are no more events.

If the end user has a really slow coroutine to process events and await stream.stop() is called, then it does not matter, the stop will wait for the event to be processed. Example in this test

Auto commit vs Manual commit:

Manual commit: With this new code it does not matter where the user commits (either at the beginning or end of the coroutine that handles the event). The reason is that the coroutine will run completely regardless how slow it can be when the stream is stopped, then we make sure that the commits always occurs

Auto commit: We have 2 scenarios:

  • Scenario 1: Similar to manual commit, we know that the coroutine that handles the event will finish before the stop. if await stream.stop() is called when an event is being processed, then for point 2 we know that no more events will arrive, then we are not losing events.

  • Scenario 2: If an event arrives and await stream.stop() is called just before the event start being processed then there is a small chance to lose the event. This could happen when the the stop is really slow and the auto_commit made the commit.

@woile
Copy link
Member

woile commented Feb 7, 2024

Maybe add in a FAQ that messages are not lost.

What happens when a stream is cancelled?
...

kstreams/streams.py Outdated Show resolved Hide resolved
@marcosschroh
Copy link
Collaborator Author

Maybe add in a FAQ that messages are not lost.

What happens when a stream is cancelled?
...

Good idea. We should add the new FAQ page

…rocessed before Streams are stopped. Related to #162
@marcosschroh marcosschroh force-pushed the fix/graceful-shutdown branch from 3af6cdd to e013ae5 Compare February 8, 2024 11:12
@marcosschroh marcosschroh merged commit af9bd68 into master Feb 8, 2024
7 of 8 checks passed
@marcosschroh marcosschroh deleted the fix/graceful-shutdown branch February 8, 2024 11:59
marcosschroh added a commit to marcosschroh/kstreams that referenced this pull request Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants