This repository has been archived by the owner on May 13, 2019. It is now read-only.
Making partitionConsumer claim loop more robust #101
Merged
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.
This attempts to resolve two race conditions we've noticed while using this library:
Timeout race condition: Consumer A is handling partition 1. It has recently grabbed a batch of messages from Kafka on that partition to process. Consumer B comes online, triggering a rebalance of partition 1 from consumer A to B. However, consumer A is taking a while to finish the batch it last received, so it eventually times out after
config.Offsets.ProcessingTimeout
. However, consumer B is in a retry loop also taking a maximum ofconfig.Offsets.ProcessingTimeout
. If it fails to get the partition in that amount of time, it will give up. Since both are working off of the same timeout value, this is sufficient to sometimes cause consumer B to fail claiming the partition even though consumer A has just released it. To resolve this, I've added two more retry iterations to ensure that we will perform a couple more attempts even after this timeout value.The root issue is actually in kazoo-go, in https://github.com/wvanbergen/kazoo-go/blob/master/consumergroup.go#L273 but I've updated the claim retry loop here to handle this and any other error cases. But for background, here's what we observed:
/consumers/<cgName>/owners/<topicName>/<partitionNumber>
, but fails since it already exists/consumers/<cgName>/owners/<topicName>/<partitionNumber>
/consumers/<cgName>/owners/<topicName>/<partitionNumber>
, but it's now gone so that fails.That specific race condition may also require a more targeted fix, but more broadly, we think that if a consumer is trying to claim a partition, it should continue trying to claim it rather than ending up in a state where the partition is unowned indefinitely. As such, the retry loop here will continue running regardless of the error we get until we hit the retry limit or successfully claim the partition. Also, if we ultimately fail to claim it, we report the error to the consumergroup's error channel so the client can determine how to handle the failure to claim a partition.