Files
knowledge-governance/internal/objectgateway/acquire.go
T
姚章浩 5eef13b615 feat(object-gateway): 统一对象写网关与不可变治理日志
方案A:所有对象写入经 loopback 网关,持久计划/grant/intent/逐版本 receipt 与
attempt→grant delivery 落在治理 PG;scope 由 provider+endpoint+bucket 决定,
同一物理桶只允许一个写域。版本 append 不等于 CAS,未锚定计划在首笔业务写前拒绝。

本提交只含网关与其适配层接线、协议文档;生产独占、凭据切换与桶设置未改动。
2026-11-01 11:35:05 +08:00

160 lines
5.2 KiB
Go

package objectgateway
import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/jackc/pgx/v5"
"math"
)
// Claim names an already persisted application attempt and write-set anchor.
// It never carries keys, bodies, a replacement plan or a chosen gateway epoch.
type Claim struct {
JobID string `json:"job_id"`
OperationID string `json:"operation_id"`
AttemptEpoch int64 `json:"attempt_epoch"`
Owner string `json:"owner"`
PlanDigest string `json:"plan_digest"`
RecoveryJobID string `json:"recovery_job_id,omitempty"`
RecoveryOperationID string `json:"recovery_operation_id,omitempty"`
}
type PlanAuthority interface {
Load(context.Context, Claim) (Plan, error)
}
func (g *Gateway) Acquire(ctx context.Context, c Claim) (Grant, error) {
if g.Authority == nil {
return Grant{}, ErrAuthority
}
if c.JobID == "" || c.OperationID == "" || c.Owner == "" || c.AttemptEpoch < 1 || !validHash(c.PlanDigest) {
return Grant{}, ErrEpoch
}
if e := g.Ready(ctx); e != nil {
return Grant{}, e
}
plan, e := g.Authority.Load(ctx, c)
if e != nil {
return Grant{}, e
}
if plan.Digest != c.PlanDigest || plan.Owner != Digest([]byte(c.JobID)) {
return Grant{}, ErrConflict
}
if e = g.InstallPlan(ctx, plan); e != nil {
return Grant{}, e
}
// Recover only sends in this exact frozen plan. No absent observation can
// settle an unknown send; the locked advancement below still checks all intents.
rows, e := g.Pool.Query(ctx, `SELECT i.request FROM kg_object_gateway_intents i
LEFT JOIN kg_object_gateway_receipts r ON r.scope=i.scope AND r.operation=i.operation
WHERE i.scope=$1 AND r.operation IS NULL`, g.Scope)
if e != nil {
return Grant{}, ErrPersistence
}
var pending []Request
for rows.Next() {
var raw []byte
var request Request
if rows.Scan(&raw) != nil || json.Unmarshal(raw, &request) != nil {
rows.Close()
return Grant{}, ErrPersistence
}
if request.Grant.Plan == plan.Digest {
pending = append(pending, request)
}
}
e = rows.Err()
rows.Close()
if e != nil {
return Grant{}, ErrPersistence
}
for _, request := range pending {
if request.Grant.Owner != plan.Owner {
return Grant{}, ErrConflict
}
if _, e = g.Reconcile(ctx, request); e != nil {
return Grant{}, e
}
}
tx, e := g.Pool.Begin(ctx)
if e != nil {
return Grant{}, ErrPersistence
}
defer tx.Rollback(ctx)
if _, e = tx.Exec(ctx, "INSERT INTO kg_object_gateway_scopes(scope) VALUES($1) ON CONFLICT DO NOTHING", g.Scope); e != nil {
return Grant{}, ErrPersistence
}
var epoch int64
if e = tx.QueryRow(ctx, "SELECT epoch FROM kg_object_gateway_scopes WHERE scope=$1 FOR UPDATE", g.Scope).Scan(&epoch); e != nil {
return Grant{}, ErrPersistence
}
// Recheck after serialization. This is admission evidence, not an OSS fence:
// a persisted uncertain object intent still prevents every epoch transition.
verified, e := g.Authority.Load(ctx, c)
if e != nil {
return Grant{}, e
}
frozen, _ := json.Marshal(plan)
again, _ := json.Marshal(verified)
if !bytes.Equal(frozen, again) {
return Grant{}, ErrConflict
}
raw, _ := json.Marshal(c)
var oldClaim, oldGrant []byte
e = tx.QueryRow(ctx, "SELECT claim,grant_body FROM kg_object_gateway_deliveries WHERE scope=$1 AND job=$2 AND attempt_epoch=$3", g.Scope, c.JobID, c.AttemptEpoch).Scan(&oldClaim, &oldGrant)
if e == nil {
var grant Grant
if !bytes.Equal(raw, oldClaim) || json.Unmarshal(oldGrant, &grant) != nil || !grant.valid() {
return Grant{}, ErrConflict
}
if grant.Epoch != epoch {
return Grant{}, ErrEpoch
}
return grant, nil
}
if !errors.Is(e, pgx.ErrNoRows) {
return Grant{}, ErrPersistence
}
var firstRaw []byte
firstErr := tx.QueryRow(ctx, "SELECT claim FROM kg_object_gateway_deliveries WHERE scope=$1 AND job=$2 ORDER BY attempt_epoch LIMIT 1", g.Scope, c.JobID).Scan(&firstRaw)
if firstErr == nil {
var first Claim
if json.Unmarshal(firstRaw, &first) != nil {
return Grant{}, ErrPersistence
}
if first.JobID != c.JobID || first.OperationID != c.OperationID || first.PlanDigest != c.PlanDigest {
return Grant{}, ErrConflict
}
} else if !errors.Is(firstErr, pgx.ErrNoRows) {
return Grant{}, ErrPersistence
}
var stale bool
if tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM kg_object_gateway_deliveries WHERE scope=$1 AND job=$2 AND attempt_epoch>=$3)", g.Scope, c.JobID, c.AttemptEpoch).Scan(&stale) != nil {
return Grant{}, ErrPersistence
}
if stale || epoch == math.MaxInt64 {
return Grant{}, ErrEpoch
}
next := Grant{Epoch: epoch + 1, Owner: plan.Owner, Plan: plan.Digest}
if e = g.advanceLocked(ctx, tx, epoch, next); e != nil {
return Grant{}, e
}
grantRaw, _ := json.Marshal(next)
if _, e = tx.Exec(ctx, "INSERT INTO kg_object_gateway_deliveries(scope,job,attempt_epoch,claim,grant_epoch,grant_body) VALUES($1,$2,$3,$4,$5,$6)", g.Scope, c.JobID, c.AttemptEpoch, raw, next.Epoch, grantRaw); e != nil {
return Grant{}, ErrPersistence
}
if tx.Commit(ctx) != nil {
return Grant{}, ErrPersistence
}
return next, nil
}
func (c *Client) Acquire(ctx context.Context, claim Claim) (Grant, error) {
var out Grant
e := c.call(ctx, "/v1/acquire", claim, &out)
if e == nil && (!out.valid() || out.Plan != claim.PlanDigest || out.Owner != Digest([]byte(claim.JobID))) {
e = ErrConflict
}
return out, e
}