Files
knowledge-governance/internal/objectgateway/takeover.go
姚章浩 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

61 lines
2.1 KiB
Go

package objectgateway
import (
"context"
"encoding/json"
"errors"
"github.com/jackc/pgx/v5"
)
// Resolve preserves the immutable send epoch and backend claim of an operation.
// An absent intent is not a claim about an earlier send; Execute still reserves
// atomically and refuses stale authorizations before any new side effect.
func (g *Gateway) Resolve(ctx context.Context, desired Request) (Request, error) {
if e := desired.validate(); e != nil {
return Request{}, e
}
if e := g.Ready(ctx); e != nil {
return Request{}, e
}
var raw []byte
e := g.Pool.QueryRow(ctx, "SELECT request FROM kg_object_gateway_intents WHERE scope=$1 AND operation=$2", g.Scope, desired.Operation).Scan(&raw)
if errors.Is(e, pgx.ErrNoRows) {
return desired, nil
}
if e != nil {
return Request{}, ErrPersistence
}
var original Request
if json.Unmarshal(raw, &original) != nil || original.validate() != nil {
return Request{}, ErrPersistence
}
if !sameOperation(original, desired) || original.Grant.Epoch > desired.Grant.Epoch {
return Request{}, ErrConflict
}
return original, nil
}
func sameOperation(a, b Request) bool { a.Grant.Epoch = b.Grant.Epoch; return a == b }
func (g *Gateway) ExecuteAuthorized(ctx context.Context, authorization Grant, r Request, body []byte) (Receipt, error) {
if !authorization.valid() || authorization.Owner != r.Grant.Owner || authorization.Plan != r.Grant.Plan || authorization.Epoch < r.Grant.Epoch {
return Receipt{}, ErrEpoch
}
return g.execute(ctx, r, body, &authorization)
}
func (c *Client) ExecuteAuthorized(ctx context.Context, authorization Grant, r Request, body []byte) (Receipt, error) {
var out Receipt
e := c.call(ctx, "/v1/execute", ExecuteRequest{Request: r, Body: body, Authorization: &authorization}, &out)
if e == nil && (out.Request != r || !out.valid()) {
e = ErrConflict
}
return out, e
}
func (c *Client) Resolve(ctx context.Context, desired Request) (Request, error) {
var out Request
e := c.call(ctx, "/v1/resolve", desired, &out)
if e == nil && (out.validate() != nil || !sameOperation(out, desired) || out.Grant.Epoch > desired.Grant.Epoch) {
e = ErrConflict
}
return out, e
}