-
Notifications
You must be signed in to change notification settings - Fork 6
/
kafkabalancer.go
160 lines (134 loc) · 4.12 KB
/
kafkabalancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"github.com/cafxx/kafkabalancer/logbuf"
"github.com/pkg/profile"
)
type BrokerID int
type PartitionID int
type TopicName string
type PartitionList struct {
Version int `json:"version"`
Partitions []Partition `json:"partitions"`
}
type Partition struct {
Topic TopicName `json:"topic"`
Partition PartitionID `json:"partition"`
Replicas []BrokerID `json:"replicas"`
// extensions
Weight float64 `json:"weight,omitempty"` // default: 1.0
NumReplicas int `json:"num_replicas,omitempty"` // default: len(replicas)
Brokers []BrokerID `json:"brokers,omitempty"` // default: (auto)
NumConsumers int `json:"num_consumers,omitempty"` // default: 1
}
func main() {
os.Exit(run(os.Stdin, os.Stdout, os.Stderr, os.Args))
}
func run(i io.Reader, o io.Writer, e io.Writer, args []string) int {
be := logbuf.NewDefaultBufferingWriter(e)
defer be.Close()
log.SetOutput(be)
f := flag.NewFlagSet("kafkabalancer", flag.ContinueOnError)
f.SetOutput(be)
jsonInput := f.Bool("input-json", false, "Parse the input as JSON")
input := f.String("input", "", "Name of the file to read (if no file is specified read from stdin, can not be used with -from-zk)")
fromZK := f.String("from-zk", "", "Zookeeper connection string (can not be used with -input)")
maxReassign := f.Int("max-reassign", 1, "Maximum number of reassignments to generate")
fullOutput := f.Bool("full-output", false, "Output the full partition list: by default only the changes are printed")
pprof := f.Bool("pprof", false, "Enable CPU profiling")
allowLeader := f.Bool("allow-leader", DefaultRebalanceConfig().AllowLeaderRebalancing, "Consider the partition leader eligible for rebalancing")
minReplicas := f.Int("min-replicas", DefaultRebalanceConfig().MinReplicasForRebalancing, "Minimum number of replicas for a partition to be eligible for rebalancing")
minUnbalance := f.Float64("min-unbalance", DefaultRebalanceConfig().MinUnbalance, "Minimum unbalance value required to perform rebalancing")
brokerIDs := f.String("broker-ids", "auto", "Comma-separated list of broker IDs")
help := f.Bool("help", false, "Display usage")
f.Usage = func() {
fmt.Fprintf(be, "Usage of %s:\n", args[0])
f.PrintDefaults()
}
f.Parse(args[1:])
if *pprof {
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
}
if *help {
f.Usage()
return 0
}
var brokers []BrokerID
if *brokerIDs != "auto" {
for _, broker := range strings.Split(*brokerIDs, ",") {
b, cerr := strconv.Atoi(broker)
if cerr != nil {
log.Printf("failed parsing broker list \"%s\": %s", *brokerIDs, cerr)
f.Usage()
return 3
}
brokers = append(brokers, BrokerID(b))
}
}
if *maxReassign < 0 {
log.Printf("invalid number of max reassignments \"%d\"", *maxReassign)
f.Usage()
return 3
}
if *input != "" && *fromZK != "" {
log.Print("can't specify both -input and -from-zk")
f.Usage()
return 3
}
var err error
in := i
if *input != "" {
in, err = os.Open(*input)
if err != nil {
log.Printf("failed opening file %s: %s", *input, err)
return 1
}
defer in.(io.Closer).Close()
}
out := o
var pl *PartitionList
if *fromZK != "" {
pl, err = GetPartitionListFromZookeeper(*fromZK)
} else {
pl, err = GetPartitionListFromReader(in, *jsonInput)
}
if err != nil {
log.Printf("failed getting partition list: %s", err)
return 2
}
cfg := RebalanceConfig{
AllowLeaderRebalancing: *allowLeader,
MinReplicasForRebalancing: *minReplicas,
MinUnbalance: *minUnbalance,
Brokers: brokers,
}
log.Printf("rebalance config: %+v", cfg)
opl := emptypl()
for i := 0; i < *maxReassign; i++ {
ppl, err := Balance(pl, cfg)
if err != nil {
log.Printf("failed optimizing distribution: %s", err)
return 3
}
if len(ppl.Partitions) == 0 {
break
}
opl.Partitions = append(opl.Partitions, ppl.Partitions...)
}
be.Flush(true)
if *fullOutput {
opl = pl
}
err = WritePartitionList(out, opl)
if err != nil {
log.Printf("failed writing partition list: %s", err)
return 4
}
return 0
}