forked from linkedin/Burrow
/
kafka_client.go
635 lines (564 loc) · 17.6 KB
/
kafka_client.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
/* Copyright 2017 LinkedIn Corp. Licensed 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.
*/
package consumer
import (
"bytes"
"encoding/binary"
"errors"
"sync"
"github.com/Shopify/sarama"
"go.uber.org/zap"
"github.com/linkedin/Burrow/core/internal/helpers"
"github.com/linkedin/Burrow/core/protocol"
"github.com/spf13/viper"
"regexp"
)
// KafkaClient is a consumer module which connects to a single Apache Kafka cluster and reads consumer group information
// from the offsets topic in the cluster, which is typically __consumer_offsets. The messages in this topic are decoded
// and the information is forwarded to the storage subsystem for use in evaluations.
type KafkaClient struct {
// App is a pointer to the application context. This stores the channel to the storage subsystem
App *protocol.ApplicationContext
// Log is a logger that has been configured for this module to use. Normally, this means it has been set up with
// fields that are appropriate to identify this coordinator
Log *zap.Logger
name string
cluster string
servers []string
offsetsTopic string
startLatest bool
saramaConfig *sarama.Config
groupWhitelist *regexp.Regexp
groupBlacklist *regexp.Regexp
quitChannel chan struct{}
running sync.WaitGroup
}
type offsetKey struct {
Group string
Topic string
Partition int32
ErrorAt string
}
type offsetValue struct {
Offset int64
Timestamp int64
ErrorAt string
}
type metadataHeader struct {
ProtocolType string
Generation int32
Protocol string
Leader string
}
type metadataMember struct {
MemberID string
ClientID string
ClientHost string
RebalanceTimeout int32
SessionTimeout int32
Assignment map[string][]int32
}
// Configure validates the configuration for the consumer. At minimum, there must be a cluster name to which these
// consumers belong, as well as a list of servers provided for the Kafka cluster, of the form host:port. If not
// explicitly configured, the offsets topic is set to the default for Kafka, which is __consumer_offsets. If the
// cluster name is unknown, or if the server list is missing or invalid, this func will panic.
func (module *KafkaClient) Configure(name string, configRoot string) {
module.Log.Info("configuring")
module.name = name
module.quitChannel = make(chan struct{})
module.running = sync.WaitGroup{}
module.cluster = viper.GetString(configRoot + ".cluster")
if !viper.IsSet("cluster." + module.cluster) {
panic("Consumer '" + name + "' references an unknown cluster '" + module.cluster + "'")
}
profile := viper.GetString(configRoot + ".client-profile")
module.saramaConfig = helpers.GetSaramaConfigFromClientProfile(profile)
module.servers = viper.GetStringSlice(configRoot + ".servers")
if len(module.servers) == 0 {
panic("No Kafka brokers specified for consumer " + module.name)
} else if !helpers.ValidateHostList(module.servers) {
panic("Consumer '" + name + "' has one or more improperly formatted servers (must be host:port)")
}
// Set defaults for configs if needed, and get them
viper.SetDefault(configRoot+".offsets-topic", "__consumer_offsets")
module.offsetsTopic = viper.GetString(configRoot + ".offsets-topic")
module.startLatest = viper.GetBool(configRoot + ".start-latest")
whitelist := viper.GetString(configRoot + ".group-whitelist")
if whitelist != "" {
re, err := regexp.Compile(whitelist)
if err != nil {
module.Log.Panic("Failed to compile group whitelist")
panic(err)
}
module.groupWhitelist = re
}
blacklist := viper.GetString(configRoot + ".group-blacklist")
if blacklist != "" {
re, err := regexp.Compile(blacklist)
if err != nil {
module.Log.Panic("Failed to compile group blacklist")
panic(err)
}
module.groupBlacklist = re
}
}
// Start connects to the Kafka cluster using the Shopify/sarama client. Any error connecting to the cluster is returned
// to the caller. Once the client is set up, the consumers for the configured offsets topic are started.
func (module *KafkaClient) Start() error {
module.Log.Info("starting")
// Connect Kafka client
client, err := sarama.NewClient(module.servers, module.saramaConfig)
if err != nil {
module.Log.Error("failed to start client", zap.Error(err))
return err
}
// Start the consumers
err = module.startKafkaConsumer(&helpers.BurrowSaramaClient{Client: client})
if err != nil {
module.Log.Error("failed to start consumer", zap.Error(err))
client.Close()
return err
}
return nil
}
// Stop closes the goroutines that listen to the client consumer.
func (module *KafkaClient) Stop() error {
module.Log.Info("stopping")
close(module.quitChannel)
module.running.Wait()
return nil
}
func (module *KafkaClient) partitionConsumer(consumer sarama.PartitionConsumer) {
defer module.running.Done()
defer consumer.AsyncClose()
for {
select {
case msg := <-consumer.Messages():
module.processConsumerOffsetsMessage(msg)
case err := <-consumer.Errors():
module.Log.Error("consume error",
zap.String("topic", err.Topic),
zap.Int32("partition", err.Partition),
zap.String("error", err.Err.Error()),
)
case <-module.quitChannel:
return
}
}
}
func (module *KafkaClient) startKafkaConsumer(client helpers.SaramaClient) error {
// Create the consumer from the client
consumer, err := client.NewConsumerFromClient()
if err != nil {
module.Log.Error("failed to get new consumer", zap.Error(err))
client.Close()
return err
}
// Get a partition count for the consumption topic
partitions, err := client.Partitions(module.offsetsTopic)
if err != nil {
module.Log.Error("failed to get partition count",
zap.String("topic", module.offsetsTopic),
zap.String("error", err.Error()),
)
client.Close()
return err
}
// Default to bootstrapping the offsets topic, unless configured otherwise
startFrom := sarama.OffsetOldest
if module.startLatest {
startFrom = sarama.OffsetNewest
}
// Start consumers for each partition with fan in
module.Log.Info("starting consumers",
zap.String("topic", module.offsetsTopic),
zap.Int("count", len(partitions)),
)
for i, partition := range partitions {
pconsumer, err := consumer.ConsumePartition(module.offsetsTopic, partition, startFrom)
if err != nil {
module.Log.Error("failed to consume partition",
zap.String("topic", module.offsetsTopic),
zap.Int("partition", i),
zap.String("error", err.Error()),
)
return err
}
module.running.Add(1)
go module.partitionConsumer(pconsumer)
}
return nil
}
func (module *KafkaClient) processConsumerOffsetsMessage(msg *sarama.ConsumerMessage) {
logger := module.Log.With(
zap.String("offset_topic", msg.Topic),
zap.Int32("offset_partition", msg.Partition),
zap.Int64("offset_offset", msg.Offset),
)
if len(msg.Value) == 0 {
// Tombstone message - we don't handle them for now
logger.Debug("dropped tombstone")
return
}
var keyver int16
keyBuffer := bytes.NewBuffer(msg.Key)
err := binary.Read(keyBuffer, binary.BigEndian, &keyver)
if err != nil {
logger.Warn("failed to decode",
zap.String("reason", "no key version"),
)
return
}
switch keyver {
case 0, 1:
module.decodeKeyAndOffset(keyBuffer, msg.Value, logger)
case 2:
module.decodeGroupMetadata(keyBuffer, msg.Value, logger)
default:
logger.Warn("failed to decode",
zap.String("reason", "key version"),
zap.Int16("version", keyver),
)
}
}
func readString(buf *bytes.Buffer) (string, error) {
var strlen int16
err := binary.Read(buf, binary.BigEndian, &strlen)
if err != nil {
return "", err
}
if strlen == -1 {
return "", nil
}
strbytes := make([]byte, strlen)
n, err := buf.Read(strbytes)
if (err != nil) || (n != int(strlen)) {
return "", errors.New("string underflow")
}
return string(strbytes), nil
}
func (module *KafkaClient) acceptConsumerGroup(group string) bool {
// No whitelist means everything passes
if (module.groupWhitelist != nil) && (!module.groupWhitelist.MatchString(group)) {
return false
}
if (module.groupBlacklist != nil) && module.groupBlacklist.MatchString(group) {
return false
}
return true
}
func (module *KafkaClient) decodeKeyAndOffset(keyBuffer *bytes.Buffer, value []byte, logger *zap.Logger) {
// Version 0 and 1 keys are decoded the same way
offsetKey, errorAt := decodeOffsetKeyV0(keyBuffer)
if errorAt != "" {
logger.Warn("failed to decode",
zap.String("message_type", "offset"),
zap.String("group", offsetKey.Group),
zap.String("topic", offsetKey.Topic),
zap.Int32("partition", offsetKey.Partition),
zap.String("reason", errorAt),
)
return
}
offsetLogger := logger.With(
zap.String("message_type", "offset"),
zap.String("group", offsetKey.Group),
zap.String("topic", offsetKey.Topic),
zap.Int32("partition", offsetKey.Partition),
)
if !module.acceptConsumerGroup(offsetKey.Group) {
offsetLogger.Debug("dropped", zap.String("reason", "whitelist"))
return
}
var valueVersion int16
valueBuffer := bytes.NewBuffer(value)
err := binary.Read(valueBuffer, binary.BigEndian, &valueVersion)
if err != nil {
offsetLogger.Warn("failed to decode",
zap.String("reason", "no value version"),
)
return
}
switch valueVersion {
case 0, 1:
module.decodeAndSendOffset(offsetKey, valueBuffer, offsetLogger)
default:
offsetLogger.Warn("failed to decode",
zap.String("reason", "value version"),
zap.Int16("version", valueVersion),
)
}
}
func (module *KafkaClient) decodeAndSendOffset(offsetKey offsetKey, valueBuffer *bytes.Buffer, logger *zap.Logger) {
offsetValue, errorAt := decodeOffsetValueV0(valueBuffer)
if errorAt != "" {
logger.Warn("failed to decode",
zap.Int64("offset", offsetValue.Offset),
zap.Int64("timestamp", offsetValue.Timestamp),
zap.String("reason", errorAt),
)
return
}
partitionOffset := &protocol.StorageRequest{
RequestType: protocol.StorageSetConsumerOffset,
Cluster: module.cluster,
Topic: offsetKey.Topic,
Partition: int32(offsetKey.Partition),
Group: offsetKey.Group,
Timestamp: int64(offsetValue.Timestamp),
Offset: int64(offsetValue.Offset),
}
logger.Debug("consumer offset",
zap.Int64("offset", offsetValue.Offset),
zap.Int64("timestamp", offsetValue.Timestamp),
)
helpers.TimeoutSendStorageRequest(module.App.StorageChannel, partitionOffset, 1)
}
func (module *KafkaClient) decodeGroupMetadata(keyBuffer *bytes.Buffer, value []byte, logger *zap.Logger) {
group, err := readString(keyBuffer)
if err != nil {
logger.Warn("failed to decode",
zap.String("message_type", "metadata"),
zap.String("reason", "group"),
)
return
}
var valueVersion int16
valueBuffer := bytes.NewBuffer(value)
err = binary.Read(valueBuffer, binary.BigEndian, &valueVersion)
if err != nil {
logger.Warn("failed to decode",
zap.String("message_type", "metadata"),
zap.String("group", group),
zap.String("reason", "no value version"),
)
return
}
switch valueVersion {
case 0, 1:
module.decodeAndSendGroupMetadata(valueVersion, group, valueBuffer, logger.With(
zap.String("message_type", "metadata"),
zap.String("group", group),
))
default:
logger.Warn("failed to decode",
zap.String("message_type", "metadata"),
zap.String("group", group),
zap.String("reason", "value version"),
zap.Int16("version", valueVersion),
)
}
}
func (module *KafkaClient) decodeAndSendGroupMetadata(valueVersion int16, group string, valueBuffer *bytes.Buffer, logger *zap.Logger) {
metadataHeader, errorAt := decodeMetadataValueHeader(valueBuffer)
metadataLogger := logger.With(
zap.String("protocol_type", metadataHeader.ProtocolType),
zap.Int32("generation", metadataHeader.Generation),
zap.String("protocol", metadataHeader.Protocol),
zap.String("leader", metadataHeader.Leader),
)
if errorAt != "" {
metadataLogger.Warn("failed to decode",
zap.String("reason", errorAt),
)
return
}
var memberCount int32
err := binary.Read(valueBuffer, binary.BigEndian, &memberCount)
if err != nil {
metadataLogger.Warn("failed to decode",
zap.String("reason", "no member size"),
)
return
}
// If memberCount is zero, clear all ownership
if memberCount == 0 {
metadataLogger.Debug("clear owners")
helpers.TimeoutSendStorageRequest(module.App.StorageChannel, &protocol.StorageRequest{
RequestType: protocol.StorageClearConsumerOwners,
Cluster: module.cluster,
Group: group,
}, 1)
return
}
count := int(memberCount)
for i := 0; i < count; i++ {
member, errorAt := decodeMetadataMember(valueBuffer, valueVersion)
if errorAt != "" {
metadataLogger.Warn("failed to decode",
zap.String("reason", errorAt),
)
return
}
metadataLogger.Debug("group metadata")
for topic, partitions := range member.Assignment {
for _, partition := range partitions {
helpers.TimeoutSendStorageRequest(module.App.StorageChannel, &protocol.StorageRequest{
RequestType: protocol.StorageSetConsumerOwner,
Cluster: module.cluster,
Topic: topic,
Partition: partition,
Group: group,
Owner: member.ClientHost,
}, 1)
}
}
}
}
func decodeMetadataValueHeader(buf *bytes.Buffer) (metadataHeader, string) {
var err error
metadataHeader := metadataHeader{}
metadataHeader.ProtocolType, err = readString(buf)
if err != nil {
return metadataHeader, "protocol_type"
}
err = binary.Read(buf, binary.BigEndian, &metadataHeader.Generation)
if err != nil {
return metadataHeader, "generation"
}
metadataHeader.Protocol, err = readString(buf)
if err != nil {
return metadataHeader, "protocol"
}
metadataHeader.Leader, err = readString(buf)
if err != nil {
return metadataHeader, "leader"
}
return metadataHeader, ""
}
func decodeMetadataMember(buf *bytes.Buffer, memberVersion int16) (metadataMember, string) {
var err error
memberMetadata := metadataMember{}
memberMetadata.MemberID, err = readString(buf)
if err != nil {
return memberMetadata, "member_id"
}
memberMetadata.ClientID, err = readString(buf)
if err != nil {
return memberMetadata, "client_id"
}
memberMetadata.ClientHost, err = readString(buf)
if err != nil {
return memberMetadata, "client_host"
}
if memberVersion == 1 {
err = binary.Read(buf, binary.BigEndian, &memberMetadata.RebalanceTimeout)
if err != nil {
return memberMetadata, "rebalance_timeout"
}
}
err = binary.Read(buf, binary.BigEndian, &memberMetadata.SessionTimeout)
if err != nil {
return memberMetadata, "session_timeout"
}
var subscriptionBytes int32
err = binary.Read(buf, binary.BigEndian, &subscriptionBytes)
if err != nil {
return memberMetadata, "subscription_bytes"
}
if subscriptionBytes > 0 {
buf.Next(int(subscriptionBytes))
}
var assignmentBytes int32
err = binary.Read(buf, binary.BigEndian, &assignmentBytes)
if err != nil {
return memberMetadata, "assignment_bytes"
}
if assignmentBytes > 0 {
assignmentData := buf.Next(int(assignmentBytes))
assignmentBuf := bytes.NewBuffer(assignmentData)
var consumerProtocolVersion int16
err = binary.Read(assignmentBuf, binary.BigEndian, &consumerProtocolVersion)
if err != nil {
return memberMetadata, "consumer_protocol_version"
}
if consumerProtocolVersion < 0 {
return memberMetadata, "consumer_protocol_version"
}
assignment, errorAt := decodeMemberAssignmentV0(assignmentBuf)
if errorAt != "" {
return memberMetadata, "assignment"
}
memberMetadata.Assignment = assignment
}
return memberMetadata, ""
}
func decodeMemberAssignmentV0(buf *bytes.Buffer) (map[string][]int32, string) {
var err error
var topics map[string][]int32
var numTopics, numPartitions, partitionID, userDataLen int32
err = binary.Read(buf, binary.BigEndian, &numTopics)
if err != nil {
return topics, "assignment_topic_count"
}
topicCount := int(numTopics)
topics = make(map[string][]int32, numTopics)
for i := 0; i < topicCount; i++ {
topicName, err := readString(buf)
if err != nil {
return topics, "topic_name"
}
err = binary.Read(buf, binary.BigEndian, &numPartitions)
if err != nil {
return topics, "assignment_partition_count"
}
partitionCount := int(numPartitions)
topics[topicName] = make([]int32, numPartitions)
for j := 0; j < partitionCount; j++ {
err = binary.Read(buf, binary.BigEndian, &partitionID)
if err != nil {
return topics, "assignment_partition_id"
}
topics[topicName][j] = int32(partitionID)
}
}
err = binary.Read(buf, binary.BigEndian, &userDataLen)
if err != nil {
return topics, "user_bytes"
}
if userDataLen > 0 {
buf.Next(int(userDataLen))
}
return topics, ""
}
func decodeOffsetKeyV0(buf *bytes.Buffer) (offsetKey, string) {
var err error
offsetKey := offsetKey{}
offsetKey.Group, err = readString(buf)
if err != nil {
return offsetKey, "group"
}
offsetKey.Topic, err = readString(buf)
if err != nil {
return offsetKey, "topic"
}
err = binary.Read(buf, binary.BigEndian, &offsetKey.Partition)
if err != nil {
return offsetKey, "partition"
}
return offsetKey, ""
}
func decodeOffsetValueV0(valueBuffer *bytes.Buffer) (offsetValue, string) {
var err error
offsetValue := offsetValue{}
err = binary.Read(valueBuffer, binary.BigEndian, &offsetValue.Offset)
if err != nil {
return offsetValue, "offset"
}
_, err = readString(valueBuffer)
if err != nil {
return offsetValue, "metadata"
}
err = binary.Read(valueBuffer, binary.BigEndian, &offsetValue.Timestamp)
if err != nil {
return offsetValue, "timestamp"
}
return offsetValue, ""
}