赞
踩
请求的实际处理逻辑是封装在 KafkaApis 类中,KafkaApis 类的定义代码如下
- class KafkaApis(val requestChannel: RequestChannel, // 请求通道
- val replicaManager: ReplicaManager, // 副本管理器,控制集群所有副本的状态转换
- val adminManager: AdminManager, // topic、分区配置等管理器
- val groupCoordinator: GroupCoordinator, // 消费者组协调器组件
- val txnCoordinator: TransactionCoordinator, // 事务管理器组件
- val controller: KafkaController, // 控制器组件,管理与保存原数据
- val zkClient: KafkaZkClient, // // ZooKeeper客户端程序,Kafka依赖于该类实现与ZooKeeper交互
- val brokerId: Int, // broker.id参数值
- val config: KafkaConfig, // Kafka配置类
- val metadataCache: MetadataCache, // 元数据缓存类
- val metrics: Metrics,
- val authorizer: Option[Authorizer],
- val quotas: QuotaManagers, // 配额管理器组件
- val fetchManager: FetchManager,
- brokerTopicStats: BrokerTopicStats,
- val clusterId: String,
- time: Time,
- val tokenManager: DelegationTokenManager) extends Logging {
- }
KafkaApis 的 handle 方法对个各种请求进行处理。
- def handle(request: RequestChannel.Request): Unit = {
- try {
- trace(s"Handling request:${request.requestDesc(true)} from connection ${request.context.connectionId};" +
- s"securityProtocol:${request.context.securityProtocol},principal:${request.context.principal}")
- // 根据请求头部信息中的apiKey字段判断属于哪类请求
- // 然后调用响应的handle***方法
- // 如果新增RPC协议类型,则:
- // 1. 添加新的apiKey标识新请求类型
- // 2. 添加新的case分支
- // 3. 添加对应的handle***方法
- request.header.apiKey match {
- case ApiKeys.PRODUCE => handleProduceRequest(request)
- case ApiKeys.FETCH => handleFetchRequest(request)
- case ApiKeys.LIST_OFFSETS => handleListOffsetRequest(request)
- case ApiKeys.METADATA => handleTopicMetadataRequest(request)
- case ApiKeys.LEADER_AND_ISR => handleLeaderAndIsrRequest(request)
- case ApiKeys.STOP_REPLICA => handleStopReplicaRequest(request)
- case ApiKeys.UPDATE_METADATA => handleUpdateMetadataRequest(request)
- case ApiKeys.CONTROLLED_SHUTDOWN => handleControlledShutdownRequest(request)
- case ApiKeys.OFFSET_COMMIT => handleOffsetCommitRequest(request)
- case ApiKeys.OFFSET_FETCH => handleOffsetFetchRequest(request)
- case ApiKeys.FIND_COORDINATOR => handleFindCoordinatorRequest(request)
- case ApiKeys.JOIN_GROUP => handleJoinGroupRequest(request)
- case ApiKeys.HEARTBEAT => handleHeartbeatRequest(request)
- case ApiKeys.LEAVE_GROUP => handleLeaveGroupRequest(request)
- case ApiKeys.SYNC_GROUP => handleSyncGroupRequest(request)
- case ApiKeys.DESCRIBE_GROUPS => handleDescribeGroupRequest(request)
- case ApiKeys.LIST_GROUPS => handleListGroupsRequest(request)
- case ApiKeys.SASL_HANDSHAKE => handleSaslHandshakeRequest(request)
- case ApiKeys.API_VERSIONS => handleApiVersionsRequest(request)
- case ApiKeys.CREATE_TOPICS => handleCreateTopicsRequest(request)
- case ApiKeys.DELETE_TOPICS => handleDeleteTopicsRequest(request)
- case ApiKeys.DELETE_RECORDS => handleDeleteRecordsRequest(request)
- case ApiKeys.INIT_PRODUCER_ID => handleInitProducerIdRequest(request)
- case ApiKeys.OFFSET_FOR_LEADER_EPOCH => handleOffsetForLeaderEpochRequest(request)
- case ApiKeys.ADD_PARTITIONS_TO_TXN => handleAddPartitionToTxnRequest(request)
- case ApiKeys.ADD_OFFSETS_TO_TXN => handleAddOffsetsToTxnRequest(request)
- case ApiKeys.END_TXN => handleEndTxnRequest(request)
- case ApiKeys.WRITE_TXN_MARKERS => handleWriteTxnMarkersRequest(request)
- case ApiKeys.TXN_OFFSET_COMMIT => handleTxnOffsetCommitRequest(request)
- case ApiKeys.DESCRIBE_ACLS => handleDescribeAcls(request)
- case ApiKeys.CREATE_ACLS => handleCreateAcls(request)
- case ApiKeys.DELETE_ACLS => handleDeleteAcls(request)
- case ApiKeys.ALTER_CONFIGS => handleAlterConfigsRequest(request)
- case ApiKeys.DESCRIBE_CONFIGS => handleDescribeConfigsRequest(request)
- case ApiKeys.ALTER_REPLICA_LOG_DIRS => handleAlterReplicaLogDirsRequest(request)
- case ApiKeys.DESCRIBE_LOG_DIRS => handleDescribeLogDirsRequest(request)
- case ApiKeys.SASL_AUTHENTICATE => handleSaslAuthenticateRequest(request)
- case ApiKeys.CREATE_PARTITIONS => handleCreatePartitionsRequest(request)
- case ApiKeys.CREATE_DELEGATION_TOKEN => handleCreateTokenRequest(request)
- case ApiKeys.RENEW_DELEGATION_TOKEN => handleRenewTokenRequest(request)
- case ApiKeys.EXPIRE_DELEGATION_TOKEN => handleExpireTokenRequest(request)
- case ApiKeys.DESCRIBE_DELEGATION_TOKEN => handleDescribeTokensRequest(request)
- case ApiKeys.DELETE_GROUPS => handleDeleteGroupsRequest(request)
- case ApiKeys.ELECT_LEADERS => handleElectReplicaLeader(request)
- case ApiKeys.INCREMENTAL_ALTER_CONFIGS => handleIncrementalAlterConfigsRequest(request)
- case ApiKeys.ALTER_PARTITION_REASSIGNMENTS => handleAlterPartitionReassignmentsRequest(request)
- case ApiKeys.LIST_PARTITION_REASSIGNMENTS => handleListPartitionReassignmentsRequest(request)
- case ApiKeys.OFFSET_DELETE => handleOffsetDeleteRequest(request)
- case ApiKeys.DESCRIBE_CLIENT_QUOTAS => handleDescribeClientQuotasRequest(request)
- case ApiKeys.ALTER_CLIENT_QUOTAS => handleAlterClientQuotasRequest(request)
- }
- } catch {
- // 如果是严重错误,则抛出异常
- case e: FatalExitError => throw e
- // 普通异常的话,记录下错误日志
- case e: Throwable => handleError(request, e)
- } finally {
- // 记录一下请求本地完成时间,即Broker处理完该请求的时间
- if (request.apiLocalCompleteTimeNanos < 0)
- request.apiLocalCompleteTimeNanos = time.nanoseconds
- }
- }
其他重要方法抛开 KafkaApis 的定义和 handle 方法,还有几个常用的方法也很重要,比如,用于发送 Response 的一组方法,以及用于鉴权的方法。
特别是前者,它是任何一类请求被处理之后都要做的必要步骤。毕竟,请求被处理完成还不够,Kafka 还需要把处理结果发送给请求发送方。
首先就是 sendResponse 系列方法。源码中带有 sendResponse 字眼的方法有 7 个之多。
sendResponse
该方法接收的实际上是 Request,而非 Response,因此,它会在内部构造出 Response 对象之后,再调用 sendResponse 方法。
- private def sendResponse(request: RequestChannel.Request,
- responseOpt: Option[AbstractResponse],
- onComplete: Option[Send => Unit]): Unit = {
- // Update error metrics for each error code in the response including Errors.NONE
- responseOpt.foreach(response => requestChannel.updateErrorMetrics(request.header.apiKey, response.errorCounts.asScala))
-
- val response = responseOpt match {
- case Some(response) =>
- val responseSend = request.context.buildResponse(response)
- val responseString =
- if (RequestChannel.isRequestLoggingEnabled) Some(response.toString(request.context.apiVersion))
- else None
- new RequestChannel.SendResponse(request, responseSend, responseString, onComplete)
- case None =>
- new RequestChannel.NoOpResponse(request)
- }
-
- requestChannel.sendResponse(response)
- }
sendNoOpResponseExemptThrottle
发送 NoOpResponse 类型的 Response 而不受请求通道上限流(throttling)的限制。所谓的 NoOpResponse,是指 Processor 线程取出该类型的 Response 后,不执行真正的 I/O 发送操作。
- private def sendNoOpResponseExemptThrottle(request: RequestChannel.Request): Unit = {
- quotas.request.maybeRecordExempt(request)
- sendResponse(request, None, None)
- }
sendErrorResponseExemptThrottle
发送携带错误信息的 Response 而不受限流限制。
- private def sendErrorResponseExemptThrottle(request: RequestChannel.Request, error: Throwable): Unit = {
- quotas.request.maybeRecordExempt(request)
- sendErrorOrCloseConnection(request, error, 0)
- }
sendResponseExemptThrottle
发送普通 Response 而不受限流限制。
- private def sendResponseExemptThrottle(request: RequestChannel.Request,
- response: AbstractResponse,
- onComplete: Option[Send => Unit] = None): Unit = {
- quotas.request.maybeRecordExempt(request)
- sendResponse(request, Some(response), onComplete)
- }
sendErrorResponseMaybeThrottle
发送携带错误信息的 Response 但接受限流的约束。
- private def sendErrorResponseMaybeThrottle(request: RequestChannel.Request, error: Throwable): Unit = {
- val throttleTimeMs = maybeRecordAndGetThrottleTimeMs(request)
- quotas.request.throttle(request, throttleTimeMs, requestChannel.sendResponse)
- sendErrorOrCloseConnection(request, error, throttleTimeMs)
- }
sendResponseMaybeThrottle
发送普通 Response 但接受限流的约束。
- private def sendResponseMaybeThrottle(request: RequestChannel.Request,
- createResponse: Int => AbstractResponse,
- onComplete: Option[Send => Unit] = None): Unit = {
- val throttleTimeMs = maybeRecordAndGetThrottleTimeMs(request)
- quotas.request.throttle(request, throttleTimeMs, requestChannel.sendResponse)
- sendResponse(request, Some(createResponse(throttleTimeMs)), onComplete)
- }
KafkaApis 实际上是把处理完成的 Response 放回到前端 Processor 线程的 Response 队列中,而真正将 Response 返还给 Clients 或其他 Broker 的,其实是 Processor 线程,而不是执行 KafkaApis 逻辑的 KafkaRequestHandler 线程。
KafkaApis 请求处理实例解析,以 handleListGroupsRequest 方法为例来介绍一下
- def handleListGroupsRequest(request: RequestChannel.Request): Unit = {
- val (error, groups) = groupCoordinator.handleListGroups() // 调用GroupCoordinator的handleListGroups方法拿到所有Group信息
- // 如果Clients具备CLUSTER资源的DESCRIBE权限
- if (authorize(request, DESCRIBE, CLUSTER, CLUSTER_NAME))
- // 直接使用刚才拿到的Group数据封装进Response然后发送
- sendResponseMaybeThrottle(request, requestThrottleMs =>
- new ListGroupsResponse(new ListGroupsResponseData()
- .setErrorCode(error.code)
- .setGroups(groups.map { group => new ListGroupsResponseData.ListedGroup()
- .setGroupId(group.groupId)
- .setProtocolType(group.protocolType)}.asJava
- )
- .setThrottleTimeMs(requestThrottleMs)
- ))
- else {
- // 找出Clients对哪些Group有GROUP资源的DESCRIBE权限,返回这些Group信息
- val filteredGroups = groups.filter(group => authorize(request, DESCRIBE, GROUP, group.groupId))
- sendResponseMaybeThrottle(request, requestThrottleMs =>
- new ListGroupsResponse(new ListGroupsResponseData()
- .setErrorCode(error.code)
- .setGroups(filteredGroups.map { group => new ListGroupsResponseData.ListedGroup()
- .setGroupId(group.groupId)
- .setProtocolType(group.protocolType)}.asJava
- )
- .setThrottleTimeMs(requestThrottleMs)
- ))
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。