libdd-ddsketch 1.1.0

Minimal implementation of Datadog's DDSketch
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
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

//! This crate defines a minimal implementation of DDSketch.
//!
//! DDSketch is a data sketch used to generate percentiles over streaming data using constant
//! memory. A DDSketch is essentially a histogram that partitions the range of positive values into
//! an infinite number of indexed bins whose size grows exponentially. It keeps track of the number
//! of values (or possibly floating-point weights) added to each bin. Negative values are
//! partitioned like positive values, symmetrically to zero. The value zero as well as its close
//! neighborhood that would be mapped to extreme bin indexes is mapped to a specific counter.

#![cfg_attr(not(test), deny(clippy::panic))]
#![cfg_attr(not(test), deny(clippy::unwrap_used))]
#![cfg_attr(not(test), deny(clippy::expect_used))]
#![cfg_attr(not(test), deny(clippy::todo))]
#![cfg_attr(not(test), deny(clippy::unimplemented))]

use std::collections::{HashMap, VecDeque};

use prost::Message;

/// Protobuf representation of DDSketch - generated by build.rs
#[rustfmt::skip]
pub mod pb;

/// This is a minimal DDSketch implementation
///
/// This implementation only supports a part of the standard (which is also only the parts dd
/// backend supports :shrug:)
/// - max length contiguous bin store, with lower bin collapse behavior.
/// - Positive or zero values
///
/// The default sketch has a 1% relative accuracy, and only accepts positive points
///
/// See <https://github.com/DataDog/sketches-go> for the reference implementation
#[derive(Debug, Default, Clone)]
pub struct DDSketch {
    store: LowCollapsingDenseStore, // Store the weight of each bin
    zero_count: f64,                // Store the weight of the bin of value 0
    mapping: LogMapping,            // Bin-Value mapping
}

impl DDSketch {
    /// Return an iterator over `(value, weight)` pair for each bin
    pub fn ordered_bins(&self) -> Vec<(f64, f64)> {
        let mut bins: Vec<_> = std::iter::once((0.0, self.zero_count))
            .chain(self.store.bins().map(|(b, v)| (self.mapping.value(b), v)))
            .collect();
        bins.sort_by(|a, b| a.0.total_cmp(&b.0));
        bins
    }

    // Return the number of points in the sketch
    pub fn count(&self) -> f64 {
        self.zero_count + self.store.bins.iter().sum::<f64>()
    }

    /// Add a point with value `point` to the sketch
    /// `point` must be positive
    pub fn add(&mut self, point: f64) -> Result<(), Box<dyn std::error::Error>> {
        self.add_with_count(point, 1.0)
    }

    /// Add `count` point with value `point` to the sketch
    /// `count` and `point` must be positive
    pub fn add_with_count(
        &mut self,
        point: f64,
        count: f64,
    ) -> Result<(), Box<dyn std::error::Error>> {
        if count.is_nan() || count.is_infinite() {
            return Err("count is invalid".into());
        }
        if point < 0.0 || point.is_nan() || point.is_infinite() {
            return Err("point is invalid".into());
        } else if point < self.mapping.min_indexable_value {
            self.zero_count += count;
        } else {
            let index = self.mapping.index(point);
            *self.store.bin_mut(index) += count;
        }
        Ok(())
    }

    /// Return a protobuf of the sketch
    pub fn into_pb(self) -> pb::DdSketch {
        let contiguous_bins: Vec<f64> = self.store.bins.into();
        pb::DdSketch {
            mapping: Some(pb::IndexMapping {
                gamma: self.mapping.gamma,
                index_offset: self.mapping.index_offset,
                interpolation: pb::index_mapping::Interpolation::None.into(),
            }),
            positive_values: Some(pb::Store {
                bin_counts: HashMap::new(),
                contiguous_bin_counts: contiguous_bins,
                contiguous_bin_index_offset: self.store.offset,
            }),
            zero_count: self.zero_count,
            negative_values: Some(pb::Store {
                bin_counts: HashMap::new(),
                contiguous_bin_counts: Vec::new(),
                contiguous_bin_index_offset: 0,
            }),
        }
    }

    /// Return a serialized protobuf of the sketch
    pub fn encode_to_vec(self) -> Vec<u8> {
        self.into_pb().encode_to_vec()
    }

