@@ -18,61 +18,12 @@ import (
18
18
"github.com/grafana/loki/v3/pkg/kafka/client"
19
19
)
20
20
21
- // Partition level metadata in a more easily digestible form than what Kafka provides
22
- type Lag struct {
23
- // First Available Offset in retention
24
- startOffset int64
25
- // Exclusive; the next available offset (as of yet unwritten)
26
- endOffset int64
27
- // Last committed offset
28
- committedOffset int64
29
- // rawLag measures how far behind the most recently committed offset is from the current offset.
30
- // In special cases, this can be positive even when there are no more records to process,
31
- // which happens when there is a gap between the last committed offset and the current offset, but
32
- // it is out of retention (unrecoverable).
33
- rawLag int64
34
- }
35
-
36
- func NewLag (startOffset , endOffset , committedOffset , rawLag int64 ) Lag {
37
- return Lag {
38
- startOffset : startOffset ,
39
- endOffset : endOffset ,
40
- committedOffset : committedOffset ,
41
- rawLag : rawLag ,
42
- }
43
- }
44
-
45
- // FirstUncommittedOffset returns the first offset that has not yet been committed
46
- func (l Lag ) FirstUncommittedOffset () int64 {
47
- // startOffset is the previously-committed offset, so we need to start processing the first
48
- // _uncommitted_ offset
49
- return max (l .committedOffset + 1 , l .startOffset )
50
- }
51
-
52
- func (l Lag ) LastCommittedOffset () int64 {
53
- return l .committedOffset
54
- }
55
-
56
- // NextAvailableOffset returns the next unwritten offset in a partition,
57
- // i.e. the end offset (exclusive)
58
- func (l Lag ) NextAvailableOffset () int64 {
59
- return l .endOffset
60
- }
61
-
62
- // Lag returns the difference between the last produced offset
63
- // and the first Uncommitted (but available) offset
64
- func (l Lag ) Lag () int64 {
65
- return l .endOffset - l .FirstUncommittedOffset ()
66
- }
67
-
68
21
type OffsetManager interface {
69
22
Topic () string
70
23
ConsumerGroup () string
71
24
72
- // GroupLag returns the lag for the consumer group. Uses fallbackOffsetMillis to calculate the lag if the consumer group has no commits.
73
- GroupLag (ctx context.Context , fallbackOffsetMillis int64 ) (map [int32 ]Lag , error )
74
- FetchLastCommittedOffset (ctx context.Context , partition int32 ) (int64 , error )
75
- FetchPartitionOffset (ctx context.Context , partition int32 , position SpecialOffset ) (int64 , error )
25
+ LastCommittedOffset (ctx context.Context , partition int32 ) (int64 , error )
26
+ PartitionOffset (ctx context.Context , partition int32 , position SpecialOffset ) (int64 , error )
76
27
NextOffset (ctx context.Context , partition int32 , t time.Time ) (int64 , error )
77
28
Commit (ctx context.Context , partition int32 , offset int64 ) error
78
29
}
@@ -157,8 +108,8 @@ func (r *KafkaOffsetManager) NextOffset(ctx context.Context, partition int32, t
157
108
return listed .Offset , nil
158
109
}
159
110
160
- // FetchLastCommittedOffset retrieves the last committed offset for this partition
161
- func (r * KafkaOffsetManager ) FetchLastCommittedOffset (ctx context.Context , partitionID int32 ) (int64 , error ) {
111
+ // LastCommittedOffset retrieves the last committed offset for this partition
112
+ func (r * KafkaOffsetManager ) LastCommittedOffset (ctx context.Context , partitionID int32 ) (int64 , error ) {
162
113
req := kmsg .NewPtrOffsetFetchRequest ()
163
114
req .Topics = []kmsg.OffsetFetchRequestTopic {{
164
115
Topic : r .cfg .Topic ,
@@ -203,7 +154,7 @@ func (r *KafkaOffsetManager) FetchLastCommittedOffset(ctx context.Context, parti
203
154
}
204
155
205
156
// FetchPartitionOffset retrieves the offset for a specific position
206
- func (r * KafkaOffsetManager ) FetchPartitionOffset (ctx context.Context , partitionID int32 , position SpecialOffset ) (int64 , error ) {
157
+ func (r * KafkaOffsetManager ) PartitionOffset (ctx context.Context , partitionID int32 , position SpecialOffset ) (int64 , error ) {
207
158
partitionReq := kmsg .NewListOffsetsRequestTopicPartition ()
208
159
partitionReq .Partition = partitionID
209
160
partitionReq .Timestamp = int64 (position )
@@ -248,40 +199,6 @@ func (r *KafkaOffsetManager) FetchPartitionOffset(ctx context.Context, partition
248
199
return partition .Offset , nil
249
200
}
250
201
251
- // GroupLag returns the lag for the consumer group. Uses fallbackOffsetMillis to calculate the lag if the consumer group has no commits.
252
- func (r * KafkaOffsetManager ) GroupLag (ctx context.Context , fallbackOffsetMillis int64 ) (map [int32 ]Lag , error ) {
253
- lag , err := GetGroupLag (ctx , r .adminClient , r .cfg .Topic , r .ConsumerGroup (), fallbackOffsetMillis )
254
- if err != nil {
255
- return nil , err
256
- }
257
-
258
- offsets , ok := lag [r .cfg .Topic ]
259
- if ! ok {
260
- return nil , errors .New ("no lag found for the topic" )
261
- }
262
-
263
- res := make (map [int32 ]Lag , len (offsets ))
264
-
265
- for partition , partitionOffset := range offsets {
266
- res [partition ] = Lag {
267
- // 1. kadm.GroupMemberLag contains valid Commit.At even when consumer group never committed any offset.
268
- // no additional validation is needed here
269
- // 2. committed offset could be behind start offset if we are falling behind retention period.
270
-
271
- // startOffset is the previously-committed offset, so we need to start processing the first
272
- // _uncommitted_ offset
273
- startOffset : max (partitionOffset .Commit .At + 1 , partitionOffset .Start .Offset ),
274
- // endOffset is initially the next available offset: this is why we treat jobs as end-exclusive:
275
- // so we won't try polling forever to a partition that won't have any more records
276
- endOffset : partitionOffset .End .Offset ,
277
- committedOffset : partitionOffset .Commit .At ,
278
- rawLag : partitionOffset .Lag ,
279
- }
280
- }
281
-
282
- return res , nil
283
- }
284
-
285
202
// Commit commits an offset to the consumer group
286
203
func (r * KafkaOffsetManager ) Commit (ctx context.Context , partitionID int32 , offset int64 ) error {
287
204
admin := kadm .NewClient (r .client )
0 commit comments