fix(library): 续作在事务内校验阶段并原子入队
build / build (push) Successful in 1m29s

This commit is contained in:
姚章浩
2026-11-01 22:05:02 +08:00
parent e292b6f674
commit a98828dc4a
6 changed files with 363 additions and 43 deletions
+26 -24
View File
@@ -271,41 +271,31 @@ func libraryJobResume(w http.ResponseWriter, r *http.Request, config Config) {
// id 落盘。另建 `-resume` 作业只能继承 operation,其它所有者仍指向旧作业:
// AcquireInstanceMaintenance 会拒绝它、journal 与预备份也查不到——那样"续作"只是
// 一个漂亮的新行,接不上原窗口。原作业 + 新 attempt 才是批准协议的形状。
// 已回滚并持有维护窗口的作业不能续作:继续跑等于丢开原 before-image 重来一遍,
// 正确出口是维护恢复(/library/maintenance/recover),它绑定原 window_job_id。
if source.Stage == "rollback" {
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_requires_recover",
"error": "作业已在回滚阶段失败并保留维护窗口,请走维护恢复而不是续作"})
return
}
// 写入已完成、只差登记版本的情况:续作只做幂等收尾,不重写生产。
body := map[string]any{"library_job_id": sourceID, "scope": source.Scope,
"target_snapshot_id": source.TargetSnapshotID}
if source.Stage == "finalize" && source.SnapshotID != "" {
body["finalize_only"] = true
}
payload, _ := json.Marshal(body)
queueID := governance.QueueJobIDFor(sourceID)
// 状态变更与队列 upsert 必须在同一事务里:否则入队失败会把作业留在 pending 却没有队列行。
// 阶段/状态/执行权与载荷全部在存储层的事务里校验并构造:handler 手里的 source 只用于
// 授权与闸口判断,不参与续作决策,避免用过期快照复活一个已经变化的作业。
resumer, ok := config.Store.(interface {
ResumeLibraryJob(ctx context.Context, jobID, queueID, queueType string, payload []byte, now time.Time) (governance.LibraryJob, bool, error)
ResumeLibraryJob(ctx context.Context, jobID, queueType string, now time.Time) (governance.LibraryJob, bool, error)
})
if !ok {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"error": "作业存储不支持续作"})
return
}
prepared, resumed, err := resumer.ResumeLibraryJob(r.Context(), sourceID, queueID, queueType, payload, now)
if errors.Is(err, postgres.ErrLibraryResumeBusy) {
prepared, resumed, err := resumer.ResumeLibraryJob(r.Context(), sourceID, queueType, now)
switch {
case errors.Is(err, postgres.ErrLibraryResumeBusy):
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_busy",
"error": "该作业已有执行在跑(租约仍有效),等它结束再续作"})
return
}
if err != nil {
case errors.Is(err, postgres.ErrLibraryResumeRequiresRecover):
// 窗口是否仍被持有由维护记录决定:已释放的回滚作业说"窗口已释放",不能一律宣称仍持有。
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_requires_recover",
"error": "作业停在回滚阶段,请走维护恢复而不是续作",
"maintenance_held": maintenanceHeldFor(r.Context(), config, source)})
return
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot resume job"})
return
}
if !resumed {
// 状态在检查之后被别的执行者推进(已接管/已完成):不许再塞一次。
case !resumed:
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_resume_state_changed",
"error": "作业已不在可续作状态(可能已被接管或完成)"})
return
@@ -313,3 +303,15 @@ func libraryJobResume(w http.ResponseWriter, r *http.Request, config Config) {
writeJSON(w, http.StatusAccepted, map[string]any{"job": prepared, "resumed_from": sourceID,
"resumed": true, "next_attempt_epoch": prepared.AttemptEpoch + 1})
}
// maintenanceHeldFor 只回答"这个作业现在是否仍持有维护窗口",用于给回滚作业的拒绝文案定性。
func maintenanceHeldFor(ctx context.Context, config Config, job governance.LibraryJob) bool {
reader, ok := config.Store.(interface {
LibraryMaintenanceOf(ctx context.Context, scope string) (*postgres.LibraryMaintenance, error)
})
if !ok {
return false
}
m, err := reader.LibraryMaintenanceOf(ctx, job.Scope)
return err == nil && m != nil && m.OwnerJobID == job.ID && m.ReleasedAt == nil
}