dbsp 0.287.0

Continuous streaming analytics engine
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
use num_format::{Locale, ToFormattedString};
use serde::{
    Serialize, Serializer,
    ser::{SerializeSeq, SerializeStruct},
};
use size_of::{HumanBytes, TotalSize};
use std::{
    borrow::Cow,
    collections::BTreeMap,
    fmt::{self, Display, Write},
    ops::{Deref, DerefMut},
    panic::Location,
    time::Duration,
};

use crate::storage::buffer_cache::CacheCounts;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, PartialOrd, Ord)]
#[repr(transparent)]
pub struct MetricId(pub Cow<'static, str>);

impl Display for MetricId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum CircuitMetricCategory {
    State,
    Inputs,
    Outputs,
    Cache,
    Time,
    Balancer,
    Multihost,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CircuitMetric {
    pub name: MetricId,
    pub category: CircuitMetricCategory,
    pub advanced: bool,
    pub description: &'static str,
}

pub type MetricLabels = Vec<(Cow<'static, str>, Cow<'static, str>)>;

#[derive(Debug, Clone)]
pub struct MetricReading {
    metric_id: MetricId,
    labels: MetricLabels,
    value: MetaItem,
}

impl MetricReading {
    pub fn new(metric_id: MetricId, labels: MetricLabels, value: MetaItem) -> Self {
        Self {
            metric_id,
            labels,
            value,
        }
    }
}

pub const USED_MEMORY_BYTES: MetricId = MetricId(Cow::Borrowed("used_memory_bytes"));
pub const ALLOCATED_MEMORY_BYTES: MetricId = MetricId(Cow::Borrowed("allocated_memory_bytes"));
pub const MEMORY_ALLOCATIONS_COUNT: MetricId = MetricId(Cow::Borrowed("memory_allocations_count"));
pub const SHARED_MEMORY_BYTES: MetricId = MetricId(Cow::Borrowed("shared_memory_bytes"));
pub const STATE_RECORDS_COUNT: MetricId = MetricId(Cow::Borrowed("state_records_count"));
pub const INPUT_RECORDS_COUNT: MetricId = MetricId(Cow::Borrowed("input_records_count"));
pub const INPUT_BATCHES_STATS: MetricId = MetricId(Cow::Borrowed("input_batches_stats"));
pub const OUTPUT_BATCHES_STATS: MetricId = MetricId(Cow::Borrowed("output_batches_stats"));
pub const EXCHANGE_WAIT_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("exchange_wait_time_seconds"));
pub const EXCHANGE_SERIALIZATION_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("exchange_serialization_time_seconds"));
pub const EXCHANGE_SERIALIZED_BYTES: MetricId =
    MetricId(Cow::Borrowed("exchange_serialized_bytes"));
pub const EXCHANGE_DESERIALIZATION_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("exchange_deserialization_time_seconds"));
pub const EXCHANGE_DESERIALIZED_BYTES: MetricId =
    MetricId(Cow::Borrowed("exchange_deserialized_bytes"));
pub const KEY_DISTRIBUTION: MetricId = MetricId(Cow::Borrowed("key_distribution"));
pub const SIZE_DISTRIBUTION: MetricId = MetricId(Cow::Borrowed("size_distribution"));
pub const LOCAL_SHARD_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("local_shard_records_count"));
pub const BALANCER_POLICY: MetricId = MetricId(Cow::Borrowed("balancer_policy"));
pub const RABALANCINGS_COUNT: MetricId = MetricId(Cow::Borrowed("rebalancings_count"));
pub const REBALANCING_IN_PROGRESS: MetricId =
    MetricId(Cow::Borrowed("rebalancing_in_progress_bool"));
pub const ACCUMULATOR_RECORDS_TO_REPARTITION_COUNT: MetricId =
    MetricId(Cow::Borrowed("accumulator_records_to_repartition_count"));
pub const INTEGRAL_RECORDS_TO_REPARTITION_COUNT: MetricId =
    MetricId(Cow::Borrowed("integral_records_to_repartition_count"));
