File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,9 @@ def is_valid(self):
47
47
def match (self , batch ):
48
48
return self .producer_id == batch .producer_id and self .epoch == batch .producer_epoch
49
49
50
+ def __eq__ (self , other ):
51
+ return isinstance (other , ProducerIdAndEpoch ) and self .producer_id == other .producer_id and self .epoch == other .epoch
52
+
50
53
def __str__ (self ):
51
54
return "ProducerIdAndEpoch(producer_id={}, epoch={})" .format (self .producer_id , self .epoch )
52
55
@@ -304,7 +307,7 @@ def reset_producer_id(self):
304
307
it's best to return the produce error to the user and let them abort the transaction and close the producer explicitly.
305
308
"""
306
309
with self ._lock :
307
- if self .is_transactional :
310
+ if self .is_transactional () :
308
311
raise Errors .IllegalStateError (
309
312
"Cannot reset producer state for a transactional producer."
310
313
" You must either abort the ongoing transaction or"
Original file line number Diff line number Diff line change 7
7
import pytest
8
8
9
9
from kafka import KafkaProducer
10
+ from kafka .cluster import ClusterMetadata
11
+ from kafka .producer .transaction_manager import TransactionManager , ProducerIdAndEpoch
12
+
10
13
11
14
@pytest .mark .skipif (platform .python_implementation () != 'CPython' ,
12
15
reason = 'Test relies on CPython-specific gc policies' )
@@ -20,4 +23,17 @@ def test_kafka_producer_gc_cleanup():
20
23
assert threading .active_count () == threads
21
24
22
25
26
+ def test_idempotent_producer_reset_producer_id ():
27
+ transaction_manager = TransactionManager (
28
+ transactional_id = None ,
29
+ transaction_timeout_ms = 1000 ,
30
+ retry_backoff_ms = 100 ,
31
+ api_version = (0 , 11 ),
32
+ metadata = ClusterMetadata (),
33
+ )
23
34
35
+ test_producer_id_and_epoch = ProducerIdAndEpoch (123 , 456 )
36
+ transaction_manager .set_producer_id_and_epoch (test_producer_id_and_epoch )
37
+ assert transaction_manager .producer_id_and_epoch == test_producer_id_and_epoch
38
+ transaction_manager .reset_producer_id ()
39
+ assert transaction_manager .producer_id_and_epoch == ProducerIdAndEpoch (- 1 , - 1 )
You can’t perform that action at this time.
0 commit comments