服务端对SyncGroupRequest的处理是rebalance的第三步,API层由KafkaApi.handleSyncGroupRequest方法负责处理SyncGroupRequest,其中会定义相关的回调函数,并将请求交给GroupCoordinator.handleSyncGroupRequest方法负责处理SyncGroupRequest,其中会定义相关的回调函数,并将请求交给GroupCoordinator.handleSyncJoinGroup()实现,最后给doSyncGroup()方法实现。
private def doSyncGroup(group: GroupMetadata,
generationId: Int,
memberId: String,
groupAssignment: Map[String, Array[Byte]],
responseCallback: SyncCallback) {
var delayedGroupStore: Option[DelayedStore] = None
group synchronized {
if (!group.has(memberId)) {// 检测memberID
responseCallback(Array.empty, Errors.UNKNOWN_MEMBER_ID.code)
} else if (generationId != group.generationId) {
responseCallback(Array.empty, Errors.ILLEGAL_GENERATION.code)
} else {
group.currentState match {
case Dead =>// 直接返回UNKNOWN_MEMBER_ID错误码
responseCallback(Array.empty, Errors.UNKNOWN_MEMBER_ID.code)
case PreparingRebalance =>// 直接返回REBALANCE_IN_PROGRESS错误码
responseCallback(Array.empty, Errors.REBALANCE_IN_PROGRESS.code)
case AwaitingSync =>
//设置awaitingSyncCallback回调函数
group.get(memberId).awaitingSyncCallback = responseCallback
// 更新最后一次收到心跳的时间戳,并创建新的delayedHeartbeat对象放入heartbeatPurgatory中等待下次心跳。
completeAndScheduleNextHeartbeatExpiration(group, group.get(memberId))
// if this is the leader, then we can attempt to persist state and transition to stable
// 处理GroupLeader发来SyncGroupRequest
if (memberId == group.leaderId) {
info(s"Assignment received from leader for group ${group.groupId} for generation ${group.generationId}")
// fill any missing members with an empty assignment
// 将未分配到分区的member对应的分配结果填充为空的byte数组
val missing = group.allMembers -- groupAssignment.keySet
val assignment = groupAssignment ++ missing.map(_ -> Array.empty[Byte]).toMap
// 通过GroupMetadtaManager将GroupMetadata相关信息形成消息,并写入到对应__CONSUMER_OFFSET分区中。
delayedGroupStore = Some(groupManager.prepareStoreGroup(group, assignment, (errorCode: Short) => {
group synchronized {
// another member may have joined the group while we were awaiting this callback,
// so we must ensure we are still in the AwaitingSync state and the same generation
// when it gets invoked. if we have transitioned to another state, then do nothing
if (group.is(AwaitingSync) && generationId == group.generationId) {
if (errorCode != Errors.NONE.code) {
// 清空分区的分配结果,发送异常响应
resetAndPropagateAssignmentError(group, errorCode)
// 切换成PreparingRebalance状态
maybePrepareRebalance(group)
} else {
// 设置分区的分配结果,发送正常的SyncGroupResponse
setAndPropagateAssignment(group, assignment)
group.transitionTo(Stable)
}
}
}
}))
}
case Stable =>
// if the group is stable, we just return the current assignment
// 将分配给member的负责处理的分区信息返回
val memberMetadata = group.get(memberId)
responseCallback(memberMetadata.assignment, Errors.NONE.code)
// 心跳相关操作
completeAndScheduleNextHeartbeatExpiration(group, group.get(memberId))
}
}
}