Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Apache Storm基礎框架剖析

背景

前期收到的問題:

1、在Topology中我們可以指定spout、bolt的並行度,提交Topology時Storm如何將spout、bolt自動發布到每個伺服器並控制服務的CPU、磁碟等資源?

2、Storm處理消息時會依Topology生成一棵消息樹,Storm如何跟蹤每個消息、如何保證消息不丟失以及如何實現重發機制?

上篇《Storm是如何保證at least once語義的》回答了第2個問題。本篇建立一個基本背景,看看構成Storm流式計算能力的基礎框架,並部分回答第1個問題。

worker、executor、task的關係

  1. worker是一個進程。
  2. executor是一個線程,是運行tasks的物理容器。
  3. task是對spout/bolt/acker等任務的邏輯抽象。

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 → ...

線程模型及消息系統

  1. worker啟動時,除了啟動多個executor線程,還會啟動多個工作線程負責消息傳遞。
  2. worker會訂閱transfer-queue來消費消息,也會發布消息到transfer-queue(例如需要遠程發布時)。
  3. executor會發布消息到executor-send-queue(如emit tuple),並從executor-receive-queue消費消息(如執行ack或fail)。
  4. batch-transfer-worker-handler線程訂閱executor-send-queue消費消息,再發布到transfer-queue供worker消費。
  5. transfer-thread訂閱transfer-queue消費消息,並透過socket發送到遠程節點端口。
  6. worker透過receive-thread收取遠程消息,以本地方式發布到指定executor對應的executor-receive-queue,executor再依第3點消費。
  7. 以上所有消息隊列都是Disruptor Queue,這是一個非常高效的線程間通訊框架。

所謂本地發布,指在worker進程內及executor線程間發布消息;所謂遠程發布,指在worker進程間、不同機器間發布消息。

任務調度及負載均衡

任務調度的主要角色

  1. nimbus把可工作的worker稱為worker-slot。
  2. nimbus是集群控管核心,負責topology提交、狀態監控、負載均衡與任務重新分配。它分配的任務包含topology代碼路徑、tasks、executors及workers信息,worker由node + port唯一確定。
  3. supervisor負責實際同步worker的操作,一個supervisor即一個node,透過把topology代碼從nimbus下載到本地來調度任務。
  4. 任務分配信息包含task到worker的映射(task -> node + host),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並觸發分配。

topology提交過程

  1. 非本地模式下,客戶端透過thrift調用nimbus接口上傳代碼並觸發提交。
  2. nimbus進行任務分配並同步到ZooKeeper。
  3. supervisor定期獲取分配信息,若代碼缺失就從nimbus下載,並據分配同步worker。
  4. worker依分配的tasks啟動多個executor線程,實例化spout、bolt、acker等組件,待所有connections啟動完畢,storm-cluster即進入工作狀態。
  5. 除非顯式呼叫kill topology,否則spout、bolt等組件會一直運行。

結語

以上基本闡述了Storm的基礎框架,但未涉及trident機制,也基本回答了問題1。終。

Copyright © Linux教程網 All Rights Reserved