-
Notifications
You must be signed in to change notification settings - Fork 956
Expand file tree
/
Copy path_AIOProducer.py
More file actions
345 lines (278 loc) · 13.5 KB
/
Copy path_AIOProducer.py
File metadata and controls
345 lines (278 loc) · 13.5 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright 2025 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import concurrent.futures
import logging
from typing import Any, Callable, Dict, Optional
import confluent_kafka
from .. import _common as _common
from ._producer_batch_processor import ProducerBatchManager
from ._kafka_batch_executor import ProducerBatchExecutor
from ._buffer_timeout_manager import BufferTimeoutManager
logger = logging.getLogger(__name__)
class AIOProducer:
# ========================================================================
# INITIALIZATION AND LIFECYCLE MANAGEMENT
# ========================================================================
def __init__(
self,
producer_conf: Dict[str, Any],
max_workers: int = 4,
executor: Optional[concurrent.futures.Executor] = None,
batch_size: int = 1000,
buffer_timeout: float = 1.0
) -> None:
if executor is not None:
self.executor = executor
else:
self.executor = concurrent.futures.ThreadPoolExecutor(
max_workers=max_workers)
# Store the event loop for async operations
self._loop = asyncio.get_running_loop()
wrap_common_callbacks = _common.wrap_common_callbacks
wrap_common_callbacks(self._loop, producer_conf)
self._producer: confluent_kafka.Producer = confluent_kafka.Producer(
producer_conf
)
# Batching configuration
self._batch_size: int = batch_size
# Producer state management
self._is_closed: bool = False # Track if producer is closed
# Initialize Kafka batch executor for handling Kafka operations
self._kafka_executor = ProducerBatchExecutor(
self._producer, self.executor
)
# Initialize batch processor for message batching and processing
self._batch_processor = ProducerBatchManager(self._kafka_executor)
# Initialize buffer timeout manager for timeout handling
self._buffer_timeout_manager = BufferTimeoutManager(
self._batch_processor, self._kafka_executor, buffer_timeout
)
if buffer_timeout > 0:
self._buffer_timeout_manager.start_timeout_monitoring()
async def close(self) -> None:
"""Close the producer and cleanup resources
This method performs a graceful shutdown sequence to ensure all resources
are properly cleaned up and no messages are lost:
1. **Signal Shutdown**: Sets the closed flag to signal the timeout task to stop
2. **Cancel Timeout Task**: Immediately cancels the buffer timeout monitoring task
3. **Flush All Messages**: Flushes any buffered messages and waits for delivery confirmation
4. **Shutdown ThreadPool**: Waits for all pending ThreadPool operations to complete
5. **Cleanup**: Ensures the underlying librdkafka producer is properly closed. The shutdown
is designed to be safe and non-blocking for the asyncio event loop
while ensuring all pending operations complete before the producer is closed.
Raises:
Exception: May raise exceptions from buffer flushing, but these are logged
and don't prevent the cleanup process from completing.
"""
# Set closed flag to signal timeout task to stop
self._is_closed = True
# Stop the buffer timeout monitoring task
self._buffer_timeout_manager.stop_timeout_monitoring()
# Flush any remaining messages
try:
await self.flush()
except Exception:
logger.error("Error flushing messages during close", exc_info=True)
raise
# Shutdown the ThreadPool executor and wait for any remaining tasks to complete
# This ensures that all pending poll(), flush(), and other blocking operations
# finish before the producer is considered fully closed
if hasattr(self, 'executor'):
# executor.shutdown(wait=True) is a blocking call that:
# - Prevents new tasks from being submitted to the ThreadPool
# - Waits for all currently executing and queued tasks to complete
# - Returns only when all worker threads have finished
#
# We run this in a separate thread (using None as executor) to avoid
# blocking the asyncio event loop during the potentially long shutdown wait
await asyncio.get_running_loop().run_in_executor(
None, self.executor.shutdown, True
)
def __del__(self) -> None:
"""Cleanup method called during garbage collection
This ensures that the timeout task is properly cancelled even if
close() wasn't explicitly called.
"""
if hasattr(self, '_is_closed'):
self._is_closed = True
if hasattr(self, '_buffer_timeout_manager'):
self._buffer_timeout_manager.stop_timeout_monitoring()
# ========================================================================
# CORE PRODUCER OPERATIONS - Main public API
# ========================================================================
async def poll(
self,
timeout: float = 0,
*args: Any,
**kwargs: Any
) -> int:
"""Processes delivery callbacks from librdkafka - blocking depends on timeout
This method triggers any pending delivery reports that have been
queued by librdkafka when messages are delivered or fail to deliver.
Args:
timeout: Timeout in seconds for waiting for callbacks:
- 0 = non-blocking, return after processing available callbacks
- >0 = block up to timeout seconds waiting for new callbacks
- -1 = block indefinitely until callbacks are available
Returns:
Number of callbacks processed during this call
"""
return await self._call(self._producer.poll, timeout, *args, **kwargs)
async def produce(
self,
topic: str,
value: Optional[Any] = None,
key: Optional[Any] = None,
*args: Any,
**kwargs: Any
) -> asyncio.Future[Any]:
"""Batched produce: Accumulates messages in buffer and flushes when threshold reached
Args:
topic: Kafka topic name (required)
value: Message payload (optional)
key: Message key (optional)
*args, **kwargs: Additional parameters like partition, timestamp, headers
Returns:
asyncio.Future: Future that resolves to the delivered message or raises exception on failure
"""
result = asyncio.get_running_loop().create_future()
msg_data = {
'topic': topic,
'value': value,
'key': key
}
# Add optional parameters to message data
if 'partition' in kwargs:
msg_data['partition'] = kwargs['partition']
if 'timestamp' in kwargs:
msg_data['timestamp'] = kwargs['timestamp']
if 'headers' in kwargs:
# Headers are not supported in batch mode due to librdkafka API limitations.
# Use individual synchronous produce() calls if headers are required.
raise NotImplementedError(
"Headers are not supported in AIOProducer batch mode. "
"Use the synchronous Producer.produce() method if headers are required."
)
self._batch_processor.add_message(msg_data, result)
self._buffer_timeout_manager.mark_activity()
# Check if we should flush the buffer
if self._batch_processor.get_buffer_size() >= self._batch_size:
await self._flush_buffer()
return result
async def flush(self, *args: Any, **kwargs: Any) -> Any:
"""Waits until all messages are delivered or timeout
This method performs a complete flush:
1. Flushes any buffered messages from local buffer to librdkafka
2. Waits for librdkafka to deliver/acknowledge all messages
"""
# First, flush any remaining messages in the buffer for all topics
if not self._batch_processor.is_buffer_empty():
await self._flush_buffer()
# Update buffer activity since we just flushed
self._buffer_timeout_manager.mark_activity()
# Then flush underlying producer and wait for delivery confirmation
return await self._call(self._producer.flush, *args, **kwargs)
async def purge(self, *args: Any, **kwargs: Any) -> Any:
"""Purges messages from internal queues - may block during cleanup"""
# Cancel all pending futures
self._batch_processor.cancel_pending_futures()
# Clear local message buffer and futures
self._batch_processor.clear_buffer()
# Update buffer activity since we cleared the buffer
self._buffer_timeout_manager.mark_activity()
return await self._call(self._producer.purge, *args, **kwargs)
async def list_topics(self, *args: Any, **kwargs: Any) -> Any:
return await self._call(self._producer.list_topics, *args, **kwargs)
# ========================================================================
# TRANSACTION OPERATIONS - Kafka transaction support
# ========================================================================
async def init_transactions(self, *args: Any, **kwargs: Any) -> Any:
"""Network call to initialize transactions"""
return await self._call(
self._producer.init_transactions, *args, **kwargs
)
async def begin_transaction(self, *args: Any, **kwargs: Any) -> Any:
"""Network call to begin transaction"""
# Flush messages to set a clean state before entering a transaction
await self.flush()
return await self._call(
self._producer.begin_transaction, *args, **kwargs
)
async def send_offsets_to_transaction(
self,
*args: Any,
**kwargs: Any
) -> Any:
"""Network call to send offsets to transaction"""
return await self._call(
self._producer.send_offsets_to_transaction, *args, **kwargs
)
async def commit_transaction(self, *args: Any, **kwargs: Any) -> Any:
"""Commit transaction after flushing all buffered messages"""
# Flush to ensure messages in the local batch_processor buffer are
# delivered to librdkafka
await self.flush()
# Then commit transaction
return await self._call(
self._producer.commit_transaction, *args, **kwargs
)
async def abort_transaction(self, *args: Any, **kwargs: Any) -> Any:
"""Network call to abort transaction
Messages produced before the call (i.e. inside the transaction boundary) will be aborted.
Messages that are still in flight may be failed by librdkafka as they are considered
outside the transaction boundary.
Refer to librdkafka documentation section "Transactional producer API"
for more details:
https://github.com/confluentinc/librdkafka/blob/master/INTRODUCTION.md#transactional-producer
"""
# Flush to ensure messages in the local batch_processor buffer are
# delivered to librdkafka
await self.flush()
return await self._call(
self._producer.abort_transaction, *args, **kwargs
)
# ========================================================================
# AUTHENTICATION AND SECURITY
# ========================================================================
async def set_sasl_credentials(self, *args: Any, **kwargs: Any) -> Any:
"""Authentication operation that may involve network calls"""
return await self._call(
self._producer.set_sasl_credentials, *args, **kwargs
)
# ========================================================================
# BATCH PROCESSING OPERATIONS - Delegated to BatchProcessor
# ========================================================================
async def _flush_buffer(self, target_topic: Optional[str] = None) -> None:
"""Flush the current message buffer using clean batch processing flow
This method demonstrates the new architecture where AIOProducer simply
orchestrates the workflow between components:
1. BatchProcessor creates immutable MessageBatch objects
2. ProducerBatchExecutor executes each batch
3. BufferTimeoutManager handles activity tracking
"""
await self._batch_processor.flush_buffer(target_topic)
# ========================================================================
# UTILITY METHODS - Helper functions and internal utilities
# ========================================================================
async def _call(
self,
blocking_task: Callable[..., Any],
*args: Any,
**kwargs: Any
) -> Any:
"""Helper method for blocking operations that need ThreadPool execution"""
return await _common.async_call(
self.executor, blocking_task, *args, **kwargs
)