forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
98 lines (88 loc) · 2.99 KB
/
manager.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
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package offset
import (
"strconv"
"sync"
"time"
"github.com/uber/jaeger-lib/metrics"
)
const (
resetInterval = 100 * time.Millisecond
)
// Manager accepts kafka offsets and commits them using the provided kafka consumer
//
// The Manager is designed to be used in a scenario where the consumption of kafka offsets
// is decoupled from the processing of offsets asynchronously via goroutines. This breaks the
// ordering guarantee which could result in the completion of processing of an earlier message
// after the processing of a later message.
//
// It assumes that Kafka offsets are sequential and monotonically increasing[1], and maintains
// sorted lists of offsets per partition.
//
// [1] https://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html
type Manager struct {
markOffsetFunction MarkOffset
offsetCommitCount metrics.Counter
lastCommittedOffset metrics.Gauge
minOffset int64
list *ConcurrentList
close chan struct{}
isClosed sync.WaitGroup
}
// MarkOffset is a func that marks offsets in Kafka
type MarkOffset func(offset int64)
// NewManager creates a new Manager
func NewManager(minOffset int64, markOffset MarkOffset, partition int32, factory metrics.Factory) *Manager {
return &Manager{
markOffsetFunction: markOffset,
close: make(chan struct{}),
offsetCommitCount: factory.Counter("offset-commits-total", map[string]string{"partition": strconv.Itoa(int(partition))}),
lastCommittedOffset: factory.Gauge("last-committed-offset", map[string]string{"partition": strconv.Itoa(int(partition))}),
list: newConcurrentList(minOffset),
minOffset: minOffset,
}
}
// MarkOffset marks the offset of a consumer message
func (m *Manager) MarkOffset(offset int64) {
m.list.insert(offset)
}
// Start starts the Manager
func (m *Manager) Start() {
m.isClosed.Add(1)
go func() {
lastCommittedOffset := m.minOffset
for {
select {
case <-time.After(resetInterval):
offset := m.list.setToHighestContiguous()
if lastCommittedOffset != offset {
m.offsetCommitCount.Inc(1)
m.lastCommittedOffset.Update(offset)
m.markOffsetFunction(offset)
lastCommittedOffset = offset
}
case <-m.close:
m.isClosed.Done()
return
}
}
}()
}
// Close closes the Manager
func (m *Manager) Close() error {
close(m.close)
m.isClosed.Wait()
return nil
}