一文帶你瞭解 Kafka 原理

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果只是爲了開發 Kafka 應用程序,或者只是在生產環境使用 Kafka,那麼瞭解 Kafka 的內部工作原理不是必須的。不過,瞭解 Kafka 的內部工作原理有助於理解 Kafka 的行爲,也利用快速診斷問題。下面我們來探討一下這三個問題"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 是如何進行復制的"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 是如何處理來自生產者和消費者的請求的"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 的存儲細節是怎樣的"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果感興趣的話,就請花費你一些時間,耐心看完這篇文章。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"集羣成員間的關係"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們知道,Kafka 是運行在 ZooKeeper 之上的,因爲 ZooKeeper 是以集羣形式出現的,所以 Kafka 也可以以集羣形式出現。這也就涉及到多個生產者和多個消費者如何協調的問題,這個維護集羣間的關係也是由 ZooKeeper 來完成的。如果你看過我之前的文章("},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247484768&idx=1&sn=724ebf1ecbb2e9df677242dec1ab217b&chksm=fc45f893cb327185db43ea9363928d71c54c62b1d9048f759671de3737d62e0e2d265289b354&token=562128166&lang=zh_CN#rd","title":null},"content":[{"type":"text","text":"真的,關於 Kafka 入門看這一篇就夠了"}],"marks":[{"type":"underline"}]},{"type":"text","text":"),你應該會知道,Kafka 集羣間會有多個 "},{"type":"codeinline","content":[{"type":"text","text":"主機(broker)"}]},{"type":"text","text":",每個 broker 都會有一個 "},{"type":"codeinline","content":[{"type":"text","text":"broker.id"}]},{"type":"text","text":",每個 broker.id 都有一個唯一的標識符用來區分,這個標識符可以在配置文件裏手動指定,也可以自動生成。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 可以通過 broker.id.generation.enable 和 reserved.broker.max.id 來配合生成新的 broker.id。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"broker.id.generation.enable參數是用來配置是否開啓自動生成 broker.id 的功能,默認情況下爲true,即開啓此功能。自動生成的broker.id有一個默認值,默認值爲1000,也就是說默認情況下自動生成的 broker.id 從1001開始。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 在啓動時會在 ZooKeeper 中 "},{"type":"codeinline","content":[{"type":"text","text":"/brokers/ids"}]},{"type":"text","text":" 路徑下注冊一個與當前 broker 的 id 相同的臨時節點。Kafka 的健康狀態檢查就依賴於此節點。當有 broker 加入集羣或者退出集羣時,這些組件就會獲得通知。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你要啓動另外一個具有相同 ID 的 broker,那麼就會得到一個錯誤 —— 新的 broker 會試着進行註冊,但不會成功,因爲 ZooKeeper 裏面已經有一個相同 ID 的 broker。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 broker 停機、出現分區或者長時間垃圾回收停頓時,broker 會從 ZooKeeper 上斷開連接,此時 broker 在啓動時創建的臨時節點會從 ZooKeeper 中移除。監聽 broker 列表的 Kafka 組件會被告知該 broker 已移除。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在關閉 broker 時,它對應的節點也會消失,不過它的 ID 會繼續存在其他數據結構中,例如主題的副本列表中,副本列表複製我們下面再說。在完全關閉一個 broker 之後,如果使用相同的 ID 啓動另一個全新的 broker,它會立刻加入集羣,並擁有一個與舊 broker 相同的分區和主題。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Broker Controller 的作用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們之前在講 Kafka Rebalance 重平衡的時候,提過一個羣組協調器,負責協調羣組間的關係,那麼 broker 之間也有一個"},{"type":"text","marks":[{"type":"strong"}],"text":"控制器組件(Controller),它是 Kafka 的核心組件。它的主要作用是在 ZooKeeper 的幫助下管理和協調整個 Kafka 集羣"},{"type":"text","text":",集羣中的每個 broker 都可以稱爲 controller,但是在 Kafka 集羣啓動後,只有一個 broker 會成爲 Controller 。既然 Kafka 集羣是依賴於 ZooKeeper 集羣的,所以有必要先介紹一下 ZooKeeper 是什麼,可以參考作者的這一篇文章("},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247484551&idx=1&sn=17f99fa32ea50a22eef5bb6fbef37a51&chksm=fc45f974cb32706258aa9bca165c3f999e3d719cb58e918dd1fd552df803380a431719ba4a46&token=2051356665&lang=zh_CN#rd","title":null},"content":[{"type":"text","text":"ZooKeeper不僅僅是註冊中心,你還知道有哪些?"}],"marks":[{"type":"underline"}]},{"type":"text","text":")詳細瞭解,在這裏就簡單提一下 "},{"type":"codeinline","content":[{"type":"text","text":"znode"}]},{"type":"text","text":" 節點的問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ZooKeeper 的數據是保存在節點上的,每個節點也被稱爲"},{"type":"codeinline","content":[{"type":"text","text":"znode"}]},{"type":"text","text":",znode 節點是一種樹形的文件結構,它很像 Linux 操作系統的文件路徑,ZooKeeper 的根節點是 "},{"type":"codeinline","content":[{"type":"text","text":"/"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f72c94d9964dbe27b8bc0151018aff13.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"znode 根據數據的持久化方式可分爲臨時節點和持久性節點。持久性節點不會因爲 ZooKeeper 狀態的變化而消失,但是臨時節點會隨着 ZooKeeper 的重啓而自動消失。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"znode 節點有一個 "},{"type":"codeinline","content":[{"type":"text","text":"Watcher"}]},{"type":"text","text":" 機制:當數據發生變化的時候, ZooKeeper 會產生一個 Watcher 事件,並且會發送到客戶端。Watcher 監聽機制是 Zookeeper 中非常重要的特性,我們基於 Zookeeper 上創建的節點,可以對這些節點綁定監聽事件,比如可以監聽節點數據變更、節點刪除、子節點狀態變更等事件,通過這個事件機制,可以基於 ZooKeeper 實現分佈式鎖、集羣管理等功能。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"控制器的選舉"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 當前選舉控制器的規則是:Kafka 集羣中第一個啓動的 broker 通過在 ZooKeeper 裏創建一個臨時節點 "},{"type":"codeinline","content":[{"type":"text","text":"/controller"}]},{"type":"text","text":" 讓自己成爲 controller 控制器。其他 broker 在啓動時也會嘗試創建這個節點,但是由於這個節點已存在,所以後面想要創建 /controller 節點時就會收到一個 "},{"type":"text","marks":[{"type":"strong"}],"text":"節點已存在"},{"type":"text","text":" 的異常。然後其他 broker 會在這個控制器上註冊一個 ZooKeeper 的 watch 對象,"},{"type":"codeinline","content":[{"type":"text","text":"/controller"}]},{"type":"text","text":"節點發生變化時,其他 broker 就會收到節點變更通知。這種方式可以確保只有一個控制器存在。那麼只有單獨的節點一定是有個問題的,那就是"},{"type":"codeinline","content":[{"type":"text","text":"單點問題"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8c/8cae515274897a37d1b1015a79bea746.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果控制器關閉或者與 ZooKeeper 斷開鏈接,ZooKeeper 上的臨時節點就會消失。集羣中的其他節點收到 watch 對象發送控制器下線的消息後,其他 broker 節點都會嘗試讓自己去成爲新的控制器。其他節點的創建規則和第一個節點的創建原則一致,都是第一個在 ZooKeeper 裏成功創建控制器節點的 broker 會成爲新的控制器,那麼其他節點就會收到節點已存在的異常,然後在新的控制器節點上再次創建 watch 對象進行監聽。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/68/6888e34672e327e6322dd4f04d5e5e47.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"控制器的作用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼說了這麼多,控制是什麼呢?控制器的作用是什麼呢?或者說控制器的這麼一個"},{"type":"codeinline","content":[{"type":"text","text":"組件"}]},{"type":"text","text":"被設計用來幹什麼?彆着急,接下來我們就要說一說。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 被設計爲一種模擬狀態機的多線程控制器,它可以作用有下面這幾點"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"控制器相當於部門(集羣)中的部門經理(broker controller),用於管理部門中的部門成員(broker)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"控制器是所有 broker 的一個監視器,用於監控 broker 的上線和下線"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 broker 宕機後,控制器能夠選舉新的分區 Leader"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"控制器能夠和 broker 新選取的 Leader 發送消息"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再細分一下可以具體分爲如下 5 點"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"主題管理"}]},{"type":"text","text":" : Kafka Controller 可以幫助我們完成對 Kafka 主題創建、刪除和增加分區的操作,簡而言之就是對分區擁有最高行使權。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"換句話說,當我們執行"},{"type":"text","marks":[{"type":"strong"}],"text":"kafka-topics 腳本"},{"type":"text","text":"時,大部分的後臺工作都是控制器來完成的。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"分區重分配"}]},{"type":"text","text":": 分區重分配主要是指,"},{"type":"text","marks":[{"type":"strong"}],"text":"kafka-reassign-partitions 腳本"},{"type":"text","text":"提供的對已有主題分區進行細粒度的分配功能。這部分功能也是控制器實現的。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Prefered 領導者選舉"}]},{"type":"text","text":" : Preferred 領導者選舉主要是 Kafka 爲了避免部分 Broker 負載過重而提供的一種換 Leader 的方案。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"集羣成員管理"}]},{"type":"text","text":": 主要管理 新增 broker、broker 關閉、broker 宕機"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"數據服務"}]},{"type":"text","text":": 控制器的最後一大類工作,就是向其他 broker 提供數據服務。控制器上保存了最全的集羣元數據信息,其他所有 broker 會定期接收控制器發來的元數據更新請求,從而更新其內存中的緩存數據。這些數據我們會在下面討論"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當控制器發現一個 broker 離開集羣(通過觀察相關 ZooKeeper 路徑),控制器會收到消息:這個 broker 所管理的那些分區需要一個新的 Leader。控制器會依次遍歷每個分區,確定誰能夠作爲新的 Leader,然後向所有包含新 Leader 或現有 Follower 的分區發送消息,該請求消息包含誰是新的 Leader 以及誰是 Follower 的信息。隨後,新的 Leader 開始處理來自生產者和消費者的請求,Follower 用於從新的 Leader 那裏進行復制。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這就很像外包公司的一個部門,這個部門就是專門出差的,每個人在不同的地方辦公,但是中央總部有一個部門經理,現在部門經理突然離職了。公司不打算外聘人員,決定從部門內部選一個能力強的人當領導,然後當上領導的人需要向自己的組員發送消息,這條消息就是任命消息和明確他管理了哪些人,大家都知道了,然後再各自給部門幹活。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當控制器發現一個 broker 加入集羣時,它會使用 broker ID 來檢查新加入的 broker 是否包含現有分區的副本。如果有控制器就會把消息發送給新加入的 broker 和 現有的 broker。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面這塊關於分區複製的內容我們接下來會說到。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"broker controller 數據存儲"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面我們介紹到 broker controller 會提供數據服務,用於保存大量的 Kafka 集羣數據。如下圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/3a/3a7c04e7670b4c6845e9eff730273c25.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以對上面保存信息歸類,主要分爲三類"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"broker 上的所有信息,包括 broker 中的所有分區,broker 所有分區副本,當前都有哪些運行中的 broker,哪些正在關閉中的 broker 。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所有主題信息,包括具體的分區信息,比如領導者副本是誰,ISR 集合中有哪些副本等。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所有涉及運維任務的分區。包括當前正在進行 Preferred 領導者選舉以及分區重分配的分區列表。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 是離不開 ZooKeeper的,所以這些數據信息在 ZooKeeper 中也保存了一份。每當控制器初始化時,它都會從 ZooKeeper 上讀取對應的元數據並填充到自己的緩存中。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"broker controller 故障轉移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在前面說過,第一個在 ZooKeeper 中的 "},{"type":"codeinline","content":[{"type":"text","text":"/brokers/ids"}]},{"type":"text","text":"下創建節點的 broker 作爲 broker controller,也就是說 broker controller 只有一個,那麼必然會存在單點失效問題。kafka 爲考慮到這種情況提供了"},{"type":"codeinline","content":[{"type":"text","text":"故障轉移"}]},{"type":"text","text":"功能,也就是 "},{"type":"codeinline","content":[{"type":"text","text":"Fail Over"}]},{"type":"text","text":"。如下圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4e/4e185c426bd350b6b319ae1b2d459016.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最一開始,broker1 會搶先註冊成功成爲 controller,然後由於網絡抖動或者其他原因致使 broker1 掉線,ZooKeeper 通過 Watch 機制覺察到 broker1 的掉線,之後所有存活的 brokers 開始競爭成爲 controller,這時 broker3 搶先註冊成功,此時 ZooKeeper 存儲的 controller 信息由 broker1 -> broker3,之後,broker3 會從 ZooKeeper 中讀取元數據信息,並初始化到自己的緩存中。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:ZooKeeper 中存儲的不是緩存信息,broker 中存儲的纔是緩存信息。"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"broker controller 存在的問題"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Kafka 0.11 版本之前,控制器的設計是相當繁瑣的。我們上面提到過一句話:Kafka controller 被設計爲一種模擬狀態機的多線程控制器,這種設計其實是存在一些問題的"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"controller 狀態的更改由不同的監聽器並罰執行,因此需要進行很複雜的同步,並且容易出錯而且難以調試。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"狀態傳播不同步,broker 可能在時間不確定的情況下出現多種狀態,這會導致不必要的額外的數據丟失"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"controller 控制器還會爲主題刪除創建額外的 I/O 線程,導致性能損耗"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"controller 的多線程設計還會訪問共享數據,我們知道,多線程訪問共享數據是線程同步最麻煩的地方,爲了保護數據安全性,控制器不得不在代碼中大量使用"},{"type":"text","marks":[{"type":"strong"}],"text":"ReentrantLock 同步機制"},{"type":"text","text":",這就進一步拖慢了整個控制器的處理速度。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"broker controller 內部設計原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Kafka 0.11 之後,Kafka controller 採用了新的設計,"},{"type":"text","marks":[{"type":"strong"}],"text":"把多線程的方案改成了單線程加事件隊列的方案"},{"type":"text","text":"。如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/77/77f4d698992cfeb5d24e7bdd6a8e5510.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主要所做的改變有下面這幾點"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一個改進是增加了一個 "},{"type":"codeinline","content":[{"type":"text","text":"Event Executor Thread"}]},{"type":"text","text":",事件執行線程,從圖中可以看出,不管是 Event Queue 事件隊列還是 Controller context 控制器上下文都會交給事件執行線程進行處理。將原來執行的操作全部建模成一個個獨立的事件,發送到專屬的事件隊列中,供此線程消費。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第二個改進是將之前同步的 ZooKeeper 全部改爲"},{"type":"codeinline","content":[{"type":"text","text":"異步操作"}]},{"type":"text","text":"。ZooKeeper API 提供了兩種讀寫的方式:同步和異步。之前控制器操作 ZooKeeper 都是採用的同步方式,這次把同步方式改爲異步,據測試,效率提升了10倍。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第三個改進是根據優先級處理請求,之前的設計是 broker 會公平性的處理所有 controller 發送的請求。什麼意思呢?公平性難道還不好嗎?在某些情況下是的,比如 broker 在排隊處理 produce 請求,這時候 controller 發出了一個 "},{"type":"text","marks":[{"type":"strong"}],"text":"StopReplica"},{"type":"text","text":" 的請求,你會怎麼辦?還在繼續處理 produce 請求嗎?這個 produce 請求還有用嗎?此時最合理的處理順序應該是,"},{"type":"text","marks":[{"type":"strong"}],"text":"賦予 StopReplica 請求更高的優先級,使它能夠得到搶佔式的處理。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"副本機制"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"複製功能是 Kafka 架構的核心功能,在 Kafka 文檔裏面 Kafka 把自己描述爲 "},{"type":"text","marks":[{"type":"strong"}],"text":"一個分佈式的、可分區的、可複製的提交日誌服務"},{"type":"text","text":"。複製之所以這麼關鍵,是因爲消息的持久存儲非常重要,這能夠保證在主節點宕機後依舊能夠保證 Kafka 高可用。副本機制也可以稱爲"},{"type":"codeinline","content":[{"type":"text","text":"備份機制(Replication)"}]},{"type":"text","text":",通常指分佈式系統在多臺網絡交互的機器上保存有相同的數據備份/拷貝。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 使用主題來組織數據,每個主題又被分爲若干個分區,分區會部署在一到多個 broker 上,每個分區都會有多個副本,所以副本也會被保存在 broker 上,每個 broker 可能會保存成千上萬個副本。下圖是一個副本複製示意圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/86/8621c4569ececbdb2b6b826f8b1ded7d.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖所示,爲了簡單我只畫出了兩個 broker ,每個 broker 指保存了一個 Topic 的消息,在 broker1 中分區0 是Leader,它負責進行分區的複製工作,把 broker1 中的分區0複製一個副本到 broker2 的主題 A 的分區0。同理,主題 A 的分區1也是一樣的道理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"副本類型分爲兩種:一種是 "},{"type":"codeinline","content":[{"type":"text","text":"Leader(領導者)"}]},{"type":"text","text":" 副本,一種是"},{"type":"codeinline","content":[{"type":"text","text":"Follower(跟隨者)"}]},{"type":"text","text":"副本。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Leader 副本"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 在創建分區的時候都要選舉一個副本,這個選舉出來的副本就是 Leader 領導者副本。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Follower 副本"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了 Leader 副本以外的副本統稱爲 "},{"type":"codeinline","content":[{"type":"text","text":"Follower 副本"}]},{"type":"text","text":",Follower 不對外提供服務。下面是 Leader 副本的工作方式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b9/b98ff90d3ba8417a02be84b244c31320.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這幅圖需要注意以下幾點"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 中,Follower 副本也就是追隨者副本是不對外提供服務的。這就是說,任何一個追隨者副本都不能響應消費者和生產者的請求。所有的請求都是由領導者副本來處理。或者說,所有的請求都必須發送到 Leader 副本所在的 broker 中,Follower 副本只是用做數據拉取,採用"},{"type":"codeinline","content":[{"type":"text","text":"異步拉取"}]},{"type":"text","text":"的方式,並寫入到自己的提交日誌中,從而實現與 Leader 的同步"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當 Leader 副本所在的 broker 宕機後,Kafka 依託於 ZooKeeper 提供的監控功能能夠實時感知到,並開啓新一輪的選舉,從追隨者副本中選一個作爲 Leader。如果宕機的 broker 重啓完成後,該分區的副本會作爲 Follower 重新加入。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首領的另一個任務是搞清楚哪個跟隨者的狀態與自己是一致的。跟隨者爲了保證與領導者的狀態一致,在有新消息到達之前先嚐試從領導者那裏複製消息。爲了與領導者保持一致,跟隨者向領導者發起獲取數據的請求,這種請求與消費者爲了讀取消息而發送的信息是一樣的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟隨者向領導者發送消息的過程是這樣的,先請求消息1,然後再接收到消息1,在時候到請求1之後,發送請求2,在收到領導者給發送給跟隨者之前,跟隨者是不會繼續發送消息的。這個過程如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f3/f3dcfc2d79c1bbbae5ded7daf9a31778.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟隨者副本在收到響應消息前,是不會繼續發送消息,這一點很重要。通過查看每個跟隨者請求的最新偏移量,首領就會知道每個跟隨者複製的進度。如果跟隨者在10s 內沒有請求任何消息,或者雖然跟隨者已經發送請求,但是在10s 內沒有收到消息,就會被認爲是"},{"type":"codeinline","content":[{"type":"text","text":"不同步"}]},{"type":"text","text":"的。如果一個副本沒有與領導者同步,那麼在領導者掉線後,這個副本將不會稱爲領導者,因爲這個副本的消息不是全部的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與之相反的,如果跟隨者同步的消息和領導者副本的消息一致,那麼這個跟隨者副本又被稱爲"},{"type":"codeinline","content":[{"type":"text","text":"同步的副本"}]},{"type":"text","text":"。也就是說,如果領導者掉線,那麼只有同步的副本能夠稱爲領導者。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關於副本機制我們說了這麼多,那麼副本機制的好處是什麼呢?"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能夠立刻看到寫入的消息,就是你使用生產者 API 成功向分區寫入消息後,馬上使用消費者就能讀取剛纔寫入的消息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能夠實現消息的冪等性,啥意思呢?就是對於生產者產生的消息,在消費者進行消費的時候,它每次都會看到消息存在,並不會存在消息不存在的情況"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"同步複製和異步複製"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我在學習副本機制的時候,有個疑問,既然領導者副本和跟隨者副本是"},{"type":"codeinline","content":[{"type":"text","text":"發送 - 等待"}]},{"type":"text","text":"機制的,這是一種同步的複製方式,那麼爲什麼說跟隨者副本同步領導者副本的時候是一種異步操作呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我認爲是這樣的,跟隨者副本在同步領導者副本後會把消息保存在本地 log 中,這個時候跟隨者會給領導者副本一個響應消息,告訴領導者自己已經保存成功了,同步複製的領導者會等待所有的跟隨者副本都寫入成功後,再返回給 producer 寫入成功的消息。而異步複製是領導者副本不需要關心跟隨者副本是否寫入成功,只要領導者副本自己把消息保存到本地 log ,就會返回給 producer 寫入成功的消息。下面是同步複製和異步複製的過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"同步複製"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"producer 通知 ZooKeeper 識別領導者"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"producer 向領導者寫入消息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領導者收到消息後會把消息寫入到本地 log"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟隨者會從領導者那裏拉取消息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟隨者向本地寫入 log"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"跟隨者向領導者發送寫入成功的消息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領導者會收到所有的跟隨者發送的消息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領導者向 producer 發送寫入成功的消息"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"異步複製"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"和同步複製的區別在於,領導者在寫入本地log之後,直接向客戶端發送寫入成功消息,不需要等待所有跟隨者複製完成。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"ISR"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka動態維護了一個同步狀態的副本的集合"},{"type":"codeinline","content":[{"type":"text","text":"(a set of In-Sync Replicas),簡稱ISR"}]},{"type":"text","text":",ISR 也是一個很重要的概念,我們之前說過,追隨者副本不提供服務,只是定期的異步拉取領導者副本的數據而已,拉取這個操作就相當於是複製,"},{"type":"codeinline","content":[{"type":"text","text":"ctrl-c + ctrl-v"}]},{"type":"text","text":"大家肯定用的熟。那麼是不是說 ISR 集合中的副本消息的數量都會與領導者副本消息數量一樣呢?那也不一定,判斷的依據是 broker 中參數 "},{"type":"codeinline","content":[{"type":"text","text":"replica.lag.time.max.ms"}]},{"type":"text","text":" 的值,這個參數的含義就是跟隨者副本能夠落後領導者副本最長的時間間隔。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"replica.lag.time.max.ms 參數默認的時間是 10秒,如果跟隨者副本落後領導者副本的時間不超過 10秒,那麼 Kafka 就認爲領導者和跟隨者是同步的。即使此時跟隨者副本中存儲的消息要小於領導者副本。如果跟隨者副本要落後於領導者副本 10秒以上的話,跟隨者副本就會從 ISR 被剔除。倘若該副本後面慢慢地追上了領導者的進度,那麼它是能夠重新被加回 ISR 的。這也表明,ISR 是一個動態調整的集合,而非靜態不變的。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Unclean 領導者選舉"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然 ISR 是可以動態調整的,那麼必然會出現 ISR 集合中爲空的情況,由於領導者副本是一定出現在 ISR 集合中的,那麼 ISR 集合爲空必然說明領導者副本也掛了,所以此時 Kafka 需要重新選舉一個新的領導者,那麼該如何選舉呢?現在你需要轉變一下思路,我們上面說 ISR 集合中一定是與領導者同步的副本,那麼不再 ISR 集合中的副本一定是不與領導者同步的副本了,也就是不再 ISR 列表中的跟隨者副本會丟失一些消息。如果你開啓 broker 端參數 "},{"type":"codeinline","content":[{"type":"text","text":"unclean.leader.election.enable"}]},{"type":"text","text":"的話,下一個領導者就會在這些非同步的副本中選舉。這種選舉也叫做"},{"type":"codeinline","content":[{"type":"text","text":"Unclean 領導者選舉"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你接觸過分佈式項目的話你一定知道 CAP 理論,那麼這種 Unclean 領導者選舉其實是犧牲了數據一致性,保證了 Kafka 的高可用性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你可以根據你的實際業務場景決定是否開啓 Unclean 領導者選舉,一般不建議開啓這個參數,因爲數據的一致性要比可用性重要的多。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Kafka 請求處理流程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"broker 的大部分工作是處理客戶端、分區副本和控制器發送給分區領導者的請求。這種請求一般都是"},{"type":"codeinline","content":[{"type":"text","text":"請求/響應"}]},{"type":"text","text":"式的,我猜測你接觸最早的請求/響應的方式應該就是 HTTP 請求了。事實上,HTTP 請求可以是同步可以是異步的。一般正常的 HTTP 請求都是同步的,同步方式最大的一個特點是"},{"type":"text","marks":[{"type":"strong"}],"text":"提交請求->等待服務器處理->處理完畢返回 這個期間客戶端瀏覽器不能做任何事"},{"type":"text","text":"。而異步方式最大的特點是 "},{"type":"text","marks":[{"type":"strong"}],"text":"請求通過事件觸發->服務器處理(這是瀏覽器仍然可以作其他事情)-> 處理完畢"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我也可以說同步請求就是順序處理的,而異步請求的執行方式則不確定,因爲異步需要創建多個執行線程,而每個線程的執行順序不同。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏需要注意一點,我們只是使用 HTTP 請求來舉例子,而 Kafka 採用的是 TCP 基於 Socket 的方式進行通訊"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼這兩種方式有什麼缺點呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我相信聰明的你應該能馬上想到,同步的方式最大的缺點就是"},{"type":"codeinline","content":[{"type":"text","text":"吞吐量太差"}]},{"type":"text","text":",資源利用率極低,由於只能順序處理請求,因此,每個請求都必須等待前一個請求處理完畢才能得到處理。這種方式只適用於"},{"type":"codeinline","content":[{"type":"text","text":"請求發送非常不頻繁的系統"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"異步的方式的缺點就是爲每個請求都創建線程的做法開銷極大,在某些場景下甚至會壓垮整個服務。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"響應式模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"說了這麼半天,Kafka 採用同步還是異步的呢?都不是,Kafka 採用的是一種 "},{"type":"codeinline","content":[{"type":"text","text":"響應式(Reactor)模型"}]},{"type":"text","text":",那麼什麼是響應式模型呢?簡單的說,"},{"type":"text","marks":[{"type":"strong"}],"text":"Reactor 模式是事件驅動架構的一種實現方式,特別適合應用於處理多個客戶端併發向服務器端發送請求的場景"},{"type":"text","text":",如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cf/cf62f143928197cfcae3b19ee6588a55.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 的 broker 端有個 SocketServer組件,類似於處理器,SocketServer 是基於 TCP 的 Socket 連接的,它用於接受客戶端請求,所有的請求消息都包含一個消息頭,消息頭中都包含如下信息"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Request type (也就是 API Key)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Request version(broker 可以處理不同版本的客戶端請求,並根據客戶版本做出不同的響應)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Correlation ID --- 一個具有唯一性的數字,用於標示請求消息,同時也會出現在響應消息和錯誤日誌中(用於診斷問題)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Client ID --- 用於標示發送請求的客戶端"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"broker 會在它所監聽的每一個端口上運行一個 "},{"type":"codeinline","content":[{"type":"text","text":"Acceptor"}]},{"type":"text","text":" 線程,這個線程會創建一個連接,並把它交給 "},{"type":"codeinline","content":[{"type":"text","text":"Processor(網絡線程池)"}]},{"type":"text","text":", Processor 的數量可以使用 "},{"type":"codeinline","content":[{"type":"text","text":"num.network.threads"}]},{"type":"text","text":" 進行配置,其默認值是3,表示每臺 broker 啓動時會創建3個線程,專門處理客戶端發送的請求。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Acceptor 線程會採用"},{"type":"codeinline","content":[{"type":"text","text":"輪詢"}]},{"type":"text","text":"的方式將入棧請求公平的發送至網絡線程池中,因此,在實際使用過程中,這些線程通常具有相同的機率被分配到待處理"},{"type":"codeinline","content":[{"type":"text","text":"請求隊列"}]},{"type":"text","text":"中,然後從"},{"type":"codeinline","content":[{"type":"text","text":"響應隊列"}]},{"type":"text","text":"獲取響應消息,把它們發送給客戶端。Processor 網絡線程池中的請求 - 響應的處理還是比較複雜的,下面是網絡線程池中的處理流程圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/34/3455a506428f74274c8a12ecd8f4a2c5.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Processor 網絡線程池接收到客戶和其他 broker 發送來的消息後,網絡線程池會把消息放到請求隊列中,注意這個是"},{"type":"codeinline","content":[{"type":"text","text":"共享請求隊列"}]},{"type":"text","text":",因爲網絡線程池是多線程機制的,所以請求隊列的消息是多線程共享的區域,然後由 IO 線程池進行處理,根據消息的種類判斷做何處理,比如 "},{"type":"codeinline","content":[{"type":"text","text":"PRODUCE"}]},{"type":"text","text":" 請求,就會將消息寫入到 log 日誌中,如果是"},{"type":"codeinline","content":[{"type":"text","text":"FETCH"}]},{"type":"text","text":"請求,則從磁盤或者頁緩存中讀取消息。也就是說,IO線程池是真正做判斷,處理請求的一個組件。在IO 線程池處理完畢後,就會判斷是放入"},{"type":"codeinline","content":[{"type":"text","text":"響應隊列"}]},{"type":"text","text":"中還是 "},{"type":"codeinline","content":[{"type":"text","text":"Purgatory"}]},{"type":"text","text":" 中,Purgatory 是什麼我們下面再說,現在先說一下響應隊列,響應隊列是每個線程所獨有的,因爲響應式模型中不會關心請求發往何處,因此把響應回傳的事情就交給每個線程了,所以也就不必共享了。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:IO 線程池可以通過 broker 端參數 "},{"type":"codeinline","content":[{"type":"text","text":"num.io.threads"}]},{"type":"text","text":" 來配置,默認的線程數是8,表示每臺 broker 啓動後自動創建 8 個IO 處理線程。"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"請求類型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是幾種常見的請求類型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"生產請求"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我在 "},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247484768&idx=1&sn=724ebf1ecbb2e9df677242dec1ab217b&chksm=fc45f893cb327185db43ea9363928d71c54c62b1d9048f759671de3737d62e0e2d265289b354&token=1263191457&lang=zh_CN#rd","title":null},"content":[{"type":"text","text":"真的,關於 Kafka 入門看這一篇就夠了"}],"marks":[{"type":"underline"}]},{"type":"text","text":" 文章中提到過 "},{"type":"codeinline","content":[{"type":"text","text":"acks"}]},{"type":"text","text":" 這個配置項的含義"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡單來講就是不同的配置對寫入成功的界定是不同的,如果 acks = 1,那麼只要領導者收到消息就表示寫入成功,如果acks = 0,表示只要領導者發送消息就表示寫入成功,根本不用考慮返回值的影響。如果 acks = all,就表示領導者需要收到所有副本的消息後才表示寫入成功。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在消息被寫入分區的首領後,如果 acks 配置的值是 "},{"type":"codeinline","content":[{"type":"text","text":"all"}]},{"type":"text","text":",那麼這些請求會被保存在 "},{"type":"codeinline","content":[{"type":"text","text":"煉獄(Purgatory)"}]},{"type":"text","text":"的緩衝區中,直到領導者副本發現跟隨者副本都複製了消息,響應纔會發送給客戶端。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"獲取請求"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"broker 獲取請求的方式與處理生產請求的方式類似,客戶端發送請求,向 broker 請求主題分區中特定偏移量的消息,如果偏移量存在,Kafka 會採用 "},{"type":"codeinline","content":[{"type":"text","text":"零複製"}]},{"type":"text","text":" 技術向客戶端發送消息,Kafka 會直接把消息從文件中發送到網絡通道中,而不需要經過任何的緩衝區,從而獲得更好的性能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端可以設置獲取請求數據的上限和下限,"},{"type":"codeinline","content":[{"type":"text","text":"上限"}]},{"type":"text","text":"指的是客戶端爲接受足夠消息分配的內存空間,這個限制比較重要,如果上限太大的話,很有可能直接耗盡客戶端內存。"},{"type":"codeinline","content":[{"type":"text","text":"下限"}]},{"type":"text","text":"可以理解爲攢足了數據包再發送的意思,這就相當於項目經理給程序員分配了 10 個bug,程序員每次改一個 bug 就會向項目經理彙報一下,有的時候改好了有的時候可能還沒改好,這樣就增加了溝通成本和時間成本,所以下限值得就是程序員你改完10個 bug 再向我彙報!!!如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/84/847ce87e0f9a347fa24d3fe5dced6dc6.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如圖你可以看到,在"},{"type":"codeinline","content":[{"type":"text","text":"拉取消息"}]},{"type":"text","text":" ---> "},{"type":"codeinline","content":[{"type":"text","text":"消息"}]},{"type":"text","text":" 之間是有一個等待消息積累這麼一個過程的,這個消息積累你可以把它想象成超時時間,不過超時會跑出異常,消息積累超時後會響應回執。延遲時間可以通過 "},{"type":"codeinline","content":[{"type":"text","text":"replica.lag.time.max.ms"}]},{"type":"text","text":" 來配置,它指定了副本在複製消息時可被允許的最大延遲時間。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"元數據請求"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"生產請求和響應請求都必須發送給領導者副本,如果 broker 收到一個針對某個特定分區的請求,而該請求的首領在另外一個 broker 中,那麼發送請求的客戶端會收到"},{"type":"codeinline","content":[{"type":"text","text":"非分區首領"}]},{"type":"text","text":"的錯誤響應;如果針對某個分區的請求被髮送到不含有領導者的 broker 上,也會出現同樣的錯誤。Kafka 客戶端需要把請求和響應發送到正確的 broker 上。這不是廢話麼?我怎麼知道要往哪發送?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事實上,客戶端會使用一種 "},{"type":"codeinline","content":[{"type":"text","text":"元數據請求"}]},{"type":"text","text":" ,這種請求會包含客戶端感興趣的主題列表,服務端的響應消息指明瞭主題的分區,領導者副本和跟隨者副本。元數據請求可以發送給任意一個 broker,因爲所有的 broker 都會緩存這些信息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般情況下,客戶端會把這些信息緩存,並直接向目標 broker 發送生產請求和相應請求,這些緩存需要隔一段時間就進行刷新,使用"},{"type":"codeinline","content":[{"type":"text","text":"metadata.max.age.ms"}]},{"type":"text","text":" 參數來配置,從而知道元數據是否發生了變更。比如,新的 broker 加入後,會觸發重平衡,部分副本會移動到新的 broker 上。這時候,如果客戶端收到 "},{"type":"codeinline","content":[{"type":"text","text":"不是首領"}]},{"type":"text","text":"的錯誤,客戶端在發送請求之前刷新元數據緩存。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Kafka 重平衡流程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我在  "},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzU2NDg0OTgyMA==&mid=2247484768&idx=1&sn=724ebf1ecbb2e9df677242dec1ab217b&chksm=fc45f893cb327185db43ea9363928d71c54c62b1d9048f759671de3737d62e0e2d265289b354&token=1263191457&lang=zh_CN#rd","title":null},"content":[{"type":"text","text":"真的,關於 Kafka 入門看這一篇就夠了"}],"marks":[{"type":"underline"}]},{"type":"text","text":" 中關於消費者描述的時候大致說了一下消費者組和重平衡之間的關係,實際上,歸納爲一點就是讓組內所有的消費者實例就消費哪些主題分區達成一致。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們知道,一個消費者組中是要有一個"},{"type":"codeinline","content":[{"type":"text","text":"羣組協調者(Coordinator)"}]},{"type":"text","text":"的,而重平衡的流程就是由 Coordinator 的幫助下來完成的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏需要先聲明一下重平衡發生的條件"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消費者訂閱的任何主題發生變化"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消費者數量發生變化"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分區數量發生變化"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你訂閱了一個還尚未創建的主題,那麼重平衡在該主題創建時發生。如果你訂閱的主題發生刪除那麼也會發生重平衡"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消費者被羣組協調器認爲是 "},{"type":"codeinline","content":[{"type":"text","text":"DEAD"}]},{"type":"text","text":" 狀態,這可能是由於消費者崩潰或者長時間處於運行狀態下發生的,這意味着在配置合理時間的範圍內,消費者沒有向羣組協調器發送任何心跳,這也會導致重平衡的發生。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"在瞭解重平衡之前,你需要知道這兩個角色"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"羣組協調器(Coordinator)"}]},{"type":"text","text":":羣組協調器是一個能夠從消費者羣組中收到所有消費者發送心跳消息的 broker。在最早期的版本中,元數據信息是保存在 ZooKeeper 中的,但是目前元數據信息存儲到了 broker 中。每個消費者組都應該和羣組中的羣組協調器同步。當所有的決策要在應用程序節點中進行時,羣組協調器可以滿足 "},{"type":"codeinline","content":[{"type":"text","text":"JoinGroup"}]},{"type":"text","text":" 請求並提供有關消費者組的元數據信息,例如分配和偏移量。羣組協調器還有權知道所有消費者的心跳,消費者羣組中還有一個角色就是領導者,注意把它和領導者副本和 kafka controller 進行區分。領導者是羣組中負責決策的角色,所以如果領導者掉線了,羣組協調器有權把所有消費者踢出組。因此,消費者羣組的一個很重要的行爲是選舉領導者,並與協調器讀取和寫入有關分配和分區的元數據信息。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"消費者領導者"}]},{"type":"text","text":": 每個消費者羣組中都有一個領導者。如果消費者停止發送心跳了,協調者會觸發重平衡。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"在瞭解重平衡之前,你需要知道狀態機是什麼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Kafka 設計了一套"},{"type":"codeinline","content":[{"type":"text","text":"消費者組狀態機(State Machine)"}]},{"type":"text","text":" ,來幫助協調者完成整個重平衡流程。消費者狀態機主要有五種狀態它們分別是 "},{"type":"text","marks":[{"type":"strong"}],"text":"Empty、Dead、PreparingRebalance、CompletingRebalance 和 Stable"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fe/fe4c2ae42e565654cc1c962f98084265.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"瞭解了這些狀態的含義之後,下面我們用幾條路徑來表示一下消費者狀態的輪轉"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消費者組一開始處於 "},{"type":"codeinline","content":[{"type":"text","text":"Empty"}]},{"type":"text","text":" 狀態,當重平衡開啓後,它會被置於 "},{"type":"codeinline","content":[{"type":"text","text":"PreparingRebalance"}]},{"type":"text","text":" 狀態等待新消費者的加入,一旦有新的消費者加入後,消費者羣組就會處於 "},{"type":"codeinline","content":[{"type":"text","text":"CompletingRebalance"}]},{"type":"text","text":" 狀態等待分配,只要有新的消費者加入羣組或者離開,就會觸發重平衡,消費者的狀態處於 PreparingRebalance 狀態。等待分配機制指定好後完成分配,那麼它的流程圖是這樣的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d3/d3e931087857ffd1a76736aa71908d39.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上圖的基礎上,當消費者羣組都到達 "},{"type":"codeinline","content":[{"type":"text","text":"Stable"}]},{"type":"text","text":" 狀態後,一旦有新的"},{"type":"text","marks":[{"type":"strong"}],"text":"消費者加入/離開/心跳過期"},{"type":"text","text":",那麼觸發重平衡,消費者羣組的狀態重新處於 PreparingRebalance 狀態。那麼它的流程圖是這樣的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e4/e4a4a1990a652b53e43c51af5a094246.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上圖的基礎上,消費者羣組處於 PreparingRebalance 狀態後,很不幸,沒人玩兒了,所有消費者都離開了,這時候還可能會保留有消費者消費的位移數據,一旦位移數據過期或者被刷新,那麼消費者羣組就處於 "},{"type":"codeinline","content":[{"type":"text","text":"Dead"}]},{"type":"text","text":" 狀態了。它的流程圖是這樣的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f8/f85b6955b9d884194c66383006ed7854.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上圖的基礎上,我們分析了消費者的重平衡,在 "},{"type":"codeinline","content":[{"type":"text","text":"PreparingRebalance"}]},{"type":"text","text":"或者 "},{"type":"codeinline","content":[{"type":"text","text":"CompletingRebalance"}]},{"type":"text","text":" 或者 "},{"type":"codeinline","content":[{"type":"text","text":"Stable"}]},{"type":"text","text":" 任意一種狀態下發生位移主題分區 Leader 發生變更,羣組會直接處於 Dead 狀態,它的所有路徑如下"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/bf/bf603bee69f75a53b4185af255fce49c.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏面需要注意兩點:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般出現 "},{"type":"text","marks":[{"type":"strong"}],"text":"Required xx expired offsets in xxx milliseconds"},{"type":"text","text":" 就表明Kafka 很可能就把該組的位移數據刪除了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只有 Empty 狀態下的組,纔會執行過期位移刪除的操作。"}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"重平衡流程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面我們瞭解到了消費者羣組狀態的轉化過程,下面我們真正開始介紹 "},{"type":"codeinline","content":[{"type":"text","text":"Rebalance"}]},{"type":"text","text":" 的過程。重平衡過程可以從兩個方面去看:消費者端和協調者端,首先我們先看一下消費者端"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"從消費者看重平衡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從消費者看重平衡有兩個步驟:分別是 "},{"type":"codeinline","content":[{"type":"text","text":"消費者加入組"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"等待領導者分配方案"}]},{"type":"text","text":"。這兩個步驟後分別對應的請求是 "},{"type":"codeinline","content":[{"type":"text","text":"JoinGroup"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"SyncGroup"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"新的消費者加入羣組時,這個消費者會向協調器發送 "},{"type":"codeinline","content":[{"type":"text","text":"JoinGroup"}]},{"type":"text","text":" 請求。在該請求中,每個消費者成員都需要將自己消費的 topic 進行提交,我們上面描述羣組協調器中說過,這麼做的目的就是爲了讓協調器收集足夠的元數據信息,來選取消費者組的領導者。通常情況下,第一個發送 JoinGroup 請求的消費者會自動稱爲領導者。"},{"type":"text","marks":[{"type":"strong"}],"text":"領導者的任務是收集所有成員的訂閱信息,然後根據這些信息,制定具體的分區消費分配方案"},{"type":"text","text":"。如圖"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/13/1348e4c238688f4398dcedd50c929e2a.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在所有的消費者都加入進來並把元數據信息提交給領導者後,領導者做出分配方案併發送 "},{"type":"codeinline","content":[{"type":"text","text":"SyncGroup "}]},{"type":"text","text":"請求給協調者,協調者負責下發羣組中的消費策略。下圖描述了 SyncGroup 請求的過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b3/b387dc3dad58b1d77d4e5b52c44ec451.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當所有成員都成功接收到分配方案後,消費者組進入到 Stable 狀態,即開始正常的消費工作。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"從協調者來看重平衡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從協調者角度來看重平衡主要有下面這幾種觸發條件,"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"新成員加入組"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"組成員主動離開"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"組成員崩潰離開"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"組成員提交位移"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們分別來描述一下,先從新成員加入組開始"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"新成員加入組"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們討論的場景消費者集羣狀態處於"},{"type":"codeinline","content":[{"type":"text","text":"Stable"}]},{"type":"text","text":" 等待分配的過程,這時候如果有新的成員加入組的話,重平衡的過程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7d/7d97933db762dbf0668a31e7c8116261.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從這個角度來看,協調者的過程和消費者類似,只是剛剛從消費者的角度去看,現在從領導者的角度去看"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"組成員離開"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"組成員離開消費者羣組指的是消費者實例調用 "},{"type":"codeinline","content":[{"type":"text","text":"close()"}]},{"type":"text","text":" 方法主動通知協調者它要退出。這裏又會有一個新的請求出現 "},{"type":"codeinline","content":[{"type":"text","text":"LeaveGroup()請求"}]},{"type":"text","text":" 。如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6f7ea124c13888138b31db89156e318e.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"組成員崩潰"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"組成員崩潰是指消費者實例出現嚴重故障,宕機或者一段時間未響應,協調者接收不到消費者的心跳,就會被認爲是"},{"type":"codeinline","content":[{"type":"text","text":"組成員崩潰"}]},{"type":"text","text":",崩潰離組是被動的,協調者通常需要等待一段時間才能感知到,這段時間一般是由消費者端參數 session.timeout.ms 控制的。如下圖所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/95/9520560097f18d19c2d26e0c9fc736f6.png","alt":"","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"重平衡時提交位移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個過程我們就不再用圖形來表示了,大致描述一下就是 消費者發送 JoinGroup 請求後,羣組中的消費者必須在指定的時間範圍內提交各自的位移,然後再開啓正常的 JoinGroup/SyncGroup 請求發送。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ea/ea41fd08fc091f4e2d07fd2ea18c8875.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章