ControllerChannelManager
Controller Leader通过发送多种请求管理集群中的其他Broker,KafkaController使用ControllerChannelManager管理其与集群中各个Broker之间的网络交互。
ControllerChannelManager中使用ControllerBrokerStatelnfo类表示与一个Broker连接的各种信息。
ControllerChannelManager的核心字段是brokerStatelnfo(HashMap[Int,ControllerBrokerStatelnfo]类型),用于管理集群中每个Broker对应的ControllerBrokerStatelnfo对象。
ControllerChannelManager.addNewBroker方法和removeBroker方法实现了对brokerStatelnfo集合的管理,sendRequest方法向指定Broker发送请求。
ControllerContext
ControllerContext中维护了Controller使用到的上下文信息,从其构造函数也能猜到,ControllerContext与ZooKeeper有密切的关系,也可以将ControllerContext看作ZooKeeper数据的缓存。
ControllerContext中各个字段的含义和作用如下所述。
- controllerChannelManager:管理Controller与集群中Broker之间的连接。
- shuttingDownBrokerlds:正在关闭的Brokerld集合。
- epoch:Controller的年代信息,初始为0。Controller的年代信息存储的ZK路径是“/controller_epoch”。每次重新选举新的Leader Controller,epoch字段值就会增加1。
- epochZkVersion:年代信息的ZK版本,初始为0。
- allTopics:整个集群中全部的Topic名称。
- partitionReplicaAssignment:Map[TopicAndPartition,Seq[Int]]类型,记录了每个分区的AR集合。
- partitionLeadershipInfo:Map[TopicAndParition,LeaderlsrAndControllerEpoch]类型,记录了每个分区的Leader副本所在的Brokerld、ISR集合以及controller_epoch等信息。
- partitionBeingReassigned:Map[TopicAndPartition,ReassignedPartitionsContext]类型,记录了正在重新分配副本的分区。该集合的value是ReassignedPartitionsContext类型,其中封装了新分配的AR集合信息以及用于监听ISR集合变化的ReassignedPartitionsIsrChangeListener。
- partitionsUndergoingPreferredReplicaElection:SetTopicAndPartition]类型,记录了正在进行“优先副本”选举的分区。
- liveBrokersUnderlying:Set[Broker]类型,记录了当前可用的Broker集合。
- liveBrokerldsUnderlying:Set[Int]类型,记录了当前可用的Brokerld集合。
ControllerContext为liveBrokersUnderlying字段、liveBrokerldsUnderlying字段和shuttingDownBrokerlds字段提供了相关的集合操纵方法。
- liveBrokers_:根据提供的Broker集合对liveBrokersUnderlying集合和liveBrokerldsUnderlying集合进行更新。
- liveBrokers/liveBrokerlds:从liveBrokersUnderlying/liveBrokerldsUnderlying集合中排除shuttingDownBrokerlds集合后返回。
- liveOrShuttingDownBrokersIds/liveOrShuttingDownBrokers: 获取liveBrokersUnderlying/liveBrokerldsUnderlying集合。
ControllerContext为partitionReplicaAssignment字段提供的管理方法如下所述。
- partitionsOnBroker:获取在指定Broker中存在有副本的分区集合。
- replicasOnBrokers:获取指定Broker集合中保存的所有副本。
- replicasForTopic:获取指定Topic的所有副本。
- partitionsForTopic:获取指定Topic的所有分区。
- allLiveReplicas:获取所有可用Broker中保存的副本。
- replicasForPartition:获取指定分区集合的副本。
- removeTopic:删除指定Topic。
ControllerBrokerRequestBatch
为了提高Controller Leader与集群中其他Broker的通信效率,KafkaController使用ControllerBrokerRequestBatch组件实现批量发送请求的功能。
ControllerBrokerRequestBatch的核心字段如下所述。
- leaderAndIsrRequestMap:Map[Int,Map[TopicPartition,PartitionStatelnfo]]类型,记录了发往指定Broker的LeaderAndlsrRequest所需的信息。
- stopReplicaRequestMap:Map[Int,Seq[StopReplicaRequestInfo]]类型,记录了发往指定Broker的StopReplicaRequest所需的信息。
- updateMetadataRequestMap:Map[Int,Map[TopicPartition,PartitionStatelnfo]]类型,记录了发往指定Broker的UpdateMetadataRequest集合。
ControllerBrokerRequestBatch.newBatch方法会检测三个请求集合是否为空,如果不为空则抛出异常。ControllerBrokerRequestBatch.clear方法则会清空三个请求集合。
ControllerBrokerRequestBatch.addLeaderAndIsrRequestForBrokers()方法会 向leaderAndIsrRequestMap集合中添加待发送的LeaderAndIsrRequest所需的数据,同时会调用addUpdateMetadataRequestForBrokers()方法准备向集群中所有可用的Broker发送UpdateMetadataRequest。
addStopReplicaRequestForBrokers方法会向stopReplicaRequestMap集合中添加StopReplicaRequest所需的数据,具体实现与上述两个add*RequestForBroker()类似,不再赘述。
ControllerBrokerRequestBatch.sendRequestsToBrokers()方法会使用上述三个集合中的数据来创建相应的请求,并添加到ControllerChannelManager中对应的messageQueue队列中,最终由RequestSendThread线程将请求发送出去。