执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
@@ -2,12 +2,9 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -360,6 +357,7 @@ type libraryVersionRef struct {
|
||||
}
|
||||
|
||||
type libraryJobStore interface {
|
||||
CreateLibraryDispatch(context.Context, governance.LibraryJob, []string, bool, time.Time) (governance.LibraryJob, bool, error)
|
||||
CreateLibraryJob(ctx context.Context, job governance.LibraryJob, stages []string, now time.Time) (governance.LibraryJob, bool, error)
|
||||
// EnsureLibraryOperation 幂等登记"一次明确意图":同键+同摘要 ⇒ 同一次意图(客户端重放),
|
||||
// 换键 ⇒ 新意图。作业是意图的执行代次,不能反过来用"历史成功作业"回答新意图(F01)。
|
||||
@@ -470,8 +468,7 @@ func libraryMaintenanceRecoverHandler(w http.ResponseWriter, r *http.Request, co
|
||||
}
|
||||
// 队列作业 id 与幂等键都按窗口固定:重复点击只会得到同一个恢复作业。
|
||||
// 队列 id 有长度上限(36),窗口作业 id 比它还长,所以按窗口摘要取短 id。
|
||||
sum := sha256.Sum256([]byte(held.OwnerJobID))
|
||||
queueID := "lrec-" + hex.EncodeToString(sum[:14])
|
||||
queueID := governance.LibraryRecoveryQueueID(held.OwnerJobID)
|
||||
queueJob, err := config.Store.Enqueue(r.Context(), jobs.EnqueueRequest{
|
||||
ID: queueID, Type: libraryRecoverJobType, Payload: payload,
|
||||
AvailableAt: now, MaxAttempts: 1, IdempotencyKey: "library.recover:" + held.OwnerJobID,
|
||||
@@ -727,7 +724,11 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
return
|
||||
}
|
||||
if archiveReadable {
|
||||
overview.TestSealed, overview.TestSealedReason = testSealedOf(versions, byID)
|
||||
var candidateErr error
|
||||
overview.TestSealed, candidateErr = currentSourceCandidate(r.Context(), config, scope)
|
||||
if candidateErr != nil {
|
||||
overview.TestSealedReason = candidateErr.Error()
|
||||
}
|
||||
}
|
||||
overview.Production = libraryKnownView{State: "unknown", Reason: "还没有生产验证通过的生效版本记录"}
|
||||
overview.Prebackup = libraryKnownView{State: "unknown", Reason: "这个范围还没有任何生产预备份"}
|
||||
@@ -873,6 +874,7 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
// OperationKey 是客户端重放键:同一次意图重发要命中同一个 operation。
|
||||
// 不传就是一次新意图(同一个快照再发布一次也必须真的执行)。
|
||||
OperationKey string `json:"operation_key"`
|
||||
SourceID string `json:"source_id"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&request); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
@@ -939,6 +941,11 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
return
|
||||
}
|
||||
|
||||
candidate, candidateErr := currentSourceCandidate(r.Context(), config, request.Scope)
|
||||
if candidateErr != nil || request.SourceID == "" || candidate == nil || candidate.SourceID != request.SourceID || candidate.SnapshotID != request.Target {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_source_candidate_changed", "error": "来源候选已变化或身份未绑定,请刷新后重试", "detail": errString(candidateErr)})
|
||||
return
|
||||
}
|
||||
// 走到这里说明闸口与证据都齐了才真正建作业;互斥与幂等由数据库索引保证。
|
||||
digest, err := governance.LibraryRequestDigest(request.Scope, governance.LibraryJobSync,
|
||||
map[string]any{"target_snapshot_id": request.Target})
|
||||
@@ -965,11 +972,11 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot register library intent"})
|
||||
return
|
||||
}
|
||||
job, reused, err := store.CreateLibraryJob(r.Context(), governance.LibraryJob{
|
||||
job, reused, err := store.CreateLibraryDispatch(r.Context(), governance.LibraryJob{
|
||||
ID: jobID, Scope: request.Scope, Kind: governance.LibraryJobSync,
|
||||
IdempotencyKey: intent.JobKey, RequestDigest: digest, OperationID: intent.Operation.ID,
|
||||
TargetSnapshotID: request.Target, Actor: actor,
|
||||
}, governance.SyncStages(), now)
|
||||
}, governance.SyncStages(), config.LibraryHandoffEnabled, now)
|
||||
if errors.Is(err, governance.ErrLibraryJobActive) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_sync_active",
|
||||
"error": "该目标已有在飞的生产写作业(同步与恢复互斥)"})
|
||||
@@ -986,13 +993,13 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
// 上一次同意图请求失败/被取消后,幂等键不能再把新请求塞回那个死作业里:
|
||||
// 在**同一个 operation** 下开一个新的执行作业(重试代次),旧作业保留为审计记录。
|
||||
if reused && (job.State == governance.LibraryJobFailed || job.State == governance.LibraryJobCancelled) {
|
||||
job, reused, err = store.CreateLibraryJob(r.Context(), governance.LibraryJob{
|
||||
job, reused, err = store.CreateLibraryDispatch(r.Context(), governance.LibraryJob{
|
||||
ID: fmt.Sprintf("libsync-%s-%d", request.Scope, time.Now().UTC().UnixNano()), Scope: request.Scope,
|
||||
Kind: governance.LibraryJobSync,
|
||||
IdempotencyKey: intent.JobKey + ":retry:" + now.Format("20060102T150405.000000000"),
|
||||
RequestDigest: digest, OperationID: intent.Operation.ID,
|
||||
TargetSnapshotID: request.Target, Actor: actor,
|
||||
}, governance.SyncStages(), now)
|
||||
}, governance.SyncStages(), config.LibraryHandoffEnabled, now)
|
||||
if err != nil || reused {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_sync_retry_conflict",
|
||||
"error": "无法为重试创建新作业", "detail": errString(err)})
|
||||
@@ -1003,27 +1010,6 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"job": job, "reused": true})
|
||||
return
|
||||
}
|
||||
// 业务作业先落库,再投递队列:投递失败就把作业标失败,不留下"看起来在跑"的空作业。
|
||||
queueID, queueType, payload, err := libraryQueueRequest(config.LibraryHandoffEnabled, job.ID,
|
||||
request.Scope, request.Target, librarySyncJobType)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot build sync payload"})
|
||||
return
|
||||
}
|
||||
queueJob, err := config.Store.Enqueue(r.Context(), jobs.EnqueueRequest{
|
||||
ID: queueID, Type: queueType, Payload: payload, AvailableAt: now,
|
||||
MaxAttempts: 1, IdempotencyKey: queueType + ".queue:" + job.ID,
|
||||
}, now)
|
||||
if err != nil || queueJob == nil {
|
||||
log.Printf("library sync enqueue failed job=%s: %v", job.ID, err)
|
||||
_ = store.CancelLibraryJob(r.Context(), job.ID, "队列投递失败,未开始同步", now)
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot enqueue sync job"})
|
||||
return
|
||||
}
|
||||
if attachErr := store.AttachLibraryQueueJob(r.Context(), job.ID, queueJob.ID, now); attachErr != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot attach sync job"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"job": job, "reused": false})
|
||||
}
|
||||
|
||||
@@ -1033,17 +1019,5 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
// 由它在成功后再入 releaseType;搬运未接线时保持原样。两种情况下 releaseType
|
||||
// 作业的 ID 都是 QueueJobIDFor(jobID),所以接管/续作的既有路径不受影响。
|
||||
func libraryQueueRequest(handoff bool, jobID, scope, snapshotID, releaseType string) (string, string, []byte, error) {
|
||||
payload := map[string]string{"library_job_id": jobID, "scope": scope, "target_snapshot_id": snapshotID}
|
||||
queueID := governance.QueueJobIDFor(jobID)
|
||||
queueType := releaseType
|
||||
if handoff {
|
||||
payload["next_type"] = releaseType
|
||||
queueID = governance.LibraryHandoffQueueJobID(jobID)
|
||||
queueType = libraryHandoffJobType
|
||||
}
|
||||
raw, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", "", nil, err
|
||||
}
|
||||
return queueID, queueType, raw, nil
|
||||
return governance.LibraryQueueRequest(handoff, jobID, scope, snapshotID, releaseType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user