Kafka的Broker节点宕机测试

原创
10/14 16:49
阅读数 89

在一个由3个broker组成的集群里,新建一个topic, 指定 3 个partition(分区),每个partition有3个replication(1个leader , 2个follower)

./bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --topic logtopic2 --partitions 3 --replication-factor 3

查看分区分配情况:

./bin/kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic logtopic2

Topic: logtopic2        TopicId: x46EnRy7QAetKIiPPn2q6w PartitionCount: 6       ReplicationFactor: 3    Configs:
        Topic: logtopic2        Partition: 0    Leader: 1       Replicas: 1,2,0 Isr: 1,0
        Topic: logtopic2        Partition: 1    Leader: 2       Replicas: 2,0,1 Isr: 2,0,1
        Topic: logtopic2        Partition: 2    Leader: 0       Replicas: 0,1,2 Isr: 0,1

如partition0的leader 的 broker编号是1,所有分区的分布情况如下图示:

宕掉broker2以后,宕机后,leader在broker2上的partition1,producter无法写入消息( Broker may not be available.),所有consumer都不能读取消息(org.apache.kafka.common.errors.DisconnectException),包括其他partition.

等kafka心跳发现后会自动选举新leader出来,这时producter会把缓存的消息写入新的leader,consumer也恢复读取。

再看分配情况:

./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic logtopic2
Topic: logtopic2        TopicId: x46EnRy7QAetKIiPPn2q6w PartitionCount: 6       ReplicationFactor: 3    Configs:
        Topic: logtopic2        Partition: 0    Leader: 1       Replicas: 1,2,0 Isr: 1,0
        Topic: logtopic2        Partition: 1    Leader: 0       Replicas: 2,0,1 Isr: 0,1
        Topic: logtopic2        Partition: 2    Leader: 0       Replicas: 0,1,2 Isr: 0,1

看到partition1的leader换成了broker0,所有Isr里的broker2都没有了。

直到新的leader重新选举产生

再把broker2启来,leader并不会有变化,新上来的broker对所有partition都是follower

./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic logtopic2
Topic: logtopic2        TopicId: x46EnRy7QAetKIiPPn2q6w PartitionCount: 6       ReplicationFactor: 3    Configs:
        Topic: logtopic2        Partition: 0    Leader: 1       Replicas: 1,2,0 Isr: 1,0,2
        Topic: logtopic2        Partition: 1    Leader: 0       Replicas: 2,0,1 Isr: 0,1,2
        Topic: logtopic2        Partition: 2    Leader: 0       Replicas: 0,1,2 Isr: 0,1,2

可以用脚本触发leader的均衡

./kafka-preferred-replica-election.sh --zookeeper 127.0.0.1:2181

./kafka-topics.sh --describe --zookeeper 127.0.0.1:2181 --topic logtopic2
Topic: logtopic2        TopicId: x46EnRy7QAetKIiPPn2q6w PartitionCount: 6       ReplicationFactor: 3    Configs:
        Topic: logtopic2        Partition: 0    Leader: 1       Replicas: 1,2,0 Isr: 1,2,0
        Topic: logtopic2        Partition: 1    Leader: 2       Replicas: 2,0,1 Isr: 1,2,0
        Topic: logtopic2        Partition: 2    Leader: 0       Replicas: 0,1,2 Isr: 1,2,0

又回到了最初的样子。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部