feat(library): 入口闸门排空 + 维护占用可再接管
build / build (push) Successful in 1m28s

被代理的 WeKnora 收到 SIGTERM 会先关监听再 exit(1),在途请求被切断。
排空改到真正的连接入口:闸门停止接客、等在途连接归零、写 drain.json
并 exit 0;协调器只认这条证据,写者退出码只作记录。

顺带修掉维护占用的一条死锁:释放后的窗口此前不能被新作业接管,
等于第一次同步之后谁也再进不去;持有中的窗口仍只允许原作业重入。
This commit is contained in:
姚章浩
2026-10-28 23:35:15 +08:00
parent 6fe98c9a30
commit 38d04488de
10 changed files with 533 additions and 67 deletions
+61
View File
@@ -251,3 +251,64 @@ func TestLibraryLevelRejectsIngressClaim(t *testing.T) {
t.Fatal("empty level accepted")
}
}
// fakeGate 记录"入口先关、写者后停、恢复时先起服务再开入口"的顺序。
type fakeGate struct {
calls *[]string
drain bool
active int
}
func (g *fakeGate) CloseIngress(context.Context) (GateEvidence, error) {
*g.calls = append(*g.calls, "gate:close")
return GateEvidence{Closed: true, Drained: g.drain, ActiveAtClose: g.active, Served: 3}, nil
}
func (g *fakeGate) OpenIngress(context.Context) error {
*g.calls = append(*g.calls, "gate:open")
return nil
}
func TestWindowClosesGateBeforeStoppingWriters(t *testing.T) {
_, c, e := instanceFixture(t)
e.ingressClosed = func(context.Context) error { return nil }
gate := &fakeGate{calls: &c.calls, drain: true, active: 1}
if err := e.SetGate(gate); err != nil {
t.Fatal(err)
}
_, err := e.RunWindow(context.Background(), "job-gate", func(context.Context) error { return nil },
func(context.Context, *Window) (string, error) { return "", nil }, func(error) bool { return true })
if err != nil {
t.Fatal(err)
}
want := "gate:close,stop:app,stop:store,start:app,start:store,gate:open"
if strings.Join(c.calls, ",") != want {
t.Fatalf("calls=%v want=%s", c.calls, want)
}
// 写者退出码非 0 也不影响结论:排空是在入口层证明的(被代理服务自己不优雅退出)。
j, err := e.load("job-gate")
if err != nil || !j.DrainVerified {
t.Fatalf("journal=%+v err=%v", j, err)
}
// 入口恢复后证据仍留着:报告要能说清"关的时候还有几个在途请求"。
if j.Gate == nil || !j.GateReopened {
t.Fatalf("gate evidence lost after reopening: %+v reopened=%v", j.Gate, j.GateReopened)
}
}
// 入口没排空(还有在途请求)时不许停写者、更不许写目标。
func TestWindowRefusesWhenGateNotDrained(t *testing.T) {
_, c, e := instanceFixture(t)
gate := &fakeGate{calls: &c.calls, drain: false, active: 2}
if err := e.SetGate(gate); err != nil {
t.Fatal(err)
}
work := 0
_, err := e.RunWindow(context.Background(), "job-gate-bad", func(context.Context) error { return nil },
func(context.Context, *Window) (string, error) { work++; return "", nil }, func(error) bool { return true })
if err == nil || work != 0 {
t.Fatalf("err=%v work=%d", err, work)
}
if strings.Join(c.calls, ",") != "gate:close" {
t.Fatalf("writers were stopped before the ingress was drained: %v", c.calls)
}
}