Skip to content

fix(kafka): Flush producers on exit - #15934

Merged
BYK merged 5 commits into
masterfrom
byk/fix/kafka-eventstream-flush
Dec 4, 2019
Merged

BYK merged 5 commits into
masterfrom
byk/fix/kafka-eventstream-flush

Conversation

@BYK

@BYK BYK commented Dec 4, 2019

Copy link
Copy Markdown
Member

We need to make sure the producers are flushed when exiting the application as otherwise we risk losing events that were not yet sent to Kafka, which happened to me with the backfill script.

We need to make sure the producers are flushed when exiting the application as otherwise we risk losing events that were not yet sent to Kafka, which happened to me with the backfill script.
@BYK
BYK requested review from mattrobenolt and tkaemming December 4, 2019 19:00
@BYK

BYK commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

FYI: I'm planning to follow up with replacing the SIGINT and SIGTERM handlers below with atexit.register and adding a global SIGINT and SIGTERM handler to runner/__init__.py that calls sys.exit(0) to ensure triggering atexit events with these signals.

@tkaemming tkaemming 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'm +1 on this, but want to make sure somebody else has eyes on it before merging in case they can think of any unwanted side-effects that we have missed.

FYI: I'm planning to follow up with replacing the SIGINT and SIGTERM handlers below with atexit.register and adding a global SIGINT and SIGTERM handler to runner/__init__.py that calls sys.exit(0) to ensure triggering atexit events with these signals.

These signal handlers should be fine as-is. They just prevent a preemptive KeyboardInterrupt from being thrown in the main loop of the batching consumer, and allow it to shutdown safely after it finishes the work in progress. To my understanding, that should be an orthogonal concern and not interfere with the atexit handlers since the main thread exits normally.

Comment thread src/sentry/utils/kafka.py Resolved
@BYK

BYK commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

To my understanding, that should be an orthogonal concern and not interfere with the atexit handlers since the main thread exits normally.

Not really as the registered handler is not always called when atexit handlers are called. There's also this issue: https://stackoverflow.com/questions/23468042/the-invocation-of-signal-handler-and-atexit-handler-in-python

@tkaemming

Copy link
Copy Markdown
Contributor

Not really as the registered handler is not always called when atexit handlers are called. There's also this issue: https://stackoverflow.com/questions/23468042/the-invocation-of-signal-handler-and-atexit-handler-in-python

Isn't that only the case when the signal handler itself calls os.exit, though?

signal_shutdown doesn't do that, it just breaks out the run loop on the next iteration which allows the main thread to exit gracefully (and should trigger atexit handlers):

def handler(signum, frame):
consumer.signal_shutdown()

Here's the relevant blocks in the batching consumer code being called:

@BYK

BYK commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

signal_shutdown doesn't do that, it just breaks out the run loop on the next iteration which allows the main thread to exit gracefully (and should trigger atexit handlers):

My argument is the correct place to do this is using the atexit handler and it seems like the StackOverflow question and answer are almost exactly the same case as you described?

@BYK
BYK requested a review from tkaemming December 4, 2019 19:51
@tkaemming

Copy link
Copy Markdown
Contributor

My argument is the correct place to do this is using the atexit handler and it seems like the StackOverflow question and answer are almost exactly the same case as you described?

I guess I don't really see how this approach would change anything — or even work — but it's possible I'm not really understanding your intention here. Are you trying to ensure the consumer completes its work gracefully before interpreter shutdown?

If so, the only way to do that is to wait for the run method to complete. The way we run these consumers is just by calling run from the from the main thread, so this is already happening — as long as no exceptions are thrown during its execution, in which case we want to shut down immediately anyway. SIGINT is trapped by this handler and translated to a call to signal_shutdown so that KeyboardInterrupt doesn't cause the consumer to crash for this reason. Calling signal_shutdown is a non-blocking operation that causes the consumer main loop to stop looping. Once it does that, the process exits.

This is the approach suggested by several comments in that SO thread:

If you need "a more graceful shutdown", you should find a way to gracefully break the loop

If this doesn't address your concerns, maybe it's easier to just make that change and we can talk about the specific change rather than a hypothetical one.

@BYK

BYK commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

If this doesn't address your concerns, maybe it's easier to just make that change and we can talk about the specific change rather than a hypothetical one.

I think we are talking about different things. All I care about is consolidating code. I'm okay with the current way of breaking the loop.

I guess I don't really see how this approach would change anything

That is my intention: move away from signal handlers and simply use atexit.register for all these things, normalizing it with a global sigterm handler.

Comment thread src/sentry/utils/kafka.py Resolved

@tkaemming tkaemming 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.

That is my intention: move away from signal handlers and simply use atexit.register for all these things, normalizing it with a global sigterm handler.

I don't really understand how this would work, but maybe if I see the implementation it'll make more sense.

BYK added a commit to getsentry/self-hosted that referenced this pull request Dec 4, 2019
Upgrades existing events from last `$SENTRY_EVENT_RETENTION_DAYS`
to Snuba automatically. Relies on getsentry/sentry#15934.
@BYK
BYK merged commit c0dc47d into master Dec 4, 2019
@BYK
BYK deleted the byk/fix/kafka-eventstream-flush branch December 4, 2019 21:39
BYK added a commit that referenced this pull request Dec 4, 2019
Instead of sprinkling incomplete signal handlers over the codebase
prefer using `atexit.register` and trigger an exit upon receiving
`SIGTERM` as it normally bypasses the `atexit` callbacks.

The new code should behave identical to the old one, but more complete
as in it now handles more exit triggers instead of just `SIGINT`.

This is a follow up to #15934.
BYK added a commit to getsentry/self-hosted that referenced this pull request Dec 9, 2019
Upgrades existing events from last `$SENTRY_EVENT_RETENTION_DAYS`
to Snuba automatically. Relies on getsentry/sentry#15934.
MaicolBen pushed a commit to hinthealth/onpremise that referenced this pull request Jul 20, 2020
)

Upgrades existing events from last `$SENTRY_EVENT_RETENTION_DAYS`
to Snuba automatically. Relies on getsentry/sentry#15934.
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants