fix(library): 守卫按行取作业身份、同作业多代次、原作业续作与执行域准入
build / build (push) Successful in 1m27s

R1 维护/发布守卫硬编码 j.id=$1:维护类入口的 $1 是 scope/instance,作业号在 $2,
导致 update/release/ingress 六个入口正常路径全部匹配不到作业。改为按被更新行自己的
owner_job_id 生成条件,并同时要求该代次在 library_attempts 里仍活跃。

R2 认领只改 jobs.attempt_epoch 不改 attempts.epoch,第二次接管即错位;唯一索引
(job_id) 与"每次接管新 attempt 且历史不可变"矛盾。改为同作业多代次:锁作业行与
operation 行、同 owner 幂等续租、活跃未过期者让路、过期/本作业旧代次标 abandoned、
新代次 epoch=max+1,并清同 operation 其它作业行租约。迟到 write/renew/finish 一律
ErrLibraryEpochStale(finish 补了"只能在活跃态进来一次")。

R3 续作不再新建 -resume 作业:PrepareLibraryJobResume 让原业务作业回 pending 并显式
结束上一代,维护窗口/journal/预备份/写集合仍按原作业 id 归属,代次由 worker 起手递增。

R4 执行域准入改为注册 handler 前读事实:目标实况(KB/租户/schema/索引)、旧 release
路由绑定的 KB、未释放维护窗口持有者;未知或重叠即拒绝执行(不做维护动作、不停既有服务)。

同时自纠 5 项设计偏离:current 次序键取意图创建时间(旧意图迟到事实被拒)、首次锚点
改事务 FOR UPDATE + 回读且 NULL≠0、版本事件冲突只做加法且 xmax=0 才算新事实、
恢复作为新 kind=recover 意图推进 current、隔离测试加测试库护栏并清理残留 schema。

