会发现, 现在storm里面有两套metrics系统, metrics framework和stats framework

并且在所有地方都是同时注册两套, 貌似准备用metrics来替代stats, 但当前版本UI仍然使用stats

这个模块统计的数据怎么被使用,

1. 在worker中, 会定期调用do-executor-heartbeats去往zk同步hb
可以看到, stats也会作为hb的一部分被同步到zk上

(defnk do-executor-heartbeats [worker :executors nil]
;; stats is how we know what executors are assigned to this worker
(let [stats (if-not executors
(into {} (map (fn [e] {e nil}) (:executors worker)))
(->> executors
(map (fn [e] {(executor/get-executor-id e) (executor/render-stats e)}))
(apply merge)))
zk-hb {:storm-id (:storm-id worker)
:

executor-stats stats

               :uptime ((:uptime worker))
:time-secs (current-time-secs)
}]
;; do the zookeeper heartbeat
(.worker-heartbeat! (:storm-cluster-state worker) (:storm-id worker) (:assignment-id worker) (:port worker) zk-hb)
))

2. 现在任何人都可以通过nimbus的thrift接口来得到相关信息

(^TopologyInfo getTopologyInfo [this ^String storm-id]
beats (.executor-beats storm-cluster-state storm-id (:executor->node+port assignment))
stats (:stats heartbeat))

3. 最直接的用户就是storm UI, 在准备topology page的时候, 就会调用getTopologyInfo来获取数据

