一次kafka增加Partition流程
线上有一个topic碰到突增,但是限制于topic的分区太少,消费实在太慢,导致影响到用户的感受。
首先我们看下这个topic
1 | ./bin/kafka-topics.sh --zookeeper prod-zk1:2181 --describe --topic Events |
我们看到这个topic就10个分区,但是副本数却有些是2,有些是1。这种是否可以直接增加分区呢,答案是不行的。
你要这个时候增加分区就会报
1 | requirement failed: All partitions should have the same number of replicas. |
所以要先把这些分区都设置成一样的replica.
那就先写个json,把所有Partition设置成一样的replica
1 | {"version":1,"partitions":[{"topic":"Events","partition":7,"replicas":[0,1,2]},{"topic":"Events","partition":0,"replicas":[2,1,0]},{"topic":"Events","partition":1,"replicas":[0,1,2]},{"topic":"Events","partition":2,"replicas":[1,2,0]},{"topic":"Events","partition":5,"replicas":[2,1,0]},{"topic":"Events","partition":6,"replicas":[2,0,1]},{"topic":"Events","partition":9,"replicas":[2,1,0]},{"topic":"Events","partition":4,"replicas":[0,2,1]},{"topic":"Events","partition":3,"replicas":[2,1,0]},{"topic":"Events","partition":8,"replicas":[1,2,0]}]} |
如果topic的分区很多,那最好自动生成这个,先我们写一个文件叫topic.json
1 | { |
然后使用命令去生成迁移的json
1 | ./bin/kafka-reassign-partitions.sh --zookeeper prod-zk1:2181 --topics-to-move-json-file topic.json --broker-list "0,1,2" --generate |
这样输出的结果复制到replication-factor.json里,自己有必要再进行手动调整。
1 | ./bin/kafka-reassign-partitions.sh --zookeeper prod-zk1:2181 --reassignment-json-file replication-factor.json --execute |
结果又来了如下的错误
1 | Partitions reassignment failed due to Partition reassignment currently in progress for Map([Events,1] -> ReassignedPartitionsContext(List(0, 1),null), [Events,8] -> ReassignedPartitionsContext(List(1, 2),null), [Events,4] -> ReassignedPartitionsContext(List(0, 2),null), [Events,2] -> ReassignedPartitionsContext(List(1, 2),null), [Events,9] -> ReassignedPartitionsContext(List(2, 1),null), [Events,7] -> ReassignedPartitionsContext(List(0, 1),null), [Events,3] -> ReassignedPartitionsContext(List(2, 1),null)). Aborting operation |
而去kafka broker里能发现如下的日志
1 | PartitionFetchInfo(674777,1048576) (kafka.server.KafkaApis) |
我去,难道以前有人更新过啊。那就去zk里看下吧。果然还真有
1 | [zk: prod-zk1(CONNECTED) 3] get /admin/reassign_partitions |
那就直接删除了吧
1 | [zk: prod-zk1(CONNECTED) 4] rmr /admin/reassign_partitions |
再次执行就了
1 | ./bin/kafka-reassign-partitions.sh --zookeeper prod-zk1:2181 --reassignment-json-file replication-factor.json --execute |
参考:
https://blog.csdn.net/huanggang028/article/details/49445569
https://stackoverflow.com/questions/51107120/partitions-reassignment-is-failing-in-kafka-1-1-0