Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/impl/kafka/input_kafka_franz.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Finally, it's also possible to specify an explicit offset to consume from by add
Field(service.NewStringField("consumer_group").
Description("An optional consumer group to consume as. When specified the partitions of specified topics are automatically distributed across consumers sharing a consumer group, and partition offsets are automatically committed and resumed under this name. Consumer groups are not supported when specifying explicit partitions to consume from in the `topics` field.").
Optional()).
Field(newTransactionIsolationLevelField()).
Field(service.NewStringField("client_id").
Description("An identifier for the client connection.").
Default("bento").
Expand Down Expand Up @@ -207,6 +208,7 @@ type franzKafkaReader struct {
saslConfs []sasl.Mechanism
checkpointLimit int
autoOffsetReset string
isolationLevel kgo.IsolationLevel
commitPeriod time.Duration
regexPattern bool
multiHeader bool
Expand Down Expand Up @@ -361,6 +363,17 @@ func newFranzKafkaReaderFromConfig(conf *service.ParsedConfig, res *service.Reso
return nil, err
}

isolationLevel, err := transactionIsolationLevelFromConfig(conf)
if err != nil {
return nil, err
}
switch isolationLevel {
case transactionIsolationLevelReadUncommitted:
f.isolationLevel = kgo.ReadUncommitted()
case transactionIsolationLevelReadCommitted:
f.isolationLevel = kgo.ReadCommitted()
}

if f.reconnectOnUnknownTopic, err = conf.FieldBool("reconnect_on_unknown_topic_or_partition"); err != nil {
return nil, err
}
Expand Down Expand Up @@ -851,6 +864,7 @@ func (f *franzKafkaReader) Connect(ctx context.Context) error {
kgo.ConsumeTopics(f.topics...),
kgo.ConsumePartitions(f.topicPartitions),
kgo.ConsumeResetOffset(initialOffset),
kgo.FetchIsolationLevel(f.isolationLevel),
kgo.SASL(f.saslConfs...),
kgo.ConsumerGroup(f.consumerGroup),
kgo.ClientID(f.clientID),
Expand Down
53 changes: 53 additions & 0 deletions internal/impl/kafka/input_kafka_franz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,59 @@ topic: foo
}
}

func TestInputKafkaFranzTransactionIsolationLevel(t *testing.T) {
testCases := []struct {
name string
config string
expected kgo.IsolationLevel
}{
{
name: "defaults to read uncommitted",
expected: kgo.ReadUncommitted(),
},
{
name: "explicit read uncommitted",
config: "transaction_isolation_level: read_uncommitted",
expected: kgo.ReadUncommitted(),
},
{
name: "read committed",
config: "transaction_isolation_level: read_committed",
expected: kgo.ReadCommitted(),
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
conf, err := franzKafkaInputConfig().ParseYAML(fmt.Sprintf(`
seed_brokers: [ example.com:1234 ]
topics: [ test ]
consumer_group: test
%s
`, test.config), nil)
require.NoError(t, err)

reader, err := newFranzKafkaReaderFromConfig(conf, service.MockResources())
require.NoError(t, err)
assert.Equal(t, test.expected, reader.isolationLevel)
})
}
}

func TestInputKafkaFranzRejectsInvalidTransactionIsolationLevel(t *testing.T) {
conf, err := franzKafkaInputConfig().ParseYAML(`
seed_brokers: [ example.com:1234 ]
topics: [ test ]
consumer_group: test
transaction_isolation_level: eventually_consistent
`, nil)
require.NoError(t, err)

_, err = newFranzKafkaReaderFromConfig(conf, service.MockResources())
require.Error(t, err)
assert.EqualError(t, err, `invalid transaction isolation level: "eventually_consistent"`)
}

func TestInputKafkaFranzRetriableError(t *testing.T) {
conf, err := franzKafkaInputConfig().ParseYAML(`
seed_brokers: [ localhost:9092 ]
Expand Down
11 changes: 11 additions & 0 deletions internal/impl/kafka/input_sarama_kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Unfortunately this error message will appear for a wide range of connection prob
service.NewStringField(iskFieldConsumerGroup).
Description("An identifier for the consumer group of the connection. This field can be explicitly made empty in order to disable stored offsets for the consumed topic partitions.").
Default(""),
newTransactionIsolationLevelField(),
service.NewStringField(iskFieldClientID).
Description("An identifier for the client connection.").
Advanced().Default("bento"),
Expand Down Expand Up @@ -480,6 +481,16 @@ func (k *kafkaReader) saramaConfigFromParsed(conf *service.ParsedConfig) (*saram

config.Net.DialTimeout = time.Second
config.Consumer.Return.Errors = true
isolationLevel, err := transactionIsolationLevelFromConfig(conf)
if err != nil {
return nil, err
}
switch isolationLevel {
case transactionIsolationLevelReadUncommitted:
config.Consumer.IsolationLevel = sarama.ReadUncommitted
case transactionIsolationLevelReadCommitted:
config.Consumer.IsolationLevel = sarama.ReadCommitted
}
if config.Consumer.MaxProcessingTime, err = conf.FieldDuration(iskFieldMaxProcessingPeriod); err != nil {
return nil, err
}
Expand Down
54 changes: 54 additions & 0 deletions internal/impl/kafka/input_sarama_kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/IBM/sarama"
"github.com/Jeffail/gabs/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -48,3 +49,56 @@ topics: %v
})
}
}

func TestKafkaTransactionIsolationLevel(t *testing.T) {
testCases := []struct {
name string
config string
expected sarama.IsolationLevel
}{
{
name: "defaults to read uncommitted",
expected: sarama.ReadUncommitted,
},
{
name: "explicit read uncommitted",
config: "transaction_isolation_level: read_uncommitted",
expected: sarama.ReadUncommitted,
},
{
name: "read committed",
config: "transaction_isolation_level: read_committed",
expected: sarama.ReadCommitted,
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
conf, err := iskConfigSpec().ParseYAML(fmt.Sprintf(`
addresses: [ example.com:1234 ]
topics: [ test ]
consumer_group: test
%s
`, test.config), nil)
require.NoError(t, err)

reader, err := newKafkaReaderFromParsed(conf, service.MockResources())
require.NoError(t, err)
assert.Equal(t, test.expected, reader.saramConf.Consumer.IsolationLevel)
})
}
}

func TestKafkaRejectsInvalidTransactionIsolationLevel(t *testing.T) {
conf, err := iskConfigSpec().ParseYAML(`
addresses: [ example.com:1234 ]
topics: [ test ]
consumer_group: test
transaction_isolation_level: eventually_consistent
`, nil)
require.NoError(t, err)

_, err = newKafkaReaderFromParsed(conf, service.MockResources())
require.Error(t, err)
assert.EqualError(t, err, `invalid transaction isolation level: "eventually_consistent"`)
}
Loading