KafkaController 分区Rebalance平衡机制

private def checkAndTriggerPartitionRebalance(): Unit = {
  if (isActive()) {
    trace("checking need to trigger partition rebalance")
    // 获取(存活的broker,AR副本集) => (2,Map([message,0]-> List(2, 0), [hadoop,0] -> List(2, 1)))
   
var preferredReplicasForTopicsByBrokers: Map[Int, Map[TopicAndPartition, Seq[Int]]] = null
   
inLock(controllerContext.controllerLock) {
      preferredReplicasForTopicsByBrokers=
        controllerContext.partitionReplicaAssignment.filterNot(p => deleteTopicManager.isTopicQueuedUpForDeletion(p._1.topic)).groupBy {
          case(topicAndPartition, assignedReplicas) => assignedReplicas.head
       
}
    }
    debug("preferred replicas by broker " + preferredReplicasForTopicsByBrokers)
    // 过滤每一个存活的broker,检查是否需要一个preferredreplica 选举被触发
   
preferredReplicasForTopicsByBrokers.foreach {
      case(leaderBroker, topicAndPartitionsForBroker) => {
        var imbalanceRatio: Double = 0
       
var topicsNotInPreferredReplica: Map[TopicAndPartition, Seq[Int]] = null
       
inLock(controllerContext.controllerLock) {
          // 我们知道,正常情况下,brokerAR副本集第一个副本(preferred replica )就是leader
          //
如果leader不是preferred replica,比如 Leader : 0 ISR[2,0]
          //
我们需要过滤出这种topicPartition,然后我们好进行在平衡
         
topicsNotInPreferredReplica=
            topicAndPartitionsForBroker.filter {
              case(topicPartition, replicas) => {
                controllerContext.partitionLeadershipInfo.contains(topicPartition) &&
                controllerContext.partitionLeadershipInfo(topicPartition).leaderAndIsr.leader != leaderBroker
             
}
            }
          debug("topics not in preferred replica " + topicsNotInPreferredReplica)
          // broker AR副本数量
         
val totalTopicPartitionsForBroker = topicAndPartitionsForBroker.size
         
// 过滤出的leader不是AR副本集的preferred replica的数量
         
val totalTopicPartitionsNotLedByBroker = topicsNotInPreferredReplica.size
         
// 计算(过滤出的leader不是AR副本集的preferred replica的数量)/(broker AR副本数量)不平衡比例
         
imbalanceRatio= totalTopicPartitionsNotLedByBroker.toDouble / totalTopicPartitionsForBroker
         
trace("leader imbalance ratio for broker %d is %f".format(leaderBroker, imbalanceRatio))
        }
        // 如果比例大于我们配置的leader.imbalance.per.broker.percentage参数,比如50%,就触发这个topic partitions的再平衡操作
       
if (imbalanceRatio > (config.leaderImbalancePerBrokerPercentage.toDouble / 100)) {
          topicsNotInPreferredReplica.foreach {
            case(topicPartition, replicas) => {
              inLock(controllerContext.controllerLock) {
                // 首先确保broker存活,而且没有分区正在重新分配或者没有进行preferredreplica 选举,且没有分区将被删除
               
if (controllerContext.liveBrokerIds.contains(leaderBroker) &&
                    controllerContext.partitionsBeingReassigned.isEmpty &&
                    controllerContext.partitionsUndergoingPreferredReplicaElection.isEmpty &&
                    !deleteTopicManager.isTopicQueuedUpForDeletion(topicPartition.topic) &&
                    controllerContext.allTopics.contains(topicPartition.topic)) {
                  // 然后真正触发Prederred Replica选举操作
                 
onPreferredReplicaElection(Set(topicPartition), true)
                }
              }
            }
          }
        }
      }
    }
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫言静好、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值