: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
fix(propdefs): keep consumer heartbeat alive through batch writes (#75745)
Eli Reismana376cd4ee77c
diff --git a/rust/property-defs-rs/src/batch_ingestion.rs b/rust/property-defs-rs/src/batch_ingestion.rsindex 6bb70ec1a72e..dd503e27fa62 100644--- a/rust/property-defs-rs/src/batch_ingestion.rs+++ b/rust/property-defs-rs/src/batch_ingestion.rs@@ -1,6 +1,7 @@use std::{sync::Arc, time::Duration};use chrono::{DateTime, Utc};+use futures::stream::{FuturesUnordered, StreamExt};use sqlx::PgPool;use tokio::task::JoinHandle;use tracing::{error, info, warn};@@ -272,7 +273,13 @@ impl PropertyDefinitionsBatch {}// HACK: making this public so the test suite file can live under "../tests/" dir-pub async fn process_batch(config: &Config, cache: Arc<Cache>, pool: &PgPool, batch: Vec<Update>) {+pub async fn process_batch(+ config: &Config,+ cache: Arc<Cache>,+ pool: &PgPool,+ batch: Vec<Update>,+ handle: &lifecycle::Handle,+) {// prep reshaped, isolated data batch bufffers and async join handleslet mut event_defs = EventDefinitionsBatch::new(config.write_batch_size);let mut event_props = EventPropertiesBatch::new(config.write_batch_size);@@ -345,9 +352,13 @@ pub async fn process_batch(config: &Config, cache: Arc<Cache>, pool: &PgPool, ba}));}- // Execute final batch handles concurrently- let final_results = futures::future::join_all(handles).await;- for result in final_results {+ // Drain the chunk writes as they complete, beating the consumer heartbeat on each+ // completion. Heartbeats track real progress: a long batch of slow-but-advancing+ // writes no longer trips the lifecycle stall detector, while a wedged consumer+ // (no chunk completing within the liveness deadline) still does.+ let mut in_flight: FuturesUnordered<_> = handles.into_iter().collect();+ while let Some(result) = in_flight.next().await {+ handle.report_healthy();match result {Ok(batch_result) => match batch_result {Ok(_) => continue,diff --git a/rust/property-defs-rs/src/lib.rs b/rust/property-defs-rs/src/lib.rsindex 4ea45228b1b7..819f15493e06 100644--- a/rust/property-defs-rs/src/lib.rs+++ b/rust/property-defs-rs/src/lib.rs@@ -136,8 +136,9 @@ pub async fn update_consumer_loop(e)});+ handle.report_healthy();- process_batch(&config, cache.clone(), &context.pool, batch).await;+ process_batch(&config, cache.clone(), &context.pool, batch, &handle).await;}}diff --git a/rust/property-defs-rs/tests/batch_ingestion.rs b/rust/property-defs-rs/tests/batch_ingestion.rsindex a521def5fd5a..bd5e52d1cb32 100644--- a/rust/property-defs-rs/tests/batch_ingestion.rs+++ b/rust/property-defs-rs/tests/batch_ingestion.rs@@ -16,6 +16,13 @@ use property_defs_rs::{update_cache::Cache,};+// process_batch beats a lifecycle heartbeat as chunk writes complete. Tests don't run+// the lifecycle monitor, so report_healthy() is just an atomic store on this handle.+fn test_lifecycle_handle() -> lifecycle::Handle {+ let mut manager = lifecycle::Manager::builder("test").build();+ manager.register("consumer", lifecycle::ComponentOptions::new())+}+#[sqlx::test(migrations = "./tests/test_migrations")]async fn test_simple_batch_write(db: PgPool) {let config = Config::init_with_defaults().unwrap();@@ -28,7 +35,7 @@ async fn test_simple_batch_write(db: PgPool) {// should decompose into 1 event def, 100 event props, 100 prop defs (of event type)assert_eq!(updates.len(), 201);- process_batch(&config, cache, &db, updates).await;+ process_batch(&config, cache, &db, updates, &test_lifecycle_handle()).await;// fetch results and ensure they landed correctlylet event_def_name: String = sqlx::query_scalar!(r#"SELECT name from posthog_eventdefinition"#)@@ -96,7 +103,7 @@ async fn test_group_batch_write(db: PgPool) {}}});- process_batch(&config, cache, &db, updates).await;+ process_batch(&config, cache, &db, updates, &test_lifecycle_handle()).await;// fetch results and ensure they landed correctlylet event_def_name: String = sqlx::query_scalar!(r#"SELECT name from posthog_eventdefinition"#)@@ -133,7 +140,7 @@ async fn test_person_batch_write(db: PgPool) {// should decompose into 1 event def, 0 event props, 100 prop defs (50 $set, 50 $set_once props)assert_eq!(updates.len(), 101);- process_batch(&config, cache, &db, updates).await;+ process_batch(&config, cache, &db, updates, &test_lifecycle_handle()).await;// fetch results and ensure they landed correctlylet event_def_name: String = sqlx::query_scalar!(r#"SELECT name from posthog_eventdefinition"#)@@ -273,7 +280,14 @@ async fn test_property_definitions_conflict_update(db: PgPool) {};let initial_updates = vec![Update::Property(initial_prop)];- process_batch(&config, cache.clone(), &db, initial_updates).await;+ process_batch(+ &config,+ cache.clone(),+ &db,+ initial_updates,+ &test_lifecycle_handle(),+ )+ .await;// Verify initial statelet initial_row = sqlx::query!(@@ -301,7 +315,14 @@ async fn test_property_definitions_conflict_update(db: PgPool) {};let updated_updates = vec![Update::Property(updated_prop)];- process_batch(&config, cache, &db, updated_updates).await;+ process_batch(+ &config,+ cache,+ &db,+ updated_updates,+ &test_lifecycle_handle(),+ )+ .await;// Verify both fields were updatedlet updated_row = sqlx::query!(@@ -381,7 +402,7 @@ async fn test_property_definitions_dedupe_within_batch(db: PgPool) {),prop("current_page", None, false, PropertyParentType::Event),];- process_batch(&config, cache, &db, batch).await;+ process_batch(&config, cache, &db, batch, &test_lifecycle_handle()).await;let count: Option<i64> = sqlx::query_scalar!(r#"SELECT COUNT(*) FROM posthog_propertydefinition WHERE name IN ('brand', 'current_page')"#@@ -429,7 +450,7 @@ async fn test_property_definitions_typed_row_wins_regardless_of_order(db: PgPoolPropertyParentType::Event,),];- process_batch(&config, cache, &db, batch).await;+ process_batch(&config, cache, &db, batch, &test_lifecycle_handle()).await;for name in ["rev_a", "rev_b"] {let row = sqlx::query!(@@ -466,7 +487,7 @@ async fn test_property_definitions_keeps_rows_that_differ_on_conflict_key(db: Pgprop("shared", None, false, PropertyParentType::Event),prop("shared", None, false, PropertyParentType::Person),];- process_batch(&config, cache, &db, batch).await;+ process_batch(&config, cache, &db, batch, &test_lifecycle_handle()).await;let count: Option<i64> = sqlx::query_scalar!(r#"SELECT COUNT(*) FROM posthog_propertydefinition WHERE name = 'shared'"#@@ -493,7 +514,7 @@ async fn test_event_definitions_dedupe_within_batch(db: PgPool) {evt("$pageview", now),evt("$autocapture", now),];- process_batch(&config, cache, &db, batch).await;+ process_batch(&config, cache, &db, batch, &test_lifecycle_handle()).await;let count: Option<i64> = sqlx::query_scalar!(r#"SELECT COUNT(*) FROM posthog_eventdefinition WHERE name IN ('$pageview', '$autocapture')"#diff --git a/rust/property-defs-rs/tests/group_type_resolver.rs b/rust/property-defs-rs/tests/group_type_resolver.rsindex 5ef2a8799280..3c7f1a50e7f4 100644--- a/rust/property-defs-rs/tests/group_type_resolver.rs+++ b/rust/property-defs-rs/tests/group_type_resolver.rs@@ -17,6 +17,13 @@ use property_defs_rs::{update_cache::Cache,};+// process_batch beats a lifecycle heartbeat as chunk writes complete. Tests don't run+// the lifecycle monitor, so report_healthy() is just an atomic store on this handle.+fn test_lifecycle_handle() -> lifecycle::Handle {+ let mut manager = lifecycle::Manager::builder("test").build();+ manager.register("consumer", lifecycle::ComponentOptions::new())+}+// -- mock server --------------------------------------------------------struct MockPersonHogService {@@ -893,7 +900,14 @@ async fn test_end_to_end_poisoned_group_def_recovers_and_persists(db: PgPool) {Some(GroupType::Unresolved("Organization".to_string())));- process_batch(&config, cache.clone(), &db, round1).await;+ process_batch(+ &config,+ cache.clone(),+ &db,+ round1,+ &test_lifecycle_handle(),+ )+ .await;assert_eq!(group_prop_def_count(&db).await,@@ -916,7 +930,14 @@ async fn test_end_to_end_poisoned_group_def_recovers_and_persists(db: PgPool) {// The negative entry has expired (TTL 0), so personhog is re-driven and now resolves to 5.assert_eq!(get_resolved_index(&round2[0]), Some(5));- process_batch(&config, cache.clone(), &db, round2).await;+ process_batch(+ &config,+ cache.clone(),+ &db,+ round2,+ &test_lifecycle_handle(),+ )+ .await;assert_eq!(group_prop_def_count(&db).await,