pub const TOTAL_REBALANCING_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("total_rebalancing_time_seconds"));
pub const INPROGRESS_REBALANCING_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("inprogress_rebalancing_time_seconds"));
pub const LEFT_INPUT_BATCHES_STATS: MetricId = MetricId(Cow::Borrowed("left_input_batches_stats"));
pub const RIGHT_INPUT_BATCHES_STATS: MetricId =
    MetricId(Cow::Borrowed("right_input_batches_stats"));
pub const RETAINMENT_BOUNDS: MetricId = MetricId(Cow::Borrowed("retainment_bounds"));
pub const LEFT_INPUT_RECORDS_COUNT: MetricId = MetricId(Cow::Borrowed("left_input_records_count"));
pub const RIGHT_INPUT_INTEGRAL_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("right_input_integral_records_count"));
pub const COMPUTED_OUTPUT_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("computed_output_records_count"));
pub const OUTPUT_REDUNDANCY_PERCENT: MetricId =
    MetricId(Cow::Borrowed("output_redundancy_percent"));
pub const CACHE_FOREGROUND_HITS: MetricId = MetricId(Cow::Borrowed("foreground_cache_hits"));
pub const CACHE_FOREGROUND_MISSES: MetricId = MetricId(Cow::Borrowed("foreground_cache_misses"));
pub const CACHE_BACKGROUND_HITS: MetricId = MetricId(Cow::Borrowed("background_cache_hits"));
pub const CACHE_BACKGROUND_MISSES: MetricId = MetricId(Cow::Borrowed("background_cache_misses"));
pub const CACHE_FOREGROUND_HIT_RATE_PERCENT: MetricId =
    MetricId(Cow::Borrowed("foreground_cache_hit_rate_percent"));
pub const CACHE_BACKGROUND_HIT_RATE_PERCENT: MetricId =
    MetricId(Cow::Borrowed("background_cache_hit_rate_percent"));
pub const LOOSE_BATCHES_COUNT: MetricId = MetricId(Cow::Borrowed("loose_batches_count"));
pub const MERGING_BATCHES_COUNT: MetricId = MetricId(Cow::Borrowed("merging_batches_count"));
pub const LOOSE_MEMORY_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("loose_memory_records_count"));
pub const LOOSE_STORAGE_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("loose_storage_records_count"));
pub const MERGING_MEMORY_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("merging_memory_records_count"));
pub const MERGING_STORAGE_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("merging_storage_records_count"));
pub const COMPLETED_MERGES: MetricId = MetricId(Cow::Borrowed("completed_merges"));
pub const NEGATIVE_WEIGHT_COUNT: MetricId = MetricId(Cow::Borrowed("negative_weight_count"));
pub const BLOOM_FILTER_BITS_PER_KEY: MetricId =
    MetricId(Cow::Borrowed("bloom_filter_bits_per_key"));
pub const BLOOM_FILTER_HITS_COUNT: MetricId = MetricId(Cow::Borrowed("bloom_filter_hits_count"));
pub const BLOOM_FILTER_MISSES_COUNT: MetricId =
    MetricId(Cow::Borrowed("bloom_filter_misses_count"));
pub const BLOOM_FILTER_HIT_RATE_PERCENT: MetricId =
    MetricId(Cow::Borrowed("bloom_filter_hit_rate_percent"));
pub const BLOOM_FILTER_SIZE_BYTES: MetricId = MetricId(Cow::Borrowed("bloom_filter_size_bytes"));
pub const RANGE_FILTER_HITS_COUNT: MetricId = MetricId(Cow::Borrowed("range_filter_hits_count"));
pub const RANGE_FILTER_MISSES_COUNT: MetricId =
    MetricId(Cow::Borrowed("range_filter_misses_count"));
pub const RANGE_FILTER_HIT_RATE_PERCENT: MetricId =
    MetricId(Cow::Borrowed("range_filter_hit_rate_percent"));
