feat(library): 整库发布身份、写集合与收尾边界
build / build (push) Successful in 1m45s

执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、
租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
姚章浩
2026-11-01 11:35:10 +08:00
parent 5afd8c0140
commit 8d838d9cb4
82 changed files with 4809 additions and 551 deletions
+42
View File
@@ -115,6 +115,16 @@ type Recorder interface {
Stage(ctx context.Context, stage string, state string, detail string)
}
// GrantRevalidator 让编排在每笔副作用前按**持久授权**回读。
//
// 内存里的 Grant 只是一个快照:它既不能延长有效期,也不能掩盖中途撤销。
// 目标实现了这个接口就必须给出权威答案(读治理库的授权行);返回错误即拒绝继续。
// 未实现时行为不变(只核静态有效期),所以这不是给"忘记接线"留的后门 ——
// 生产目标的 wrapper 由 runLibraryRelease 无条件接线。
type GrantRevalidator interface {
RevalidateGrant(ctx context.Context, grant Grant) error
}
// BackupObserver 让调用方在预备份完成时立刻持久化快照身份:
// worker 被杀后重启必须能接管这份预备份,而不是重新备份覆盖它。
type BackupObserver interface {
@@ -184,6 +194,8 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
if recorder == nil {
recorder = NoopRecorder{}
}
// Request.Now is a diagnostic input, not a clock authority for execution.
request.Now = time.Now().UTC()
if err := ValidateGrant(request); err != nil {
outcome.Error = err.Error()
recorder.Stage(ctx, "prebackup", "failed", err.Error())
@@ -217,6 +229,33 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
}
recorder.Stage(ctx, "prebackup", "succeeded", "预备份 "+backup.ID)
// Backup may outlive the grant. Recheck before planning (which may allocate
// numbers), and again after durable plan registration before the first apply.
// With a GrantRevalidator the check is authoritative: a revoked or expired
// authorization row stops the run even though the in-memory grant still looks
// valid, and a stale in-memory expiry cannot be extended either.
checkLiveGrant := func() bool {
request.Now = time.Now().UTC()
if e := ValidateGrant(request); e != nil {
outcome.Error = e.Error()
outcome.Recovery = "预备份保留;需要有效的新授权后继续"
recorder.Stage(ctx, "prebackup", "failed", outcome.Error)
return false
}
if revalidator, ok := target.(GrantRevalidator); ok {
if e := revalidator.RevalidateGrant(ctx, request.Grant); e != nil {
outcome.Error = "持久授权已失效,未继续写入:" + e.Error()
outcome.Recovery = "预备份保留;重新签发授权后按原预备份继续"
recorder.Stage(ctx, "prebackup", "failed", outcome.Error)
return false
}
}
return true
}
if !checkLiveGrant() {
return outcome
}
// 写集合必须在第一笔写入之前落盘并登记:恢复只按"这次明确写了什么"清理,
// 不按目标现状推断残渣(那会把历史孤儿/共享资源当成本次残留删掉)。
existingPlan := backup.WriteSet != nil
@@ -265,6 +304,9 @@ func Run(ctx context.Context, request Request, target Target, backups BackupStor
}
}
if !checkLiveGrant() {
return outcome
}
for _, stage := range Stages() {
recorder.Stage(ctx, string(stage), "running", "")
if err = target.Apply(ctx, stage, request.SnapshotID); err != nil {