Files
knowledge-governance/internal/objectgateway/acquire_integration_test.go
T
姚章浩 a55319d972
build / build (push) Failing after 2m29s
fix(objectgateway): 取消整桶独占前置并按维护停写更新
现象:共享桶部署下网关启动即报 gateway_auditor_key_required,残留过期
审计文件则每次 Execute 报 gateway_exclusive_writer_evidence_unverified;
用户已改为“停目标写入容器→更新→启动”的维护式更新。

根因:Gateway.Ready 强制 Isolation.Verify,main 强制 Ed25519 公钥,等于
把整桶独占证明当作发布前置;这与共享桶、沿用原 AK 的实际部署矛盾。

取舍:删除审计校验与相关 env 依赖,保留 journal、治理 PG 只读 authority、
plan/owner/attempt/epoch 绑定、intent-before-send、单次写与精确版本回滚。
create 仅在精确版本读回且仍为 current 时提交;发现外部 latest 只返回冲突,
不在 confirm 阶段删除(journal create intent 已提交,删除会变成无 delete
intent/receipt 的未记账副作用并让 intent 永久 held)。同 key 不自动合并。

证据:真实二进制无审计/残留审计两环境通过;kg-pgtest-writeset 一次性库 +
随机 MinIO 桶跑 14 个网关用例(含共享桶共存写者、同 key 既有对象拒绝、
精确回滚)全过;umask 022 下 make verify 本地必需项全 success。
2026-11-04 09:45:47 +08:00

123 lines
4.5 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, 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, 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")
}