Kafka GroupCoordinator机制(十四):GroupCoordinator之SyncGroupRequest分析

服务端对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))
        }
      }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值