8d838d9cb4
build / build (push) Successful in 1m45s
执行时真实时钟与持久授权回读(撤销/过期即停并保留预备份)、写集合写前落盘、 租约与 attempt 身份、恢复队列与原发送身份分离、成功收尾必须释放维护后再登记终态。
177 lines
6.1 KiB
Go
177 lines
6.1 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
|
|
"knowledge-governance/internal/libraryrelease"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
// Compile local number requests without changing sequence state. Allocation is
|
|
// a separate, explicitly mutating phase after the backup and P0 are durable.
|
|
func (t *ProductionLibraryTarget) localIDRequests(archive *librarysnapshot.Archive, manifest librarysnapshot.Manifest) ([]libraryrelease.LocalIDMapping, error) {
|
|
var result []libraryrelease.LocalIDMapping
|
|
seen := map[string]bool{}
|
|
for _, part := range manifest.Parts {
|
|
if part.Kind != "database" && part.Kind != "wiki" {
|
|
continue
|
|
}
|
|
contract, ok := migrationIdentityContracts[part.Name]
|
|
if !ok {
|
|
if part.Count != 0 {
|
|
return nil, fmt.Errorf("unknown table %s", part.Name)
|
|
}
|
|
continue
|
|
}
|
|
if contract.LocalColumn == "" {
|
|
continue
|
|
}
|
|
reader, err := archive.OpenPart(part)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var rows []map[string]json.RawMessage
|
|
err = json.NewDecoder(reader).Decode(&rows)
|
|
reader.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, row := range rows {
|
|
raw, ok := row[contract.LocalColumn]
|
|
if !ok && contract.LocalColumn != "id" {
|
|
continue
|
|
} // older snapshots lacking the added column remain explicit legacy input
|
|
source, err := positiveBigint(raw)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s.%s: %w", part.Name, contract.LocalColumn, err)
|
|
}
|
|
key := part.Name + "/" + source
|
|
if seen[key] {
|
|
return nil, fmt.Errorf("duplicate local identity %s", key)
|
|
}
|
|
seen[key] = true
|
|
result = append(result, libraryrelease.LocalIDMapping{Table: part.Name, Column: contract.LocalColumn, Source: source})
|
|
}
|
|
}
|
|
sort.Slice(result, func(i, j int) bool {
|
|
if result[i].Table != result[j].Table {
|
|
return result[i].Table < result[j].Table
|
|
}
|
|
a, _ := strconv.ParseInt(result[i].Source, 10, 64)
|
|
b, _ := strconv.ParseInt(result[j].Source, 10, 64)
|
|
return a < b
|
|
})
|
|
return result, nil
|
|
}
|
|
|
|
func (t *ProductionLibraryTarget) allocateLocalIDs(ctx context.Context, set *libraryrelease.WriteSet) error {
|
|
// Preflight every sequence before consuming even one value. No setval and no
|
|
// rewind on failure: an unanchored allocation can leave holes, never aliases.
|
|
checked := map[string]bool{}
|
|
for _, mapping := range set.LocalIDs {
|
|
if checked[mapping.Table] {
|
|
continue
|
|
}
|
|
checked[mapping.Table] = true
|
|
contract := migrationIdentityContracts[mapping.Table]
|
|
var datatype, def string
|
|
err := t.pool.QueryRow(ctx, `SELECT format_type(a.atttypid,a.atttypmod),coalesce(pg_get_expr(d.adbin,d.adrelid),'') FROM pg_attribute a JOIN pg_class c ON c.oid=a.attrelid JOIN pg_namespace n ON n.oid=c.relnamespace LEFT JOIN pg_attrdef d ON d.adrelid=c.oid AND d.adnum=a.attnum WHERE n.nspname='public' AND c.relname=$1 AND a.attname=$2 AND NOT a.attisdropped`, mapping.Table, mapping.Column).Scan(&datatype, &def)
|
|
if err != nil || datatype != "bigint" || def != "nextval('"+contract.Sequence+"'::regclass)" {
|
|
return fmt.Errorf("local sequence schema mismatch: %s.%s", mapping.Table, mapping.Column)
|
|
}
|
|
var increment, maximum, cache int64
|
|
var cycle bool
|
|
if err := t.pool.QueryRow(ctx, `SELECT seqincrement,seqmax,seqcache,seqcycle FROM pg_sequence WHERE seqrelid=to_regclass($1)`, contract.Sequence).Scan(&increment, &maximum, &cache, &cycle); err != nil {
|
|
return err
|
|
}
|
|
if increment != 1 || cycle || cache != 1 {
|
|
return errors.New("unsupported local sequence allocation policy")
|
|
}
|
|
var last, maxUsed int64
|
|
var called bool
|
|
if err := t.pool.QueryRow(ctx, fmt.Sprintf(`SELECT last_value,is_called FROM %s`, contract.Sequence)).Scan(&last, &called); err != nil {
|
|
return err
|
|
}
|
|
if err := t.pool.QueryRow(ctx, fmt.Sprintf(`SELECT coalesce(max(%s),0) FROM %s`, mapping.Column, mapping.Table)).Scan(&maxUsed); err != nil {
|
|
return err
|
|
}
|
|
next := last
|
|
if called {
|
|
if last >= maximum {
|
|
return errors.New("local sequence exhausted")
|
|
}
|
|
next++
|
|
}
|
|
count := int64(0)
|
|
for _, m := range set.LocalIDs {
|
|
if m.Table == mapping.Table {
|
|
count++
|
|
}
|
|
}
|
|
if next <= maxUsed || count-1 > maximum-next {
|
|
return fmt.Errorf("local sequence behind occupied values or exhausted: %s", contract.Sequence)
|
|
}
|
|
}
|
|
raw, err := json.Marshal(set)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
if err := persistImmutableJSON(filepath.Join(t.config.ArchiveRoot, "plans-p0", hex.EncodeToString(sum[:])+".json"), raw); err != nil {
|
|
return err
|
|
}
|
|
for i := range set.LocalIDs {
|
|
mapping := &set.LocalIDs[i]
|
|
contract := migrationIdentityContracts[mapping.Table]
|
|
var allocated int64
|
|
if err := t.pool.QueryRow(ctx, `SELECT nextval($1::regclass)`, contract.Sequence).Scan(&allocated); err != nil {
|
|
return err
|
|
}
|
|
var occupied bool
|
|
if err := t.pool.QueryRow(ctx, fmt.Sprintf(`SELECT EXISTS(SELECT 1 FROM %s WHERE %s=$1)`, mapping.Table, mapping.Column), allocated).Scan(&occupied); err != nil {
|
|
return err
|
|
}
|
|
if occupied {
|
|
return errors.New("allocated local identity already occupied; maintenance/fence lost")
|
|
}
|
|
mapping.Target = strconv.FormatInt(allocated, 10)
|
|
if contract.Kind == "local-bigint" {
|
|
set.Rows = append(set.Rows, libraryrelease.WriteSetRow{Table: mapping.Table, ID: mapping.Target})
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *ProductionLibraryTarget) mapLocalID(table string, row map[string]json.RawMessage) (bool, error) {
|
|
if t.activePlan == nil || t.activeSnapshot != t.activePlan.TargetSnapshotID || t.preserveChunkSequence {
|
|
return false, nil
|
|
}
|
|
contract := migrationIdentityContracts[table]
|
|
if contract.LocalColumn == "" {
|
|
return false, nil
|
|
}
|
|
raw, ok := row[contract.LocalColumn]
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
source, err := positiveBigint(raw)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, mapping := range t.activePlan.LocalIDs {
|
|
if mapping.Table == table && mapping.Column == contract.LocalColumn && mapping.Source == source {
|
|
row[contract.LocalColumn] = json.RawMessage(mapping.Target)
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, fmt.Errorf("missing persisted local mapping: %s.%s", table, contract.LocalColumn)
|
|
}
|