: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(warehouse-sources): count why coarsening declines a table (#82777)
Daniel RC95f4b043e77a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.pyindex 1b2e1cdb10d9..9ef01bc69f98 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition_controller.py@@ -33,6 +33,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.repselect_repartition_target,)from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.load.metrics import (+ DELTA_COARSEN_DECLINE_TOTAL,DELTA_REPARTITION_SKIP_TOTAL,)@@ -223,12 +224,22 @@ async def maybe_flag_for_coarsening(the only route by which a nominated table there is evaluated). Never raises (the caller swallows)."""measured_partitions = len(partition_bytes)++ def _decline(reason: str) -> None:+ # Why an over-fragmented table did NOT coarsen. Without this a stalled rollout is invisible:+ # every gate below returns silently, so "nothing happened" and "nothing was eligible" look+ # identical from outside. A counter rather than an event because this runs per sync per table.+ DELTA_COARSEN_DECLINE_TOTAL.labels(reason=reason).inc()+# Never fight a rewrite that is already staged or mid-swap, however the evaluation was prompted.if schema.repartition_pending is not None or schema.repartition_swap is not None:returnrequested = schema.coarsen_requestedif requested is None:+ # Below these two the table is not over-fragmented, so it is not a coarsening candidate at all+ # and its decline carries no information — returning before `_decline` keeps the metric to the+ # population the rollout is about rather than every table on every sync.if measured_partitions < COARSEN_MIN_PARTITIONS:returnif max_bytes * COARSEN_TRIGGER_DIVISOR > budget:@@ -237,10 +248,10 @@ async def maybe_flag_for_coarsening(# count is rule-filtered, so a table whose deaths were all explained away passes it — the# authoritative raw-signal gate is `has_recent_occurrences` further down.if recent_oom_count > 0:- return+ return _decline("oom_history_recent")layout_age = _seconds_since_last_repartition(schema)if layout_age is not None and layout_age < COARSEN_MIN_LAYOUT_AGE_SECONDS:- return+ return _decline("layout_too_young")target, reason = await asyncio.to_thread(select_coarsen_target, schema, partition_bytes, budget // COARSEN_TARGET_DIVISOR@@ -266,7 +277,7 @@ async def maybe_flag_for_coarsening(max_partition_bytes=max_bytes,partition_count=measured_partitions,)- return+ return _decline(reason)if requested is None:# Raw occurrences, not the rule-filtered `recent_count`: the rules exist to withhold splits,@@ -275,7 +286,7 @@ async def maybe_flag_for_coarsening(if await asyncio.to_thread(ExternalDataSchemaOOMEvent.has_recent_occurrences, schema, days=COARSEN_OOM_FREE_DAYS):- return+ return _decline("oom_within_free_window")if not await asyncio.to_thread(is_auto_coarsen_enabled, schema):await logger.adebug(@@ -285,7 +296,7 @@ async def maybe_flag_for_coarsening(max_partition_bytes=max_bytes,partition_count=measured_partitions,)- return+ return _decline("flag_disabled")# Distinct reason for a nominated rewrite, the same way an admin-staged one is distinguishable, so# the backlog pass can be tracked separately from what the controller does on its own.diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.pyindex 643051809bad..8ebe92e53346 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition_controller.py@@ -454,6 +454,48 @@ class TestCoarsenTrigger:schema.refresh_from_db()assert schema.repartition_pending is None+ @pytest.mark.parametrize(+ "case,expected_reason",+ [+ ("rule_excluded_oom", "oom_within_free_window"),+ ("fresh_layout", "layout_too_young"),+ ("flag_disabled", "flag_disabled"),+ ],+ )+ def test_declining_to_coarsen_records_which_gate_stopped_it(self, team, case, expected_reason):+ # A stalled rollout is otherwise invisible: every gate returns silently, so "the flag reached+ # nobody" and "no table was eligible" look identical from outside. The reason label is what+ # tells those apart without shell access to a customer's tables.+ schema = self._fragmented_schema(+ team,+ **(+ {"last_repartition_at": datetime.datetime.now(datetime.UTC).isoformat()}+ if case == "fresh_layout"+ else {}+ ),+ )+ if case == "rule_excluded_oom":+ ExternalDataSchemaOOMEvent.objects.for_team(schema.team_id).create(+ team_id=schema.team_id,+ schema=schema,+ run_id="run-1",+ self_phase="extract",+ self_report_age_at_death_seconds=1.0,+ )++ with tempfile.TemporaryDirectory() as d:+ delta = _write_partitioned_delta(f"{d}/t", [str(bucket) for bucket in range(16)])+ with (+ patch.object(ctrl, "target_partition_bytes", return_value=10**12),+ patch.object(ctrl, "is_auto_repartition_enabled", return_value=True),+ patch.object(ctrl, "is_auto_coarsen_enabled", return_value=case != "flag_disabled"),+ patch.object(ctrl, "capture_repartition_event"),+ patch.object(ctrl, "DELTA_COARSEN_DECLINE_TOTAL") as decline_metric,+ ):+ self._detect(team, schema, delta)++ assert decline_metric.labels.call_args.kwargs == {"reason": expected_reason}+def test_operator_nomination_overrides_the_policy_gates(self, team):# The backlog of already-over-split tables is blocked by the OOM-free gate, because the signal# that over-split them keeps firing. A nomination is how an operator gets past that, so it hasdiff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/metrics.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/metrics.pyindex 6f0367fcb3c8..363981780069 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/metrics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/load/metrics.py@@ -47,6 +47,16 @@ DELTA_REPARTITION_SKIP_TOTAL = Counter(labelnames=["team_id", "reason"],)+# Deliberately unlabelled by team: this fires on every sync of every over-fragmented table fleet-wide,+# which is the whole population the coarsening rollout is aimed at, so a team label would explode+# cardinality. `reason` is a bounded set of gate names, which is what "why isn't coarsening firing"+# actually needs; per-table detail stays in the operator-facing INFO log on a nominated table.+DELTA_COARSEN_DECLINE_TOTAL = Counter(+ "warehouse_load_delta_coarsen_decline_total",+ "Over-fragmented tables the controller declined to coarsen, by gate",+ labelnames=["reason"],+)+# deltalite real-write path (phase 2). `outcome` is one of:# written - deltalite performed the incremental merge and committed# fallback - deltalite was enabled but failed (or refused), so the delta-rs MERGE ran instead