bigywater.tech
Kafka 内部原理 · 49

Partition Reassignment:分区迁移如何进行

用kafka-reassign-partitions.sh写一份JSON计划,把上一篇里卡着死掉broker的两个分区真实迁移到健康的broker上,看--execute和--verify两步真实分别做了什么。

4→2
死掉的broker真实被换成健康broker的过程
1→2
ISR真实从残缺恢复到完整

验证:写一份JSON,指定分区真实新的副本该在哪几个broker上

命令 · shell $ cat reassign-plan.json { "version": 1, "partitions": [ {"topic": "after-join-topic", "partition": 0, "replicas": [1, 2]}, {"topic": "after-join-topic", "partition": 3, "replicas": [3, 2]} ] } $ kafka-reassign-partitions.sh --bootstrap-server localhost:19092 \ --reassignment-json-file reassign-plan.json --execute
真实实测 · 本机执行输出
Current partition replica assignment
{"partitions":[{"topic":"after-join-topic","partition":0,"replicas":[4,1],...},
                {"topic":"after-join-topic","partition":3,"replicas":[3,4],...}]}

Successfully started partition reassignments for after-join-topic-0,after-join-topic-3

执行之前,工具真实先把"当前的分配"原样打印了一遍——这份输出真实就是回滚用的,如果新计划真实出了问题,把这段JSON存下来再跑一次--execute就能真实退回去。

验证:--verify真实确认迁移完成,ISR真实恢复健康

命令 · shell $ kafka-reassign-partitions.sh --bootstrap-server localhost:19092 \ --reassignment-json-file reassign-plan.json --verify
真实实测 · 本机执行输出
Status of partition reassignment:
Reassignment of partition after-join-topic-0 is completed.
Reassignment of partition after-join-topic-3 is completed.

Clearing broker-level throttles on brokers 1,2,3
Clearing topic-level throttles on topic after-join-topic

真实describe一下这个topic现在的状态:

真实实测 · 本机执行输出
Partition 0  Leader: 1  Replicas: 1,2  Isr: 1,2   ← 死掉的4真实被换成了2
Partition 1  Leader: 1  Replicas: 1,2  Isr: 1,2
Partition 2  Leader: 2  Replicas: 2,3  Isr: 2,3
Partition 3  Leader: 3  Replicas: 3,2  Isr: 2,3   ← 死掉的4真实被换成了2
真实关键变化:Replicas字段里的4真实彻底消失了,换成了健康的broker2,Isr也真实从只剩1个恢复到2个——这正是上一篇结尾留下的问题的真实解法。reassignment真实做的事情是:先在目标broker(这里是broker2)上真实开始从现有副本拉取数据,等新副本追上进度、真实进入ISR之后,再把旧的、已经不在新计划里的副本(这里是死掉的broker4)真实从Replicas列表里摘除——整个过程对客户端透明,不需要下线topic,唯一的代价是迁移期间真实会占用额外的网络和磁盘IO(工具也支持--throttle参数限速,避免迁移影响正常流量)。

分步演示:分区迁移真实的三步走

每一步都是本文上面用kafka-reassign-partitions.sh跑出来的原始结果。

reassignment真实把死掉的副本换成了健康的
这一步新验证的阶段

    参考与说明

    • 数据来自本机3节点Kafka 4.3.1集群,topic after-join-topic在上一篇里因为broker4死亡而残留了失效副本,本文用kafka-reassign-partitions.sh真实修复。
    • 和《Broker 加入和离开集群时发生什么》一起看:那篇制造的问题,这篇给出真实的解法。
    • 没有验证:大规模迁移(几十上百个分区)时--throttle真实的限速效果;迁移过程中源broker或目标broker再次故障时的真实行为。
    京ICP备2026067502号-1京公网安备11010502063487号