当前位置: 首页 > news >正文

泰安放心的企业建站公司/互联网营销师报名入口

泰安放心的企业建站公司,互联网营销师报名入口,aliyun wordpress,做棋牌网站团队转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6238029.html 最近需要详细研究下kafka reblance过程中分区计算的算法细节,网上搜了部分说法,感觉比较晦涩且不太易懂,还是自己抠源码比较简便一点。 kafka reblance计算部分代码…

转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/6238029.html

     最近需要详细研究下kafka reblance过程中分区计算的算法细节,网上搜了部分说法,感觉比较晦涩且不太易懂,还是自己抠源码比较简便一点。

kafka reblance计算部分代码如下:

class RangeAssignor() extends PartitionAssignor with Logging {def assign(ctx: AssignmentContext) = {val valueFactory = (topic: String) => new mutable.HashMap[TopicAndPartition, ConsumerThreadId]val partitionAssignment =new Pool[String, mutable.Map[TopicAndPartition, ConsumerThreadId]](Some(valueFactory))for (topic <- ctx.myTopicThreadIds.keySet) {
      val curConsumers = ctx.consumersForTopic(topic)val curPartitions: Seq[Int] = ctx.partitionsForTopic(topic)val nPartsPerConsumer = curPartitions.size / curConsumers.sizeval nConsumersWithExtraPart = curPartitions.size % curConsumers.sizeinfo("Consumer " + ctx.consumerId + " rebalancing the following partitions: " + curPartitions +" for topic " + topic + " with consumers: " + curConsumers)for (consumerThreadId <- curConsumers) {
        val myConsumerPosition = curConsumers.indexOf(consumerThreadId)assert(myConsumerPosition >= 0)val startPart = nPartsPerConsumer * myConsumerPosition + myConsumerPosition.min(nConsumersWithExtraPart)val nParts = nPartsPerConsumer + (if (myConsumerPosition + 1 > nConsumersWithExtraPart) 0 else 1)/***   Range-partition the sorted partitions to consumers for better locality.*  The first few consumers pick up an extra partition, if any.*/if (nParts <= 0)warn("No broker partitions consumed by consumer thread " + consumerThreadId + " for topic " + topic)else {for (i <- startPart until startPart + nParts) {val partition = curPartitions(i)info(consumerThreadId + " attempting to claim partition " + partition)// record the partition ownership decisionval assignmentForConsumer = partitionAssignment.getAndMaybePut(consumerThreadId.consumer)assignmentForConsumer += (TopicAndPartition(topic, partition) -> consumerThreadId)}}}}

 

  def getPartitionsForTopics(topics: Seq[String]): mutable.Map[String, Seq[Int]] = {getPartitionAssignmentForTopics(topics).map { topicAndPartitionMap =>val topic = topicAndPartitionMap._1val partitionMap = topicAndPartitionMap._2debug("partition assignment of /brokers/topics/%s is %s".format(topic, partitionMap))    (topic -> partitionMap.keys.toSeq.sortWith((s,t) => s < t))}}

 

  def getConsumersPerTopic(group: String, excludeInternalTopics: Boolean) : mutable.Map[String, List[ConsumerThreadId]] = {val dirs = new ZKGroupDirs(group)val consumers = getChildrenParentMayNotExist(dirs.consumerRegistryDir)val consumersPerTopicMap = new mutable.HashMap[String, List[ConsumerThreadId]]for (consumer <- consumers) {val topicCount = TopicCount.constructTopicCount(group, consumer, this, excludeInternalTopics)for ((topic, consumerThreadIdSet) <- topicCount.getConsumerThreadIdsPerTopic) {for (consumerThreadId <- consumerThreadIdSet)consumersPerTopicMap.get(topic) match {case Some(curConsumers) => consumersPerTopicMap.put(topic, consumerThreadId :: curConsumers)case _ => consumersPerTopicMap.put(topic, List(consumerThreadId))}}}for ( (topic, consumerList) <- consumersPerTopicMap )    consumersPerTopicMap.put(topic, consumerList.sortWith((s,t) => s < t))consumersPerTopicMap}

 

 

计算过程主要由上述高亮代码部分实现,举例说明,一个拥有十个分区的topic,相同group拥有三个consumerid为aaa,ccc,bbb的消费者

1 由后两段代码可知,获取consumerid列表和partition分区列表都是已经排好序的,所以

curConsumers=(aaa,bbb,ccc)

curPartitions=(0,1,2,3,4,5,6,7,8,9)

2

nPartsPerConsumer=10/3  =3

nConsumersWithExtraPart=10%3  =1

3 假设当前客户端id为aaa

myConsumerPosition= curConsumers.indexof(aaa) =0

4 计算分区范围

startPart= 3*0+0.min(1) = 0

nParts = 3+(if (0 + 1 > 1) 0 else 1)=3+1=4

所以aaa对应的分区号为[0,4),即0,1,2,3前面四个分区

同理可得bbb对应myConsumerPosition=1,对应分区4,5,6中间三个分区

ccc对应myConsumerPosition=2,对应7,8,9最后三个分区。

 

转载于:https://www.cnblogs.com/dongxiao-yang/p/6238029.html

http://www.jmfq.cn/news/5111677.html

相关文章:

  • wordpress 交友主题/关键词优化怎么优化
  • 定制包装需要多少钱/seo的作用
  • 白领兼职做网站/培训网站源码
  • 常见的手机网站/网络推广营销网站建设专家
  • 有没有小学生做兼职的网站/个人在百度上发广告怎么发
  • 岳阳网站建设制作/刷赞网站推广永久
  • 网站建设如何做用户名密码/全国新冠疫苗接种率
  • 呼和浩特市手机网站/最新收录查询
  • 手机网站规格/培训网站搭建
  • 深圳化妆品网站建设/推广渠道怎么写
  • 什么网站可以做章/百度电脑版官网
  • 东莞做网站企业铭/发帖子最好的几个网站
  • asp网站添加背景音乐/网页友情链接
  • 深圳网站建设哪家强/教育培训网站模板
  • java用哪种构架做网站/系统优化的例子
  • 做外贸怎么能上国外网站/网络宣传方案
  • 衡阳公司网站建设/seo诊断报告
  • 乌鲁木齐房地产网站建设/湖南疫情最新消息今天
  • 恶搞网站怎么做/郑州网站推广公司咨询
  • 建设银行网站能变更手机号吗/国际免费b站
  • wordpress中下载按钮/seo网站优化推荐
  • wordpress拖拽式建站主题/企业品牌推广网站
  • 做传单免费的网站/北京seo优化推广
  • 北京最大的网站开发公司/网络营销策略存在的问题
  • 装修平台是怎么找客户的/山东搜索引擎优化
  • 湖州市建设培训中心网站/优化网站打开速度
  • seo工作前景如何/青岛seo全网营销
  • 金华做网站建设公司/免费代理上网网站
  • 张家口网站开发/舆情分析报告
  • 做任务佣金的网站/广州网站优化页面