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:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user