7
7
import time
8
8
9
9
import kafka .errors as Errors
10
+ from kafka .future import Future
10
11
from kafka .producer .buffer import SimpleBufferPool
11
12
from kafka .producer .future import FutureRecordMetadata , FutureProduceResult
12
13
from kafka .record .memory_records import MemoryRecordsBuilder
@@ -198,7 +199,7 @@ def __init__(self, **configs):
198
199
self ._drain_index = 0
199
200
200
201
def append (self , tp , timestamp_ms , key , value , headers , max_time_to_block_ms ,
201
- estimated_size = 0 ):
202
+ estimated_size = 0 , chain_future = None ):
202
203
"""Add a record to the accumulator, return the append result.
203
204
204
205
The append result will contain the future metadata, and flag for
@@ -213,12 +214,14 @@ def append(self, tp, timestamp_ms, key, value, headers, max_time_to_block_ms,
213
214
headers (List[Tuple[str, bytes]]): The header fields for the record
214
215
max_time_to_block_ms (int): The maximum time in milliseconds to
215
216
block for buffer memory to be available
216
-
217
+ chain_future (Future): chain future
217
218
Returns:
218
219
tuple: (future, batch_is_full, new_batch_created)
219
220
"""
220
221
assert isinstance (tp , TopicPartition ), 'not TopicPartition'
221
222
assert not self ._closed , 'RecordAccumulator is closed'
223
+ if chain_future is not None :
224
+ assert isinstance (chain_future , Future ), 'not Future'
222
225
# We keep track of the number of appending thread to make sure we do
223
226
# not miss batches in abortIncompleteBatches().
224
227
self ._appends_in_progress .increment ()
@@ -235,6 +238,8 @@ def append(self, tp, timestamp_ms, key, value, headers, max_time_to_block_ms,
235
238
last = dq [- 1 ]
236
239
future = last .try_append (timestamp_ms , key , value , headers )
237
240
if future is not None :
241
+ if chain_future :
242
+ future .chain (chain_future )
238
243
batch_is_full = len (dq ) > 1 or last .records .is_full ()
239
244
return future , batch_is_full , False
240
245
@@ -253,6 +258,8 @@ def append(self, tp, timestamp_ms, key, value, headers, max_time_to_block_ms,
253
258
# Somebody else found us a batch, return the one we
254
259
# waited for! Hopefully this doesn't happen often...
255
260
self ._free .deallocate (buf )
261
+ if chain_future :
262
+ future .chain (chain_future )
256
263
batch_is_full = len (dq ) > 1 or last .records .is_full ()
257
264
return future , batch_is_full , False
258
265
@@ -269,6 +276,8 @@ def append(self, tp, timestamp_ms, key, value, headers, max_time_to_block_ms,
269
276
270
277
dq .append (batch )
271
278
self ._incomplete .add (batch )
279
+ if chain_future :
280
+ future .chain (chain_future )
272
281
batch_is_full = len (dq ) > 1 or batch .records .is_full ()
273
282
return future , batch_is_full , True
274
283
finally :
0 commit comments