fix(storage): t_fa797edd 优雅停止全程有界 — Stop 最终落盘不再无限阻塞

根因:SIGTERM 到 StopRecallWriteBuffer(),Stop() 同步调用无界的 b.Flush()。
Rust/LanceDB 批量更新在活 IPC 流量下被卡住(生产实测 20:44 停止窗口内 Rust 侧
update_recall_batch 80s 未完成),Stop 永不返回,信号协程到不了 srv.Shutdown,
进程跑满 systemd 90s TimeoutStopSec 被 SIGKILL(丢写风险 + 重启不可控)。

修复:Stop() 全程有界 — 等后台协程 <=5s + 最终落盘 <=8s(goroutine+超时)。
召回统计丢失可接受(源码注释本就声明崩溃会丢最后一窗口),超时即放行退出。
新增反向用例 TestRecallWriteBufferStopBoundedWhenFlushHangs:flush 挂起时
Stop 在 8.0s 内放行(原实现无限阻塞)。storage 包全量测试通过,zhiyid 构建
exit=0。未部署(AC5 边界:仅改源码 + 隔离验证)。
This commit is contained in:
小唯
2026-10-18 00:51:46 +08:00
parent f6c373ea94
commit 71d5f2a7cd
2 changed files with 54 additions and 3 deletions
+26 -3
View File
@@ -45,6 +45,12 @@ type BatchRecallUpdater interface {
const (
defaultRecallFlushInterval = 300 * time.Second
defaultRecallFlushMaxItems = 256
// 优雅停止窗口(t_fa797edd):Stop 内的「等后台协程退出 + 最终落盘」都必须有界。
// 否则 Rust/LanceDB 批量更新在活 IPC 流量下阻塞时,Stop 永远不返回 → 信号协程
// 到不了 srv.Shutdown → 进程跑满 systemd 90s TimeoutStopSec 被 SIGKILL
// (丢写风险 + 每次重启都变强杀 + 40s IPC 中断)。
defaultRecallStopLoopWait = 5 * time.Second // 等后台 flush 协程退出的上限
defaultRecallStopFlushWait = 8 * time.Second // 最终落盘的上限(召回统计,丢失可接受)
)
type pendingRecall struct {
@@ -249,6 +255,10 @@ func (b *RecallWriteBuffer) Flush() (int, error) {
}
// Stop 停止后台 flush 协程,并把最后一个窗口落盘(进程优雅退出时调用)。
// 全程有界(t_fa797edd):等后台协程 ≤ defaultRecallStopLoopWait,最终落盘
// ≤ defaultRecallStopFlushWait。Rust/LanceDB 批量更新在活 IPC 流量下可能阻塞
// 数十秒;召回统计丢了可接受(源码注释「进程崩溃会丢最后一个窗口」),
// 因此超时即放弃落盘、放行退出,保证进程总在有限秒内到 srv.Shutdown。
func (b *RecallWriteBuffer) Stop() {
b.mu.Lock()
if b.stopped {
@@ -261,10 +271,23 @@ func (b *RecallWriteBuffer) Stop() {
close(b.stopCh)
select {
case <-b.doneCh:
case <-time.After(10 * time.Second):
log.Printf("[recall-buffer] 停止超时,直接落盘剩余窗口")
case <-time.After(defaultRecallStopLoopWait):
log.Printf("[recall-buffer] 后台 flush 协程停止超时(%s),直接进入最终落盘", defaultRecallStopLoopWait)
}
// 最终落盘有界:放到 goroutine 跑,到点不再等。若后台 ticker/阈值 flush
// 正持锁阻塞在 Rust IPC 上,本 goroutine 的 Flush 会先等 b.mu —— 同样被
// defaultRecallStopFlushWait 兜住,超时即放行进程退出。
flushDone := make(chan struct{})
go func() {
b.Flush()
close(flushDone)
}()
select {
case <-flushDone:
case <-time.After(defaultRecallStopFlushWait):
log.Printf("[recall-buffer] 最终落盘超时(%s),放弃剩余窗口(仅丢召回统计,进程照常退出)", defaultRecallStopFlushWait)
}
b.Flush()
}
// Stats 观测用(flush 频率 / 合并率 / 版本写入行数)。
@@ -240,3 +240,31 @@ func TestRecallWriteBufferStopFlushes(t *testing.T) {
t.Fatalf("Stop 应落盘最后一个窗口,batchCalls=%d", fake.batchCalls)
}
}
// hangingLDB:UpdateRecallBatch 永不返回(模拟 Rust/LanceDB 批量更新在活 IPC
// 流量下被卡死,t_fa797edd 生产实测 80s+)。用于验证 Stop 有界:不会因 flush
// 挂起而无限阻塞进程退出。
type hangingLDB struct{ fakeLDB }
func (h *hangingLDB) UpdateRecallBatch(table string, items []RecallWriteItem, lastRecalledAt string) (int64, error) {
select {} // 永不返回
}
// t_fa797edd 反向用例:最终落盘挂起时,Stop 必须在有界时间内返回(不许无限阻塞)。
// 原实现 b.Flush() 同步调用 → Rust 卡死则 Stop 永不返回 → 进程到不了 srv.Shutdown
// → 跑满 systemd 90s 被 SIGKILL。修复后 Stop 在 defaultRecallStopFlushWait 内放行。
func TestRecallWriteBufferStopBoundedWhenFlushHangs(t *testing.T) {
h := &hangingLDB{}
b := NewRecallWriteBuffer(h, 0, 1000)
b.Record([]string{"mem_hang"})
start := time.Now()
b.Stop()
elapsed := time.Since(start)
// 有界上限:后台协程等待(5s) + 最终落盘等待(8s) + 裕量。50s 远小于 systemd 90s 超时。
if elapsed > 50*time.Second {
t.Fatalf("Stop 应是有界的,实际阻塞了 %s(>50s,会触发 systemd 90s SIGKILL)", elapsed)
}
t.Logf("Stop 在 %s 内放行(flush 挂起时也不阻塞进程退出)", elapsed)
}