我有一个有 5 个分区的 Kafka 集群。
在缩减到 3 时,领导者选举发生了几次。
最后,只有一位经纪人成为我的一个主题的所有 3 个分区的领导者。
Topic: test PartitionCount:3 ReplicationFactor:3
Topic: test Partition: 0 Leader: 2 Replicas: 2,0,1 Isr: 2,1,0
Topic: test Partition: 1 Leader: 2 Replicas: 3,1,2 Isr: 2,1
Topic: test Partition: 2 Leader: 2 Replicas: 4,2,3 Isr: 2
2,1,0 是正在运行的代理。
partition 0 is available with 2, 0, 1. All the brokers are available. So, isr=2,1,0
partition 1 is available with 3, 1, 2 but 3 is removed broker. So isr=2,1
partition 2 is available with 4,2,3 but both 4,3 are removed brokers. So isr=2
请注意,只有 2 个被选为领导者。即使我们假设它在其他代理中具有最高的水印,给定分区的所有 ISR 可能已经同步,因此所有给定分区的偏移量都相同(否则它们会从 ISR 中删除)。
我已经等了很多时间(有一段时间,如果其中一个副本不符合标准,它将从 ISR 中删除)但仍然是领导者选举。
领导者可以均匀分布(负载均衡)。
For example, partition-0 leader can be 0
partition 1 leader can be 1
partition 2 leader can be 2
为什么不是这样?
注意:我没有启用
unclean leader election
.它只是默认值。If we assume that 0,1 came up after the leader election happened, why is not there a re-election then? If the ISRs are updated, ideally the leaders should also be. Isn't it?
i.e. if Kafka knows that 0,1 are up and have in-sync replicas, it SHOULD have conducted one more leader election.
Is there any specific reason why is it not so?
最佳答案
Kafka 具有首选领导者的概念,这意味着如果可能,它会选择该副本作为领导者。 第一 副本列表中列出的副本是首选领导者。现在查看当前集群状态:
Topic: test Partition: 0 Leader: 2 Replicas: 2,0,1 Isr: 2,1,0
Topic: test Partition: 1 Leader: 2 Replicas: 3,1,2 Isr: 2,1
Topic: test Partition: 2 Leader: 2 Replicas: 4,2,3 Isr: 2
如果您的所有代理都同步返回,默认情况下 Kafka 将重新选举首选领导者(或者可以使用
kafka-preferred-replica-election.sh
工具强制,参见 Balancing leadership)。如果不打算重新启动丢失的代理,您可以将分区的副本分配更改为 balance the leadership manually使用
kafka-reassign-partitions.sh
工具。只需确保将首选领导者作为副本列表中的第一个条目即可。
关于apache-kafka - Kafka 中的不公平领导者选举 - 所有分区的领导者相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56514667/