Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring kafka , Does ConsumerSeekAware interface, onPartitionsAssigned method call when rebalancing

Does ConsumerSeekAware interface, onPartitionsAssigned method call when rebalancing. Because I want to seek to specific offset which are in the offset when initializing and rebalancing. Could I use consumerSeekAware for both purposes or should I use ConsumerRebalanceListener for rebalancing purpose. Please give simple answers because I don't have depth knowledge about spring kafka yet. If you could please provide a sample code. Thank you

like image 354
Shiwantha Sandamal Avatar asked Oct 16 '22 22:10

Shiwantha Sandamal


1 Answers

The ConsumerSeekAware has this method:

/**
 * When using group management, called when partition assignments change.
 * @param assignments the new assignments and their current offsets.
 * @param callback the callback to perform an initial seek after assignment.
 */
void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback);

It is called from the KafkaMessageListenerContainer.seekPartitions(Collection<TopicPartition> partitions, boolean idle), which, in turn, from the ConsumerRebalanceListener.onPartitionsAssigned() internal implementation. And thew last one has this JavaDocs:

 * A callback method the user can implement to provide handling of customized offsets on completion of a successful
 * partition re-assignment. This method will be called after an offset re-assignment completes and before the
 * consumer starts fetching data.

So, yes, ConsumerSeekAware.onPartitionsAssigned() is always called during rebalancing. By the way there is no such a state for Apache Kafka as initializing. It is always rebalancing - the broker is in waiting state and starts rebalancing whenever a new consumer is joined.

like image 199
Artem Bilan Avatar answered Oct 20 '22 23:10

Artem Bilan