066dc45a25
build / build (push) Successful in 1m34s
- ES `_stats` 写计数是分片生命周期统计(官方文档:分片迁到另一节点时该分片统计清零), 索引 uuid 不变不绑定统计生命周期,清零后重新累计到同一数值(ABA)无法排除。 撤销 r36 的"写计数器栅栏"授权:既不落盘也不读取,统计仅作诊断。 - 删除型恢复(独占 `_create`)只有在本次尝试开始时不存在任何恢复意图、且文档确实缺失时 才可重做;既存 restore-mark-intent 配缺失文档一律 held、零写入。 - 保留可证明路径:marker 仍在且世代一致的 clean 收尾重试;文档仍是 applied 结果、 世代一致的 DELETE 补偿重试。 - 新增 ABA 反例(传输层 stats 故障模拟、真实 ES 文档写删、未重启共享 ES); B19 / clean / delete 安全正例全回归。 验证(真实 PG/ES/MinIO 隔离夹具): - ABA 反例:旧代码 RED(restoration must be held, not rebuilt),新代码 PASS - 索引/恢复回归 13 项 PASS - 回滚回归 2 项 PASS - make verify 16 项 success(identity 仅 CI 可核) 未部署、未写生产/云、未重启共享 ES。
188 lines
7.7 KiB
Go
188 lines
7.7 KiB
Go
package v071
|
||
|
||
import (
|
||
"context"
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"errors"
|
||
"knowledge-governance/internal/libraryrelease"
|
||
)
|
||
|
||
const indexRestoreClaimPrefix = "kg-release-transient-restore-claim:"
|
||
|
||
// restorationProvablyUnsent 判定"这份恢复意图确实没有到达索引"。
|
||
//
|
||
// 判据只能是"每成功写一次必然会在服务端留下的东西":
|
||
// - 更新型:观察到的内容必须仍是 applied 结果,且世代与 applied 回执完全相同;
|
||
// - 删除型(恢复写入是独占 create):服务端没有持久、不可重用的证据。文档缺失与
|
||
// "从未写过"在索引里完全同形;ES 的写计数器只是分片生命周期内的统计量(官方
|
||
// `_stats` 文档:分片迁到别的节点时该分片的统计会被清零),索引 uuid 不变并不
|
||
// 绑定统计生命周期,清零后重新累计到同一数值(ABA)无法排除。删除型只有当
|
||
// **本次尝试开始时不存在任何恢复意图**(freshIntent:先落意图、后派发的不变量
|
||
// 说明本作业从未派发过)且文档确实缺失时才能重做;既存意图一律 held(B19)。
|
||
//
|
||
// 任何"内容看起来对但世代对不上"的状态都只能 held,不认领。
|
||
func (t *ProductionLibraryTarget) restorationProvablyUnsent(ctx context.Context, observed indexWriteGuard, c libraryrelease.IndexWrite, applied indexReceipt, freshIntent bool) (bool, error) {
|
||
if len(c.After) == 0 {
|
||
return freshIntent && !observed.Found, nil
|
||
}
|
||
if !sameIndexGeneration(observed, applied.SeqNo, applied.PrimaryTerm) {
|
||
return false, nil
|
||
}
|
||
if indexBytesEqual(observed, c.After) {
|
||
return true, nil
|
||
}
|
||
if applied.Marked {
|
||
// 应用期停在"标记态"(claim 未收尾)时,applied 回执说的就是那份标记体。
|
||
claim, err := t.indexClaimBody(c)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return indexBytesEqual(observed, claim), nil
|
||
}
|
||
return false, nil
|
||
}
|
||
|
||
func (t *ProductionLibraryTarget) restoreIndexClaimBody(c libraryrelease.IndexWrite) (json.RawMessage, error) {
|
||
var body map[string]json.RawMessage
|
||
if json.Unmarshal(c.Before, &body) != nil || body == nil {
|
||
return nil, errors.New("restore claim requires frozen before-image")
|
||
}
|
||
var content string
|
||
if json.Unmarshal(body["content"], &content) != nil {
|
||
return nil, errors.New("restore claim requires existing content field")
|
||
}
|
||
sum := sha256.Sum256([]byte(t.activePlan.Digest + "\x00" + c.Index + "\x00" + c.ID + "\x00restore"))
|
||
body["content"], _ = json.Marshal(indexRestoreClaimPrefix + hex.EncodeToString(sum[:]) + "\n" + content)
|
||
encoded, err := json.Marshal(body)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return canonicalJSON(encoded)
|
||
}
|
||
|
||
// Only a plan-specific marker plus the entire frozen body can reconcile a lost
|
||
// restore-write response. Equal clean content is not proof of who wrote it.
|
||
// Old unknown writes without this pre-send intent remain held, not adopted.
|
||
func (t *ProductionLibraryTarget) restoreClaimedIndexBefore(ctx context.Context, c libraryrelease.IndexWrite, applied indexReceipt, start bool) error {
|
||
body, err := t.restoreIndexClaimBody(c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
markIntent, err := t.loadIndexEffect(c, "restore-mark-intent")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 本次尝试开始时没有任何恢复意图,才可能证明"本作业从未派发过":
|
||
// 生产路径先落意图再派发,意图不可覆盖。既存意图的派发历史无法由缺失证明。
|
||
freshIntent := markIntent == nil
|
||
claimed, err := t.loadIndexEffect(c, "restore-claimed")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if markIntent == nil {
|
||
if !start {
|
||
return errors.New("legacy unknown index restoration lacks attributable marker intent")
|
||
}
|
||
if err = t.beginRestoreMarker(ctx, c, applied); err != nil {
|
||
return err
|
||
}
|
||
if markIntent, err = t.loadIndexEffect(c, "restore-mark-intent"); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
finalizing, err := t.loadIndexEffect(c, "restore-finalize-intent")
|
||
if err != nil {
|
||
return err
|
||
}
|
||
observed, err := t.observePlannedIndex(ctx, c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if finalizing != nil {
|
||
// 干净写入在派发边界上被拒绝时,标记体会原样停在自己那份世代上;落地过一次
|
||
// 干净写入必然换掉内容并推进世代。两条同时成立才证明没写过,否则 held。
|
||
if finalizing.Receipt == nil || !indexBytesEqual(observed, body) || !sameIndexGeneration(observed, finalizing.Receipt.SeqNo, finalizing.Receipt.PrimaryTerm) {
|
||
return errors.New("unknown clean restoration cannot be attributed by appearance; held")
|
||
}
|
||
return t.finishRestoreCleanWrite(ctx, c, *finalizing.Receipt)
|
||
}
|
||
if !indexBytesEqual(observed, body) {
|
||
// The marker is absent: either the intent never reached the index (a
|
||
// deterministic refusal before dispatch) or its write outcome is unknown.
|
||
// Only a server-side generation that every successful write would move —
|
||
// available for updates — separates the two. The exclusive-create path has
|
||
// no durable, non-reusable evidence (ES counters are shard-lifetime
|
||
// statistics and are cleared on shard relocation), so it stays held (B19)
|
||
// and is never adopted by appearance or by counter equality.
|
||
unsent, err := t.restorationProvablyUnsent(ctx, observed, c, applied, freshIntent)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !unsent {
|
||
return errors.New("unknown restoration lacks exact operation marker/content/CAS; held")
|
||
}
|
||
if err := t.refuseRestoreWrite(c); err != nil {
|
||
return err
|
||
}
|
||
receipt, err := t.mutatePlannedIndex(ctx, c, body, applied.SeqNo, applied.PrimaryTerm, len(c.After) == 0)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
receipt.Marked = true
|
||
if err = t.saveIndexEffect(c, "restore-claimed", &receipt); err != nil {
|
||
return err
|
||
}
|
||
claimed = &indexEffect{Plan: t.activePlan.Digest, Change: c, Receipt: &receipt}
|
||
observed, err = t.observePlannedIndex(ctx, c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if !indexBytesEqual(observed, body) || observed.SeqNo == nil || observed.PrimaryTerm == nil {
|
||
return errors.New("unknown restoration lacks exact operation marker/content/CAS; held")
|
||
}
|
||
if claimed == nil {
|
||
receipt := indexReceipt{Index: c.Index, ID: c.ID, Result: "updated", SeqNo: observed.SeqNo, PrimaryTerm: observed.PrimaryTerm, Marked: true}
|
||
if len(c.After) == 0 {
|
||
receipt.Result = "created"
|
||
}
|
||
if err = t.saveIndexEffect(c, "restore-claimed", &receipt); err != nil {
|
||
return err
|
||
}
|
||
claimed = &indexEffect{Plan: t.activePlan.Digest, Change: c, Receipt: &receipt}
|
||
}
|
||
if claimed.Receipt == nil || !claimed.Receipt.Marked || !sameIndexGeneration(observed, claimed.Receipt.SeqNo, claimed.Receipt.PrimaryTerm) {
|
||
return errors.New("restoration claim generation changed; held")
|
||
}
|
||
if err = t.saveIndexEffect(c, "restore-finalize-intent", claimed.Receipt); err != nil {
|
||
return err
|
||
}
|
||
return t.finishRestoreCleanWrite(ctx, c, *claimed.Receipt)
|
||
}
|
||
|
||
// finishRestoreCleanWrite 派发"标记体 -> 冻结 before-image"的 CAS 收尾,并把回执与
|
||
// 实时观察逐位核对。finalize-intent 存在时,只有标记体仍停在它自己的世代上才会
|
||
// 走到这里:落地过干净写入必然换掉内容并推进世代。
|
||
func (t *ProductionLibraryTarget) finishRestoreCleanWrite(ctx context.Context, c libraryrelease.IndexWrite, marker indexReceipt) error {
|
||
if err := t.refuseRestoreWrite(c); err != nil {
|
||
return err
|
||
}
|
||
receipt, err := t.mutatePlannedIndex(ctx, c, c.Before, marker.SeqNo, marker.PrimaryTerm, false)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if err = t.saveIndexEffect(c, "restored", &receipt); err != nil {
|
||
return err
|
||
}
|
||
observed, err := t.observePlannedIndex(ctx, c)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if !indexBytesEqual(observed, c.Before) || !sameIndexGeneration(observed, receipt.SeqNo, receipt.PrimaryTerm) {
|
||
return errors.New("restoration clean readback differs from receipt")
|
||
}
|
||
return nil
|
||
}
|