From f0bd6fb0d250aa713767638a973c3916c530008f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Fri, 30 Oct 2026 02:32:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(library):=20=E7=89=88=E6=9C=AC=E5=8F=B0?= =?UTF-8?q?=E8=B4=A6=E6=94=B9=E6=8C=89=E8=BA=AB=E4=BB=BD=E7=B2=BE=E7=A1=AE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 写集合台账的取回走的是"按 scope 列最近 N 条再筛",而真实 Store 只接受 1..100, 于是真作业在预备份已落盘之后登记失败(library version limit out of range), 写集合引用没能持久化。假版本库没有这个上限,单测因此一路绿。 只把 200 改成 100 不够:历史超过窗口后,存在会被读成不存在(本机实测:把记录推到 250 条更新版本之后,接管/恢复就找不到它)。改为 Store.LibraryVersionByRef: scope+kind+作业+快照 一条 SQL 精确命中;命中多行拒绝挑选,命中别的作业拒绝借用, 真的没有才返回 found=false。登记/接管/恢复三处共用它,公开分页上限保持 1..100 不变。 版本行解码抽成 scanLibraryVersions,窗口列表与精确查询共用一份。 --- cmd/worker/library_writeset.go | 57 +++++++++--------- internal/platform/postgres/library_jobs.go | 68 ++++++++++++++++++++-- 2 files changed, 91 insertions(+), 34 deletions(-) diff --git a/cmd/worker/library_writeset.go b/cmd/worker/library_writeset.go index f6c3013..b5dc6ad 100644 --- a/cmd/worker/library_writeset.go +++ b/cmd/worker/library_writeset.go @@ -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, diff --git a/internal/platform/postgres/library_jobs.go b/internal/platform/postgres/library_jobs.go index ad4ff36..b5d1e49 100644 --- a/internal/platform/postgres/library_jobs.go +++ b/internal/platform/postgres/library_jobs.go @@ -388,18 +388,61 @@ WHERE state IN ('pending','running') AND queue_job_id='' AND created_at < $2`, n return int(tag.RowsAffected()), nil } -func (s *Store) LibraryVersions(ctx context.Context, scope string, limit int) ([]governance.LibraryVersion, error) { - if limit < 1 || limit > 100 { - return nil, errors.New("library version limit out of range") +// LibraryVersionByRef 按 (scope, kind, 作业, 快照) 精确定位一条版本记录。 +// +// 为什么不能"取最近 N 条再在内存里筛":那是窗口,历史一旦超过窗口上限, +// 登记/接管/恢复就会把"存在"读成"不存在"——真实现场是写集合的引用无法登记, +// 报"读不到现有版本证据 / library version limit out of range"。 +// 身份查询必须走索引条件,不许依赖排序窗口(把上限从 200 改成 100 只是换个数)。 +// +// 同一 (scope, kind, snapshot) 只该有一行(id 由这三者决定,写入是 upsert)。 +// 命中多行、或命中的那行归别的作业所有,都说明台账不唯一:返回错误让调用方 +// 拒绝,不许挑一条"看起来对"的记录去恢复生产。 +func (s *Store) LibraryVersionByRef(ctx context.Context, scope string, kind governance.LibraryVersionKind, + sourceJobID, snapshotID string) (governance.LibraryVersion, bool, error) { + if scope == "" || kind == "" || sourceJobID == "" || snapshotID == "" { + return governance.LibraryVersion{}, false, errors.New("library version ref requires scope, kind, job and snapshot") } rows, err := s.db.Query(ctx, ` SELECT id, scope, kind, snapshot_id, version_label, source_job_id, evidence, created_by, created_at, superseded_at -FROM library_versions WHERE scope=$1 ORDER BY created_at DESC, id DESC LIMIT $2`, scope, limit) +FROM library_versions +WHERE scope=$1 AND kind=$2 AND source_job_id=$3 AND snapshot_id=$4 +ORDER BY created_at DESC, id DESC LIMIT 2`, scope, string(kind), sourceJobID, snapshotID) if err != nil { - return nil, fmt.Errorf("list library versions: %w", err) + return governance.LibraryVersion{}, false, fmt.Errorf("load library version by ref: %w", err) } + items, err := scanLibraryVersions(rows, 2) + if err != nil { + return governance.LibraryVersion{}, false, err + } + if len(items) > 1 { + return governance.LibraryVersion{}, false, fmt.Errorf("同一身份存在 %d 条版本记录(作业 %s,快照 %s):拒绝挑选", + len(items), sourceJobID, snapshotID) + } + if len(items) == 1 { + return items[0], true, nil + } + // 没命中时要区分"确实没有"与"这一行归别的作业":后者是身份冲突, + // 当成"没有记录"就会允许调用方覆盖别人的台账。 + var owner string + err = s.db.QueryRow(ctx, ` +SELECT source_job_id FROM library_versions WHERE scope=$1 AND kind=$2 AND snapshot_id=$3 +ORDER BY created_at DESC LIMIT 1`, scope, string(kind), snapshotID).Scan(&owner) + if errors.Is(err, pgx.ErrNoRows) { + return governance.LibraryVersion{}, false, nil + } + if err != nil { + return governance.LibraryVersion{}, false, fmt.Errorf("load library version owner: %w", err) + } + return governance.LibraryVersion{}, false, fmt.Errorf("版本记录(快照 %s)属于作业 %s,不是 %s:拒绝借用", + snapshotID, owner, sourceJobID) +} + +// scanLibraryVersions 是版本行唯一的扫描实现:两个查询(窗口列表与按身份精确查) +// 必须对同一种形状解码,否则"查得到"和"读得对"会分叉。 +func scanLibraryVersions(rows pgx.Rows, capacity int) ([]governance.LibraryVersion, error) { defer rows.Close() - result := make([]governance.LibraryVersion, 0, limit) + result := make([]governance.LibraryVersion, 0, capacity) for rows.Next() { var item governance.LibraryVersion var kind string @@ -417,6 +460,19 @@ FROM library_versions WHERE scope=$1 ORDER BY created_at DESC, id DESC LIMIT $2` return result, rows.Err() } +func (s *Store) LibraryVersions(ctx context.Context, scope string, limit int) ([]governance.LibraryVersion, error) { + if limit < 1 || limit > 100 { + return nil, errors.New("library version limit out of range") + } + rows, err := s.db.Query(ctx, ` +SELECT id, scope, kind, snapshot_id, version_label, source_job_id, evidence, created_by, created_at, superseded_at +FROM library_versions WHERE scope=$1 ORDER BY created_at DESC, id DESC LIMIT $2`, scope, limit) + if err != nil { + return nil, fmt.Errorf("list library versions: %w", err) + } + return scanLibraryVersions(rows, limit) +} + // CreateLibraryAuthorization 记录一次生产发布授权;同一 (scope,snapshot,digest) 只保留最新有效。 func (s *Store) CreateLibraryAuthorization(ctx context.Context, item governance.LibraryReleaseAuthorization) error { if item.ID == "" || item.Scope == "" || item.SnapshotID == "" || item.ParamsDigest == "" || item.ExpiresAt.IsZero() {