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

Backport #45

Merged
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
49 changes: 34 additions & 15 deletions pkg/channel/consolidated/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import (
"go.opencensus.io/trace"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"
"knative.dev/eventing-kafka/pkg/channel/consolidated/utils"
"knative.dev/eventing-kafka/pkg/channel/distributed/common/env"
"knative.dev/eventing-kafka/pkg/common/consumer"
eventingchannels "knative.dev/eventing/pkg/channel"
"knative.dev/eventing/pkg/channel/fanout"
"knative.dev/eventing/pkg/kncloudevents"
"knative.dev/pkg/kmeta"

"knative.dev/eventing-kafka/pkg/channel/consolidated/utils"
"knative.dev/eventing-kafka/pkg/channel/distributed/common/env"
"knative.dev/eventing-kafka/pkg/common/consumer"
)

type KafkaDispatcher struct {
Expand Down Expand Up @@ -104,6 +105,12 @@ func NewDispatcher(ctx context.Context, args *KafkaDispatcherArgs) (*KafkaDispat
return nil, fmt.Errorf("unable to create kafka producer against Kafka bootstrap servers %v : %v", args.Brokers, err)
}

// Configured with the same connection arguments of IMC
kncloudevents.ConfigureConnectionArgs(&kncloudevents.ConnectionArgs{
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 100,
})

dispatcher := &KafkaDispatcher{
dispatcher: eventingchannels.NewMessageDispatcher(args.Logger.Desugar()),
kafkaConsumerFactory: consumer.NewConsumerGroupFactory(args.Brokers, conf),
Expand Down Expand Up @@ -268,26 +275,38 @@ func (d *KafkaDispatcher) UpdateKafkaConsumers(config *Config) (map[types.UID]er
d.logger.Debug("Number of subs failed to subscribe", zap.Any("subs", len(failedToSubscribe)))

// Unsubscribe and close consumer for any deleted subscriptions
for channelRef, subs := range d.channelSubscriptions {
for _, oldSub := range subs {
removedSub := true
for _, s := range newSubs {
if s == oldSub {
removedSub = false
}
}
subsToRemove := make(map[eventingchannels.ChannelReference][]types.UID)
for channelRef, actualSubs := range d.channelSubscriptions {
subsToRemove[channelRef] = uidSetDifference(actualSubs, newSubs)
}

if removedSub {
if err := d.unsubscribe(channelRef, d.subscriptions[oldSub]); err != nil {
return nil, err
}
for channelRef, subs := range subsToRemove {
for _, s := range subs {
if err := d.unsubscribe(channelRef, d.subscriptions[s]); err != nil {
return nil, err
}
}
d.channelSubscriptions[channelRef] = newSubs
}

return failedToSubscribe, nil
}

func uidSetDifference(a, b []types.UID) (diff []types.UID) {
m := make(map[types.UID]bool)

for _, item := range b {
m[item] = true
}

for _, item := range a {
if _, ok := m[item]; !ok {
diff = append(diff, item)
}
}
return
}

// UpdateHostToChannelMap will be called by new CRD based kafka channel dispatcher controller.
func (d *KafkaDispatcher) UpdateHostToChannelMap(config *Config) error {
if config == nil {
Expand Down