@@ -99,19 +99,12 @@ def __init__(self, client, subscription, metrics, metric_group_prefix,
99
99
else :
100
100
interval = self .config ['auto_commit_interval_ms' ] / 1000.0
101
101
self ._auto_commit_task = AutoCommitTask (weakref .proxy (self ), interval )
102
-
103
- # When using broker-coordinated consumer groups, auto-commit will
104
- # be automatically enabled on group join (see _on_join_complete)
105
- # Otherwise, we should enable now b/c there will be no group join
106
- if self .config ['api_version' ] < (0 , 9 ):
107
- self ._auto_commit_task .enable ()
102
+ self ._auto_commit_task .reschedule ()
108
103
109
104
self ._sensors = ConsumerCoordinatorMetrics (metrics , metric_group_prefix ,
110
105
self ._subscription )
111
106
112
107
def __del__ (self ):
113
- if hasattr (self , '_auto_commit_task' ) and self ._auto_commit_task :
114
- self ._auto_commit_task .disable ()
115
108
if hasattr (self , '_cluster' ) and self ._cluster :
116
109
self ._cluster .remove_listener (WeakMethod (self ._handle_metadata_update ))
117
110
@@ -199,9 +192,9 @@ def _on_join_complete(self, generation, member_id, protocol,
199
192
# based on the received assignment
200
193
assignor .on_assignment (assignment )
201
194
202
- # restart the autocommit task if needed
195
+ # reschedule the auto commit starting from now
203
196
if self ._auto_commit_task :
204
- self ._auto_commit_task .enable ()
197
+ self ._auto_commit_task .reschedule ()
205
198
206
199
assigned = set (self ._subscription .assigned_partitions ())
207
200
log .info ("Setting newly assigned partitions %s for group %s" ,
@@ -378,10 +371,6 @@ def _maybe_auto_commit_offsets_sync(self):
378
371
if self ._auto_commit_task is None :
379
372
return
380
373
381
- # disable periodic commits prior to committing synchronously. note that they will
382
- # be re-enabled after a rebalance completes
383
- self ._auto_commit_task .disable ()
384
-
385
374
try :
386
375
self .commit_offsets_sync (self ._subscription .all_consumed_offsets ())
387
376
@@ -654,47 +643,25 @@ def __init__(self, coordinator, interval):
654
643
self ._coordinator = coordinator
655
644
self ._client = coordinator ._client
656
645
self ._interval = interval
657
- self ._enabled = False
658
- self ._request_in_flight = False
659
-
660
- def enable (self ):
661
- if self ._enabled :
662
- log .warning ("AutoCommitTask is already enabled" )
663
- return
664
-
665
- self ._enabled = True
666
- if not self ._request_in_flight :
667
- self ._client .schedule (self , time .time () + self ._interval )
668
646
669
- def disable (self ):
670
- self ._enabled = False
671
- try :
672
- self ._client .unschedule (self )
673
- except KeyError :
674
- pass
675
-
676
- def _reschedule (self , at ):
677
- assert self ._enabled , 'AutoCommitTask not enabled'
647
+ def reschedule (self , at = None ):
648
+ if at is None :
649
+ at = time .time () + self ._interval
678
650
self ._client .schedule (self , at )
679
651
680
652
def __call__ (self ):
681
- if not self ._enabled :
682
- return
683
-
684
653
if self ._coordinator .coordinator_unknown ():
685
654
log .debug ("Cannot auto-commit offsets for group %s because the"
686
655
" coordinator is unknown" , self ._coordinator .group_id )
687
656
backoff = self ._coordinator .config ['retry_backoff_ms' ] / 1000.0
688
- self ._client . schedule ( self , time .time () + backoff )
657
+ self .reschedule ( time .time () + backoff )
689
658
return
690
659
691
- self ._request_in_flight = True
692
660
self ._coordinator .commit_offsets_async (
693
661
self ._coordinator ._subscription .all_consumed_offsets (),
694
662
self ._handle_commit_response )
695
663
696
664
def _handle_commit_response (self , offsets , result ):
697
- self ._request_in_flight = False
698
665
if result is True :
699
666
log .debug ("Successfully auto-committed offsets for group %s" ,
700
667
self ._coordinator .group_id )
@@ -713,10 +680,7 @@ def _handle_commit_response(self, offsets, result):
713
680
self ._coordinator .group_id , result )
714
681
next_at = time .time () + self ._interval
715
682
716
- if not self ._enabled :
717
- log .warning ("Skipping auto-commit reschedule -- it is disabled" )
718
- return
719
- self ._reschedule (next_at )
683
+ self .reschedule (next_at )
720
684
721
685
722
686
class ConsumerCoordinatorMetrics (object ):
0 commit comments