/
stats.clj
1596 lines (1464 loc) · 62.1 KB
/
stats.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Licensed to the Apache Software Foundation (ASF) under one
;; or more contributor license agreements. See the NOTICE file
;; distributed with this work for additional information
;; regarding copyright ownership. The ASF licenses this file
;; to you under the Apache License, Version 2.0 (the
;; "License"); you may not use this file except in compliance
;; with the License. You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(ns org.apache.storm.stats
(:import [org.apache.storm.generated Nimbus Nimbus$Processor Nimbus$Iface StormTopology ShellComponent
NotAliveException AlreadyAliveException InvalidTopologyException GlobalStreamId
ClusterSummary TopologyInfo TopologySummary ExecutorInfo ExecutorSummary ExecutorStats
ExecutorSpecificStats SpoutStats BoltStats ErrorInfo
SupervisorSummary CommonAggregateStats ComponentAggregateStats
ComponentPageInfo ComponentType BoltAggregateStats
ExecutorAggregateStats WorkerSummary SpecificAggregateStats
SpoutAggregateStats TopologyPageInfo TopologyStats
WorkerResources])
(:import [org.apache.storm.utils Utils])
(:import [org.apache.storm.scheduler WorkerSlot])
(:import [org.apache.storm.metric.internal MultiCountStatAndMetric MultiLatencyStatAndMetric]
(com.codahale.metrics Counter))
(:use [org.apache.storm log util])
(:use [clojure.math.numeric-tower :only [ceil]]))
(def TEN-MIN-IN-SECONDS (* 10 60))
(def COMMON-FIELDS [:emitted :transferred])
(defrecord CommonStats [^MultiCountStatAndMetric emitted
^MultiCountStatAndMetric transferred
rate])
(def BOLT-FIELDS [:acked :failed :process-latencies :executed :execute-latencies])
;;acked and failed count individual tuples
(defrecord BoltExecutorStats [^CommonStats common
^MultiCountStatAndMetric acked
^MultiCountStatAndMetric failed
^MultiLatencyStatAndMetric process-latencies
^MultiCountStatAndMetric executed
^MultiLatencyStatAndMetric execute-latencies])
(def SPOUT-FIELDS [:acked :failed :complete-latencies])
;;acked and failed count tuple completion
(defrecord SpoutExecutorStats [^CommonStats common
^MultiCountStatAndMetric acked
^MultiCountStatAndMetric failed
^MultiLatencyStatAndMetric complete-latencies])
(def NUM-STAT-BUCKETS 20)
(defn- mk-common-stats
[rate]
(CommonStats.
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
rate))
(defn mk-bolt-stats
[rate]
(BoltExecutorStats.
(mk-common-stats rate)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiLatencyStatAndMetric. NUM-STAT-BUCKETS)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiLatencyStatAndMetric. NUM-STAT-BUCKETS)))
(defn mk-spout-stats
[rate]
(SpoutExecutorStats.
(mk-common-stats rate)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiCountStatAndMetric. NUM-STAT-BUCKETS)
(MultiLatencyStatAndMetric. NUM-STAT-BUCKETS)))
(defmacro stats-rate
[stats]
`(-> ~stats :common :rate))
(defmacro stats-emitted
[stats]
`(-> ~stats :common :emitted))
(defmacro stats-transferred
[stats]
`(-> ~stats :common :transferred))
(defmacro stats-executed
[stats]
`(:executed ~stats))
(defmacro stats-acked
[stats]
`(:acked ~stats))
(defmacro stats-failed
[stats]
`(:failed ~stats))
(defmacro stats-execute-latencies
[stats]
`(:execute-latencies ~stats))
(defmacro stats-process-latencies
[stats]
`(:process-latencies ~stats))
(defmacro stats-complete-latencies
[stats]
`(:complete-latencies ~stats))
(defn emitted-tuple!
[stats ^Counter emitted-counter stream]
(let [^MultiCountStatAndMetric emitted (stats-emitted stats)
^long rate (stats-rate stats)]
(.incBy emitted ^Object stream rate)
(.inc emitted-counter rate)))
(defn transferred-tuples!
[stats ^Counter transferred-counter stream amt]
(let [^MultiCountStatAndMetric transferred (stats-transferred stats)]
(.inc transferred-counter amt)
(.incBy transferred ^Object stream ^long (* (stats-rate stats) amt))))
(defn bolt-execute-tuple!
[^BoltExecutorStats stats component stream latency-ms]
(let [key [component stream]
^MultiCountStatAndMetric executed (stats-executed stats)
^MultiLatencyStatAndMetric exec-lat (stats-execute-latencies stats)]
(.incBy executed key (stats-rate stats))
(.record exec-lat key latency-ms)))
(defn bolt-acked-tuple!
[^BoltExecutorStats stats ^Counter acked-counter component stream latency-ms]
(let [key [component stream]
^MultiCountStatAndMetric acked (stats-acked stats)
^MultiLatencyStatAndMetric process-lat (stats-process-latencies stats)
^long rate (stats-rate stats)]
(.incBy acked key rate)
(.inc acked-counter rate)
(.record process-lat key latency-ms)))
(defn bolt-failed-tuple!
[^BoltExecutorStats stats ^Counter failed-counter component stream latency-ms]
(let [key [component stream]
^MultiCountStatAndMetric failed (stats-failed stats)
^long rate (stats-rate stats)]
(.incBy failed key rate)
(.inc failed-counter rate)))
(defn spout-acked-tuple!
[^SpoutExecutorStats stats ^Counter acked-counter stream latency-ms]
(let [^MultiCountStatAndMetric acked (stats-acked stats)
^MultiLatencyStatAndMetric complete-latencies (stats-complete-latencies stats)
^long rate (stats-rate stats)]
(.incBy acked stream rate)
(.inc acked-counter rate)
(.record complete-latencies stream latency-ms)))
(defn spout-failed-tuple!
[^SpoutExecutorStats stats ^Counter failed-counter stream latency-ms]
(let [^MultiCountStatAndMetric failed (stats-failed stats)
^long rate (stats-rate stats)]
(.incBy failed stream rate)
(.inc failed-counter rate)))
(defn- close-stat! [stat]
(.close stat))
(defn- cleanup-common-stats!
[^CommonStats stats]
(doseq [f COMMON-FIELDS]
(close-stat! (f stats))))
(defn cleanup-bolt-stats!
[^BoltExecutorStats stats]
(cleanup-common-stats! (:common stats))
(doseq [f BOLT-FIELDS]
(close-stat! (f stats))))
(defn cleanup-spout-stats!
[^SpoutExecutorStats stats]
(cleanup-common-stats! (:common stats))
(doseq [f SPOUT-FIELDS]
(close-stat! (f stats))))
(defn- value-stats
[stats fields]
(into {} (dofor [f fields]
[f (if (instance? MultiCountStatAndMetric (f stats))
(.getTimeCounts ^MultiCountStatAndMetric (f stats))
(.getTimeLatAvg ^MultiLatencyStatAndMetric (f stats)))])))
(defn- value-common-stats
[^CommonStats stats]
(merge
(value-stats stats COMMON-FIELDS)
{:rate (:rate stats)}))
(defn value-bolt-stats!
[^BoltExecutorStats stats]
(merge (value-common-stats (:common stats))
(value-stats stats BOLT-FIELDS)
{:type :bolt}))
(defn value-spout-stats!
[^SpoutExecutorStats stats]
(merge (value-common-stats (:common stats))
(value-stats stats SPOUT-FIELDS)
{:type :spout}))
(defmulti render-stats! class-selector)
(defmethod render-stats! SpoutExecutorStats
[stats]
(value-spout-stats! stats))
(defmethod render-stats! BoltExecutorStats
[stats]
(value-bolt-stats! stats))
(defmulti cleanup-stats! class-selector)
(defmethod cleanup-stats! SpoutExecutorStats
[stats]
(cleanup-spout-stats! stats))
(defmethod cleanup-stats! BoltExecutorStats
[stats]
(cleanup-bolt-stats! stats))
(defmulti thriftify-specific-stats :type)
(defmulti clojurify-specific-stats class-selector)
(defn window-set-converter
([stats key-fn first-key-fun]
(into {}
(for [[k v] stats]
;apply the first-key-fun only to first key.
[(first-key-fun k)
(into {} (for [[k2 v2] v]
[(key-fn k2) v2]))])))
([stats first-key-fun]
(window-set-converter stats identity first-key-fun)))
(defn to-global-stream-id
[[component stream]]
(GlobalStreamId. component stream))
(defn from-global-stream-id [global-stream-id]
[(.get_componentId global-stream-id) (.get_streamId global-stream-id)])
(defmethod clojurify-specific-stats BoltStats [^BoltStats stats]
[(window-set-converter (.get_acked stats) from-global-stream-id identity)
(window-set-converter (.get_failed stats) from-global-stream-id identity)
(window-set-converter (.get_process_ms_avg stats) from-global-stream-id identity)
(window-set-converter (.get_executed stats) from-global-stream-id identity)
(window-set-converter (.get_execute_ms_avg stats) from-global-stream-id identity)])
(defmethod clojurify-specific-stats SpoutStats [^SpoutStats stats]
[(.get_acked stats)
(.get_failed stats)
(.get_complete_ms_avg stats)])
(defn clojurify-executor-stats
[^ExecutorStats stats]
(let [ specific-stats (.get_specific stats)
is_bolt? (.is_set_bolt specific-stats)
specific-stats (if is_bolt? (.get_bolt specific-stats) (.get_spout specific-stats))
specific-stats (clojurify-specific-stats specific-stats)
common-stats (CommonStats. (.get_emitted stats)
(.get_transferred stats)
(.get_rate stats))]
(if is_bolt?
; worker heart beat does not store the BoltExecutorStats or SpoutExecutorStats , instead it stores the result returned by render-stats!
; which flattens the BoltExecutorStats/SpoutExecutorStats by extracting values from all atoms and merging all values inside :common to top
;level map we are pretty much doing the same here.
(dissoc (merge common-stats {:type :bolt} (apply ->BoltExecutorStats (into [nil] specific-stats))) :common)
(dissoc (merge common-stats {:type :spout} (apply ->SpoutExecutorStats (into [nil] specific-stats))) :common)
)))
(defmethod thriftify-specific-stats :bolt
[stats]
(ExecutorSpecificStats/bolt
(BoltStats.
(window-set-converter (:acked stats) to-global-stream-id str)
(window-set-converter (:failed stats) to-global-stream-id str)
(window-set-converter (:process-latencies stats) to-global-stream-id str)
(window-set-converter (:executed stats) to-global-stream-id str)
(window-set-converter (:execute-latencies stats) to-global-stream-id str))))
(defmethod thriftify-specific-stats :spout
[stats]
(ExecutorSpecificStats/spout
(SpoutStats. (window-set-converter (:acked stats) str)
(window-set-converter (:failed stats) str)
(window-set-converter (:complete-latencies stats) str))))
(defn thriftify-executor-stats
[stats]
(let [specific-stats (thriftify-specific-stats stats)
rate (:rate stats)]
(ExecutorStats. (window-set-converter (:emitted stats) str)
(window-set-converter (:transferred stats) str)
specific-stats
rate)))
(defn valid-number?
"Returns true if x is a number that is not NaN or Infinity, false otherwise"
[x]
(and (number? x)
(not (Double/isNaN x))
(not (Double/isInfinite x))))
(defn apply-default
[f defaulting-fn & args]
(apply f (map defaulting-fn args)))
(defn apply-or-0
[f & args]
(apply apply-default
f
#(if (valid-number? %) % 0)
args))
(defn sum-or-0
[& args]
(apply apply-or-0 + args))
(defn product-or-0
[& args]
(apply apply-or-0 * args))
(defn max-or-0
[& args]
(apply apply-or-0 max args))
(defn- agg-bolt-lat-and-count
"Aggregates number executed, process latency, and execute latency across all
streams."
[idk->exec-avg idk->proc-avg idk->num-executed]
(letfn [(weight-avg [[id avg]]
(let [num-e (get idk->num-executed id)]
(product-or-0 avg num-e)))]
{:executeLatencyTotal (sum (map weight-avg idk->exec-avg))
:processLatencyTotal (sum (map weight-avg idk->proc-avg))
:executed (sum (vals idk->num-executed))}))
(defn- agg-spout-lat-and-count
"Aggregates number acked and complete latencies across all streams."
[sid->comp-avg sid->num-acked]
(letfn [(weight-avg [[id avg]]
(product-or-0 avg (get sid->num-acked id)))]
{:completeLatencyTotal (sum (map weight-avg sid->comp-avg))
:acked (sum (vals sid->num-acked))}))
(defn add-pairs
([] [0 0])
([[a1 a2] [b1 b2]]
[(+ a1 b1) (+ a2 b2)]))
(defn mk-include-sys-fn
[include-sys?]
(if include-sys?
(fn [_] true)
(fn [stream] (and (string? stream) (not (Utils/isSystemId stream))))))
(defn mk-include-sys-filter
"Returns a function that includes or excludes map entries whose keys are
system ids."
[include-sys?]
(if include-sys?
identity
(partial filter-key (mk-include-sys-fn false))))
(defn- agg-bolt-streams-lat-and-count
"Aggregates number executed and process & execute latencies."
[idk->exec-avg idk->proc-avg idk->executed]
(letfn [(weight-avg [id avg]
(let [num-e (idk->executed id)]
(product-or-0 avg num-e)))]
(into {}
(for [k (keys idk->exec-avg)]
[k {:executeLatencyTotal (weight-avg k (get idk->exec-avg k))
:processLatencyTotal (weight-avg k (get idk->proc-avg k))
:executed (idk->executed k)}]))))
(defn- agg-spout-streams-lat-and-count
"Aggregates number acked and complete latencies."
[idk->comp-avg idk->acked]
(letfn [(weight-avg [id avg]
(let [num-e (get idk->acked id)]
(product-or-0 avg num-e)))]
(into {}
(for [k (keys idk->comp-avg)]
[k {:completeLatencyTotal (weight-avg k (get idk->comp-avg k))
:acked (get idk->acked k)}]))))
(defn swap-map-order
"For a nested map, rearrange data such that the top-level keys become the
nested map's keys and vice versa.
Example:
{:a {:X :banana, :Y :pear}, :b {:X :apple, :Y :orange}}
-> {:Y {:a :pear, :b :orange}, :X {:a :banana, :b :apple}}"
[m]
(apply merge-with
merge
(map (fn [[k v]]
(into {}
(for [[k2 v2] v]
[k2 {k v2}])))
m)))
(defn- compute-agg-capacity
"Computes the capacity metric for one executor given its heartbeat data and
uptime."
[m uptime]
(when uptime
(->>
;; For each stream, create weighted averages and counts.
(merge-with (fn weighted-avg+count-fn
[avg cnt]
[(* avg cnt) cnt])
(get (:execute-latencies m) (str TEN-MIN-IN-SECONDS))
(get (:executed m) (str TEN-MIN-IN-SECONDS)))
vals ;; Ignore the stream ids.
(reduce add-pairs
[0. 0]) ;; Combine weighted averages and counts.
((fn [[weighted-avg cnt]]
(if (> uptime 0)
(div weighted-avg (* 1000 (min uptime TEN-MIN-IN-SECONDS)))
0.))))))
(defn agg-pre-merge-comp-page-bolt
[{exec-id :exec-id
host :host
port :port
uptime :uptime
comp-id :comp-id
num-tasks :num-tasks
statk->w->sid->num :stats}
window
include-sys?]
(let [str-key (partial map-key str)
handle-sys-components-fn (mk-include-sys-filter include-sys?)]
{:executor-id exec-id,
:host host,
:port port,
:uptime uptime,
:num-executors 1,
:num-tasks num-tasks,
:capacity (compute-agg-capacity statk->w->sid->num uptime)
:cid+sid->input-stats
(merge-with
merge
(swap-map-order
{:acked (-> statk->w->sid->num
:acked
str-key
(get window))
:failed (-> statk->w->sid->num
:failed
str-key
(get window))})
(agg-bolt-streams-lat-and-count (-> statk->w->sid->num
:execute-latencies
str-key
(get window))
(-> statk->w->sid->num
:process-latencies
str-key
(get window))
(-> statk->w->sid->num
:executed
str-key
(get window)))),
:sid->output-stats
(swap-map-order
{:emitted (-> statk->w->sid->num
:emitted
str-key
(get window)
handle-sys-components-fn)
:transferred (-> statk->w->sid->num
:transferred
str-key
(get window)
handle-sys-components-fn)})}))
(defn agg-pre-merge-comp-page-spout
[{exec-id :exec-id
host :host
port :port
uptime :uptime
comp-id :comp-id
num-tasks :num-tasks
statk->w->sid->num :stats}
window
include-sys?]
(let [str-key (partial map-key str)
handle-sys-components-fn (mk-include-sys-filter include-sys?)]
{:executor-id exec-id,
:host host,
:port port,
:uptime uptime,
:num-executors 1,
:num-tasks num-tasks,
:sid->output-stats
(merge-with
merge
(agg-spout-streams-lat-and-count (-> statk->w->sid->num
:complete-latencies
str-key
(get window))
(-> statk->w->sid->num
:acked
str-key
(get window)))
(swap-map-order
{:acked (-> statk->w->sid->num
:acked
str-key
(get window))
:failed (-> statk->w->sid->num
:failed
str-key
(get window))
:emitted (-> statk->w->sid->num
:emitted
str-key
(get window)
handle-sys-components-fn)
:transferred (-> statk->w->sid->num
:transferred
str-key
(get window)
handle-sys-components-fn)}))}))
(defn agg-pre-merge-topo-page-bolt
[{comp-id :comp-id
num-tasks :num-tasks
statk->w->sid->num :stats
uptime :uptime}
window
include-sys?]
(let [str-key (partial map-key str)
handle-sys-components-fn (mk-include-sys-filter include-sys?)]
{comp-id
(merge
(agg-bolt-lat-and-count (-> statk->w->sid->num
:execute-latencies
str-key
(get window))
(-> statk->w->sid->num
:process-latencies
str-key
(get window))
(-> statk->w->sid->num
:executed
str-key
(get window)))
{:num-executors 1
:num-tasks num-tasks
:emitted (-> statk->w->sid->num
:emitted
str-key
(get window)
handle-sys-components-fn
vals
sum)
:transferred (-> statk->w->sid->num
:transferred
str-key
(get window)
handle-sys-components-fn
vals
sum)
:capacity (compute-agg-capacity statk->w->sid->num uptime)
:acked (-> statk->w->sid->num
:acked
str-key
(get window)
vals
sum)
:failed (-> statk->w->sid->num
:failed
str-key
(get window)
vals
sum)})}))
(defn agg-pre-merge-topo-page-spout
[{comp-id :comp-id
num-tasks :num-tasks
statk->w->sid->num :stats}
window
include-sys?]
(let [str-key (partial map-key str)
handle-sys-components-fn (mk-include-sys-filter include-sys?)]
{comp-id
(merge
(agg-spout-lat-and-count (-> statk->w->sid->num
:complete-latencies
str-key
(get window))
(-> statk->w->sid->num
:acked
str-key
(get window)))
{:num-executors 1
:num-tasks num-tasks
:emitted (-> statk->w->sid->num
:emitted
str-key
(get window)
handle-sys-components-fn
vals
sum)
:transferred (-> statk->w->sid->num
:transferred
str-key
(get window)
handle-sys-components-fn
vals
sum)
:failed (-> statk->w->sid->num
:failed
str-key
(get window)
vals
sum)})}))
(defn merge-agg-comp-stats-comp-page-bolt
[{acc-in :cid+sid->input-stats
acc-out :sid->output-stats
:as acc-bolt-stats}
{bolt-in :cid+sid->input-stats
bolt-out :sid->output-stats
:as bolt-stats}]
{:num-executors (inc (or (:num-executors acc-bolt-stats) 0)),
:num-tasks (sum-or-0 (:num-tasks acc-bolt-stats) (:num-tasks bolt-stats)),
:sid->output-stats (merge-with (partial merge-with sum-or-0)
acc-out
bolt-out),
:cid+sid->input-stats (merge-with (partial merge-with sum-or-0)
acc-in
bolt-in),
:executor-stats
(let [sum-streams (fn [m k] (->> m vals (map k) (apply sum-or-0)))
executed (sum-streams bolt-in :executed)]
(conj (:executor-stats acc-bolt-stats)
(merge
(select-keys bolt-stats
[:executor-id :uptime :host :port :capacity])
{:emitted (sum-streams bolt-out :emitted)
:transferred (sum-streams bolt-out :transferred)
:acked (sum-streams bolt-in :acked)
:failed (sum-streams bolt-in :failed)
:executed executed}
(->>
(if (and executed (pos? executed))
[(div (sum-streams bolt-in :executeLatencyTotal) executed)
(div (sum-streams bolt-in :processLatencyTotal) executed)]
[nil nil])
(mapcat vector [:execute-latency :process-latency])
(apply assoc {})))))})
(defn merge-agg-comp-stats-comp-page-spout
[{acc-out :sid->output-stats
:as acc-spout-stats}
{spout-out :sid->output-stats
:as spout-stats}]
{:num-executors (inc (or (:num-executors acc-spout-stats) 0)),
:num-tasks (sum-or-0 (:num-tasks acc-spout-stats) (:num-tasks spout-stats)),
:sid->output-stats (merge-with (partial merge-with sum-or-0)
acc-out
spout-out),
:executor-stats
(let [sum-streams (fn [m k] (->> m vals (map k) (apply sum-or-0)))
acked (sum-streams spout-out :acked)]
(conj (:executor-stats acc-spout-stats)
(merge
(select-keys spout-stats [:executor-id :uptime :host :port])
{:emitted (sum-streams spout-out :emitted)
:transferred (sum-streams spout-out :transferred)
:acked acked
:failed (sum-streams spout-out :failed)}
{:complete-latency (if (and acked (pos? acked))
(div (sum-streams spout-out
:completeLatencyTotal)
acked)
nil)})))})
(defn merge-agg-comp-stats-topo-page-bolt
[acc-bolt-stats bolt-stats]
{:num-executors (inc (or (:num-executors acc-bolt-stats) 0))
:num-tasks (sum-or-0 (:num-tasks acc-bolt-stats) (:num-tasks bolt-stats))
:emitted (sum-or-0 (:emitted acc-bolt-stats) (:emitted bolt-stats))
:transferred (sum-or-0 (:transferred acc-bolt-stats)
(:transferred bolt-stats))
:capacity (max-or-0 (:capacity acc-bolt-stats) (:capacity bolt-stats))
;; We sum average latency totals here to avoid dividing at each step.
;; Compute the average latencies by dividing the total by the count.
:executeLatencyTotal (sum-or-0 (:executeLatencyTotal acc-bolt-stats)
(:executeLatencyTotal bolt-stats))
:processLatencyTotal (sum-or-0 (:processLatencyTotal acc-bolt-stats)
(:processLatencyTotal bolt-stats))
:executed (sum-or-0 (:executed acc-bolt-stats) (:executed bolt-stats))
:acked (sum-or-0 (:acked acc-bolt-stats) (:acked bolt-stats))
:failed (sum-or-0 (:failed acc-bolt-stats) (:failed bolt-stats))})
(defn merge-agg-comp-stats-topo-page-spout
[acc-spout-stats spout-stats]
{:num-executors (inc (or (:num-executors acc-spout-stats) 0))
:num-tasks (sum-or-0 (:num-tasks acc-spout-stats) (:num-tasks spout-stats))
:emitted (sum-or-0 (:emitted acc-spout-stats) (:emitted spout-stats))
:transferred (sum-or-0 (:transferred acc-spout-stats) (:transferred spout-stats))
;; We sum average latency totals here to avoid dividing at each step.
;; Compute the average latencies by dividing the total by the count.
:completeLatencyTotal (sum-or-0 (:completeLatencyTotal acc-spout-stats)
(:completeLatencyTotal spout-stats))
:acked (sum-or-0 (:acked acc-spout-stats) (:acked spout-stats))
:failed (sum-or-0 (:failed acc-spout-stats) (:failed spout-stats))})
(defn aggregate-count-streams
[stats]
(->> stats
(map-val #(reduce + (vals %)))))
(defn- agg-topo-exec-stats*
"A helper function that does the common work to aggregate stats of one
executor with the given map for the topology page."
[window
include-sys?
{:keys [workers-set
bolt-id->stats
spout-id->stats
window->emitted
window->transferred
window->comp-lat-wgt-avg
window->acked
window->failed] :as acc-stats}
{:keys [stats] :as new-data}
pre-merge-fn
merge-fn
comp-key]
(let [cid->statk->num (pre-merge-fn new-data window include-sys?)
{w->compLatWgtAvg :completeLatencyTotal
w->acked :acked}
(if (:complete-latencies stats)
(swap-map-order
(into {}
(for [w (keys (:acked stats))]
[w (agg-spout-lat-and-count
(get (:complete-latencies stats) w)
(get (:acked stats) w))])))
{:completeLatencyTotal nil
:acks (aggregate-count-streams (:acked stats))})
handle-sys-components-fn (mk-include-sys-filter include-sys?)]
(assoc {:workers-set (conj workers-set
[(:host new-data) (:port new-data)])
:bolt-id->stats bolt-id->stats
:spout-id->stats spout-id->stats
:window->emitted (->> (:emitted stats)
(map-val handle-sys-components-fn)
aggregate-count-streams
(merge-with + window->emitted))
:window->transferred (->> (:transferred stats)
(map-val handle-sys-components-fn)
aggregate-count-streams
(merge-with + window->transferred))
:window->comp-lat-wgt-avg (merge-with +
window->comp-lat-wgt-avg
w->compLatWgtAvg)
:window->acked (if (= :spout (:type stats))
(merge-with + window->acked w->acked)
window->acked)
:window->failed (if (= :spout (:type stats))
(->> (:failed stats)
aggregate-count-streams
(merge-with + window->failed))
window->failed)}
comp-key (merge-with merge-fn
(acc-stats comp-key)
cid->statk->num)
:type (:type stats))))
(defmulti agg-topo-exec-stats
"Combines the aggregate stats of one executor with the given map, selecting
the appropriate window and including system components as specified."
(fn dispatch-fn [& args] (:type (last args))))
(defmethod agg-topo-exec-stats :bolt
[window include-sys? acc-stats new-data]
(agg-topo-exec-stats* window
include-sys?
acc-stats
new-data
agg-pre-merge-topo-page-bolt
merge-agg-comp-stats-topo-page-bolt
:bolt-id->stats))
(defmethod agg-topo-exec-stats :spout
[window include-sys? acc-stats new-data]
(agg-topo-exec-stats* window
include-sys?
acc-stats
new-data
agg-pre-merge-topo-page-spout
merge-agg-comp-stats-topo-page-spout
:spout-id->stats))
(defmethod agg-topo-exec-stats :default [_ _ acc-stats _] acc-stats)
(defn get-last-error
[storm-cluster-state storm-id component-id]
(if-let [e (.last-error storm-cluster-state storm-id component-id)]
(ErrorInfo. (:error e) (:time-secs e))))
(defn component-type
"Returns the component type (either :bolt or :spout) for a given
topology and component id. Returns nil if not found."
[^StormTopology topology id]
(let [bolts (.get_bolts topology)
spouts (.get_spouts topology)]
(cond
(Utils/isSystemId id) :bolt
(.containsKey bolts id) :bolt
(.containsKey spouts id) :spout)))
(defn extract-nodeinfos-from-hb-for-comp
([exec->host+port task->component include-sys? comp-id]
(distinct (for [[[start end :as executor] [host port]] exec->host+port
:let [id (task->component start)]
:when (and (or (nil? comp-id) (= comp-id id))
(or include-sys? (not (Utils/isSystemId id))))]
{:host host
:port port}))))
(defn extract-data-from-hb
([exec->host+port task->component beats include-sys? topology comp-id]
(for [[[start end :as executor] [host port]] exec->host+port
:let [beat (beats executor)
id (task->component start)]
:when (and (or (nil? comp-id) (= comp-id id))
(or include-sys? (not (Utils/isSystemId id))))]
{:exec-id executor
:comp-id id
:num-tasks (count (range start (inc end)))
:host host
:port port
:uptime (:uptime beat)
:stats (:stats beat)
:type (or (:type (:stats beat))
(component-type topology id))}))
([exec->host+port task->component beats include-sys? topology]
(extract-data-from-hb exec->host+port
task->component
beats
include-sys?
topology
nil)))
(defn aggregate-topo-stats
[window include-sys? data]
(let [init-val {:workers-set #{}
:bolt-id->stats {}
:spout-id->stats {}
:window->emitted {}
:window->transferred {}
:window->comp-lat-wgt-avg {}
:window->acked {}
:window->failed {}}
reducer-fn (partial agg-topo-exec-stats
window
include-sys?)]
(reduce reducer-fn init-val data)))
(defn- compute-weighted-averages-per-window
[acc-data wgt-avg-key divisor-key]
(into {} (for [[window wgt-avg] (wgt-avg-key acc-data)
:let [divisor ((divisor-key acc-data) window)]
:when (and divisor (pos? divisor))]
[(str window) (div wgt-avg divisor)])))
(defn- post-aggregate-topo-stats
[task->component exec->node+port last-err-fn acc-data]
{:num-tasks (count task->component)
:num-workers (count (:workers-set acc-data))
:num-executors (count exec->node+port)
:bolt-id->stats
(into {} (for [[id m] (:bolt-id->stats acc-data)
:let [executed (:executed m)]]
[id (-> m
(assoc :execute-latency
(if (and executed (pos? executed))
(div (or (:executeLatencyTotal m) 0)
executed)
0)
:process-latency
(if (and executed (pos? executed))
(div (or (:processLatencyTotal m) 0)
executed)
0))
(dissoc :executeLatencyTotal
:processLatencyTotal)
(assoc :lastError (last-err-fn id)))]))
:spout-id->stats
(into {} (for [[id m] (:spout-id->stats acc-data)
:let [acked (:acked m)]]
[id (-> m
(assoc :complete-latency
(if (and acked (pos? acked))
(div (:completeLatencyTotal m)
(:acked m))
0))
(dissoc :completeLatencyTotal)
(assoc :lastError (last-err-fn id)))]))
:window->emitted (map-key str (:window->emitted acc-data))
:window->transferred (map-key str (:window->transferred acc-data))
:window->complete-latency
(compute-weighted-averages-per-window acc-data
:window->comp-lat-wgt-avg
:window->acked)
:window->acked (map-key str (:window->acked acc-data))
:window->failed (map-key str (:window->failed acc-data))})
(defn- thriftify-common-agg-stats
[^ComponentAggregateStats s
{:keys [num-tasks
emitted
transferred
acked
failed
num-executors] :as statk->num}]
(let [cas (CommonAggregateStats.)]
(and num-executors (.set_num_executors cas num-executors))
(and num-tasks (.set_num_tasks cas num-tasks))
(and emitted (.set_emitted cas emitted))
(and transferred (.set_transferred cas transferred))
(and acked (.set_acked cas acked))
(and failed (.set_failed cas failed))
(.set_common_stats s cas)))
(defn thriftify-bolt-agg-stats
[statk->num]
(let [{:keys [lastError
execute-latency
process-latency
executed
capacity]} statk->num
s (ComponentAggregateStats.)]
(.set_type s ComponentType/BOLT)
(and lastError (.set_last_error s lastError))
(thriftify-common-agg-stats s statk->num)
(.set_specific_stats s
(SpecificAggregateStats/bolt
(let [bas (BoltAggregateStats.)]
(and execute-latency (.set_execute_latency_ms bas execute-latency))
(and process-latency (.set_process_latency_ms bas process-latency))
(and executed (.set_executed bas executed))
(and capacity (.set_capacity bas capacity))
bas)))
s))
(defn thriftify-spout-agg-stats
[statk->num]
(let [{:keys [lastError
complete-latency]} statk->num
s (ComponentAggregateStats.)]
(.set_type s ComponentType/SPOUT)
(and lastError (.set_last_error s lastError))
(thriftify-common-agg-stats s statk->num)
(.set_specific_stats s
(SpecificAggregateStats/spout
(let [sas (SpoutAggregateStats.)]
(and complete-latency (.set_complete_latency_ms sas complete-latency))
sas)))
s))
(defn thriftify-topo-page-data
[topology-id data]
(let [{:keys [num-tasks
num-workers
num-executors
spout-id->stats
bolt-id->stats
window->emitted
window->transferred
window->complete-latency
window->acked
window->failed]} data