pub const RANGE_FILTER_SIZE_BYTES: MetricId = MetricId(Cow::Borrowed("range_filter_size_bytes"));
pub const SPINE_BATCHES_COUNT: MetricId = MetricId(Cow::Borrowed("spine_batches_count"));
pub const SPINE_STORAGE_SIZE_BYTES: MetricId = MetricId(Cow::Borrowed("spine_storage_size_bytes"));
pub const MERGING_SIZE_BYTES: MetricId = MetricId(Cow::Borrowed("merging_size_bytes"));
pub const MERGE_REDUCTION_PERCENT: MetricId = MetricId(Cow::Borrowed("merge_reduction_percent"));
pub const MERGE_BACKPRESSURE_WAIT_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("merge_backpressure_wait_time_seconds"));
pub const INVOCATIONS_COUNT: MetricId = MetricId(Cow::Borrowed("invocations_count"));
pub const RUNTIME_SECONDS: MetricId = MetricId(Cow::Borrowed("runtime_seconds"));
pub const RUNTIME_PERCENT: MetricId = MetricId(Cow::Borrowed("runtime_percent"));
pub const CIRCUIT_WAIT_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("circuit_wait_time_seconds"));
pub const STEPS_COUNT: MetricId = MetricId(Cow::Borrowed("steps_count"));
pub const CIRCUIT_RUNTIME_SECONDS: MetricId = MetricId(Cow::Borrowed("circuit_runtime_seconds"));
pub const CIRCUIT_IDLE_TIME_SECONDS: MetricId =
    MetricId(Cow::Borrowed("circuit_idle_time_seconds"));
pub const CIRCUIT_RUNTIME_ELAPSED_SECONDS: MetricId =
    MetricId(Cow::Borrowed("circuit_runtime_elapsed_seconds"));
pub const FOREGROUND_CACHE_OCCUPANCY: MetricId =
    MetricId(Cow::Borrowed("foreground_cache_occupancy"));
pub const BACKGROUND_CACHE_OCCUPANCY: MetricId =
    MetricId(Cow::Borrowed("background_cache_occupancy"));
pub const PREFIX_BATCHES_STATS: MetricId = MetricId(Cow::Borrowed("prefix_batches_stats"));
pub const INPUT_INTEGRAL_RECORDS_COUNT: MetricId =
    MetricId(Cow::Borrowed("input_integral_records_count"));

