Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions posthog/models/cohort/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions posthog/models/cohort/test/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading