IBM / sarama

Sarama is a Go library for Apache Kafka.
MIT License
11.57k stars 1.76k forks source link

Metadata refresh should skip updates when metadata response is empty #2664

Closed HaoSunUber closed 1 year ago

HaoSunUber commented 1 year ago
Description

After my PR, the Sarama client will always pick up a least loaded broker from cached brokers to fetch metadata. Then the metadata response will update our cached brokers (updateBroker).

But if the chosen broker returns a response with empty brokers, updateBroker will clear our cached brokers into empty. The client will fall back to the seed broker to refresh metadata in the next metadata refresh because cached brokers are empty. This is what we want to avoid using the seed broker after bootstrap due to the stale metadata issue (issues).

Technically, the broker should not return an empty broker in the response. But we do come across this situation where the broker returns empty brokers without any error and emptys our cached brokers. It looks like a bug in Kafka. The Java client skips the empty response to update the metadata cache (code).

In Sarama, we should also implement the same logic to skip the update when the response is empty and move to the next candidate broker.

I can make a PR if you think it is reasonable.

dnwe commented 1 year ago

@HaoSunUber thanks for reporting this. It does look like a bug that could be easy to hit during a rolling restart of a kafka cluster. I agree that we should match the Java client and ignore empty metadata like this

HaoSunUber commented 1 year ago

Thanks. I will do a change soon

HaoSunUber commented 1 year ago

Thanks @dnwe for your quick response. Would you mind building a new release so that we can use it in our company?