Skip to content

KAFKA-3117: handle metadata updates during consumer rebalance #766

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

Merged
merged 1 commit into from
Jul 17, 2016
Merged
Show file tree
Hide file tree
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
34 changes: 24 additions & 10 deletions kafka/coordinator/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def __init__(self, client, subscription, metrics, metric_group_prefix,
assert self.config['assignors'], 'Coordinator requires assignors'

self._subscription = subscription
self._partitions_per_topic = {}
self._metadata_snapshot = {}
self._assignment_snapshot = None
self._cluster = client.cluster
self._cluster.request_update()
self._cluster.add_listener(WeakMethod(self._handle_metadata_update))
Expand Down Expand Up @@ -146,7 +147,7 @@ def _handle_metadata_update(self, cluster):

# check if there are any changes to the metadata which should trigger
# a rebalance
if self._subscription_metadata_changed():
if self._subscription_metadata_changed(cluster):

if (self.config['api_version'] >= (0, 9)
and self.config['group_id'] is not None):
Expand All @@ -159,20 +160,20 @@ def _handle_metadata_update(self, cluster):
self._subscription.assign_from_subscribed([
TopicPartition(topic, partition)
for topic in self._subscription.subscription
for partition in self._partitions_per_topic[topic]
for partition in self._metadata_snapshot[topic]
])

def _subscription_metadata_changed(self):
def _subscription_metadata_changed(self, cluster):
if not self._subscription.partitions_auto_assigned():
return False

old_partitions_per_topic = self._partitions_per_topic
self._partitions_per_topic = {}
metadata_snapshot = {}
for topic in self._subscription.group_subscription():
partitions = self._cluster.partitions_for_topic(topic) or []
self._partitions_per_topic[topic] = set(partitions)
partitions = cluster.partitions_for_topic(topic) or []
metadata_snapshot[topic] = set(partitions)

if self._partitions_per_topic != old_partitions_per_topic:
if self._metadata_snapshot != metadata_snapshot:
self._metadata_snapshot = metadata_snapshot
return True
return False

Expand All @@ -184,8 +185,15 @@ def _lookup_assignor(self, name):

def _on_join_complete(self, generation, member_id, protocol,
member_assignment_bytes):
# if we were the assignor, then we need to make sure that there have
# been no metadata updates since the rebalance begin. Otherwise, we
# won't rebalance again until the next metadata change
if self._assignment_snapshot and self._assignment_snapshot != self._metadata_snapshot:
self._subscription.mark_for_reassignment()
return

assignor = self._lookup_assignor(protocol)
assert assignor, 'invalid assignment protocol: %s' % protocol
assert assignor, 'Coordinator selected invalid assignment protocol: %s' % protocol

assignment = ConsumerProtocol.ASSIGNMENT.decode(member_assignment_bytes)

Expand Down Expand Up @@ -235,6 +243,11 @@ def _perform_assignment(self, leader_id, assignment_strategy, members):
self._subscription.group_subscribe(all_subscribed_topics)
self._client.set_topics(self._subscription.group_subscription())

# keep track of the metadata used for assignment so that we can check
# after rebalance completion whether anything has changed
self._cluster.request_update()
self._assignment_snapshot = self._metadata_snapshot

log.debug("Performing assignment for group %s using strategy %s"
" with subscriptions %s", self.group_id, assignor.name,
member_metadata)
Expand Down Expand Up @@ -264,6 +277,7 @@ def _on_join_prepare(self, generation, member_id):
" for group %s failed on_partitions_revoked",
self._subscription.listener, self.group_id)

self._assignment_snapshot = None
self._subscription.mark_for_reassignment()

def need_rejoin(self):
Expand Down
2 changes: 1 addition & 1 deletion test/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_pattern_subscription(coordinator, api_version):
coordinator.config['api_version'] = api_version
coordinator._subscription.subscribe(pattern='foo')
assert coordinator._subscription.subscription == set([])
assert coordinator._subscription_metadata_changed() is False
assert coordinator._subscription_metadata_changed({}) is False
assert coordinator._subscription.needs_partition_assignment is False

cluster = coordinator._client.cluster
Expand Down