-
Notifications
You must be signed in to change notification settings - Fork 48
/
functional_consumergroup_test.go
748 lines (624 loc) · 16.7 KB
/
functional_consumergroup_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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package kazoo
import (
"reflect"
"sync"
"testing"
"time"
"github.com/samuel/go-zookeeper/zk"
)
func TestConsumergroups(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroups")
cgs, err := kz.Consumergroups()
if err != nil {
t.Error(err)
}
originalCount := len(cgs)
if cg := cgs.Find(cg.Name); cg != nil {
t.Error("Consumergoup `test.kazoo.TestConsumergroups` should not be found")
}
if exists, _ := cg.Exists(); exists {
t.Error("Consumergoup `test.kazoo.TestConsumergroups` should not be registered yet")
}
if err := cg.Create(); err != nil {
t.Error(err)
}
if exists, _ := cg.Exists(); !exists {
t.Error("Consumergoup `test.kazoo.TestConsumergroups` should be registered now")
}
cgs, err = kz.Consumergroups()
if err != nil {
t.Error(err)
}
if len(cgs) != originalCount+1 {
t.Error("Should have one more consumergroup than at the start")
}
if err := cg.Delete(); err != nil {
t.Error(err)
}
if exists, _ := cg.Exists(); exists {
t.Error("Consumergoup `test.kazoo.TestConsumergroups` should not be registered anymore")
}
cgs, err = kz.Consumergroups()
if err != nil {
t.Error(err)
}
if len(cgs) != originalCount {
t.Error("Should have the original number of consumergroups again")
}
}
func TestConsumergroupInstances(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupInstances")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
if instances, err := cg.Instances(); err != nil {
t.Error(err)
} else if len(instances) != 0 {
t.Fatal("Expected no active consumergroup instances")
}
instance1 := cg.NewInstance()
// Make sure that the instance is unregistered.
if reg, err := instance1.Registration(); err != ErrInstanceNotRegistered || reg != nil {
t.Errorf("Expected no registration: reg=%v, err=(%v)", reg, err)
}
// Register a new instance
if instance1.ID == "" {
t.Error("It should generate a valid instance ID")
}
if err := instance1.Register([]string{"topic"}); err != nil {
t.Error(err)
}
// Verify registration
reg, err := instance1.Registration()
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(reg.Subscription, map[string]int{"topic": 1}) {
t.Errorf("Unexpected registration: %v", reg)
}
// Try to register an instance with the same ID.
if err := cg.Instance(instance1.ID).Register([]string{"topic"}); err != ErrInstanceAlreadyRegistered {
t.Error("The instance should already be registered")
}
instance2 := cg.Instance("test")
if err := instance2.Register([]string{"topic"}); err != nil {
t.Error(err)
}
time.Sleep(50 * time.Millisecond)
if instances, err := cg.Instances(); err != nil {
t.Error(err)
} else {
if len(instances) != 2 {
t.Error("Expected 2 active consumergroup instances")
}
if i := instances.Find(instance1.ID); i == nil {
t.Error("Expected instance1 to be registered.")
}
if i := instances.Find(instance2.ID); i == nil {
t.Error("Expected instance2 to be registered.")
}
}
// Deregister the two running instances
if err := instance1.Deregister(); err != nil {
t.Error(err)
}
if err := instance2.Deregister(); err != nil {
t.Error(err)
}
// Try to deregister an instance that was not register
instance3 := cg.NewInstance()
if err := instance3.Deregister(); err != ErrInstanceNotRegistered {
t.Error("Expected new instance to not be registered")
}
if instances, err := cg.Instances(); err != nil {
t.Error(err)
} else if len(instances) != 0 {
t.Error("Expected no active consumergroup instances")
}
}
func TestConsumergroupInstanceCrash(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupInstancesEphemeral")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
// Create a kazoo instance on which we will simulate a crash.
config := NewConfig()
config.Timeout = 50 * time.Millisecond
crashingKazoo, err := NewKazoo(zookeeperPeers, config)
if err != nil {
t.Fatal(err)
}
crashingCG := crashingKazoo.Consumergroup(cg.Name)
// Instantiate and register the instance.
instance := crashingCG.NewInstance()
if err := instance.Register([]string{"test.1"}); err != nil {
t.Error(err)
}
time.Sleep(50 * time.Millisecond)
if instances, err := cg.Instances(); err != nil {
t.Error(err)
} else if len(instances) != 1 {
t.Error("Should have 1 running instance, found", len(instances))
}
// Simulate a crash, and wait for Zookeeper to pick it up
_ = crashingKazoo.Close()
time.Sleep(200 * time.Millisecond)
if instances, err := cg.Instances(); err != nil {
t.Error(err)
} else if len(instances) != 0 {
t.Error("Should have 0 running instances")
}
}
func TestConsumergroupWatchInstances(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupWatchInstances")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
instances, c, err := cg.WatchInstances()
if err != nil {
t.Fatal(err)
}
if len(instances) != 0 {
t.Error("Expected 0 running instances")
}
instance := cg.NewInstance()
// Make sure a proper error is returned when an unregistered instance is
// updated.
err = instance.UpdateRegistration([]string{"foo"})
if err != ErrInstanceNotRegistered {
t.Fatal("Expected ErrInstanceNotRegistered")
}
if err := instance.Register([]string{"topic"}); err != nil {
t.Fatal(err)
}
// The instance watch should have been triggered
assertWatchTriggered(t, c, 5*time.Second)
instances, c, err = cg.WatchInstances()
if err != nil {
t.Fatal(err)
}
if len(instances) != 1 {
t.Error("Expected 1 running instance")
}
if err := instance.Deregister(); err != nil {
t.Fatal(err)
}
// The instance watch should have been triggered again
assertWatchTriggered(t, c, 5*time.Second)
instances, err = cg.Instances()
if err != nil {
t.Fatal(err)
}
if len(instances) != 0 {
t.Error("Expected 0 running instances")
}
}
func TestConsumergroupInstanceWatchRegistration(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupWatchRegistration")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
instance := cg.NewInstance()
if err := instance.Register([]string{"topic"}); err != nil {
t.Fatal(err)
}
// Set a watch and make sure that it does not trigger on its own.
reg, c, err := instance.WatchRegistration()
if err != nil {
t.Fatal(err)
}
if reflect.DeepEqual(reg.Subscription, map[string]int{}) {
t.Fatalf("Bad subscription, got=%v", reg.Subscription)
}
assertWatchNotTriggered(t, c, 200*time.Millisecond)
// The watch is triggered by an update.
if err = instance.UpdateRegistration([]string{"foo", "bar"}); err != nil {
t.Fatal(err)
}
assertWatchTriggered(t, c, 5*time.Second)
// Update registration.
if err = instance.UpdateRegistration([]string{"foo", "bazz"}); err != nil {
t.Fatal(err)
}
// Set a watch again.
reg, c, err = instance.WatchRegistration()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(reg.Subscription, map[string]int{"foo": 1, "bazz": 1}) {
t.Fatalf("Bad subscription, got=%v", reg.Subscription)
}
assertWatchNotTriggered(t, c, 200*time.Millisecond)
// A watch is triggered by any update operation even if the value does not
// change.
if err = instance.UpdateRegistration([]string{"foo", "bazz"}); err != nil {
t.Fatal(err)
}
assertWatchTriggered(t, c, 5*time.Second)
// Cleanup
if err := instance.Deregister(); err != nil {
t.Fatal(err)
}
}
func TestConsumergroupInstanceClaimPartition(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupInstanceClaimPartition")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
// Create two instances for this consumergroup
i1 := cg.NewInstance()
if err := i1.Register([]string{"test.4"}); err != nil {
t.Fatal(err)
}
i2 := cg.NewInstance()
if err := i2.Register([]string{"test.4"}); err != nil {
t.Fatal(err)
}
// Claim all partitions divided by instance 1 and 2
if err := i1.ClaimPartition("test.4", 0); err != nil {
t.Error(err)
}
if err := i1.ClaimPartition("test.4", 1); err != nil {
t.Error(err)
}
if err := i2.ClaimPartition("test.4", 2); err != nil {
t.Error(err)
}
if err := i2.ClaimPartition("test.4", 3); err != nil {
t.Error(err)
}
// Try to claim more partitions
if err := i1.ClaimPartition("test.4", 3); err != ErrPartitionClaimedByOther {
t.Error("Expected ErrPartitionClaimedByOther to be returned, found", err)
}
if err := i2.ClaimPartition("test.4", 0); err != ErrPartitionClaimedByOther {
t.Error("Expected ErrPartitionClaimedByOther to be returned, found", err)
}
// Instance 1: release some partitions
if err := i1.ReleasePartition("test.4", 0); err != nil {
t.Error(err)
}
if err := i1.ReleasePartition("test.4", 1); err != nil {
t.Error(err)
}
// Instance 2: claim the released partitions
if err := i2.ClaimPartition("test.4", 0); err != nil {
t.Error(err)
}
if err := i2.ClaimPartition("test.4", 1); err != nil {
t.Error(err)
}
// Instance 2: release all partitions
if err := i2.ReleasePartition("test.4", 0); err != nil {
t.Error(err)
}
if err := i2.ReleasePartition("test.4", 1); err != nil {
t.Error(err)
}
if err := i2.ReleasePartition("test.4", 2); err != nil {
t.Error(err)
}
if err := i2.ReleasePartition("test.4", 3); err != nil {
t.Error(err)
}
if err := i1.Deregister(); err != nil {
t.Error(err)
}
if err := i2.Deregister(); err != nil {
t.Error(err)
}
}
func TestConsumergroupInstanceClaimPartitionSame(t *testing.T) {
// Given
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupInstanceClaimPartition2")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
instance := cg.NewInstance()
if err := instance.Register([]string{"test.4"}); err != nil {
t.Fatal(err)
}
if err := instance.ClaimPartition("test.4", 0); err != nil {
t.Error(err)
}
// When: claim the same partition again
err = instance.ClaimPartition("test.4", 0)
// Then
if err != nil {
t.Error(err)
}
// Cleanup
if err := instance.ReleasePartition("test.4", 0); err != nil {
t.Error(err)
}
if err := instance.Deregister(); err != nil {
t.Error(err)
}
}
func TestConsumergroupInstanceWatchPartitionClaim(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupInstanceWatchPartitionClaim")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
instance1 := cg.NewInstance()
if err := instance1.Register([]string{"test.4"}); err != nil {
t.Fatal(err)
}
// Assert the partition isn't claimed
instance, change, err := cg.WatchPartitionOwner("test.4", 0)
if err != nil {
t.Fatal(err)
}
if instance != nil {
t.Fatal("An unclaimed partition should not return an instance")
}
if change != nil {
t.Fatal("An unclaimed partition should not return a watch")
}
// Now claim the partition
if err := instance1.ClaimPartition("test.4", 0); err != nil {
t.Fatal(err)
}
// This time, we should get an insance back
instance, change, err = cg.WatchPartitionOwner("test.4", 0)
if err != nil {
t.Fatal(err)
}
if instance.ID != instance1.ID {
t.Error("Our instance should have claimed the partition")
}
go func() {
time.Sleep(100 * time.Millisecond)
if err := instance1.ReleasePartition("test.4", 0); err != nil {
t.Fatal(err)
}
}()
// Wait for the zookeeper watch to trigger
<-change
// Ensure the partition is no longer claimed
instance, err = cg.PartitionOwner("test.4", 0)
if err != nil {
t.Fatal(err)
}
if instance != nil {
t.Error("The partition should have been release by now")
}
// Cleanup
if err := instance1.Deregister(); err != nil {
t.Error(err)
}
}
func TestConsumergroupOffsets(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupOffsets")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
offset, err := cg.FetchOffset("test", 0)
if err != nil {
t.Error(err)
}
if offset >= 0 {
t.Error("Expected to get a negative offset for a partition that hasn't seen an offset commit yet")
}
if err := cg.CommitOffset("test", 0, 1234); err != nil {
t.Error(err)
}
offset, err = cg.FetchOffset("test", 0)
if err != nil {
t.Error(err)
}
if offset != 1234 {
t.Error("Expected to get the offset that was committed.")
}
}
func TestConsumergroupResetOffsetsRace(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupResetOffsetsRace")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
offsets, err := cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if len(offsets) > 0 {
t.Errorf("A new consumergroup shouldn't have any offsets set, but found offsets for %d topics", len(offsets))
}
if err := cg.CommitOffset("test", 0, 1234); err != nil {
t.Error(err)
}
if err := cg.CommitOffset("test", 1, 2345); err != nil {
t.Error(err)
}
offsets, err = cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if offsets["test"][0] == 1234 && offsets["test"][1] == 2345 {
t.Log("All offsets present in offset map")
} else {
t.Logf("Offset map not as expected: %v", offsets)
}
cg2 := kz.Consumergroup("test.kazoo.TestConsumergroupResetOffsetsRace")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
if err := cg2.ResetOffsets(); err != nil {
t.Fatal(err)
}
}()
go func() {
defer wg.Done()
if err := cg.ResetOffsets(); err != nil {
t.Fatal(err)
}
}()
wg.Wait()
offsets, err = cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if len(offsets) > 0 {
t.Errorf("After a reset, consumergroup shouldn't have any offsets set, but found offsets for %d topics", len(offsets))
}
}
func TestConsumergroupResetOffsets(t *testing.T) {
kz, err := NewKazoo(zookeeperPeers, nil)
if err != nil {
t.Fatal(err)
}
defer assertSuccessfulClose(t, kz)
cg := kz.Consumergroup("test.kazoo.TestConsumergroupResetOffsets")
if err := cg.Create(); err != nil {
t.Fatal(err)
}
defer func() {
if err := cg.Delete(); err != nil {
t.Error(err)
}
}()
offsets, err := cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if len(offsets) > 0 {
t.Errorf("A new consumergroup shouldn't have any offsets set, but found offsets for %d topics", len(offsets))
}
if err := cg.CommitOffset("test1", 0, 1234); err != nil {
t.Error(err)
}
if err := cg.CommitOffset("test1", 1, 2345); err != nil {
t.Error(err)
}
if err := cg.CommitOffset("test2", 0, 3456); err != nil {
t.Error(err)
}
offsets, err = cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if offsets["test1"][0] == 1234 && offsets["test1"][1] == 2345 && offsets["test2"][0] == 3456 {
t.Log("All offsets present in offset map")
} else {
t.Logf("Offset map not as expected: %v", offsets)
}
if err := cg.ResetOffsets(); err != nil {
t.Fatal(err)
}
offsets, err = cg.FetchAllOffsets()
if err != nil {
t.Error(err)
}
if len(offsets) > 0 {
t.Errorf("After a reset, consumergroup shouldn't have any offsets set, but found offsets for %d topics", len(offsets))
}
}
func assertWatchTriggered(t *testing.T, ch <-chan zk.Event, timeout time.Duration) {
select {
case <-ch:
case <-time.After(timeout):
t.Fatal("Watch is not triggered")
}
}
func assertWatchNotTriggered(t *testing.T, ch <-chan zk.Event, timeout time.Duration) {
select {
case <-ch:
t.Fatal("Watch is not supposed to be triggered")
case <-time.After(timeout):
}
}