被代理的 WeKnora 收到 SIGTERM 会先关监听再 exit(1),在途请求被切断。 排空改到真正的连接入口:闸门停止接客、等在途连接归零、写 drain.json 并 exit 0;协调器只认这条证据,写者退出码只作记录。 顺带修掉维护占用的一条死锁:释放后的窗口此前不能被新作业接管, 等于第一次同步之后谁也再进不去;持有中的窗口仍只允许原作业重入。
This commit is contained in:
@@ -29,6 +29,24 @@ type Service struct {
|
||||
ExitCode *int `json:"exit_code,omitempty"`
|
||||
}
|
||||
|
||||
// GateEvidence 是"入口真的关了、在途真的排空了"的证据。
|
||||
// 只有被代理的服务自己不会优雅退出时,才知道为什么要在入口这一层排空。
|
||||
type GateEvidence struct {
|
||||
Closed bool `json:"closed"`
|
||||
Drained bool `json:"drained"`
|
||||
ActiveAtClose int `json:"active_at_close"`
|
||||
Served int `json:"served"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
// Gate 是目标的唯一对外入口:维护窗口先关它、等在途请求结束,再停写入者。
|
||||
type Gate interface {
|
||||
// CloseIngress 必须等到在途请求结束才返回成功;返回的不是"已发信号"而是"已排空"。
|
||||
CloseIngress(context.Context) (GateEvidence, error)
|
||||
// OpenIngress 恢复入口,并确认它真的在服务。
|
||||
OpenIngress(context.Context) error
|
||||
}
|
||||
|
||||
// Inventory must enumerate the complete, independently scoped test writer set,
|
||||
// using immutable instance IDs. Never implement it as only the currently running
|
||||
// subset. Start must wait for readiness; callers still read back the result.
|
||||
@@ -54,16 +72,21 @@ type journal struct {
|
||||
Result
|
||||
// Level 区分"实例级维护"(写入者/入口被真的关掉)与"仅治理库互斥"。
|
||||
// 没有 Level 的旧 journal 来自封存链路,按实例级处理但不冒充已验证。
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
IngressClosed bool `json:"ingress_closed,omitempty"`
|
||||
DrainVerified bool `json:"drain_verified,omitempty"`
|
||||
ContentRisk bool `json:"content_risk,omitempty"`
|
||||
RestoreBlocked bool `json:"restore_blocked,omitempty"`
|
||||
Services []serviceRecord `json:"services"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
IngressClosed bool `json:"ingress_closed,omitempty"`
|
||||
DrainVerified bool `json:"drain_verified,omitempty"`
|
||||
ContentRisk bool `json:"content_risk,omitempty"`
|
||||
RestoreBlocked bool `json:"restore_blocked,omitempty"`
|
||||
// Gate 记录入口关闭/排空的证据;没有它就不能说"入口关了"。
|
||||
// 入口恢复后证据仍保留(GateReopened=true),否则事后查不到
|
||||
// "关的时候还有几个在途请求"。
|
||||
Gate *GateEvidence `json:"gate,omitempty"`
|
||||
GateReopened bool `json:"gate_reopened,omitempty"`
|
||||
Services []serviceRecord `json:"services"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
type Coordinator struct {
|
||||
root *os.Root
|
||||
@@ -75,6 +98,7 @@ type Coordinator struct {
|
||||
// 证明入口真的关了、真的回来了,而不是"停了容器就算关"。
|
||||
ingressClosed func(context.Context) error
|
||||
ingressReady func(context.Context) error
|
||||
gate Gate
|
||||
}
|
||||
|
||||
func Open(root, source string, c Controller) (*Coordinator, error) {
|
||||
@@ -116,6 +140,15 @@ func (c *Coordinator) SetTarget(level, ingress string, closed, ready func(contex
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetGate 声明目标入口闸门:实例级维护里"关入口"就是关它、并从它拿到排空证据。
|
||||
func (c *Coordinator) SetGate(g Gate) error {
|
||||
if g == nil {
|
||||
return errors.New("invalid ingress gate")
|
||||
}
|
||||
c.gate = g
|
||||
return nil
|
||||
}
|
||||
|
||||
func validIngress(raw string) bool {
|
||||
parsed, err := url.Parse(raw)
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && parsed.Path != "" && parsed.User == nil
|
||||
@@ -312,6 +345,12 @@ func (c *Coordinator) load(job string) (journal, error) {
|
||||
if j.RestoreBlocked && !j.ContentRisk {
|
||||
return journal{}, errors.New("invalid freeze restore block")
|
||||
}
|
||||
if j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
if j.GateReopened && j.Gate == nil {
|
||||
return journal{}, errors.New("ingress reopened without close evidence")
|
||||
}
|
||||
if len(j.Services) == 0 || len(j.Services) > 256 {
|
||||
return journal{}, errors.New("invalid freeze journal services")
|
||||
}
|
||||
@@ -444,5 +483,18 @@ func (c *Coordinator) restore(ctx context.Context, j *journal) error {
|
||||
failures = errors.Join(failures, errors.New("original test service missing; replacement not started"))
|
||||
}
|
||||
}
|
||||
// 服务都回来了才开放入口:先开门再启动服务,等于把流量打向没准备好的目标。
|
||||
if j.Gate != nil {
|
||||
if c.gate == nil {
|
||||
failures = errors.Join(failures, errors.New("入口闸门缺失:不能确认目标已恢复"))
|
||||
} else if err := c.gate.OpenIngress(ctx); err != nil {
|
||||
failures = errors.Join(failures, fmt.Errorf("目标入口恢复失败:%w", err))
|
||||
} else {
|
||||
j.GateReopened = true
|
||||
if err := c.save(*j); err != nil {
|
||||
failures = errors.Join(failures, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return failures
|
||||
}
|
||||
|
||||
@@ -125,7 +125,23 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
}
|
||||
}
|
||||
|
||||
// 停写入者 + 排空 + 关入口:任何一步不成立都不许往下写。
|
||||
// 顺序是有原因的:入口先停(新请求被拒、在途请求跑完),写者后停。
|
||||
// 反过来做,正在处理的请求就会被 SIGTERM 切断——那不算排空。
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
evidence, gateErr := c.gate.CloseIngress(ctx)
|
||||
if gateErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("关闭目标入口失败:%w", gateErr), "gate_failed")
|
||||
}
|
||||
if !evidence.Closed || !evidence.Drained {
|
||||
return c.abandonWindow(ctx, &j, errors.New("目标入口未确认排空(仍有在途请求)"), "gate_not_drained")
|
||||
}
|
||||
j.Gate = &evidence
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 停写入者:任何一步不成立都不许往下写。
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
@@ -284,6 +300,10 @@ func (c *Coordinator) verifyDrain(ctx context.Context, j *journal) error {
|
||||
if needed == 0 {
|
||||
return nil
|
||||
}
|
||||
// 入口层已经证明"在途请求全部结束"时,写者的退出码只作为记录:
|
||||
// 被代理的 WeKnora 收到 SIGTERM 会自己 exit(1)(upstream bug),
|
||||
// 那不代表请求被切断——请求早在入口关闭时就已经跑完了。
|
||||
gateDrained := j.Gate != nil && j.Gate.Closed && j.Gate.Drained
|
||||
items, err := inventory(ctx, c.control)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -303,18 +323,18 @@ func (c *Coordinator) verifyDrain(ctx context.Context, j *journal) error {
|
||||
return errors.New("test writer still running")
|
||||
}
|
||||
if now.ExitCode == nil {
|
||||
if c.level == "instance" {
|
||||
if c.level == "instance" && !gateDrained {
|
||||
return fmt.Errorf("无法证明 %s 的在途请求已排空(控制器没有退出码)", s.ID)
|
||||
}
|
||||
continue
|
||||
}
|
||||
s.ExitCode = now.ExitCode
|
||||
if *now.ExitCode != 0 {
|
||||
if *now.ExitCode != 0 && !gateDrained {
|
||||
return fmt.Errorf("写入者 %s 未优雅退出(exit=%d):在途请求没有完成排空", s.ID, *now.ExitCode)
|
||||
}
|
||||
verified++
|
||||
}
|
||||
j.DrainVerified = verified == needed
|
||||
j.DrainVerified = gateDrained || verified == needed
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user