@@ -141,7 +141,7 @@ def initialize_transactions(self):
141
141
self ._transition_to (TransactionState .INITIALIZING )
142
142
self .set_producer_id_and_epoch (ProducerIdAndEpoch (NO_PRODUCER_ID , NO_PRODUCER_EPOCH ))
143
143
self ._sequence_numbers .clear ()
144
- handler = InitProducerIdHandler (self , self .transactional_id , self . transaction_timeout_ms )
144
+ handler = InitProducerIdHandler (self , self .transaction_timeout_ms )
145
145
self ._enqueue_request (handler )
146
146
return handler .result
147
147
@@ -172,7 +172,7 @@ def begin_abort(self):
172
172
def _begin_completing_transaction (self , committed ):
173
173
if self ._new_partitions_in_transaction :
174
174
self ._enqueue_request (self ._add_partitions_to_transaction_handler ())
175
- handler = EndTxnHandler (self , self . transactional_id , self . producer_id_and_epoch , committed )
175
+ handler = EndTxnHandler (self , committed )
176
176
self ._enqueue_request (handler )
177
177
return handler .result
178
178
@@ -184,7 +184,7 @@ def send_offsets_to_transaction(self, offsets, consumer_group_id):
184
184
raise Errors .KafkaError ("Cannot send offsets to transaction because the producer is not in an active transaction" )
185
185
186
186
log .debug ("Begin adding offsets %s for consumer group %s to transaction" , offsets , consumer_group_id )
187
- handler = AddOffsetsToTxnHandler (self , self . transactional_id , self . producer_id_and_epoch , consumer_group_id , offsets )
187
+ handler = AddOffsetsToTxnHandler (self , consumer_group_id , offsets )
188
188
self ._enqueue_request (handler )
189
189
return handler .result
190
190
@@ -492,7 +492,7 @@ def _add_partitions_to_transaction_handler(self):
492
492
with self ._lock :
493
493
self ._pending_partitions_in_transaction .update (self ._new_partitions_in_transaction )
494
494
self ._new_partitions_in_transaction .clear ()
495
- return AddPartitionsToTxnHandler (self , self .transactional_id , self . producer_id_and_epoch , self . _pending_partitions_in_transaction )
495
+ return AddPartitionsToTxnHandler (self , self ._pending_partitions_in_transaction )
496
496
497
497
498
498
class TransactionalRequestResult (object ):
@@ -537,6 +537,18 @@ def __init__(self, transaction_manager, result=None):
537
537
self ._result = result or TransactionalRequestResult ()
538
538
self ._is_retry = False
539
539
540
+ @property
541
+ def transactional_id (self ):
542
+ return self .transaction_manager .transactional_id
543
+
544
+ @property
545
+ def producer_id (self ):
546
+ return self .transaction_manager .producer_id_and_epoch .producer_id
547
+
548
+ @property
549
+ def producer_epoch (self ):
550
+ return self .transaction_manager .producer_id_and_epoch .epoch
551
+
540
552
def fatal_error (self , exc ):
541
553
self .transaction_manager ._transition_to_fatal_error (exc )
542
554
self ._result .done (error = exc )
@@ -604,16 +616,15 @@ def priority(self):
604
616
605
617
606
618
class InitProducerIdHandler (TxnRequestHandler ):
607
- def __init__ (self , transaction_manager , transactional_id , transaction_timeout_ms ):
619
+ def __init__ (self , transaction_manager , transaction_timeout_ms ):
608
620
super (InitProducerIdHandler , self ).__init__ (transaction_manager )
609
621
610
- self .transactional_id = transactional_id
611
622
if transaction_manager ._api_version >= (2 , 0 ):
612
623
version = 1
613
624
else :
614
625
version = 0
615
626
self .request = InitProducerIdRequest [version ](
616
- transactional_id = transactional_id ,
627
+ transactional_id = self . transactional_id ,
617
628
transaction_timeout_ms = transaction_timeout_ms )
618
629
619
630
@property
@@ -638,10 +649,9 @@ def handle_response(self, response):
638
649
self .fatal_error (Errors .KafkaError ("Unexpected error in InitProducerIdResponse: %s" % (error ())))
639
650
640
651
class AddPartitionsToTxnHandler (TxnRequestHandler ):
641
- def __init__ (self , transaction_manager , transactional_id , producer_id_and_epoch , topic_partitions ):
652
+ def __init__ (self , transaction_manager , topic_partitions ):
642
653
super (AddPartitionsToTxnHandler , self ).__init__ (transaction_manager )
643
654
644
- self .transactional_id = transactional_id
645
655
if transaction_manager ._api_version >= (2 , 7 ):
646
656
version = 2
647
657
elif transaction_manager ._api_version >= (2 , 0 ):
@@ -652,9 +662,9 @@ def __init__(self, transaction_manager, transactional_id, producer_id_and_epoch,
652
662
for tp in topic_partitions :
653
663
topic_data [tp .topic ].append (tp .partition )
654
664
self .request = AddPartitionsToTxnRequest [version ](
655
- transactional_id = transactional_id ,
656
- producer_id = producer_id_and_epoch .producer_id ,
657
- producer_epoch = producer_id_and_epoch . epoch ,
665
+ transactional_id = self . transactional_id ,
666
+ producer_id = self .producer_id ,
667
+ producer_epoch = self . producer_epoch ,
658
668
topics = list (topic_data .items ()))
659
669
660
670
@property
@@ -790,20 +800,19 @@ def handle_response(self, response):
790
800
791
801
792
802
class EndTxnHandler (TxnRequestHandler ):
793
- def __init__ (self , transaction_manager , transactional_id , producer_id_and_epoch , committed ):
803
+ def __init__ (self , transaction_manager , committed ):
794
804
super (EndTxnHandler , self ).__init__ (transaction_manager )
795
805
796
- self .transactional_id = transactional_id
797
806
if self .transaction_manager ._api_version >= (2 , 7 ):
798
807
version = 2
799
808
elif self .transaction_manager ._api_version >= (2 , 0 ):
800
809
version = 1
801
810
else :
802
811
version = 0
803
812
self .request = EndTxnRequest [version ](
804
- transactional_id = transactional_id ,
805
- producer_id = producer_id_and_epoch .producer_id ,
806
- producer_epoch = producer_id_and_epoch . epoch ,
813
+ transactional_id = self . transactional_id ,
814
+ producer_id = self .producer_id ,
815
+ producer_epoch = self . producer_epoch ,
807
816
committed = committed )
808
817
809
818
@property
@@ -832,11 +841,9 @@ def handle_response(self, response):
832
841
833
842
834
843
class AddOffsetsToTxnHandler (TxnRequestHandler ):
835
- def __init__ (self , transaction_manager , transactional_id , producer_id_and_epoch , consumer_group_id , offsets ):
844
+ def __init__ (self , transaction_manager , consumer_group_id , offsets ):
836
845
super (AddOffsetsToTxnHandler , self ).__init__ (transaction_manager )
837
846
838
- self .transactional_id = transactional_id
839
- self .producer_id_and_epoch = producer_id_and_epoch
840
847
self .consumer_group_id = consumer_group_id
841
848
self .offsets = offsets
842
849
if self .transaction_manager ._api_version >= (2 , 7 ):
@@ -846,9 +853,9 @@ def __init__(self, transaction_manager, transactional_id, producer_id_and_epoch,
846
853
else :
847
854
version = 0
848
855
self .request = AddOffsetsToTxnRequest [version ](
849
- transactional_id = transactional_id ,
850
- producer_id = producer_id_and_epoch .producer_id ,
851
- producer_epoch = producer_id_and_epoch . epoch ,
856
+ transactional_id = self . transactional_id ,
857
+ producer_id = self .producer_id ,
858
+ producer_epoch = self . producer_epoch ,
852
859
group_id = consumer_group_id )
853
860
854
861
@property
@@ -864,8 +871,8 @@ def handle_response(self, response):
864
871
# note the result is not completed until the TxnOffsetCommit returns
865
872
for tp , offset in six .iteritems (self .offsets ):
866
873
self .transaction_manager ._pending_txn_offset_commits [tp ] = offset
867
- handler = TxnOffsetCommitHandler (self .transaction_manager , self .transactional_id , self . producer_id_and_epoch ,
868
- self .consumer_group_id , self . transaction_manager ._pending_txn_offset_commits , self ._result )
874
+ handler = TxnOffsetCommitHandler (self .transaction_manager , self .consumer_group_id ,
875
+ self .transaction_manager ._pending_txn_offset_commits , self ._result )
869
876
self .transaction_manager ._enqueue_request (handler )
870
877
self .transaction_manager ._transaction_started = True
871
878
elif error in (Errors .CoordinatorNotAvailableError , Errors .NotCoordinatorError ):
@@ -884,12 +891,10 @@ def handle_response(self, response):
884
891
885
892
886
893
class TxnOffsetCommitHandler (TxnRequestHandler ):
887
- def __init__ (self , transaction_manager , transactional_id , producer_id_and_epoch , consumer_group_id , offsets , result ):
894
+ def __init__ (self , transaction_manager , consumer_group_id , offsets , result ):
888
895
super (TxnOffsetCommitHandler , self ).__init__ (transaction_manager , result = result )
889
896
890
- self .transactional_id = transactional_id
891
897
self .consumer_group_id = consumer_group_id
892
- self .producer_id_and_epoch = producer_id_and_epoch
893
898
self .offsets = offsets
894
899
self .request = self ._build_request ()
895
900
@@ -912,8 +917,8 @@ def _build_request(self):
912
917
return TxnOffsetCommitRequest [version ](
913
918
transactional_id = self .transactional_id ,
914
919
group_id = self .consumer_group_id ,
915
- producer_id = self .producer_id_and_epoch . producer_id ,
916
- producer_epoch = self .producer_id_and_epoch . epoch ,
920
+ producer_id = self .producer_id ,
921
+ producer_epoch = self .producer_epoch ,
917
922
topics = list (topic_data .items ()))
918
923
919
924
@property
0 commit comments