forked from childe/healer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
158 lines (132 loc) · 3.38 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
package healer
import (
"sync"
"time"
"github.com/golang/glog"
)
// 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
}
func NewConsumer(config *ConsumerConfig, topics ...string) (*Consumer, error) {
brokerConfig := getBrokerConfigFromConsumerConfig(config)
brokers, err := NewBrokersWithConfig(config.BootstrapServers, brokerConfig)
if err != nil {
return nil, err
}
assign := make(map[string][]int)
for _, topic := range topics {
assign[topic] = nil
}
c := &Consumer{
config: config,
assign: assign,
brokers: brokers,
}
return c, nil
}
func (c *Consumer) Subscribe(topics ...string) {
c.assign = make(map[string][]int)
for _, topic := range topics {
c.assign[topic] = nil
}
}
func (c *Consumer) Assign(topicPartitons map[string][]int) {
c.assign = topicPartitons
}
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 = nil
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 {
glog.Errorf("could not get metadata of topics %v: %s", topics, err)
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, _ := c.assign[topicName]; pids == nil { // 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 {
glog.Errorf("get coordinator error: %s", err)
time.Sleep(time.Millisecond * time.Duration(c.config.RetryBackOffMS))
continue
}
break
}
c.simpleConsumers = append(c.simpleConsumers, simpleConsumer)
}
}
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:
glog.Info("all simple consumers stopped. return")
return
case <-time.After(timeout):
glog.Info("consumer await timeout. return")
return
}
}()
consumer.stop()
go func() {
consumer.wg.Wait()
consumer.brokers.Close()
c <- true
}()
}