ReconcileLibraryJobs 不再把 completed capture queue 当成不存在,保留待核对 pending 封存作业;failed/cancelled/missing 仍按真实队列状态处置。管理员核对 入口只允许精确的启动误判形态,并要求 completed queue、export succeeded、 唯一 test_sealed 版本、来源身份与完整归档复核。新增 library_job_repairs 不可变审计表,在同一事务内 CAS 保存原 state/stage/error/recovery/finished_at 及本次 actor/原因/证据后修复;真实失败/取消不开放。 验证:真实隔离 PostgreSQL 启动收敛 RED/GREEN、启动→handler 纵向 pending 与 系统误判 failed、审计保留/幂等、负例;真实 Go 全量与 make verify 17 项通过。
This commit is contained in:
@@ -346,6 +346,73 @@ WHERE id=$1 AND scope=$2 AND kind='seal' AND queue_job_id=$3
|
||||
return tag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
// ReconcileMisclassifiedLibraryCapture repairs ONLY the known startup
|
||||
// misclassification: a failed seal whose queue actually completed, with no
|
||||
// stage/snapshot and the exact legacy error/recovery. The original fields are
|
||||
// copied to the immutable audit row and the job update is CAS-bound to them;
|
||||
// any other failed/cancelled row is rejected.
|
||||
func (s *Store) ReconcileMisclassifiedLibraryCapture(ctx context.Context, jobID, scope, queueJobID, snapshotID,
|
||||
expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time,
|
||||
auditID, actor, reason string, evidence map[string]any, now time.Time) (bool, error) {
|
||||
if jobID == "" || scope == "" || queueJobID == "" || snapshotID == "" || auditID == "" {
|
||||
return false, errors.New("misclassification repair requires job/scope/queue/snapshot/audit")
|
||||
}
|
||||
rawEvidence, err := json.Marshal(evidence)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("encode repair evidence: %w", err)
|
||||
}
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("begin repair: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
var state, stage, errText, recovery, currentSnapshot string
|
||||
var finished *time.Time
|
||||
if err := tx.QueryRow(ctx, `
|
||||
SELECT state, stage, error, recovery, snapshot_id, finished_at
|
||||
FROM library_jobs WHERE id=$1 AND scope=$2 AND kind='seal' AND queue_job_id=$3
|
||||
FOR UPDATE`, jobID, scope, queueJobID).Scan(&state, &stage, &errText, &recovery, ¤tSnapshot, &finished); err != nil {
|
||||
return false, fmt.Errorf("read repair target: %w", err)
|
||||
}
|
||||
if state != "failed" || stage != expectedStage || errText != expectedError ||
|
||||
recovery != expectedRecovery || currentSnapshot != "" ||
|
||||
(finished == nil) != (expectedFinished == nil) ||
|
||||
(finished != nil && expectedFinished != nil && !finished.Equal(*expectedFinished)) {
|
||||
return false, nil
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE library_jobs
|
||||
SET state='succeeded', stage='export', error='', recovery='', snapshot_id=$3,
|
||||
updated_at=$4, finished_at=$4
|
||||
WHERE id=$1 AND scope=$2 AND kind='seal' AND queue_job_id=$5
|
||||
AND state='failed' AND stage=$6 AND error=$7 AND recovery=$8 AND snapshot_id=''
|
||||
AND finished_at IS NOT DISTINCT FROM $9`,
|
||||
jobID, scope, snapshotID, now.UTC(), queueJobID, expectedStage, expectedError, expectedRecovery, expectedFinished)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("repair misclassified seal: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return false, nil
|
||||
}
|
||||
tag, err = tx.Exec(ctx, `
|
||||
INSERT INTO library_job_repairs
|
||||
(id, job_id, scope, kind, queue_job_id, snapshot_id, original_state, original_stage,
|
||||
original_error, original_recovery, original_finished_at, reason, actor, evidence, created_at)
|
||||
VALUES ($1,$2,$3,'seal',$4,$5,'failed',$6,$7,$8,$9,$10,$11,$12,$13)
|
||||
ON CONFLICT (id) DO NOTHING`, auditID, jobID, scope, queueJobID, snapshotID,
|
||||
expectedStage, expectedError, expectedRecovery, expectedFinished, reason, actor, rawEvidence, now.UTC())
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("write repair audit: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
return false, errors.New("repair audit already exists")
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return false, fmt.Errorf("commit repair: %w", err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// RecordLibraryVersion 写入一条**不可变版本事件**,并在需要时推进 current 指针。
|
||||
//
|
||||
// 与旧实现的区别(F02/F09 的根因):
|
||||
@@ -724,7 +791,7 @@ func (s *Store) ReconcileLibraryJobs(ctx context.Context, now time.Time, kinds [
|
||||
if len(kinds) == 0 {
|
||||
return 0, errors.New("reconcile library jobs requires the kinds this executor owns")
|
||||
}
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
tag, err := s.db.Exec(ctx, `
|
||||
UPDATE library_jobs AS lj
|
||||
SET state='failed', error='队列中已无对应作业,业务作业未收到结果',
|
||||
recovery=CASE WHEN lj.kind IN ('sync','restore','recover','baseline')
|
||||
@@ -737,12 +804,15 @@ WHERE lj.state IN ('pending','running')
|
||||
AND lj.queue_job_id <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM jobs AS j
|
||||
WHERE j.id = lj.queue_job_id AND j.state IN ('pending','running','retry')
|
||||
)`, now.UTC(), kinds, s.libraryTargetID); err != nil {
|
||||
WHERE j.id = lj.queue_job_id
|
||||
AND j.state IN ('pending','running','retry','completed')
|
||||
)`, now.UTC(), kinds, s.libraryTargetID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reconcile library jobs: %w", err)
|
||||
}
|
||||
first := int(tag.RowsAffected())
|
||||
// 建了业务作业但队列投递没完成(queue_job_id 为空)超过 5 分钟,也是孤儿。
|
||||
tag, err := s.db.Exec(ctx, `
|
||||
tag, err = s.db.Exec(ctx, `
|
||||
UPDATE library_jobs
|
||||
SET state='failed', error='队列投递未完成,作业从未开始', recovery='无需恢复来源;可直接重发', updated_at=$1, finished_at=$1
|
||||
WHERE state IN ('pending','running') AND queue_job_id='' AND kind = ANY($3) AND (kind='seal' OR target_id=$4) AND created_at < $2`,
|
||||
@@ -750,7 +820,7 @@ WHERE state IN ('pending','running') AND queue_job_id='' AND kind = ANY($3) AND
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reconcile unattached library jobs: %w", err)
|
||||
}
|
||||
return int(tag.RowsAffected()), nil
|
||||
return first + int(tag.RowsAffected()), nil
|
||||
}
|
||||
|
||||
// LibraryVersionByRef 按 (scope, kind, 作业, 快照) 精确定位一条版本记录。
|
||||
|
||||
Reference in New Issue
Block a user