The consumer applies to the coordinator to join the consumer group
There is a coordinator who manages consumers on the server side, and the coordinator adds consumers to the consumer group
The coordinator assigns partitions to all consumers
The consumer gets the partitions assigned to it from the coordinator
Consumers pull messages from partitions
2. Send a request and get the result
Each consumer has to send a JoinGroupRequest (request to join the consumer group) to the coordinator, and each consumer gets the assigned partition from the JoinGroupResponse (response to join the consumer group)
The pseudo code of the process is as follows, the real code is in the joinGroupIfNeeded method of AbstractCoordinator
The actual situation is to wait for all consumers to join the consumer group before executing the algorithm for allocating partitions, so what is returned at the beginning is an asynchronous object Future
Note: The member number when the request is initiated is UNKNOWN_MEMBER_ID, and an actual member number will be returned after the first join, and the actual member number needs to be specified when rejoining or rebalancing occurs
The logic for consumers to connect to the coordinator, send "join consumer group" requests, and obtain partitions are all in AbstractCoordinator
Make sure to assign to the partition to execute the process
The initial needJoinPrepare and rejoinNeeded are both true, and the consumer will join the consumer group by default when starting
To meet needRejoin(), first call onJoinPrepare() to make preparations, such as submitting offsets, etc.
After the preparation work is completed, modify needsJoinPrepare=false to prevent the preparation work from being performed multiple times before joining the consumer group
Satisfy needRejoin(), execute the loop body: send a request, call the client polling once to try to get the result
Consumers are assigned to partitions, update rejoinNeeded to false, and reset needsJoinPrepare to true
The onJoinComplete() method will update the needsPartitionsAssigned of the subscription status to false after the consumer successfully joins the consumer group and assigns it to a partition. This variable is to determine whether to rejoin the consumer group
When the consumer starts, it registers listeners for different events (such as partition changes, consumer member changes, and session timeouts) with ZK. When the registered events occur, it will trigger the rebalancing operation of ZKRebalanceListener