-
Notifications
You must be signed in to change notification settings - Fork 4
/
consumergroup.py
167 lines (143 loc) · 7.21 KB
/
consumergroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import struct
from typing import Any, Dict, List, Optional, cast
import pykafka
from esque.cluster import Cluster
from esque.errors import ConsumerGroupDoesNotExistException
from pykafka.protocol import OffsetFetchResponseV1, PartitionOffsetFetchRequest
from pykafka.protocol.admin import DescribeGroupResponse
# TODO: Refactor this shit hole
class ConsumerGroup:
def __init__(self, id: str, cluster: Cluster):
self.id = id
self.cluster = cluster
self._pykafka_group_coordinator_instance: Optional[pykafka.Broker] = None
self.topic_controller = cluster.topic_controller
@property
def _pykafka_group_coordinator(self) -> pykafka.Broker:
consumer_id = self.id.encode("UTF-8")
if not self._pykafka_group_coordinator_instance:
self._pykafka_group_coordinator_instance: pykafka.Broker = cast(
pykafka.Broker, self.cluster.pykafka_client.cluster.get_group_coordinator(consumer_id)
)
return self._pykafka_group_coordinator_instance
@property
def topics(self):
consumer_id = self.id.encode("UTF-8")
# Get Consumers who already have an offset
consumer_offsets = self._unpack_offset_response(
self._pykafka_group_coordinator.fetch_consumer_group_offsets(consumer_id, preqs=[])
)
topic_with_offsets = set(topic.decode("UTF-8") for topic in consumer_offsets.keys())
topics_with_members = set()
# Get Consumers which have a member
try:
resp = self._pykafka_group_coordinator.describe_groups([consumer_id])
meta = self._unpack_consumer_group_response(resp.groups[consumer_id])
topics_with_members = set(
member["member_metadata"]["subscription"][0].decode("UTF-8") for member in meta["members"]
)
except struct.error:
pass
return list(topic_with_offsets | topics_with_members)
def describe(self, *, verbose=False):
consumer_id = self.id.encode("UTF-8")
if consumer_id not in self._pykafka_group_coordinator.list_groups().groups:
raise ConsumerGroupDoesNotExistException(self.id)
resp = self._pykafka_group_coordinator.describe_groups([consumer_id])
assert len(resp.groups) == 1
meta = self._unpack_consumer_group_response(resp.groups[consumer_id])
consumer_offsets = self._get_consumer_offsets(consumer_id, verbose=verbose)
return {
"group_id": consumer_id,
"group_coordinator": self._pykafka_group_coordinator.host,
"offsets": consumer_offsets,
"meta": meta,
}
def _get_consumer_offsets(self, consumer_id, verbose: bool):
consumer_offsets = self._unpack_offset_response(
self._pykafka_group_coordinator.fetch_consumer_group_offsets(consumer_id, preqs=[])
)
if verbose:
for topic in consumer_offsets.keys():
topic_offsets = self.topic_controller.get_cluster_topic(topic).offsets
for partition_id, consumer_offset in consumer_offsets[topic].items():
consumer_offsets[topic][partition_id] = {
"consumer_offset": consumer_offset,
"topic_low_watermark": topic_offsets[partition_id].low,
"topic_high_watermark": topic_offsets[partition_id].high,
"consumer_lag": topic_offsets[partition_id].high - consumer_offset,
}
return consumer_offsets
for topic in consumer_offsets.keys():
topic_offsets = self.topic_controller.get_cluster_topic(topic).offsets
new_consumer_offsets = {
"consumer_offset": (float("inf"), float("-inf")),
"topic_low_watermark": (float("inf"), float("-inf")),
"topic_high_watermark": (float("inf"), float("-inf")),
"consumer_lag": (float("inf"), float("-inf")),
}
for partition_id, consumer_offset in consumer_offsets[topic].items():
current_offset = consumer_offset
old_min, old_max = new_consumer_offsets["consumer_offset"]
new_consumer_offsets["consumer_offset"] = (min(old_min, current_offset), max(old_max, current_offset))
old_min, old_max = new_consumer_offsets["topic_low_watermark"]
new_consumer_offsets["topic_low_watermark"] = (
min(old_min, topic_offsets[partition_id].low),
max(old_max, topic_offsets[partition_id].low),
)
old_min, old_max = new_consumer_offsets["topic_high_watermark"]
new_consumer_offsets["topic_high_watermark"] = (
min(old_min, topic_offsets[partition_id].high),
max(old_max, topic_offsets[partition_id].high),
)
old_min, old_max = new_consumer_offsets["consumer_lag"]
new_consumer_offsets["consumer_lag"] = (
min(old_min, topic_offsets[partition_id].high - current_offset),
max(old_max, topic_offsets[partition_id].high - current_offset),
)
return new_consumer_offsets
def _get_member_assignment(self, member_assignment: Dict[str, Any]) -> List[PartitionOffsetFetchRequest]:
"""
Creates a list of style [PartitionOffsetFetchRequest('topic', partition_id)]
"""
return [
PartitionOffsetFetchRequest(topic, partition)
for member in member_assignment
for topic, partitions in member["member_assignment"]["partition"].items()
for partition in partitions
]
def _unpack_offset_response(self, resp: OffsetFetchResponseV1) -> Dict[str, Any]:
return {
topic_name: {
partition_id: partition_data._asdict()["offset"] for partition_id, partition_data in partitions.items()
}
for topic_name, partitions in resp.topics.items()
}
def _unpack_consumer_group_response(self, resp: DescribeGroupResponse) -> Dict[str, Any]:
return {
"group_id": resp.group_id,
"protocol": resp.protocol,
"state": resp.state,
"error_code": resp.error_code,
"protocol_type": resp.protocol_type,
"members": [
{
"member_id": member.member_id,
"client_id": member.client_id,
"client_host": member.client_host,
"member_metadata": {
# "version": member.member_metadata.version,
"subscription": [topic for topic in member.member_metadata.topic_names],
# "client": member.member_metadata.user_data,
},
"member_assignment": {
# "version": member.member_assignment.version,
"partition": {
assignment[0]: assignment[1]
for assignment in member.member_assignment.partition_assignment
}
},
}
for _, member in resp.members.items()
],
}