@@ -235,14 +235,16 @@ def _reset_offset(self, partition):
235
235
log .debug ("Resetting offset for partition %s to %s offset." ,
236
236
partition , strategy )
237
237
offsets = self ._retrieve_offsets ({partition : timestamp })
238
- if partition not in offsets :
239
- raise NoOffsetForPartitionError (partition )
240
- offset = offsets [partition ][0 ]
241
238
242
- # we might lose the assignment while fetching the offset,
243
- # so check it is still active
244
- if self ._subscriptions .is_assigned (partition ):
245
- self ._subscriptions .seek (partition , offset )
239
+ if partition in offsets :
240
+ offset = offsets [partition ][0 ]
241
+
242
+ # we might lose the assignment while fetching the offset,
243
+ # so check it is still active
244
+ if self ._subscriptions .is_assigned (partition ):
245
+ self ._subscriptions .seek (partition , offset )
246
+ else :
247
+ log .debug ("Could not find offset for partition %s since it is probably deleted" % (partition ,))
246
248
247
249
def _retrieve_offsets (self , timestamps , timeout_ms = float ("inf" )):
248
250
"""Fetch offset for each partition passed in ``timestamps`` map.
@@ -267,6 +269,9 @@ def _retrieve_offsets(self, timestamps, timeout_ms=float("inf")):
267
269
start_time = time .time ()
268
270
remaining_ms = timeout_ms
269
271
while remaining_ms > 0 :
272
+ if not timestamps :
273
+ return {}
274
+
270
275
future = self ._send_offset_requests (timestamps )
271
276
self ._client .poll (future = future , timeout_ms = remaining_ms )
272
277
@@ -283,6 +288,15 @@ def _retrieve_offsets(self, timestamps, timeout_ms=float("inf")):
283
288
if future .exception .invalid_metadata :
284
289
refresh_future = self ._client .cluster .request_update ()
285
290
self ._client .poll (future = refresh_future , timeout_ms = remaining_ms )
291
+
292
+ # Issue #1780
293
+ # Recheck partition existance after after a successful metadata refresh
294
+ if refresh_future .succeeded () and isinstance (future .exception , Errors .StaleMetadata ):
295
+ log .debug ("Stale metadata was raised, and we now have an updated metadata. Rechecking partition existance" )
296
+ unknown_partition = future .exception .args [0 ] # TopicPartition from StaleMetadata
297
+ if not self ._client .cluster .leader_for_partition (unknown_partition ):
298
+ log .debug ("Removed partition %s from offsets retrieval" % (unknown_partition , ))
299
+ timestamps .pop (unknown_partition )
286
300
else :
287
301
time .sleep (self .config ['retry_backoff_ms' ] / 1000.0 )
288
302
0 commit comments