Skip to content

Commit

Permalink
Do a final commit on end consumer group generation for immediate comm…
Browse files Browse the repository at this point in the history
…its (#715)

* Do a final commit on end consumer group generation for immediate commits

* add test for final commit on generation end

Co-authored-by: rhansen2 <rob.hansen@sendgrid.com>
  • Loading branch information
Steve van Loben Sels and rhansen2 committed Jan 21, 2022
1 parent d81d37a commit 382e96d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
19 changes: 19 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ func (r *Reader) commitLoopImmediate(ctx context.Context, gen *Generation) {
for {
select {
case <-ctx.Done():
// drain the commit channel and prepare a single, final commit.
// the commit will combine any outstanding requests and the result
// will be sent back to all the callers of CommitMessages so that
// they can return.
var errchs []chan<- error
for hasCommits := true; hasCommits; {
select {
case req := <-r.commits:
offsets.merge(req.commits)
errchs = append(errchs, req.errch)
default:
hasCommits = false
}
}
err := r.commitOffsetsWithRetry(gen, offsets, defaultCommitRetries)
for _, errch := range errchs {
// NOTE : this will be a buffered channel and will not block.
errch <- err
}
return

case req := <-r.commits:
Expand Down
50 changes: 48 additions & 2 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func makeTestSequence(n int) []Message {
}

func prepareReader(t *testing.T, ctx context.Context, r *Reader, msgs ...Message) {
var config = r.Config()
config := r.Config()
var conn *Conn
var err error

Expand Down Expand Up @@ -710,7 +710,6 @@ func TestReaderPartitionWhenConsumerGroupsEnabled(t *testing.T) {
if !invoke() {
t.Fatalf("expected panic; but NewReader worked?!")
}

}

func TestExtractTopics(t *testing.T) {
Expand Down Expand Up @@ -1281,6 +1280,53 @@ func TestValidateReader(t *testing.T) {
}
}

func TestCommitLoopImmediateFlushOnGenerationEnd(t *testing.T) {
t.Parallel()
var committedOffset int64
var commitCount int
gen := &Generation{
conn: mockCoordinator{
offsetCommitFunc: func(r offsetCommitRequestV2) (offsetCommitResponseV2, error) {
commitCount++
committedOffset = r.Topics[0].Partitions[0].Offset
return offsetCommitResponseV2{}, nil
},
},
done: make(chan struct{}),
log: func(func(Logger)) {},
logError: func(func(Logger)) {},
}

// initialize commits so that the commitLoopImmediate select statement blocks
r := &Reader{stctx: context.Background(), commits: make(chan commitRequest, 100)}

for i := 0; i < 100; i++ {
cr := commitRequest{
commits: []commit{{
topic: "topic",
partition: 0,
offset: int64(i) + 1,
}},
errch: make(chan<- error, 1),
}
r.commits <- cr
}

gen.Start(func(ctx context.Context) {
r.commitLoopImmediate(ctx, gen)
})

gen.close()

if committedOffset != 100 {
t.Fatalf("expected commited offset to be 100 but got %d", committedOffset)
}

if commitCount >= 100 {
t.Fatalf("expected a single final commit on generation end got %d", commitCount)
}
}

func TestCommitOffsetsWithRetry(t *testing.T) {
offsets := offsetStash{"topic": {0: 0}}

Expand Down

0 comments on commit 382e96d

Please sign in to comment.