参考site:https://cwiki.apache.org/confluence/display/KAFKA/Replication+tools
目前我们的topic test-add-replication-topic中partition 0的replicas为[1,2],则1为preferred replica,应该成为leader。
这时我们期望2为preferred replica,并变成leader。
操作步骤:
1. 查看当前topic详细信息:
> bin/kafka-topics.sh --describe --zookeeper h1:2181 --topic test-add-replication-topic
Topic:test-add-replication-topic PartitionCount:3 ReplicationFactor:3 Configs:
Topic: test-add-replication-topic Partition: 0 Leader: 1 Replicas: 1,2 Isr: 1,2
Topic: test-add-replication-topic Partition: 1 Leader: 2 Replicas: 2,0,1 Isr: 0,1,2
Topic: test-add-replication-topic Partition: 2 Leader: 0 Replicas: 0,1 Isr: 0,1
2. 修改replicas顺序
> cat partitions-to-move.json
{
"version":1,
"partitions":
[
{
"topic":"test-add-replication-topic",
"partition":0,
"replicas":[2,1]
}
]
}
> bin/kafka-reassign-partitions.sh --zookeeper h1:2181 --reassignment-json-file partitions-to-move.json --execute
Current partition replica assignment
{"version":1,"partitions":[{"topic":"test-add-replication-topic","partition":1,"replicas":[2,0,1]},{"topic":"test-add-replication-topic","partition":2,"replicas":[0,1]},
{"topic":"test-add-replication-topic","partition":0,"replicas":[1,2]}]}
Save this to use as the --reassignment-json-file option during rollback
Successfully started reassignment of partitions {"version":1,"partitions":[{"topic":"test-add-replication-topic","partition":0,"replicas":[2,1]}]}
3. 更改leader
> cat topicPartition.json
{
"version":1,
"partitions":
[
{
"topic":"test-add-replication-topic",
"partition":0
}
]
}
> bin/kafka-preferred-replica-election.sh --zookeeper h1:2181 --path-to-json-file topicPartition.json
Successfully started preferred replica election for partitions Set([test-add-replication-topic,0])
4.检查replicas leader切换情况
> bin/kafka-topics.sh --describe --zookeeper h1:2181 --topic test-add-replication-topic
Topic:test-add-replication-topic PartitionCount:3 ReplicationFactor:3 Configs:
Topic: test-add-replication-topic Partition: 0 Leader: 2 Replicas: 2,1 Isr: 1,2
Topic: test-add-replication-topic Partition: 1 Leader: 2 Replicas: 2,0,1 Isr: 0,1,2
Topic: test-add-replication-topic Partition: 2 Leader: 0 Replicas: 0,1 Isr: 0,1
到此,partition 0中的leader已经修改成功。