fix(library): 收紧 DS01 派发租约/投递/治理身份与绑定冲突
F2.a:Acquire/Confirm 在取得 effect 行锁后用数据库时钟重核 job/attempt/ 恢复投递租约,等锁跨过到期零许可且状态保持 prepared。 F2.b:同一 CAS 事务锁 recovery queue 与 operation,复核 window/scope、 idempotency、owner/attempt/lease,并按原 window 锚点重算恢复请求摘要。 F3.identity:激活证据 v3 携带 PG 集群 system_identifier + 库 OID、完整构建 SHA 与协议批次;激活与使用点都现场重读并拒绝同名异域/副本/unknown/不兼容 SHA。 F4.binding:区分绑定冲突与仅禁止重发的 Held,准入/确认/续作/收尾在所有状态 拒绝冲突,合法 dispatching + 精确 marker 仍可确认。
This commit is contained in:
@@ -103,11 +103,17 @@ WHERE target_identity=$1 AND protocol_version=1 AND revoked_at IS NULL`, targetI
|
||||
// lockRestoreDispatchActivation 在**同一个 CAS 事务**里锁激活行。撤销是同一行的
|
||||
// UPDATE:撤销先完成 ⇒ 这里看到 revoked_at;CAS 先取锁 ⇒ 撤销必须等 CAS 提交,
|
||||
// 之后按"已派发"的窄边界处理。两者只能有一个线性化顺序。
|
||||
func lockRestoreDispatchActivation(ctx context.Context, tx pgx.Tx, targetIdentity string) error {
|
||||
//
|
||||
// 这里同时是**使用点**的权威域/构建身份重核:激活行里的 verified 事实必须与当前
|
||||
// 连接的 PG 权威域(集群 system_identifier + 库 OID + primary)一致;运行构建必须
|
||||
// 有完整 SHA,且与激活批次兼容;恢复目标指纹必须等于本次观察值。同名库、副本、
|
||||
// unknown 构建或换目标都在这里失败关闭,而不是只在激活那一刻检查。
|
||||
func lockRestoreDispatchActivation(ctx context.Context, tx pgx.Tx, targetIdentity, targetFingerprint string) error {
|
||||
var revoked *time.Time
|
||||
var evidenceRaw, rowFingerprint string
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT revoked_at FROM library_index_restore_dispatch_activation
|
||||
WHERE target_identity=$1 AND protocol_version=1 FOR UPDATE`, targetIdentity).Scan(&revoked)
|
||||
SELECT revoked_at, evidence::text, target_fingerprint FROM library_index_restore_dispatch_activation
|
||||
WHERE target_identity=$1 AND protocol_version=1 FOR UPDATE`, targetIdentity).Scan(&revoked, &evidenceRaw, &rowFingerprint)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("%w(目标 %s 没有激活登记)", ErrRestoreDispatchNotActivated, targetIdentity)
|
||||
}
|
||||
@@ -117,6 +123,39 @@ WHERE target_identity=$1 AND protocol_version=1 FOR UPDATE`, targetIdentity).Sca
|
||||
if revoked != nil {
|
||||
return fmt.Errorf("%w(目标 %s 的激活已于 %s 撤销)", ErrRestoreDispatchNotActivated, targetIdentity, revoked.UTC().Format(time.RFC3339))
|
||||
}
|
||||
if rowFingerprint != targetFingerprint {
|
||||
return fmt.Errorf("%w(目标 %s 的激活绑定指纹 %s,本次恢复目标指纹 %s)",
|
||||
ErrRestoreDispatchNotActivated, targetIdentity, rowFingerprint, targetFingerprint)
|
||||
}
|
||||
var record restoreDispatchActivationRecord
|
||||
if err := json.Unmarshal([]byte(evidenceRaw), &record); err != nil {
|
||||
return fmt.Errorf("parse restore dispatch activation record: %w", err)
|
||||
}
|
||||
verified := record.Verified
|
||||
authority, primary, _, err := observedGovernanceAuthority(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if verified.GovernanceAuthority.SystemIdentifier == "" || verified.GovernanceAuthority.DatabaseOID == "" {
|
||||
return fmt.Errorf("%w(目标 %s 的激活登记没有治理权威域身份:拒绝仅凭库名启用)",
|
||||
ErrRestoreDispatchNotActivated, targetIdentity)
|
||||
}
|
||||
if authority != verified.GovernanceAuthority {
|
||||
return fmt.Errorf("%w(目标 %s 的激活登记属于治理域 %s/%s,当前是 %s/%s)",
|
||||
ErrRestoreDispatchNotActivated, targetIdentity, verified.GovernanceAuthority.SystemIdentifier,
|
||||
verified.GovernanceAuthority.DatabaseOID, authority.SystemIdentifier, authority.DatabaseOID)
|
||||
}
|
||||
if !primary {
|
||||
return fmt.Errorf("%w(治理库当前处于恢复/副本角色,拒绝授予派发许可)", ErrRestoreDispatchNotActivated)
|
||||
}
|
||||
buildEvidence := libraryrelease.RestoreDispatchActivationEvidence{
|
||||
ProtocolEpoch: verified.ProtocolEpoch,
|
||||
ProtocolBuild: verified.ProtocolBuild,
|
||||
ProtocolBuildSHA: verified.ProtocolBuildSHA,
|
||||
}
|
||||
if _, _, _, err := verifyRestoreDispatchBuild(buildEvidence); err != nil {
|
||||
return fmt.Errorf("%w(目标 %s:%v)", ErrRestoreDispatchNotActivated, targetIdentity, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -143,7 +182,7 @@ WHERE target_identity=$1 AND operation_id=$2 AND window_job_id=$3 AND plan_diges
|
||||
return libraryrelease.RestoreDispatchStatus{}, false, fmt.Errorf("inspect restore dispatch: %w", err)
|
||||
}
|
||||
if stored != digest {
|
||||
return libraryrelease.RestoreDispatchStatus{State: state, Held: true,
|
||||
return libraryrelease.RestoreDispatchStatus{State: state, Held: true, BindingConflict: true,
|
||||
Reason: "restore dispatch binding differs from the immutable plan"}, true, nil
|
||||
}
|
||||
switch state {
|
||||
@@ -223,7 +262,7 @@ func (s *Store) PrepareRestoreMarker(ctx context.Context, b libraryrelease.Resto
|
||||
if err := lockRestoreDispatchIdentity(ctx, tx, b); err != nil {
|
||||
return libraryrelease.RestoreDispatchStatus{}, err
|
||||
}
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity); err != nil {
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity, b.TargetFingerprint); err != nil {
|
||||
return libraryrelease.RestoreDispatchStatus{}, err
|
||||
}
|
||||
if err := rejectRestoreDispatchPlanAlias(ctx, tx, b); err != nil {
|
||||
@@ -270,7 +309,7 @@ func (s *Store) acquireRestoreMarker(ctx context.Context, b libraryrelease.Resto
|
||||
return nil, libraryrelease.RestoreDispatchStatus{}, err
|
||||
}
|
||||
// 撤销与 CAS 在同一事务线性化:这里拿激活行锁,撤销的 UPDATE 必须排队。
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity); err != nil {
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity, b.TargetFingerprint); err != nil {
|
||||
return nil, libraryrelease.RestoreDispatchStatus{}, err
|
||||
}
|
||||
// 当前代次必须属于这个原 window 作业,且租户/epoch/租约都仍有效。lease 用
|
||||
@@ -322,13 +361,24 @@ WHERE target_identity=$1 AND operation_id=$2 AND window_job_id=$3 AND plan_diges
|
||||
return nil, libraryrelease.RestoreDispatchStatus{}, fmt.Errorf("lock restore dispatch: %w", err)
|
||||
}
|
||||
if stored != digest {
|
||||
return nil, libraryrelease.RestoreDispatchStatus{State: state, Held: true,
|
||||
return nil, libraryrelease.RestoreDispatchStatus{State: state, Held: true, BindingConflict: true,
|
||||
Reason: "restore dispatch binding differs from the immutable plan"}, libraryrelease.ErrRestoreDispatchHeld
|
||||
}
|
||||
if state != libraryrelease.RestoreDispatchPrepared {
|
||||
return nil, libraryrelease.RestoreDispatchStatus{State: state, Held: true,
|
||||
Reason: "dispatch already claimed; no second send is permitted"}, libraryrelease.ErrRestoreDispatchHeld
|
||||
}
|
||||
// effect 行锁是本次 CAS 前最后一个可能阻塞的锁。job/attempt/恢复投递的租约可能
|
||||
// 正好在等它的时候过期;前面那几次检查发生在拿锁之前,不能充当 CAS 点的判据。
|
||||
// 这些行锁都已被本事务持有,这里只重读、不改变锁序。
|
||||
if err := recheckRestoreDispatchLeases(ctx, tx, b); err != nil {
|
||||
var held *restoreDispatchLeaseExpired
|
||||
if errors.As(err, &held) {
|
||||
return nil, libraryrelease.RestoreDispatchStatus{State: libraryrelease.RestoreDispatchPrepared, Held: true,
|
||||
Reason: held.Error()}, fmt.Errorf("%w: %s", governance.ErrLibraryEpochStale, held.Error())
|
||||
}
|
||||
return nil, libraryrelease.RestoreDispatchStatus{}, err
|
||||
}
|
||||
|
||||
var raw [16]byte
|
||||
if _, err := rand.Read(raw[:]); err != nil {
|
||||
@@ -384,7 +434,7 @@ func (s *Store) ConfirmRestoreMarker(ctx context.Context, b libraryrelease.Resto
|
||||
if err := lockRestoreDispatchIdentity(ctx, tx, b); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity); err != nil {
|
||||
if err := lockRestoreDispatchActivation(ctx, tx, b.Key.TargetIdentity, b.TargetFingerprint); err != nil {
|
||||
return err
|
||||
}
|
||||
var epoch int64
|
||||
@@ -404,6 +454,23 @@ WHERE operation_id=$1 AND state IN ('claimed','running') FOR UPDATE`, b.Key.Oper
|
||||
if attemptJob != b.Key.WindowJobID || owner != b.Owner || epoch != b.Epoch || !leaseLive {
|
||||
return fmt.Errorf("%w: confirmation by a stale owner", governance.ErrLibraryEpochStale)
|
||||
}
|
||||
// 与 Acquire 同一个顺序:确认前的最后一把锁也要先拿到,再按数据库实时时钟重核
|
||||
// 租约。否则"确认"可以在等锁跨过到期之后仍落库,把一次过期 owner 的回执写成事实。
|
||||
var effectID int64
|
||||
if err := tx.QueryRow(ctx, `
|
||||
SELECT id FROM library_index_restore_dispatch
|
||||
WHERE target_identity=$1 AND operation_id=$2 AND window_job_id=$3 AND plan_digest=$4
|
||||
AND index_name=$5 AND document_id=$6 AND phase=$7 FOR UPDATE`,
|
||||
b.Key.TargetIdentity, b.Key.OperationID, b.Key.WindowJobID, b.Key.PlanDigest,
|
||||
b.Key.Index, b.Key.DocumentID, b.Key.Phase).Scan(&effectID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return errors.New("restore marker confirmation did not match a dispatch record")
|
||||
}
|
||||
return fmt.Errorf("lock restore marker for confirmation: %w", err)
|
||||
}
|
||||
if err := recheckRestoreDispatchLeases(ctx, tx, b); err != nil {
|
||||
return fmt.Errorf("%w: %s", governance.ErrLibraryEpochStale, err)
|
||||
}
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE library_index_restore_dispatch
|
||||
SET state='marker_confirmed', marker_receipt=$1::jsonb, confirmed_at=clock_timestamp(), updated_at=clock_timestamp()
|
||||
@@ -513,23 +580,8 @@ FROM library_instance_maintenance WHERE instance=$1 FOR UPDATE`, b.TargetInstanc
|
||||
}
|
||||
|
||||
if b.RecoveryJobID != "" {
|
||||
var queueState string
|
||||
var lease string
|
||||
if err := tx.QueryRow(ctx, `SELECT state, COALESCE(lease_owner,'') FROM jobs WHERE id=$1`, b.RecoveryJobID).
|
||||
Scan(&queueState, &lease); err != nil {
|
||||
return fmt.Errorf("read recovery queue job: %w", err)
|
||||
}
|
||||
if queueState != "running" || lease == "" {
|
||||
return fmt.Errorf("recovery queue job %s is not a live delivery", b.RecoveryJobID)
|
||||
}
|
||||
var recoveryKind, recoveryScope, recoveryBackup string
|
||||
if err := tx.QueryRow(ctx, `
|
||||
SELECT kind, scope, target_snapshot_id FROM library_operations WHERE id=$1`, b.RecoveryOperationID).
|
||||
Scan(&recoveryKind, &recoveryScope, &recoveryBackup); err != nil {
|
||||
return fmt.Errorf("read recovery operation: %w", err)
|
||||
}
|
||||
if recoveryKind != string(governance.LibraryJobRecover) || recoveryScope != b.Scope || recoveryBackup != b.BackupSnapshot {
|
||||
return fmt.Errorf("recovery operation %s is not bound to this window and backup", b.RecoveryOperationID)
|
||||
if err := lockAndVerifyRecoveryDelivery(ctx, tx, b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user