Skip to content

: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.

fix(data-warehouse): salt schedule buckets per interval to de-align cadence tiers (#75906)

Jovan Sakoviccc6b0cb6659e
diff --git a/products/data_modeling/backend/schedule.py b/products/data_modeling/backend/schedule.py
index 51dac2bd8320..653c2ed5afa3 100644
--- a/products/data_modeling/backend/schedule.py
+++ b/products/data_modeling/backend/schedule.py
@@ -155,7 +155,9 @@ def _short_interval_spec(entity_id: uuid.UUID, interval: timedelta, timezone: st
"""
interval_mins = int(interval.total_seconds() // 60)
num_windows = 60 // interval_mins
- base_min = _deterministic_int(entity_id, "minute") % interval_mins
+ # The interval must participate in the salt: with a shared salt, a coarser tier's fire
+ # minutes are always a subset of every finer tier's, so all tiers of a DAG fire together.
+ base_min = _deterministic_int(entity_id, f"minute-{interval_mins}") % interval_mins
mins = [(base_min + i * interval_mins) % 60 for i in range(num_windows)]
return ScheduleSpec(
calendars=[
@@ -181,7 +183,9 @@ def _medium_interval_spec(entity_id: uuid.UUID, interval: timedelta, timezone: s
"""
interval_hours = int(interval.total_seconds() // 3600)
num_windows = 24 // interval_hours
- base_hour = _deterministic_int(entity_id, "hour") % interval_hours
+ # Interval in the salt keeps tiers of one DAG de-aligned from each other (and the plain
+ # "hour" salt of the weekly/monthly specs).
+ base_hour = _deterministic_int(entity_id, f"hour-{interval_hours}") % interval_hours
hours = [(base_hour + i * interval_hours) % 24 for i in range(num_windows)]
return ScheduleSpec(
calendars=[
diff --git a/products/data_modeling/backend/test/test_schedule.py b/products/data_modeling/backend/test/test_schedule.py
index 8e058e97e3e8..1a37baf59516 100644
--- a/products/data_modeling/backend/test/test_schedule.py
+++ b/products/data_modeling/backend/test/test_schedule.py
@@ -6,6 +6,7 @@ import pytest
from posthog.test.base import BaseTest
from unittest import mock
+from parameterized import parameterized
from temporalio.client import ScheduleCalendarSpec, ScheduleListActionStartWorkflow
from products.data_modeling.backend.models import Node
@@ -221,6 +222,49 @@ class TestMonthlySpec:
assert len(days) == 28
+class TestTierPhaseAlignment:
+ """Cadence tiers of one DAG must not be phase-aligned.
+
+ All tiers of a DAG derive their bucket from the same entity_id, so unless the
+ interval participates in the salt, a coarser tier's fire times are a subset of
+ every finer tier's — every tier of a DAG piles onto the same minute/hour.
+ """
+
+ N = 500
+
+ @staticmethod
+ def _minute_set(spec) -> set[int]:
+ return {r.start for r in spec.calendars[0].minute}
+
+ @staticmethod
+ def _hour_set(spec) -> set[int]:
+ return {r.start for r in spec.calendars[0].hour}
+
+ @parameterized.expand(
+ [
+ ("15min_vs_30min", timedelta(minutes=15), timedelta(minutes=30), "_minute_set"),
+ ("15min_vs_1hr", timedelta(minutes=15), timedelta(hours=1), "_minute_set"),
+ ("30min_vs_1hr", timedelta(minutes=30), timedelta(hours=1), "_minute_set"),
+ ("6hr_vs_12hr", timedelta(hours=6), timedelta(hours=12), "_hour_set"),
+ ("6hr_vs_24hr", timedelta(hours=6), timedelta(hours=24), "_hour_set"),
+ ("12hr_vs_24hr", timedelta(hours=12), timedelta(hours=24), "_hour_set"),
+ ("24hr_vs_weekly", timedelta(hours=24), timedelta(days=7), "_hour_set"),
+ ]
+ )
+ def test_coarser_tier_not_contained_in_finer(self, _name, finer, coarser, extractor):
+ buckets = getattr(self, extractor)
+ aligned = 0
+ for i in range(self.N):
+ entity_id = uuid.UUID(int=i)
+ finer_set = buckets(build_schedule_spec(entity_id, finer))
+ coarser_set = buckets(build_schedule_spec(entity_id, coarser))
+ if coarser_set <= finer_set:
+ aligned += 1
+ # Incidental overlap is fine (expected ~1/interval-ratio of ids); systematic
+ # alignment is the defect.
+ assert aligned < self.N // 2, f"{aligned}/{self.N} ids have the coarser tier phase-aligned into the finer"
+
+
class TestBuildScheduleSpecEdgeCases:
def test_timezone_passed_to_all_tiers(self):
for interval in [timedelta(minutes=15), timedelta(hours=6), timedelta(days=7), timedelta(days=30)]: