-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathconsumer.go
155 lines (146 loc) · 4.58 KB
/
consumer.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
// Copyright 2024 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"strings"
"time"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func getPartitionNum(o *option) (int32, error) {
configMap := &kafka.ConfigMap{
"bootstrap.servers": strings.Join(o.address, ","),
}
if len(o.ca) != 0 {
_ = configMap.SetKey("security.protocol", "SSL")
_ = configMap.SetKey("ssl.ca.location", o.ca)
_ = configMap.SetKey("ssl.key.location", o.key)
_ = configMap.SetKey("ssl.certificate.location", o.cert)
}
admin, err := kafka.NewAdminClient(configMap)
if err != nil {
return 0, errors.Trace(err)
}
defer admin.Close()
timeout := 3000
for i := 0; i <= o.retryTime; i++ {
resp, err := admin.GetMetadata(&o.topic, false, timeout)
if err != nil {
if err.(kafka.Error).Code() == kafka.ErrTransport {
log.Info("retry get partition number", zap.Int("retryTime", i), zap.Int("timeout", timeout))
timeout += 100
continue
}
return 0, errors.Trace(err)
}
if topicDetail, ok := resp.Topics[o.topic]; ok {
numPartitions := int32(len(topicDetail.Partitions))
log.Info("get partition number of topic",
zap.String("topic", o.topic),
zap.Int32("partitionNum", numPartitions))
return numPartitions, nil
}
log.Info("retry get partition number", zap.String("topic", o.topic))
time.Sleep(1 * time.Second)
}
return 0, errors.Errorf("get partition number(%s) timeout", o.topic)
}
type consumer struct {
client *kafka.Consumer
writer *writer
}
// newConsumer will create a consumer client.
func newConsumer(ctx context.Context, o *option) *consumer {
partitionNum, err := getPartitionNum(o)
if err != nil {
log.Panic("cannot get the partition number", zap.String("topic", o.topic), zap.Error(err))
}
if o.partitionNum == 0 {
o.partitionNum = partitionNum
}
topics := strings.Split(o.topic, ",")
if len(topics) == 0 {
log.Panic("no topic provided for the consumer")
}
configMap := &kafka.ConfigMap{
"bootstrap.servers": strings.Join(o.address, ","),
"group.id": o.groupID,
// Start reading from the first message of each assigned
// partition if there are no previously committed offsets
// for this group.
"auto.offset.reset": "earliest",
// Whether we store offsets automatically.
"enable.auto.offset.store": false,
"enable.auto.commit": false,
}
if len(o.ca) != 0 {
_ = configMap.SetKey("security.protocol", "SSL")
_ = configMap.SetKey("ssl.ca.location", o.ca)
_ = configMap.SetKey("ssl.key.location", o.key)
_ = configMap.SetKey("ssl.certificate.location", o.cert)
}
if level, err := zapcore.ParseLevel(o.logLevel); err == nil && level.String() == "debug" {
configMap.SetKey("debug", "all")
}
client, err := kafka.NewConsumer(configMap)
if err != nil {
log.Panic("create kafka consumer failed", zap.Error(err))
}
err = client.SubscribeTopics(topics, nil)
if err != nil {
log.Panic("subscribe topics failed", zap.Error(err))
}
return &consumer{
writer: newWriter(ctx, o),
client: client,
}
}
// Consume will read message from Kafka.
func (c *consumer) Consume(ctx context.Context) {
defer func() {
if err := c.client.Close(); err != nil {
log.Panic("close kafka consumer failed", zap.Error(err))
}
}()
for {
select {
case <-ctx.Done():
log.Info("consumer exist: context cancelled")
return
default:
}
msg, err := c.client.ReadMessage(-1)
if err != nil {
log.Error("read message failed, just continue to retry", zap.Error(err))
continue
}
needCommit := c.writer.WriteMessage(ctx, msg)
if !needCommit {
continue
}
topicPartition, err := c.client.CommitMessage(msg)
if err != nil {
log.Error("commit message failed, just continue",
zap.String("topic", *msg.TopicPartition.Topic), zap.Int32("partition", msg.TopicPartition.Partition),
zap.Any("offset", msg.TopicPartition.Offset), zap.Error(err))
continue
}
log.Debug("commit message success",
zap.String("topic", topicPartition[0].String()), zap.Int32("partition", topicPartition[0].Partition),
zap.Any("offset", topicPartition[0].Offset))
}
}