简体   繁体   English

卡夫卡不公平的领导人选举-所有分区的相同领导人

[英]Unfair Leader election in Kafka - Same leader for all partitions

I have a Kafka cluster with 5 partitions. 我有一个带有5个分区的Kafka集群。 On scaling down to 3, leader election took place several times. 缩减至3时,领导者选举进行了数次。

Finally only one broker became the leader for all the 3 partitions of one of my topics. 最终,只有一个经纪人成为我主题之一的所有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 are the brokers that are running. 2,1,0是正在运行的代理。

partition 0 is available with 2, 0, 1. All the brokers are available. 分区0可用2、0、1。所有代理都可用。 So, isr=2,1,0 因此,isr = 2,1,0

partition 1 is available with 3, 1, 2 but 3 is removed broker. 分区1可与3、1、2一起使用,但删除了3个代理。 So isr=2,1 所以isr = 2,1

partition 2 is available with 4,2,3 but both 4,3 are removed brokers. 分区2可用于4,2,3,但两个4,3均已删除代理。 So isr=2 所以isr = 2

Note that, only 2 has been elected as the leader. 请注意,只有2位当选为领导者。 Even if we assume that it has the highest watermark among the other brokers, all the ISRs for a given partition could have been in sync, therefore all have the same offsets for a given partition (otherwise they would have been removed from the ISR). 即使我们假设它在其他代理中具有最高的水印,给定分区的所有ISR也可能已经同步,因此,给定分区的所有ISR都具有相同的偏移量(否则它们将从ISR中删除)。

I have waited for a lot of time (there is a time after which if one of the replicas is not up to the mark, it will be removed from ISR) but still that is the leader election. 我已经等待了很多时间(有一段时间,如果其中一个副本达不到要求,它将被从ISR中删除),但这仍然是领导者的选举。

Leaders can be evenly distributed (load balanced). 领导者可以平均分配(负载均衡)。

For example, partition-0 leader can be 0
             partition 1 leader can be 1
             partition 2 leader can be 2

Why is this not so? 为什么不是这样?

Note: I did not enable unclean leader election . 注意:我没有启用unclean leader election It is the default value only. 仅是默认值。

If we assume that 0,1 came up after the leader election happened, why is not there a re-election then? 如果我们假设领导人选举发生后出现了0.1,那为什么不进行连任呢? If the ISRs are updated, ideally the leaders should also be. 如果更新了ISR,理想情况下,领导者也应该进行更新。 Isn't it? 是不是

ie if Kafka knows that 0,1 are up and have in-sync replicas, it SHOULD have conducted one more leader election. 例如,如果卡夫卡知道0,1已经建立并且具有同步副本,则它应该再次进行一次领导人选举。

Is there any specific reason why is it not so? 是否有任何特定原因为什么不这样呢?

Kafka has the concept of a preferred leader, meaning that if possible it will elect that replica to serve as the leader. Kafka具有首选领导者的概念,这意味着如果可能,它将选择该副本作为领导者。 The first replica listed in the replicas list is the preferred leader. 副本列表中列出的第一个副本是首选的领导者。 Now looking at the current cluster state: 现在查看当前集群状态:

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
  • Partition 0, broker 2 is the preferred leader and is the current leader 分区0,经纪人2是首选领导者,并且是当前领导者
  • Partition 1, broker 3 is the preferred leader but it's not in-sync, so a random leader is picked between 2 and 1 分区1,代理3是首选的领导者,但它不是同步的,因此在2和1之间选择一个随机的领导者
  • Partition 2, broker 4 is the preferred leader but again 4 is not in-sync. 分区2是代理4的首选领导者,但4又不是同步的。 Only 2 is in-sync, so it's elected. 只有2个处于同步状态,因此当选。

If all your brokers were to go back in-sync, by default Kafka would re-elect the preferred leaders (or it can be forced using the kafka-preferred-replica-election.sh tool, see Balancing leadership ). 如果您的所有经纪人都将恢复同步,则默认情况下,Kafka将重新选择首选领导者(或者可以使用kafka-preferred-replica-election.sh工具强制其kafka-preferred-replica-election.sh ,请参见平衡领导者 )。

If the missing brokers are not going to be restarted, you can change the replica assignments for the partitions, to balance the leadership manually using the kafka-reassign-partitions.sh tool. 如果丢失的代理不会重新启动,则可以更改分区的副本分配,以使用kafka-reassign-partitions.sh工具手动平衡领导地位 Just make sure you put the preferred leader as the first entry in the replicas list. 只需确保将首选领导者作为副本列表中的第一项即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM