- 新增 library_versions:测试封存、生产生效、生产预备份三类分开登记,
生产生效版本唯一(旧版本必须先 supersede),测试快照不能顶替生产版本
- 新增 library_jobs + library_job_stages:业务作业身份、阶段、错误与恢复说明,
分项计数用 NULL 表示"总量未知";互斥与幂等由数据库唯一索引保证
- 新增 GET /api/v1/library/overview、/jobs、/jobs/{id};POST /syncs 闸口未齐
返回 503 缺项且不建作业
- 封存作业落库:先建业务作业再入队,worker 推进 export 阶段并登记 test_sealed
- 发布管理读真实状态:生产未知不再顶替、差异 unknown、启动同步按缺项禁用、
同步明细/四类分项来自持久阶段;阶段勾选只认当前作业证据
- 准备信息标注封存时间与范围,不声称实时测试库已就绪
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 整库版本与作业的领域类型:与队列(jobs)分开——队列负责租约与重试,
|
||||
// 这里负责业务身份、阶段与结果,重启后仍能如实展示。
|
||||
|
||||
type LibraryVersionKind string
|
||||
|
||||
const (
|
||||
// LibraryVersionTestSealed 是测试库封存产物:只证明一份完整测试版本被封存,
|
||||
// 不代表生产生效,也不代表做过生产恢复演练。
|
||||
LibraryVersionTestSealed LibraryVersionKind = "test_sealed"
|
||||
// LibraryVersionProductionEffective 是生产当前权威版本:只有生产验证通过并
|
||||
// 记录证据后才能写入,测试快照不能顶替它。
|
||||
LibraryVersionProductionEffective LibraryVersionKind = "production_effective"
|
||||
// LibraryVersionProductionPrebackup 是每次生产写入前的独立预备份。
|
||||
LibraryVersionProductionPrebackup LibraryVersionKind = "production_prebackup"
|
||||
)
|
||||
|
||||
type LibraryJobKind string
|
||||
|
||||
const (
|
||||
LibraryJobSeal LibraryJobKind = "seal"
|
||||
LibraryJobSync LibraryJobKind = "sync"
|
||||
)
|
||||
|
||||
type LibraryJobState string
|
||||
|
||||
const (
|
||||
LibraryJobPending LibraryJobState = "pending"
|
||||
LibraryJobRunning LibraryJobState = "running"
|
||||
LibraryJobSucceeded LibraryJobState = "succeeded"
|
||||
LibraryJobFailed LibraryJobState = "failed"
|
||||
LibraryJobCancelled LibraryJobState = "cancelled"
|
||||
)
|
||||
|
||||
// ErrLibraryJobActive:同一目标同一类作业已有在飞作业(数据库级互斥)。
|
||||
var ErrLibraryJobActive = errors.New("library job already active for this target")
|
||||
|
||||
// ErrLibraryJobIdempotencyConflict:同一幂等键对应了不同的请求体。
|
||||
var ErrLibraryJobIdempotencyConflict = errors.New("library job idempotency key reused with a different request")
|
||||
|
||||
type LibraryStage struct {
|
||||
Stage string `json:"stage"`
|
||||
State string `json:"state"`
|
||||
// Total/Completed/BytesTotal/BytesDone 为 nil 表示该维度没有可靠数据,
|
||||
// 界面必须显示"未知",不得当 0 或据此算百分比。
|
||||
Total *int64 `json:"total"`
|
||||
Completed *int64 `json:"completed"`
|
||||
BytesTotal *int64 `json:"bytes_total"`
|
||||
BytesDone *int64 `json:"bytes_done"`
|
||||
Detail string `json:"detail"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type LibraryJob struct {
|
||||
ID string `json:"id"`
|
||||
Scope string `json:"scope"`
|
||||
Kind LibraryJobKind `json:"kind"`
|
||||
State LibraryJobState `json:"state"`
|
||||
Stage string `json:"stage"`
|
||||
IdempotencyKey string `json:"-"`
|
||||
RequestDigest string `json:"request_digest"`
|
||||
QueueJobID string `json:"queue_job_id"`
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
TargetSnapshotID string `json:"target_snapshot_id"`
|
||||
Actor string `json:"actor"`
|
||||
Error string `json:"error"`
|
||||
Recovery string `json:"recovery"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
FinishedAt *time.Time `json:"finished_at"`
|
||||
Stages []LibraryStage `json:"stages"`
|
||||
}
|
||||
|
||||
type LibraryVersion struct {
|
||||
ID string `json:"id"`
|
||||
Scope string `json:"scope"`
|
||||
Kind LibraryVersionKind `json:"kind"`
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
VersionLabel string `json:"version_label"`
|
||||
SourceJobID string `json:"source_job_id"`
|
||||
Evidence map[string]any `json:"evidence"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
SupersededAt *time.Time `json:"superseded_at"`
|
||||
}
|
||||
|
||||
// LibraryRequestDigest 绑定一次请求的确切含义:同键不同体必须被拒绝,而不是
|
||||
// 复用旧作业。只覆盖会影响执行结果的字段;actor 不参与摘要。
|
||||
func LibraryRequestDigest(scope string, kind LibraryJobKind, payload map[string]any) (string, error) {
|
||||
canonical := map[string]any{"scope": scope, "kind": string(kind), "payload": payload}
|
||||
raw, err := json.Marshal(canonical)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := sha256.Sum256(raw)
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
// SyncStages 是生产同步作业的固定阶段顺序:先备份并核验恢复条件,再写生产,
|
||||
// 最后验证;失败时走恢复而不是把作业标成功。
|
||||
func SyncStages() []string {
|
||||
return []string{"prebackup", "database", "resources", "index", "wiki", "verify", "rollback"}
|
||||
}
|
||||
|
||||
// SealStages 是封存作业的阶段:导出是原子操作,没有分项进度。
|
||||
func SealStages() []string {
|
||||
return []string{"export"}
|
||||
}
|
||||
Reference in New Issue
Block a user