8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
111 lines
3.8 KiB
Go
111 lines
3.8 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"knowledge-governance/internal/libraryrelease"
|
|
)
|
|
|
|
const indexRestoreClaimPrefix = "kg-release-transient-restore-claim:"
|
|
|
|
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
|
|
}
|
|
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.saveIndexEffect(c, "restore-mark-intent", &applied); 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}
|
|
}
|
|
finalizing, err := t.loadIndexEffect(c, "restore-finalize-intent")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if finalizing != nil {
|
|
return errors.New("unknown clean restoration cannot be attributed by appearance; held")
|
|
}
|
|
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
|
|
}
|
|
receipt, err := t.mutatePlannedIndex(ctx, c, c.Before, claimed.Receipt.SeqNo, claimed.Receipt.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
|
|
}
|