Repository navigation
fix(flags): measure backfill consumer lag from the Kafka message time - #111071
Conversation
|
😎 Merged successfully - details. |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The query preserves existing filtering behavior while the focused regression test verifies the intended backlog safety check.
Review effort: Balanced
Findings: None
What changed in this PR
Updates flag-evaluation backfill safeguards to measure Kafka consumer lag using message time rather than ClickHouse write time.
Changes:
- Calculates per-partition lag from
_timestamp. - Extends test fixtures and coverage for delayed backlog consumption.
| File | Description |
|---|---|
posthog/dags/flag_evaluations_backfill.py |
Uses Kafka message timestamps for consumer lag. |
posthog/dags/tests/test_flag_evaluations_backfill.py |
Tests rows written later than their Kafka timestamp. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
[Medium risk] Changes how consumer lag is measured in a data pipeline. The PR appears safe to merge; no actionable issue was identified. Reviews (1) · Last reviewed commit: "fix(flags): measure backfill consumer la..." |
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 🧰 Additional context used📚 Code guidelines (5)📝 WalkthroughWalkthroughThe partition lag query now compares the latest Kafka message timestamp with the current time, instead of using row insertion time. Tests now create Kafka rows with separate message and insertion timestamps and include a safety-check case with a two-hour-old message and a two-hour consumer delay. Priority: ➖ Normal Merge Risk: 🔵 Low · up to The lag check has a possible ordering limitation, but a duplicate-copy failure has not been established. The change is mergeable with owner awareness of that limitation. Architecture SummaryArchitecture risk: 🔵 Low · up to The change affects 1 system. Changed systems: Architecture concerns Review detailsSystems and components
Before / after behavior
🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
🤖 CI report
|
|
Risk: No findings The only change since the last review is one explanatory comment line above Sentinel reviewed |
A new stamphog review started for this PR — the fresh verdict replaces this approval.
There was a problem hiding this comment.
Approved.
Small, contained fix to a backfill safety check in an op the author knows well (strong familiarity, owning team). The lag value is unchanged today and the new test covers the case. _timestamp is already a column the backfill copies.
- Author wrote 100% of the modified lines and has 4 merged PRs in these paths (familiarity STRONG).
- 👍 on the PR from greptile-apps[bot].
Gate mechanics and policy version
| Gate | Result | |
|---|---|---|
| prerequisites | ✓ | all clear |
| deny-list | ✓ | no deny categories matched |
| size | ✓ | 5L, 1F substantive, 48L/2F incl. docs/generated/snapshots — within ceiling |
| tier | ✓ | T1-agent / T1b-small (48L, 2F, single-area, fix) |
| stamphog 2.3.1 | .stamphog/policy.yml @ c6a1daa · reviewed head c6a1daa |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
🟡 Minor · Use Kafka positions for the lag check. · flag_evaluations_backfill.py:69
posthog/dags/flag_evaluations_backfill.py:69
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick winUse Kafka positions for the lag check.
queueMessagessends the batch in parallel, and each producer call sets the Kafka timestamp withDate.now(). Kafka can therefore append a lower-timestamp message after a higher-timestamp message in the same partition.max(_timestamp)can then hide an older offset, allowing the backfill to insert a duplicate that the plainMergeTreeretains.Use partition offset positions, or another position-based watermark, instead of relying on timestamp ordering.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: PostHog/posthog/.coderabbit.yaml
Review profile: QUIET
Plan: Enterprise
Run ID: d8b91905-356d-4d08-b69a-63f1becc80bf
📒 Files selected for processing (1)
posthog/dags/flag_evaluations_backfill.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 9 remain after this review.
HostHog preview —
|

Problem
inserted_at.inserted_atwith the time ClickHouse consumes the message, as the events MV does. The current Kafka-timeinserted_atlets the last-called sync skip flag calls when the ClickHouse consumer stalls.written_atcolumn approach in feat(flags): add written_at column to flag_evaluations #110412.Changes
_timestamp, the Kafka message time.inserted_atequals its_timestamptoday, so the check's result does not change until the MV change lands.inserted_atfilters stay. The 7-day lookback prunes on theinserted_atskip index, andinserted_at != timestampstill excludes copied rows.Next, each its own PR:
flag_evaluations_mvto selectnow64() AS inserted_at.flag_evaluationsbuffer and uses the replication buffer (feat(flags): read last_called_at sync calls from flag_evaluations behind a setting #110150 or a follow-up).How did you test this code?
test_backfill_fails_without_copying_when_a_safety_check_fails: a consumer writing a two-hour-old backlog now. With the oldmax(inserted_at)query the backfill copies rows and the case fails. With this change the backfill stops._timestamp, and a case can write a row later than its Kafka time.Test rationale: The new case catches the lag check moving back to a write-time column. No existing case separates a row's write time from its Kafka time, so the existing cases pass with either query.
Release status
Automatic notifications
Docs update
None.
🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Agent: Claude Code, Opus 5.5
/gopipeline from a plan the author approved.inserted_atafter reviewing thewritten_atcolumn design in feat(flags): add written_at column to flag_evaluations #110412. That PR closes in favor of this sequence.inserted_at, and the backfill job is the only tagged reader offlag_evaluations.inserted_at./writing-tests,/writing-code-comments,/simplify,/comment-cleanup,/reviewing-with-coderabbit,/writing-pr-descriptions,/metabase-prod-query.--deepagainstmaster: 0 findings.https://claude.ai/code/session_01PjQA3YgRBZqNdm7qCpBVck