|
| 1 | +// Copyright 2025, DragonflyDB authors. All rights reserved. |
| 2 | +// See LICENSE for licensing terms. |
| 3 | +// |
| 4 | + |
| 5 | +#include "core/count_min_sketch.h" |
| 6 | + |
| 7 | +#include <absl/time/clock.h> |
| 8 | +#include <xxhash.h> |
| 9 | + |
| 10 | +#include <cmath> |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +constexpr auto MAX = std::numeric_limits<dfly::CountMinSketch::SizeT>::max(); |
| 16 | + |
| 17 | +uint64_t GetCurrentMS() { |
| 18 | + return absl::GetCurrentTimeNanos() / 1000 / 1000; |
| 19 | +} |
| 20 | + |
| 21 | +// Value halves every 5000 ms: ln(2) / 5000 |
| 22 | +constexpr double EXP_DECAY_CONST = 0.000138629; |
| 23 | + |
| 24 | +uint64_t ExponentialDecay(uint64_t value, int64_t time_delta) { |
| 25 | + return value * std::exp(-time_delta * EXP_DECAY_CONST); |
| 26 | +} |
| 27 | + |
| 28 | +// Value decrements by one every 1000 ms |
| 29 | +constexpr double LIN_DECAY_CONST = 0.001; |
| 30 | + |
| 31 | +uint64_t LinearDecay(uint64_t value, int64_t time_delta) { |
| 32 | + const double decay = time_delta * LIN_DECAY_CONST; |
| 33 | + return value - std::min(static_cast<double>(value), decay); |
| 34 | +} |
| 35 | + |
| 36 | +uint64_t ApplyDecay(uint64_t value, int64_t time_delta, dfly::MultiSketch::Decay decay) { |
| 37 | + switch (decay) { |
| 38 | + case dfly::MultiSketch::Decay::Exponential: |
| 39 | + return ExponentialDecay(value, time_delta); |
| 40 | + case dfly::MultiSketch::Decay::Linear: |
| 41 | + return LinearDecay(value, time_delta); |
| 42 | + case dfly::MultiSketch::Decay::SlidingWindow: |
| 43 | + return value; |
| 44 | + } |
| 45 | + ABSL_UNREACHABLE(); |
| 46 | +} |
| 47 | + |
| 48 | +} // namespace |
| 49 | + |
| 50 | +namespace dfly { |
| 51 | + |
| 52 | +CountMinSketch::CountMinSketch(double epsilon, double delta) { |
| 53 | + width_ = std::exp(1) / epsilon; |
| 54 | + depth_ = std::log(1.0 / delta); |
| 55 | + counters_.reserve(depth_); |
| 56 | + for (uint64_t i = 0; i < depth_; ++i) { |
| 57 | + counters_.emplace_back(width_, 0); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void CountMinSketch::Update(uint64_t key, CountMinSketch::SizeT incr) { |
| 62 | + uint64_t i = 0; |
| 63 | + std::for_each(counters_.begin(), counters_.end(), [&](auto& ctr) { |
| 64 | + const uint64_t index = Hash(key, i++); |
| 65 | + const SizeT curr = ctr[index]; |
| 66 | + const SizeT updated = curr + incr; |
| 67 | + ctr[index] = updated < curr ? MAX : updated; |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +CountMinSketch::SizeT CountMinSketch::EstimateFrequency(uint64_t key) const { |
| 72 | + uint64_t i = 0; |
| 73 | + auto it = counters_.begin(); |
| 74 | + SizeT estimate = (*it)[Hash(key, i++)]; |
| 75 | + std::for_each(++it, counters_.end(), |
| 76 | + [&](const auto& ctr) { estimate = std::min(estimate, ctr[Hash(key, i++)]); }); |
| 77 | + return estimate; |
| 78 | +} |
| 79 | + |
| 80 | +void CountMinSketch::Reset() { |
| 81 | + for (auto& ctr : counters_) { |
| 82 | + std::fill(ctr.begin(), ctr.end(), 0); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +uint64_t CountMinSketch::Hash(uint64_t key, uint64_t i) const { |
| 87 | + return XXH3_64bits_withSeed(&key, sizeof(key), i) % width_; |
| 88 | +} |
| 89 | + |
| 90 | +MultiSketch::MultiSketch(uint64_t rollover_ms, double epsilon, double delta, Decay decay) |
| 91 | + : rollover_ms_(rollover_ms), current_sketch_(sketches_.size() - 1), decay_t_(decay) { |
| 92 | + uint64_t now = GetCurrentMS(); |
| 93 | + for (uint64_t i = 0; i < sketches_.size(); ++i) { |
| 94 | + sketches_[i] = SketchWithTimestamp{CountMinSketch{epsilon, delta}, now, now}; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void MultiSketch::Update(uint64_t key, CountMinSketch::SizeT incr) { |
| 99 | + if (++rollover_check_ >= rollover_check_every_) { |
| 100 | + MaybeRolloverCurrentSketch(); |
| 101 | + rollover_check_ = 0; |
| 102 | + } |
| 103 | + sketches_[current_sketch_].sketch_.Update(key, incr); |
| 104 | +} |
| 105 | + |
| 106 | +CountMinSketch::SizeT MultiSketch::EstimateFrequency(uint64_t key) const { |
| 107 | + CountMinSketch::SizeT estimate = 0; |
| 108 | + const uint64_t now = GetCurrentMS(); |
| 109 | + |
| 110 | + for (const auto& sketch : sketches_) { |
| 111 | + const auto e = sketch.sketch_.EstimateFrequency(key); |
| 112 | + // TODO use average time of sketch to compute delta |
| 113 | + estimate += ApplyDecay(e, now - sketch.start_time_, decay_t_); |
| 114 | + } |
| 115 | + return estimate; |
| 116 | +} |
| 117 | + |
| 118 | +void MultiSketch::MaybeRolloverCurrentSketch() { |
| 119 | + const uint64_t now = GetCurrentMS(); |
| 120 | + const uint64_t oldest = (current_sketch_ + 1) % sketches_.size(); |
| 121 | + if (const uint64_t oldest_ts = sketches_[oldest].start_time_; now - oldest_ts > rollover_ms_) { |
| 122 | + sketches_[oldest].sketch_.Reset(); |
| 123 | + sketches_[oldest].start_time_ = now; |
| 124 | + sketches_[current_sketch_].end_time_ = now; |
| 125 | + current_sketch_ = oldest; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace dfly |
0 commit comments