@@ -161,11 +161,13 @@ def reset_offsets_if_needed(self, partitions, timeout_ms=None):
161161 Raises:
162162 KafkaTimeoutError if timeout_ms provided
163163 """
164- inner_timeout_ms = timeout_ms_fn ( timeout_ms , 'Timeout resetting offsets' )
164+ needs_offset_reset = set ( )
165165 for tp in partitions :
166- # TODO: If there are several offsets to reset, we could submit offset requests in parallel
167166 if self ._subscriptions .is_assigned (tp ) and self ._subscriptions .is_offset_reset_needed (tp ):
168- self ._reset_offset (tp , timeout_ms = inner_timeout_ms ())
167+ needs_offset_reset .add (tp )
168+
169+ if needs_offset_reset :
170+ self ._reset_offsets (needs_offset_reset , timeout_ms = timeout_ms )
169171
170172 def _clean_done_fetch_futures (self ):
171173 while True :
@@ -191,25 +193,28 @@ def update_fetch_positions(self, partitions, timeout_ms=None):
191193 partition and no reset policy is available
192194 KafkaTimeoutError if timeout_ms provided.
193195 """
194- inner_timeout_ms = timeout_ms_fn ( timeout_ms , 'Timeout updating fetch positions' )
196+ needs_offset_reset = set ( )
195197 # reset the fetch position to the committed position
196198 for tp in partitions :
197199 if not self ._subscriptions .is_assigned (tp ) or self ._subscriptions .has_valid_position (tp ):
198200 continue
199201
200202 if self ._subscriptions .is_offset_reset_needed (tp ):
201- self . _reset_offset (tp , timeout_ms = inner_timeout_ms () )
203+ needs_offset_reset . add (tp )
202204 elif self ._subscriptions .assignment [tp ].committed is None :
203205 # there's no committed position, so we need to reset with the
204206 # default strategy
205207 self ._subscriptions .need_offset_reset (tp )
206- self . _reset_offset (tp , timeout_ms = inner_timeout_ms () )
208+ needs_offset_reset . add (tp )
207209 else :
208210 committed = self ._subscriptions .assignment [tp ].committed .offset
209211 log .debug ("Resetting offset for partition %s to the committed"
210212 " offset %s" , tp , committed )
211213 self ._subscriptions .seek (tp , committed )
212214
215+ if needs_offset_reset :
216+ self ._reset_offsets (needs_offset_reset , timeout_ms = timeout_ms )
217+
213218 def get_offsets_by_times (self , timestamps , timeout_ms ):
214219 offsets = self ._retrieve_offsets (timestamps , timeout_ms )
215220 for tp in timestamps :
@@ -232,37 +237,36 @@ def beginning_or_end_offset(self, partitions, timestamp, timeout_ms):
232237 offsets [tp ] = offsets [tp ].offset
233238 return offsets
234239
235- def _reset_offset (self , partition , timeout_ms = None ):
236- """Reset offsets for the given partition using the offset reset strategy.
240+ def _reset_offsets (self , partitions , timeout_ms = None ):
241+ """Reset offsets for the given partitions using the offset reset strategy.
237242
238243 Arguments:
239- partition ( TopicPartition): the partition that needs reset offset
244+ partitions ([ TopicPartition] ): the partitions that need offsets reset
240245
241246 Raises:
242247 NoOffsetForPartitionError: if no offset reset strategy is defined
243248 KafkaTimeoutError if timeout_ms provided
244249 """
245- timestamp = self ._subscriptions .assignment [partition ].reset_strategy
246- if timestamp is OffsetResetStrategy .EARLIEST :
247- strategy = 'earliest'
248- elif timestamp is OffsetResetStrategy .LATEST :
249- strategy = 'latest'
250- else :
251- raise NoOffsetForPartitionError (partition )
250+ offset_resets = dict ()
251+ for tp in partitions :
252+ ts = self ._subscriptions .assignment [tp ].reset_strategy
253+ if not ts :
254+ raise NoOffsetForPartitionError (tp )
255+ offset_resets [tp ] = ts
252256
253- log .debug ("Resetting offset for partition %s to offset %s." ,
254- partition , strategy )
255- offsets = self ._retrieve_offsets ({partition : timestamp }, timeout_ms = timeout_ms )
257+ offsets = self ._retrieve_offsets (offset_resets , timeout_ms = timeout_ms )
256258
257- if partition in offsets :
258- offset = offsets [partition ].offset
259+ for partition in partitions :
260+ if partition not in offsets :
261+ raise NoOffsetForPartitionError (partition )
259262
260263 # we might lose the assignment while fetching the offset,
261264 # so check it is still active
262265 if self ._subscriptions .is_assigned (partition ):
266+ offset = offsets [partition ].offset
267+ log .debug ("Resetting offset for partition %s to offset %s." ,
268+ partition , offset )
263269 self ._subscriptions .seek (partition , offset )
264- else :
265- log .debug ("Could not find offset for partition %s since it is probably deleted" % (partition ,))
266270
267271 def _retrieve_offsets (self , timestamps , timeout_ms = None ):
268272 """Fetch offset for each partition passed in ``timestamps`` map.
0 commit comments