上一次窗口写下的 StopRequested 只是历史,写入者被人工拉起来后必须再停一次, 否则恢复校验会跑在半开的目标上。补一条回归测试。
This commit is contained in:
@@ -331,14 +331,26 @@ func (c *Coordinator) awaitBackgroundIdle(ctx context.Context, j *journal) error
|
||||
// stopAll 只对"原本在跑"的服务发停止指令,每发一个都先落盘意图:
|
||||
// 进程在停止过程中死掉时,磁盘上仍能看出"哪些服务被要求停过"。
|
||||
func (c *Coordinator) stopAll(ctx context.Context, j *journal) error {
|
||||
// 以"现在是否真的在跑"为准,而不是上次窗口留下的 StopRequested:
|
||||
// 恢复阶段被人工拉起来的写入者必须再停一次,否则校验跑在半开的目标上。
|
||||
items, err := inventory(ctx, c.control)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
running := map[string]bool{}
|
||||
for _, s := range items {
|
||||
running[s.ID] = s.Running
|
||||
}
|
||||
var failures error
|
||||
for i := range j.Services {
|
||||
if !j.Services[i].Running || j.Services[i].StopRequested {
|
||||
if !running[j.Services[i].ID] {
|
||||
continue
|
||||
}
|
||||
j.Services[i].StopRequested = true
|
||||
if err := c.save(*j); err != nil {
|
||||
return err
|
||||
if !j.Services[i].StopRequested {
|
||||
j.Services[i].StopRequested = true
|
||||
if err := c.save(*j); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := c.control.Stop(ctx, j.Services[i].ID); err != nil {
|
||||
failures = errors.Join(failures, err)
|
||||
|
||||
@@ -325,3 +325,33 @@ func TestWindowRefusesWhenGateNotDrained(t *testing.T) {
|
||||
t.Fatalf("writers were stopped before the ingress was drained: %v", c.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// 窗口开过之后被人工拉起来的写入者,恢复时必须再停一次:
|
||||
// 上一次窗口写下的 StopRequested 不能当成"它现在还停着"。
|
||||
func TestStopAllStopsWriterRestartedAfterWindowOpened(t *testing.T) {
|
||||
_, c, engine := instanceFixture(t)
|
||||
j := journal{Version: 1, Source: "target-instance", Job: "job-restart",
|
||||
Result: Result{State: "held"}, Level: "instance",
|
||||
Services: []serviceRecord{{Service: Service{ID: "app", Running: true, Healthy: true}, StopRequested: true}}}
|
||||
if err := engine.stopAll(context.Background(), &j); err != nil {
|
||||
t.Fatalf("stopAll: %v", err)
|
||||
}
|
||||
items, err := c.Inventory(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, s := range items {
|
||||
if s.ID == "app" && s.Running {
|
||||
t.Fatal("写入者被人工拉起后没有被重新停住")
|
||||
}
|
||||
}
|
||||
found := false
|
||||
for _, call := range c.calls {
|
||||
if call == "stop:app" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("没有发出停止指令:%v", c.calls)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user