每个消费者都会通过HeartbeatTask任务定时向GroupCoordinator发送heartbeatRequest,告知GroupCoordinator自己正常在线。
HeartBeatRequest首先由KafkaApi.handleHeartbeatRequest方法进行处理,它负责验证权限,定义回调函数,并将请求委托给GroupCoordinator处理。
def handleHeartbeatRequest(request: RequestChannel.Request) {
val heartbeatRequest = request.body.asInstanceOf[HeartbeatRequest]
val respHeader = new ResponseHeader(request.header.correlationId)
// the callback for sending a heartbeat response
// 定义回调函数,把heartbeatResponse放入requestChannel等待发送
def sendResponseCallback(errorCode: Short) {
val response = new HeartbeatResponse(errorCode)
trace("Sending heartbeat response %s for correlation id %d to client %s."
.format(response, request.header.correlationId, request.header.clientId))
requestChannel.sendResponse(new RequestChannel.Response(request, new ResponseSend(request.connectionId, respHeader, response)))
}
if (!authorize(request.session, Read, new Resource(Group, heartbeatRequest.groupId))) {
val heartbeatResponse = new HeartbeatResponse(Errors.GROUP_AUTHORIZATION_FAILED.code)
requestChannel.sendResponse(new Response(request, new ResponseSend(request.connectionId, respHeader, heartbeatResponse)))
}
else {
// let the coordinator to handle heartbeat
// 把heartbeat委托给GroupCoordinator处理
coordinator.handleHeartbeat(
heartbeatRequest.groupId(),
heartbeatRequest.memberId(),
heartbeatRequest.groupGenerationId(),
sendResponseCallback)
}
}
Grou