1# Copyright 2022 Confluent Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from enum import Enum
16from typing import List, Optional
17
18from .. import cimpl
19from ..cimpl import TopicPartition
20
21
22class Node:
23 """
24 Represents node information.
25 Used by :class:`ConsumerGroupDescription`
26
27 Parameters
28 ----------
29 id: int
30 The node id of this node.
31 id_string:
32 String representation of the node id.
33 host:
34 The host name for this node.
35 port: int
36 The port for this node.
37 rack: str
38 The rack for this node.
39 """
40
41 def __init__(self, id: int, host: str, port: int, rack: Optional[str] = None) -> None:
42 self.id = id
43 self.id_string = str(id)
44 self.host = host
45 self.port = port
46 self.rack = rack
47
48 def __str__(self) -> str:
49 return f"({self.id}) {self.host}:{self.port} {f'(Rack - {self.rack})' if self.rack else ''}"
50
51
52class ConsumerGroupTopicPartitions:
53 """
54 Represents consumer group and its topic partition information.
55 Used by :meth:`AdminClient.list_consumer_group_offsets` and
56 :meth:`AdminClient.alter_consumer_group_offsets`.
57
58 Parameters
59 ----------
60 group_id: str
61 Id of the consumer group.
62 topic_partitions: list(TopicPartition)
63 List of topic partitions information.
64 """
65
66 def __init__(self, group_id: str, topic_partitions: Optional[List[TopicPartition]] = None) -> None:
67 self.group_id = group_id
68 self.topic_partitions = topic_partitions
69
70
71class ConsumerGroupState(Enum):
72 """
73 Enumerates the different types of Consumer Group State.
74
75 Note that the state :py:attr:`UNKOWN` (typo one) is deprecated and will be removed in
76 future major release. Use :py:attr:`UNKNOWN` instead.
77 """
78
79 #: State is not known or not set
80 UNKNOWN = cimpl.CONSUMER_GROUP_STATE_UNKNOWN
81 #: .. deprecated:: 2.3.0
82 #:
83 #: Use :py:attr:`UNKNOWN` instead.
84 UNKOWN = UNKNOWN
85 #: Preparing rebalance for the consumer group.
86 PREPARING_REBALANCING = cimpl.CONSUMER_GROUP_STATE_PREPARING_REBALANCE
87 #: Consumer Group is completing rebalancing.
88 COMPLETING_REBALANCING = cimpl.CONSUMER_GROUP_STATE_COMPLETING_REBALANCE
89 #: Consumer Group is stable.
90 STABLE = cimpl.CONSUMER_GROUP_STATE_STABLE
91 #: Consumer Group is dead.
92 DEAD = cimpl.CONSUMER_GROUP_STATE_DEAD
93 #: Consumer Group is empty.
94 EMPTY = cimpl.CONSUMER_GROUP_STATE_EMPTY
95
96 def __lt__(self, other: object) -> bool:
97 if not isinstance(other, ConsumerGroupState):
98 return NotImplemented
99 return self.value < other.value
100
101
102class ConsumerGroupType(Enum):
103 """
104 Enumerates the different types of Consumer Group Type.
105
106 Values:
107 -------
108 """
109
110 #: Type is not known or not set
111 UNKNOWN = cimpl.CONSUMER_GROUP_TYPE_UNKNOWN
112 #: Consumer Type
113 CONSUMER = cimpl.CONSUMER_GROUP_TYPE_CONSUMER
114 #: Classic Type
115 CLASSIC = cimpl.CONSUMER_GROUP_TYPE_CLASSIC
116
117 def __lt__(self, other: object) -> bool:
118 if not isinstance(other, ConsumerGroupType):
119 return NotImplemented
120 return self.value < other.value
121
122
123class TopicCollection:
124 """
125 Represents collection of topics in the form of different identifiers
126 for the topic.
127
128 Parameters
129 ----------
130 topic_names: list(str)
131 List of topic names.
132 """
133
134 def __init__(self, topic_names: List[str]) -> None:
135 self.topic_names = topic_names
136
137
138class TopicPartitionInfo:
139 """
140 Represents partition information.
141 Used by :class:`TopicDescription`.
142
143 Parameters
144 ----------
145 id : int
146 Id of the partition.
147 leader : Node
148 Leader broker for the partition.
149 replicas: list(Node)
150 Replica brokers for the partition.
151 isr: list(Node)
152 In-Sync-Replica brokers for the partition.
153 """
154
155 def __init__(self, id: int, leader: Node, replicas: List[Node], isr: List[Node]) -> None:
156 self.id = id
157 self.leader = leader
158 self.replicas = replicas
159 self.isr = isr
160
161
162class IsolationLevel(Enum):
163 """
164 Enum for Kafka isolation levels.
165
166 Values:
167 -------
168 """
169
170 READ_UNCOMMITTED = cimpl.ISOLATION_LEVEL_READ_UNCOMMITTED #: Receive all the offsets.
171 READ_COMMITTED = cimpl.ISOLATION_LEVEL_READ_COMMITTED #: Skip offsets belonging to an aborted transaction.
172
173 def __lt__(self, other: object) -> bool:
174 if not isinstance(other, IsolationLevel):
175 return NotImplemented
176 return self.value < other.value
177
178
179class ElectionType(Enum):
180 """
181 Enumerates the different types of leader elections.
182
183 Values:
184 -------
185 """
186
187 #: Preferred election
188 PREFERRED = cimpl.ELECTION_TYPE_PREFERRED
189 #: Unclean election
190 UNCLEAN = cimpl.ELECTION_TYPE_UNCLEAN
191
192 def __lt__(self, other: object) -> bool:
193 if not isinstance(other, ElectionType):
194 return NotImplemented
195 return self.value < other.value