8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
291 lines
9.0 KiB
Go
291 lines
9.0 KiB
Go
package v071
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"knowledge-governance/internal/libraryrelease"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
type indexReceipt struct {
|
|
Index string `json:"_index"`
|
|
ID string `json:"_id"`
|
|
Result string `json:"result"`
|
|
SeqNo *int64 `json:"_seq_no"`
|
|
PrimaryTerm *int64 `json:"_primary_term"`
|
|
Marked bool `json:"marked,omitempty"`
|
|
}
|
|
type indexEffect struct {
|
|
Plan string `json:"plan"`
|
|
Change libraryrelease.IndexWrite `json:"change"`
|
|
Receipt *indexReceipt `json:"receipt,omitempty"`
|
|
}
|
|
|
|
func (t *ProductionLibraryTarget) indexEffectPath(c libraryrelease.IndexWrite, phase string) string {
|
|
sum := sha256.Sum256([]byte(c.Index + "\x00" + c.ID))
|
|
return filepath.Join(t.config.ArchiveRoot, "effects", t.activePlan.Digest, "index", hex.EncodeToString(sum[:]), phase+".json")
|
|
}
|
|
func (t *ProductionLibraryTarget) saveIndexEffect(c libraryrelease.IndexWrite, phase string, r *indexReceipt) error {
|
|
raw, err := json.Marshal(indexEffect{t.activePlan.Digest, c, r})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return persistImmutableJSON(t.indexEffectPath(c, phase), raw)
|
|
}
|
|
func (t *ProductionLibraryTarget) loadIndexEffect(c libraryrelease.IndexWrite, phase string) (*indexEffect, error) {
|
|
path := t.indexEffectPath(c, phase)
|
|
exists, err := effectExists(path)
|
|
if err != nil || !exists {
|
|
return nil, err
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var e indexEffect
|
|
if json.Unmarshal(raw, &e) != nil {
|
|
return nil, errors.New("invalid index effect evidence")
|
|
}
|
|
want, _ := json.Marshal(c)
|
|
got, _ := json.Marshal(e.Change)
|
|
if e.Plan != t.activePlan.Digest || !bytes.Equal(want, got) {
|
|
return nil, errors.New("index effect binding differs from immutable plan")
|
|
}
|
|
if e.Receipt != nil && (e.Receipt.Index != c.Index || e.Receipt.ID != c.ID || e.Receipt.SeqNo == nil || e.Receipt.PrimaryTerm == nil) {
|
|
return nil, errors.New("incomplete index receipt")
|
|
}
|
|
return &e, nil
|
|
}
|
|
func (t *ProductionLibraryTarget) observePlannedIndex(ctx context.Context, c libraryrelease.IndexWrite) (indexWriteGuard, error) {
|
|
guards, err := t.search.indexWriteGuards(ctx, []librarysnapshot.IndexDocument{{ID: c.ID}}, SearchIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID})
|
|
if err != nil {
|
|
return indexWriteGuard{}, err
|
|
}
|
|
return guards[c.ID], nil
|
|
}
|
|
func indexBytesEqual(g indexWriteGuard, want json.RawMessage) bool {
|
|
if len(want) == 0 {
|
|
return !g.Found
|
|
}
|
|
if !g.Found {
|
|
return false
|
|
}
|
|
raw, err := canonicalJSON(g.Source)
|
|
return err == nil && bytes.Equal(raw, want)
|
|
}
|
|
func sameIndexGeneration(g indexWriteGuard, seq, term *int64) bool {
|
|
return g.Found && g.SeqNo != nil && g.PrimaryTerm != nil && seq != nil && term != nil && *g.SeqNo == *seq && *g.PrimaryTerm == *term
|
|
}
|
|
|
|
// Each network mutation has an immutable intent before send and a per-ID
|
|
// server generation receipt. A lost response is UNKNOWN, never "did not write".
|
|
// No retry is issued for an unresolved intent; downstream ownership fencing is
|
|
// required before unknown operations can be conclusively reconciled.
|
|
func (t *ProductionLibraryTarget) applyPlannedIndex(ctx context.Context) error {
|
|
for _, c := range t.activePlan.Index {
|
|
if bytes.Equal(c.Before, c.After) {
|
|
continue
|
|
}
|
|
applied, err := t.loadIndexEffect(c, "applied")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if applied != nil {
|
|
if applied.Receipt == nil {
|
|
return errors.New("index receipt missing")
|
|
}
|
|
g, err := t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !indexBytesEqual(g, c.After) || (len(c.After) > 0 && !sameIndexGeneration(g, applied.Receipt.SeqNo, applied.Receipt.PrimaryTerm)) {
|
|
return errors.New("index changed after confirmed apply")
|
|
}
|
|
continue
|
|
}
|
|
intent, err := t.loadIndexEffect(c, "apply-intent")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if intent != nil {
|
|
if _, err := t.reconcileIndexClaim(ctx, c); err != nil {
|
|
return err
|
|
}
|
|
receipt, err := t.finalizeIndexClaim(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := t.saveIndexEffect(c, "applied", &receipt); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
g, err := t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !indexBytesEqual(g, c.Before) || (len(c.Before) > 0 && !sameIndexGeneration(g, c.SeqNo, c.PrimaryTerm)) {
|
|
return errors.New("index changed since frozen plan")
|
|
}
|
|
if err = t.saveIndexEffect(c, "apply-intent", nil); err != nil {
|
|
return err
|
|
}
|
|
receipt, err := t.applyIndexClaim(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = t.saveIndexEffect(c, "applied", &receipt); err != nil {
|
|
return err
|
|
}
|
|
g, err = t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !indexBytesEqual(g, c.After) || (len(c.After) > 0 && !sameIndexGeneration(g, receipt.SeqNo, receipt.PrimaryTerm)) {
|
|
return errors.New("index readback differs from receipt")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (t *ProductionLibraryTarget) mutatePlannedIndex(ctx context.Context, c libraryrelease.IndexWrite, body json.RawMessage, seq, term *int64, create bool) (indexReceipt, error) {
|
|
method := http.MethodPut
|
|
endpoint := "_doc"
|
|
if create {
|
|
endpoint = "_create"
|
|
}
|
|
if len(body) == 0 {
|
|
method = http.MethodDelete
|
|
}
|
|
query := url.Values{"refresh": {"true"}}
|
|
if !create {
|
|
if seq == nil || term == nil {
|
|
return indexReceipt{}, errors.New("index mutation lacks CAS")
|
|
}
|
|
query.Set("if_seq_no", fmt.Sprint(*seq))
|
|
query.Set("if_primary_term", fmt.Sprint(*term))
|
|
}
|
|
path := "/" + url.PathEscape(c.Index) + "/" + endpoint + "/" + url.PathEscape(c.ID) + "?" + query.Encode()
|
|
var payload any
|
|
if len(body) > 0 {
|
|
payload = body
|
|
}
|
|
var receipt indexReceipt
|
|
if err := t.search.requestJSON(ctx, method, path, payload, &receipt); err != nil {
|
|
return receipt, fmt.Errorf("index effect outcome unknown: %w", err)
|
|
}
|
|
if receipt.ID != c.ID || receipt.Index != c.Index || receipt.SeqNo == nil || receipt.PrimaryTerm == nil {
|
|
return receipt, errors.New("index mutation receipt incomplete")
|
|
}
|
|
if len(body) == 0 && receipt.Result != "deleted" || len(body) > 0 && receipt.Result != "created" && receipt.Result != "updated" {
|
|
return receipt, errors.New("index mutation receipt not confirmed")
|
|
}
|
|
return receipt, nil
|
|
}
|
|
func (t *ProductionLibraryTarget) restorePlannedIndex(ctx context.Context) error {
|
|
for _, c := range t.activePlan.Index {
|
|
intent, err := t.loadIndexEffect(c, "apply-intent")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if intent == nil {
|
|
continue
|
|
}
|
|
// Completed compensation is read back before attempting to reconcile an
|
|
// old marked apply; the marker was intentionally removed by restoration.
|
|
done, err := t.loadIndexEffect(c, "restored")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if done != nil {
|
|
g, err := t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !indexBytesEqual(g, c.Before) || (len(c.Before) > 0 && (done.Receipt == nil || !sameIndexGeneration(g, done.Receipt.SeqNo, done.Receipt.PrimaryTerm))) {
|
|
return errors.New("restored index changed")
|
|
}
|
|
continue
|
|
}
|
|
if reconciled, err := t.reconcileIndexCompensationDelete(ctx, c); err != nil {
|
|
return err
|
|
} else if reconciled {
|
|
continue
|
|
}
|
|
applied, err := t.loadIndexEffect(c, "applied")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if applied == nil {
|
|
receipt, err := t.reconcileIndexClaim(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
applied = &indexEffect{Plan: t.activePlan.Digest, Change: c, Receipt: &receipt}
|
|
}
|
|
if applied.Receipt == nil {
|
|
return errors.New("missing index compensation receipt")
|
|
}
|
|
g, err := t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
restoring, err := t.loadIndexEffect(c, "restore-intent")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if restoring != nil {
|
|
if len(c.Before) > 0 {
|
|
if err := t.restoreClaimedIndexBefore(ctx, c, *applied.Receipt, false); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
return errors.New("unknown index compensation requires reconciliation")
|
|
}
|
|
expected := c.After
|
|
if applied.Receipt.Marked {
|
|
expected, err = t.indexClaimBody(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !indexBytesEqual(g, expected) || (len(expected) > 0 && !sameIndexGeneration(g, applied.Receipt.SeqNo, applied.Receipt.PrimaryTerm)) {
|
|
return errors.New("another writer changed index; compensation refused")
|
|
}
|
|
if err = t.saveIndexEffect(c, "restore-intent", applied.Receipt); err != nil {
|
|
return err
|
|
}
|
|
if len(c.Before) > 0 {
|
|
if err := t.restoreClaimedIndexBefore(ctx, c, *applied.Receipt, true); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
receipt, err := t.mutatePlannedIndex(ctx, c, c.Before, applied.Receipt.SeqNo, applied.Receipt.PrimaryTerm, len(c.After) == 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = t.saveIndexEffect(c, "restored", &receipt); err != nil {
|
|
return err
|
|
}
|
|
g, err = t.observePlannedIndex(ctx, c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !indexBytesEqual(g, c.Before) {
|
|
return errors.New("index compensation readback failed")
|
|
}
|
|
}
|
|
return nil
|
|
}
|