fix(library): 版本台账改按身份精确查询

写集合台账的取回走的是"按 scope 列最近 N 条再筛",而真实 Store 只接受 1..100,
于是真作业在预备份已落盘之后登记失败(library version limit out of range),
写集合引用没能持久化。假版本库没有这个上限,单测因此一路绿。

只把 200 改成 100 不够:历史超过窗口后,存在会被读成不存在(本机实测:把记录推到
250 条更新版本之后,接管/恢复就找不到它)。改为 Store.LibraryVersionByRef:
scope+kind+作业+快照 一条 SQL 精确命中;命中多行拒绝挑选,命中别的作业拒绝借用,
真的没有才返回 found=false。登记/接管/恢复三处共用它,公开分页上限保持 1..100 不变。
版本行解码抽成 scanLibraryVersions,窗口列表与精确查询共用一份。
This commit is contained in:
姚章浩
2026-10-30 02:32:23 +08:00
parent 27828ff21e
commit f0bd6fb0d2
2 changed files with 91 additions and 34 deletions
+29 -28
View File
@@ -16,38 +16,36 @@ import (
// 为什么不用新表:版本记录本来就绑定 scope + snapshot + 作业,且 id 由这三者
// 决定;把摘要挂上去,重启后按同一身份就能取回,不需要改 schema。
// libraryVersionLister 只暴露"按 scope 列版本"这一件事:装配真实 store 用,
// 单元测试用假实现,不必为验证摘要锚点去连治理库。
type libraryVersionLister interface {
LibraryVersions(ctx context.Context, scope string, limit int) ([]governance.LibraryVersion, error)
// libraryVersionLookup 只暴露"按身份取一条版本记录"这一件事。
//
// 为什么不是"按 scope 列表":列表是有排序窗口的(真实 Store 上限 100),
// 历史超过窗口后登记/接管/恢复会把"存在"读成"不存在"。身份查询必须是精确条件,
// 而 found=false 只表示"这个身份没有记录",冲突由 store 返回错误(不许挑一条)。
type libraryVersionLookup interface {
LibraryVersionByRef(ctx context.Context, scope string, kind governance.LibraryVersionKind,
sourceJobID, snapshotID string) (governance.LibraryVersion, bool, error)
}
// writeSetDigestOf 读某次作业/某份预备份登记的写集合摘要。
// 读不到就返回错误:缺失必须让调用方失败,不能降级成"没有记录就当没写过"。
func writeSetDigestOf(ctx context.Context, store libraryVersionLister, scope, jobID, snapshotID string) (string, error) {
versions, err := store.LibraryVersions(ctx, scope, 200)
func writeSetDigestOf(ctx context.Context, store libraryVersionLookup, scope, jobID, snapshotID string) (string, error) {
version, found, err := store.LibraryVersionByRef(ctx, scope, governance.LibraryVersionProductionPrebackup, jobID, snapshotID)
if err != nil {
return "", fmt.Errorf("读不到版本记录:%w", err)
}
for _, version := range versions {
if version.Kind != governance.LibraryVersionProductionPrebackup || version.SnapshotID != snapshotID {
continue
}
if version.SourceJobID != jobID {
continue
}
raw, _ := version.Evidence["writeset_digest"].(string)
if raw == "" {
return "", fmt.Errorf("预备份 %s 没有登记写集合摘要(作业 %s)", snapshotID, jobID)
}
return raw, nil
if !found {
return "", fmt.Errorf("找不到作业 %s 的预备份版本记录(快照 %s)", jobID, snapshotID)
}
return "", fmt.Errorf("找不到作业 %s 的预备份版本记录(快照 %s)", jobID, snapshotID)
raw, _ := version.Evidence["writeset_digest"].(string)
if raw == "" {
return "", fmt.Errorf("预备份 %s 没有登记写集合摘要(作业 %s)", snapshotID, jobID)
}
return raw, nil
}
// loadWriteSetForJob 按"作业 + 目标 + 预备份"取回写集合并逐项核对绑定。
// 任何一步不符都返回错误:恢复宁可保持维护,也不许用一份对不上的记录去删目标数据。
func loadWriteSetForJob(ctx context.Context, store libraryVersionLister, archiveRoot, scope, jobID, snapshotID string) (*libraryrelease.WriteSet, error) {
func loadWriteSetForJob(ctx context.Context, store libraryVersionLookup, archiveRoot, scope, jobID, snapshotID string) (*libraryrelease.WriteSet, error) {
digest, err := writeSetDigestOf(ctx, store, scope, jobID, snapshotID)
if err != nil {
return nil, err
@@ -90,19 +88,22 @@ func writeSetEvidence(existing map[string]any, set libraryrelease.WriteSet, mapp
// WriteSetPlanned 把写集合摘要登记进"这次作业的预备份版本"证据。
// 它在第一笔写入之前被调用;失败即中止(调用方据此不写目标)。
//
// 这里读的是"这一条预备份记录"(scope+kind+作业+快照),不是"最近 N 条":
// 现场正是历史上限把这条记录顶出窗口,才在预备份已经落盘之后登记失败。
// 记录不存在同样拒绝——把锚点写到一个不存在的证据行上,会让后续接管/恢复
// 读不到备份字节与索引映射,等于伪造一份台账。
func (r libraryStageRecorder) WriteSetPlanned(ctx context.Context, set libraryrelease.WriteSet) error {
versions, err := r.store.LibraryVersions(ctx, r.scope, 200)
version, found, err := r.store.LibraryVersionByRef(ctx, r.scope, governance.LibraryVersionProductionPrebackup,
r.jobID, set.BackupSnapshotID)
if err != nil {
return fmt.Errorf("读不到现有版本证据:%w", err)
}
var existing map[string]any
for _, version := range versions {
if version.Kind == governance.LibraryVersionProductionPrebackup && version.SnapshotID == set.BackupSnapshotID {
existing = version.Evidence
break
}
if !found {
return fmt.Errorf("预备份 %s 的版本证据不存在(作业 %s):拒绝把写集合挂到不存在的记录上",
set.BackupSnapshotID, r.jobID)
}
evidence := writeSetEvidence(existing, set, r.mappingDigest)
evidence := writeSetEvidence(version.Evidence, set, r.mappingDigest)
return r.store.RecordLibraryVersion(ctx, governance.LibraryVersion{
ID: governance.LibraryVersionID(governance.LibraryVersionProductionPrebackup, r.scope, set.BackupSnapshotID),
Scope: r.scope, Kind: governance.LibraryVersionProductionPrebackup, SnapshotID: set.BackupSnapshotID,