问题
When and how often does Kafka High Level Producer elect a leader? Does it do before sending each message or only once at the time of creating connection?
回答1:
Every broker has a information about the list of topics(and partitions) and their leaders which will be kept up to date by the zoo keeper whenever the new leader is elected or when the number of partition changes.
Thus, when the producer makes a call to one of the brokers, it responds with this information list. Once the producer receives this information, it caches this and uses it to connect to the leader. So next time when it wants to send the message to that particular topic (and partition) it will use this cached information.
Lets assume there was only one leader and there are no replicas for that topic/ partition duo and it got crushed. In this case it will try to connect to that leader and it fails. It will try to fetch the leader from the other brokers list which it has cached to check if there is any leader for this topic! As it does not find any, it will try to hit to the same leader(that is dead) and after reaching a maximum no of retries it will throw an exception !!
来源:https://stackoverflow.com/questions/43146546/when-does-kafka-leader-election-happen