-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
consumer: bugfix for broker workers getting stuck #369
Changes from all commits
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 |
---|---|---|
|
@@ -115,10 +115,10 @@ func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) | |
return nil, err | ||
} | ||
|
||
if leader, err := c.client.Leader(child.topic, child.partition); err != nil { | ||
var leader *Broker | ||
var err error | ||
if leader, err = c.client.Leader(child.topic, child.partition); err != nil { | ||
return nil, err | ||
} else { | ||
child.broker = leader | ||
} | ||
|
||
if err := c.addChild(child); err != nil { | ||
|
@@ -127,8 +127,8 @@ func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) | |
|
||
go withRecover(child.dispatcher) | ||
|
||
brokerWorker := c.refBrokerConsumer(child.broker) | ||
brokerWorker.input <- child | ||
child.broker = c.refBrokerConsumer(leader) | ||
child.broker.input <- child | ||
|
||
return child, nil | ||
} | ||
|
@@ -171,31 +171,39 @@ func (c *consumer) refBrokerConsumer(broker *Broker) *brokerConsumer { | |
newSubscriptions: make(chan []*partitionConsumer), | ||
wait: make(chan none), | ||
subscriptions: make(map[*partitionConsumer]none), | ||
refs: 1, | ||
refs: 0, | ||
} | ||
go withRecover(brokerWorker.subscriptionManager) | ||
go withRecover(brokerWorker.subscriptionConsumer) | ||
c.brokerConsumers[broker] = brokerWorker | ||
} else { | ||
brokerWorker.refs++ | ||
} | ||
|
||
brokerWorker.refs++ | ||
|
||
return brokerWorker | ||
} | ||
|
||
func (c *consumer) unrefBrokerConsumer(broker *Broker) { | ||
func (c *consumer) unrefBrokerConsumer(brokerWorker *brokerConsumer) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
brokerWorker := c.brokerConsumers[broker] | ||
brokerWorker.refs-- | ||
|
||
if brokerWorker.refs == 0 { | ||
close(brokerWorker.input) | ||
delete(c.brokerConsumers, broker) | ||
if c.brokerConsumers[brokerWorker.broker] == brokerWorker { | ||
delete(c.brokerConsumers, brokerWorker.broker) | ||
} | ||
} | ||
} | ||
|
||
func (c *consumer) abandonBrokerConsumer(brokerWorker *brokerConsumer) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
delete(c.brokerConsumers, brokerWorker.broker) | ||
} | ||
|
||
// PartitionConsumer | ||
|
||
// PartitionConsumer processes Kafka messages from a given topic and partition. You MUST call Close() | ||
|
@@ -237,7 +245,7 @@ type partitionConsumer struct { | |
topic string | ||
partition int32 | ||
|
||
broker *Broker | ||
broker *brokerConsumer | ||
messages chan *ConsumerMessage | ||
errors chan *ConsumerError | ||
trigger, dying chan none | ||
|
@@ -291,15 +299,15 @@ func (child *partitionConsumer) dispatch() error { | |
return err | ||
} | ||
|
||
if leader, err := child.consumer.client.Leader(child.topic, child.partition); err != nil { | ||
var leader *Broker | ||
var err error | ||
if leader, err = child.consumer.client.Leader(child.topic, child.partition); err != nil { | ||
return err | ||
} else { | ||
child.broker = leader | ||
} | ||
|
||
brokerWorker := child.consumer.refBrokerConsumer(child.broker) | ||
child.broker = child.consumer.refBrokerConsumer(leader) | ||
|
||
brokerWorker.input <- child | ||
child.broker.input <- child | ||
|
||
return nil | ||
} | ||
|
@@ -463,6 +471,7 @@ func (w *brokerConsumer) updateSubscriptionCache(newSubscriptions []*partitionCo | |
} | ||
|
||
func (w *brokerConsumer) abort(err error) { | ||
w.consumer.abandonBrokerConsumer(w) | ||
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. Another clarification: because we only have one goroutine dealing with this broker, there is no chance of us using the broker's connection between the occurrence of the error, and this call where we abandon 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. Technically the subscriptionManager is a separate goroutine, so there are two, but effectively yes. |
||
_ = w.broker.Close() // we don't care about the error this might return, we already have one | ||
|
||
for child := range w.subscriptions { | ||
|
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.
So if I understand it correctly, this
if
case only triggers during a normal consumer shutdown? When we shut down because of a broker error, thisdelete
will already have happened?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.
It triggers when all PartitionConsumers move off of the brokerConsumer "safely", either via being closed or via a
NotLeaderForPartition
response. When the broker blows up we abort, and the delete happens immediately to prevent new partitions from taking a reference.