Conversation
The stats logger could stall, leaving gaps in sg_stats.log, whenever a database config update held a lock the stats path also needed. Two independent locks were involved: - _databasesLock: updateCalculatedStats took RLock, but a config update holds the write lock across an infinite index-readiness wait, blocking the reader for the whole (unbounded) window. - base.SgwStats.dbStatsMapMutex: String() held it for the entire marshal, and NewDBStats/ClearDBStats held it for all Prometheus (un)registration under _databasesLock. Decouple the reader from both: - ServerContext now keeps a lock-free atomic snapshot of the database contexts, refreshed under _databasesLock at each _databases mutation. updateCalculatedStats reads the snapshot, never the lock. - dbStatsMapMutex is now held only for the map operation: String() marshals a shallow copy, and NewDBStats/ClearDBStats do their Prometheus work outside the lock. The unregister* calls only touch Prometheus (not the DbStats maps), so this stays race-free. The stats ticker now keeps emitting (stale-but-present) while databases are added, removed or reloaded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
CBG-5472
The stats logger could stall, leaving gaps in sg_stats.log, whenever a database config update held a lock the stats path also needed. Two independent locks were involved:
_databasesLock:updateCalculatedStatstookRLock, but a config update holds the write lock across an infinite index-readiness wait, blocking the reader for the whole (unbounded) window.base.SgwStats.dbStatsMapMutex:String()held it for the entire marshal, andNewDBStats/ClearDBStatsheld it for all Prometheus (un)registration under_databasesLock.Decouple the reader from both:
ServerContextnow keeps a lock-free atomic snapshot of the database contexts, refreshed under_databasesLockat each_databasesmutation.updateCalculatedStatsreads the snapshot, never the lock.dbStatsMapMutexis now held only for the map operation:String()marshals a shallow copy, andNewDBStats/ClearDBStatsdo their Prometheus work outside the lock. The unregister* calls only touch Prometheus (not theDbStatsmaps), so this stays race-free.The stats ticker now keeps emitting (stale-but-present) while databases are added, removed or reloaded.
Pre-review checklist
fmt.Print,log.Print, ...)base.UD(docID),base.MD(dbName))docs/apiDependencies (if applicable)
Integration Tests