Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --kafka.producer.batch-min-messages collector flag #2371

Merged
merged 1 commit into from
Aug 4, 2020
Merged
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
2 changes: 2 additions & 0 deletions pkg/kafka/producer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Configuration struct {
ProtocolVersion string `mapstructure:"protocol_version"`
BatchLinger time.Duration `mapstructure:"batch_linger"`
BatchSize int `mapstructure:"batch_size"`
BatchMinMessages int `mapstructure:"batch_min_messages"`
BatchMaxMessages int `mapstructure:"batch_max_messages"`
auth.AuthenticationConfig `mapstructure:"authentication"`
}
Expand All @@ -49,6 +50,7 @@ func (c *Configuration) NewProducer() (sarama.AsyncProducer, error) {
saramaConfig.Producer.Return.Successes = true
saramaConfig.Producer.Flush.Bytes = c.BatchSize
saramaConfig.Producer.Flush.Frequency = c.BatchLinger
saramaConfig.Producer.Flush.Messages = c.BatchMinMessages
saramaConfig.Producer.Flush.MaxMessages = c.BatchMaxMessages
if len(c.ProtocolVersion) > 0 {
ver, err := sarama.ParseKafkaVersion(c.ProtocolVersion)
Expand Down
10 changes: 9 additions & 1 deletion plugin/storage/kafka/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
suffixProtocolVersion = ".protocol-version"
suffixBatchLinger = ".batch-linger"
suffixBatchSize = ".batch-size"
suffixBatchMinMessages = ".batch-min-messages"
suffixBatchMaxMessages = ".batch-max-messages"

defaultBroker = "127.0.0.1:9092"
Expand All @@ -55,6 +56,7 @@ const (
defaultCompressionLevel = 0
defaultBatchLinger = 0
defaultBatchSize = 0
defaultBatchMinMessages = 0
defaultBatchMaxMessages = 0
)

Expand Down Expand Up @@ -157,10 +159,15 @@ func (opt *Options) AddFlags(flagSet *flag.FlagSet) {
defaultBatchSize,
"(experimental) Number of bytes to batch before sending records to Kafka. Higher value reduce request to Kafka but increase latency and the possibility of data loss in case of process restart. See https://kafka.apache.org/documentation/",
)
flagSet.Int(
configPrefix+suffixBatchMinMessages,
defaultBatchMinMessages,
"(experimental) The best-effort minimum number of messages needed to send a batch of records to Kafka. Higher value reduce request to Kafka but increase latency and the possibility of data loss in case of process restart. See https://kafka.apache.org/documentation/",
)
flagSet.Int(
configPrefix+suffixBatchMaxMessages,
defaultBatchMaxMessages,
"(experimental) Number of message to batch before sending records to Kafka. Higher value reduce request to Kafka but increase latency and the possibility of data loss in case of process restart. See https://kafka.apache.org/documentation/",
"(experimental) Maximum number of message to batch before sending records to Kafka",
)
auth.AddFlags(configPrefix, flagSet)
}
Expand Down Expand Up @@ -195,6 +202,7 @@ func (opt *Options) InitFromViper(v *viper.Viper) {
AuthenticationConfig: authenticationOptions,
BatchLinger: v.GetDuration(configPrefix + suffixBatchLinger),
BatchSize: v.GetInt(configPrefix + suffixBatchSize),
BatchMinMessages: v.GetInt(configPrefix + suffixBatchMinMessages),
BatchMaxMessages: v.GetInt(configPrefix + suffixBatchMaxMessages),
}
opt.Topic = v.GetString(configPrefix + suffixTopic)
Expand Down
3 changes: 3 additions & 0 deletions plugin/storage/kafka/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestOptionsWithFlags(t *testing.T) {
"--kafka.producer.compression-level=7",
"--kafka.producer.batch-linger=1s",
"--kafka.producer.batch-size=128000",
"--kafka.producer.batch-min-messages=50",
"--kafka.producer.batch-max-messages=100",
})
opts.InitFromViper(v)
Expand All @@ -52,6 +53,7 @@ func TestOptionsWithFlags(t *testing.T) {
assert.Equal(t, 7, opts.Config.CompressionLevel)
assert.Equal(t, 128000, opts.Config.BatchSize)
assert.Equal(t, time.Duration(1*time.Second), opts.Config.BatchLinger)
assert.Equal(t, 50, opts.Config.BatchMinMessages)
assert.Equal(t, 100, opts.Config.BatchMaxMessages)
}

Expand All @@ -69,6 +71,7 @@ func TestFlagDefaults(t *testing.T) {
assert.Equal(t, 0, opts.Config.CompressionLevel)
assert.Equal(t, 0, opts.Config.BatchSize)
assert.Equal(t, time.Duration(0*time.Second), opts.Config.BatchLinger)
assert.Equal(t, 0, opts.Config.BatchMinMessages)
assert.Equal(t, 0, opts.Config.BatchMaxMessages)
}

Expand Down