feat(library): 整库发布身份、写集合与收尾边界
build / build (push) Successful in 1m45s

执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、
租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
This commit is contained in:
姚章浩
2026-11-01 11:35:10 +08:00
parent 5afd8c0140
commit 8d838d9cb4
82 changed files with 4809 additions and 551 deletions
+62 -14
View File
@@ -43,6 +43,40 @@ type contentTrackedTarget struct {
inner libraryrelease.Target
jobID string
onApply func()
// revalidateGrant 由 runLibraryRelease 无条件接线:每笔副作用前按治理库里的
// 授权行重新判定(撤销/过期即停)。测试夹具可以显式传 nil 表示"这份夹具没有
// 持久授权行",但真实发布路径不允许为空 —— 那里的空值等于回到只信内存快照。
revalidateGrant func(context.Context, libraryrelease.Grant) error
}
// revalidateLibraryGrant 是"授权没有被中途撤销/过期"的唯一判定实现:
// 真实发布链路与测试都走它,避免测试自己写一份看起来相同的查询。
func revalidateLibraryGrant(store *postgresstore.Store) func(context.Context, libraryrelease.Grant) error {
return func(ctx context.Context, current libraryrelease.Grant) error {
// 权威来源是治理库里的授权行:参数摘要一变就查不到,撤销/过期立即消失。
found, ok, err := store.ActiveLibraryAuthorization(ctx, current.Target, current.SnapshotID,
current.ParamsDigest, time.Now().UTC())
if err != nil {
return fmt.Errorf("读取持久授权失败:%w", err)
}
if !ok {
return errors.New("当前授权已撤销或已过期")
}
if found.AuthorizedBy != current.AuthorizedBy {
return errors.New("持久授权与本次执行身份不一致")
}
return nil
}
}
func (t *contentTrackedTarget) RevalidateGrant(ctx context.Context, grant libraryrelease.Grant) error {
if t.revalidateGrant != nil {
return t.revalidateGrant(ctx, grant)
}
if inner, ok := t.inner.(libraryrelease.GrantRevalidator); ok {
return inner.RevalidateGrant(ctx, grant)
}
return nil
}
func (t *contentTrackedTarget) Current(ctx context.Context) (libraryrelease.CurrentState, error) {
@@ -91,6 +125,20 @@ func (t *contentTrackedTarget) PlanWriteSet(ctx context.Context, plan libraryrel
return set, err
}
func (t *contentTrackedTarget) AdoptWriteSet(set libraryrelease.WriteSet) error {
adopter, ok := t.inner.(libraryrelease.WriteSetAdopter)
if !ok {
return errors.New("目标实现不支持接管已持久化写集合,拒绝重建计划")
}
if set.JobID != t.jobID {
return errors.New("写集合与 worker 作业身份不符")
}
return adopter.AdoptWriteSet(set)
}
var _ libraryrelease.WriteSetPlanner = (*contentTrackedTarget)(nil)
var _ libraryrelease.WriteSetAdopter = (*contentTrackedTarget)(nil)
// libraryReleasePayload 是同步与恢复共用的队列载荷形状。
type libraryReleasePayload struct {
LibraryJobID string `json:"library_job_id"`
@@ -117,19 +165,13 @@ func runLibraryRelease(ctx context.Context, base *postgresstore.Store, config ap
if _, admitErr := admitLibraryJob(ctx, base, job, payload.LibraryJobID, payload.Scope, kind, true); admitErr != nil {
return admitErr
}
// 执行代次:这一次投递是一个确定的执行者身份。所有治理写入都走 writer
// (同名方法带 epoch 守卫),失租的旧执行从此写不进业务状态/维护锁(F13)。
writer, attemptErr := beginLibraryWriter(ctx, base, payload.LibraryJobID, libraryQueueOwner(config.WorkerID, job, payload.LibraryJobID))
if attemptErr != nil {
return attemptErr
}
defer func() {
if err != nil {
writer.finish(context.WithoutCancel(ctx), governance.LibraryAttemptFailed, err.Error())
return
}
writer.finish(context.WithoutCancel(ctx), governance.LibraryAttemptCompleted, "")
}()
return runOwnedLibraryAttempt(ctx, base, config, job, payload.LibraryJobID, func(runCtx context.Context, writer *libraryAttemptWriter) error {
return runLibraryReleaseAttempt(runCtx, writer, config, kind, payload)
})
}
func runLibraryReleaseAttempt(ctx context.Context, writer *libraryAttemptWriter, config appconfig.Config,
kind governance.LibraryJobKind, payload libraryReleasePayload) (err error) {
store := writer
now := time.Now().UTC()
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobRunning, "prebackup", "", "", "", "", now)
@@ -320,7 +362,8 @@ func runLibraryRelease(ctx context.Context, base *postgresstore.Store, config ap
}
pending := successBoundary{}
tracked := &contentTrackedTarget{inner: target, jobID: payload.LibraryJobID,
onApply: func() { contentState = contentHalfWritten }}
onApply: func() { contentState = contentHalfWritten },
revalidateGrant: revalidateLibraryGrant(store.Store)}
allowRestore := func(error) bool {
return contentState == contentUntouched || contentState == contentSucceeded || contentState == contentRolledBack
}
@@ -549,6 +592,11 @@ func runLibraryRelease(ctx context.Context, base *postgresstore.Store, config ap
// 已经持久化预备份的重新排队(同一个业务作业 id,阶段与备份都接着用),
// 连预备份都没完成的才判失败——那种情况没有可回滚的基线,不能盲写。
func resumeInterruptedLibraryJobs(ctx context.Context, store libraryResumeStore, config appconfig.Config) (int, int, error) {
if recoveries, ok := store.(interface{ ResumeExpiredLibraryRecoveryQueues(context.Context) error }); ok {
if err := recoveries.ResumeExpiredLibraryRecoveryQueues(ctx); err != nil {
return 0, 0, err
}
}
interrupted, err := store.InterruptedLibraryJobs(ctx)
if err != nil {
return 0, 0, err