|
| 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 <functional> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +constexpr auto MAX = std::numeric_limits<dfly::CountMinSketch::SizeT>::max(); |
| 17 | + |
| 18 | +uint64_t GetCurrentMS() { |
| 19 | + return absl::GetCurrentTimeNanos() / 1000 / 1000; |
| 20 | +} |
| 21 | + |
| 22 | +uint64_t ExponentialDecay(uint64_t value, int64_t time_delta) { |
| 23 | + // Value halves every 5000 ms: ln(2) / 5000 |
| 24 | + static constexpr double EXP_DECAY_CONST = 0.000138629; |
| 25 | + |
| 26 | + return value * std::exp(-time_delta * EXP_DECAY_CONST); |
| 27 | +} |
| 28 | + |
| 29 | +uint64_t LinearDecay(uint64_t value, int64_t time_delta) { |
| 30 | + // Value decrements by one every 1000 ms |
| 31 | + static constexpr double LIN_DECAY_CONST = 0.001; |
| 32 | + |
| 33 | + const double decay = time_delta * LIN_DECAY_CONST; |
| 34 | + return value - std::min(static_cast<double>(value), decay); |
| 35 | +} |
| 36 | + |
| 37 | +using DecayFn = std::function<uint64_t(uint64_t, int64_t)>; |
| 38 | + |
| 39 | +std::array<DecayFn, 3> decay_fns = {ExponentialDecay, LinearDecay, [](auto v, auto) { return v; }}; |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +namespace dfly { |
| 44 | + |
| 45 | +CountMinSketch::CountMinSketch(double epsilon, double delta) { |
| 46 | + width_ = std::exp(1) / epsilon; |
| 47 | + depth_ = std::log(1.0 / delta); |
| 48 | + counters_.reserve(depth_); |
| 49 | + for (uint64_t i = 0; i < depth_; ++i) { |
| 50 | + counters_.emplace_back(width_, 0); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void CountMinSketch::Update(uint64_t key, CountMinSketch::SizeT incr) { |
| 55 | + uint64_t i = 0; |
| 56 | + std::for_each(counters_.begin(), counters_.end(), [&](auto& counter) { |
| 57 | + // It is possible to compute just two initial hashes and then use them to derive next i-2 |
| 58 | + // hashes, but it results in a lot more collisions and thus much larger overestimates. |
| 59 | + const uint64_t index = Hash(key, i++); |
| 60 | + const SizeT curr = counter[index]; |
| 61 | + const SizeT updated = curr + incr; |
| 62 | + counter[index] = updated < curr ? MAX : updated; |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +CountMinSketch::SizeT CountMinSketch::EstimateFrequency(uint64_t key) const { |
| 67 | + SizeT estimate = MAX; |
| 68 | + for (uint64_t i = 0; i < counters_.size(); ++i) { |
| 69 | + estimate = std::min(estimate, counters_[i][Hash(key, i)]); |
| 70 | + } |
| 71 | + return estimate; |
| 72 | +} |
| 73 | + |
| 74 | +void CountMinSketch::Reset() { |
| 75 | + for (auto& ctr : counters_) { |
| 76 | + std::fill(ctr.begin(), ctr.end(), 0); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +uint64_t CountMinSketch::Hash(uint64_t key, uint64_t i) const { |
| 81 | + return XXH3_64bits_withSeed(&key, sizeof(key), i) % width_; |
| 82 | +} |
| 83 | + |
| 84 | +MultiSketch::MultiSketch(uint64_t rollover_ms, double epsilon, double delta, Decay decay) |
| 85 | + : rollover_ms_(rollover_ms), current_sketch_(sketches_.size() - 1), decay_t_(decay) { |
| 86 | + const uint64_t now = GetCurrentMS(); |
| 87 | + for (uint64_t i = 0; i < sketches_.size(); ++i) { |
| 88 | + sketches_[i] = SketchWithTimestamp{CountMinSketch{epsilon, delta}, now, now}; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void MultiSketch::Update(uint64_t key, CountMinSketch::SizeT incr) { |
| 93 | + if (++rollover_check_ >= rollover_check_every_) { |
| 94 | + MaybeRolloverCurrentSketch(); |
| 95 | + rollover_check_ = 0; |
| 96 | + } |
| 97 | + sketches_[current_sketch_].sketch_.Update(key, incr); |
| 98 | +} |
| 99 | + |
| 100 | +CountMinSketch::SizeT MultiSketch::EstimateFrequency(uint64_t key) const { |
| 101 | + CountMinSketch::SizeT estimate = 0; |
| 102 | + const uint64_t now = GetCurrentMS(); |
| 103 | + |
| 104 | + for (const auto& sketch : sketches_) { |
| 105 | + const auto e = sketch.sketch_.EstimateFrequency(key); |
| 106 | + // TODO use average time of sketch to compute delta |
| 107 | + estimate += decay_fns[static_cast<uint8_t>(decay_t_)](e, now - sketch.start_time_); |
| 108 | + } |
| 109 | + return estimate; |
| 110 | +} |
| 111 | + |
| 112 | +void MultiSketch::MaybeRolloverCurrentSketch() { |
| 113 | + const uint64_t now = GetCurrentMS(); |
| 114 | + const uint64_t oldest = (current_sketch_ + 1) % sketches_.size(); |
| 115 | + if (const uint64_t oldest_ts = sketches_[oldest].start_time_; now - oldest_ts > rollover_ms_) { |
| 116 | + sketches_[oldest].sketch_.Reset(); |
| 117 | + sketches_[oldest].start_time_ = now; |
| 118 | + sketches_[current_sketch_].end_time_ = now; |
| 119 | + current_sketch_ = oldest; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace dfly |
0 commit comments