-
Notifications
You must be signed in to change notification settings - Fork 592
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
support partition consumer mode for kafka channel #879
Merged
knative-prow-robot
merged 4 commits into
knative:master
from
yuzisun:kafka_partition_consumer
Mar 14, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package controller | ||
|
||
import "github.com/bsm/sarama-cluster" | ||
|
||
type KafkaProvisionerConfig struct { | ||
Brokers []string | ||
Brokers []string | ||
ConsumerMode cluster.ConsumerMode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,24 +49,34 @@ type KafkaDispatcher struct { | |
|
||
type KafkaConsumer interface { | ||
Messages() <-chan *sarama.ConsumerMessage | ||
Partitions() <-chan cluster.PartitionConsumer | ||
MarkOffset(msg *sarama.ConsumerMessage, metadata string) | ||
Close() (err error) | ||
} | ||
|
||
type KafkaCluster interface { | ||
NewConsumer(groupID string, topics []string) (KafkaConsumer, error) | ||
|
||
GetConsumerMode() cluster.ConsumerMode | ||
} | ||
|
||
type saramaCluster struct { | ||
kafkaBrokers []string | ||
|
||
consumerMode cluster.ConsumerMode | ||
} | ||
|
||
func (c *saramaCluster) NewConsumer(groupID string, topics []string) (KafkaConsumer, error) { | ||
consumerConfig := cluster.NewConfig() | ||
consumerConfig.Version = sarama.V1_1_0_0 | ||
consumerConfig.Group.Mode = c.consumerMode | ||
return cluster.NewConsumer(c.kafkaBrokers, groupID, topics, consumerConfig) | ||
} | ||
|
||
func (c *saramaCluster) GetConsumerMode() cluster.ConsumerMode { | ||
return c.consumerMode | ||
} | ||
|
||
type subscription struct { | ||
Namespace string | ||
Name string | ||
|
@@ -166,6 +176,7 @@ func (d *KafkaDispatcher) subscribe(channelRef provisioners.ChannelReference, su | |
|
||
group := fmt.Sprintf("%s.%s.%s", controller.Name, sub.Namespace, sub.Name) | ||
consumer, err := d.kafkaCluster.NewConsumer(group, []string{topicName}) | ||
|
||
if err != nil { | ||
// we can not create a consumer - logging that, with reason | ||
d.logger.Info("Could not create proper consumer", zap.Error(err)) | ||
|
@@ -179,26 +190,55 @@ func (d *KafkaDispatcher) subscribe(channelRef provisioners.ChannelReference, su | |
} | ||
channelMap[sub] = consumer | ||
|
||
go func() { | ||
for { | ||
msg, more := <-consumer.Messages() | ||
if more { | ||
d.logger.Info("Dispatching a message for subscription", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
message := fromKafkaMessage(msg) | ||
err := d.dispatchMessage(message, sub) | ||
if err != nil { | ||
d.logger.Warn("Got error trying to dispatch message", zap.Error(err)) | ||
} | ||
// TODO: handle errors with pluggable strategy | ||
consumer.MarkOffset(msg, "") // Mark message as processed | ||
} else { | ||
break | ||
if cluster.ConsumerModePartitions == d.kafkaCluster.GetConsumerMode() { | ||
yuzisun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
go d.partitionConsumerLoop(consumer, channelRef, sub) | ||
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. thanks a lot, this looks easier to test now. |
||
} else { | ||
go d.multiplexConsumerLoop(consumer, channelRef, sub) | ||
} | ||
return nil | ||
} | ||
|
||
func (d *KafkaDispatcher) partitionConsumerLoop(consumer KafkaConsumer, channelRef provisioners.ChannelReference, sub subscription) { | ||
d.logger.Info("Partition Consumer for subscription started", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
for { | ||
pc, more := <-consumer.Partitions() | ||
if !more { | ||
break | ||
} | ||
go func(pc cluster.PartitionConsumer) { | ||
for msg := range pc.Messages() { | ||
d.dispatch(channelRef, sub, consumer, msg) | ||
} | ||
}(pc) | ||
} | ||
d.logger.Info("Partition Consumer for subscription stopped", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
} | ||
|
||
func (d *KafkaDispatcher) multiplexConsumerLoop(consumer KafkaConsumer, channelRef provisioners.ChannelReference, sub subscription) { | ||
d.logger.Info("Consumer for subscription started", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
for { | ||
msg, more := <-consumer.Messages() | ||
if more { | ||
d.dispatch(channelRef, sub, consumer, msg) | ||
} else { | ||
break | ||
} | ||
d.logger.Info("Consumer for subscription stopped", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
}() | ||
} | ||
d.logger.Info("Consumer for subscription stopped", zap.Any("channelRef", channelRef), zap.Any("subscription", sub)) | ||
} | ||
|
||
return nil | ||
func (d *KafkaDispatcher) dispatch(channelRef provisioners.ChannelReference, sub subscription, consumer KafkaConsumer, | ||
msg *sarama.ConsumerMessage) error { | ||
d.logger.Info("Dispatching a message for subscription", zap.Any("channelRef", channelRef), | ||
zap.Any("subscription", sub), zap.Any("partition", msg.Partition), zap.Any("offset", msg.Offset)) | ||
message := fromKafkaMessage(msg) | ||
err := d.dispatchMessage(message, sub) | ||
if err != nil { | ||
d.logger.Warn("Got error trying to dispatch message", zap.Error(err)) | ||
} | ||
// TODO: handle errors with pluggable strategy | ||
consumer.MarkOffset(msg, "") // Mark message as processed | ||
return err | ||
} | ||
|
||
func (d *KafkaDispatcher) unsubscribe(channel provisioners.ChannelReference, sub subscription) error { | ||
|
@@ -224,7 +264,7 @@ func (d *KafkaDispatcher) setConfig(config *multichannelfanout.Config) { | |
d.config.Store(config) | ||
} | ||
|
||
func NewDispatcher(brokers []string, logger *zap.Logger) (*KafkaDispatcher, error) { | ||
func NewDispatcher(brokers []string, consumerMode cluster.ConsumerMode, logger *zap.Logger) (*KafkaDispatcher, error) { | ||
|
||
conf := sarama.NewConfig() | ||
conf.Version = sarama.V1_1_0_0 | ||
|
@@ -242,7 +282,7 @@ func NewDispatcher(brokers []string, logger *zap.Logger) (*KafkaDispatcher, erro | |
dispatcher := &KafkaDispatcher{ | ||
dispatcher: provisioners.NewMessageDispatcher(logger.Sugar()), | ||
|
||
kafkaCluster: &saramaCluster{kafkaBrokers: brokers}, | ||
kafkaCluster: &saramaCluster{kafkaBrokers: brokers, consumerMode: consumerMode}, | ||
kafkaConsumers: make(map[provisioners.ChannelReference]map[subscription]KafkaConsumer), | ||
kafkaAsyncProducer: producer, | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did you run
gofmt
on this file? I'd have expected it to put this import above next toflag
.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.
hmm I did run
gofmt -w main.go
, nothing changes, then I purposely put next toflag
and rerungofmt
it putsos
back after_ k8s.io/client-go/plugin/pkg/client/auth/gcp
, looks weird. I am usinggo version go1.11.5 darwin/amd64
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.
weird! I don't know why it does that, but I guess it's correct by definition. So 👍