forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
balancer.go
275 lines (229 loc) · 6.83 KB
/
balancer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package cluster
import (
"container/ring"
"math"
"sort"
"github.com/Shopify/sarama"
)
// NotificationType defines the type of notification
type NotificationType uint8
// String describes the notification type
func (t NotificationType) String() string {
switch t {
case RebalanceStart:
return "rebalance start"
case RebalanceOK:
return "rebalance OK"
case RebalanceError:
return "rebalance error"
}
return "unknown"
}
const (
UnknownNotification NotificationType = iota
RebalanceStart
RebalanceOK
RebalanceError
)
// Notification are state events emitted by the consumers on rebalance
type Notification struct {
// Type exposes the notification type
Type NotificationType
// Claimed contains topic/partitions that were claimed by this rebalance cycle
Claimed map[string][]int32
// Released contains topic/partitions that were released as part of this rebalance cycle
Released map[string][]int32
// Current are topic/partitions that are currently claimed to the consumer
Current map[string][]int32
}
func newNotification(current map[string][]int32) *Notification {
return &Notification{
Type: RebalanceStart,
Current: current,
}
}
func (n *Notification) success(current map[string][]int32) *Notification {
o := &Notification{
Type: RebalanceOK,
Claimed: make(map[string][]int32),
Released: make(map[string][]int32),
Current: current,
}
for topic, partitions := range current {
o.Claimed[topic] = int32Slice(partitions).Diff(int32Slice(n.Current[topic]))
}
for topic, partitions := range n.Current {
o.Released[topic] = int32Slice(partitions).Diff(int32Slice(current[topic]))
}
return o
}
// --------------------------------------------------------------------
// Assignor is a function which returns specific partition assignments
// given the set of topic subscriptions of a given group.
type Assignor func(subs *Subscriptions, topics []*TopicPartitions) Assignments
// TopicPartitions identifies a topic and its partition IDs.
type TopicPartitions struct {
Name string
Partitions []int32
}
// Subscriptions contains information about all members in a consumer
// group, and which topics they have subscribed to.
type Subscriptions struct {
memberIDs []string
subscribers map[string][]string
}
// NewSubscriptions returns an empty set of subscriptions.
func NewSubscriptions() *Subscriptions {
return &Subscriptions{
memberIDs: []string{},
subscribers: map[string][]string{},
}
}
// Members returns the list of all member IDs in the group.
func (m *Subscriptions) Members() []string {
return m.memberIDs
}
// AddSubscriber registers a member as subscribed to a topic.
// Returns self.
func (m *Subscriptions) AddSubscriber(memberID, topic string) *Subscriptions {
seen := false
for i := range m.memberIDs {
if m.memberIDs[i] == memberID {
seen = true
}
}
if !seen {
m.memberIDs = append(m.memberIDs, memberID)
}
m.subscribers[topic] = append(m.subscribers[topic], memberID)
return m
}
// SubscribedMembers returns the full list of members subscribed
// to a topic.
func (m *Subscriptions) SubscribedMembers(topic string) []string {
return m.subscribers[topic]
}
// IsSubscribed returns true if a member is subscribed to a topic.
func (m *Subscriptions) IsSubscribed(memberID, topic string) bool {
subs, ok := m.subscribers[topic]
if !ok {
return false
}
for i := range subs {
if subs[i] == memberID {
return true
}
}
return false
}
// Assignments is a mapping of member IDs to the topic partitions that they
// have been assigned.
type Assignments map[string]map[string][]int32
// NewAssignments returns an empty set of assignments.
func NewAssignments() Assignments {
return map[string]map[string][]int32{}
}
// Assign adds a partition to the list of a member's assignments.
func (a Assignments) Assign(memberID, topic string, partition int32) {
topics, ok := a[memberID]
if !ok {
topics = map[string][]int32{}
a[memberID] = topics
}
topics[topic] = append(topics[topic], partition)
}
type balancer struct {
client sarama.Client
subs *Subscriptions
topics []*TopicPartitions
}
func newBalancerFromMeta(client sarama.Client, members map[string]sarama.ConsumerGroupMemberMetadata) (*balancer, error) {
balancer := &balancer{
client: client,
subs: NewSubscriptions(),
topics: []*TopicPartitions{},
}
for memberID, meta := range members {
for _, topic := range meta.Topics {
balancer.subs.AddSubscriber(memberID, topic)
if err := balancer.AddTopic(topic); err != nil {
return nil, err
}
}
}
return balancer, nil
}
func (r *balancer) AddTopic(name string) error {
for i := range r.topics {
if r.topics[i].Name == name {
return nil
}
}
nums, err := r.client.Partitions(name)
if err != nil {
return err
}
r.topics = append(r.topics, &TopicPartitions{
Name: name,
Partitions: nums,
})
return nil
}
func (r *balancer) Perform(fn Assignor) Assignments {
return fn(r.subs, r.topics)
}
// RangeAssignor assigns partitions to subscribed group members by
// dividing the number of partitions, per-topic, by the number of
// consumers to determine the number of partitions per consumer that
// should be assigned. If the value does not evenly divide, consumers
// lexicographically earlier will be assigned an extra partition.
func RangeAssignor(subs *Subscriptions, topics []*TopicPartitions) Assignments {
assignments := NewAssignments()
sort.Slice(topics, func(i, j int) bool { return topics[i].Name < topics[j].Name })
for _, tp := range topics {
members := subs.SubscribedMembers(tp.Name)
mlen := len(members)
plen := len(tp.Partitions)
sort.Strings(members)
for pos, memberID := range members {
n, i := float64(plen)/float64(mlen), float64(pos)
min := int(math.Floor(i*n + 0.5))
max := int(math.Floor((i+1)*n + 0.5))
sub := tp.Partitions[min:max]
for i := range sub {
assignments.Assign(memberID, tp.Name, sub[i])
}
}
}
return assignments
}
// RoundRobinAssignor assigns partitions by iterating through the
// list of group members and assigning one to each consumer until
// all partitions have been assigned. If a group member is not
// subscribed to a topic, the next subscribed member is assigned
// instead.
func RoundRobinAssignor(subs *Subscriptions, topics []*TopicPartitions) Assignments {
assignments := NewAssignments()
memberIDs := subs.Members()
sort.Strings(memberIDs)
r := ring.New(len(memberIDs))
for i := 0; i < r.Len(); i++ {
r.Value = memberIDs[i]
r = r.Next()
}
sort.Slice(topics, func(i, j int) bool { return topics[i].Name < topics[j].Name })
for _, tp := range topics {
if len(subs.SubscribedMembers(tp.Name)) == 0 {
continue
}
partitions := tp.Partitions
for i := range partitions {
for ; !subs.IsSubscribed(r.Value.(string), tp.Name); r = r.Next() {
continue
}
assignments.Assign(r.Value.(string), tp.Name, partitions[i])
r = r.Next()
}
}
return assignments
}