pub const CIRCUIT_METRICS: [CircuitMetric; 70] = [
    // State
    CircuitMetric {
        name: USED_MEMORY_BYTES,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Bytes used by the in-memory state of the operator. See also 'allocated_memory_bytes'.",
    },
    CircuitMetric {
        name: ALLOCATED_MEMORY_BYTES,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Total bytes reserved by the operator's in-memory state, including unused capacity; can exceed 'used_memory_bytes'.",
    },
    CircuitMetric {
        name: MEMORY_ALLOCATIONS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Number of contiguous memory regions allocated for the operator's in-memory state.",
    },
    CircuitMetric {
        name: SHARED_MEMORY_BYTES,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Bytes of in-memory state possibly shared with other operators.",
    },
    CircuitMetric {
        name: STATE_RECORDS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Number of records stored in operator state (both in-memory and on-disk).",
    },
    CircuitMetric {
        name: SPINE_BATCHES_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Number of batches in the spine.",
    },
    CircuitMetric {
        name: SPINE_STORAGE_SIZE_BYTES,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Size of the spine.",
    },
    CircuitMetric {
        name: MERGING_BATCHES_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Total number of currently merging batches in the operator's state.",
    },
    CircuitMetric {
        name: MERGING_MEMORY_RECORDS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of in-memory records in merging batches.",
    },
    CircuitMetric {
        name: MERGING_STORAGE_RECORDS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of on-disk records in merging batches.",
    },
    CircuitMetric {
        name: MERGING_SIZE_BYTES,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Approximate combined in-memory and on-disk size of currently merging batches.",
    },
    CircuitMetric {
        name: MERGE_REDUCTION_PERCENT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Merge reduction. across all merges performed by the operator.",
    },
    CircuitMetric {
        name: LOOSE_BATCHES_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of loose batches, i.e., batches that are not being merged, in the operator's state.",
    },
    CircuitMetric {
        name: LOOSE_MEMORY_RECORDS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of in-memory records in loose batches.",
    },
    CircuitMetric {
        name: LOOSE_STORAGE_RECORDS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of on-disk records in loose batches.",
    },
    CircuitMetric {
        name: NEGATIVE_WEIGHT_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Total number of negative weight records in the spine.",
    },
    CircuitMetric {
        name: BLOOM_FILTER_BITS_PER_KEY,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Average number of bits per key in the Bloom filter.",
    },
    CircuitMetric {
        name: BLOOM_FILTER_SIZE_BYTES,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Size of the Bloom filter in bytes.",
    },
    CircuitMetric {
        name: BLOOM_FILTER_HITS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of hits across all Bloom filters. The hits are summed across the Bloom filters for all batches in the spine.",
    },
    CircuitMetric {
        name: BLOOM_FILTER_MISSES_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of misses across all Bloom filters. The misses are summed across the Bloom filters for all batches in the spine.",
    },
    CircuitMetric {
        name: BLOOM_FILTER_HIT_RATE_PERCENT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Hit rate of the Bloom filter.",
    },
    CircuitMetric {
        name: RANGE_FILTER_SIZE_BYTES,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Size of the cached range filter in bytes.",
    },
    CircuitMetric {
        name: RANGE_FILTER_HITS_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of hits across all range filters. The hits are summed across the range filters for all batches in the spine.",
    },
    CircuitMetric {
        name: RANGE_FILTER_MISSES_COUNT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "The number of misses across all range filters. The misses are summed across the range filters for all batches in the spine.",
    },
    CircuitMetric {
        name: RANGE_FILTER_HIT_RATE_PERCENT,
        category: CircuitMetricCategory::State,
        advanced: false,
        description: "Hit rate of the range filter.",
    },
    CircuitMetric {
        name: RETAINMENT_BOUNDS,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Bounds used by the garbage collector to discard unused state.",
    },
    CircuitMetric {
        name: SIZE_DISTRIBUTION,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Distribution of sizes in batches. The metric value is an array of counts, one for each batch in the state.",
    },
    CircuitMetric {
        name: COMPLETED_MERGES,
        category: CircuitMetricCategory::State,
        advanced: true,
        description: "Information about the batches that were compacted (merged).",
    },
    // Inputs
    CircuitMetric {
        name: INPUT_RECORDS_COUNT,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Total input records ingested by the operator.",
    },
    CircuitMetric {
        name: INPUT_BATCHES_STATS,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Distribution of input batch sizes processed by the operator.",
    },
    CircuitMetric {
        name: LEFT_INPUT_BATCHES_STATS,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Distribution of input batch sizes in the left input stream of an asof-join operator.",
    },
    CircuitMetric {
        name: RIGHT_INPUT_BATCHES_STATS,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Distribution of input batch sizes in the right input stream of an asof-join operator.",
    },
    CircuitMetric {
        name: LEFT_INPUT_RECORDS_COUNT,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Total number of input records received in the left input stream of a join operator.",
    },
    CircuitMetric {
        name: RIGHT_INPUT_INTEGRAL_RECORDS_COUNT,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "The size of the integral of the right input stream of a join operator.",
    },
    CircuitMetric {
        name: PREFIX_BATCHES_STATS,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "Distribution of prefix batch sizes ingested by a match operator.",
    },
    CircuitMetric {
        name: INPUT_INTEGRAL_RECORDS_COUNT,
        category: CircuitMetricCategory::Inputs,
        advanced: false,
        description: "The size of the integral input to a match operator.",
    },
    // Outputs
    CircuitMetric {
        name: OUTPUT_BATCHES_STATS,
        category: CircuitMetricCategory::Outputs,
        advanced: false,
        description: "Distribution of output batch sizes produced by the operator.",
    },
    CircuitMetric {
        name: COMPUTED_OUTPUT_RECORDS_COUNT,
        category: CircuitMetricCategory::Outputs,
        advanced: false,
        description: "Total number of output records computed by the operator. This number can include duplicate records and be greater than the number of output records produced by the operator.",
    },
    CircuitMetric {
        name: OUTPUT_REDUNDANCY_PERCENT,
        category: CircuitMetricCategory::Outputs,
        advanced: false,
        description: "Percentage of redundant output records eliminated during compaction.",
    },
    // Time
    CircuitMetric {
        name: INVOCATIONS_COUNT,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Number of times the operator has been invoked.",
    },
    CircuitMetric {
        name: RUNTIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Total time spent evaluating the operator.",
    },
    CircuitMetric {
        name: RUNTIME_PERCENT,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Percentage of time spent evaluating the operator as a fraction of the total runtime of all operators in the circuit.",
    },
    CircuitMetric {
        name: CIRCUIT_WAIT_TIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Time the circuit scheduler spent waiting for an operator to become ready.",
    },
    CircuitMetric {
        name: STEPS_COUNT,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Number of steps performed by the circuit.",
    },
    CircuitMetric {
        name: CIRCUIT_RUNTIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Total time spent evaluating the circuit, including operators runtime and circuit wait time ('circuit_wait_time_seconds').",
    },
    CircuitMetric {
        name: CIRCUIT_IDLE_TIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Total time spent between circuit invocations, waiting for new data from input connectors or for output connector queues to clear out.",
    },
    CircuitMetric {
        name: CIRCUIT_RUNTIME_ELAPSED_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: true,
        description: "Time elapsed while the circuit is executing a step, multiplied by the number of foreground and background threads.",
    },
    CircuitMetric {
        name: EXCHANGE_WAIT_TIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: true,
        description: "Total time the exchange operator spent waiting for data from other workers. This includes the time between the operator sent local data to its peers and the time it received data from all peers. Other operators in the circuit may be running during this time.",
    },
    CircuitMetric {
        name: MERGE_BACKPRESSURE_WAIT_TIME_SECONDS,
        category: CircuitMetricCategory::Time,
        advanced: false,
        description: "Time spent waiting for backpressure.",
    },
    // Balancer
    CircuitMetric {
        name: KEY_DISTRIBUTION,
        category: CircuitMetricCategory::Balancer,
        advanced: true,
        description: "Distribution of input keys received by the local worker. The metric value is an array of counts, where each index corresponds to a worker and the value at that index is the number of input records mapped to that worker based on the hash of the key.",
    },
    CircuitMetric {
        name: LOCAL_SHARD_RECORDS_COUNT,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Total number of input records mapped to the local worker based on the hash of the key. This metric is used to measure the skew of the input stream. The stream is skewed if the number of records mapped to some of the workers is significantly higher from the average number of records per worker.",
    },
    CircuitMetric {
        name: BALANCER_POLICY,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Current balancing policy.",
    },
    CircuitMetric {
        name: RABALANCINGS_COUNT,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Number of stream rebalancing events. A rebalancing event occurs when the balancing policy is changed and requires repartitioning of the input stream across workers.",
    },
    CircuitMetric {
        name: REBALANCING_IN_PROGRESS,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Indicates if rebalancing is currently in progress.",
    },
    CircuitMetric {
        name: ACCUMULATOR_RECORDS_TO_REPARTITION_COUNT,
        category: CircuitMetricCategory::Balancer,
        advanced: true,
        description: "The number of records in the local accumulator that need to be repartitioned in the current rebalance.",
    },
    CircuitMetric {
        name: INTEGRAL_RECORDS_TO_REPARTITION_COUNT,
        category: CircuitMetricCategory::Balancer,
        advanced: true,
        description: "The number of records in the local integral operator that need to be repartitioned in the current rebalance.",
    },
    CircuitMetric {
        name: TOTAL_REBALANCING_TIME_SECONDS,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Total time spent rebalancing the stream.",
    },
    CircuitMetric {
        name: INPROGRESS_REBALANCING_TIME_SECONDS,
        category: CircuitMetricCategory::Balancer,
        advanced: false,
        description: "Elapsed time for the current rebalance.",
    },
    // Cache
    CircuitMetric {
        name: CACHE_FOREGROUND_HITS,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Statistics about cache hits in the foreground thread.",
    },
    CircuitMetric {
        name: CACHE_FOREGROUND_MISSES,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Statistics about cache misses in the foreground thread.",
    },
    CircuitMetric {
        name: CACHE_BACKGROUND_HITS,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Statistics about cache hits in the background thread.",
    },
    CircuitMetric {
        name: CACHE_BACKGROUND_MISSES,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Statistics about cache misses in the background thread.",
    },
    CircuitMetric {
        name: CACHE_FOREGROUND_HIT_RATE_PERCENT,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Cache hit rate for the foreground thread.",
    },
    CircuitMetric {
        name: CACHE_BACKGROUND_HIT_RATE_PERCENT,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Cache hit rate for the background thread.",
    },
    CircuitMetric {
        name: FOREGROUND_CACHE_OCCUPANCY,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Occupancy of the foreground cache.",
    },
    CircuitMetric {
        name: BACKGROUND_CACHE_OCCUPANCY,
        category: CircuitMetricCategory::Cache,
        advanced: false,
        description: "Occupancy of the background cache.",
    },
    CircuitMetric {
        name: EXCHANGE_SERIALIZATION_TIME_SECONDS,
        category: CircuitMetricCategory::Multihost,
        advanced: false,
        description: "Time spent serializing data to send to other hosts.",
    },
    CircuitMetric {
        name: EXCHANGE_SERIALIZED_BYTES,
        category: CircuitMetricCategory::Multihost,
        advanced: false,
        description: "Amount of data serialized to send to other hosts.",
    },
    CircuitMetric {
        name: EXCHANGE_DESERIALIZATION_TIME_SECONDS,
        category: CircuitMetricCategory::Multihost,
        advanced: false,
        description: "Time spent deserializing data received from other hosts.",
    },
    CircuitMetric {
        name: EXCHANGE_DESERIALIZED_BYTES,
        category: CircuitMetricCategory::Multihost,
        advanced: false,
        description: "Amount of serialized data received from other hosts.",
    },
];

