-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[v2]adds consumer offset reset policy option to keda kafka scaler #925
Changes from 1 commit
b0432a9
c1967c8
6de5df5
81f5605
0187474
f8a4648
81246de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,11 @@ type kafkaScaler struct { | |
} | ||
|
||
type kafkaMetadata struct { | ||
bootstrapServers []string | ||
group string | ||
topic string | ||
lagThreshold int64 | ||
bootstrapServers []string | ||
group string | ||
topic string | ||
lagThreshold int64 | ||
consumerOffsetReset offsetResetPolicy | ||
|
||
// auth | ||
authMode kafkaAuthMode | ||
|
@@ -42,6 +43,13 @@ type kafkaMetadata struct { | |
ca string | ||
} | ||
|
||
type offsetResetPolicy string | ||
|
||
const ( | ||
latest offsetResetPolicy = "latest" | ||
earliest offsetResetPolicy = "earliest" | ||
) | ||
|
||
type kafkaAuthMode string | ||
|
||
const ( | ||
|
@@ -57,6 +65,7 @@ const ( | |
lagThresholdMetricName = "lagThreshold" | ||
kafkaMetricType = "External" | ||
defaultKafkaLagThreshold = 10 | ||
defaultOffsetReset = latest | ||
) | ||
|
||
var kafkaLog = logf.Log.WithName("kafka_scaler") | ||
|
@@ -100,6 +109,16 @@ func parseKafkaMetadata(resolvedEnv, metadata, authParams map[string]string) (ka | |
} | ||
meta.topic = metadata["topic"] | ||
|
||
meta.consumerOffsetReset = defaultOffsetReset | ||
|
||
if metadata["consumerOffsetReset"] != "" { | ||
policy := offsetResetPolicy(metadata["offsetReset"]) | ||
grassiale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if policy != earliest && policy != latest { | ||
return meta, fmt.Errorf("err offsetReset policy %s given", policy) | ||
grassiale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
meta.consumerOffsetReset = policy | ||
} | ||
|
||
meta.lagThreshold = defaultKafkaLagThreshold | ||
|
||
if val, ok := metadata[lagThresholdMetricName]; ok { | ||
|
@@ -295,11 +314,13 @@ func (s *kafkaScaler) getLagForPartition(partition int32, offsets *sarama.Offset | |
} | ||
|
||
var lag int64 | ||
// For now, assume a consumer group that has no committed | ||
// offset will read all messages from the topic. This may be | ||
// something we want to allow users to configure. | ||
|
||
if consumerOffset == sarama.OffsetNewest || consumerOffset == sarama.OffsetOldest { | ||
lag = latestOffset | ||
if s.metadata.consumerOffsetReset == latest { | ||
lag = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when you have no offset committed and you create a new consumer with reset latest policy then the lag should be 0 since the consumer is aligned with the latest offset on the topic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I am not a Kafka expert, but the properties naming is pretty confusing |
||
} else { | ||
lag = latestOffset | ||
} | ||
} else { | ||
lag = latestOffset - consumerOffset | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we find a better name instead of
consumerOffsetReset
. Actually the type name would be a great name for the struct field as well sooffsetResetPolicy
. It's not the first time that a field name is the same as the type name, or?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely ;)