Files
knowledge-governance/internal/objectgateway/acquire.go
T
dev cbc6b981c6
build / build (push) Successful in 1m56s
fix(library): 0.9.0 索引写入/归属校验不补 tenant_id,冲突可定位
- SearchIdentity 增加 NoTenantField,按目标 profile 决定是否注入 tenant_id
  (写入、归属预检、恢复搬运三条路径)
- 归属冲突与写集合不匹配写明具体字段,排查不用再猜
- 网关侧 acquire 冲突打印 plan/claim/owner 三项
2026-11-17 11:59:45 +08:00

164 lines
5.5 KiB
Go

package objectgateway
import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/jackc/pgx/v5"
"log"
"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)) {
// 冲突原因要能在服务端日志里定位:是谁的 plan、对不上哪一项。
log.Printf("gateway acquire conflict job=%s op=%s plan=%s claim=%s owner_ok=%t",
c.JobID, c.OperationID, 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
}