Twitter Storm中Topology的状态

阅读更多

Twitter Storm中Topology的状态

状态转换如下,Topology 的持久化状态包括: active, inactive, killed, rebalancing 四个状态。

代码上看到每种状态都可以转换成一些持久化 ( 写入到 zk 中的状态 ) 或者中间状态。

(defn state-transitions [nimbus storm-id status]
  {:active {:monitor (reassign-transition nimbus storm-id)
            :inactivate :inactive            
            :activate nil
            :rebalance (rebalance-transition nimbus storm-id status)
            :kill (kill-transition nimbus storm-id)
            }
   :inactive {:monitor (reassign-transition nimbus storm-id)
              :activate :active
              :inactivate nil
              :rebalance (rebalance-transition nimbus storm-id status)
              :kill (kill-transition nimbus storm-id)
              }
   :killed {:startup (fn [] (delay-event nimbus
                                         storm-id
                                         (:kill-time-secs status)
                                         :remove))
            :kill (kill-transition nimbus storm-id)
            :remove (fn []
                      (log-message "Killing topology: " storm-id)
                      (.remove-storm! (:storm-cluster-state nimbus)
                                      storm-id)
                      nil)
            }
   :rebalancing {:startup (fn [] (delay-event nimbus
                                              storm-id
                                              (:delay-secs status)
                                              :do-rebalance))
                 :kill (kill-transition nimbus storm-id)
                 :do-rebalance (fn []
                                 (do-rebalance nimbus storm-id status)
                                 (:old-status status))
                 }})

      1. active

 

active 状态的时候可以转换成 monitor, inactivate, activate, rebalance, kill

(1) monitor: 转换成 monitor 实际上是执行了 reassign-transition 操作:

(defn reassign-transition [nimbus storm-id]
  (fn []
    (reassign-topology nimbus storm-id)
    nil
    ))

 

可以看出,实际上是为这个 topology 重新分配任务,返回值为 nil 说明在 zk 中不会更改 topology 的持久化状态。

(2)inactivate: 返回值是 inactive, 状态转换的时候会将 zk topology 的状态转换成 inactive

(3)activate: nil 说明什么操作都不做

(4)rebalance: 实际上是调用了 rebalance-transition 函数,从代码可以看出,会将状态改成 rebalancing, 然后再转换成 do-rebalance do-rebalance 其实也是重新分配任务,具体看4

(defn rebalance-transition [nimbus storm-id status]
  (fn [time num-workers executor-overrides]
    (let [delay (if time
                  time
                  (get (read-storm-conf (:conf nimbus) storm-id)
                       TOPOLOGY-MESSAGE-TIMEOUT-SECS))]
      (delay-event nimbus
                   storm-id
                   delay
                   :do-rebalance)
      {:type :rebalancing
       :delay-secs delay
       :old-status status
       :num-workers num-workers
       :executor-overrides executor-overrides
       })))

 (5)kill: 实际上执行的是 kill-transition 方法,将 topology 的状态先改为 killed, 然后经过 kill-time 的时间,将 topology remove, 详见3 

(defn kill-transition [nimbus storm-id]
  (fn [kill-time]
    (let [delay (if kill-time
                  kill-time
                  (get (read-storm-conf (:conf nimbus) storm-id)
                       TOPOLOGY-MESSAGE-TIMEOUT-SECS))]
      (delay-event nimbus
                   storm-id
                   delay
                   :remove)
      {:type :killed
       :kill-time-secs delay})
    ))

  

2. inactvie

(1) monitor: 与1中相同

(2) activate: 返回值是 active, 状态转换的时候会将 zk topology 的状态转换成 active

(3) inactivate: nil 说明什么操作都不做

(4) rebalance: 与1中相同

 

3. killed

(1) startup:将状态转换成remove

(2) kill:  与1中相同

(3) remove:   实际上是调用了 remove-storm!函数, 清楚topology在zk上的相关目录。

 (remove-storm! [this storm-id]
   (delete-node cluster-state (storm-task-root storm-id))
   (delete-node cluster-state (assignment-path storm-id))
   (remove-storm-base! this storm-id))

 
 4. rebalancing

 

(1) startup:将状态转换成do-rebalance

(2) kill:  与1中相同

(3) do-rebalance:  实际上是重新将任务分配,与初始分配任务不同,它假设所有的任务都是活跃的,所有的端口都不要判断是否需要保留,也就是说所有的任务重新分配,无论某些端口上的任务分配已经满足均衡要求。

你可能感兴趣的:(实时计算,storm,nimbus)