Repository navigation
Conversation
…on (QuantumNous#8536) SaveQuotaDataCache held CacheQuotaDataLock while writing every cached row to quota_data, so RecordConsumeLog waited behind a slow database write before the HTTP response could finish. Take the pending snapshot under the cache lock and write it outside, serialized by a separate save lock. Entries logged during the write stay in the cache and land in the next flush.
|
No actionable comments were generated in the recent review. Walkthrough
ChangesQuota cache persistence
Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Sequence Diagram(s)sequenceDiagram
participant LogQuotaData
participant CacheQuotaDataLock
participant SaveQuotaDataCache
participant Database
SaveQuotaDataCache->>CacheQuotaDataLock: Swap shared cache for empty map
SaveQuotaDataCache->>Database: Persist detached snapshot
LogQuotaData->>CacheQuotaDataLock: Merge entry into new cache
LogQuotaData-->>SaveQuotaDataCache: Complete while database write continues
Suggested reviewers: Merge Risk:
|
Closes #8536
Request summary
Summary
SaveQuotaDataCacheheldCacheQuotaDataLockfor everyquota_dataquery and write.RecordConsumeLogcallsLogQuotaData, which needs the same lock, so a slowquota_datawrite delayed the end of the HTTP request after settlement.SaveQuotaDataCachenow holds the cache lock only to swap the pending map for an empty one. It writes the snapshot without that lock. A separatequotaDataSaveLockkeeps the periodic save and the shutdown save from writing at the same time.If a request calls
LogQuotaDataduring a save, the entry goes into the new map and the next save writes it. The save does not drop or double count that entry. The upsert logic and thequota_dataschema are unchanged.Evidence
The new test
TestLogQuotaDataDoesNotWaitForQuotaDataWriteinmodel/usedata_flow_test.goholds the only SQLite connection in an open transaction. This makesSaveQuotaDataCacheblock on its firstquota_dataquery. The test then callsLogQuotaDataand expects it to return. After the transaction ends, the test checks that the next save writescount = 2,quota = 150,token_used = 80.go test ./model -run TestLogQuotaDataDoesNotWaitForQuotaDataWrite -count=1 -vfails withsave never took its snapshot while the database was busy. The save heldCacheQuotaDataLockfor the full 1003 ms query.go test ./model -run TestLogQuotaData -race -count=1passes.go test ./model -count=1passes.go vet ./modelreports nothing.The change adds no SQL, schema, or migration, so it has no database dialect impact.