根因(实证):runMerges O(n²) 软删 + 并发遗忘碎片器 在同一张 memories 单表上 制造 MVCC 写风暴;旧 3 次/~350ms 无 jitter 预算下每次重放都撞已推进的新版本 (单次 op 3 次重放 version 598346→598347→598348→598350 逐版推进仍耗尽), 失败软删被 consolidation_pipe.go:252 的 `_ =` 静默丢弃 ⇒ 源记录 is_deleted 停 false,每次重启窗口留新残留(检索重复命中)。 修(3 件,均 Go 侧 zhiyid): ① durable pending-softdelete 队列(softdelete_pending.go):MVCC 冲突耗尽 ⇒ 把 (id, reason) 追加进 /var/lib/memoryweave/pending-softdelete.jsonl (单写者 append + 原子 tmp/rename + 同 id 去重);zhiyid 启动 + 每 30s 触发器 tick 各 drain 一次(SET is_deleted=true 幂等、重读最新已提交版本),风暴退去 后重放必落盘 —— 根治「每次重启窗口都留新残留」。 ② retrySoftDeleteRPC 预算 3→5 + 退避加 0~50% jitter:破 50+ 并发软删锁步雷群 (锁步本身是冲突持续的主因),多两次机会跨风暴窗口。 ③ consolidation_pipe.go:252 `_ =` → 显式 log(观测、禁静默);队列负责最终落盘。 测试:storage 全绿(耗尽入队 / 非可重试不入队 / 同 id 去重 / 风暴退去 drain 落盘 清空 / 风暴未退 drain 保留队列)。6 文件:2 新 + 4 改。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
144 lines
5.3 KiB
Go
144 lines
5.3 KiB
Go
// 织忆 MemoryWeave — durable pending-softdelete 队列(t_cf680fad 机制修复)
|
||
//
|
||
// 背景 / 根因:
|
||
// runMerges O(n²) 软删 + 并发遗忘碎片器在同一张 memories 单表上制造 MVCC 写风暴;
|
||
// retrySoftDeleteRPC 旧预算 3 次/~350ms(且退避无 jitter,50+ 并发软删锁步重放)⇒
|
||
// 风暴下每次重放都撞已推进的新版本 ⇒ 4 次全败 ⇒ 失败软删被 consolidation_pipe.go:252
|
||
// 的 `_ =` 静默丢弃 ⇒ 源记录 is_deleted 停在 false,每次重启窗口都留新残留
|
||
// (检索里重复命中)。实证:单次 op 的 3 次重放 version 598346→598347→598348→598350
|
||
// 逐版推进仍耗尽;24h「重试成功=0」。
|
||
//
|
||
// 本文件提供的保证("重试必须能成功落盘"):
|
||
// ① EnqueuePendingSoftDelete:retrySoftDeleteRPC MVCC 冲突耗尽时把 (id, reason) 追加进
|
||
// durable JSONL(单写者 append + 原子 tmp/rename,同 id 去重)。
|
||
// ② DrainPendingSoftDeletes:zhiyid 启动 + 每 30s 触发器 tick 各调用一次,逐条重放
|
||
// SoftDelete(SET is_deleted=true 幂等、对最新已提交版本重读),成功后移除;
|
||
// 队列空/缺失 = 立即 no-op。风暴退去后(或下次重启窗口)重放必落盘。
|
||
package storage
|
||
|
||
import (
|
||
"bufio"
|
||
"encoding/json"
|
||
"log"
|
||
"os"
|
||
"path/filepath"
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// defaultPendingSoftDeletePath 生产路径。测试用 env ZHIYI_SOFTDELETE_PENDING 覆盖(不污染生产库)。
|
||
const defaultPendingSoftDeletePath = "/var/lib/memoryweave/pending-softdelete.jsonl"
|
||
|
||
func pendingSoftDeletePath() string {
|
||
if p := os.Getenv("ZHIYI_SOFTDELETE_PENDING"); p != "" {
|
||
return p
|
||
}
|
||
return defaultPendingSoftDeletePath
|
||
}
|
||
|
||
type pendingSoftDeleteEntry struct {
|
||
ID string `json:"id"`
|
||
Reason string `json:"reason"`
|
||
TS string `json:"ts"`
|
||
}
|
||
|
||
// pendingMu 单写者护栏:Go 侧 zhiyid 是唯一写者(Rust sidecar 不碰此文件)。
|
||
var pendingMu sync.Mutex
|
||
|
||
func readPendingEntries(p string) []pendingSoftDeleteEntry {
|
||
f, err := os.Open(p)
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
defer f.Close()
|
||
var out []pendingSoftDeleteEntry
|
||
sc := bufio.NewScanner(f)
|
||
sc.Buffer(make([]byte, 1<<20), 1<<20)
|
||
for sc.Scan() {
|
||
var e pendingSoftDeleteEntry
|
||
if json.Unmarshal([]byte(sc.Text()), &e) == nil && e.ID != "" {
|
||
out = append(out, e)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
func rewritePendingFile(p string, entries []pendingSoftDeleteEntry) {
|
||
tmp := p + ".tmp"
|
||
f, err := os.OpenFile(tmp, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
|
||
if err != nil {
|
||
log.Printf("[ipc] pending-softdelete rewrite open failed: %v", err)
|
||
return
|
||
}
|
||
w := bufio.NewWriter(f)
|
||
for _, e := range entries {
|
||
b, _ := json.Marshal(e)
|
||
_, _ = w.Write(b)
|
||
_, _ = w.Write([]byte{'\n'})
|
||
}
|
||
if err := w.Flush(); err != nil {
|
||
f.Close()
|
||
log.Printf("[ipc] pending-softdelete rewrite flush failed: %v", err)
|
||
return
|
||
}
|
||
f.Close()
|
||
if err := os.Rename(tmp, p); err != nil {
|
||
log.Printf("[ipc] pending-softdelete rewrite rename failed: %v", err)
|
||
}
|
||
}
|
||
|
||
// EnqueuePendingSoftDelete 追加一条待软删(幂等去重:同 id 只保留一条)。
|
||
// 由 retrySoftDeleteRPC 在 MVCC 冲突耗尽时调用;schema/空 id 等真错误不调(入队无意义)。
|
||
func EnqueuePendingSoftDelete(id, reason string) {
|
||
p := pendingSoftDeletePath()
|
||
if p == "" {
|
||
return
|
||
}
|
||
_ = os.MkdirAll(filepath.Dir(p), 0o755)
|
||
pendingMu.Lock()
|
||
defer pendingMu.Unlock()
|
||
entries := readPendingEntries(p)
|
||
for _, e := range entries {
|
||
if e.ID == id {
|
||
return // 已在队列,不重复(reason 保留最早一条)
|
||
}
|
||
}
|
||
entries = append(entries, pendingSoftDeleteEntry{ID: id, Reason: reason, TS: time.Now().Format(time.RFC3339)})
|
||
rewritePendingFile(p, entries)
|
||
log.Printf("[ipc] pending-softdelete ENQUEUED: id=%s reason=%s queue=%d", id, reason, len(entries))
|
||
}
|
||
|
||
// PendingSoftDeleteCount 返回队列当前条数(观测 / 测试用;缺失 = 0)。
|
||
func PendingSoftDeleteCount() int {
|
||
entries := readPendingEntries(pendingSoftDeletePath())
|
||
return len(entries)
|
||
}
|
||
|
||
// DrainPendingSoftDeletes 逐条重放待软删(幂等),成功后从队列移除。
|
||
// 启动 + 每 tick 各调用一次;队列空/缺失 = 立即 no-op(廉价 stat+read)。
|
||
// 参数取 LanceDB 接口(任意后端);内部调 rc.SoftDelete(RustLanceDBClient 走 IPC+重试+入队)。
|
||
// 返回 (成功条数, 仍失败条数);仍失败者保留在队列,下一 tick/下次启动再重放。
|
||
func DrainPendingSoftDeletes(ldb LanceDB) (ok, failed int) {
|
||
p := pendingSoftDeletePath()
|
||
entries := readPendingEntries(p)
|
||
if len(entries) == 0 {
|
||
return 0, 0
|
||
}
|
||
var remaining []pendingSoftDeleteEntry
|
||
for _, e := range entries {
|
||
if err := ldb.SoftDelete(e.ID, e.Reason); err != nil {
|
||
// 仍失败(仍在风暴内 / 真错误):保留在队列,下一轮再试。
|
||
// 注意:若这条 SoftDelete 内部又因 MVCC 耗尽触发 EnqueuePendingSoftDelete,
|
||
// 同 id 去重保证不会重复入队;此处 remaining 收口最终一致。
|
||
log.Printf("[ipc] pending-softdelete drain RETRY FAILED: id=%s reason=%s err=%v", e.ID, e.Reason, err)
|
||
remaining = append(remaining, e)
|
||
failed++
|
||
} else {
|
||
ok++
|
||
}
|
||
}
|
||
rewritePendingFile(p, remaining)
|
||
log.Printf("[ipc] pending-softdelete DRAIN: retried=%d ok=%d still_pending=%d", len(entries), ok, len(remaining))
|
||
return ok, failed
|
||
}
|