Skip to content

Commit

Permalink
fix: Use separate variable to track the consume offset
Browse files Browse the repository at this point in the history
This commit updates reader_service.go to use a separate variable to
track the consume offset instead of mutating the last committed
offset. I found this makes the code easier to follow as the last
committed offset does not change.
  • Loading branch information
grobinson-grafana committed Nov 25, 2024
1 parent 5d5affa commit bb99686
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/kafka/partition/reader_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ func (s *ReaderService) starting(ctx context.Context) error {

if lastCommittedOffset == int64(KafkaEndOffset) {
level.Warn(s.logger).Log("msg", fmt.Sprintf("no committed offset found, starting from %d", kafkaStartOffset))
lastCommittedOffset = int64(KafkaStartOffset)
} else {
level.Debug(r.logger).Log("msg", "last committed offset", "offset", lastCommittedOffset)
}

consumeOffset := kafkaStartOffset
if lastCommittedOffset >= 0 {
lastCommittedOffset++ // We want to begin to read from the next offset, but only if we've previously committed an offset.
// Read from the next offset.
consumeOffset = lastCommittedOffset + 1
}

s.reader.SetOffsetForConsumption(lastCommittedOffset)
s.reader.SetOffsetForConsumption(consumeOffset)

if targetLag, maxLag := s.cfg.TargetConsumerLagAtStartup, s.cfg.MaxConsumerLagAtStartup; targetLag > 0 && maxLag > 0 {
consumer, err := s.consumerFactory(s.committer)
Expand Down

0 comments on commit bb99686

Please sign in to comment.