.net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(一)
我們知道Kafka支持Consumer Group的功能,但是最近在應(yīng)用Consumer Group時(shí)發(fā)現(xiàn)了一個(gè)Topic 的Partition不能100%覆蓋的問題。
程序部署后,發(fā)現(xiàn)Kafka在pdb組的consumer消費(fèi)topic時(shí)存在問題,consumer無法完全覆蓋Topic的各個(gè)partition。如下圖:
frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;"> frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;"> frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;">
frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;">
無論我在開啟多少個(gè)consumer實(shí)例,最高覆蓋只能達(dá)到66%。
進(jìn)一步跟蹤發(fā)現(xiàn),pdb組的consumer覆蓋到partition1和partion2.
frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;">
在kafka的主消費(fèi)組defaultGroup中的consumer,覆蓋partition0
frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;">
根據(jù)以上現(xiàn)象猜測(cè),當(dāng)有多個(gè)消費(fèi)者組對(duì)topic進(jìn)行消費(fèi)時(shí),存在partition的競(jìng)爭(zhēng)機(jī)制在里面。
為驗(yàn)證partion是否存在競(jìng)爭(zhēng),關(guān)掉測(cè)試程序,default group中的consumer覆蓋恢復(fù)100%。
如下圖。
frameborder="0" scrolling="no" style="border-width: medium; width: 1px; height: 1px;">
翻閱官方文檔,有提到
1、同一個(gè)partition不支持comsumer并發(fā)。
2、不同gourp組中的consumer,可以對(duì)同一個(gè)topic進(jìn)行消費(fèi)。
同時(shí)在spark的kafka插件中,對(duì)同一topic的消費(fèi)者,不同組都可以達(dá)到100%的覆蓋率。
在本地做測(cè)試,同樣存在該問題。
不同組的消費(fèi)者對(duì)同一個(gè)topic進(jìn)行消費(fèi)時(shí),兩個(gè)消費(fèi)者都達(dá)不到100%覆蓋率(每個(gè)組的消費(fèi)者總有一些數(shù)據(jù)消費(fèi)不到)。
在中文社區(qū)翻閱所有的消費(fèi)者和連接以及流的配置項(xiàng),暫未發(fā)現(xiàn)影響該問題的配置項(xiàng)(http://orchome.com/kafka/index kafka中文社區(qū)地址。)。
關(guān)于該問題的研究暫時(shí)沒有結(jié)論,暫未確定是配置不合適或kafka.Client 存在問題,后續(xù)會(huì)持續(xù)跟蹤該問題。
通過該次問題的研究,對(duì)kafka消費(fèi)組、消費(fèi)者、連接流、partion和consumer映射關(guān)系、parttion 分配策略有了較為深入的了解,唯一遺憾的是未找到關(guān)于該問題的官方解釋和相關(guān)說明。
附,Kafka-Partion和consumer重新Rebalance算法如下: ?
將目標(biāo)Topic下的所有Partirtion排序,存于PT
對(duì)某Consumer Group下所有Consumer排序,存于CG,第i個(gè)Consumer記為Ci
N=size(PT)/size(CG),向上取整
解除Ci對(duì)原來分配的Partition的消費(fèi)權(quán)(i從0開始)
將第i?N到(i+1)?N?1個(gè)Partition分配給Ci
目前,最新版(0.8.2.1)Kafka的Consumer Rebalance的控制策略是由每一個(gè)Consumer通過在Zookeeper上注冊(cè)Watch完成的。每個(gè)Consumer被創(chuàng)建時(shí)會(huì)觸發(fā)Consumer Group的Rebalance,具體啟動(dòng)流程如下:
High Level Consumer啟動(dòng)時(shí)將其ID注冊(cè)到其Consumer Group下,在Zookeeper上的路徑為/consumers/[consumer group]/ids/[consumer id]
在/consumers/[consumer group]/ids上注冊(cè)Watch
在/brokers/ids上注冊(cè)Watch
如果Consumer通過Topic Filter創(chuàng)建消息流,則它會(huì)同時(shí)在/brokers/topics上也創(chuàng)建Watch
強(qiáng)制自己在其Consumer Group內(nèi)啟動(dòng)Rebalance流程
在這種策略下,每一個(gè)Consumer或者Broker的增加或者減少都會(huì)觸發(fā)Consumer Rebalance。因?yàn)槊總€(gè)Consumer只負(fù)責(zé)調(diào)整自己所消費(fèi)的Partition,為了保證整個(gè)Consumer Group的一致性,當(dāng)一個(gè)Consumer觸發(fā)了Rebalance時(shí),該Consumer Group內(nèi)的其它所有其它Consumer也應(yīng)該同時(shí)觸發(fā)Rebalance。
相關(guān)文章:
消息隊(duì)列 Kafka 的基本知識(shí)及 .NET Core 客戶端
原文地址:http://www.cnblogs.com/vveiliang/p/6844345.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的.net Kafka.Client多个Consumer Group对Topic消费不能完全覆盖研究总结(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 窥探ASP.Net MVC底层原理 实现
- 下一篇: View Components as T