forked from bsm/sarama-cluster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsumer_test.go
361 lines (298 loc) · 10.6 KB
/
consumer_test.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package cluster
import (
"fmt"
"regexp"
"sync/atomic"
"time"
"github.com/Shopify/sarama"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Consumer", func() {
var newConsumerOf = func(group string, topics ...string) (*Consumer, error) {
config := NewConfig()
config.Consumer.Return.Errors = true
config.Consumer.Offsets.Initial = sarama.OffsetOldest
return NewConsumer(testKafkaAddrs, group, topics, config)
}
var subscriptionsOf = func(c *Consumer) GomegaAsyncAssertion {
return Eventually(func() map[string][]int32 {
return c.Subscriptions()
}, "10s", "100ms")
}
It("should init and share", func() {
// start CS1
cs1, err := newConsumerOf(testGroup, testTopics...)
Expect(err).NotTo(HaveOccurred())
// CS1 should consume all 8 partitions
subscriptionsOf(cs1).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3},
"topic-b": {0, 1, 2, 3},
}))
// start CS2
cs2, err := newConsumerOf(testGroup, testTopics...)
Expect(err).NotTo(HaveOccurred())
defer cs2.Close()
// CS1 and CS2 should consume 4 partitions each
subscriptionsOf(cs1).Should(HaveLen(2))
subscriptionsOf(cs1).Should(HaveKeyWithValue("topic-a", HaveLen(2)))
subscriptionsOf(cs1).Should(HaveKeyWithValue("topic-b", HaveLen(2)))
subscriptionsOf(cs2).Should(HaveLen(2))
subscriptionsOf(cs2).Should(HaveKeyWithValue("topic-a", HaveLen(2)))
subscriptionsOf(cs2).Should(HaveKeyWithValue("topic-b", HaveLen(2)))
// shutdown CS1, now CS2 should consume all 8 partitions
Expect(cs1.Close()).NotTo(HaveOccurred())
subscriptionsOf(cs2).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3},
"topic-b": {0, 1, 2, 3},
}))
})
It("should allow more consumers than partitions", func() {
cs1, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs1.Close()
cs2, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs2.Close()
cs3, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs3.Close()
cs4, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
// start 4 consumers, one for each partition
subscriptionsOf(cs1).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs2).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs3).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs4).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
// add a 5th consumer
cs5, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs5.Close()
// make sure no errors occurred
Expect(cs1.Errors()).ShouldNot(Receive())
Expect(cs2.Errors()).ShouldNot(Receive())
Expect(cs3.Errors()).ShouldNot(Receive())
Expect(cs4.Errors()).ShouldNot(Receive())
Expect(cs5.Errors()).ShouldNot(Receive())
// close 4th, make sure the 5th takes over
Expect(cs4.Close()).To(Succeed())
subscriptionsOf(cs1).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs2).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs3).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
subscriptionsOf(cs4).Should(BeEmpty())
subscriptionsOf(cs5).Should(HaveKeyWithValue("topic-a", HaveLen(1)))
// there should still be no errors
Expect(cs1.Errors()).ShouldNot(Receive())
Expect(cs2.Errors()).ShouldNot(Receive())
Expect(cs3.Errors()).ShouldNot(Receive())
Expect(cs4.Errors()).ShouldNot(Receive())
Expect(cs5.Errors()).ShouldNot(Receive())
})
It("should be allowed to subscribe to partitions via white/black-lists", func() {
config := NewConfig()
config.Consumer.Return.Errors = true
config.Group.Topics.Whitelist = regexp.MustCompile(`topic-\w+`)
config.Group.Topics.Blacklist = regexp.MustCompile(`[bcd]$`)
cs, err := NewConsumer(testKafkaAddrs, testGroup, nil, config)
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
subscriptionsOf(cs).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3},
}))
})
It("should receive rebalance notifications", func() {
config := NewConfig()
config.Consumer.Return.Errors = true
config.Group.Return.Notifications = true
cs, err := NewConsumer(testKafkaAddrs, testGroup, testTopics, config)
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
select {
case n := <-cs.Notifications():
Expect(n).To(Equal(&Notification{
Type: RebalanceStart,
Current: map[string][]int32{},
}))
case err := <-cs.Errors():
Expect(err).NotTo(HaveOccurred())
case <-cs.Messages():
Fail("expected notification to arrive before message")
}
select {
case n := <-cs.Notifications():
Expect(n).To(Equal(&Notification{
Type: RebalanceOK,
Claimed: map[string][]int32{
"topic-a": {0, 1, 2, 3},
"topic-b": {0, 1, 2, 3},
},
Released: map[string][]int32{},
Current: map[string][]int32{
"topic-a": {0, 1, 2, 3},
"topic-b": {0, 1, 2, 3},
},
}))
case err := <-cs.Errors():
Expect(err).NotTo(HaveOccurred())
case <-cs.Messages():
Fail("expected notification to arrive before message")
}
})
It("should support manual mark/commit", func() {
cs, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
subscriptionsOf(cs).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3}},
))
cs.MarkPartitionOffset("topic-a", 1, 3, "")
cs.MarkPartitionOffset("topic-a", 2, 4, "")
Expect(cs.CommitOffsets()).NotTo(HaveOccurred())
offsets, err := cs.fetchOffsets(cs.Subscriptions())
Expect(err).NotTo(HaveOccurred())
Expect(offsets).To(Equal(map[string]map[int32]offsetInfo{
"topic-a": {0: {Offset: -1}, 1: {Offset: 4}, 2: {Offset: 5}, 3: {Offset: -1}},
}))
})
It("should support rewind of offsets", func() {
rewindTime := time.Now().UnixNano() / int64(time.Millisecond)
cs, err := newConsumerOf(testGroup, "topic-a")
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
subscriptionsOf(cs).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3}},
))
cs.MarkPartitionOffset("topic-a", 1, 3, "")
cs.MarkPartitionOffset("topic-a", 2, 4, "")
Expect(cs.CommitOffsets()).NotTo(HaveOccurred())
offsets, err := cs.fetchOffsets(cs.Subscriptions())
Expect(err).NotTo(HaveOccurred())
Expect(offsets).To(Equal(map[string]map[int32]offsetInfo{
"topic-a": {0: {Offset: -1}, 1: {Offset: 4}, 2: {Offset: 5}, 3: {Offset: -1}},
}))
// get the offset to rewind
offset, err := testClient.GetOffset("topic-a", 1, rewindTime)
Expect(err).NotTo(HaveOccurred())
// Now use that offset to rewind
cs.subs.Fetch("topic-a", 1).RewindOffset(offset, "")
Expect(cs.CommitOffsets()).NotTo(HaveOccurred())
// verify if the offset rewind happened as expected
offsetsRewind, err := cs.fetchOffsets(cs.Subscriptions())
Expect(err).NotTo(HaveOccurred())
Expect(offsetsRewind).To(Equal(map[string]map[int32]offsetInfo{
"topic-a": {0: {Offset: -1}, 1: {Offset: offset}, 2: {Offset: 5}, 3: {Offset: -1}},
}))
})
It("should not commit unprocessed offsets", func() {
const groupID = "panicking"
cs, err := newConsumerOf(groupID, "topic-a")
Expect(err).NotTo(HaveOccurred())
subscriptionsOf(cs).Should(Equal(map[string][]int32{
"topic-a": {0, 1, 2, 3},
}))
n := 0
Expect(func() {
for range cs.Messages() {
n++
panic("stop here!")
}
}).To(Panic())
Expect(cs.Close()).To(Succeed())
Expect(n).To(Equal(1))
bk, err := testClient.Coordinator(groupID)
Expect(err).NotTo(HaveOccurred())
req := &sarama.OffsetFetchRequest{
Version: 1,
ConsumerGroup: groupID,
}
req.AddPartition("topic-a", 0)
req.AddPartition("topic-a", 1)
req.AddPartition("topic-a", 2)
req.AddPartition("topic-a", 3)
Expect(bk.FetchOffset(req)).To(Equal(&sarama.OffsetFetchResponse{
Blocks: map[string]map[int32]*sarama.OffsetFetchResponseBlock{
"topic-a": {0: {Offset: -1}, 1: {Offset: -1}, 2: {Offset: -1}, 3: {Offset: -1}},
},
}))
})
It("should consume partitions", func() {
count := int32(0)
consume := func(consumerID string) {
defer GinkgoRecover()
config := NewConfig()
config.Group.Mode = ConsumerModePartitions
config.Consumer.Offsets.Initial = sarama.OffsetOldest
cs, err := NewConsumer(testKafkaAddrs, "partitions", testTopics, config)
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
for pc := range cs.Partitions() {
go func(pc PartitionConsumer) {
defer pc.Close()
for msg := range pc.Messages() {
atomic.AddInt32(&count, 1)
cs.MarkOffset(msg, "")
}
}(pc)
}
}
go consume("A")
go consume("B")
go consume("C")
Eventually(func() int32 {
return atomic.LoadInt32(&count)
}, "30s", "100ms").Should(BeNumerically(">=", 2000))
})
It("should consume/commit/resume", func() {
acc := make(chan *testConsumerMessage, 20000)
consume := func(consumerID string, max int32) {
defer GinkgoRecover()
cs, err := NewConsumer(testKafkaAddrs, "fuzzing", testTopics, nil)
Expect(err).NotTo(HaveOccurred())
defer cs.Close()
cs.consumerID = consumerID
for msg := range cs.Messages() {
acc <- &testConsumerMessage{*msg, consumerID}
cs.MarkOffset(msg, "")
if atomic.AddInt32(&max, -1) <= 0 {
return
}
}
}
go consume("A", 1500)
go consume("B", 2000)
go consume("C", 1500)
go consume("D", 200)
go consume("E", 100)
Expect(testSeed(5000)).NotTo(HaveOccurred())
Eventually(func() int { return len(acc) }, "30s", "100ms").Should(BeNumerically(">=", 5000))
go consume("F", 300)
go consume("G", 400)
go consume("H", 1000)
go consume("I", 2000)
Expect(testSeed(5000)).NotTo(HaveOccurred())
Eventually(func() int { return len(acc) }, "30s", "100ms").Should(BeNumerically(">=", 8000))
go consume("J", 1000)
Expect(testSeed(5000)).NotTo(HaveOccurred())
Eventually(func() int { return len(acc) }, "30s", "100ms").Should(BeNumerically(">=", 9000))
go consume("K", 1000)
go consume("L", 3000)
Expect(testSeed(5000)).NotTo(HaveOccurred())
Eventually(func() int { return len(acc) }, "30s", "100ms").Should(BeNumerically(">=", 12000))
go consume("M", 1000)
Expect(testSeed(5000)).NotTo(HaveOccurred())
Eventually(func() int { return len(acc) }, "30s", "100ms").Should(BeNumerically(">=", 15000))
close(acc)
uniques := make(map[string][]string)
for msg := range acc {
key := fmt.Sprintf("%s/%d/%d", msg.Topic, msg.Partition, msg.Offset)
uniques[key] = append(uniques[key], msg.ConsumerID)
}
Expect(uniques).To(HaveLen(15000))
})
It("should allow close to be called multiple times", func() {
cs, err := newConsumerOf(testGroup, testTopics...)
Expect(err).NotTo(HaveOccurred())
Expect(cs.Close()).NotTo(HaveOccurred())
Expect(cs.Close()).NotTo(HaveOccurred())
})
})