feat(memtable): 在 Get 读路径上新增命中计数#43
Conversation
为观测缓存命中率,在 MemTable 上加 hits 字段,并在 Get 命中时 +1, 新增 Stats() 返回命中数。同时把 BanGD 评审 Action 更新到最新版本。
📝 WalkthroughWalkthroughThis PR updates the pinned commit for the BanGD AI PR review tool in the GitHub Actions workflow, and adds a hit counter to MemTable's Get method to track successful key lookups with a Stats method to expose the counter. ChangesCI Review Tool Update
MemTable Hit Counter Instrumentation
🎯 2 (Simple) | ⏱️ ~8 minutes Possibly Related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🐯 BanGD 数据库内核评审整体风险:🔴 高 变更总结:## PR 变更总结 本 PR 在两个层面做了改动: 1. 基础设施层(CI/CD):将 BanGD 评审 Action 从旧版本升级到新版本,包含读取完整文件内容和重试健壮性增强。该部分无功能性影响,只是工具链自身的可靠性改进。 2. 存储引擎层(memtable):在 核心架构问题: 1. 🛑 [阻塞 · 并发]
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@storage/zstorage/memtable.go`:
- Line 46: The field `hits` in the memtable struct is incremented and read
without synchronization causing races; update `Get` to use
atomic.AddUint64(&m.hits, 1) instead of `m.hits++`, and change reads in
`Stats()` (and any other direct reads at the noted locations) to use
atomic.LoadUint64(&m.hits); ensure you import sync/atomic and operate on the
memtable pointer receiver (e.g., methods `Get`, `Stats`) so atomic functions
receive the address of the `hits` field.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c106052f-0732-4c89-9654-2ff64fac88af
📒 Files selected for processing (2)
.github/workflows/bangd-review.ymlstorage/zstorage/memtable.go
| sst *SSTable | ||
|
|
||
| // hits 统计 Get 命中次数,用于观测缓存命中率 | ||
| hits uint64 |
There was a problem hiding this comment.
Use atomic operations for hits to avoid data races.
m.hits++ in Get and direct reads in Stats() are unsynchronized; concurrent Get/Stats calls race and can return incorrect counts.
🔧 Proposed fix
import (
"bytes"
"errors"
"fmt"
"log/slog"
"math/rand"
"sync"
+ "sync/atomic"
@@
- // hits 统计 Get 命中次数,用于观测缓存命中率
- hits uint64
+ // hits 统计 Get 命中次数,用于观测缓存命中率
+ hits atomic.Uint64
@@
if val, found := active.search(key); found {
- m.hits++ // 命中计数
+ m.hits.Add(1) // 命中计数
return val, nil
}
@@
if dirty != nil && dirty.head != nil {
if val, found := dirty.search(key); found {
- m.hits++ // 命中计数
+ m.hits.Add(1) // 命中计数
return val, nil
}
}
@@
func (m *MemTable) Stats() uint64 {
- return m.hits
+ return m.hits.Load()
}Also applies to: 123-123, 129-129, 142-143
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@storage/zstorage/memtable.go` at line 46, The field `hits` in the memtable
struct is incremented and read without synchronization causing races; update
`Get` to use atomic.AddUint64(&m.hits, 1) instead of `m.hits++`, and change
reads in `Stats()` (and any other direct reads at the noted locations) to use
atomic.LoadUint64(&m.hits); ensure you import sync/atomic and operate on the
memtable pointer receiver (e.g., methods `Get`, `Stats`) so atomic functions
receive the address of the `hits` field.
|
演示用的临时 PR(含故意的并发坑),已验证 BanGD 能从架构层面点评,关闭。pin 升级将由单独的干净 PR 提交。 |
为观测缓存命中率,在
MemTable上新增hits计数:Get命中时m.hits++,并加Stats()返回命中数。Summary by CodeRabbit
New Features
Chores