kafka分区及副本在broker的分配
kafka分區及副本在broker的分配
@(KAFKA)[kafka, 大數據]
部分內容參考自:http://blog.csdn.net/lizhitao/article/details/41778193
下面以一個Kafka集群中4個Broker舉例,創建1個topic包含4個Partition,2 Replication;數據Producer流動如圖所示:
(1)
(2)當集群中新增2節點,Partition增加到6個時分布情況如下:
副本分配邏輯規則如下:
在Kafka集群中,每個Broker都有均等分配Partition的Leader機會。
上述圖Broker Partition中,箭頭指向為副本,以Partition-0為例:broker1中parition-0為Leader,Broker2中Partition-0為副本。
上述圖種每個Broker(按照BrokerId有序)依次分配主Partition,下一個Broker為副本,如此循環迭代分配,多副本都遵循此規則。
副本分配算法如下:
將所有N Broker和待分配的i個Partition排序.
將第i個Partition分配到第(i mod n)個Broker上.
將第i個Partition的第j個副本分配到第((i + j) mod n)個Broker上.
事實上以上的算法是有誤的,因為很明顯,每個topic的分區0都會被分配在broker 0上,第1個分區都分配到broker 1上,直到partition的id超過broker的數據才開始從頭開始重復,這樣會導致前面幾臺機器的壓力比后面的機器壓力更大。
因此,kafka是先隨機挑選一個broker放置分區0,然后再按順序放置其它分區。如下圖的情況:
Topic:ljh_test3 PartitionCount:10 ReplicationFactor:2 Configs:Topic: ljh_test3 Partition: 0 Leader: 5 Replicas: 5,6 Isr: 5,6Topic: ljh_test3 Partition: 1 Leader: 6 Replicas: 6,7 Isr: 6,7Topic: ljh_test3 Partition: 2 Leader: 7 Replicas: 7,2 Isr: 7,2Topic: ljh_test3 Partition: 3 Leader: 2 Replicas: 2,3 Isr: 2,3Topic: ljh_test3 Partition: 4 Leader: 3 Replicas: 3,4 Isr: 3,4Topic: ljh_test3 Partition: 5 Leader: 4 Replicas: 4,5 Isr: 4,5Topic: ljh_test3 Partition: 6 Leader: 5 Replicas: 5,7 Isr: 5,7Topic: ljh_test3 Partition: 7 Leader: 6 Replicas: 6,2 Isr: 6,2Topic: ljh_test3 Partition: 8 Leader: 7 Replicas: 7,3 Isr: 7,3Topic: ljh_test3 Partition: 9 Leader: 2 Replicas: 2,4 Isr: 2,4這里分區0放到了broker5中,分區1–broker6,分區2—broker7….
再看一個例子:
Topic:ljh_test2 PartitionCount:10 ReplicationFactor:2 Configs:Topic: ljh_test2 Partition: 0 Leader: 2 Replicas: 2,7 Isr: 2,7Topic: ljh_test2 Partition: 1 Leader: 3 Replicas: 3,2 Isr: 3,2Topic: ljh_test2 Partition: 2 Leader: 4 Replicas: 4,3 Isr: 4,3Topic: ljh_test2 Partition: 3 Leader: 5 Replicas: 5,4 Isr: 5,4Topic: ljh_test2 Partition: 4 Leader: 6 Replicas: 6,5 Isr: 6,5Topic: ljh_test2 Partition: 5 Leader: 7 Replicas: 7,6 Isr: 7,6Topic: ljh_test2 Partition: 6 Leader: 2 Replicas: 2,3 Isr: 2,3Topic: ljh_test2 Partition: 7 Leader: 3 Replicas: 3,4 Isr: 3,4Topic: ljh_test2 Partition: 8 Leader: 4 Replicas: 4,5 Isr: 4,5Topic: ljh_test2 Partition: 9 Leader: 5 Replicas: 5,6 Isr: 5,6總結
以上是生活随笔為你收集整理的kafka分区及副本在broker的分配的全部內容,希望文章能夠幫你解決所遇到的問題。