/// An operator's location within the source program
pub type OperatorLocation = Option<&'static Location<'static>>;

/// The label to a metadata item
pub type MetaLabel = Cow<'static, str>;

/// Stats about batch sizes.
///
/// Can be used to track the distribution of batch sizes in input/output streams.
/// Batches here don't have to be DBSP `Batch`s. These can be vector of tuples or
/// anything else that has a size.
// TODO: add a histogram.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct BatchSizeStats {
    /// Smallest batch size.
    min: usize,

    /// Largest batch size.
    max: usize,

    /// The number of batches.
    cnt: usize,

    /// Total size.
    total: usize,
}

impl BatchSizeStats {
    pub const fn new() -> Self {
        Self {
            min: usize::MAX,
            max: 0,
            cnt: 0,
            total: 0,
        }
    }

    pub fn add_batch(&mut self, size: usize) {
        self.cnt += 1;
        self.total = self.total.wrapping_add(size);
        self.min = if size < self.min { size } else { self.min };
        self.max = if size > self.max { size } else { self.max };
    }

    pub fn total_size(&self) -> usize {
        self.total
    }

    pub fn metadata(&self) -> MetaItem {
        MetaItem::Map(BTreeMap::from([
            (Cow::Borrowed("batches_count"), MetaItem::Count(self.cnt)),
            (
                Cow::Borrowed("min_records_count"),
                MetaItem::Count(if self.cnt == 0 { 0 } else { self.min }),
            ),
            (
                Cow::Borrowed("max_records_count"),
                MetaItem::Count(self.max),
            ),
            (
                Cow::Borrowed("avg_records_count"),
                MetaItem::Count(if self.cnt == 0 {
                    0
                } else {
                    self.total / self.cnt
                }),
            ),
            (
                Cow::Borrowed("total_records_count"),
                MetaItem::Count(self.total),
            ),
        ]))
    }
}

