kafka分區及副本在broker的分配
部分內容參考自:http://blog.csdn.net/lizhitao/article/details/41778193
以下以一個Kafka集群中4個Broker舉例,創建1個topic包括4個Partition,2 Replication;數據Producer流動如圖所看到的:
(1)
pic
(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,6
Topic: ljh_test3 Partition: 1 Leader: 6 Replicas: 6,7 Isr: 6,7
Topic: ljh_test3 Partition: 2 Leader: 7 Replicas: 7,2 Isr: 7,2
Topic: ljh_test3 Partition: 3 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: ljh_test3 Partition: 4 Leader: 3 Replicas: 3,4 Isr: 3,4
Topic: ljh_test3 Partition: 5 Leader: 4 Replicas: 4,5 Isr: 4,5
Topic: ljh_test3 Partition: 6 Leader: 5 Replicas: 5,7 Isr: 5,7
Topic: ljh_test3 Partition: 7 Leader: 6 Replicas: 6,2 Isr: 6,2
Topic: ljh_test3 Partition: 8 Leader: 7 Replicas: 7,3 Isr: 7,3
Topic: 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,7
Topic: ljh_test2 Partition: 1 Leader: 3 Replicas: 3,2 Isr: 3,2
Topic: ljh_test2 Partition: 2 Leader: 4 Replicas: 4,3 Isr: 4,3
Topic: ljh_test2 Partition: 3 Leader: 5 Replicas: 5,4 Isr: 5,4
Topic: ljh_test2 Partition: 4 Leader: 6 Replicas: 6,5 Isr: 6,5
Topic: ljh_test2 Partition: 5 Leader: 7 Replicas: 7,6 Isr: 7,6
Topic: ljh_test2 Partition: 6 Leader: 2 Replicas: 2,3 Isr: 2,3
Topic: ljh_test2 Partition: 7 Leader: 3 Replicas: 3,4 Isr: 3,4
Topic: ljh_test2 Partition: 8 Leader: 4 Replicas: 4,5 Isr: 4,5
Topic: ljh_test2 Partition: 9 Leader: 5 Replicas: 5,6 Isr: 5,6