-
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 5 commits
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 = earliest | ||
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. in Kafka the default is "latest", why are you setting "earliest" out of curiosity? 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. Yes, you are right on the default in kafka. I set latest as a default because, right now, if you create a kafka scaler, it will behave as if we set earliest, if no offset is committed. So I would not break the current behavior for anyone upgrading from a previous version and not seeing the option. 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. yes I would agree, let's see what @zroubalik thinks. 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. I agree, it makes sense to set it to Kafka default, a new major release is a great fit for a change like this. We could add a small note to the docs about it. 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. I've done the change and also changed the docs, let me know if it needs further clarification there. |
||
) | ||
|
||
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["consumerOffsetReset"]) | ||
if policy != earliest && policy != latest { | ||
return meta, fmt.Errorf("err consumerOffsetReset policy %s given", policy) | ||
} | ||
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 ;)