-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Cluster metadata
Willem van Bergen edited this page May 12, 2015
·
3 revisions
The current implementation is ugly and not optimal, but works reasonably well in practice.
- We initialize the cluster with a list of seed brokers. The order of this list is randomized so that every running client will contact a different broker initially. This list of brokers are stored in the
seedBrokersslice of the client. They have no ID assigned to them. - Once we get metadata back, any broker that is included in the response will be added to the
brokersmap, indexed by ID. - All the operations that require a broker will look it up in the
brokersmap, based on ID.
- We contact the first broker on the seed broker list.
- If it does not respond, we remove it from the
seedBrokersslice, and add it to thedeadSeedsslice. - If the
seedBrokerslist is empty, we grab a broker from thebrokersmap instead. If this one fails to respond as well, we remove it from thebrokersmap. - If the
brokersmap is empty, we wait for a bit so the cluster can recover, and resurrect all thedeadBrokersby moving them back to theseedBrokersslice. This costs one retry attempt. - If we get a successful response, we add all brokers included in the response back to the
brokersmap. - If we get a response, but it is a failure, we wait for a bit and try again. This costs a retry as well.
- By default we retry 3 times, after which we return an error.
- The meaning of a retry is unclear. Sometimes it means "try all brokers", but sometimes we go through a retry after talking to only a single broker.
- We keep 3 sets of brokers: the
brokersmap,seedBrokers, anddeadSeeds. - The logic is hard to understand.
- Only use the
brokersmap to store brokers that have an ID. Only use the list of seed brokers inNewClientto initialize thebrokersmap, throw them away afterwards. - Contact brokers that are already connected before connecting to brokers that are not used, to minimize connections.
- Contact brokers that haven't processed a request recently before contacting brokers that just handled one, to better load balance requests over the active brokers. Alternatively, pick a random (connected) broker.
- Right now, every operations that triggers stale metadata will request a refresh. This means that when a broker goes down, we potentially have many goroutines asking for new metadata. The Kafka protocol allows us to batch these requests into one.