fix(Segment Membership): Stagger the recurring count refresh fan-out#7943
Conversation
refresh_all_segment_counts enqueued every project's refresh at once, so the task processor ran a burst of concurrent `FROM IDENTITIES FINAL` count queries that could exhaust ClickHouse's memory and get an unrelated query killed by the overcommit tracker. Spread organisations across a configurable stagger window and cluster each org's projects a few seconds apart, streaming in organisation order so worker memory stays flat. Same-org counts stay consistently fresh; cross-org load is bounded. beep boop Claude-Session: https://claude.ai/code/session_01EgZ5iHpDASZzCapiHRxHLB
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
📝 WalkthroughWalkthroughThis PR adds a setting for staggering segment membership refresh scheduling and updates
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant refresh_all_segment_counts
participant enqueue_membership_refresh
refresh_all_segment_counts->>refresh_all_segment_counts: build ordered live-segment project queryset
refresh_all_segment_counts->>refresh_all_segment_counts: calculate spacing from stagger window
loop for each project
refresh_all_segment_counts->>enqueue_membership_refresh: enqueue with delay_until
end
Related issues: None specified in the provided context. Related PRs: None specified in the provided context. Suggested labels: backend, segment-membership, scheduling Suggested reviewers: None specified in the provided context. Poem A staggered queue begins to hum, Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7943 +/- ##
=======================================
Coverage 98.63% 98.63%
=======================================
Files 1497 1497
Lines 59151 59189 +38
=======================================
+ Hits 58345 58383 +38
Misses 806 806 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Exercises the early return in refresh_all_segment_counts when no organisation has a live segment, restoring 100% patch coverage. beep boop Claude-Session: https://claude.ai/code/session_01EgZ5iHpDASZzCapiHRxHLB
Docker builds report
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: e610eecc-eebc-4870-bc06-54099d2a22aa
📒 Files selected for processing (4)
api/app/settings/common.pyapi/segment_membership/tasks.pyapi/tests/unit/segment_membership/test_unit_segment_membership_tasks.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-16)Details
Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
|
Visual Regression19 screenshots compared. See report for details. |
…s slot With a fixed project stagger and an org_spacing that shrinks as organisations grow, an organisation with enough projects pushed its later refreshes past the next organisation's slot, colliding at scale. Accumulate a monotonic delay so each organisation starts at its slot or after the previous org's last project, whichever is later. beep boop Claude-Session: https://claude.ai/code/session_01EgZ5iHpDASZzCapiHRxHLB
emyller
left a comment
There was a problem hiding this comment.
The idea is sound. I'd like to discuss the approach a bit, and consider distributing projects evenly rather than grouping by organisations first — my visual imagination of this patch currently leads to a weird fragmenting of turns.
emyller
left a comment
There was a problem hiding this comment.
The two code suggestions remain, but neither is a blocker now.
The design LGTM now but we may need to revisit it down the road.
…s the window Per review, drop the per-organisation grouping (and its cumulative-delay overflow handling and STAGGER_SECONDS setting) in favour of spreading all live-segment projects evenly across the stagger window, ordered by organisation so an org's projects still refresh contiguously. Simpler, and no org-slot overflow to guard. beep boop Claude-Session: https://claude.ai/code/session_01EgZ5iHpDASZzCapiHRxHLB
4c06af4 to
d3d2db9
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 9b705641-f921-406f-8e91-d75dfbe030b0
📒 Files selected for processing (4)
api/app/settings/common.pyapi/segment_membership/tasks.pyapi/tests/unit/segment_membership/test_unit_segment_membership_tasks.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
💤 Files with no reviewable changes (1)
- api/app/settings/common.py
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 6d8973b4-0122-4be2-b5ab-02e584f5a285
📒 Files selected for processing (4)
api/app/settings/common.pyapi/segment_membership/tasks.pyapi/tests/unit/segment_membership/test_unit_segment_membership_tasks.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
💤 Files with no reviewable changes (1)
- api/app/settings/common.py
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature.Changes
Contributes to #7942
In this PR, we distribute per-organisation segment count refreshes to avoid a thundering herd problem when ClickHouse has to compute segment counts for all projects at roughly the same time.
We introduce a new setting:
SEGMENT_MEMBERSHIP_REFRESH_PROJECT_STAGGER_WINDOW_HOURS(default 1) — the window organisations are spread across. Validated at startup to not exceedSEGMENT_MEMBERSHIP_REFRESH_INTERVAL_HOURSso consecutive refresh cycles won't overlap.How did you test this code?
Added unit tests to assert the fan-out is staggered.