8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
113 lines
3.7 KiB
Go
113 lines
3.7 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"knowledge-governance/internal/libraryrelease"
|
|
)
|
|
|
|
const indexClaimPrefix = "kg-release-transient-claim:"
|
|
|
|
// The transient marker is only visible inside the maintenance window. Final
|
|
// content is byte-equivalent to the frozen source, without control-plane fields.
|
|
// A matching payload alone never establishes ownership of an ambiguous write.
|
|
func (t *ProductionLibraryTarget) indexClaimBody(c libraryrelease.IndexWrite) (json.RawMessage, error) {
|
|
if len(c.After) == 0 {
|
|
return nil, errors.New("deleted index document has no ownership marker")
|
|
}
|
|
var body map[string]json.RawMessage
|
|
if json.Unmarshal(c.After, &body) != nil || body == nil {
|
|
return nil, errors.New("invalid index claim source")
|
|
}
|
|
var content string
|
|
if json.Unmarshal(body["content"], &content) != nil {
|
|
return nil, errors.New("index claim requires existing string content field")
|
|
}
|
|
sum := sha256.Sum256([]byte(t.activePlan.Digest + "\x00" + c.Index + "\x00" + c.ID + "\x00apply"))
|
|
body["content"], _ = json.Marshal(indexClaimPrefix + hex.EncodeToString(sum[:]) + "\n" + content)
|
|
encoded, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return canonicalJSON(encoded)
|
|
}
|
|
func (t *ProductionLibraryTarget) applyIndexClaim(ctx context.Context, c libraryrelease.IndexWrite) (indexReceipt, error) {
|
|
if len(c.After) == 0 {
|
|
return t.mutatePlannedIndex(ctx, c, nil, c.SeqNo, c.PrimaryTerm, false)
|
|
}
|
|
body, err := t.indexClaimBody(c)
|
|
if err != nil {
|
|
return indexReceipt{}, err
|
|
}
|
|
receipt, err := t.mutatePlannedIndex(ctx, c, body, c.SeqNo, c.PrimaryTerm, len(c.Before) == 0)
|
|
if err != nil {
|
|
return receipt, err
|
|
}
|
|
receipt.Marked = true
|
|
if err = t.saveIndexEffect(c, "claimed", &receipt); err != nil {
|
|
return receipt, err
|
|
}
|
|
return t.finalizeIndexClaim(ctx, c)
|
|
}
|
|
func (t *ProductionLibraryTarget) reconcileIndexClaim(ctx context.Context, c libraryrelease.IndexWrite) (indexReceipt, error) {
|
|
empty := indexReceipt{}
|
|
intent, err := t.loadIndexEffect(c, "apply-intent")
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if intent == nil {
|
|
return empty, errors.New("index claim has no durable intent")
|
|
}
|
|
finalizing, err := t.loadIndexEffect(c, "finalize-intent")
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if finalizing != nil {
|
|
return empty, errors.New("unknown clean finalization cannot be attributed by appearance")
|
|
}
|
|
body, err := t.indexClaimBody(c)
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
g, err := t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if !indexBytesEqual(g, body) || g.SeqNo == nil || g.PrimaryTerm == nil {
|
|
return empty, errors.New("unknown index effect lacks exact operation marker/content/CAS")
|
|
}
|
|
claimed, err := t.loadIndexEffect(c, "claimed")
|
|
if err != nil {
|
|
return empty, err
|
|
}
|
|
if claimed != nil {
|
|
if claimed.Receipt == nil || !claimed.Receipt.Marked || !sameIndexGeneration(g, claimed.Receipt.SeqNo, claimed.Receipt.PrimaryTerm) {
|
|
return empty, errors.New("claimed index generation changed")
|
|
}
|
|
return *claimed.Receipt, nil
|
|
}
|
|
result := "updated"
|
|
if len(c.Before) == 0 {
|
|
result = "created"
|
|
}
|
|
receipt := indexReceipt{Index: c.Index, ID: c.ID, Result: result, SeqNo: g.SeqNo, PrimaryTerm: g.PrimaryTerm, Marked: true}
|
|
if err = t.saveIndexEffect(c, "claimed", &receipt); err != nil {
|
|
return empty, err
|
|
}
|
|
return receipt, nil
|
|
}
|
|
func (t *ProductionLibraryTarget) finalizeIndexClaim(ctx context.Context, c libraryrelease.IndexWrite) (indexReceipt, error) {
|
|
receipt, err := t.reconcileIndexClaim(ctx, c)
|
|
if err != nil {
|
|
return indexReceipt{}, err
|
|
}
|
|
if err = t.saveIndexEffect(c, "finalize-intent", &receipt); err != nil {
|
|
return indexReceipt{}, err
|
|
}
|
|
return t.mutatePlannedIndex(ctx, c, c.After, receipt.SeqNo, receipt.PrimaryTerm, false)
|
|
}
|