(defn topology-page [id window include-sys?]
(with-nimbus nimbus
(let [summ (.getTopologyInfo ^Nimbus$Client nimbus id)]
)

Stats

这个模块用于spout和bolt来抽样统计数据, 需要统计的具体metics如下

(def COMMON-FIELDS [:emitted :transferred])
(defrecord CommonStats [emitted transferred rate]) (def BOLT-FIELDS [:acked :failed :process-latencies :executed :execute-latencies])
;;acked and failed count individual tuples
(defrecord BoltExecutorStats [common acked failed process-latencies executed execute-latencies]) (def SPOUT-FIELDS [:acked :failed :complete-latencies])
;;acked and failed count tuple completion
(defrecord SpoutExecutorStats [common acked failed complete-latencies])

抽样的比例在storm-conf, TOPOLOGY_STATS_SAMPLE_RATE, 配置

为什么统计时每次加rate, 而不是加1?

因为这里的统计是抽样的, 所以如果抽样比例是10%, 那么发现一个, 应该加1/(10%), 10个

(defn sampling-rate [conf]
(->> (conf TOPOLOGY-STATS-SAMPLE-RATE)
(/ 1)
int))

然后统计是基于时间窗口的, 底下是对应默认的bucket和时间窗口的定义

(def NUM-STAT-BUCKETS 20) ;;bucket数
;; 10 minutes, 3 hours, 1 day ;;定义3种时间窗口
(def STAT-BUCKETS [30 540 4320]) ;;bucket大小分别是30,540,4320秒

核心数据结构是RollingWindowSet, 包含:

统计数据需要的函数, updater extractor, 之所以治理也需要是因为需要统计all-time 
一组rolling windows, 默认是3个时间窗, 10 minutes, 3 hours, 1 day

all-time, 在完整的时间区间上的统计结果

(defrecord RollingWindowSet [updater extractor windows all-time])
(defn rolling-window-set [updater merger extractor num-buckets & bucket-sizes]
(RollingWindowSet. updater extractor (dofor [s bucket-sizes] (rolling-window updater merger extractor s num-buckets)) nil)
)

继续看看rolling window的定义,

核心数据, buckets, hashmap, {streamid, data}, 初始化为{}

统计data需要的函数, updater merger extractor

时间窗口, buckets大小和buckets个数

(defrecord RollingWindow [updater merger extractor bucket-size-secs num-buckets buckets])
(defn rolling-window [updater merger extractor bucket-size-secs num-buckets]
(RollingWindow. updater merger extractor bucket-size-secs num-buckets {}))

1. mk-stats

在mk-executedata的时候需要创建stats

mk-executor-stats <> (sampling-rate storm-conf)
;; TODO: refactor this to be part of an executor-specific map
(defmethod mk-executor-stats :spout [_ rate]
(stats/mk-spout-stats rate))
(defmethod mk-executor-stats :bolt [_ rate]
(stats/mk-bolt-stats rate))

第一个参数忽略, 其实就是分别调用stats/mk-spout-stats或stats/mk-bolt-stats, 可见就是对于每个需要统计的数据, 创建一个rolling-windows-set

(defn- mk-common-stats [rate]
(CommonStats. (atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
rate
)) (defn mk-bolt-stats [rate]
(BoltExecutorStats. (mk-common-stats rate)
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-avg-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-avg-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
)) (defn mk-spout-stats [rate]
(SpoutExecutorStats. (mk-common-stats rate)
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-counter-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
(atom (apply keyed-avg-rolling-window-set NUM-STAT-BUCKETS STAT-BUCKETS))
))

2. 数据更新

(defn spout-acked-tuple! [^SpoutExecutorStats stats stream latency-ms]
(update-executor-stat! stats :acked stream (stats-rate stats))
(update-executor-stat! stats :complete-latencies stream latency-ms)
)
(defmacro update-executor-stat! [stats path & args]
(let [path (collectify path)]
`(swap! (-> ~stats ~@path) update-rolling-window-set ~@args)
))

就以update-executor-stat! stats :acked stream (stats-rate stats)为例子看看怎么做的?

SpoutExecutorStats取出用于记录spout acked情况的rolling-windows-set
然后使用update-rolling-window-set来swap这个atom

来看看记录acked的rolling-windows-set是如何定义的?

keyed-counter-rolling-window-set, 预定义了updater merger extractor

updater, incr-val [amap key amt], 把给定的值amt加到amap的对应的key的value上

merger, (partial merge-with +), 用+作为map merge的逻辑, 即出现相同key则相加

extractor, counter-extract, (if v v {}), 有则返回, 无则返回{}

windows, rolling-window的list

all-time, 初始化为nil

(defn keyed-counter-rolling-window-set [num-buckets & bucket-sizes]
(apply rolling-window-set incr-val (partial merge-with +) counter-extract num-buckets bucket-sizes))

好, 下面就看看, 当spout-acked-tuple!时更新:acked时, 如何update的?

首先更新每个rolling-window, 并把更新过的rolling-window-set更新到:windows
并且更新:all-time, (apply (:updater rws) (:all-time rws) args)

updated, incr-val [amap key amt]

args, steamid, rate

all-time, 是用来记录整个时间区间上的, 某个stream的统计情况

(defn update-rolling-window-set
([^RollingWindowSet rws & args]
(let [now (current-time-secs)
new-windows (dofor [w (:windows rws)]
(apply update-rolling-window w now args))]
(assoc rws :windows new-windows :all-time (apply (:updater rws) (:all-time rws) args))
)))

看下如何更新某个rolling-windw
根据now算出当前属于哪个bucket, time-bucket

取出buckets, 并使用:updater更新相应的bucket, 这里的操作仍然是把rate叠加到streamid的value上

(defn update-rolling-window
([^RollingWindow rw time-secs & args]
;; this is 2.5x faster than using update-in...
(let [time-bucket (curr-time-bucket time-secs (:bucket-size-secs rw))
buckets (:buckets rw)
curr (get buckets time-bucket)
curr (apply (:updater rw) curr args)
]
(assoc rw :buckets (assoc buckets time-bucket curr))
)))

Storm-源码分析-Stats (backtype.storm.stats)的更多相关文章

  1. storm源码分析之任务分配--task assignment

    在"storm源码分析之topology提交过程"一文最后,submitTopologyWithOpts函数调用了mk-assignments函数.该函数的主要功能就是进行topo ...

  2. Storm源码分析--Nimbus-data

    nimbus-datastorm-core/backtype/storm/nimbus.clj (defn nimbus-data [conf inimbus] (let [forced-schedu ...

  3. JStorm与Storm源码分析(四)--均衡调度器,EvenScheduler

    EvenScheduler同DefaultScheduler一样,同样实现了IScheduler接口, 由下面代码可以看出: (ns backtype.storm.scheduler.EvenSche ...

  4. JStorm与Storm源码分析(三)--Scheduler,调度器

    Scheduler作为Storm的调度器,负责为Topology分配可用资源. Storm提供了IScheduler接口,用户可以通过实现该接口来自定义Scheduler. 其定义如下: public ...

  5. JStorm与Storm源码分析(二)--任务分配,assignment

    mk-assignments主要功能就是产生Executor与节点+端口的对应关系,将Executor分配到某个节点的某个端口上,以及进行相应的调度处理.代码注释如下: ;;参数nimbus为nimb ...

  6. JStorm与Storm源码分析(一)--nimbus-data

    Nimbus里定义了一些共享数据结构,比如nimbus-data. nimbus-data结构里定义了很多公用的数据,请看下面代码: (defn nimbus-data [conf inimbus] ...

  7. Nimbus<三>Storm源码分析--Nimbus启动过程

    Nimbus server, 首先从启动命令开始, 同样是使用storm命令"storm nimbus”来启动看下源码, 此处和上面client不同, jvmtype="-serv ...

  8. storm源码分析之topology提交过程

    storm集群上运行的是一个个topology,一个topology是spouts和bolts组成的图.当我们开发完topology程序后将其打成jar包,然后在shell中执行storm jar x ...

  9. JStorm与Storm源码分析(五)--SpoutOutputCollector与代理模式

    本文主要是解析SpoutOutputCollector源码,顺便分析该类中所涉及的设计模式–代理模式. 首先介绍一下Spout输出收集器接口–ISpoutOutputCollector,该接口主要声明 ...

  10. Storm-源码分析- hook (backtype.storm.hooks)

    task hook 在某些task事件发生时, 如果用户希望执行一些额外的逻辑, 就需要使用hook 当前定义如下事件, emit, cleanup, spoutAck-- 用户只需要开发实现ITas ...

随机推荐

  1. 466. Count Linked List Nodes【Naive】

    Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...

  2. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 04、Quick Start for Windows

    创建一个新工程 1.在 VS 上,选择 File > New > Project.. 2.在新工程窗口,选择  Visual C# > Windows Store > Blan ...

  4. 如何生成项目的chm文档

    如何生成项目的chm文档 2014-11-30 Generate .chm based documentation of your project using SandCastle  tool

  5. 由于link顺序错误导致的undefined reference

    其实我之前就遇到过这个问题,也强调过,GNU-G++在link阶段是依赖输入的.o或者.a文件的顺序的.如果顺序错误会导致undefined reference错误 见这篇随笔:http://www. ...

  6. poj3261(后缀数组)

    题意:给出一串长度为n的字符,再给出一个k值,要你求重复次数大于等于k次的最长子串长度........ 思路:其实也非常简单,直接求出height值,然后将它分组,二分答案......结果就出来了.. ...

  7. Linux下HTTP Server

    想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...

  8. CentOS 7 上systemctl 的用法

    我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...

  9. 下列JSP代码:

    下列JSP代码: <html> <body> <% for(int i = 0; i < 10; i++) { //1 } %> </body> ...

  10. 【BZOJ】1037: [ZJOI2008]生日聚会Party(递推+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 看来自己越来越弱了... 这些计数题设计的状态都很巧妙,,自己智商太低QAQ 和矩阵dp做的那 ...