780ab988c4
build / build (push) Successful in 1m20s
真实现场 2026-10-30T00:19Z:新恢复作业恢复历史快照 fc595511(已被 0d982f76 顶替),写入 database 后被 SIGKILL;重启接管时 classifyTakeover 只要看到 该快照有 SupersededAt 就判 superseded,没有判这行历史版本属于哪个作业, 把历史那次发布被顶替当成这次作业过期,作业 failed/prebackup,而恢复文案 又写"无需恢复",目标半写且维护锁未释放。 修法:被顶替分支先分清归属——本作业 finalize 成功或版本行属于本作业 ⇒ 仍 拒绝;历史行属于别的作业时,仅当本次是 restore、授权签发于顶替之后、scope 与 instance 两把锁仍由本作业独占且指纹一致、预备份登记属于本作业且清单字节 仍等于摘要,才按 fresh 继续重跑整条链路。任一证据缺失/读不到仍拒绝。 同时把续作早退统一走 failLibraryJobOrTakeover:有持久化预备份时保留 needs_restore 并点名 backup,不再写"无需恢复"。 验证: - 先写失败回归(历史行+新授权 ⇒ 期望 fresh,实得 superseded),修后绿; - 真实 PostgreSQL 正负例:授权撤销/实例锁释放/预备份归属被改/清单字节被改 均回到 superseded;续作早退保留备份与 needs_restore; - 本地 make verify 16 项必需检查全绿(identity 本地档案跳过)。
82 lines
3.3 KiB
Go
82 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
)
|
|
|
|
// takeoverBaselineMaxBlobBytes 与其它归档读取点保持一致:基线清单只校验自身与
|
|
// 分区哈希,不把整份快照读进内存。
|
|
const takeoverBaselineMaxBlobBytes = 256 * 1024 * 1024
|
|
|
|
// loadTakeoverResumeFacts 收集"能否安全续写"的持久事实。
|
|
//
|
|
// 任何一项读不到都按**不成立**处理(fail closed):这条路径的用途是允许一次
|
|
// 恢复覆盖"已被更新版本顶替"的历史快照,证据缺一条就必须退回拒绝,不能把
|
|
// "查不到"当成"没问题"。
|
|
func loadTakeoverResumeFacts(ctx context.Context, store *postgresstore.Store,
|
|
archiveRoot string, job governance.LibraryJob, now time.Time) takeoverResumeFacts {
|
|
facts := takeoverResumeFacts{}
|
|
// 授权必须绑定本作业的请求摘要,且**签发于顶替之后**:顶替前签的旧授权
|
|
// 不知道这个快照已成历史,不能当作"恢复历史版本"的新意图。
|
|
if job.RequestDigest != "" && job.TargetSnapshotID != "" {
|
|
if grant, found, err := store.ActiveLibraryAuthorization(ctx, job.Scope,
|
|
job.TargetSnapshotID, job.RequestDigest, now); err == nil && found {
|
|
facts.AuthorizedAt = grant.CreatedAt
|
|
}
|
|
}
|
|
if job.SnapshotID != "" {
|
|
// 基线必须先有本作业自己的预备份登记,再证明清单字节仍是那个摘要。
|
|
if _, found, err := store.LibraryVersionByRef(ctx, job.Scope,
|
|
governance.LibraryVersionProductionPrebackup, job.ID, job.SnapshotID); err == nil && found {
|
|
facts.BaselineRecorded = true
|
|
}
|
|
facts.BaselineVerified = snapshotManifestIntact(archiveRoot, job.SnapshotID)
|
|
}
|
|
facts.MaintenanceOwned = takeoverWindowOwnedBy(ctx, store, job)
|
|
return facts
|
|
}
|
|
|
|
// takeoverWindowOwnedBy 要求 scope 与 instance 两把锁都由本作业持有且未释放,
|
|
// 且两行的目标指纹一致:只有本作业自己开的那扇窗口才允许续写,别人的窗口
|
|
// 即使作业号碰巧相同也进不来(Acquire* 的互斥语义在接管判定里同样成立)。
|
|
func takeoverWindowOwnedBy(ctx context.Context, store *postgresstore.Store, job governance.LibraryJob) bool {
|
|
scopeRow, err := store.LibraryMaintenanceOf(ctx, job.Scope)
|
|
if err != nil || scopeRow == nil || scopeRow.OwnerJobID != job.ID || scopeRow.ReleasedAt != nil {
|
|
return false
|
|
}
|
|
held, err := store.HeldInstanceMaintenance(ctx)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, row := range held {
|
|
if row.OwnerJobID == job.ID && row.OwnerScope == job.Scope && row.ReleasedAt == nil &&
|
|
scopeRow.TargetFingerprint != "" && row.TargetFingerprint == scopeRow.TargetFingerprint {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// snapshotManifestIntact 校验预备份清单未被改动:归档的清单文件名就是内容摘要,
|
|
// LoadManifest 会重算比对,字节被换过就失败。分区 blob 的完整复核留给恢复流程,
|
|
// 这里只回答"基线定义是否还是那一份"。
|
|
func snapshotManifestIntact(root, snapshotID string) bool {
|
|
if root == "" || snapshotID == "" {
|
|
return false
|
|
}
|
|
archive, err := librarysnapshot.Open(root, takeoverBaselineMaxBlobBytes)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer archive.Close()
|
|
if _, err := archive.LoadManifest(snapshotID); err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|