为什么:父审指出预备份引用持久化失败仍继续写目标、接管依赖启动扫描且漏掉租约未过期、 恢复失败被标成"无需恢复"、版本登记与作业状态分叉。 - BackupPrepared 返回 error,持久化失败即零写入;预备份同时登记 production_prebackup 版本。 - 接管改为按队列租约判定 + 运行期 30s 扫描 + 原子抢占 + 固定幂等键 + attempt 归零。 - 接管场景保留预备份、不再宣称"未写无需恢复"。 - 恢复/重放同一快照重新激活 production_effective(清 superseded_at)。 - finalize 续作只做校验+登记(finalize_only),不重写生产。 - 索引映射基线缺失时采用首次观察值并登记;索引校验加孤儿文档检查(按 knowledge_id)。 真实验收(候选 API :18089 + 真实 PG/ES/MinIO): A0 首次发布、A1 撤销、B 按版本恢复、C 写阶段 SIGKILL 接管、 D 跨 kind 互斥与多 worker 单次执行、E1 登记失败、E2 finalize 续作、E3 预备份失败零写入。
This commit is contained in:
@@ -93,7 +93,9 @@ type Recorder interface {
|
||||
// BackupObserver 让调用方在预备份完成时立刻持久化快照身份:
|
||||
// worker 被杀后重启必须能接管这份预备份,而不是重新备份覆盖它。
|
||||
type BackupObserver interface {
|
||||
BackupPrepared(ctx context.Context, backup BackupRef)
|
||||
// 必须返回错误:预备份引用没能持久化时,worker 被杀就没有恢复基线,
|
||||
// 这种情况下一个字节都不许写目标。调用方据此直接失败。
|
||||
BackupPrepared(ctx context.Context, backup BackupRef) error
|
||||
}
|
||||
|
||||
// FinalizeStages 是"写入完成之后的收尾":先登记权威版本,再把作业标成功。
|
||||
@@ -117,6 +119,7 @@ type Outcome struct {
|
||||
FailedAt Stage `json:"failed_at,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Rollback string `json:"rollback,omitempty"`
|
||||
Recovery string `json:"recovery,omitempty"`
|
||||
Finished time.Time `json:"finished"`
|
||||
}
|
||||
|
||||
@@ -178,7 +181,14 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
|
||||
}
|
||||
outcome.Backup = backup
|
||||
if observer, ok := recorder.(BackupObserver); ok {
|
||||
observer.BackupPrepared(ctx, backup)
|
||||
if err := observer.BackupPrepared(ctx, backup); err != nil {
|
||||
// 硬门禁:持久化失败 = 没有可恢复基线 = 绝不写目标。
|
||||
outcome.State = "failed"
|
||||
outcome.Error = "预备份已生成但引用无法持久化,未对生产执行任何写入:" + err.Error()
|
||||
outcome.Recovery = "预备份 " + backup.SnapshotID + " 已落盘,先修复治理库再重试"
|
||||
recorder.Stage(ctx, "prebackup", "failed", outcome.Error)
|
||||
return outcome
|
||||
}
|
||||
}
|
||||
recorder.Stage(ctx, "prebackup", "succeeded", "预备份 "+backup.ID)
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package libraryrelease
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// failingObserver 模拟"预备份引用无法持久化":真实场景是治理库写入失败。
|
||||
type failingObserver struct{ stageLog }
|
||||
|
||||
func (f *failingObserver) BackupPrepared(context.Context, BackupRef) error {
|
||||
return errors.New("persist backup ref failed")
|
||||
}
|
||||
|
||||
// 预备份引用持久化失败时必须零写入:没有可恢复基线就不许碰目标。
|
||||
func TestRunRefusesToWriteWhenBackupReferenceCannotBePersisted(t *testing.T) {
|
||||
target := &fakeTarget{}
|
||||
backups := &fakeBackups{}
|
||||
recorder := &failingObserver{}
|
||||
outcome := Run(context.Background(), request(time.Now()), target, backups, recorder)
|
||||
if outcome.State == "succeeded" {
|
||||
t.Fatalf("state = %s, must not succeed without a recoverable backup", outcome.State)
|
||||
}
|
||||
if len(target.applied) != 0 {
|
||||
t.Fatalf("applied stages = %v, must be zero when the backup reference is lost", target.applied)
|
||||
}
|
||||
if target.restored != 0 {
|
||||
t.Fatalf("restore must not run when nothing was written (restored=%d)", target.restored)
|
||||
}
|
||||
if !recorder.has("prebackup:failed") {
|
||||
t.Fatalf("stage log = %v, want prebackup:failed", recorder.entries)
|
||||
}
|
||||
if outcome.Recovery == "" {
|
||||
t.Fatal("failed outcome must explain how to recover")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user