:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(propvals): add a seen cache to the events worker (#99883)
Andy Zhao40d8a30d2cdd
diff --git a/rust/property-vals-rs/src/config.rs b/rust/property-vals-rs/src/config.rsindex 2c21c8145e0c..ad4b277f30fb 100644--- a/rust/property-vals-rs/src/config.rs+++ b/rust/property-vals-rs/src/config.rs@@ -53,6 +53,9 @@ pub struct Config {#[envconfig(nested = true)]pub length_caps: LengthCaps,+ #[envconfig(default = "0")]+ pub events_seen_cache_capacity: usize,+#[envconfig(default = "0")]pub merger_seen_cache_capacity: usize,diff --git a/rust/property-vals-rs/src/main.rs b/rust/property-vals-rs/src/main.rsindex d0ec8db02b11..50e620703c97 100644--- a/rust/property-vals-rs/src/main.rs+++ b/rust/property-vals-rs/src/main.rs@@ -162,7 +162,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {events_handle.clone(),move |e: &Event| fan_out(e, &excluded_events, length_caps),"events",- ReductionConfig::default(),+ ReductionConfig {+ max_values_per_key: 0,+ seen_cache_capacity: shared_config.events_seen_cache_capacity,+ },));tokio::spawn(worker_loop::<GroupIdentify, _, _>(shared_config.clone(),diff --git a/rust/property-vals-rs/src/metrics_consts.rs b/rust/property-vals-rs/src/metrics_consts.rsindex 70d4a9957543..eb47335e938e 100644--- a/rust/property-vals-rs/src/metrics_consts.rs+++ b/rust/property-vals-rs/src/metrics_consts.rs@@ -15,3 +15,4 @@ pub const TOP_K_DROPPED: &str = "property_vals_rs_top_k_dropped_total";pub const SEEN_CACHE_HITS: &str = "property_vals_rs_seen_cache_hits_total";pub const SEEN_CACHE_MISSES: &str = "property_vals_rs_seen_cache_misses_total";pub const SEEN_CACHE_EVICTED: &str = "property_vals_rs_seen_cache_evicted_total";+pub const SEEN_CACHE_ROLLS: &str = "property_vals_rs_seen_cache_rolls_total";diff --git a/rust/property-vals-rs/src/seen_cache.rs b/rust/property-vals-rs/src/seen_cache.rsindex 78ae4eabf0df..b5fcf869a44b 100644--- a/rust/property-vals-rs/src/seen_cache.rs+++ b/rust/property-vals-rs/src/seen_cache.rs@@ -1,6 +1,11 @@+use std::sync::atomic::{AtomicU64, Ordering};+use std::time::{SystemTime, UNIX_EPOCH};+use quick_cache::{sync, DefaultHashBuilder, Lifecycle, UnitWeighter};-use crate::metrics_consts::{SEEN_CACHE_EVICTED, SEEN_CACHE_HITS, SEEN_CACHE_MISSES};+use crate::metrics_consts::{+ SEEN_CACHE_EVICTED, SEEN_CACHE_HITS, SEEN_CACHE_MISSES, SEEN_CACHE_ROLLS,+};use crate::types::TupleKey;#[derive(Clone)]@@ -23,6 +28,14 @@ type Inner = sync::Cache<TupleKey, (), UnitWeighter, DefaultHashBuilder, Evictinpub struct SeenCache {cache: Inner,worker: &'static str,+ day: AtomicU64,+}++pub fn utc_day() -> u64 {+ SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .map(|since_epoch| since_epoch.as_secs() / 86_400)+ .unwrap_or(0)}impl SeenCache {@@ -34,7 +47,19 @@ impl SeenCache {DefaultHashBuilder::default(),EvictingLifecycle { worker },);- Self { cache, worker }+ Self {+ cache,+ worker,+ day: AtomicU64::new(utc_day()),+ }+ }++ pub fn roll(&self, day: u64) {+ if self.day.swap(day, Ordering::AcqRel) == day {+ return;+ }+ self.cache.clear();+ metrics::counter!(SEEN_CACHE_ROLLS, "worker" => self.worker).increment(1);}pub fn seen(&self, key: &TupleKey) -> bool {@@ -51,3 +76,31 @@ impl SeenCache {self.cache.insert(key.clone(), ());}}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::types::PropertyType;++ fn tuple(value: &str) -> TupleKey {+ TupleKey {+ team_id: 2,+ property_type: PropertyType::Event,+ property_key: "$browser".to_string(),+ property_value: value.to_string(),+ }+ }++ #[test]+ fn roll_clears_entries_only_when_the_day_changes() {+ let cache = SeenCache::new(1000, "test");+ cache.roll(100);+ cache.insert(&tuple("Chrome"));++ cache.roll(100);+ assert!(cache.seen(&tuple("Chrome")), "same day keeps entries");++ cache.roll(101);+ assert!(!cache.seen(&tuple("Chrome")), "new day clears entries");+ }+}diff --git a/rust/property-vals-rs/src/worker.rs b/rust/property-vals-rs/src/worker.rsindex 51f645a4b687..279815a262e8 100644--- a/rust/property-vals-rs/src/worker.rs+++ b/rust/property-vals-rs/src/worker.rs@@ -9,7 +9,7 @@ use crate::aggregator::Aggregator;use crate::config::Config;use crate::metrics_consts::*;use crate::producer::Producer;-use crate::seen_cache::SeenCache;+use crate::seen_cache::{utc_day, SeenCache};use crate::types::{IngestableEvent, PropertyType, TupleKey};#[derive(Clone, Copy, Default)]@@ -126,6 +126,10 @@ pub(crate) async fn flush<P: Producer>(return;}+ if let Some(cache) = seen_cache {+ cache.roll(utc_day());+ }+let mut snapshot: Vec<(TupleKey, u64)> = aggregator.drain().into_iter().collect();if max_values_per_key > 0 {snapshot = cap_top_k(snapshot, max_values_per_key, worker);