Skip to content

Commit

Permalink
Add -ingest-storage.kafka.target-consumer-lag-at-startup support
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Pracucci <[email protected]>
  • Loading branch information
pracucci committed Jul 2, 2024
1 parent d972d57 commit a86cb02
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 51 deletions.
12 changes: 11 additions & 1 deletion cmd/mimir/config-descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -6578,11 +6578,21 @@
"fieldFlag": "ingest-storage.kafka.consume-from-timestamp-at-startup",
"fieldType": "int"
},
{
"kind": "field",
"name": "target_consumer_lag_at_startup",
"required": false,
"desc": "The best-effort maximum lag a consumer tries to achieve at startup. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup.",
"fieldValue": null,
"fieldDefaultValue": 2000000000,
"fieldFlag": "ingest-storage.kafka.target-consumer-lag-at-startup",
"fieldType": "duration"
},
{
"kind": "field",
"name": "max_consumer_lag_at_startup",
"required": false,
"desc": "The maximum tolerated lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set 0 to disable waiting for maximum consumer lag being honored at startup.",
"desc": "The guaranteed maximum lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup.",
"fieldValue": null,
"fieldDefaultValue": 15000000000,
"fieldFlag": "ingest-storage.kafka.max-consumer-lag-at-startup",
Expand Down
4 changes: 3 additions & 1 deletion cmd/mimir/help-all.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1332,9 +1332,11 @@ Usage of ./cmd/mimir/mimir:
-ingest-storage.kafka.last-produced-offset-retry-timeout duration
How long to retry a failed request to get the last produced offset. (default 10s)
-ingest-storage.kafka.max-consumer-lag-at-startup duration
The maximum tolerated lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set 0 to disable waiting for maximum consumer lag being honored at startup. (default 15s)
The guaranteed maximum lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup. (default 15s)
-ingest-storage.kafka.producer-max-record-size-bytes int
The maximum size of a Kafka record data that should be generated by the producer. An incoming write request larger than this size is split into multiple Kafka records. We strongly recommend to not change this setting unless for testing purposes. (default 15983616)
-ingest-storage.kafka.target-consumer-lag-at-startup duration
The best-effort maximum lag a consumer tries to achieve at startup. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup. (default 2s)
-ingest-storage.kafka.topic string
The Kafka topic name.
-ingest-storage.kafka.wait-strong-read-consistency-timeout duration
Expand Down
4 changes: 3 additions & 1 deletion cmd/mimir/help.txt.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,11 @@ Usage of ./cmd/mimir/mimir:
-ingest-storage.kafka.last-produced-offset-retry-timeout duration
How long to retry a failed request to get the last produced offset. (default 10s)
-ingest-storage.kafka.max-consumer-lag-at-startup duration
The maximum tolerated lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set 0 to disable waiting for maximum consumer lag being honored at startup. (default 15s)
The guaranteed maximum lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup. (default 15s)
-ingest-storage.kafka.producer-max-record-size-bytes int
The maximum size of a Kafka record data that should be generated by the producer. An incoming write request larger than this size is split into multiple Kafka records. We strongly recommend to not change this setting unless for testing purposes. (default 15983616)
-ingest-storage.kafka.target-consumer-lag-at-startup duration
The best-effort maximum lag a consumer tries to achieve at startup. Set both -ingest-storage.kafka.target-consumer-lag-at-startup and -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting for maximum consumer lag being honored at startup. (default 2s)
-ingest-storage.kafka.topic string
The Kafka topic name.
-ingest-storage.kafka.wait-strong-read-consistency-timeout duration
Expand Down
15 changes: 12 additions & 3 deletions docs/sources/mimir/configure/configuration-parameters/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3751,10 +3751,19 @@ kafka:
# CLI flag: -ingest-storage.kafka.consume-from-timestamp-at-startup
[consume_from_timestamp_at_startup: <int> | default = 0]
# The maximum tolerated lag before a consumer is considered to have caught up
# The best-effort maximum lag a consumer tries to achieve at startup. Set both
# -ingest-storage.kafka.target-consumer-lag-at-startup and
# -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting
# for maximum consumer lag being honored at startup.
# CLI flag: -ingest-storage.kafka.target-consumer-lag-at-startup
[target_consumer_lag_at_startup: <duration> | default = 2s]
# The guaranteed maximum lag before a consumer is considered to have caught up
# reading from a partition at startup, becomes ACTIVE in the hash ring and
# passes the readiness check. Set 0 to disable waiting for maximum consumer
# lag being honored at startup.
# passes the readiness check. Set both
# -ingest-storage.kafka.target-consumer-lag-at-startup and
# -ingest-storage.kafka.max-consumer-lag-at-startup to 0 to disable waiting
# for maximum consumer lag being honored at startup.
# CLI flag: -ingest-storage.kafka.max-consumer-lag-at-startup
[max_consumer_lag_at_startup: <duration> | default = 15s]
Expand Down
21 changes: 19 additions & 2 deletions pkg/storage/ingest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
consumeFromStart = "start"
consumeFromEnd = "end"
consumeFromTimestamp = "timestamp"

kafkaConfigFlagPrefix = "ingest-storage.kafka"
targetConsumerLagAtStartupFlag = kafkaConfigFlagPrefix + ".target-consumer-lag-at-startup"
maxConsumerLagAtStartupFlag = kafkaConfigFlagPrefix + ".max-consumer-lag-at-startup"
)

