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