-
Notifications
You must be signed in to change notification settings - Fork 120
/
offsetmgr.go
559 lines (500 loc) · 15.4 KB
/
offsetmgr.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package offsetmgr
import (
"math"
"sync"
"time"
"github.com/Shopify/sarama"
"github.com/mailgun/kafka-pixy/actor"
"github.com/mailgun/kafka-pixy/config"
"github.com/mailgun/kafka-pixy/mapper"
"github.com/mailgun/kafka-pixy/none"
"github.com/pkg/errors"
)
const (
minOffsetCommitTimeout = 300 * time.Millisecond
)
var (
undefinedOffset = Offset{Val: math.MaxInt64}
// To be used in tests only! If true then offset manager will initialize
// their errors channel and will send internal errors.
testReportErrors bool
errRequestTimeout = errors.New("request timeout")
)
// Factory provides a method to spawn offset manager instances to commit
// offsets for a particular group-topic-partition. It makes sure that there is
// only one running manager instance for a particular group-topic-partition
// combination.
//
// One Factory instance per application is usually more then enough, but it is
// possible to create many of them.
//
// Factory spawns background goroutines so it must be explicitly stopped by the
// application. But first it should explicitly stop all spawned offset manager
// instances.
type Factory interface {
// Span creates and starts an offset manager for a group-topic-partition.
// It returns an error if given group-topic-partition has a running
// OffsetManager instance already. After an old offset manager instance is
// stopped a new one can be started.
Spawn(parentActDesc *actor.Descriptor, group, topic string, partition int32) (T, error)
// Stop waits for the spawned offset managers to stop and then terminates. Note
// that all spawned offset managers has to be explicitly stopped by calling
// their Stop method.
Stop()
}
// T provides interface to store and retrieve offsets for a particular
// group-topic-partition in Kafka.
type T interface {
// SubmitOffset triggers saving of the specified offset in Kafka. Commits are
// performed periodically in a background goroutine. The commit interval is
// configured by `Config.Consumer.Offsets.CommitInterval`. Note that not every
// submitted offset gets committed. Committed offsets are sent down to the
// `CommittedOffsets()` channel. The `CommittedOffsets()` channel has to be
// read alongside with submitting offsets, otherwise the partition offset
// manager will block.
SubmitOffset(offset Offset)
// CommittedOffsets returns a channel that offsets committed to Kafka are
// sent to. The first offset sent to this channel is the initial offset
// fetched from Kafka for the group-topic-partition. The user must read
// from this channel otherwise the `SubmitOffset` function will eventually
// block forever.
CommittedOffsets() <-chan Offset
// Stop stops the offset manager. It is required to stop all spawned offset
// managers before their parent factory can be stopped.
//
// It is guaranteed that the most recent offset is committed before `Stop`
// returns.
Stop()
}
// Offset represents an offset data as it is stored in Kafka, that is an offset
// value decorated with a metadata string.
type Offset struct {
Val int64
Meta string
}
// SpawnFactory creates a new offset manager factory from the given client.
func SpawnFactory(parentActDesc *actor.Descriptor, cfg *config.Proxy, kafkaClt sarama.Client) Factory {
f := &factory{
actDesc: parentActDesc.NewChild("offset_mgr_f"),
kafkaClt: kafkaClt,
cfg: cfg,
children: make(map[instanceID]*offsetMgr),
}
f.mapper = mapper.Spawn(f.actDesc, cfg, f)
return f
}
// implements `Factory`
// implements `mapper.Resolver`
type factory struct {
actDesc *actor.Descriptor
kafkaClt sarama.Client
cfg *config.Proxy
mapper *mapper.T
childrenMu sync.Mutex
children map[instanceID]*offsetMgr
}
type instanceID struct {
group string
topic string
partition int32
}
// implements `Factory`
func (f *factory) Spawn(namespace *actor.Descriptor, group, topic string, partition int32) (T, error) {
id := instanceID{group, topic, partition}
f.childrenMu.Lock()
defer f.childrenMu.Unlock()
if _, ok := f.children[id]; ok {
return nil, errors.Errorf("offset manager %v already exists", id)
}
actDesc := namespace.NewChild("offset_mgr")
actDesc.AddLogField("kafka.group", group)
actDesc.AddLogField("kafka.topic", topic)
actDesc.AddLogField("kafka.partition", partition)
om := &offsetMgr{
actDesc: actDesc,
f: f,
id: id,
submitRequestsCh: make(chan submitRq),
assignmentCh: make(chan mapper.Executor, 1),
committedOffsetsCh: make(chan Offset, f.cfg.Consumer.ChannelBufferSize),
}
if testReportErrors {
om.testErrorsCh = make(chan error, f.cfg.Consumer.ChannelBufferSize)
}
om.retryTimer = time.NewTimer(0)
<-om.retryTimer.C
f.children[id] = om
actor.Spawn(om.actDesc, &om.wg, om.run)
return om, nil
}
// implements `mapper.Resolver`.
func (f *factory) ResolveBroker(worker mapper.Worker) (*sarama.Broker, error) {
om := worker.(*offsetMgr)
if err := f.kafkaClt.RefreshCoordinator(om.id.group); err != nil {
return nil, err
}
brokerConn, err := f.kafkaClt.Coordinator(om.id.group)
if err != nil {
return nil, err
}
return brokerConn, nil
}
// implements `mapper.Resolver`.
func (f *factory) SpawnExecutor(brokerConn *sarama.Broker) mapper.Executor {
be := &brokerExecutor{
aggrActDesc: f.actDesc.NewChild("broker", brokerConn.ID(), "aggr"),
execActDesc: f.actDesc.NewChild("broker", brokerConn.ID(), "exec"),
cfg: f.cfg,
conn: brokerConn,
requestsCh: make(chan submitRq),
requestBatchesCh: make(chan map[string]map[instanceID]submitRq),
flushAggregatorCh: make(chan none.T, 1),
flushExecutorCh: make(chan none.T, 1),
execStopCh: make(chan none.T),
}
actor.Spawn(be.aggrActDesc, &be.wg, be.runAggregator)
actor.Spawn(be.execActDesc, &be.wg, be.runExecutor)
return be
}
// implements `Factory.Stop()`
func (f *factory) Stop() {
f.mapper.Stop()
}
func (f *factory) onOffsetMgrSpawned(om *offsetMgr) {
f.mapper.OnWorkerSpawned(om)
}
func (f *factory) onOffsetMgrStopped(om *offsetMgr) {
f.childrenMu.Lock()
delete(f.children, om.id)
f.childrenMu.Unlock()
f.mapper.OnWorkerStopped(om)
}
// implements `T`
// implements `mapper.Worker`
type offsetMgr struct {
actDesc *actor.Descriptor
f *factory
id instanceID
submitRequestsCh chan submitRq
assignmentCh chan mapper.Executor
committedOffsetsCh chan Offset
brokerRequestsCh chan<- submitRq
nilOrBrokerRequestsCh chan<- submitRq
retryTimer *time.Timer
nilOrRetryTimerCh <-chan time.Time
wg sync.WaitGroup
// To be used in tests only!
testErrorsCh chan error
}
// implements `T`.
func (om *offsetMgr) SubmitOffset(offset Offset) {
om.submitRequestsCh <- submitRq{
id: om.id,
offset: offset,
}
}
// implements `T`.
func (om *offsetMgr) CommittedOffsets() <-chan Offset {
return om.committedOffsetsCh
}
// implements `T`.
func (om *offsetMgr) Stop() {
close(om.submitRequestsCh)
om.wg.Wait()
}
// implements `mapper.Worker`.
func (om *offsetMgr) Assignment() chan<- mapper.Executor {
return om.assignmentCh
}
func (om *offsetMgr) String() string {
return om.actDesc.String()
}
func (om *offsetMgr) run() {
defer close(om.committedOffsetsCh)
if om.testErrorsCh != nil {
defer close(om.testErrorsCh)
}
defer om.retryTimer.Stop()
om.f.onOffsetMgrSpawned(om)
defer om.f.onOffsetMgrStopped(om)
var (
initialOffset = undefinedOffset
latestRq = submitRq{offset: undefinedOffset}
nilOrRequestsCh = om.submitRequestsCh
responseCh = make(chan submitRs, 1)
stopping = false
)
// Retrieve the initial offset.
for {
select {
case bw := <-om.assignmentCh:
om.actDesc.Log().Infof("Assigned executor: %s", bw)
be := bw.(*brokerExecutor)
om.brokerRequestsCh = be.requestsCh
initialOffset, err := om.fetchInitialOffset(be.conn)
if err != nil {
om.triggerReassign(err, "Failed to fetch initial offset")
continue
}
om.committedOffsetsCh <- initialOffset
goto handleRequests
case rq, ok := <-nilOrRequestsCh:
if !ok {
// It was signalled to stop, but return only if there is no
// uncommitted offset, otherwise keep running.
if latestRq.offset == initialOffset {
return
}
stopping = true
nilOrRequestsCh = nil
continue
}
latestRq = rq
latestRq.resultCh = responseCh
}
}
handleRequests:
committedOffset := initialOffset
if latestRq.offset == undefinedOffset {
latestRq.offset = committedOffset
}
if latestRq.offset != committedOffset {
om.nilOrBrokerRequestsCh = om.brokerRequestsCh
}
var submittedRq submitRq
var submittedAt time.Time
var be *brokerExecutor
offsetCommitTimeout := om.f.cfg.Consumer.OffsetsCommitInterval * 2
if offsetCommitTimeout < minOffsetCommitTimeout {
offsetCommitTimeout = minOffsetCommitTimeout
}
for {
select {
case bw := <-om.assignmentCh:
om.actDesc.Log().Infof("Assigned executor: %s", bw)
be = bw.(*brokerExecutor)
om.brokerRequestsCh = be.requestsCh
if latestRq.offset != committedOffset {
om.nilOrBrokerRequestsCh = om.brokerRequestsCh
}
case rq, ok := <-nilOrRequestsCh:
if !ok {
if latestRq.offset == committedOffset {
return
}
// Keep running until the last submitter offset is committed.
stopping = true
// Signal broker executor to flush offsets to Kafka.
if be != nil {
be.flushAggregatorCh <- none.V
}
nilOrRequestsCh = nil
continue
}
latestRq = rq
latestRq.resultCh = responseCh
om.nilOrBrokerRequestsCh = om.brokerRequestsCh
case om.nilOrBrokerRequestsCh <- latestRq:
om.nilOrBrokerRequestsCh = nil
submittedRq = latestRq
submittedAt = time.Now()
if om.nilOrRetryTimerCh == nil {
om.retryTimer.Reset(offsetCommitTimeout)
om.nilOrRetryTimerCh = om.retryTimer.C
}
case rs := <-responseCh:
if err := om.getCommitError(rs); err != nil {
om.triggerReassign(err, "Request failed")
continue
}
committedOffset = rs.offset
om.committedOffsetsCh <- committedOffset
if stopping && latestRq.offset == committedOffset {
return
}
case <-om.nilOrRetryTimerCh:
om.nilOrRetryTimerCh = nil
sinceSubmitted := time.Since(submittedAt)
if sinceSubmitted >= offsetCommitTimeout {
if submittedRq.offset == committedOffset {
continue
}
om.triggerReassign(errRequestTimeout, "Request timeout %v", sinceSubmitted)
continue
}
timeoutLeft := offsetCommitTimeout - sinceSubmitted
om.retryTimer.Reset(timeoutLeft)
om.nilOrRetryTimerCh = om.retryTimer.C
}
}
}
func (om *offsetMgr) stopRetryTimer() {
if om.nilOrRetryTimerCh == nil {
return
}
if !om.retryTimer.Stop() {
<-om.retryTimer.C
}
om.nilOrRetryTimerCh = nil
}
func (om *offsetMgr) triggerReassign(err error, format string, args ...interface{}) {
om.actDesc.Log().WithError(err).Errorf(format, args...)
om.stopRetryTimer()
if om.testErrorsCh != nil {
om.testErrorsCh <- err
}
om.brokerRequestsCh = nil
om.nilOrBrokerRequestsCh = nil
om.f.mapper.TriggerReassign(om)
}
func (om *offsetMgr) fetchInitialOffset(conn *sarama.Broker) (Offset, error) {
request := new(sarama.OffsetFetchRequest)
request.Version = 1
request.ConsumerGroup = om.id.group
request.AddPartition(om.id.topic, om.id.partition)
response, err := conn.FetchOffset(request)
if err != nil {
// In case of network error the connection has to be explicitly closed,
// otherwise it won't be re-establish and following requests to this
// broker will fail as well.
_ = conn.Close()
return Offset{}, err
}
block := response.GetBlock(om.id.topic, om.id.partition)
if block == nil {
return Offset{}, sarama.ErrIncompleteResponse
}
if block.Err != sarama.ErrNoError {
return Offset{}, block.Err
}
fetchedOffset := Offset{block.Offset, block.Metadata}
return fetchedOffset, nil
}
func (om *offsetMgr) getCommitError(rs submitRs) error {
if rs.connErr != nil {
return rs.connErr
}
if rs.kafkaRs.Errors[om.id.topic] == nil {
return sarama.ErrIncompleteResponse
}
err, ok := rs.kafkaRs.Errors[om.id.topic][om.id.partition]
if !ok {
return sarama.ErrIncompleteResponse
}
if err != sarama.ErrNoError {
return err
}
return nil
}
type submitRq struct {
id instanceID
offset Offset
resultCh chan<- submitRs
}
type submitRs struct {
offset Offset
kafkaRs *sarama.OffsetCommitResponse
connErr error
}
// brokerExecutor aggregates submitted offsets from partition offset managers
// and periodically commits them to Kafka.
//
// implements `mapper.Executor`.
type brokerExecutor struct {
aggrActDesc *actor.Descriptor
execActDesc *actor.Descriptor
cfg *config.Proxy
conn *sarama.Broker
requestsCh chan submitRq
requestBatchesCh chan map[string]map[instanceID]submitRq
flushAggregatorCh chan none.T
flushExecutorCh chan none.T
execStopCh chan none.T
wg sync.WaitGroup
}
// implements `mapper.Executor`.
func (be *brokerExecutor) BrokerConn() *sarama.Broker {
return be.conn
}
// implements `mapper.Executor`.
func (be *brokerExecutor) Stop() {
close(be.requestsCh)
be.wg.Wait()
}
func (be *brokerExecutor) runAggregator() {
defer close(be.execStopCh)
requestBatch := make(map[string]map[instanceID]submitRq)
var nilOrOffsetBatchesCh chan map[string]map[instanceID]submitRq
for {
select {
case rq, ok := <-be.requestsCh:
if !ok {
return
}
groupRequests := requestBatch[rq.id.group]
if groupRequests == nil {
groupRequests = make(map[instanceID]submitRq)
requestBatch[rq.id.group] = groupRequests
}
groupRequests[rq.id] = rq
nilOrOffsetBatchesCh = be.requestBatchesCh
case <-be.flushAggregatorCh:
be.flushExecutorCh <- none.V
case nilOrOffsetBatchesCh <- requestBatch:
nilOrOffsetBatchesCh = nil
requestBatch = make(map[string]map[instanceID]submitRq)
}
}
}
func (be *brokerExecutor) runExecutor() {
nilOrRequestBatchesCh := be.requestBatchesCh
var lastErrTime time.Time
commitTicker := time.NewTicker(be.cfg.Consumer.OffsetsCommitInterval)
defer commitTicker.Stop()
offsetCommitLoop:
for {
select {
case requestBatch := <-nilOrRequestBatchesCh:
nilOrRequestBatchesCh = nil
for group, groupRequests := range requestBatch {
kafkaRq := &sarama.OffsetCommitRequest{
Version: 1,
ConsumerGroup: group,
ConsumerGroupGeneration: sarama.GroupGenerationUndefined,
}
for _, rq := range groupRequests {
kafkaRq.AddBlock(rq.id.topic, rq.id.partition, rq.offset.Val, sarama.ReceiveTime, rq.offset.Meta)
}
kafkaRs, err := be.conn.CommitOffset(kafkaRq)
if err != nil {
lastErrTime = time.Now()
be.execActDesc.Log().WithError(err).Error("Connection reset")
be.conn.Close()
}
// Fan the response out to the partition offset managers.
for _, rq := range groupRequests {
rq.resultCh <- submitRs{rq.offset, kafkaRs, err}
}
}
case <-be.flushExecutorCh:
nilOrRequestBatchesCh = be.requestBatchesCh
case <-commitTicker.C:
// Skip several circles after a connection failure to allow a Kafka
// cluster some time to recuperate. Some requests will timeout
// waiting in the channel, but that is ok, for they will be retried.
if time.Since(lastErrTime) < be.cfg.Consumer.RetryBackoff {
be.execActDesc.Log().Warn("Backing off after connection error")
continue offsetCommitLoop
}
nilOrRequestBatchesCh = be.requestBatchesCh
case <-be.execStopCh:
return
}
}
}
func (be *brokerExecutor) String() string {
if be == nil {
return "<nil>"
}
return be.aggrActDesc.String()
}