/// General metadata about an operator's execution
#[derive(Debug, Clone, PartialEq, Default)]
pub struct OperatorMeta {
    entries: BTreeMap<(MetricId, MetricLabels), MetaItem>,
}

#[derive(Serialize)]
struct MetricReadingRef<'a> {
    metric_id: &'a MetricId,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    labels: &'a MetricLabels,
    value: &'a MetaItem,
}

impl Serialize for OperatorMeta {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.entries.len()))?;
        for ((metric_id, labels), value) in &self.entries {
            let reading = MetricReadingRef {
                metric_id,
                labels,
                value,
            };
            seq.serialize_element(&reading)?;
        }
        seq.end()
    }
}

impl OperatorMeta {
    /// Create a new `OperatorMeta`
    pub const fn new() -> Self {
        Self {
            entries: BTreeMap::new(),
        }
    }

    pub fn get(&self, metric_id: MetricId) -> Option<MetaItem> {
        self.entries.get(&(metric_id, Vec::new())).cloned()
    }

    pub fn merge(&mut self, other: &Self) {
        for (label, src) in &other.entries {
            if src.is_mergeable() {
                if let Some(dst) = self.entries.get_mut(label) {
                    if let Some(merged) = src.merge(dst) {
                        *dst = merged;
                    } else {
                        self.entries.remove(label);
                    }
                } else {
                    self.entries.insert(label.clone(), src.clone());
                }
            }
        }
    }

