删掉会自己开入口、只看服务健康的 FinalizeAfterOperatorRecovery。 新的 RecoverWindow 绑定原作业/目标指纹/持久化预备份,在维护关闭下 执行恢复与逐类校验,通过才恢复服务与入口;失败继续维护。 预备份身份与内容风险在第一笔写入前一起落盘;后台任务必须静默后才停写者; 写入者发布端口即拒绝开窗口;维护占用改为按实例互斥(000072)。
This commit is contained in:
@@ -34,6 +34,25 @@ func (w *Window) MarkContentRisk(ctx context.Context) error {
|
||||
return w.c.save(*w.j)
|
||||
}
|
||||
|
||||
// SetBackupSnapshot 把"这次窗口的预备份身份"落盘。恢复只认它:
|
||||
// 没有这份身份的窗口,之后既不能自动恢复,也不许被外部收尾。
|
||||
func (w *Window) SetBackupSnapshot(ctx context.Context, snapshotID string) error {
|
||||
if w == nil || w.c == nil || w.j == nil {
|
||||
return errors.New("invalid maintenance window")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validDigest(snapshotID) {
|
||||
return errors.New("invalid backup snapshot id")
|
||||
}
|
||||
if w.j.BackupSnapshotID != "" && w.j.BackupSnapshotID != snapshotID {
|
||||
return errors.New("backup snapshot changed inside the same window")
|
||||
}
|
||||
w.j.BackupSnapshotID = snapshotID
|
||||
return w.c.save(*w.j)
|
||||
}
|
||||
|
||||
// Job 返回窗口所属作业 id(恢复器与调用方据此对账)。
|
||||
func (w *Window) Job() string {
|
||||
if w == nil || w.j == nil {
|
||||
@@ -113,7 +132,7 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
return Result{}, err
|
||||
}
|
||||
j = journal{Version: 1, Source: c.source, Job: job, Level: c.level, Ingress: c.ingress,
|
||||
Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
for _, s := range items {
|
||||
if s.Running && !s.Healthy {
|
||||
return Result{}, errors.New("test writer is not ready")
|
||||
@@ -125,6 +144,12 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
}
|
||||
}
|
||||
|
||||
// 后台任务必须先在"还能跑"的时候跑完:入口排空只覆盖 HTTP 连接,
|
||||
// app 内的 asynq worker 会被上游的 exit(1) 直接切断。
|
||||
if idleErr := c.awaitBackgroundIdle(ctx, &j); idleErr != nil {
|
||||
return c.abandonWindow(ctx, &j, idleErr, "background_busy")
|
||||
}
|
||||
|
||||
// 顺序是有原因的:入口先停(新请求被拒、在途请求跑完),写者后停。
|
||||
// 反过来做,正在处理的请求就会被 SIGTERM 切断——那不算排空。
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
@@ -267,6 +292,42 @@ func (c *Coordinator) closeWindow(ctx context.Context, j *journal, cause error,
|
||||
return j.Result, cause
|
||||
}
|
||||
|
||||
// awaitBackgroundIdle 等到后台任务队列为空再往下走。拿不到结论(查询失败)
|
||||
// 也按"没空"处理:不能把"问不到"当成"没任务"。
|
||||
func (c *Coordinator) awaitBackgroundIdle(ctx context.Context, j *journal) error {
|
||||
if c.quiescer == nil {
|
||||
if c.level == "instance" {
|
||||
return errors.New("没有后台任务静默检查:不能证明停写者时后台任务已结束")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
wait := c.quiesceWait
|
||||
if wait <= 0 {
|
||||
wait = 2 * time.Minute
|
||||
}
|
||||
deadline := time.Now().Add(wait)
|
||||
var last string
|
||||
for {
|
||||
idle, detail, err := c.quiescer.Idle(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读不到后台任务状态:%w", err)
|
||||
}
|
||||
if idle {
|
||||
j.BackgroundIdle = true
|
||||
return nil
|
||||
}
|
||||
last = detail
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("后台任务仍在运行(%s):等它结束再开窗口,否则会被 SIGTERM 切断", last)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// stopAll 只对"原本在跑"的服务发停止指令,每发一个都先落盘意图:
|
||||
// 进程在停止过程中死掉时,磁盘上仍能看出"哪些服务被要求停过"。
|
||||
func (c *Coordinator) stopAll(ctx context.Context, j *journal) error {
|
||||
@@ -342,63 +403,3 @@ func validDigest(id string) bool {
|
||||
decoded, err := hex.DecodeString(id)
|
||||
return err == nil && len(decoded) == 32 && strings.ToLower(id) == id
|
||||
}
|
||||
|
||||
// FinalizeAfterOperatorRecovery 收尾一个"内容风险未验证 + 人工已恢复"的窗口。
|
||||
//
|
||||
// 它不启动任何服务、也不释放治理库存的占用:只做两件事——核对"原本在跑的服务
|
||||
// 现在确实在跑且健康、入口确实在服务",然后把 journal 标成 operator_recovered。
|
||||
// 没有这两条核对,它就退化成"把卡住的记录清掉",那是另一种误放行。
|
||||
func (c *Coordinator) FinalizeAfterOperatorRecovery(ctx context.Context, job string) error {
|
||||
if !validID(job) {
|
||||
return errors.New("invalid freeze job id")
|
||||
}
|
||||
lock, err := c.lock()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer unlock(lock)
|
||||
j, err := c.load(job)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if terminal(j.State) {
|
||||
return errors.New("freeze journal is already terminal")
|
||||
}
|
||||
if !j.ContentRisk {
|
||||
return errors.New("journal has no content risk; use Recover instead")
|
||||
}
|
||||
items, err := inventory(ctx, c.control)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
running := map[string]Service{}
|
||||
for _, item := range items {
|
||||
running[item.ID] = item
|
||||
}
|
||||
for _, s := range j.Services {
|
||||
if !s.Running {
|
||||
continue
|
||||
}
|
||||
now, ok := running[s.ID]
|
||||
if !ok || !now.Running || !now.Healthy {
|
||||
return fmt.Errorf("服务 %s 还没有恢复到健康状态,不能收尾这个窗口", s.ID)
|
||||
}
|
||||
}
|
||||
if c.gate != nil && j.Gate != nil && !j.GateReopened {
|
||||
if err := c.gate.OpenIngress(ctx); err != nil {
|
||||
return fmt.Errorf("入口还没有恢复:%w", err)
|
||||
}
|
||||
j.GateReopened = true
|
||||
}
|
||||
if c.ingressReady != nil {
|
||||
if err := c.ingressReady(ctx); err != nil {
|
||||
return fmt.Errorf("入口验证未通过:%w", err)
|
||||
}
|
||||
}
|
||||
ended := time.Now().UTC()
|
||||
j.State = "failed"
|
||||
j.Code = "operator_recovered"
|
||||
j.RestoreBlocked = false
|
||||
j.FinishedAt = &ended
|
||||
return c.save(j)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user