Skip to content

Commit 2e077cf

Browse files
authored
Fix default retention time value in offset commit (#2700)
The retention time field of the offset commit request uses -1 to mean "use the broker's default". Sarama uses the value 0 in the `Config.Consumer.Offsets.Retenton` field to mean "use the broker's default". Ensure that Sarama's default (0) is correctly mapped to the broker's default (-1). Fixes: #2677 Signed-off-by: Adrian Preston <[email protected]>
1 parent f97ced2 commit 2e077cf

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

offset_manager.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,13 @@ func (om *offsetManager) constructRequest() *OffsetCommitRequest {
318318

319319
// request controlled retention was only supported from V2-V4 (it became
320320
// broker-only after that) so if the user has set the config options then
321-
// flow those through as retention time on the commit request
322-
if r.Version >= 2 && r.Version < 5 && om.conf.Consumer.Offsets.Retention > 0 {
323-
r.RetentionTime = int64(om.conf.Consumer.Offsets.Retention / time.Millisecond)
321+
// flow those through as retention time on the commit request.
322+
if r.Version >= 2 && r.Version < 5 {
323+
// Map Sarama's default of 0 to Kafka's default of -1
324+
r.RetentionTime = -1
325+
if om.conf.Consumer.Offsets.Retention > 0 {
326+
r.RetentionTime = int64(om.conf.Consumer.Offsets.Retention / time.Millisecond)
327+
}
324328
}
325329

326330
om.pomsLock.RLock()

offset_manager_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sarama
22

33
import (
44
"errors"
5+
"fmt"
56
"sync/atomic"
67
"testing"
78
"time"
@@ -572,3 +573,63 @@ func TestAbortPartitionOffsetManager(t *testing.T) {
572573
safeClose(t, om)
573574
safeClose(t, testClient)
574575
}
576+
577+
// Validate that the constructRequest() method correctly maps Sarama's default for
578+
// Config.Consumer.Offsets.Retention to the equivalent Kafka value.
579+
func TestConstructRequestRetentionTime(t *testing.T) {
580+
expectedRetention := func(version KafkaVersion, retention time.Duration) int64 {
581+
switch {
582+
case version.IsAtLeast(V2_1_0_0):
583+
// version >= 2.1.0: Client specified retention time isn't supported in the
584+
// offset commit request anymore, thus the retention time field set in the
585+
// OffsetCommitRequest struct should be 0.
586+
return 0
587+
case version.IsAtLeast(V0_9_0_0):
588+
// 0.9.0 <= version < 2.1.0: Retention time *is* supported in the offset commit
589+
// request. Sarama's default retention times (0) must be mapped to the Kafka
590+
// default (-1). Non-zero Sarama times are converted from time.Duration to
591+
// an int64 millisecond value.
592+
if retention > 0 {
593+
return int64(retention / time.Millisecond)
594+
} else {
595+
return -1
596+
}
597+
default:
598+
// version < 0.9.0: Client specified retention time is not supported in the offset
599+
// commit request, thus the retention time field set in the OffsetCommitRequest
600+
// struct should be 0.
601+
return 0
602+
}
603+
}
604+
605+
for _, version := range SupportedVersions {
606+
for _, retention := range []time.Duration{0, time.Millisecond} {
607+
name := fmt.Sprintf("version %s retention: %s", version, retention)
608+
t.Run(name, func(t *testing.T) {
609+
// Perform necessary setup for calling the constructRequest() method. This
610+
// test-case only cares about the code path that sets the retention time
611+
// field in the returned request struct.
612+
conf := NewTestConfig()
613+
conf.Version = version
614+
conf.Consumer.Offsets.Retention = retention
615+
om := &offsetManager{
616+
conf: conf,
617+
poms: map[string]map[int32]*partitionOffsetManager{
618+
"topic": {
619+
0: {
620+
dirty: true,
621+
},
622+
},
623+
},
624+
}
625+
626+
req := om.constructRequest()
627+
628+
expectedRetention := expectedRetention(version, retention)
629+
if req.RetentionTime != expectedRetention {
630+
t.Errorf("expected retention time %d, got: %d", expectedRetention, req.RetentionTime)
631+
}
632+
})
633+
}
634+
}
635+
}

0 commit comments

Comments
 (0)