diff --git a/posthog/models/cohort/dependencies.py b/posthog/models/cohort/dependencies.py index b138a55897aeb..7725ef375100b 100644 --- a/posthog/models/cohort/dependencies.py +++ b/posthog/models/cohort/dependencies.py @@ -40,7 +40,7 @@ def extract_cohort_dependencies(cohort: Cohort) -> set[int]: return dependencies -def get_cohort_dependencies(cohort: Cohort) -> list[int]: +def get_cohort_dependencies(cohort: Cohort, _warming: bool = False) -> list[int]: """ Get the list of cohort IDs that the given cohort depends on. """ @@ -50,10 +50,11 @@ def get_cohort_dependencies(cohort: Cohort) -> list[int]: cache_hit = cache.has_key(cache_key) def compute_dependencies(): - COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss").inc() + if not _warming: + COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss").inc() return list(extract_cohort_dependencies(cohort)) - if cache_hit: + if cache_hit and not _warming: COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit").inc() result = cache.get_or_set( @@ -114,7 +115,7 @@ def warm_team_cohort_dependency_cache(team_id: int, batch_size: int = 1000): for cohort in Cohort.objects.filter(team_id=team_id, deleted=False).iterator(chunk_size=batch_size): # Any invalidated dependencies cache is rebuilt here dependents_map.setdefault(_cohort_dependents_key(cohort.id), []) - dependencies = get_cohort_dependencies(cohort) + dependencies = get_cohort_dependencies(cohort, _warming=True) # Dependency keys aren't fully invalidated; make sure they don't expire. cache.touch(_cohort_dependencies_key(cohort.id), timeout=DEPENDENCY_CACHE_TIMEOUT) # Build reverse map diff --git a/posthog/models/cohort/test/test_dependencies.py b/posthog/models/cohort/test/test_dependencies.py index 28571790500ec..3a12ad891ad2e 100644 --- a/posthog/models/cohort/test/test_dependencies.py +++ b/posthog/models/cohort/test/test_dependencies.py @@ -390,3 +390,23 @@ def test_cache_hit_miss_sequence(self): cache_type="dependents", result="hit" )._value._value self.assertEqual(dept_hits_after_second, dept_initial_hits + 1) + + def test_warming_does_not_increment_counters(self): + """Verify that cache warming operations don't increment the counter metrics""" + cohort_a = self._create_cohort(name="Test Cohort A") + self._create_cohort( + name="Test Cohort B", groups=[{"properties": [{"key": "id", "type": "cohort", "value": cohort_a.id}]}] + ) + + cache.clear() + + initial_hits = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit")._value._value + initial_misses = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss")._value._value + + warm_team_cohort_dependency_cache(self.team.id) + + # Verify counters did not increment during warming + final_hits = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="hit")._value._value + final_misses = COHORT_DEPENDENCY_CACHE_COUNTER.labels(cache_type="dependencies", result="miss")._value._value + self.assertEqual(final_hits, initial_hits) + self.assertEqual(final_misses, initial_misses)