    /// Reconstruct a sketch from its protobuf. `None` if mapping missing/invalid.
    pub fn from_pb(sketch: pb::DdSketch) -> Option<DDSketch> {
        let mapping_pb = sketch.mapping?;
        let mapping = LogMapping::new(mapping_pb.gamma, mapping_pb.index_offset)?;
        let store = match sketch.positive_values {
            Some(s) => LowCollapsingDenseStore {
                bins: s.contiguous_bin_counts.into(),
                offset: s.contiguous_bin_index_offset,
                max_size: LowCollapsingDenseStore::default().max_size,
            },
            None => LowCollapsingDenseStore::default(),
        };
        Some(DDSketch {
            store,
            zero_count: sketch.zero_count,
            mapping,
        })
    }

    /// Decode a sketch from serialized protobuf bytes; `None` on decode/mapping failure.
    pub fn from_encoded(bytes: &[u8]) -> Option<DDSketch> {
        Self::from_pb(pb::DdSketch::decode(bytes).ok()?)
    }
}

/// A store mapping the bin indexes to their respective weights
///
/// Stores the weights as contiguousBinCounts, only the bins within `offset` and the highest
/// non empty bin are stored.
///
/// Stores the weights of a contiguous range of bins containing all non-empty bins. The range start
/// at index `offset` and end at index `offset + bins.len()`
///
/// The range of stored bins is updated when accessing the index of a bin out of the range
/// with [`Self::bin_idx_to_bin_idx()`]. If the `max_size` is reached the lower bins are
/// collapsed together to free space.
#[derive(Debug, Clone)]
struct LowCollapsingDenseStore {
    bins: VecDeque<f64>,
    offset: i32,
    max_size: i32,
}

impl LowCollapsingDenseStore {
    fn new(max_size: i32) -> Option<Self> {
        if max_size < 0 {
            return None;
        }
        Some(Self {
            bins: VecDeque::new(),
            offset: 0,
            max_size,
        })
    }

    /// Return an iterator over the bins
    /// The iterator yields pairs `(bin_index, count)`
    fn bins(&self) -> impl Iterator<Item = (i32, f64)> + '_ {
        self.bins
            .iter()
            .enumerate()
            .map(|(i, &v)| (i as i32 + self.offset, v))
    }

    /// Return a mutable reference to the bin at index `bin_index`
    fn bin_mut(&mut self, bin_index: i32) -> &mut f64 {
        let store_index = self.bin_idx_to_store_idx(bin_index);
        &mut self.bins[store_index]
    }

    /// Return the `store_index` of the bin at index `bin_index`
    fn bin_idx_to_store_idx(&mut self, bin_index: i32) -> usize {
        if self.bins.is_empty() {
            // If the bins are empty, start them at the index
            self.offset = bin_index;
            self.bins.push_back(0.0);
            return 0;
        }

        // General case
        // Bucket lower than the stored range
        if bin_index < self.offset {
            let additional_low_bins = self.offset - bin_index;
            debug_assert!(additional_low_bins >= 0);

            let additional_low_bins = std::cmp::min(
                additional_low_bins as usize,
                self.max_size as usize - self.bins.len(),
            );

            self.bins.reserve(additional_low_bins);
            for _ in 0..additional_low_bins {
                self.bins.push_front(0.0);
            }

            self.offset -= additional_low_bins as i32;
            0
        }
        // Bucket higher than the stored range
        else if self.offset + self.bins.len() as i32 <= bin_index {
            let bin_range_size = bin_index - self.offset + 1; // Number of bucket to store

            if bin_range_size > self.max_size {
                self.collapse_low_bins(bin_range_size - self.max_size);
            }
            debug_assert!(self.bins.len() as i32 <= self.max_size);

            let store_index = bin_index - self.offset;
            for _ in 0..(store_index - self.bins.len() as i32 + 1) {
                self.bins.push_back(0.0);
            }
            store_index as usize
        }
        // Bucket within the stored range
        else {
            (bin_index - self.offset) as usize
        }
    }

    /// Collapse the `bin_number` lowest bins
    fn collapse_low_bins(&mut self, bin_number: i32) {
        let mut count = 0.0;
        for _ in 0..bin_number {
            count += self.bins.pop_front().unwrap_or(0.0);
        }
        if let Some(lowest_bin) = self.bins.front_mut() {
            *lowest_bin += count;
        } else {
            self.bins.push_front(count);
        }
        self.offset += bin_number;
    }
}

impl Default for LowCollapsingDenseStore {
    fn default() -> Self {
        #[allow(clippy::unwrap_used)]
        Self::new(2048).unwrap()
    }
}

/// Logarithmic mapping of bucket index to value
#[derive(Debug, Clone, Copy)]
struct LogMapping {
    gamma: f64,
    multiplier: f64,
    min_indexable_value: f64,
    index_offset: f64,
}

impl LogMapping {
    fn new(gamma: f64, offset: f64) -> Option<Self> {
        if gamma <= 1.0 {
            return None;
        }
        let multiplier = Self::multiplier_from_gamma(gamma);
        Some(Self {
            gamma,
            multiplier,
            min_indexable_value: max(
                // So that the value representing the lowest bucket is >= std::f64::MIN_POSITIVE
                f64::MIN_POSITIVE * gamma,
                // Minimum value so that index >= i32::MIN
                ((i32::MIN as f64 - offset) / multiplier + 1.0).exp(),
            )?,
            index_offset: offset,
        })
    }

    /// Returns the multiplier used to convert ln to base-gamma logarithm
    fn multiplier_from_gamma(gamma: f64) -> f64 {
        1.0 / gamma.ln()
    }

    /// Returns the relative accuracy guaranteed by the mapping
    fn relative_accuracy(&self) -> f64 {
        1.0 - 2.0 / (1.0 + self.gamma)
    }

    /// Returns the index of the bucket containing `value`
    fn index(&self, value: f64) -> i32 {
        (value.ln() * self.multiplier + self.index_offset).floor() as i32
    }

    /// Returns the value representing the bucket at `index`
    fn value(&self, index: i32) -> f64 {
        ((index as f64 - self.index_offset) / self.multiplier).exp()
            * (1.0 + self.relative_accuracy())
    }
}

impl Default for LogMapping {
    fn default() -> Self {
        const RELATIVE_ACCURACY: f64 = 0.007751937984496124;
        const GAMMA: f64 = (1.0 + RELATIVE_ACCURACY) / (1.0 - RELATIVE_ACCURACY);

        const BACKEND_SKETCH_MIN_VALUE: f64 = 1e-9;
        // offset used in datadog's backend for sketches
        let offset: f64 = (1.0 - (BACKEND_SKETCH_MIN_VALUE.ln() / GAMMA.ln()).floor()) + 0.5;

        #[allow(clippy::unwrap_used)]
        Self::new(GAMMA, offset).unwrap()
    }
}

fn max(a: f64, b: f64) -> Option<f64> {
    if a.is_nan() || b.is_nan() {
        None
    } else if a > b {
        Some(a)
    } else {
        Some(b)
    }
}

#[cfg(test)]
mod test {
    use prost::Message;

    use super::*;

    macro_rules! assert_within {
        ($x:expr, $y:expr, $tolerance:expr) => {
            let diff = $x - $y;
            assert!(
                -$tolerance < diff && diff < $tolerance,
                "x: {} y: {}",
                $x,
                $y,
            );
        };
    }

    #[test]
    fn test_exponential_mapping_within_tolerances() {
        let mapping = LogMapping::default();

        let values: &[f64] = &[1e-30, 0.1, 2.0, 10.0, 25.0, 10000.0];
        for &value in values {
            let index = mapping.index(value);
            let value_bucket = mapping.value(index);

            assert_within!(value_bucket / value, 1.0, 0.01);
        }
    }

    #[test]
    fn test_exponential_mapping_relative_accuracy() {
        let mapping = LogMapping::default();

        assert_within!(
            mapping.relative_accuracy(),
            0.007751937984496138,
            f64::EPSILON
        );
    }

    #[test]
    fn test_sketch_add() {
        let mut sketch = DDSketch::default();
        let points: &[f64] = &[0.0, 1e-5, 0.1, 2.0, 10.0, 25.0, 10000.0];
        for (i, &point) in points.iter().enumerate() {
            assert!(sketch.add_with_count(point, i as f64 + 1.0).is_ok());
        }

        dbg!(sketch.store.bins.len(), sketch.store.offset);

        for (i, (value, count)) in sketch
            .ordered_bins()
            .into_iter()
            .filter(|(_, p)| *p != 0.0)
            .enumerate()
        {
            if points[i] == 0.0 {
                assert_within!(value, 0.0, f64::EPSILON);
                assert_within!(count, i as f64 + 1.0, f64::EPSILON);
            } else {
                assert_within!(value / points[i], 1.0, 0.01);
                assert_within!(count, i as f64 + 1.0, f64::EPSILON);
            }
        }
    }

    #[test]
    fn test_skecth_add_negative() {
        let mut sketch = DDSketch::default();
        assert!(sketch.add(-1.0).is_err());
    }

    #[test]
    fn test_skecth_add_nan() {
        let mut sketch = DDSketch::default();
        assert!(sketch.add(f64::NAN).is_err());
    }

    #[test]
    fn test_sketch_count_add_with_count() {
        let mut sketch = DDSketch::default();
        assert_within!(sketch.count(), 0.0, f64::EPSILON);

        let points: &[f64] = &[0.0, 1e-30, 0.1, 2.0, 10.0, 25.0, 10000.0];
        for (i, &point) in points.iter().enumerate() {
            assert!(sketch.add_with_count(point, i as f64).is_ok());
        }

        assert_within!(sketch.count(), 21.0, f64::EPSILON);
    }

    #[test]
    fn test_sketch_count_add() {
        let mut sketch = DDSketch::default();
        assert_within!(sketch.count(), 0.0, f64::EPSILON);

        let points: &[f64] = &[0.0, 1e-30, 0.1, 2.0, 10.0, 25.0, 10000.0];
        for &point in points.iter() {
            assert!(sketch.add(point).is_ok());
        }
        assert_within!(sketch.count(), 7.0, f64::EPSILON);

        assert!(sketch.add(1.0).is_ok());
        assert_within!(sketch.count(), 8.0, f64::EPSILON);

        for n in 0..100 {
            assert!(sketch.add(n as f64).is_ok());
        }
        assert_within!(sketch.count(), 108.0, f64::EPSILON);
    }

    #[test]
    fn test_sketch_pb_roundtrip() {
        let mut sketch = DDSketch::default();
        for &p in &[0.0_f64, 0.1, 2.0, 10.0, 25.0, 10000.0] {
            assert!(sketch.add(p).is_ok());
        }
        let expected_count = sketch.count();
        let expected_bins = sketch.ordered_bins().len();
        let decoded = DDSketch::from_pb(sketch.into_pb()).expect("decodes");
        assert_within!(decoded.count(), expected_count, f64::EPSILON);
        assert_eq!(decoded.ordered_bins().len(), expected_bins);
    }

    #[test]
    fn test_skecth_encode() {
        let mut sketch = DDSketch::default();
        let points: &[f64] = &[0.0, 1e-30, 0.1, 2.0, 10.0, 25.0, 10000.0];
        for (i, &point) in points.iter().enumerate() {
            assert!(sketch.add_with_count(point, i as f64).is_ok());
        }

        let pb_sketch = sketch.into_pb().encode_to_vec();
        assert!(!pb_sketch.is_empty());
    }

    #[test]
    fn test_low_collapsing_store() {
        let mut store = LowCollapsingDenseStore::new(5).unwrap();

        // Test initial push up to capacity
        for i in 0..5 {
            *store.bin_mut(i + 10) = 1.0;
        }
        for (i, b) in store.bins().enumerate() {
            assert_eq!(b.0, i as i32 + 10);
            assert_eq!(b.1, 1.0)
        }

        // Indexing existing bins
        for i in 0..5 {
            *store.bin_mut(i + 10) += 1.0;
        }
        for (i, b) in store.bins().enumerate() {
            assert_eq!(b.0, i as i32 + 10);
            assert_eq!(b.1, 2.0)
        }
    }

    #[test]
    fn test_low_collapsing_store_low_bins_are_collapsed() {
        let mut store = LowCollapsingDenseStore::new(5).unwrap();

        // Test initial push up to capacity to max
        for i in 0..5 {
            *store.bin_mut(i + 10) = 1.0;
        }

        // Indexing low bins at max capacity
        for i in 0..3 {
            *store.bin_mut(i) += 1.0;
        }
        for (i, b) in store.bins().enumerate() {
            assert_eq!(b.0, i as i32 + 10);
            if i == 0 {
                assert_eq!(b.1, 4.0)
            } else {
                assert_eq!(b.1, 1.0)
            }
        }

        // Indexing higer bins collapses lower bins
        *store.bin_mut(15) = 1.0;
        for (i, b) in store.bins().enumerate() {
            assert_eq!(b.0, i as i32 + 11);
            if i == 0 {
                assert_eq!(b.1, 5.0)
            } else {
                assert_eq!(b.1, 1.0)
            }
        }
    }

    #[test]
    fn test_low_collapsing_store_up_expansion() {
        let mut store = LowCollapsingDenseStore::new(3).unwrap();

        *store.bin_mut(1) = 1.0;
        *store.bin_mut(3) = 1.0;
        assert_eq!(
            store.bins().collect::<Vec<_>>(),
            &[(1, 1.0), (2, 0.0), (3, 1.0)]
        )
    }

    #[test]
    fn test_low_collapsing_store_down_expansion() {
        let mut store = LowCollapsingDenseStore::new(3).unwrap();

        *store.bin_mut(3) = 1.0;
        *store.bin_mut(1) = 1.0;
        assert_eq!(
            store.bins().collect::<Vec<_>>(),
            &[(1, 1.0), (2, 0.0), (3, 1.0)]
        )
    }
}