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