程序员

Kafka副本机制

2023-01-27  本文已影响0人  我可能是个假开发

一、副本基本信息

1.定义

Kafka 是有主题概念的,而每个主题又进一步划分成若干个分区。副本的概念实际上是在分区层级下定义的,每个分区配置有若干个副本。
副本本质就是一个只能追加写消息的提交日志。
根据 Kafka 副本机制的定义,同一个分区下的所有副本保存有相同的消息序列,这些副本分散保存在不同的 Broker 上,从而能够对抗部分 Broker 宕机带来的数据不可用。

AR:Kafka 分区中的所有副本统称为 AR(Assigned Repllicas)

ISR:表示和 Leader 保持同步的 Follower 集合。 如果 Follower 长时间未向 Leader 发送通信请求或同步数据,则该 Follower 将被踢出 ISR。
该时间阈值由 replica.lag.time.max.ms参数设定,默认 30s。 Leader 发生故障之后,就会从 ISR 中选举新的 Leader。

OSR:表示 Follower 与 Leader 副本同步时,延迟过多的副本

AR = ISR + OSR

2.作用

提高数据可靠性

3 台 Broker 的 Kafka 集群上的副本分布情况:


副本.png

主题 1 分区 0 的 3 个副本分散在 3 台 Broker 上,其他主题分区的副本也都散落在不同的 Broker 上,从而实现数据冗余。

二、Leader 选举流程

Kafka 集群中有一个 broker 的 Controller 会被选举为 Controller Leader,负责管理集群broker 的上下线,所有 topic 的分区副本分配和 Leader 选举等工作。
Controller 的信息同步工作是依赖于 Zookeeper 的。

Kafka工作流程.png

1.选举流程

2.选举规则

在isr中存活为前提,按照AR中排在前面的优先。例如ar[1,0,2], isr [2,0,1],那么leader就会按照1, 0, 2的顺序轮询

3.示例

hadoop102:brokerId=0
hadoop103:brokerId=1
hadoop104:brokerId=2
hadoop105:brokerId=3

(1) 创建一个新的 topic, 4 个分区, 4 个副本

[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server
hadoop102:9092 --create --topic myTopic --partitions 4 --replication-factor 4
Created topic myTopic.

(2) 查看 Leader 分布情况

[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 3 Replicas: 3,0,2,1 Isr: 3,0,2,1
Topic: myTopic Partition: 1 Leader: 1 Replicas: 1,2,3,0 Isr: 1,2,3,0
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,3,1,2
Topic: myTopic Partition: 3 Leader: 2 Replicas: 2,1,0,3 Isr: 2,1,0,3

(3) 停止掉 hadoop105 的 kafka 进程,并查看 Leader 分区情况

[hadoop105 kafka]$ bin/kafka-server-stop.sh
[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 0 Replicas: 3,0,2,1 Isr: 0,2,1
Topic: myTopic Partition: 1 Leader: 1 Replicas: 1,2,3,0 Isr: 1,2,0
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,1,2
Topic: myTopic Partition: 3 Leader: 2 Replicas: 2,1,0,3 Isr: 2,1,0

(4) 停止掉 hadoop104 的 kafka 进程,并查看 Leader 分区情况

[hadoop104 kafka]$ bin/kafka-server-stop.sh
[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 0 Replicas: 3,0,2,1 Isr: 0,1
Topic: myTopic Partition: 1 Leader: 1 Replicas: 1,2,3,0 Isr: 1,0
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,1
Topic: myTopic Partition: 3 Leader: 1 Replicas: 2,1,0,3 Isr: 1,0

(5) 启动 hadoop105 的 kafka 进程,并查看 Leader 分区情况

[hadoop105 kafka]$ bin/kafka-server-start.sh -daemon config/server.properties
[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 0 Replicas: 3,0,2,1 Isr: 0,1,3
Topic: myTopic Partition: 1 Leader: 1 Replicas: 1,2,3,0 Isr: 1,0,3
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,1,3
Topic: myTopic Partition: 3 Leader: 1 Replicas: 2,1,0,3 Isr: 1,0,3

(6) 启动 hadoop104 的 kafka 进程,并查看 Leader 分区情况

[hadoop104 kafka]$ bin/kafka-server-start.sh -daemon config/server.properties
[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 0 Replicas: 3,0,2,1 Isr: 0,1,3,2
Topic: myTopic Partition: 1 Leader: 1 Replicas: 1,2,3,0 Isr: 1,0,3,2
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,1,3,2
Topic: myTopic Partition: 3 Leader: 1 Replicas: 2,1,0,3 Isr: 1,0,3,2

(7) 停止掉 hadoop103 的 kafka 进程,并查看 Leader 分区情况

[hadoop103 kafka]$ bin/kafka-server-stop.sh
[hadoop102 kafka]$ bin/kafka-topics.sh --bootstrap-server hadoop102:9092 --describe --topic myTopic
Topic: myTopic TopicId: awpgX_7WR-OX3Vl6HE8sVg PartitionCount: 4 ReplicationFactor: 4
Configs: segment.bytes=1073741824
Topic: myTopic Partition: 0 Leader: 0 Replicas: 3,0,2,1 Isr: 0,3,2
Topic: myTopic Partition: 1 Leader: 2 Replicas: 1,2,3,0 Isr: 0,3,2
Topic: myTopic Partition: 2 Leader: 0 Replicas: 0,3,1,2 Isr: 0,3,2
Topic: myTopic Partition: 3 Leader: 2 Replicas: 2,1,0,3 Isr: 0,3,2

极客时间《Kafka 核心技术与实战》学习笔记Day13 - http://gk.link/a/11UOV

上一篇 下一篇

猜你喜欢

热点阅读