Pretext
I have:
- A connected Kafka consumer
- The consumer is part of a consumer group.
- A rebalance listener attached to the consumer
- Auto-commit is disabled
Additionally, I have a method that takes in two parameters, the consumer, and a rebalance-listener that tracks what partitions have been assigned to the consumer
void aggregateProcessing(ConsumerRecords<String, SomeClass> consumer, RebalanceListener listener)
public class RebalanceListener implements ConsumerRebalanceListener {
private Set<TopicPartition> assignedPartitions = new LinkedHashSet<>();
@Override
public void onPartitionsAssigned(final Collection<TopicPartition> partitions) {
// keep track of assigned partitions
}
}
Goal
This method is run on a timer, and its goal is to process records until there are none left to read or up until some max time in all of the partitions.
Since rebalancing could happen in the middle of consumption (after consumer.poll() has fired several times already), I would like to detect this, reset, and restart processing from the last committed offset for all assigned partitions (even if that had already been assigned).
Is there a way to reset the consumer's internal offset for each partition back to the latest committed offset for a list of assigned partitions?
Sidenote
I understand reprocessing for all partitions (instead of just for the ones that were changed) is less efficient than selectively expunging some of the processing, but will probably be significantly easier than tracking what data needs to be expunged when a partition is removed.
Thanks!