Debian Codegraph @

examples/alter_partition_reassignments/README.md

raw
1 # Alter partition reassignments example
2
3 This example shows how to use `ClusterAdmin.AlterPartitionReassignments` to
4 change the replication factor of an existing topic, plus how to track the
5 reassignment to completion with `ListPartitionReassignments`.
6
7 ```bash
8 $ go run main.go -brokers="127.0.0.1:9092" -topic my-topic -replication-factor 3
9 ```
10
11 ## What `replicas []int32` actually means
12
13 `AlterPartitionReassignments` takes one `[]int32` per partition. Each slice is
14 the **full target replica set** for that partition, not a delta against the
15 current set: the first entry is the preferred leader, and the rest are
16 followers, in order. Brokers omitted from the slice are removed; brokers added
17 to it are added.
18
19 To raise the replication factor without disturbing the current leader, read the
20 existing assignment first (`DescribeTopics`), then append additional broker IDs
21 to each partition's replica list:
22
23 ```go
24 admin, _ := sarama.NewClusterAdmin(brokers, config)
25 defer admin.Close()
26
27 meta, _ := admin.DescribeTopics([]string{topic})
28 brokers, _, _ := admin.DescribeCluster()
29
30 target := make([][]int32, len(meta[0].Partitions))
31 for _, p := range meta[0].Partitions {
32 replicas := append([]int32(nil), p.Replicas...)
33 for _, b := range brokers {
34 if !slices.Contains(replicas, b.ID()) && len(replicas) < targetRF {
35 replicas = append(replicas, b.ID())
36 }
37 }
38 target[p.ID] = replicas
39 }
40
41 if err := admin.AlterPartitionReassignments(topic, target); err != nil {
42 log.Fatal(err)
43 }
44 ```
45
46 The `[][]int32` argument is indexed by partition ID, so partition `N` must be
47 at index `N`. The example in `main.go` handles this plus uneven existing
48 distributions, and balances new replicas across the least-used brokers in the
49 cluster.
50
51 ## Waiting for the move to complete
52
53 The call above only submits the request. The actual replica movement happens
54 in the background on the controller. Poll `ListPartitionReassignments` to
55 observe progress — the broker only returns partitions that are still moving,
56 so an empty response for the topic means the reassignment is done:
57
58 ```go
59 for {
60 status, _ := admin.ListPartitionReassignments(topic, partitions)
61 if len(status[topic]) == 0 {
62 break
63 }
64 time.Sleep(2 * time.Second)
65 }
66 ```
67
68 ## Flags
69
70 | flag | description |
71 | --- | --- |
72 | `-brokers` | Bootstrap brokers, comma separated |
73 | `-topic` | Topic whose partitions should be reassigned |
74 | `-replication-factor` | Target replication factor for every partition |
75 | `-version` | Kafka cluster version (must be 2.4 or newer; AlterPartitionReassignments was added in KIP-455) |
76 | `-poll-interval` | Interval between `ListPartitionReassignments` polls |
77 | `-poll-timeout` | Maximum time to wait for an in-progress reassignment to complete |
78 | `-verbose` | Enable sarama logging |