var (
Expand All @@ -25,6 +29,8 @@ var (
ErrInvalidWriteClients = errors.New("the configured number of write clients is invalid (must be greater than 0)")
ErrInvalidConsumePosition = errors.New("the configured consume position is invalid")
ErrInvalidProducerMaxRecordSizeBytes = fmt.Errorf("the configured producer max record size bytes must be a value between %d and %d", minProducerRecordDataBytesLimit, maxProducerRecordDataBytesLimit)
ErrInconsistentConsumerLagAtStartup = fmt.Errorf("the target and max consumer lag at startup must be either both set to 0 or to a value greater than 0")
ErrInvalidMaxConsumerLagAtStartup = fmt.Errorf("the configured max consumer lag at startup must greater or equal than the configured target consumer lag")

consumeFromPositionOptions = []string{consumeFromLastOffset, consumeFromStart, consumeFromEnd, consumeFromTimestamp}
)
Expand All @@ -38,7 +44,7 @@ type Config struct {
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.BoolVar(&cfg.Enabled, "ingest-storage.enabled", false, "True to enable the ingestion via object storage.")

cfg.KafkaConfig.RegisterFlagsWithPrefix("ingest-storage.kafka", f)
cfg.KafkaConfig.RegisterFlagsWithPrefix(kafkaConfigFlagPrefix, f)
cfg.Migration.RegisterFlagsWithPrefix("ingest-storage.migration", f)
}

Expand Down Expand Up @@ -73,6 +79,7 @@ type KafkaConfig struct {

ConsumeFromPositionAtStartup string `yaml:"consume_from_position_at_startup"`
ConsumeFromTimestampAtStartup int64 `yaml:"consume_from_timestamp_at_startup"`
TargetConsumerLagAtStartup time.Duration `yaml:"target_consumer_lag_at_startup"`
MaxConsumerLagAtStartup time.Duration `yaml:"max_consumer_lag_at_startup"`

AutoCreateTopicEnabled bool `yaml:"auto_create_topic_enabled"`
Expand Down Expand Up @@ -106,7 +113,11 @@ func (cfg *KafkaConfig) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet)

f.StringVar(&cfg.ConsumeFromPositionAtStartup, prefix+".consume-from-position-at-startup", consumeFromLastOffset, fmt.Sprintf("From which position to start consuming the partition at startup. Supported options: %s.", strings.Join(consumeFromPositionOptions, ", ")))
f.Int64Var(&cfg.ConsumeFromTimestampAtStartup, prefix+".consume-from-timestamp-at-startup", 0, fmt.Sprintf("Milliseconds timestamp after which the consumption of the partition starts at startup. Only applies when consume-from-position-at-startup is %s", consumeFromTimestamp))
f.DurationVar(&cfg.MaxConsumerLagAtStartup, prefix+".max-consumer-lag-at-startup", 15*time.Second, "The maximum tolerated lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. Set 0 to disable waiting for maximum consumer lag being honored at startup.")

howToDisableConsumerLagAtStartup := fmt.Sprintf("Set both -%s and -%s to 0 to disable waiting for maximum consumer lag being honored at startup.", targetConsumerLagAtStartupFlag, maxConsumerLagAtStartupFlag)
f.DurationVar(&cfg.TargetConsumerLagAtStartup, targetConsumerLagAtStartupFlag, 2*time.Second, "The best-effort maximum lag a consumer tries to achieve at startup. "+howToDisableConsumerLagAtStartup)
f.DurationVar(&cfg.MaxConsumerLagAtStartup, maxConsumerLagAtStartupFlag, 15*time.Second, "The guaranteed maximum lag before a consumer is considered to have caught up reading from a partition at startup, becomes ACTIVE in the hash ring and passes the readiness check. "+howToDisableConsumerLagAtStartup)

f.BoolVar(&cfg.AutoCreateTopicEnabled, prefix+".auto-create-topic-enabled", true, "Enable auto-creation of Kafka topic if it doesn't exist.")
f.IntVar(&cfg.AutoCreateTopicDefaultPartitions, prefix+".auto-create-topic-default-partitions", 0, "When auto-creation of Kafka topic is enabled and this value is positive, Kafka's num.partitions configuration option is set on Kafka brokers with this value when Mimir component that uses Kafka starts. This configuration option specifies the default number of partitions that the Kafka broker uses for auto-created topics. Note that this is a Kafka-cluster wide setting, and applies to any auto-created topic. If the setting of num.partitions fails, Mimir proceeds anyways, but auto-created topics could have an incorrect number of partitions.")

Expand Down Expand Up @@ -141,6 +152,12 @@ func (cfg *KafkaConfig) Validate() error {
if cfg.ProducerMaxRecordSizeBytes < minProducerRecordDataBytesLimit || cfg.ProducerMaxRecordSizeBytes > maxProducerRecordDataBytesLimit {
return ErrInvalidProducerMaxRecordSizeBytes
}
if (cfg.TargetConsumerLagAtStartup != 0) != (cfg.MaxConsumerLagAtStartup != 0) {
return ErrInconsistentConsumerLagAtStartup
}
if cfg.MaxConsumerLagAtStartup < cfg.TargetConsumerLagAtStartup {
return ErrInvalidMaxConsumerLagAtStartup
}

return nil
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/storage/ingest/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ func TestConfig_Validate(t *testing.T) {
},
expectedErr: ErrInvalidProducerMaxRecordSizeBytes,
},
"should fail if target consumer lag is enabled but max consumer lag is not": {
setup: func(cfg *Config) {
cfg.Enabled = true
cfg.KafkaConfig.Address = "localhost"
cfg.KafkaConfig.Topic = "test"
cfg.KafkaConfig.TargetConsumerLagAtStartup = 2 * time.Second
cfg.KafkaConfig.MaxConsumerLagAtStartup = 0
},
expectedErr: ErrInconsistentConsumerLagAtStartup,
},
"should fail if max consumer lag is enabled but target consumer lag is not": {
setup: func(cfg *Config) {
cfg.Enabled = true
cfg.KafkaConfig.Address = "localhost"
cfg.KafkaConfig.Topic = "test"
cfg.KafkaConfig.TargetConsumerLagAtStartup = 0
cfg.KafkaConfig.MaxConsumerLagAtStartup = 2 * time.Second
},
expectedErr: ErrInconsistentConsumerLagAtStartup,
},
"should fail if target consumer lag is > max consumer lag": {
setup: func(cfg *Config) {
cfg.Enabled = true
cfg.KafkaConfig.Address = "localhost"
cfg.KafkaConfig.Topic = "test"
cfg.KafkaConfig.TargetConsumerLagAtStartup = 2 * time.Second
cfg.KafkaConfig.MaxConsumerLagAtStartup = 1 * time.Second
},
expectedErr: ErrInvalidMaxConsumerLagAtStartup,
},
}

for testName, testData := range tests {
Expand Down
Loading

0 comments on commit a86cb02

Please sign in to comment.