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

Patch sarama_kafka rebalance fix #818

Merged
merged 4 commits into from
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions protocol/kafka_sarama/v2/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,40 @@ func (r *Receiver) Close(context.Context) error {
return nil
}

// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages().
// Also the method should return when `session.Context()` is done.
// Refer - https://github.com/Shopify/sarama/blob/5e2c2ef0e429f895c86152189f625bfdad7d3452/examples/consumergroup/main.go#L177
func (r *Receiver) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
msg := message
m := NewMessageFromConsumerMessage(msg)

r.incoming <- msgErr{
msg: binding.WithFinish(m, func(err error) {
if protocol.IsACK(err) {
session.MarkMessage(msg, "")
}
}),
// NOTE:
// Do not move the code below to a goroutine.
// The `ConsumeClaim` itself is called within a goroutine, see:
// https://github.com/Shopify/sarama/blob/main/consumer_group.go#L27-L29
for {
select {
case msg := <-claim.Messages():
m := NewMessageFromConsumerMessage(msg)

r.incoming <- msgErr{
msg: binding.WithFinish(m, func(err error) {
if protocol.IsACK(err) {
session.MarkMessage(msg, "")
}
}),
}

// Should return when `session.Context()` is done.
// If not, will raise `ErrRebalanceInProgress` or `read tcp <ip>:<port>: i/o timeout` when kafka rebalance. see:
// https://github.com/Shopify/sarama/issues/1192
// https://github.com/Shopify/sarama/issues/2118
// Also checked Shopify/sarama code which calls this ConsumeClaim method, and don't see if there is any difference
// whether this method returns error or not. If it returns the error, as per current implementation, it could
// get printed in logs and later drained when the ConsumerGroup gets closed.
// For now, to be on safer side, returning nil instead of session.Context().Err() as suggested in
// https://github.com/Shopify/sarama/blob/5e2c2ef0e429f895c86152189f625bfdad7d3452/examples/consumergroup/main.go
case <-session.Context().Done():
return nil
nbajaj90 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}

func (r *Receiver) Receive(ctx context.Context) (binding.Message, error) {
Expand Down