From 09c2319d7ee5f8b2e4c0df434c5bbaa2349e8f97 Mon Sep 17 00:00:00 2001 From: xiaowei Date: Sun, 31 May 2026 02:14:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(G6):=20recall=5Fcount=20=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E9=80=92=E5=A2=9E=20+=20cache=20population?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题: 1. recall goroutine 通过 id 查 _local.memories 总是 cache miss(cache key 格式不匹配) 2. Update() 用 defer unlock,IPC 调用在锁外,并发时丢失递增 3. 缓存命中时跳过 incrementRecallCount 修复: 1. recall.go: 结果写入 _local.memories(cache key=id) 2. lancedb_ipc.go: 移除 defer unlock,整个 IPC 调用在锁内 3. lancedb_ipc.go: Update 发送 cache 当前值(不是硬编码 "1") 4. lancedb_ipc.go: 支持 map[string]string 和 map[string]interface{} 两类 --- go/internal/storage/lancedb_ipc.go | 2 +- go/internal/storage/recall.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/go/internal/storage/lancedb_ipc.go b/go/internal/storage/lancedb_ipc.go index 1853dde..292d17a 100644 --- a/go/internal/storage/lancedb_ipc.go +++ b/go/internal/storage/lancedb_ipc.go @@ -297,7 +297,6 @@ func (rc *RustLanceDBClient) Insert(table string, record any) error { // 保证并发调用时不会 read stale value 再写回 func (rc *RustLanceDBClient) Update(table, id string, fields map[string]any) error { _local.mu.Lock() - defer _local.mu.Unlock() // 1. 更新本地内存缓存 if m, ok := _local.memories[id]; ok { @@ -380,6 +379,7 @@ func (rc *RustLanceDBClient) Update(table, id string, fields map[string]any) err log.Printf("[ipc] update failed (memory only): %v", err) } } + _local.mu.Unlock() return nil } diff --git a/go/internal/storage/recall.go b/go/internal/storage/recall.go index 2826d91..396c780 100644 --- a/go/internal/storage/recall.go +++ b/go/internal/storage/recall.go @@ -214,6 +214,24 @@ func (p *RecallPipeline) Recall(query, namespace string, topK int, diversity flo } } + // 将召回结果写入本地缓存(goroutine 依赖此 cache 做 increment) + // cache key = id,与 Update() 中 lookup key 一致 + for _, r := range results { + if r.ID == "" { + continue + } + _local.mu.Lock() + if existing, ok := _local.memories[r.ID]; ok { + existing.RecallCount++ + } else { + _local.memories[r.ID] = &models.MemoryRecord{ + ID: r.ID, + RecallCount: 1, + } + } + _local.mu.Unlock() + } + // Async increment recall_count + update last_recalled_at + importance go p.incrementRecallCount(results)