Skip to content

fix(telemetry): let every replica write the minute's rollup row - #914

Merged
hyperb1iss merged 1 commit into
mainfrom
nova/telemetry-rollup-upsert
Nov 24, 2026
Merged

hyperb1iss merged 1 commit into
mainfrom
nova/telemetry-rollup-upsert

Conversation

@hyperb1iss

@hyperb1iss hyperb1iss commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

💡 What changes

Every API process writes the runtime telemetry rollup for the current minute into one shared row. The second process to write a given minute was refused, so a deployment with two API replicas logged this once a minute:

runtime_telemetry_rollup_failed error=Couldn't coerce value for field `created_at`
of `telemetry_rollups:...`: Expected `datetime` but found `NONE`

It was found in the logs of a two-replica deployment on 1.5.0, where the two API pods logged 54 of these in an hour between them, one per minute. A single daemon hits the same failure when it restarts inside a minute it has already written.

After this change the later writer updates the row, and the warning stops.

🛠️ How it works

The write was UPSERT telemetry_rollups CONTENT $record WHERE bucket_key = $bucket_key. CONTENT replaces the whole row, the record carries no created_at, and the field is TYPE datetime DEFAULT time::now(), which fills in only on create. On an existing row the replacement left created_at empty and the schema refused the write.

The upsert now uses SET for the ten fields the rollup owns, so created_at keeps the value it got when the row was created. Each listed field is still replaced whole (summaries included), so nothing from an earlier writer's snapshot leaks into the row the way MERGE would allow.

test_surreal_live_nested_fields.py carried its own copy of the statement. It now imports the service's, so the live suite exercises what the product runs.

🧪 Validation

  • Embedded engine: test_rollup_for_a_minute_that_already_has_a_row_updates_it writes the same minute twice as two fresh processes and checks one row, the second writer's data, and the original created_at. With the old CONTENT statement put back, it fails on "the second write was refused".
  • SurrealDB 4.0.1 server (RocksDB): the new test_live_runtime_rollup_accepts_every_replica_in_the_same_minute does the same over two connections, then has both write a fresh minute at once. tests/test_surreal_live_nested_fields.py and tests/test_telemetry_routes.py together: 17 passed.
  • Concurrent first writes: a separate probe ran 400 rounds of two connections creating the same new minute at the same instant. All 800 writes succeeded and every round left exactly one row, so the WHERE bucket_key form stays and no record-id scheme is added.

🚦 Rollout

No schema change and nothing to migrate. During a rolling deploy an old pod can still be refused for a minute it writes second; that ends when the last old pod is gone. A revert brings the warning back and loses nothing.

📌 Not in this PR

With several replicas, the row for a minute holds the snapshot of whichever replica wrote last, since each process only sees its own counters. That was already true of the first writer before this fix. Combining replicas is a separate piece of work.

Summary by CodeRabbit

  • Bug Fixes
    • Telemetry summaries for the same time period now update the existing entry instead of creating duplicates, while keeping its original creation time.
    • Simultaneous updates to separate time periods are retained, improving the accuracy and consistency of stored telemetry.

Each API process writes the telemetry rollup for the current minute.
The write was UPSERT ... CONTENT, which replaces the whole row, and
created_at only defaults on create. The second process to write a
minute (another replica, or a daemon restarted inside the minute) was
refused with "Couldn't coerce value for field created_at: Expected
datetime but found NONE". With two API replicas that is one warning a
minute, and the stored row belongs to whichever replica wrote first.

The upsert now sets the fields it owns and leaves created_at alone, so
a later writer updates the row. The live test file used its own copy
of the statement; it now runs the product's.

Co-Authored-By: Nova (Claude Fable 5.2) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The telemetry rollup UPSERT now assigns explicit fields instead of replacing the whole record. Tests check repeated writes, preservation of created_at, and concurrent writes to separate buckets.

Changes: Telemetry Rollup Persistence

Layer / File(s) Summary
Define and use the rollup UPSERT
apps/api/src/sibyl/services/telemetry.py
The shared UPSERT assigns the listed rollup fields. persist_runtime_rollup uses this query instead of an inline CONTENT UPSERT.
Verify rollup persistence
apps/api/tests/test_surreal_live_nested_fields.py, apps/api/tests/test_telemetry_routes.py
Tests check that repeated writes update the existing row and preserve created_at. The live test also checks concurrent writes to separate buckets. The migration-59 test record includes window_seconds and updated_at.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix

Merge Risk: Minimal · up to af5a3

Telemetry rollup writes for a minute that already has a row now update that row and keep its original creation time. No merge-blocking risk was found. During a rolling deploy, an old pod that writes second may still be refused until it is replaced.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the fix and its scope: the telemetry rollup write now lets every replica write the minute's row. The change replaces CONTENT with SET assignments in the rollup upsert, so a later write…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the fix and its scope: the telemetry rollup write now lets every replica write the minute's row. The change replaces CONTENT with SET assignments in the rollup upsert, so a later writer updates the shared row and keeps its original created_at. The title is specific and matches the main change. It is 63 characters, within a normal title length, and follows a conventional commit style.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 515d6148-f4af-43d8-bbc3-1a10863110ac
Commits

Reviewing files that changed from the base of the PR and between 405c21a and af5a318.

Files selected for processing: 3

  • apps/api/src/sibyl/services/telemetry.py
  • apps/api/tests/test_surreal_live_nested_fields.py
  • apps/api/tests/test_telemetry_routes.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@hyperb1iss
hyperb1iss merged commit eed8cc9 into main Nov 24, 2026
25 checks passed
@hyperb1iss
hyperb1iss deleted the nova/telemetry-rollup-upsert branch November 24, 2026 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant