Make the capture-path vault's upsert monotonic in observed_at - #703
Merged
Conversation
observed_at is the EPICS PV's own timestamp, not CORA's clock, so a monitor reconnect or an IOC clock step can replay an older reading after a newer one. The old unconditional overwrite let that stale write clobber a newer observation, and since a Run can now hold one row per storage tier with get_latest ordering on observed_at DESC, a single backwards write could make get_latest resolve to the wrong location entirely. Guard the row-level UPDATE with a WHERE on the whole SET so a row only ever moves forward as a unit (never a newer timestamp paired with an older path), and mirror the same rule in the in-memory adapter so a declined write is invisible in both.
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
observed_aton arun_capture_pathrow is the EPICS PV's own timestamp, not CORA's clock. It reaches the vault straight fromRunWitnessRecorder._resolve_capture_path, so a monitor reconnect replaying a stale value, or an IOC whose clock stepped, can deliver an older reading after a newer one.The upsert overwrote unconditionally, so it believed the stale one.
Why it matters now
It used to be close to harmless. Since #699 a Run can hold one row per storage tier, and
_LATEST_SQLorders onobserved_at DESC, so a single backwards write no longer corrupts just one row's path. It can makeload_run_capture_pathresolve to the wrong tier entirely:The fix
A
WHEREon the wholeDO UPDATE, not aGREATESTin theSET. The row moves forward as a unit or not at all, so it can never end up carrying a newer timestamp paired with an older path. The in-memory adapter mirrors the same>=rule, so a declined write is invisible in both.>=rather than>so a genuine retry of the same observation, carrying a corrected path with no new reading in between, still lands.A declined write affects zero rows in Postgres. That is correct and the caller ignores the count, which the Protocol docstring now says explicitly so nobody later reads it as a failure.
No migration: this is a statement change.
Tests
Four scenarios, in both the Postgres and in-memory adapters:
observed_atleaves the row untouched, asserted throughupdated_at == created_atso it proves nothing was written rather than just that the path survivedobserved_atisTIMESTAMPTZ NOT NULL, so the guard cannot silently never-fire on a null comparison.Written by Claude Sonnet 5, reviewed before submission.
🤖 Generated with Claude Code