forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_test.go
524 lines (442 loc) · 14.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package sarama
import (
"log"
"os"
"os/signal"
"sync"
"testing"
"time"
)
func TestConsumerOffsetManual(t *testing.T) {
seedBroker := newMockBroker(t, 1)
leader := newMockBroker(t, 2)
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
for i := 0; i <= 10; i++ {
fetchResponse := new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i+1234))
leader.Returns(fetchResponse)
}
master, err := NewConsumer([]string{seedBroker.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
consumer, err := master.ConsumePartition("my_topic", 0, 1234)
if err != nil {
t.Fatal(err)
}
seedBroker.Close()
for i := 0; i < 10; i++ {
select {
case message := <-consumer.Messages():
if message.Offset != int64(i+1234) {
t.Error("Incorrect message offset!")
}
case err := <-consumer.Errors():
t.Error(err)
}
}
safeClose(t, consumer)
safeClose(t, master)
leader.Close()
}
func TestConsumerLatestOffset(t *testing.T) {
seedBroker := newMockBroker(t, 1)
leader := newMockBroker(t, 2)
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
offsetResponse := new(OffsetResponse)
offsetResponse.AddTopicPartition("my_topic", 0, 0x010101)
leader.Returns(offsetResponse)
fetchResponse := new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), 0x010101)
leader.Returns(fetchResponse)
master, err := NewConsumer([]string{seedBroker.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
seedBroker.Close()
consumer, err := master.ConsumePartition("my_topic", 0, OffsetNewest)
if err != nil {
t.Fatal(err)
}
leader.Close()
safeClose(t, consumer)
safeClose(t, master)
// we deliver one message, so it should be one higher than we return in the OffsetResponse
if consumer.(*partitionConsumer).offset != 0x010102 {
t.Error("Latest offset not fetched correctly:", consumer.(*partitionConsumer).offset)
}
}
func TestConsumerFunnyOffsets(t *testing.T) {
// for topics that are compressed and/or compacted (different things!) we have to be
// able to handle receiving offsets that are non-sequential (though still strictly increasing) and
// possibly starting prior to the actual value we requested
seedBroker := newMockBroker(t, 1)
leader := newMockBroker(t, 2)
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
fetchResponse := new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(1))
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(3))
leader.Returns(fetchResponse)
fetchResponse = new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(5))
leader.Returns(fetchResponse)
master, err := NewConsumer([]string{seedBroker.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
consumer, err := master.ConsumePartition("my_topic", 0, 2)
if err != nil {
t.Fatal(err)
}
message := <-consumer.Messages()
if message.Offset != 3 {
t.Error("Incorrect message offset!")
}
leader.Close()
seedBroker.Close()
safeClose(t, consumer)
safeClose(t, master)
}
func TestConsumerRebalancingMultiplePartitions(t *testing.T) {
// initial setup
seedBroker := newMockBroker(t, 1)
leader0 := newMockBroker(t, 2)
leader1 := newMockBroker(t, 3)
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader0.Addr(), leader0.BrokerID())
metadataResponse.AddBroker(leader1.Addr(), leader1.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader0.BrokerID(), nil, nil, ErrNoError)
metadataResponse.AddTopicPartition("my_topic", 1, leader1.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
// launch test goroutines
config := NewConfig()
config.Consumer.Retry.Backoff = 0
master, err := NewConsumer([]string{seedBroker.Addr()}, config)
if err != nil {
t.Fatal(err)
}
// we expect to end up (eventually) consuming exactly ten messages on each partition
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
consumer, err := master.ConsumePartition("my_topic", int32(i), 0)
if err != nil {
t.Error(err)
}
go func(c PartitionConsumer) {
for err := range c.Errors() {
t.Error(err)
}
}(consumer)
wg.Add(1)
go func(partition int32, c PartitionConsumer) {
for i := 0; i < 10; i++ {
message := <-consumer.Messages()
if message.Offset != int64(i) {
t.Error("Incorrect message offset!", i, partition, message.Offset)
}
if message.Partition != partition {
t.Error("Incorrect message partition!")
}
}
safeClose(t, consumer)
wg.Done()
}(int32(i), consumer)
}
// leader0 provides first four messages on partition 0
fetchResponse := new(FetchResponse)
for i := 0; i < 4; i++ {
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i))
}
leader0.Returns(fetchResponse)
// leader0 says no longer leader of partition 0
fetchResponse = new(FetchResponse)
fetchResponse.AddError("my_topic", 0, ErrNotLeaderForPartition)
leader0.Returns(fetchResponse)
// metadata assigns both partitions to leader1
metadataResponse = new(MetadataResponse)
metadataResponse.AddTopicPartition("my_topic", 0, leader1.BrokerID(), nil, nil, ErrNoError)
metadataResponse.AddTopicPartition("my_topic", 1, leader1.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
time.Sleep(50 * time.Millisecond) // dumbest way to force a particular response ordering
// leader1 provides five messages on partition 1
fetchResponse = new(FetchResponse)
for i := 0; i < 5; i++ {
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i))
}
leader1.Returns(fetchResponse)
// leader1 provides three more messages on both partitions
fetchResponse = new(FetchResponse)
for i := 0; i < 3; i++ {
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i+4))
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i+5))
}
leader1.Returns(fetchResponse)
// leader1 provides three more messages on partition0, says no longer leader of partition1
fetchResponse = new(FetchResponse)
for i := 0; i < 3; i++ {
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i+7))
}
fetchResponse.AddError("my_topic", 1, ErrNotLeaderForPartition)
leader1.Returns(fetchResponse)
// metadata assigns 0 to leader1 and 1 to leader0
metadataResponse = new(MetadataResponse)
metadataResponse.AddTopicPartition("my_topic", 0, leader1.BrokerID(), nil, nil, ErrNoError)
metadataResponse.AddTopicPartition("my_topic", 1, leader0.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
time.Sleep(50 * time.Millisecond) // dumbest way to force a particular response ordering
// leader0 provides two messages on partition 1
fetchResponse = new(FetchResponse)
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(8))
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(9))
leader0.Returns(fetchResponse)
// leader0 provides last message on partition 1
fetchResponse = new(FetchResponse)
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(10))
leader0.Returns(fetchResponse)
// leader1 provides last message on partition 0
fetchResponse = new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(10))
leader1.Returns(fetchResponse)
wg.Wait()
leader1.Close()
leader0.Close()
seedBroker.Close()
safeClose(t, master)
}
func TestConsumerInterleavedClose(t *testing.T) {
t.Skip("Enable once bug #325 is fixed.")
seedBroker := newMockBroker(t, 1)
leader := newMockBroker(t, 2)
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
config := NewConfig()
config.ChannelBufferSize = 0
master, err := NewConsumer([]string{seedBroker.Addr()}, config)
if err != nil {
t.Fatal(err)
}
c0, err := master.ConsumePartition("my_topic", 0, 0)
if err != nil {
t.Fatal(err)
}
fetchResponse := new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(0))
leader.Returns(fetchResponse)
c1, err := master.ConsumePartition("my_topic", 1, 0)
if err != nil {
t.Fatal(err)
}
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(0))
leader.Returns(fetchResponse)
safeClose(t, c1)
safeClose(t, c0)
safeClose(t, master)
leader.Close()
seedBroker.Close()
}
func TestConsumerBounceWithReferenceOpen(t *testing.T) {
seedBroker := newMockBroker(t, 1)
leader := newMockBroker(t, 2)
leaderAddr := leader.Addr()
metadataResponse := new(MetadataResponse)
metadataResponse.AddBroker(leader.Addr(), leader.BrokerID())
metadataResponse.AddTopicPartition("my_topic", 0, leader.BrokerID(), nil, nil, ErrNoError)
metadataResponse.AddTopicPartition("my_topic", 1, leader.BrokerID(), nil, nil, ErrNoError)
seedBroker.Returns(metadataResponse)
config := NewConfig()
config.Consumer.Return.Errors = true
config.Consumer.Retry.Backoff = 0
config.ChannelBufferSize = 0
master, err := NewConsumer([]string{seedBroker.Addr()}, config)
if err != nil {
t.Fatal(err)
}
c0, err := master.ConsumePartition("my_topic", 0, 0)
if err != nil {
t.Fatal(err)
}
c1, err := master.ConsumePartition("my_topic", 1, 0)
if err != nil {
t.Fatal(err)
}
fetchResponse := new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(0))
fetchResponse.AddError("my_topic", 1, ErrNoError)
leader.Returns(fetchResponse)
<-c0.Messages()
fetchResponse = new(FetchResponse)
fetchResponse.AddError("my_topic", 0, ErrNoError)
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(0))
leader.Returns(fetchResponse)
<-c1.Messages()
leader.Close()
leader = newMockBrokerAddr(t, 2, leaderAddr)
// unblock one of the two (it doesn't matter which)
select {
case <-c0.Errors():
case <-c1.Errors():
}
// send it back to the same broker
seedBroker.Returns(metadataResponse)
fetchResponse = new(FetchResponse)
fetchResponse.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(1))
fetchResponse.AddMessage("my_topic", 1, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(1))
leader.Returns(fetchResponse)
time.Sleep(5 * time.Millisecond)
// unblock the other one
select {
case <-c0.Errors():
case <-c1.Errors():
}
// send it back to the same broker
seedBroker.Returns(metadataResponse)
select {
case <-c0.Messages():
case <-c1.Messages():
}
leader.Close()
seedBroker.Close()
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
_ = c0.Close()
wg.Done()
}()
go func() {
_ = c1.Close()
wg.Done()
}()
wg.Wait()
safeClose(t, master)
}
// This example has the simplest use case of the consumer. It simply
// iterates over the messages channel using a for/range loop. Because
// a producer never stopsunless requested, a signal handler is registered
// so we can trigger a clean shutdown of the consumer.
func ExampleConsumer_for_loop() {
master, err := NewConsumer([]string{"localhost:9092"}, nil)
if err != nil {
log.Fatalln(err)
}
defer func() {
if err := master.Close(); err != nil {
log.Fatalln(err)
}
}()
consumer, err := master.ConsumePartition("my_topic", 0, 0)
if err != nil {
log.Fatalln(err)
}
go func() {
// By default, the consumer will always keep going, unless we tell it to stop.
// In this case, we capture the SIGINT signal so we can tell the consumer to stop
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
<-signals
consumer.AsyncClose()
}()
msgCount := 0
for message := range consumer.Messages() {
log.Println(string(message.Value))
msgCount++
}
log.Println("Processed", msgCount, "messages.")
}
// This example shows how to use a consumer with a select statement
// dealing with the different channels.
func ExampleConsumer_select() {
config := NewConfig()
config.Consumer.Return.Errors = true // Handle errors manually instead of letting Sarama log them.
master, err := NewConsumer([]string{"localhost:9092"}, config)
if err != nil {
log.Fatalln(err)
}
defer func() {
if err := master.Close(); err != nil {
log.Fatalln(err)
}
}()
consumer, err := master.ConsumePartition("my_topic", 0, 0)
if err != nil {
log.Fatalln(err)
}
defer func() {
if err := consumer.Close(); err != nil {
log.Fatalln(err)
}
}()
msgCount := 0
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
consumerLoop:
for {
select {
case err := <-consumer.Errors():
log.Println(err)
case <-consumer.Messages():
msgCount++
case <-signals:
log.Println("Received interrupt")
break consumerLoop
}
}
log.Println("Processed", msgCount, "messages.")
}
// This example shows how to use a consumer with different goroutines
// to read from the Messages and Errors channels.
func ExampleConsumer_goroutines() {
config := NewConfig()
config.Consumer.Return.Errors = true // Handle errors manually instead of letting Sarama log them.
master, err := NewConsumer([]string{"localhost:9092"}, config)
if err != nil {
log.Fatalln(err)
}
defer func() {
if err := master.Close(); err != nil {
panic(err)
}
}()
consumer, err := master.ConsumePartition("my_topic", 0, OffsetOldest)
if err != nil {
log.Fatalln(err)
}
var (
wg sync.WaitGroup
msgCount int
)
wg.Add(1)
go func() {
defer wg.Done()
for message := range consumer.Messages() {
log.Printf("Consumed message with offset %d", message.Offset)
msgCount++
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for err := range consumer.Errors() {
log.Println(err)
}
}()
// Wait for an interrupt signal to trigger the shutdown
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
<-signals
consumer.AsyncClose()
// Wait for the Messages and Errors channel to be fully drained.
wg.Wait()
log.Println("Processed", msgCount, "messages.")
}