-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconsumer.go
169 lines (143 loc) · 4.02 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package healer
import (
"sync"
"time"
)
// Consumer instance is built to consume messages from kafka broker
type Consumer struct {
assign map[string][]int
config ConsumerConfig
brokers *Brokers
closed bool
simpleConsumers []*SimpleConsumer
wg sync.WaitGroup // wg is used to tell if all consumer has already stopped
}
// NewConsumer creates a new consumer instance
func NewConsumer(config interface{}, topics ...string) (*Consumer, error) {
cfg, err := createConsumerConfig(config)
logger.Info("create consumer", "origin_config", config, "final_config", cfg)
if err != nil {
return nil, err
}
brokerConfig := getBrokerConfigFromConsumerConfig(cfg)
brokers, err := NewBrokersWithConfig(cfg.BootstrapServers, brokerConfig)
if err != nil {
return nil, err
}
assign := make(map[string][]int)
for _, topic := range topics {
assign[topic] = nil
}
c := &Consumer{
config: cfg,
assign: assign,
brokers: brokers,
}
return c, nil
}
// Subscribe subscribes to the given list of topics, consume all the partitions of the topics.
// Do not call this after calling Consume
func (c *Consumer) Subscribe(topics ...string) {
c.assign = make(map[string][]int)
for _, topic := range topics {
c.assign[topic] = nil
}
}
// Assign assigns the given partitions to the consumer, the consumer will only consume the given partitions
// Do not call this after calling Consume
func (c *Consumer) Assign(topicPartitons map[string][]int) {
c.assign = topicPartitons
}
// Consume consumes messages from kafka broker, returns a channel of messages
func (c *Consumer) Consume(messageChan chan *FullMessage) (<-chan *FullMessage, error) {
var messages chan *FullMessage
if messageChan == nil {
messages = make(chan *FullMessage, 100)
} else {
messages = messageChan
}
var (
metadataResponse MetadataResponse
err error
topics []string = make([]string, 0)
)
for topicName := range c.assign {
topics = append(topics, topicName)
}
for !c.closed {
if metadataResponse, err = c.brokers.RequestMetaData(c.config.ClientID, topics); err != nil {
logger.Error(err, "get metadata failed", "topics", topics)
time.Sleep(time.Millisecond * 1000)
} else {
break
}
}
if c.closed {
return nil, nil
}
c.simpleConsumers = make([]*SimpleConsumer, 0)
for _, topicMetadatas := range metadataResponse.TopicMetadatas {
topicName := topicMetadatas.TopicName
var partitions = make([]int, 0)
if pids, ok := c.assign[topicName]; !ok || len(pids) == 0 { // consume all partitions
for _, partitionMetadataInfo := range topicMetadatas.PartitionMetadatas {
partitions = append(partitions, int(partitionMetadataInfo.PartitionID))
}
} else {
partitions = pids
}
for _, p := range partitions {
simpleConsumer := NewSimpleConsumerWithBrokers(topicName, int32(p), c.config, c.brokers)
simpleConsumer.wg = &c.wg
for {
err := simpleConsumer.getCoordinator()
if err != nil {
logger.Error(err, "get coordinator failed")
time.Sleep(time.Millisecond * time.Duration(c.config.RetryBackOffMS))
continue
}
break
}
c.simpleConsumers = append(c.simpleConsumers, simpleConsumer)
}
}
logger.V(4).Info("create simple consumers", "simpleConsumerCount", len(c.simpleConsumers), "consumers", c.simpleConsumers)
var offset int64
if c.config.FromBeginning {
offset = -2
} else {
offset = -1
}
for _, simpleConsumer := range c.simpleConsumers {
c.wg.Add(1)
simpleConsumer.Consume(offset, messages)
}
return messages, nil
}
func (c *Consumer) stop() {
c.closed = true
if c.simpleConsumers != nil {
for _, simpleConsumer := range c.simpleConsumers {
simpleConsumer.Stop()
}
}
}
func (consumer *Consumer) AwaitClose(timeout time.Duration) {
c := make(chan bool)
defer func() {
select {
case <-c:
logger.Info("all simple consumers stopped. return")
return
case <-time.After(timeout):
logger.Info("consumer await timeout. return")
return
}
}()
consumer.stop()
go func() {
consumer.wg.Wait()
consumer.brokers.Close()
c <- true
}()
}