    pub fn insert(&mut self, metric_id: MetricId, labels: MetricLabels, value: MetaItem) {
        self.entries.insert((metric_id, labels), value);
    }
}

impl Deref for OperatorMeta {
    type Target = BTreeMap<(MetricId, MetricLabels), MetaItem>;

    fn deref(&self) -> &Self::Target {
        &self.entries
    }
}

impl DerefMut for OperatorMeta {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.entries
    }
}

impl Extend<MetricReading> for OperatorMeta {
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = MetricReading>,
    {
        for reading in iter {
            self.entries
                .insert((reading.metric_id, reading.labels), reading.value);
        }
    }
}

impl<const N: usize> From<[MetricReading; N]> for OperatorMeta {
    fn from(array: [MetricReading; N]) -> Self {
        let mut this = Self::new();
        this.extend(array);
        this
    }
}

impl<'a> From<&'a [MetricReading]> for OperatorMeta {
    fn from(slice: &'a [MetricReading]) -> Self {
        let mut this = Self::new();
        this.extend(slice.iter().cloned());
        this
    }
}
/// An operator metadata entry
#[derive(Debug, Clone, PartialEq)]
pub enum MetaItem {
    /// An integer with no particular semantics.
    Int(usize),

    /// An integer count of something.
    ///
    /// This should be used for kinds of things that make sense to summarize by
    /// adding, e.g. counts of allocations or stored batches.
    Count(usize),

    /// A percentage in terms of a numerator and denominator. Separating these
    /// makes it possible to aggregate them.
    Percent {
        numerator: u64,
        denominator: u64,
    },

    CacheCounts(CacheCounts),

    String(String),
    Array(Vec<Self>),
    Map(BTreeMap<Cow<'static, str>, MetaItem>),
    Bytes(HumanBytes),
    Duration(Duration),
    Bool(bool),
}

#[derive(Serialize)]
struct PercentValue {
    numerator: u64,
    denominator: u64,
}

impl MetaItem {
    pub fn as_string(&self) -> Option<&str> {
        match self {
            MetaItem::String(string) => Some(string.as_str()),
            _ => None,
        }
    }
}

impl Serialize for MetaItem {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        fn serialize_typed<S, T>(
            serializer: S,
            type_name: &'static str,
            value: &T,
        ) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
            T: ?Sized + Serialize,
        {
            let mut state = serializer.serialize_struct("MetaItem", 2)?;
            state.serialize_field("type", type_name)?;
            state.serialize_field("value", value)?;
            state.end()
        }

        match self {
            MetaItem::Int(x) => {
                let value = *x as u64;
                serialize_typed(serializer, "int", &value)
            }
            MetaItem::Count(x) => {
                let value = *x as u64;
                serialize_typed(serializer, "count", &value)
            }
            MetaItem::Percent {
                numerator,
                denominator,
            } => {
                let value = PercentValue {
                    numerator: *numerator,
                    denominator: *denominator,
                };
                serialize_typed(serializer, "percent", &value)
            }
            MetaItem::CacheCounts(cache_counts) => {
                serialize_typed(serializer, "cachecounts", cache_counts)
            }
            MetaItem::String(x) => serialize_typed(serializer, "string", x),
            MetaItem::Array(meta_items) => meta_items.serialize(serializer),
            MetaItem::Map(operator_meta) => operator_meta.serialize(serializer),
            MetaItem::Bytes(human_bytes) => {
                let value = human_bytes.into_inner() as i128;
                serialize_typed(serializer, "bytes", &value)
            }
            MetaItem::Duration(duration) => serialize_typed(serializer, "duration", duration),
            MetaItem::Bool(bool) => serialize_typed(serializer, "bool", bool),
        }
    }
}

impl MetaItem {
    pub fn bytes(bytes: usize) -> Self {
        Self::Bytes(HumanBytes::from(bytes))
    }

