ClusterMetadata

class kafka.cluster.ClusterMetadata(**configs)[source]

A class to manage kafka cluster metadata.

This class does not perform any IO. It simply updates internal state given API responses (MetadataResponse, FindCoordinatorResponse).

Keyword Arguments:
  • retry_backoff_ms (int) – Milliseconds to backoff when retrying on errors. Default: 100.

  • metadata_max_age_ms (int) – The period of time in milliseconds after which we force a refresh of metadata even if we haven’t seen any partition leadership changes to proactively discover any new brokers or partitions. Default: 300000

  • bootstrap_servers – ‘host[:port]’ string (or list of ‘host[:port]’ strings) that the client should contact to bootstrap initial cluster metadata. This does not have to be the full node list. It just needs to have at least one broker that will respond to a Metadata API Request. Default port is 9092. If no servers are specified, will default to localhost:9092.

add_coordinator(response, coord_type, coord_key)[source]

Update with metadata for a group or txn coordinator

Parameters:
  • response (FindCoordinatorResponse) – broker response

  • coord_type (str) – ‘group’ or ‘transaction’

  • coord_key (str) – consumer_group or transactional_id

Returns:

coordinator node_id if metadata is updated, None on error

Return type:

string

add_listener(listener)[source]

Add a callback function to be called on each metadata update

available_partitions_for_topic(topic)[source]

Return set of partitions with known leaders

Parameters:

topic (str) – topic to check for partitions

Returns:

{partition (int), …} None if topic not found.

Return type:

set

broker_metadata(broker_id)[source]

Get BrokerMetadata

Parameters:

broker_id (int or str) – node_id for a broker to check

Returns:

BrokerMetadata or None if not found

brokers()[source]

Get all BrokerMetadata

Returns:

{BrokerMetadata, …}

Return type:

set

coordinator_for_group(group)[source]

Return node_id of group coordinator.

Parameters:

group (str) – name of consumer group

Returns:

node_id (int or str) for group coordinator, -1 if coordinator unknown None if the group does not exist.

failed_update(exception)[source]

Update cluster state given a failed MetadataRequest.

leader_for_partition(partition)[source]

Return node_id of leader, -1 unavailable, None if unknown.

partitions_for_broker(broker_id)[source]

Return TopicPartitions for which the broker is a leader.

Parameters:

broker_id (int or str) – node id for a broker

Returns:

{TopicPartition, …} None if the broker either has no partitions or does not exist.

Return type:

set

partitions_for_topic(topic)[source]

Return set of all partitions for topic (whether available or not)

Parameters:

topic (str) – topic to check for partitions

Returns:

{partition (int), …} None if topic not found.

Return type:

set

refresh_backoff()[source]

Return milliseconds to wait before attempting to retry after failure

remove_listener(listener)[source]

Remove a previously added listener callback

request_update()[source]

Flags metadata for update, return Future()

Actual update must be handled separately. This method will only change the reported ttl()

Returns:

kafka.future.Future (value will be the cluster object after update)

topics(exclude_internal_topics=True)[source]

Get set of known topics.

Parameters:

exclude_internal_topics (bool) – Whether records from internal topics (such as offsets) should be exposed to the consumer. If set to True the only way to receive records from an internal topic is subscribing to it. Default True

Returns:

{topic (str), …}

Return type:

set

ttl()[source]

Milliseconds until metadata should be refreshed

update_metadata(metadata)[source]

Update cluster state given a MetadataResponse.

Parameters:

metadata (MetadataResponse) – broker response to a metadata request

Returns: None

with_partitions(partitions_to_add)[source]

Returns a copy of cluster metadata with partitions added