-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsumer.go
372 lines (347 loc) · 10 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
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
package kafclient
import (
"context"
"encoding/json"
"errors"
"log"
"strings"
"sync"
"time"
"github.com/IBM/sarama"
)
func newConsumerGroup(consumerGroup string, reconnect chan bool, brokerURLs ...string) (sarama.ConsumerGroup, error) {
config := sarama.NewConfig()
// config.ClientID = clientid
config.Consumer.Return.Errors = true
config.Consumer.Group.Session.Timeout = 20 * time.Second
config.Consumer.Group.Heartbeat.Interval = 6 * time.Second
config.Consumer.MaxProcessingTime = 500 * time.Millisecond
// config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
// config.Consumer.Group.Rebalance.GroupStrategies = []sarama.BalanceStrategy{sarama.BalanceStrategyRoundRobin}
version, err := sarama.ParseKafkaVersion(kafkaVersion)
if err != nil {
log.Printf("Error parsing Kafka version: %v", err)
return nil, err
}
config.Version = version
// config.Producer.Partitioner =
config.Consumer.Offsets.Initial = sarama.OffsetOldest
// Start with a client
client, err := sarama.NewClient(brokerURLs, config)
if err != nil {
return nil, err
}
// Start a new consumer group
group, err := sarama.NewConsumerGroupFromClient(consumerGroup, client)
if err != nil {
client.Close()
return nil, err
}
go func() {
for err := range group.Errors() {
log.Println("ERROR:", err)
sarama.NewConsumerGroupFromClient(consumerGroup, client)
}
}()
return group, nil
}
func newConsumer(brokerURLs ...string) (sarama.Consumer, error) {
config := sarama.NewConfig()
version, err := sarama.ParseKafkaVersion(kafkaVersion)
if err != nil {
log.Printf("Error parsing Kafka version: %v", err)
return nil, err
}
config.Version = version
// config.Producer.Partitioner =
// config.Consumer.Offsets.Initial = sarama.OffsetOldest
config.Consumer.Return.Errors = true
config.Consumer.Offsets.AutoCommit.Enable = true
config.Consumer.Offsets.AutoCommit.Interval = 500 * time.Millisecond
consumer, err := sarama.NewConsumer(brokerURLs, config)
if err != nil {
return nil, err
}
return consumer, nil
}
func (ps *Client) createTopic(topic string) error {
config := sarama.NewConfig()
config.Version = ps.kafkaVersion
admin, err := sarama.NewClusterAdmin(ps.brokerURLs, config)
if err != nil {
log.Println("[warning]: ", err, ps.brokerURLs)
return err
}
detail := &sarama.TopicDetail{
NumPartitions: int32(NUM_PARTITION),
ReplicationFactor: int16(REPLICATION_FACTOR),
}
err = admin.CreateTopic(topic, detail, false)
if err != nil {
log.Println("[psub]:", err)
}
log.Print(detail)
return err
}
func (ps *Client) InitConsumerGroup(consumerGroup string, brokerURLs ...string) error {
client, err := newConsumerGroup(consumerGroup, ps.reconnect, brokerURLs...)
if err != nil {
return err
}
ps.group = client
ps.brokerURLs = brokerURLs
ps.consumerGroup = consumerGroup
// ps.reconnect = make(chan bool)
return nil
}
func (ps *Client) InitConsumer(brokerURLs ...string) error {
client, err := newConsumer(brokerURLs...)
if err != nil {
return err
}
ps.consumer = client
ps.brokerURLs = brokerURLs
return nil
}
func (ps *Client) OnScanMessages(topics []string, bufMessage chan Message) error {
done := make(chan bool)
for _, topic := range topics {
if strings.Contains(topic, "__consumer_offsets") {
continue
}
partitions, _ := ps.consumer.Partitions(topic)
log.Printf("number partitions: %v", len(partitions))
// this only consumes partition no 1, you would probably want to consume all partitions
for _, partition := range partitions {
partitionConsumer, err := ps.consumer.ConsumePartition(topic, partition, 0)
if nil != err {
log.Printf("Topic %v Partitions: %v", topic, partition)
continue
}
defer func() {
if err := partitionConsumer.Close(); err != nil {
log.Print(err)
}
}()
go func(topic string, partitionConsumer sarama.PartitionConsumer) {
for {
select {
case consumerError := <-partitionConsumer.Errors():
log.Print(consumerError.Err)
done <- true
case msg := <-partitionConsumer.Messages():
messageHandler(msg, bufMessage)
partitionConsumer.HighWaterMarkOffset()
}
}
}(topic, partitionConsumer)
}
}
<-done
return nil
}
func BodyParse(bin []byte, p interface{}) error {
return json.Unmarshal(bin, p)
}
func (ps *Client) ListTopics(brokers ...string) ([]string, error) {
config := sarama.NewConfig()
config.Consumer.Return.Errors = true
cluster, err := sarama.NewConsumer(brokers, config)
if err != nil {
return nil, err
}
defer func() {
cluster.Close()
config = nil
}()
return cluster.Topics()
}
func (ps *Client) OnAsyncSubscribe(topics []*Topic, numberPuller int, buf chan Message) error {
// ps.onAsyncSubscribe(topics, numberPuller, buf)
var err error
for {
err = ps.onAsyncSubscribe(topics, numberPuller, buf)
if err != nil {
log.Print(err)
}
time.Sleep(5 * time.Second)
log.Print("try reconnecting ....")
ps.InitConsumerGroup(ps.consumerGroup, ps.brokerURLs...)
}
// return err
}
// onAsyncSubscribe listener
func (ps *Client) onAsyncSubscribe(topics []*Topic, numberPuller int, buf chan Message) error {
if len(topics) == 0 {
log.Print("not found topics")
return nil
}
txtTopics := []string{}
autoCommit := map[string]bool{}
allTopics, err := ps.ListTopics(ps.brokerURLs...)
if err != nil {
log.Print("can't not list topics existed")
return err
}
mTopic := make(map[string]bool)
for _, topic := range allTopics {
mTopic[topic] = true
}
for _, topic := range topics {
if strings.Contains(topic.Name, "__consumer_offsets") {
continue
}
if topic.IsNeedManualCreateTopic {
if _, has := mTopic[topic.Name]; has {
ps.createTopic(topic.Name)
}
}
txtTopics = append(txtTopics, topic.Name)
autoCommit[topic.Name] = topic.AutoCommit
if topic.AutoCommit {
log.Print("don't forget commit topic: ", topic.Name)
}
}
consumer := &ConsumerGroupHandle{
wg: &sync.WaitGroup{},
bufMessage: buf,
lock: make(chan bool),
autoCommit: autoCommit,
}
ctx, cancel := context.WithCancel(context.Background())
consumer.wg.Add(numberPuller)
for i := 0; i < numberPuller; i++ {
go func() {
defer consumer.wg.Done()
for {
// `Consume` should be called inside an infinite loop, when a
// server-side rebalance happens, the consumer session will need to be
// recreated to get the new claims
err := ps.group.Consume(ctx, txtTopics, consumer)
if err != nil {
log.Printf("[psub]: %v", err)
consumer.lock <- true
log.Print("con gi nua dau 4445555")
break
}
// // check if context was cancelled, signaling that the consumer should stop
// if ctx.Err() != nil {
// log.Print("con gi nua dau 4444 ", ctx.Err())
// return
// }
// consumer.lock = make(chan bool)
// log.Print("con gi nua dau 222")
}
}()
}
log.Print("[kafka] start all worker")
<-consumer.lock
cancel()
consumer.wg.Wait()
if err := ps.group.Close(); err != nil {
log.Printf("Error closing client: %v", err)
return err
}
log.Print("con gi nua dau")
return nil
}
func messageHandler(m *sarama.ConsumerMessage, bufMessage chan Message) error {
if len(m.Value) == 0 {
// Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
return errors.New("message error")
}
msg := Message{
Topic: m.Topic,
Body: m.Value,
Offset: m.Offset,
Partition: int(m.Partition),
Timestamp: m.Timestamp.Unix(),
}
if len(m.Headers) != 0 {
headers := map[string]string{}
for _, header := range m.Headers {
headers[string(header.Key)] = string(header.Value)
}
msg.Headers = headers
}
bufMessage <- msg
return nil
}
// Setup is run at the beginning of a new session, before ConsumeClaim
func (consumer *ConsumerGroupHandle) Setup(ss sarama.ConsumerGroupSession) error {
// Mark the consumer as ready
log.Print("done: ", ss.MemberID())
// close(consumer.lock)
return nil
}
// Cleanup is run at the end of a session, once all ConsumeClaim goroutines have exited
func (consumer *ConsumerGroupHandle) Cleanup(ss sarama.ConsumerGroupSession) error {
log.Print("sarama clearuppp: ", ss.MemberID())
return nil
}
// ConsumeClaim must start a consumer loop of ConsumerGroupClaim's Messages().
func (consumer *ConsumerGroupHandle) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
// NOTE:
// Do not move the code below to a goroutine.
// The `ConsumeClaim` itself is called within a goroutine, see:
// https://github.com/IBM/sarama/blob/master/consumer_group.go#L27-L29
for {
select {
case m := <-claim.Messages():
if len(m.Value) == 0 {
// Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
return errors.New("message error")
}
msg := Message{
Topic: m.Topic,
Body: m.Value,
Offset: m.Offset,
Partition: int(m.Partition),
Timestamp: m.Timestamp.Unix(),
}
if len(m.Headers) != 0 {
headers := map[string]string{}
for _, header := range m.Headers {
headers[string(header.Key)] = string(header.Value)
}
msg.Headers = headers
}
if consumer.autoCommit[m.Topic] {
session.MarkOffset(m.Topic, m.Partition, m.Offset, "")
session.MarkMessage(m, "")
consumer.bufMessage <- msg
msg.Commit = func() {}
continue
}
msg.Commit = func() {
session.MarkOffset(m.Topic, m.Partition, m.Offset, "")
session.MarkMessage(m, "")
}
consumer.bufMessage <- msg
// Should return when `session.Context()` is done.
// If not, will raise `ErrRebalanceInProgress` or `read tcp <ip>:<port>: i/o timeout` when kafka rebalance. see:
// https://github.com/IBM/sarama/issues/1192
case <-session.Context().Done():
return nil
}
}
}
func (ps *Client) Close() error {
if ps.consumer != nil {
if err := ps.consumer.Close(); err != nil {
return err
}
}
// var err error
// ps.mProducer.Range(func(k interface{}, sp interface{}) bool {
// if sp == nil {
// return true
// }
// err = sp.(sarama.SyncProducer).Close()
// if err != nil {
// log.Print("close error: ", err.Error())
// }
// return true
// })
return nil
}