前期收到的問題:
1、在Topology中我們可以指定spout、bolt的並行度,提交Topology時Storm如何將spout、bolt自動發布到每個伺服器並控制服務的CPU、磁碟等資源?
2、Storm處理消息時會依Topology生成一棵消息樹,Storm如何跟蹤每個消息、如何保證消息不丟失以及如何實現重發機制?
上篇《Storm是如何保證at least once語義的》回答了第2個問題。本篇建立一個基本背景,看看構成Storm流式計算能力的基礎框架,並部分回答第1個問題。
supervisor會定時從ZooKeeper獲取topologies、assignments及心跳信息作為分配依據;同步時依新分配啟動新worker或關閉舊worker並做負載均衡。worker透過定期更新connections得知應通訊的對象,啟動時依分配到的任務啟動一個或多個executor線程,且這些線程只處理唯一的topology。executor則負責處理多個spout或多個bolts(即tasks)。
具體有多少worker、多少executor、每個executor負責多少task,由配置與parallelism-hint共同決定,但這不一定等於實際運行數量;若計算出的executors總數超過nimbus限制,該topology將不會執行。
並行度相關的核心代碼:
(defn- compute-topology->executors [nimbus storm-ids] "compute a topology-id -> executors map" (into {} (for [tid storm-ids] {tid (set (compute-executors nimbus tid))})))
(defn- compute-executors [nimbus storm-id] (let [conf (:conf nimbus) storm-base (.storm-base (:storm-cluster-state nimbus) storm-id nil) component->executors (:component->executors storm-base) storm-conf (read-storm-conf conf storm-id) topology (read-storm-topology conf storm-id) task->component (storm-task-info topology storm-conf)] (->> (storm-task-info topology storm-conf) reverse-map (map-val sort) (join-maps component->executors) (map-val (partial apply partition-fixed)) (mapcat second) (map to-executor-id) )))
(defn storm-task-info "Returns map from task -> component id" [^StormTopology user-topology storm-conf] (->> (system-topology! storm-conf user-topology) all-components (map-val (comp #(get % TOPOLOGY-TASKS) component-conf)) (sort-by first) (mapcat (fn [[c num-tasks]] (repeat num-tasks c))) (map (fn [id comp] [id comp]) (iterate (comp int inc) (int 1))) (into {}) ))
上述代碼會在nimbus進行任務分配時調用:mk-assignments → compute-new-topology->executor->node+port → compute-topology->executors → ...
所謂本地發布,指在worker進程內及executor線程間發布消息;所謂遠程發布,指在worker進程間、不同機器間發布消息。
集群狀態由storm-cluster-state物件描述,提供ZooKeeper基本操作(create-node、set-data、remove-node、get-children等)、心跳接口(supervisor-heartbeat!、worker-heatbeat!等)、心跳信息(executors-beats等)以及storm的啟動、更新、停止(update-storm!等)。
依據:ZooKeeper是集群狀態同步與協調的核心;supervisor、worker、executor定期寫心跳;topology出錯或有新topology提交時,topologies信息會同步;nimbus定期監視assignments並把重新分配的計劃同步回ZooKeeper。
時機:rebalance/do-rebalance會觸發mk-assignments;nimbus啟動後會週期性呼叫mk-assignments;客戶端透過 storm jar ... topology 提交時會經thrift調用nimbus並觸發分配。
以上基本闡述了Storm的基礎框架,但未涉及trident機制,也基本回答了問題1。終。