@@ -303,12 +303,12 @@ func (c *Consumer) startConsumer() error {
303
303
var currentOffset int64
304
304
switch c .conf .StartAtOffset {
305
305
case "oldest" :
306
- currentOffset , _ , err = c .tryGetOffset (topic , partition , int64 (confluent .OffsetBeginning ), 3 , time .Second )
306
+ currentOffset , err = c .tryGetOffset (topic , partition , int64 (confluent .OffsetBeginning ), 3 , time .Second )
307
307
if err != nil {
308
308
return err
309
309
}
310
310
case "newest" :
311
- _ , currentOffset , err = c .tryGetOffset (topic , partition , int64 (confluent .OffsetEnd ), 3 , time .Second )
311
+ currentOffset , err = c .tryGetOffset (topic , partition , int64 (confluent .OffsetEnd ), 3 , time .Second )
312
312
if err != nil {
313
313
return err
314
314
}
@@ -318,9 +318,13 @@ func (c *Consumer) startConsumer() error {
318
318
return fmt .Errorf ("invalid offest format %s: %s" , c .conf .StartAtOffset , err )
319
319
}
320
320
currentOffset = time .Now ().Add (- 1 * offsetDuration ).UnixNano () / int64 (time .Millisecond )
321
- currentOffset , _ , err = c .tryGetOffset (topic , partition , currentOffset , 3 , time .Second )
321
+ currentOffset , err = c .tryGetOffset (topic , partition , currentOffset , 3 , time .Second )
322
322
if err != nil {
323
- return err
323
+ log .Warn ("kafka-consumer: Failed to get specified offset %s, falling back to \" oldest\" " , c .conf .StartAtOffset )
324
+ currentOffset , err = c .tryGetOffset (topic , partition , int64 (confluent .OffsetBeginning ), 3 , time .Second )
325
+ if err != nil {
326
+ return err
327
+ }
324
328
}
325
329
}
326
330
@@ -344,44 +348,50 @@ func (c *Consumer) startConsumer() error {
344
348
return c .consumer .Assign (topicPartitions )
345
349
}
346
350
347
- func (c * Consumer ) tryGetOffset (topic string , partition int32 , offsetI int64 , attempts int , sleep time.Duration ) (int64 , int64 , error ) {
351
+ func (c * Consumer ) tryGetOffset (topic string , partition int32 , offsetI int64 , attempts int , sleep time.Duration ) (int64 , error ) {
348
352
offset , err := confluent .NewOffset (offsetI )
349
353
if err != nil {
350
- return 0 , 0 , err
354
+ return 0 , err
351
355
}
352
356
353
- var val1 , val2 int64
357
+ var beginning , end int64
354
358
355
359
attempt := 1
356
360
for {
357
361
if offset == confluent .OffsetBeginning || offset == confluent .OffsetEnd {
358
- val1 , val2 , err = c .consumer .QueryWatermarkOffsets (topic , partition , c .conf .MetadataTimeout )
362
+ beginning , end , err = c .consumer .QueryWatermarkOffsets (topic , partition , c .conf .MetadataTimeout )
363
+ if err == nil {
364
+ if offset == confluent .OffsetBeginning {
365
+ return beginning , nil
366
+ } else {
367
+ return end , nil
368
+ }
369
+ }
359
370
} else {
360
371
times := []confluent.TopicPartition {{Topic : & topic , Partition : partition , Offset : offset }}
361
372
times , err = c .consumer .OffsetsForTimes (times , c .conf .MetadataTimeout )
362
- if err == nil {
363
- if len ( times ) == 0 {
364
- err = fmt .Errorf ("Got 0 topics returned from broker" )
373
+ if err != nil || len ( times ) == 0 {
374
+ if err == nil {
375
+ err = fmt .Errorf ("Failed to get offset %d from kafka, falling back to \" oldest \" " , offset )
365
376
} else {
366
- val1 = int64 ( times [ 0 ]. Offset )
377
+ err = fmt . Errorf ( "Failed to get offset %d from kafka, falling back to \" oldest \" : %s" , offset , err )
367
378
}
379
+ offset = confluent .OffsetBeginning
380
+ } else {
381
+ return int64 (times [0 ].Offset ), nil
368
382
}
369
383
}
370
384
371
- if err == nil {
372
- return val1 , val2 , err
373
- }
374
-
375
385
if attempt >= attempts {
376
386
break
377
387
}
378
388
379
- log .Warn ("kafka-consumer: Error when qerying offsets, %d retries left: %s" , attempts - attempt , err )
389
+ log .Warn ("kafka-consumer: Error when querying offsets, %d retries left: %s" , attempts - attempt , err )
380
390
attempt += 1
381
391
time .Sleep (sleep )
382
392
}
383
393
384
- return 0 , 0 , fmt .Errorf ("Failed to get offset %s of partition %s:%d. %s (attempt %d/%d)" , offset .String (), topic , partition , err , attempt , attempts )
394
+ return 0 , fmt .Errorf ("Failed to get offset %s of partition %s:%d. %s (attempt %d/%d)" , offset .String (), topic , partition , err , attempt , attempts )
385
395
}
386
396
387
397
func (c * Consumer ) Stop () {
0 commit comments