测试:真实 PG(kg-pgtest-writeset,一用例一 schema)新增 8 条集成用例覆盖上述正负例;
go test ./... 全绿(50 包)、go vet/build rc=0、web tsc + vitest 485 通过、make verify 16 项通过。
This commit is contained in:
姚章浩
2026-10-30 17:20:40 +08:00
parent afe9affc2a
commit a7d643635a
26 changed files with 1551 additions and 215 deletions
+27 -22
View File
@@ -17,7 +17,7 @@ import (
// upsert 并会改写 source_job_id,所以"生产上有这个快照、但它不是本作业发的"是存储层
// 才能造出的真实形状。内存夹具只能造出调用方相信的那个版本。
func TestTakeoverRefusesSameSnapshotRegisteredByAnotherJob(t *testing.T) {
store, _ := openLedgerStore(t)
store, pool := openLedgerStore(t)
ctx := context.Background()
now := time.Now().UTC()
const scope = "internal"
@@ -27,15 +27,10 @@ func TestTakeoverRefusesSameSnapshotRegisteredByAnotherJob(t *testing.T) {
record := func(source string, created time.Time) {
t.Helper()
if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{
ID: governance.LibraryVersionID(governance.LibraryVersionProductionEffective, scope, shared),
Scope: scope, Kind: governance.LibraryVersionProductionEffective, SnapshotID: shared,
SourceJobID: source, CreatedBy: "worker",
Evidence: map[string]any{"action": "sync", "index_mapping_sha256": "mapping-live"},
CreatedAt: created,
}); err != nil {
t.Fatalf("record effective for %s: %v", source, err)
}
opID := testLibraryOperation(t, store, scope, governance.LibraryJobSync, "shared:"+source, created)
recordVersionEvent(t, store, scope, governance.LibraryVersionProductionEffective, shared,
source, opID, created,
map[string]any{"action": "sync", "index_mapping_sha256": "mapping-live"})
}
record(otherJob, now.Add(-time.Hour))
@@ -56,14 +51,25 @@ func TestTakeoverRefusesSameSnapshotRegisteredByAnotherJob(t *testing.T) {
got, takeoverForeignEffective)
}
// 本作业自己登记过同一快照(upsert 把归属改写回来)⇒ 只差收尾。
// 另一条**独立事实**:本作业用自己的意图登记同一快照(归属不会被改写,
// 而是新增一条属于本作业的事件,current 指向最新的那条)。
record(thisJob, now)
facts = loadLibraryVersionFacts(ctx, store, scope, shared)
if facts.Err != nil {
t.Fatalf("read facts after own record: %v", facts.Err)
}
if facts.Current.SourceJobID != thisJob {
t.Fatalf("upsert 没有把归属改成本作业:%+v", facts.Current)
t.Fatalf("本作业的新意图没有成为当前生效事实:%+v", facts.Current)
}
// 旧事件必须原样保留:新意图不许改写别人的登记(F02 的反面证据)。
var otherOwner string
if err := pool.QueryRow(ctx,
`SELECT source_job_id FROM library_versions WHERE scope=$1 AND kind='production_effective'
AND snapshot_id=$2 ORDER BY created_at ASC LIMIT 1`, scope, shared).Scan(&otherOwner); err != nil {
t.Fatalf("read original event: %v", err)
}
if otherOwner != otherJob {
t.Fatalf("别人的历史事件被改写成 %q(必须保持 %q)", otherOwner, otherJob)
}
if got := classifyTakeover(job, facts, takeoverResumeFacts{}); got != takeoverPendingFinal {
t.Fatalf("本作业登记的同一快照分类 = %q, want %q", got, takeoverPendingFinal)
@@ -147,17 +153,15 @@ func TestTakeoverClassificationUsesExactCurrentVersionBeyondHistoryWindow(t *tes
job := governance.LibraryJob{ID: jobID, Scope: scope, Kind: governance.LibraryJobSync,
TargetSnapshotID: targetSnapshot, SnapshotID: "snap-prebackup"}
// 每次登记都是一个**独立意图**(不同 snapshot/job 就是不同 operation):
// current 的次序键取自意图创建时间,测试要证明的正是"按意图时间单调"。
recordEffective := func(sourceJob, snapshot string, created time.Time) {
t.Helper()
if err := store.RecordLibraryVersion(ctx, governance.LibraryVersion{
ID: governance.LibraryVersionID(governance.LibraryVersionProductionEffective, scope, snapshot),
Scope: scope, Kind: governance.LibraryVersionProductionEffective, SnapshotID: snapshot,
SourceJobID: sourceJob, CreatedBy: "worker",
Evidence: map[string]any{"action": "sync", "index_mapping_sha256": "mapping-live"},
CreatedAt: created,
}); err != nil {
t.Fatalf("record effective %s: %v", snapshot, err)
}
opID := testLibraryOperation(t, store, scope, governance.LibraryJobSync,
string(governance.LibraryJobSync)+":"+snapshot+":"+sourceJob, created)
recordVersionEvent(t, store, scope, governance.LibraryVersionProductionEffective, snapshot,
sourceJob, opID, created,
map[string]any{"action": "sync", "index_mapping_sha256": "mapping-live"})
}
// 生效版本登记在 48 小时前,然后 250 条更新的历史记录把它挤出任何窗口。
@@ -218,7 +222,8 @@ func TestTakeoverClassificationUsesExactCurrentVersionBeyondHistoryWindow(t *tes
if state := classifyBaselineVersions(current, hasCurrent, "libbase-internal-x", "snap-newer"); state.Own || state.Foreign == nil {
t.Fatalf("别人的生效版本必须挡住基线:%+v", state)
}
recordEffective("libbase-internal-y", "snap-baseline", now)
// 新的意图必须真的更新:晚一分钟,而不是与上一条同刻(同刻只能靠 id 字典序,不是语义)。
recordEffective("libbase-internal-y", "snap-baseline", now.Add(time.Minute))
current, hasCurrent, err = store.LibraryCurrentVersion(ctx, scope)
if err != nil || !hasCurrent {
t.Fatalf("再读当前生效版本失败:found=%v err=%v", hasCurrent, err)