forked from elodina/go_kafka_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk_coordinator.go
1270 lines (1130 loc) · 45.4 KB
/
zk_coordinator.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package go_kafka_client
import (
"encoding/json"
"errors"
"fmt"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/samuel/go-zookeeper/zk"
)
var (
consumersPath = "/consumers"
brokerIdsPath = "/brokers/ids"
brokerTopicsPath = "/brokers/topics"
)
type GroupWatch struct {
coordinatorEvents chan CoordinatorEvent
zkEvents chan zk.Event
poisonPillMessage string
}
// ZookeeperCoordinator implements ConsumerCoordinator and OffsetStorage interfaces and is used to coordinate multiple consumers that work within the same consumer group
// as well as storing and retrieving their offsets.
type ZookeeperCoordinator struct {
config *ZookeeperConfig
zkConn *zk.Conn
unsubscribe chan bool
closed bool
watches map[string]*GroupWatch
}
func (this *ZookeeperCoordinator) String() string {
return "zk"
}
// Creates a new ZookeeperCoordinator with a given configuration.
// The new created ZookeeperCoordinator does NOT automatically connect to zookeeper, you should call Connect() explicitly
func NewZookeeperCoordinator(Config *ZookeeperConfig) *ZookeeperCoordinator {
return &ZookeeperCoordinator{
config: Config,
unsubscribe: make(chan bool),
watches: make(map[string]*GroupWatch),
}
}
/* Establish connection to this ConsumerCoordinator. Returns an error if fails to connect, nil otherwise. */
func (this *ZookeeperCoordinator) Connect() (err error) {
var connectionEvents <-chan zk.Event
for i := 0; i <= this.config.MaxRequestRetries; i++ {
this.zkConn, connectionEvents, err = this.tryConnect()
if err == nil {
go this.listenConnectionEvents(connectionEvents)
return
}
Tracef(this, "Zookeeper connect failed after %d-th retry", i)
time.Sleep(this.config.RequestBackoff)
}
return
}
func (this *ZookeeperCoordinator) tryConnect() (zkConn *zk.Conn, connectionEvents <-chan zk.Event, err error) {
Infof(this, "Connecting to ZK at %s\n", this.config.ZookeeperConnect)
zkConn, connectionEvents, err = zk.Connect(this.config.ZookeeperConnect, this.config.ZookeeperSessionTimeout)
return
}
func (this *ZookeeperCoordinator) Disconnect() {
Infof(this, "Closing connection to ZK at %s\n", this.config.ZookeeperConnect)
this.closed = true
this.zkConn.Close()
}
func (this *ZookeeperCoordinator) listenConnectionEvents(connectionEvents <-chan zk.Event) {
for event := range connectionEvents { // will be closed by zk.Conn when it's stopped
Infof(this, "Received zkConnectionEvent Type: %s Server: %s State: %s", event.Type.String(), event.Server, event.State.String())
switch event.State {
case zk.StateConnecting:
// (Re)connecting to a ZK server
// Nothing to do
case zk.StateConnected:
// (Re)connected to a ZK server (TCP layer)
// We have no idea about ZK session at this moment
// Nothing to do
case zk.StateExpired:
// Failed to reuse the previous session (timeout)
// Existing watchers will be discarded
// Exsiting ephemeral nodes will be removed
for _, watch := range this.watches {
watch.coordinatorEvents <- Reinitialize
}
case zk.StateHasSession:
// Got an new or existing session
// Nothing to do
}
}
Infof(this, "Stopping listening connection events")
}
/* Registers a new consumer with Consumerid id and TopicCount subscription that is a part of consumer group Groupid in this ConsumerCoordinator. Returns an error if registration failed, nil otherwise. */
func (this *ZookeeperCoordinator) RegisterConsumer(Consumerid string, Groupid string, TopicCount TopicsToNumStreams) (err error) {
backoffMultiplier := 1
this.ensureZkPathsExist(Groupid)
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err = this.tryRegisterConsumer(Consumerid, Groupid, TopicCount)
if err == nil {
return
}
Tracef(this, "Registering consumer %s in group %s failed after %d-th retry", Consumerid, Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryRegisterConsumer(Consumerid string, Groupid string, TopicCount TopicsToNumStreams) (err error) {
Debugf(this, "Trying to register consumer %s at group %s in Zookeeper", Consumerid, Groupid)
registryDir := newZKGroupDirs(this.config.Root, Groupid).ConsumerRegistryDir
pathToConsumer := fmt.Sprintf("%s/%s", registryDir, Consumerid)
data, mappingError := json.Marshal(&ConsumerInfo{
Version: int16(1),
Subscription: TopicCount.GetTopicsToNumStreamsMap(),
Pattern: TopicCount.Pattern(),
Timestamp: time.Now().Unix() * 1000,
})
if mappingError != nil {
return mappingError
}
Debugf(this, "Path: %s", pathToConsumer)
_, err = this.zkConn.Create(pathToConsumer, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
if err == zk.ErrNoNode {
err = this.createOrUpdatePathParentMayNotExistFailFast(registryDir, make([]byte, 0))
if err != nil {
return
}
_, err = this.zkConn.Create(pathToConsumer, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
} else if err == zk.ErrNodeExists {
var stat *zk.Stat
_, stat, err = this.zkConn.Get(pathToConsumer)
if err != nil {
Debugf(this, "%v; path: %s", err, pathToConsumer)
return err
}
_, err = this.zkConn.Set(pathToConsumer, data, stat.Version)
if err != nil {
Debugf(this, "%v; path: %s", err, pathToConsumer)
return err
}
}
return
}
/* Deregisters consumer with Consumerid id that is a part of consumer group Groupid form this ConsumerCoordinator. Returns an error if deregistration failed, nil otherwise. */
func (this *ZookeeperCoordinator) DeregisterConsumer(Consumerid string, Groupid string) (err error) {
path := fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, Groupid).ConsumerRegistryDir, Consumerid)
Debugf(this, "Trying to deregister consumer at path: %s", path)
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err = this.deleteNode(path)
if err == nil {
return
}
Tracef(this, "Deregistering consumer %s in group %s failed after %d-th retry", Consumerid, Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
// Gets the information about consumer with Consumerid id that is a part of consumer group Groupid from this ConsumerCoordinator.
// Returns ConsumerInfo on success and error otherwise (For example if consumer with given Consumerid does not exist).
func (this *ZookeeperCoordinator) GetConsumerInfo(Consumerid string, Groupid string) (info *ConsumerInfo, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
info, err = this.tryGetConsumerInfo(Consumerid, Groupid)
if err == nil {
return
}
Tracef(this, "GetConsumerInfo failed for consumer %s in group %s after %d-th retry", Consumerid, Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetConsumerInfo(Consumerid string, Groupid string) (*ConsumerInfo, error) {
zkPath := fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, Groupid).ConsumerRegistryDir, Consumerid)
data, _, err := this.zkConn.Get(zkPath)
if err != nil {
Debugf(this, "%v; path: %s", err, zkPath)
return nil, err
}
type consumerInfoTmp struct {
Version int16
Subscription map[string]int
Pattern string
Timestamp json.RawMessage
}
tmpInfo := &consumerInfoTmp{}
err = json.Unmarshal(data, tmpInfo)
if err != nil {
return nil, fmt.Errorf("%v Path: %s, Data: %s", err, zkPath, string(data))
}
ts, convErr := fixTimestamp(tmpInfo.Timestamp)
if convErr != nil {
return nil, fmt.Errorf("%v Path: %s, Data: %s", err, zkPath, string(data))
}
consumerInfo := &ConsumerInfo{Version: tmpInfo.Version, Subscription: tmpInfo.Subscription, Pattern: tmpInfo.Pattern, Timestamp: ts}
return consumerInfo, nil
}
func fixTimestamp(b json.RawMessage) (int64, error) {
var s string
var i int64
var err error
err = json.Unmarshal(b, &s)
if err == nil {
var n int64
n, err = strconv.ParseInt(s, 10, 64)
if err == nil {
return n, nil
}
}
err = json.Unmarshal(b, &i)
if err == nil {
return i, nil
}
return 0, fmt.Errorf("Unable to convert raw value %+v to int64", b)
}
// Gets the information about consumers per topic in consumer group Groupid excluding internal topics (such as offsets) if ExcludeInternalTopics = true.
// Returns a map where keys are topic names and values are slices of consumer ids and fetcher ids associated with this topic and error on failure.
func (this *ZookeeperCoordinator) GetConsumersPerTopic(Groupid string, ExcludeInternalTopics bool) (consumers map[string][]ConsumerThreadId, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
consumers, err = this.tryGetConsumersPerTopic(Groupid, ExcludeInternalTopics)
if err == nil {
return
}
Tracef(this, "GetConsumersPerTopic failed for group %s after %d-th retry", Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetConsumersPerTopic(Groupid string, ExcludeInternalTopics bool) (map[string][]ConsumerThreadId, error) {
consumers, err := this.GetConsumersInGroup(Groupid)
if err != nil {
return nil, err
}
consumersPerTopicMap := make(map[string][]ConsumerThreadId)
for _, consumer := range consumers {
topicsToNumStreams, err := NewTopicsToNumStreams(Groupid, consumer, this, ExcludeInternalTopics)
if err != nil {
return nil, err
}
for topic, threadIds := range topicsToNumStreams.GetConsumerThreadIdsPerTopic() {
for _, threadId := range threadIds {
consumersPerTopicMap[topic] = append(consumersPerTopicMap[topic], threadId)
}
}
}
for topic := range consumersPerTopicMap {
sort.Sort(byName(consumersPerTopicMap[topic]))
}
return consumersPerTopicMap, nil
}
/* Gets the list of all consumer ids within a consumer group Groupid. Returns a slice containing all consumer ids in group and error on failure. */
func (this *ZookeeperCoordinator) GetConsumersInGroup(Groupid string) (consumers []string, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
consumers, err = this.tryGetConsumersInGroup(Groupid)
if err == nil {
return
}
Tracef(this, "GetConsumersInGroup failed for group %s after %d-th retry", Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetConsumersInGroup(Groupid string) (consumers []string, err error) {
Debugf(this, "Getting consumers in group %s", Groupid)
zkPath := newZKGroupDirs(this.config.Root, Groupid).ConsumerRegistryDir
consumers, _, err = this.zkConn.Children(zkPath)
if err != nil {
Debugf(this, "%v; path: %s", err, zkPath)
return nil, err
}
return
}
/* Gets the list of all topics registered in this ConsumerCoordinator. Returns a slice conaining topic names and error on failure. */
func (this *ZookeeperCoordinator) GetAllTopics() (topics []string, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
topics, err = this.tryGetAllTopics()
if err == nil {
return
}
Tracef(this, "GetAllTopics failed after %d-th retry", i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) rootedPath(path string) string {
return this.config.Root + path
}
func (this *ZookeeperCoordinator) tryGetAllTopics() (topics []string, err error) {
zkPath := this.rootedPath(brokerTopicsPath)
topics, _, err = this.zkConn.Children(zkPath)
if err != nil {
Debugf(this, "%v; path: %s", err, zkPath)
return nil, err
}
return
}
// Gets the information about existing partitions for a given Topics.
// Returns a map where keys are topic names and values are slices of partition ids associated with this topic and error on failure.
func (this *ZookeeperCoordinator) GetPartitionsForTopics(Topics []string) (partitions map[string][]int32, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
partitions, err = this.tryGetPartitionsForTopics(Topics)
if err == nil {
return
}
Tracef(this, "GetPartitionsForTopics for topics %s failed after %d-th retry", Topics, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetPartitionsForTopics(Topics []string) (map[string][]int32, error) {
result := make(map[string][]int32)
partitionAssignments, err := this.getPartitionAssignmentsForTopics(Topics)
if err != nil {
return nil, err
}
for topic, partitionAssignment := range partitionAssignments {
for partition, _ := range partitionAssignment {
result[topic] = append(result[topic], partition)
}
}
for topic, _ := range partitionAssignments {
sort.Sort(intArray(result[topic]))
}
return result, nil
}
// Gets the information about all Kafka brokers registered in this ConsumerCoordinator.
// Returns a slice of BrokerInfo and error on failure.
func (this *ZookeeperCoordinator) GetAllBrokers() (brokers []*BrokerInfo, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
brokers, err = this.tryGetAllBrokers()
if err == nil {
return
}
Tracef(this, "GetAllBrokers failed after %d-th retry", i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetAllBrokers() ([]*BrokerInfo, error) {
Debug(this, "Getting all brokers in cluster")
zkPath := this.rootedPath(brokerIdsPath)
brokerIds, _, err := this.zkConn.Children(zkPath)
if err != nil {
Debugf(this, "%v; path: %s", err, zkPath)
return nil, err
}
brokers := make([]*BrokerInfo, len(brokerIds))
for i, brokerId := range brokerIds {
brokerIdNum, err := strconv.Atoi(brokerId)
if err != nil {
return nil, err
}
brokers[i], err = this.getBrokerInfo(int32(brokerIdNum))
if err != nil {
return nil, err
}
brokers[i].Id = int32(brokerIdNum)
}
return brokers, nil
}
// Gets the offset for a given topic, partition and consumer group.
// Returns offset on sucess, error otherwise.
func (this *ZookeeperCoordinator) GetOffset(Groupid string, topic string, partition int32) (offset int64, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
offset, err = this.tryGetOffsetForTopicPartition(Groupid, topic, partition)
if err == nil {
return
}
Tracef(this, "GetOffset for group %s, topic %s and partition %d failed after %d-th retry", Groupid, topic, partition, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetOffsetForTopicPartition(Groupid string, topic string, partition int32) (int64, error) {
dirs := newZKGroupTopicDirs(this.config.Root, Groupid, topic)
zkPath := fmt.Sprintf("%s/%d", dirs.ConsumerOffsetDir, partition)
offset, _, err := this.zkConn.Get(zkPath)
if err != nil {
if err == zk.ErrNoNode {
return InvalidOffset, nil
} else {
Debugf(this, "%v; path: %s", err, zkPath)
return InvalidOffset, err
}
}
offsetNum, err := strconv.Atoi(string(offset))
if err != nil {
return InvalidOffset, err
}
return int64(offsetNum), nil
}
// Subscribes for any change that should trigger consumer rebalance on consumer group Groupid in this ConsumerCoordinator.
// Returns a read-only channel of booleans that will get values on any significant coordinator event (e.g. new consumer appeared, new broker appeared etc.) and error if failed to subscribe.
func (this *ZookeeperCoordinator) SubscribeForChanges(Groupid string) (events <-chan CoordinatorEvent, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
events, err = this.trySubscribeForChanges(Groupid)
if err == nil {
return
}
Tracef(this, "SubscribeForChanges for group %s failed after %d-th retry", Groupid, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) trySubscribeForChanges(Groupid string) (<-chan CoordinatorEvent, error) {
var groupWatch *GroupWatch
if _, ok := this.watches[Groupid]; !ok {
groupWatch = &GroupWatch{
coordinatorEvents: make(chan CoordinatorEvent, 100),
poisonPillMessage: uuid(),
}
this.watches[Groupid] = groupWatch
} else {
groupWatch = this.watches[Groupid]
}
Infof(this, "Subscribing for changes for %s", Groupid)
zkEvents := make(chan zk.Event, 100)
this.watches[Groupid].zkEvents = zkEvents
consumersWatcher, err := this.getConsumersInGroupWatcher(Groupid)
if err != nil {
return nil, err
}
blueGreenWatcher, err := this.getBlueGreenWatcher(Groupid)
if err != nil {
return nil, err
}
topicsWatcher, err := this.getTopicsWatcher()
if err != nil {
return nil, err
}
brokersWatcher, err := this.getAllBrokersInClusterWatcher()
if err != nil {
return nil, err
}
inputChannels := make([]*<-chan zk.Event, 0)
inputChannels = append(inputChannels, &consumersWatcher, &blueGreenWatcher, &topicsWatcher, &brokersWatcher)
stopRedirecting := redirectChannelsTo(inputChannels, zkEvents)
go func() {
for {
select {
case e := <-zkEvents:
{
Infof(this, "Received zkEvent Type: %s State: %s Path: %s", e.Type.String(), e.State.String(), e.Path)
if e.Type != zk.EventNotWatching && e.State != zk.StateDisconnected {
if strings.HasPrefix(e.Path, fmt.Sprintf("%s/%s",
newZKGroupDirs(this.config.Root, Groupid).ConsumerApiDir, BlueGreenDeploymentAPI)) {
groupWatch.coordinatorEvents <- BlueGreenRequest
} else {
groupWatch.coordinatorEvents <- Regular
}
}
if strings.HasPrefix(e.Path, newZKGroupDirs(this.config.Root, Groupid).ConsumerRegistryDir) {
Info(this, "Trying to renew watcher for consumer registry")
consumersWatcher, err = this.getConsumersInGroupWatcher(Groupid)
if err != nil {
this.config.PanicHandler(err)
}
} else if strings.HasPrefix(e.Path, fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, Groupid).ConsumerApiDir, BlueGreenDeploymentAPI)) {
Info(this, "Trying to renew watcher for consumer API dir")
blueGreenWatcher, err = this.getBlueGreenWatcher(Groupid)
if err != nil {
this.config.PanicHandler(err)
}
} else if strings.HasPrefix(e.Path, this.rootedPath(brokerTopicsPath)) {
Info(this, "Trying to renew watcher for consumer topic dir")
topicsWatcher, err = this.getTopicsWatcher()
if err != nil {
this.config.PanicHandler(err)
}
} else if strings.HasPrefix(e.Path, this.rootedPath(brokerIdsPath)) {
Info(this, "Trying to renew watcher for brokers in cluster")
brokersWatcher, err = this.getAllBrokersInClusterWatcher()
if err != nil {
this.config.PanicHandler(err)
}
} else {
Warnf(this, "Unknown event path: %s", e.Path)
}
stopRedirecting <- true
stopRedirecting = redirectChannelsTo(inputChannels, zkEvents)
}
case <-this.unsubscribe:
{
stopRedirecting <- true
return
}
}
}
}()
return groupWatch.coordinatorEvents, nil
}
// Gets all deployed topics for consume group Group from consumer coordinator.
// Returns a map where keys are notification ids and values are DeployedTopics. May also return an error (e.g. if failed to reach coordinator).
func (this *ZookeeperCoordinator) GetBlueGreenRequest(Group string) (topics map[string]*BlueGreenDeployment, err error) {
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
topics, err = this.tryGetBlueGreenRequest(Group)
if err == nil {
return
}
Tracef(this, "GetNewDeployedTopics for group %s failed after %d-th retry", Group, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return
}
func (this *ZookeeperCoordinator) tryGetBlueGreenRequest(Group string) (map[string]*BlueGreenDeployment, error) {
apiPath := fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, Group).ConsumerApiDir, BlueGreenDeploymentAPI)
children, _, err := this.zkConn.Children(apiPath)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to get new deployed topics %s: %s", err.Error(), apiPath))
}
deployedTopics := make(map[string]*BlueGreenDeployment)
for _, child := range children {
entryPath := fmt.Sprintf("%s/%s", apiPath, child)
rawDeployedTopicsEntry, _, err := this.zkConn.Get(entryPath)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to fetch deployed topic entry %s: %s", err.Error(), entryPath))
}
deployedTopicsEntry := &BlueGreenDeployment{}
err = json.Unmarshal(rawDeployedTopicsEntry, deployedTopicsEntry)
if err != nil {
return nil, errors.New(fmt.Sprintf("Unable to parse deployed topic entry %s: %s", err.Error(), rawDeployedTopicsEntry))
}
deployedTopics[child] = deployedTopicsEntry
}
return deployedTopics, nil
}
func (this *ZookeeperCoordinator) RequestBlueGreenDeployment(blue BlueGreenDeployment, green BlueGreenDeployment) error {
var err error
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err := this.tryRequestBlueGreenDeployment(green.Group, blue)
if err == nil {
break
}
Tracef(this, "DeployTopics for group %s and topics %s failed after %d-th retry", green.Group, blue.Topics, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
if err != nil {
return err
}
backoffMultiplier = 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err := this.tryRequestBlueGreenDeployment(blue.Group, green)
if err == nil {
return err
}
Tracef(this, "DeployTopics for group %s and topics %s failed after %d-th retry", blue.Group, green.Topics, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return err
}
func (this *ZookeeperCoordinator) tryRequestBlueGreenDeployment(Group string, blueOrGreen BlueGreenDeployment) error {
data, err := json.Marshal(blueOrGreen)
if err != nil {
return err
}
return this.createOrUpdatePathParentMayNotExistFailFast(fmt.Sprintf("%s/%s/%d", newZKGroupDirs(this.config.Root, Group).ConsumerApiDir, BlueGreenDeploymentAPI, time.Now().Unix()), data)
}
func (this *ZookeeperCoordinator) RemoveOldApiRequests(group string) (err error) {
for _, api := range availableAPIs {
err = this.tryRemoveOldApiRequests(group, api)
if err != nil {
break
}
}
return
}
func (this *ZookeeperCoordinator) tryRemoveOldApiRequests(group string, api ConsumerGroupApi) error {
requests := make([]string, 0)
var err error
apiPath := fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, group).ConsumerApiDir, api)
for i := 0; i <= this.config.MaxRequestRetries; i++ {
requests, _, err = this.zkConn.Children(apiPath)
if err != nil {
continue
}
for _, request := range requests {
var data []byte
var t int64
childPath := fmt.Sprintf("%s/%s", apiPath, request)
if api == Rebalance {
if data, _, err = this.zkConn.Get(childPath); err != nil && err == zk.ErrNoNode {
// It's possible another consumer deleted the node before we could read it's data
continue
}
if t, err = strconv.ParseInt(string(data), 10, 64); err != nil {
t = int64(0) // If the data isn't a timestamp ensure it will be deleted anyway.
}
} else if api == BlueGreenDeploymentAPI {
if t, err = strconv.ParseInt(string(request), 10, 64); err != nil {
break
}
}
// Delete if this zk node has an expired timestamp
if time.Unix(t, 0).Before(time.Now().Add(-10 * time.Minute)) {
// If the data is not a timestamp or is a timestamp but has reached expiration delete it
err = this.deleteNode(childPath)
if err != nil && err != zk.ErrNoNode {
break
}
}
}
}
return err
}
func (this *ZookeeperCoordinator) AwaitOnStateBarrier(consumerId string, group string, barrierName string,
barrierSize int, api string, timeout time.Duration) bool {
barrierPath := fmt.Sprintf("%s/%s/%s", newZKGroupDirs(this.config.Root, group).ConsumerApiDir, api, barrierName)
var barrierExpiration time.Time
var err error
// Block and wait for this to consumerId to join the state barrier
if barrierExpiration, err = this.joinStateBarrier(barrierPath, consumerId, timeout); err == nil {
// Now that we've joined the barrier wait to verify all consumers have reached consensus.
barrierTimeout := barrierExpiration.Sub(time.Now())
err = this.waitForMembersToJoin(barrierPath, barrierSize, barrierTimeout)
}
if err != nil {
// Encountered an error waiting for consensus... Fail it
Errorf(this, "Failed awaiting on state barrier %s [%v]", barrierName, err)
return false
}
Infof(this, "Successfully awaited on state barrier %s", barrierName)
return true
}
func (this *ZookeeperCoordinator) joinStateBarrier(barrierPath, consumerId string, timeout time.Duration) (time.Time, error) {
deadline := time.Now().Add(timeout)
var err error
Infof(this, "Joining state barrier %s", barrierPath)
for i := 0; i <= this.config.MaxRequestRetries; i++ {
// Attempt to create the barrier path, with a shared deadline
_, err = this.zkConn.Create(barrierPath, []byte(strconv.FormatInt(deadline.Unix(), 10)), 0, zk.WorldACL(zk.PermAll))
if err != nil {
if err != zk.ErrNodeExists {
continue
}
// If the barrier path already exists, read it's value
if data, _, err := this.zkConn.Get(barrierPath); err == nil {
deadlineInt, _ := strconv.ParseInt(string(data), 10, 64)
deadline = time.Unix(deadlineInt, 0)
Infof(this, "Barrier already exists with deadline set to %v. Joining...", deadline)
} else {
continue
}
}
// Register our consumerId as a child node on the barrierPath. This should notify other consumers we have joined.
// Need to join as an ephemeral node to ensure that if the barrier Id is re-used we aren't permanently registered giving false counts.
if _, err = this.zkConn.Create(fmt.Sprintf("%s/%s", barrierPath, consumerId), make([]byte, 0), zk.FlagEphemeral, zk.WorldACL(zk.PermAll)); err == nil || err == zk.ErrNodeExists {
Infof(this, "Successfully joined state barrier %s", barrierPath)
return deadline, nil
}
Warnf(this, "Failed to join state barrier %s, retrying...", barrierPath)
}
return time.Now(), fmt.Errorf("Failed to join state barrier %s after %d retries [%v]", barrierPath, this.config.MaxRequestRetries, err)
}
func (this *ZookeeperCoordinator) waitForMembersToJoin(barrierPath string, expected int, timeout time.Duration) error {
// Will be used to make sure we don't leave the zk watcher channel without someone to receive events off it.
blackholeFunc := func(blackhole <-chan zk.Event) {
<-blackhole
}
t := time.NewTimer(timeout)
defer t.Stop()
for {
select {
// Using a priority select to provide precedence to the timeout
case <-t.C:
return fmt.Errorf("Timed out waiting for consensus on barrier path %s", barrierPath)
default:
children, _, zkMemberJoinedWatcher, err := this.zkConn.ChildrenW(barrierPath)
if err != nil && err == zk.ErrNoNode {
return fmt.Errorf("%v; path: %s", err, barrierPath)
} else if len(children) == expected {
// don't leave the zkMemberJoinedWatcher chan out there with no one to receive the message it produces later as it would cause a block.
go blackholeFunc(zkMemberJoinedWatcher)
return nil
}
// Haven't seen all expected consumers on this barrier path. Watch for changes to the path...
select {
case <-t.C:
go blackholeFunc(zkMemberJoinedWatcher)
return fmt.Errorf("Timed out waiting for consensus on barrier path %s", barrierPath)
case <-zkMemberJoinedWatcher:
continue
}
}
}
return nil
}
func (this *ZookeeperCoordinator) RemoveStateBarrier(group string, stateHash string, api string) error {
var err error
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err = this.tryRemoveStateBarrier(group, stateHash, api)
if err == nil || err == zk.ErrNoNode {
return nil
}
Tracef(this, "State assertion deletion %s in group %s failed after %d-th retry", hash, group, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return err
}
func (this *ZookeeperCoordinator) tryRemoveStateBarrier(group string, stateHash string, api string) error {
path := fmt.Sprintf("%s/%s/%s", newZKGroupDirs(this.config.Root, group).ConsumerApiDir, api, stateHash)
Debugf(this, "Trying to fail rebalance at path: %s", path)
return this.deleteNode(path)
}
func (this *ZookeeperCoordinator) deleteNode(path string) error {
children, _, err := this.zkConn.Children(path)
if err != nil {
Debugf(this, "%v; path: %s", err, path)
return err
}
for _, child := range children {
err := this.deleteNode(fmt.Sprintf("%s/%s", path, child))
if err != nil && err != zk.ErrNoNode {
return err
}
}
_, stat, err := this.zkConn.Get(path)
if err != nil {
Debugf(this, "%v; path: %s", err, path)
return err
}
return this.zkConn.Delete(path, stat.Version)
}
/* Tells the ConsumerCoordinator to unsubscribe from events for the consumer it is associated with. */
func (this *ZookeeperCoordinator) Unsubscribe() {
this.unsubscribe <- true
}
// Tells the ConsumerCoordinator to claim partition topic Topic and partition Partition for consumerThreadId fetcher that works within a consumer group Group.
// Returns true if claim is successful, false and error explaining failure otherwise.
func (this *ZookeeperCoordinator) ClaimPartitionOwnership(Groupid string, Topic string, Partition int32, consumerThreadId ConsumerThreadId) (bool, error) {
var err error
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
ok, err := this.tryClaimPartitionOwnership(Groupid, Topic, Partition, consumerThreadId)
if ok {
return ok, err
}
Tracef(this, "Claim failed for topic %s, partition %d after %d-th retry", Topic, Partition, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return false, err
}
func (this *ZookeeperCoordinator) tryClaimPartitionOwnership(group string, topic string, partition int32, consumerThreadId ConsumerThreadId) (bool, error) {
dirs := newZKGroupTopicDirs(this.config.Root, group, topic)
this.createOrUpdatePathParentMayNotExistFailFast(dirs.ConsumerOwnerDir, make([]byte, 0))
pathToOwn := fmt.Sprintf("%s/%d", dirs.ConsumerOwnerDir, partition)
_, err := this.zkConn.Create(pathToOwn, []byte(consumerThreadId.String()), zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
if err == zk.ErrNoNode {
err = this.createOrUpdatePathParentMayNotExistFailFast(dirs.ConsumerOwnerDir, make([]byte, 0))
if err != nil {
return false, err
}
_, err = this.zkConn.Create(pathToOwn, []byte(consumerThreadId.String()), zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}
if err != nil {
if err == zk.ErrNodeExists {
var data []byte
if data, _, err = this.zkConn.Get(pathToOwn); err == nil && string(data) == consumerThreadId.String() {
// If the current owner of the partition is the same consumer Id as the current one, carry on.
return true, nil
}
Debugf(consumerThreadId, "waiting for the partition ownership to be deleted: %d", partition)
return false, nil
} else {
Error(consumerThreadId, err)
return false, err
}
}
Debugf(this, "Successfully claimed partition %d in topic %s for %s", partition, topic, consumerThreadId)
return true, nil
}
// Tells the ConsumerCoordinator to release partition ownership on topic Topic and partition Partition for consumer group Groupid.
// Returns error if failed to released partition ownership.
func (this *ZookeeperCoordinator) ReleasePartitionOwnership(Groupid string, Topic string, Partition int32) error {
var err error
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
err = this.tryReleasePartitionOwnership(Groupid, Topic, Partition)
if err == nil {
return err
}
Tracef(this, "ReleasePartitionOwnership failed for group %s, topic %s, partition %d after %d-th retry", Groupid, Topic, Partition, i)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return err
}
func (this *ZookeeperCoordinator) tryReleasePartitionOwnership(group string, topic string, partition int32) error {
path := fmt.Sprintf("%s/%d", newZKGroupTopicDirs(this.config.Root, group, topic).ConsumerOwnerDir, partition)
err := this.deleteNode(path)
if err != nil && err != zk.ErrNoNode {
return err
} else {
return nil
}
}
// Tells the ConsumerCoordinator to commit offset Offset for topic and partition TopicPartition for consumer group Groupid.
// Returns error if failed to commit offset.
func (this *ZookeeperCoordinator) CommitOffset(Groupid string, Topic string, Partition int32, Offset int64) error {
dirs := newZKGroupTopicDirs(this.config.Root, Groupid, Topic)
err := this.updateRecord(fmt.Sprintf("%s/%d", dirs.ConsumerOffsetDir, Partition), []byte(strconv.FormatInt(Offset, 10)))
if err == zk.ErrNoNode {
return this.createOrUpdatePathParentMayNotExistFailFast(fmt.Sprintf("%s/%d", dirs.ConsumerOffsetDir, Partition), []byte(strconv.FormatInt(Offset, 10)))
}
return err
}
func (this *ZookeeperCoordinator) ensureZkPathsExist(group string) {
dirs := newZKGroupDirs(this.config.Root, group)
this.createOrUpdatePathParentMayNotExistFailSafe(dirs.ConsumerDir, make([]byte, 0))
this.createOrUpdatePathParentMayNotExistFailSafe(dirs.ConsumerGroupDir, make([]byte, 0))
this.createOrUpdatePathParentMayNotExistFailSafe(dirs.ConsumerRegistryDir, make([]byte, 0))
this.createOrUpdatePathParentMayNotExistFailSafe(dirs.ConsumerApiDir, make([]byte, 0))
for _, api := range availableAPIs {
this.createOrUpdatePathParentMayNotExistFailSafe(fmt.Sprintf("%s/%s", dirs.ConsumerApiDir, api), make([]byte, 0))
}
}
func (this *ZookeeperCoordinator) getWatcher(path string) (<-chan zk.Event, error) {
Debugf(this, "Getting watcher for %s", path)
var watcher <-chan zk.Event
var err error
backoffMultiplier := 1
for i := 0; i <= this.config.MaxRequestRetries; i++ {
_, _, watcher, err = this.zkConn.ChildrenW(path)
if err == nil {
return watcher, err
}
Debugf(this, "%v; path: %s", err, path)
time.Sleep(this.config.RequestBackoff * time.Duration(backoffMultiplier))
backoffMultiplier++
}
return nil, err
}
func (this *ZookeeperCoordinator) getAllBrokersInClusterWatcher() (<-chan zk.Event, error) {
return this.getWatcher(this.rootedPath(brokerIdsPath))
}
func (this *ZookeeperCoordinator) getConsumersInGroupWatcher(group string) (<-chan zk.Event, error) {
return this.getWatcher(newZKGroupDirs(this.config.Root, group).ConsumerRegistryDir)
}
func (this *ZookeeperCoordinator) getBlueGreenWatcher(group string) (<-chan zk.Event, error) {
return this.getWatcher(fmt.Sprintf("%s/%s", newZKGroupDirs(this.config.Root, group).ConsumerApiDir, BlueGreenDeploymentAPI))
}
func (this *ZookeeperCoordinator) getTopicsWatcher() (<-chan zk.Event, error) {
return this.getWatcher(this.rootedPath(brokerTopicsPath))
}
func (this *ZookeeperCoordinator) getBrokerInfo(brokerId int32) (*BrokerInfo, error) {
Debugf(this, "Getting info for broker %d", brokerId)
pathToBroker := fmt.Sprintf("%s/%d", this.rootedPath(brokerIdsPath), brokerId)
data, _, zkError := this.zkConn.Get(pathToBroker)
if zkError != nil {
Debugf(this, "%v; path: %s", zkError, pathToBroker)
return nil, zkError
}
broker := &BrokerInfo{}
mappingError := json.Unmarshal([]byte(data), broker)
return broker, mappingError