    pub fn format(&self, output: &mut dyn Write) -> fmt::Result {
        match self {
            Self::Int(int) | Self::Count(int) => {
                write!(output, "{}", int.to_formatted_string(&Locale::en))
            }
            Self::Percent {
                numerator,
                denominator,
            } => {
                let percent = (*numerator as f64) / (*denominator as f64) * 100.0;
                if !percent.is_nan() && !percent.is_infinite() {
                    write!(output, "{percent:.02}%")
                } else {
                    write!(output, "(undefined)")
                }
            }
            Self::CacheCounts(CacheCounts {
                count,
                bytes,
                elapsed,
            }) => {
                if *count > 0 {
                    write!(
                        output,
                        "{count} ({}) over {:.1} s ({} ns/op)",
                        HumanBytes::new(*bytes),
                        elapsed.as_secs_f64(),
                        elapsed.as_nanos() / *count as u128
                    )
                } else {
                    write!(output, "none")
                }
            }
            Self::String(string) => output.write_str(string),
            Self::Bytes(bytes) => write!(output, "{bytes}"),
            Self::Duration(duration) => write!(output, "{duration:#?}"),

            Self::Array(array) => {
                output.write_char('[')?;
                for (idx, item) in array.iter().enumerate() {
                    item.format(output)?;
                    if idx != array.len() - 1 {
                        output.write_str(", ")?
                    }
                }
                output.write_char(']')
            }

            Self::Map(map) => {
                output.write_char('{')?;
                for (idx, (label, item)) in map.iter().enumerate() {
                    output.write_str(label)?;
                    output.write_str(": ")?;
                    item.format(output)?;

                    if idx != map.len() - 1 {
                        output.write_str(", ")?;
                    }
                }
                output.write_char('}')
            }
            Self::Bool(bool) => write!(output, "{bool}"),
        }
    }

    pub fn is_mergeable(&self) -> bool {
        matches!(
            self,
            MetaItem::Count(_)
                | MetaItem::Bytes(_)
                | MetaItem::CacheCounts(..)
                | MetaItem::Duration(_)
                | MetaItem::Percent { .. }
        )
    }

    pub fn merge(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (Self::Count(a), Self::Count(b)) => Some(Self::Count(a + b)),
            (
                Self::Percent {
                    numerator: an,
                    denominator: ad,
                },
                Self::Percent {
                    numerator: bn,
                    denominator: bd,
                },
            ) => Some(Self::Percent {
                numerator: an + bn,
                denominator: ad + bd,
            }),
            (Self::Bytes(a), Self::Bytes(b)) => Some(Self::Bytes(HumanBytes {
                bytes: a.bytes + b.bytes,
            })),
            (Self::CacheCounts(a), Self::CacheCounts(b)) => Some(Self::CacheCounts(*a + *b)),
            (Self::Duration(a), Self::Duration(b)) => Some(Self::Duration(a.saturating_add(*b))),
            _ => None,
        }
    }
}

impl Default for MetaItem {
    fn default() -> Self {
        Self::String(String::new())
    }
}

impl From<Duration> for MetaItem {
    fn from(duration: Duration) -> Self {
        Self::Duration(duration)
    }
}

impl From<HumanBytes> for MetaItem {
    fn from(bytes: HumanBytes) -> Self {
        Self::Bytes(bytes)
    }
}

impl From<String> for MetaItem {
    fn from(string: String) -> Self {
        Self::String(string)
    }
}

impl From<usize> for MetaItem {
    fn from(int: usize) -> Self {
        Self::Int(int)
    }
}

impl From<bool> for MetaItem {
    fn from(bool: bool) -> Self {
        Self::Bool(bool)
    }
}

#[macro_export]
macro_rules! metadata {
    ($($name:expr_2021 => $value:expr_2021),* $(,)?) => {
        [$(($crate::circuit::metadata::MetricReading::new($name, Vec::new(), $crate::circuit::metadata::MetaItem::from($value))),)*]
    };
}

impl From<TotalSize> for MetaItem {
    fn from(size: TotalSize) -> Self {
        Self::Map(BTreeMap::from([
            (
                Cow::Borrowed("allocated bytes"),
                Self::bytes(size.total_bytes()),
            ),
            (Cow::Borrowed("used bytes"), Self::bytes(size.used_bytes())),
            (
                Cow::Borrowed("allocations"),
                Self::Count(size.distinct_allocations()),
            ),
            (
                Cow::Borrowed("shared bytes"),
                Self::bytes(size.shared_bytes()),
            ),
        ]))
    }
}