-
Notifications
You must be signed in to change notification settings - Fork 15
/
partition.go
315 lines (294 loc) · 7.72 KB
/
partition.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
package consumergroup
import (
"errors"
"fmt"
"sync"
"time"
"github.com/Shopify/sarama"
"github.com/sirupsen/logrus"
)
type partitionConsumer struct {
owner *topicConsumer
group string
topic string
partition int32
offset int64
prevOffset int64
consumer sarama.PartitionConsumer
}
func newPartitionConsumer(owner *topicConsumer, partition int32) *partitionConsumer {
return &partitionConsumer{
owner: owner,
topic: owner.name,
group: owner.owner.name,
partition: partition,
offset: 0,
prevOffset: 0,
}
}
func (pc *partitionConsumer) start() {
var wg sync.WaitGroup
cg := pc.owner.owner
err := pc.claim()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"err": err,
}).Error("Failed to claim the partition and gave up")
goto ERROR
}
defer func() {
err = pc.release()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"err": err,
}).Error("Failed to release the partition")
} else {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
}).Info("Success to release the partition")
}
}()
err = pc.loadOffsetFromZk()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"err": err,
}).Error("Failed to fetch the partition's offset")
goto ERROR
}
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
}).Info("Fetched the partition's offset from zk")
pc.consumer, err = cg.getPartitionConsumer(pc.topic, pc.partition, pc.offset)
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
"err": err,
}).Error("Failed to create the partition's consumer")
goto ERROR
}
defer func() {
pc.consumer.Close()
}()
if cg.config.OffsetAutoCommitEnable { // start auto commit-offset thread when enable
wg.Add(1)
go func() {
defer cg.callRecover()
defer wg.Done()
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
}).Info("Start the partition's offset auto-commit thread")
pc.autoCommitOffset()
}()
}
pc.fetch()
if cg.config.OffsetAutoCommitEnable {
err = pc.commitOffset()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
"err": err,
}).Error("Failed to commit the partition's offset")
}
wg.Wait() // Wait for auto-commit-offset thread
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
}).Info("The partition's offset auto-commit thread was stopped")
}
return
ERROR:
cg.stop()
}
func (pc *partitionConsumer) loadOffsetFromZk() error {
cg := pc.owner.owner
offset, err := cg.storage.getOffset(pc.group, pc.topic, pc.partition)
if err != nil {
return err
}
if offset == -1 {
offset = cg.config.OffsetAutoReset
}
pc.offset = offset
pc.prevOffset = offset
return nil
}
func (pc *partitionConsumer) claim() error {
cg := pc.owner.owner
timer := time.NewTimer(cg.config.ClaimPartitionRetryInterval)
defer timer.Stop()
retry := cg.config.ClaimPartitionRetryTimes
// Claim partition would retry until success
for i := 0; i < retry+1 || retry <= 0; i++ {
err := cg.storage.claimPartition(pc.group, pc.topic, pc.partition, cg.id)
if err == nil {
return nil
}
if i != 0 && (i%3 == 0 || retry > 0) {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"retries": i,
"err": err,
}).Warn("Failed to claim the partition with retries")
}
select {
case <-timer.C:
timer.Reset(cg.config.ClaimPartitionRetryInterval)
case <-cg.stopCh:
return errors.New("stop signal was received when claim partition")
}
}
return fmt.Errorf("failed to claim partition after %d retries", retry)
}
func (pc *partitionConsumer) release() error {
cg := pc.owner.owner
owner, err := cg.storage.getPartitionOwner(pc.group, pc.topic, pc.partition)
if err != nil {
return err
}
if cg.id == owner {
return cg.storage.releasePartition(pc.group, pc.topic, pc.partition)
}
return fmt.Errorf("the owner of topic[%s] partition[%d] expected %s, but got %s",
pc.topic, pc.partition, owner, cg.id)
}
func (pc *partitionConsumer) fetch() {
cg := pc.owner.owner
messageChan := pc.owner.messages
errorChan := pc.owner.errors
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
}).Info("Start to fetch the partition's messages")
PARTITION_CONSUMER_LOOP:
for {
select {
case <-cg.stopCh:
break PARTITION_CONSUMER_LOOP
case err := <-pc.consumer.Errors():
if err.Err == sarama.ErrOffsetOutOfRange {
pc.restart()
break
}
errorChan <- err
case message, ok := <-pc.consumer.Messages():
//check if the channel is closed. message channel close while the offset out of range
if !ok {
pc.restart()
break
}
if message == nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
}).Error("Sarama partition consumer encounter error, the consumer would be exited")
cg.stop()
break PARTITION_CONSUMER_LOOP
}
select {
case messageChan <- message:
pc.offset = message.Offset + 1
case <-cg.stopCh:
break PARTITION_CONSUMER_LOOP
}
}
}
}
func (pc *partitionConsumer) autoCommitOffset() {
cg := pc.owner.owner
defer cg.callRecover()
timer := time.NewTimer(cg.config.OffsetAutoCommitInterval)
for {
select {
case <-cg.stopCh:
return
case <-timer.C:
err := pc.commitOffset()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"topic": pc.topic,
"partition": pc.partition,
"offset": pc.offset,
"err": err,
}).Error("Failed to auto-commit the partition's offset")
}
timer.Reset(cg.config.OffsetAutoCommitInterval)
}
}
}
func (pc *partitionConsumer) commitOffset() error {
cg := pc.owner.owner
offset := pc.offset
if pc.prevOffset == offset {
return nil
}
err := cg.storage.commitOffset(pc.group, pc.topic, pc.partition, offset)
if err != nil {
return err
}
pc.prevOffset = offset
return nil
}
func (pc *partitionConsumer) restart() {
cg := pc.owner.owner
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
}).Infof("Restart partition consumer while the offset out of range")
err := pc.consumer.Close()
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
}).Error("Stop consumer group because the old partition consumer cannot be closed")
cg.stop()
return
}
pc.consumer, err = cg.getPartitionConsumer(pc.topic, pc.partition, sarama.OffsetOldest)
if err != nil {
cg.logger.WithFields(logrus.Fields{
"group": pc.group,
"topic": pc.topic,
"partition": pc.partition,
"err": err,
}).Error("Stop consumer group because the new partition consumer cannot be start")
cg.stop()
}
}
func (pc *partitionConsumer) getOffset() map[string]interface{} {
offset := make(map[string]interface{})
offset["offset"] = pc.offset
offset["prev_offset"] = pc.prevOffset
return offset
}