-
Notifications
You must be signed in to change notification settings - Fork 6
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
rework of the event consumer #61
Open
ewollesen
wants to merge
8
commits into
master
Choose a base branch
from
eric/sarama-rework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toddkazakov
previously approved these changes
Apr 9, 2024
28473c8
to
514cd7b
Compare
should be required after an update to the branch
Just makes it a little easier to find it without having to swap files.
Nothing was using it or looking for it.
I believe there was confusion about how this is called or should run. These changes provide equivalent functionality, while being easier to read and understand. There are instructions from the library authors here: https://github.com/IBM/sarama/blob/v1.38.1/consumer_group.go#L19 I think there was confusion around the steps detailed in the link above. So let me clarify my understanding: - Canceling the context passed to Consume is how the implementor of the sarama.ConsumerGroupHandler tells Consume to return. In other words, when we're shutting down, we cancel the context to get Consume to return. I base this off of #4 in the link. - No goroutine needs to be launched on our side, the sarama library will launch the goroutines and those goroutines will call our implementation of ConsumeClaim(). This comes from #3. - The warnings about thread-safety apply to our implementation of ConsumeClaim(). In other words, we need to be careful that s.consumer isn't nil, but since nothing in our code modifies s.consumer after instantiation, we're fine. This comes from #3. - Since our own events.EventConsumer has to be able to respond to Stop() calls, we need to be able to break the for-loop in Start(), which is done by canceling the context that we pass to Consume(). The context.CancelFunc is guarded with a mutex for for extra thread-safety, so that if Stop() is called from multiple goroutines, we don't have any race conditions to worry about. We can reason that the goroutine being previously launched was not necessary, because there was only a single goroutine being launched, and the same function that launched the goroutine then blocked, waiting for it to exit before continuing. Without the goroutine, the WaitGroup, stop channel and stopOnce are no longer necessary.
SaramaConsumerGroup was implementing two different interfaces: sarama.ConsumerGroupHandler, and events.EventConsumer. This changes separates these two concerns, making it clearer the purpose of several methods that are somewhat similarly named (Start, Stop, Cleanup, and Setup). The two interfaces are worth separate implementations as they each have different authors, and different purposes, with sarama.ConsumerGroupHandler being a 3rd-party library concerned with Kafka message consumption, while events.EventConsumer is homegrown and (despite its name) is concerned with lifecycle management. SeramaConsumerGroup is renamed to more clearly indicate its focus as an implementation of EventConsumer.
The only method that uses consumerGroup is Start, so we can just instantiate it in Start, and not have to worry about shared state. After the consumerGroup instantiation is moved to Start, there's very little going on in Initialize, so its remaining work is moved to NewSaramaEventConsumer(), where it can fail sooner.
No need to keep it as a member, it's derived from the config that we're keeping around anyway, and only used in a single function.
When the context is canceled, there's no guarantee that Consume() will return context.Canceled. As a result, the context needs to be double-checked before looping in order to shutdown cleanly.
514cd7b
to
3170fdd
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
As I was reading this code, the goroutine launch seemed really out of place, and the more I read, the more I thought it wasn't necessary, so then I sought to try and remove it. This is the result, and I think it's simpler and easier to read, so I'm submitting it for consideration. Maybe it's not worth messing with working code though.
I'd recommend looking at the individual commits in sequence, I think that might make it overall easier to see what is happening.
Take or leave the changes, it's not a big deal either way.
I've run it locally with the hydrophone, and checked that messages are still consumed, that all checked out. Of course it's used in other places as well, but that seemed like a good enough integration test before going any further.