e09fddcb74
为什么:恢复在发现 index held 之前可能已经回放 database/resources; "最后数据相同"不等于"本次零派发"。改为 Restore 在任何写之前按整份 冻结计划做纯读准入,收尾前再拒绝未决 effect。 删除型恢复的 _create 只从唯一 dispatcher 发出:preflight/故障钩子 在 CAS 之前,许可来自 COMMIT 明确的短事务;发送请求 GetBody=nil, 传输层无法重放。旧意图无台账、dispatching 无精确 marker 一律 held。 验证:真 ES+真 PG 覆盖旧意图零写 held、prepared 续作到终态 (create 恰 1 次)、dispatching+missing 两轮零写、新 OS 进程续作正例。
300 lines
11 KiB
Go
300 lines
11 KiB
Go
package v071
|
||
|
||
import (
|
||
"context"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
|
||
"knowledge-governance/internal/libraryrelease"
|
||
)
|
||
|
||
// DS01:删除型恢复(Before 非空、After 为空)的标记 create 是唯一会"无中生有"的
|
||
// 恢复写:ES 里文档缺失与"从未写过"完全同形,所以它不能靠外观、stats 或内存计数
|
||
// 放行。新协议把"是否已经有人取得发送许可"放进治理库的单调台账;本文件是 adapter
|
||
// 侧的全部接入点——准入、唯一派发、确认与收尾检查。
|
||
//
|
||
// 明确不保证:ES 端 fence;已 dispatching 的旧 owner 绝不迟到一次。协议只保证
|
||
// 遵守协议的派发者不会得到第二次许可,且 dispatching 永不回退。
|
||
|
||
func (t *ProductionLibraryTarget) restoreMarkerBinding(c libraryrelease.IndexWrite, body json.RawMessage) (libraryrelease.RestoreMarkerBinding, error) {
|
||
if t.config.RestoreDispatch == nil {
|
||
return libraryrelease.RestoreMarkerBinding{}, errors.New("restore dispatch protocol is not wired")
|
||
}
|
||
if t.activePlan == nil {
|
||
return libraryrelease.RestoreMarkerBinding{}, errors.New("restore dispatch requires the frozen plan")
|
||
}
|
||
if t.config.RestoreTargetIdentity == "" || t.config.RestoreOperationID == "" ||
|
||
t.config.RestoreWindowJobID == "" || t.config.RestoreAttemptOwner == "" ||
|
||
t.config.RestoreAttemptEpoch <= 0 {
|
||
return libraryrelease.RestoreMarkerBinding{}, errors.New("restore dispatch lacks the original job/attempt identity")
|
||
}
|
||
changeRaw, err := json.Marshal(c)
|
||
if err != nil {
|
||
return libraryrelease.RestoreMarkerBinding{}, err
|
||
}
|
||
changeDigest := sha256.Sum256(changeRaw)
|
||
markerDigest := sha256.Sum256(body)
|
||
method, path, _, err := t.indexMutation(c, body, nil, nil, true)
|
||
if err != nil {
|
||
return libraryrelease.RestoreMarkerBinding{}, err
|
||
}
|
||
requestDigest := sha256.Sum256([]byte(method + "\x00" + path))
|
||
return libraryrelease.RestoreMarkerBinding{
|
||
Key: libraryrelease.RestoreMarkerKey{
|
||
TargetIdentity: t.config.RestoreTargetIdentity,
|
||
OperationID: t.config.RestoreOperationID,
|
||
WindowJobID: t.config.RestoreWindowJobID,
|
||
PlanDigest: t.activePlan.Digest,
|
||
Index: c.Index,
|
||
DocumentID: c.ID,
|
||
Phase: libraryrelease.RestoreMarkerPhaseCreate,
|
||
},
|
||
Scope: t.config.RestoreScope,
|
||
SourceSnapshot: t.config.RestoreSnapshotID,
|
||
BackupSnapshot: t.config.RestoreSnapshotID,
|
||
ChangeDigest: hex.EncodeToString(changeDigest[:]),
|
||
MarkerDigest: hex.EncodeToString(markerDigest[:]),
|
||
RequestDigest: hex.EncodeToString(requestDigest[:]),
|
||
Owner: t.config.RestoreAttemptOwner,
|
||
Epoch: t.config.RestoreAttemptEpoch,
|
||
}, nil
|
||
}
|
||
|
||
func heldRestoreMarker(status libraryrelease.RestoreDispatchStatus, context string) error {
|
||
return fmt.Errorf("%w: %s(state=%s)", libraryrelease.ErrRestoreDispatchHeld, context, status.State)
|
||
}
|
||
|
||
// admitRestorePlan 是 Restore 在**任何** PG/对象/索引写之前的纯读准入。
|
||
//
|
||
// 删除型恢复一旦开始写 recovery 的行/对象,后面才发现某个 effect 的派发结果未知,
|
||
// 目标已经从"未动"变成"半恢复"。所以先按整份冻结计划逐 effect 判定:
|
||
// - 台账不存在(旧世界):只有"没有任何恢复意图"才算首次恢复;有意图就 held;
|
||
// - prepared:唯一可续作区段;
|
||
// - dispatching:只有精确 marker+世代才可走确认分支,否则 held;
|
||
// - marker_confirmed:只允许确认后的 clean 路径。
|
||
func (t *ProductionLibraryTarget) admitRestorePlan(ctx context.Context) error {
|
||
for _, c := range t.activePlan.Index {
|
||
if len(c.After) != 0 {
|
||
continue
|
||
}
|
||
body, err := t.restoreIndexClaimBody(c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
intent, err := t.loadIndexEffect(c, "restore-mark-intent")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 已完成 clean 的 effect 是终态:恢复内容与证据都在时,重放不是未决。
|
||
done, err := t.loadIndexEffect(c, "restored")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if done != nil {
|
||
observed, err := t.observePlannedIndex(ctx, c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if done.Receipt == nil || !indexBytesEqual(observed, c.Before) ||
|
||
!sameIndexGeneration(observed, done.Receipt.SeqNo, done.Receipt.PrimaryTerm) {
|
||
return fmt.Errorf("%w: 已标记 restored 的 effect 与目标现状不符", libraryrelease.ErrRestoreDispatchHeld)
|
||
}
|
||
continue
|
||
}
|
||
if t.config.RestoreDispatch != nil {
|
||
binding, bindErr := t.restoreMarkerBinding(c, body)
|
||
if bindErr != nil {
|
||
return bindErr
|
||
}
|
||
status, found, inspErr := t.config.RestoreDispatch.InspectRestoreMarker(ctx, binding)
|
||
if inspErr != nil {
|
||
return fmt.Errorf("恢复准入读不到派发台账:%w", inspErr)
|
||
}
|
||
if found {
|
||
switch status.State {
|
||
case libraryrelease.RestoreDispatchPrepared:
|
||
continue
|
||
case libraryrelease.RestoreDispatchDispatching, libraryrelease.RestoreDispatchMarkerConfirmed:
|
||
observed, obsErr := t.observePlannedIndex(ctx, c)
|
||
if obsErr != nil {
|
||
return obsErr
|
||
}
|
||
if len(c.After) == 0 && indexBytesEqual(observed, body) && observed.SeqNo != nil && observed.PrimaryTerm != nil {
|
||
continue // 只允许原确认分支继续,不重发 create
|
||
}
|
||
return heldRestoreMarker(status, "目标上缺少可归属的精确恢复标记")
|
||
default:
|
||
return heldRestoreMarker(status, "派发台账状态不可识别")
|
||
}
|
||
}
|
||
}
|
||
if intent == nil {
|
||
continue
|
||
}
|
||
observed, err := t.observePlannedIndex(ctx, c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if indexBytesEqual(observed, body) {
|
||
continue // 旧标记已在目标上:按既有确认/CAS 规则处理,不派发新 create
|
||
}
|
||
return fmt.Errorf("%w: 既存恢复意图没有新协议派发台账,无法证明未派发", libraryrelease.ErrRestoreDispatchHeld)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// confirmDispatchedMarker 只在"marker 与冻结内容逐位相同且世代齐全"时记录回执。
|
||
// 它不发送任何请求;确认失败即 held,绝不因为"看起来已经写好了"就继续 clean。
|
||
func (t *ProductionLibraryTarget) confirmDispatchedMarker(ctx context.Context, c libraryrelease.IndexWrite, body json.RawMessage, observed indexWriteGuard) error {
|
||
if t.config.RestoreDispatch == nil || len(c.After) != 0 {
|
||
return nil
|
||
}
|
||
binding, err := t.restoreMarkerBinding(c, body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
status, found, err := t.config.RestoreDispatch.InspectRestoreMarker(ctx, binding)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !found {
|
||
return fmt.Errorf("%w: 删除型恢复缺少派发台账,拒绝按外观继续", libraryrelease.ErrRestoreDispatchHeld)
|
||
}
|
||
switch status.State {
|
||
case libraryrelease.RestoreDispatchPrepared:
|
||
// 还没派发:由调用方按 prepared 竞争许可。
|
||
return nil
|
||
case libraryrelease.RestoreDispatchMarkerConfirmed:
|
||
return nil
|
||
case libraryrelease.RestoreDispatchDispatching:
|
||
if !indexBytesEqual(observed, body) || observed.SeqNo == nil || observed.PrimaryTerm == nil {
|
||
return heldRestoreMarker(status, "dispatching 结果未知且目标上没有精确标记")
|
||
}
|
||
receipt := indexReceipt{Index: c.Index, ID: c.ID, Result: "created", SeqNo: observed.SeqNo, PrimaryTerm: observed.PrimaryTerm}
|
||
raw, err := json.Marshal(receipt)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return t.config.RestoreDispatch.ConfirmRestoreMarker(ctx, binding, raw)
|
||
default:
|
||
return heldRestoreMarker(status, "派发台账状态不可识别")
|
||
}
|
||
}
|
||
|
||
// dispatchRestoreMarkerCreate 是恢复期唯一获得发送许可的入口:preflight 与故障钩子
|
||
// 都在 CAS 之前,Acquire 只在 dispatching 明确提交后返回许可。
|
||
func (t *ProductionLibraryTarget) dispatchRestoreMarkerCreate(ctx context.Context, c libraryrelease.IndexWrite, body json.RawMessage) (indexReceipt, error) {
|
||
if t.config.RestoreDispatch == nil {
|
||
return indexReceipt{}, errors.New("恢复标记派发协议未接线:拒绝发送")
|
||
}
|
||
binding, err := t.restoreMarkerBinding(c, body)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
status, found, err := t.config.RestoreDispatch.InspectRestoreMarker(ctx, binding)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
if !found || status.State != libraryrelease.RestoreDispatchPrepared {
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
return indexReceipt{}, heldRestoreMarker(status, "没有可竞争的 prepared 许可")
|
||
}
|
||
// 控制流故障注入必须在 CAS 之前:拒绝时状态仍是 prepared,可安全重试,零转发。
|
||
if err := t.refuseRestoreWrite(c); err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
permit, acquireStatus, err := t.config.RestoreDispatch.AcquireRestoreMarker(ctx, binding)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
if permit == nil {
|
||
return indexReceipt{}, heldRestoreMarker(acquireStatus, "许可竞争未胜出")
|
||
}
|
||
receipt, err := t.sendRestoreMarkerOnce(ctx, c, body, binding, permit)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
raw, err := json.Marshal(receipt)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
// 回执来自这次唯一发送:必须落进台账才算确认。写失败时 marker 已在目标上,
|
||
// 下一进程走"精确 marker+世代"确认分支,不会重发 create。
|
||
if err := t.config.RestoreDispatch.ConfirmRestoreMarker(ctx, binding, raw); err != nil {
|
||
return indexReceipt{}, fmt.Errorf("恢复标记已发送但确认未落库:%w", err)
|
||
}
|
||
return receipt, nil
|
||
}
|
||
|
||
// sendRestoreMarkerOnce 是唯一的真实网络发送点:许可消费一次、请求不可被传输层重放。
|
||
func (t *ProductionLibraryTarget) sendRestoreMarkerOnce(ctx context.Context, c libraryrelease.IndexWrite, body json.RawMessage,
|
||
binding libraryrelease.RestoreMarkerBinding, permit *libraryrelease.RestoreSendPermit) (indexReceipt, error) {
|
||
digest, err := binding.Digest()
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
if err := permit.Consume(binding.Key, digest); err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
method, path, payload, err := t.indexMutation(c, body, nil, nil, true)
|
||
if err != nil {
|
||
return indexReceipt{}, err
|
||
}
|
||
var receipt indexReceipt
|
||
if err := t.search.requestJSONOnce(ctx, method, path, payload, &receipt); err != nil {
|
||
return receipt, fmt.Errorf("restore marker outcome unknown: %w", err)
|
||
}
|
||
if err := validateIndexReceipt(c, len(body) > 0, receipt); err != nil {
|
||
return receipt, err
|
||
}
|
||
return receipt, nil
|
||
}
|
||
|
||
// verifyRestoreEffectsClosed 是 Restore 收尾前的最后一道检查:任何一个删除型 effect
|
||
// 只要留下恢复意图,就必须已经完成 clean(restored 证据),且台账不得停在 dispatching。
|
||
func (t *ProductionLibraryTarget) verifyRestoreEffectsClosed(ctx context.Context) error {
|
||
for _, c := range t.activePlan.Index {
|
||
if len(c.After) != 0 {
|
||
continue
|
||
}
|
||
intent, err := t.loadIndexEffect(c, "restore-mark-intent")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if intent == nil {
|
||
continue
|
||
}
|
||
done, err := t.loadIndexEffect(c, "restored")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if done == nil {
|
||
return fmt.Errorf("%w: 恢复收尾前仍有未决 effect %s", libraryrelease.ErrRestoreDispatchHeld, c.ID)
|
||
}
|
||
if t.config.RestoreDispatch == nil {
|
||
continue
|
||
}
|
||
body, err := t.restoreIndexClaimBody(c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
binding, err := t.restoreMarkerBinding(c, body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
status, found, err := t.config.RestoreDispatch.InspectRestoreMarker(ctx, binding)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if found && status.State == libraryrelease.RestoreDispatchDispatching {
|
||
return heldRestoreMarker(status, "收尾前台账仍停在 dispatching")
|
||
}
|
||
}
|
||
return nil
|
||
}
|