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

Use dedicated connection for group coordinator #1822

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 14 additions & 22 deletions kafka/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def __init__(self, **configs):
self.config[key] = configs[key]

self._bootstrap_brokers = self._generate_bootstrap_brokers()
self._coordinator_brokers = {}

def _generate_bootstrap_brokers(self):
# collect_hosts does not perform DNS, so we should be fine to re-use
Expand Down Expand Up @@ -96,7 +97,11 @@ def broker_metadata(self, broker_id):
Returns:
BrokerMetadata or None if not found
"""
return self._brokers.get(broker_id) or self._bootstrap_brokers.get(broker_id)
return (
self._brokers.get(broker_id) or
self._bootstrap_brokers.get(broker_id) or
self._coordinator_brokers.get(broker_id)
)

def partitions_for_topic(self, topic):
"""Return set of all partitions for topic (whether available or not)
Expand Down Expand Up @@ -341,41 +346,28 @@ def add_group_coordinator(self, group, response):
response (GroupCoordinatorResponse): broker response

Returns:
bool: True if metadata is updated, False on error
string: coordinator node_id if metadata is updated, None on error
"""
log.debug("Updating coordinator for %s: %s", group, response)
error_type = Errors.for_code(response.error_code)
if error_type is not Errors.NoError:
log.error("GroupCoordinatorResponse error: %s", error_type)
self._groups[group] = -1
return False
return

node_id = response.coordinator_id
# Use a coordinator-specific node id so that group requuests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: requuests

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uu are the best

# get a dedicated connection
node_id = 'coordinator-{}'.format(response.coordinator_id)
coordinator = BrokerMetadata(
response.coordinator_id,
node_id,
response.host,
response.port,
None)

# Assume that group coordinators are just brokers
# (this is true now, but could diverge in future)
if node_id not in self._brokers:
self._brokers[node_id] = coordinator

# If this happens, either brokers have moved without
# changing IDs, or our assumption above is wrong
else:
node = self._brokers[node_id]
if coordinator.host != node.host or coordinator.port != node.port:
log.error("GroupCoordinator metadata conflicts with existing"
" broker metadata. Coordinator: %s, Broker: %s",
coordinator, node)
self._groups[group] = node_id
return False

log.info("Group coordinator for %s is %s", group, coordinator)
self._coordinator_brokers[node_id] = coordinator
self._groups[group] = node_id
return True
return node_id

def with_partitions(self, partitions_to_add):
"""Returns a copy of cluster metadata with partitions added"""
Expand Down
6 changes: 3 additions & 3 deletions kafka/coordinator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,14 +676,14 @@ def _handle_group_coordinator_response(self, future, response):
error_type = Errors.for_code(response.error_code)
if error_type is Errors.NoError:
with self._client._lock, self._lock:
ok = self._client.cluster.add_group_coordinator(self.group_id, response)
if not ok:
coordinator_id = self._client.cluster.add_group_coordinator(self.group_id, response)
if not coordinator_id:
# This could happen if coordinator metadata is different
# than broker metadata
future.failure(Errors.IllegalStateError())
return

self.coordinator_id = response.coordinator_id
self.coordinator_id = coordinator_id
log.info("Discovered coordinator %s for group %s",
self.coordinator_id, self.group_id)
self._client.maybe_connect(self.coordinator_id)
Expand Down