Skip to content
This repository was archived by the owner on Sep 23, 2024. It is now read-only.

Commit 400fde8

Browse files
committed
Refactored list_topics() to return specific types rather than dict
Added tests for list_topics() This also removes the rich broker objects at each reference point (such as leader, isrs, etc), since it is possible that the broker does not exist. Instead we ask the user to check that the broker id exists in ClusterMetadata.brokers before referencing.
1 parent 0a67983 commit 400fde8

File tree

13 files changed

+689
-406
lines changed

13 files changed

+689
-406
lines changed

confluent_kafka/__init__.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,105 @@ def alter_configs(self, resources, **kwargs):
201201
if not f.set_running_or_notify_cancel():
202202
raise RuntimeError("Future was cancelled prematurely")
203203
return super(AdminClient, self).alter_configs(resources, f, **kwargs)
204+
205+
206+
class ClusterMetadata (object):
207+
"""
208+
ClusterMetadata as returned by list_topics() contains information
209+
about the Kafka cluster, brokers, and topics.
210+
211+
:ivar cluster_id: Cluster id string, if supported by broker, else None.
212+
:ivar controller_id: Current controller broker id, or -1.
213+
:ivar brokers: Map of brokers indexed by the int broker id. Value is BrokerMetadata object.
214+
:ivar topics: Map of topics indexed by the topic name. Value is TopicMetadata object.
215+
:ivar orig_broker_id: The broker this metadata originated from.
216+
:ivar orig_broker_name: Broker name/address this metadata originated from.
217+
"""
218+
def __init__(self):
219+
self.cluster_id = None
220+
self.controller_id = -1
221+
self.brokers = {}
222+
self.topics = {}
223+
self.orig_broker_id = -1
224+
self.orig_broker_name = None
225+
226+
def __repr__(self):
227+
return "ClusterMetadata({})".format(self.cluster_id)
228+
229+
def __str__(self):
230+
return str(self.cluster_id)
231+
232+
233+
class BrokerMetadata (object):
234+
"""
235+
BrokerMetadata contains information about a Kafka broker.
236+
237+
:ivar id: Broker id.
238+
:ivar host: Broker hostname.
239+
:ivar port: Broker port.
240+
"""
241+
def __init__(self):
242+
self.id = -1
243+
self.host = None
244+
self.port = -1
245+
246+
def __repr__(self):
247+
return "BrokerMetadata({}, {}:{})".format(self.id, self.host, self.port)
248+
249+
def __str__(self):
250+
return "{}:{}/{}".format(self.host, self.port, self.id)
251+
252+
253+
class TopicMetadata (object):
254+
"""
255+
TopicMetadata contains information about a Kafka topic.
256+
257+
:ivar topic: Topic name.
258+
:ivar partitions: Map of partitions indexed by partition id. Value is PartitionMetadata object.
259+
:ivar error: Topic error, or None. Value is a KafkaError object.
260+
"""
261+
def __init__(self):
262+
self.topic = None
263+
self.partitions = {}
264+
self.error = None
265+
266+
def __repr__(self):
267+
if self.error is not None:
268+
return "TopicMetadata({}, {} partitions, {})".format(self.topic, len(self.partitions), self.error)
269+
else:
270+
return "TopicMetadata({}, {} partitions)".format(self.topic, len(self.partitions))
271+
272+
def __str__(self):
273+
return self.topic
274+
275+
276+
class PartitionMetadata (object):
277+
"""
278+
PartitionsMetadata contains information about a Kafka partition.
279+
280+
:ivar id: Partition id.
281+
:ivar leader: Current leader broker for this partition, or -1.
282+
:ivar replicas: List of replica broker ids for this partition.
283+
:ivar isrs: List of in-sync-replica broker ids for this partition.
284+
:ivar error: Partition error, or None. Value is a KafkaError object.
285+
286+
:warning: Depending on cluster state the broker ids referenced in
287+
leader, replicas and isrs may temporarily not be reported
288+
in ClusterMetadata.brokers. Always check the availability
289+
of a broker id in the brokers dict.
290+
"""
291+
def __init__(self):
292+
self.partition = -1
293+
self.leader = -1
294+
self.replicas = []
295+
self.isrs = []
296+
self.error = None
297+
298+
def __repr__(self):
299+
if self.error is not None:
300+
return "PartitionMetadata({}, {})".format(self.partition, self.error)
301+
else:
302+
return "PartitionMetadata({})".format(self.partition)
303+
304+
def __str__(self):
305+
return "{}".format(self.partition)

confluent_kafka/src/Admin.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,11 @@ static PyMethodDef Admin_methods[] = {
10891089
" :rtype: int\n"
10901090
"\n"
10911091
},
1092+
1093+
{ "list_topics", (PyCFunction)list_topics, METH_VARARGS|METH_KEYWORDS,
1094+
list_topics_doc
1095+
},
1096+
10921097
{ NULL }
10931098
};
10941099

confluent_kafka/src/Consumer.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,14 +1265,9 @@ static PyMethodDef Consumer_methods[] = {
12651265
" :raises: RuntimeError if called on a closed consumer\n"
12661266
"\n"
12671267
},
1268-
1269-
{
1270-
"list_topics",
1271-
(PyCFunction)get_metadata,
1272-
METH_VARARGS|METH_KEYWORDS,
1273-
get_metadata_doc
1274-
},
1275-
1268+
{ "list_topics", (PyCFunction)list_topics, METH_VARARGS|METH_KEYWORDS,
1269+
list_topics_doc
1270+
},
12761271

12771272
{ NULL }
12781273
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy