Files
knowledge-governance/internal/objectgateway/acquire_integration_test.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

123 lines
4.6 KiB
Go

package objectgateway_test
import (
"context"
"errors"
g "knowledge-governance/internal/objectgateway"
"testing"
)
type currentPlanAuthority struct {
claim g.Claim
plan g.Plan
}
func (a *currentPlanAuthority) Load(_ context.Context, c g.Claim) (g.Plan, error) {
if c != a.claim {
return g.Plan{}, g.ErrEpoch
}
return a.plan, nil
}
func TestGatewayAcquisitionPersistsAttemptDelivery(t *testing.T) {
ctx, pool, backend := fixture(t)
claim := g.Claim{JobID: "job-acquire", OperationID: "operation-acquire", AttemptEpoch: 1, Owner: "worker-1", PlanDigest: g.Digest([]byte(t.Name()))}
expected := g.Grant{Epoch: 1, Owner: g.Digest([]byte(claim.JobID)), Plan: claim.PlanDigest}
req := request("acquired-object", expected)
authority := &currentPlanAuthority{claim, planFor(expected, req)}
gateway := &g.Gateway{Pool: pool, Scope: backend.Bucket, Backend: backend, Isolation: verifiedFixture{}, MaxBytes: 1 << 20, Authority: authority}
grant, e := gateway.Acquire(ctx, claim)
if e != nil || grant != expected {
t.Fatal("anchored active attempt grant unavailable", e)
}
replay, e := gateway.Acquire(ctx, claim)
if e != nil || replay != grant {
t.Fatal("grant delivery replay changed epoch", e)
}
oldClaim := claim
claim.AttemptEpoch = 2
claim.Owner = "worker-2"
authority.claim = claim
next, e := gateway.Acquire(ctx, claim)
if e != nil || next.Epoch != 2 {
t.Fatal("next active attempt", e)
}
if _, e = gateway.Acquire(ctx, oldClaim); !errors.Is(e, g.ErrEpoch) {
t.Fatal("stale attempt acquired grant", e)
}
replacement := claim
replacement.AttemptEpoch++
replacement.PlanDigest = g.Digest([]byte("replacement-plan"))
authority.claim = replacement
authority.plan.Digest = replacement.PlanDigest
if _, e = gateway.Acquire(ctx, replacement); !errors.Is(e, g.ErrConflict) {
t.Fatal("new attempt replaced original plan", e)
}
authority.claim = claim
authority.plan.Digest = claim.PlanDigest
req.Grant = next
receipt, e := gateway.ExecuteAuthorized(ctx, next, req, []byte("gateway fixture acquired-object"))
if e != nil {
t.Fatal(e)
}
cleanup(t, ctx, gateway, receipt)
var count int
if e = pool.QueryRow(ctx, "SELECT count(*) FROM kg_object_gateway_deliveries WHERE scope=$1", gateway.Scope).Scan(&count); e != nil || count != 2 {
t.Fatal("immutable delivery count", count, e)
}
for _, statement := range []string{"UPDATE kg_object_gateway_deliveries SET job=job", "DELETE FROM kg_object_gateway_deliveries", "TRUNCATE kg_object_gateway_deliveries"} {
if _, e = pool.Exec(ctx, statement); e == nil {
t.Fatal("mutable grant delivery", statement)
}
}
}
func TestGatewayAcquisitionCannotHandOverUnknownSend(t *testing.T) {
ctx, pool, real := fixture(t)
backend := &stoppedBeforeSend{Backend: real}
claim := g.Claim{JobID: "job-held", OperationID: "operation-held", AttemptEpoch: 1, Owner: "worker-first", PlanDigest: g.Digest([]byte(t.Name()))}
expected := g.Grant{Epoch: 1, Owner: g.Digest([]byte(claim.JobID)), Plan: claim.PlanDigest}
req := request("acquire-held", expected)
body := []byte("gateway fixture acquire-held")
authority := &currentPlanAuthority{claim, planFor(expected, req)}
gateway := &g.Gateway{Pool: pool, Scope: real.Bucket, Backend: backend, Isolation: verifiedFixture{}, MaxBytes: 1 << 20, Authority: authority}
if _, e := gateway.Acquire(ctx, claim); e != nil {
t.Fatal(e)
}
if _, e := gateway.ExecuteAuthorized(ctx, expected, req, body); !errors.Is(e, g.ErrHeld) {
t.Fatal(e)
}
claim.AttemptEpoch++
claim.Owner = "worker-next"
authority.claim = claim
for i := 0; i < 2; i++ {
if _, e := gateway.Acquire(ctx, claim); !errors.Is(e, g.ErrHeld) {
t.Fatal("absent send handed over", e)
}
}
if backend.calls != 1 {
t.Fatal("control acquisition re-sent PUT")
}
var count int
if e := pool.QueryRow(ctx, "SELECT count(*) FROM kg_object_gateway_deliveries WHERE scope=$1", gateway.Scope).Scan(&count); e != nil || count != 1 {
t.Fatal("unknown send advanced delivery", count, e)
}
// Deliver the previously uncertain request late, using its exact immutable claim.
if _, e := real.Put(ctx, req.Key, body, req.Claim()); e != nil {
t.Fatal(e)
}
next, e := gateway.Acquire(ctx, claim)
if e != nil || next.Epoch != 2 {
t.Fatal("committed send was not reconciled before handover", e)
}
receipt, e := gateway.ExecuteAuthorized(ctx, next, req, body)
if e != nil || receipt.Request != req {
t.Fatal(e)
}
if backend.calls != 1 {
t.Fatal("reconciled send was retried")
}
receipt.Request.Grant = next
cleanup(t, ctx, gateway, receipt)
t.Log("new controller attempt stayed held while absent; late committed original recovered without replay PUT")
}