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

PubSub Kafka: Respect Subscribe context #3363

Merged
merged 2 commits into from
Feb 28, 2024

Conversation

JoshVanL
Copy link
Contributor

Updates the kafka consumer group to correctly respect context cancellation for individual topic subscriptions. The consumer will be reloaded for every topic subscription, or un-subscription, using the same consumer group. Ensures there are no infinite tight loops, and only a singular consumer is run at a time. Ensures all go routines have returned on close.

@JoshVanL JoshVanL requested review from a team as code owners February 23, 2024 18:34
Copy link

codecov bot commented Feb 23, 2024

Codecov Report

Attention: Patch coverage is 17.10526% with 126 lines in your changes are missing coverage. Please review.

Project coverage is 35.30%. Comparing base (fff4d41) to head (3baf1fd).

❗ Current head 3baf1fd differs from pull request most recent head d4130df. Consider uploading reports for the commit d4130df to get more accurate results

Files Patch % Lines
common/component/kafka/subscriber.go 0.00% 50 Missing ⚠️
common/component/azure/eventhubs/eventhubs.go 0.00% 25 Missing ⚠️
common/component/kafka/kafka.go 0.00% 24 Missing ⚠️
common/authentication/azure/auth.go 33.33% 10 Missing and 2 partials ⚠️
state/redis/redis_query.go 33.33% 12 Missing ⚠️
configuration/azure/appconfig/appconfig.go 87.50% 1 Missing and 1 partial ⚠️
state/postgresql/v2/postgresql.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3363      +/-   ##
==========================================
+ Coverage   35.26%   35.30%   +0.03%     
==========================================
  Files         244      245       +1     
  Lines       30969    30920      -49     
==========================================
- Hits        10921    10915       -6     
+ Misses      19116    19072      -44     
- Partials      932      933       +1     

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

@ItalyPaleAle ItalyPaleAle changed the base branch from release-1.13 to main February 23, 2024 19:41
@ItalyPaleAle
Copy link
Contributor

Please note that in components-contrib we open all PR against main, and then cherry-pick in the relevant release branch (this has worked better here).

@ItalyPaleAle ItalyPaleAle force-pushed the pubsub-kafka-subscribe-ctx branch from 3baf1fd to 3b7e459 Compare February 23, 2024 19:43
k.subscribeLock.Lock()
defer k.subscribeLock.Unlock()
for _, topic := range topics {
k.subscribeTopics[topic] = handlerConfig
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this check if there's already a value for k.subscribeTopics? Otherwise, we may be in a bad state, where one subscriber stopping causes the other subscriber to stop receiving messages (see the delete below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In principle yes, however this is the same behaviour as today and I don't want to change it for 1.13.

I think this check should exist in runtime, and cover all subscribers.

@@ -54,7 +54,9 @@ func (p *PubSub) Subscribe(ctx context.Context, req pubsub.SubscribeRequest, han
Handler: adaptHandler(handler),
ValueSchemaType: valueSchemaType,
}
return p.subscribeUtil(ctx, req, handlerConfig)

p.subscribeUtil(ctx, req, handlerConfig)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should batch this?

At startup, Dapr will likely invoke Subscribe multiple times for different topics. This will cause the connection to Kafka to be destroyed and re-created multiple times. Perhaps here we could add a 200ms delay to batch all subscribe calls

Copy link
Member

@yaron2 yaron2 Feb 23, 2024

Choose a reason for hiding this comment

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

The code in 1.13 right now doesn't destroy the connection to Kafka, it only recreates the context that the Consume function uses on every topic subscription. This PR should adhere to the same behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, this code will only close the consumer group when the pubsub component is Close()ed.

I think adding a batcher is a good idea. I've added one for 200ms. It uses a new struct in kit, please see the PR here dapr/kit#85

@JoshVanL JoshVanL marked this pull request as draft February 23, 2024 19:49
@JoshVanL JoshVanL force-pushed the pubsub-kafka-subscribe-ctx branch from 3e51b58 to aea9183 Compare February 26, 2024 15:44
@yaron2 yaron2 modified the milestones: v1.14, v1.13 Feb 26, 2024
@JoshVanL JoshVanL marked this pull request as ready for review February 28, 2024 09:57
Updates the kafka consumer group to correctly respect context
cancellation for individual topic subscriptions. The consumer will be
reloaded for every topic subscription, or un-subscription, using the
same consumer group. Ensures there are no infinite tight loops, and only
a singular consumer is run at a time. Ensures all go routines have
returned on close.

Signed-off-by: joshvanl <[email protected]>
@JoshVanL JoshVanL force-pushed the pubsub-kafka-subscribe-ctx branch from 01f2de8 to fdd54f0 Compare February 28, 2024 10:00
@yaron2 yaron2 merged commit 6413239 into dapr:main Feb 28, 2024
@yaron2
Copy link
Member

yaron2 commented Feb 28, 2024

@JoshVanL please cherry pick to 1.13.

ItalyPaleAle pushed a commit that referenced this pull request Feb 28, 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