Skip to content

CBG-5472: Make the stats logger independent of the database lock#8431

Open
bbrks wants to merge 1 commit into
mainfrom
CBG-5472
Open

CBG-5472: Make the stats logger independent of the database lock#8431
bbrks wants to merge 1 commit into
mainfrom
CBG-5472

Conversation

@bbrks

@bbrks bbrks commented Jul 6, 2026

Copy link
Copy Markdown
Member

CBG-5472

Avoid blocking stats logger from database updates and initialisations. Access lock-free version of databases on ServerContext to build stats and minimise the locking required inside DbStats.

Pre-review checklist

Dependencies (if applicable)

Integration Tests

@bbrks bbrks self-assigned this Jul 6, 2026
Base automatically changed from CBG-3658 to main July 7, 2026 13:41
Copilot AI review requested due to automatic review settings July 7, 2026 13:42

This comment was marked as outdated.

@bbrks
bbrks force-pushed the CBG-5472 branch 3 times, most recently from 1e27930 to f2a7a17 Compare July 16, 2026 20:55
The stats logger could stall, leaving gaps in sg_stats.log, whenever a
database config update held a lock the stats path also needed:
- _databasesLock: updateCalculatedStats took RLock, but a config update
  holds the write lock across an unbounded index-readiness wait.
- the per-db stats map mutex, contended by serialization and Prometheus
  (un)registration.

Decouple the stats reader from both:
- ServerContext keeps a lock-free atomic snapshot of the database
  contexts, refreshed under _databasesLock at each _databases mutation;
  updateCalculatedStats reads the snapshot instead of taking the lock.
- The concurrently-mutated stats maps (per-db and per-db replicator) are
  now a generic snapshotMap that serializes itself under its own lock
  (snapshot-then-marshal) and performs Prometheus (un)registration
  outside it, so serialization neither blocks nor is blocked by database
  registration/removal.

Serialized stats output is unchanged (per_db always present; empty
replications omitted via omitzero). The stats ticker keeps emitting while
databases are added, removed or reloaded.

The stats-definition-exporter traverses the stats tree by reflection;
teach it to descend into a snapshotMap via the new StatMapValues accessor
(the previous map field was handled by the reflect.Map branch).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants