From 37bcd38af80302ac037a85e2ab90cd6cad41c70e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 03:27:39 -0800 Subject: [PATCH] test: a checked commit by change is reused only by that commit Controls: the changes mutated after the check (in the map, or through a reference the caller holds), a full check then a commit by change and the reverse, a commit after the session was aborted, and a commit by change with no check first each do their own preparation and lookups and refuse an object no session of theirs wrote; HEAD is unchanged. Positive controls: an unchecked commit by change of prepared objects commits, and a new check after an abort adopts the upload. A reuse check that ignores the changes and mode fails every refusal case. The capability test also checks that the bound wrapper forwards CASHeadDelta with its binding. Co-Authored-By: Claude Opus 5.5 --- .../layout_commit_by_change_test.go | 149 ++++++++++++++++++ .../durable/remote_capabilities_test.go | 15 ++ 2 files changed, 164 insertions(+) diff --git a/seaweed-block/core/frontend/chunkstore/layout_commit_by_change_test.go b/seaweed-block/core/frontend/chunkstore/layout_commit_by_change_test.go index b8005795c9..f7da8aa0e2 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_commit_by_change_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_commit_by_change_test.go @@ -2,6 +2,7 @@ package chunkstore import ( "context" + "errors" "fmt" "path" "reflect" @@ -208,3 +209,151 @@ func TestCommitByChangeOfZerosAndUnchangedRefs(t *testing.T) { t.Fatalf("HEAD names %v; want %v", got.Chunks, want) } } + +// unputRef names a chunk at index i that no session wrote: a commit naming +// it must refuse (not prepared), never commit. +func unputRef(r *LayoutRemote, i uint64) storage.ChunkRef { + data := []byte(fmt.Sprintf("never-put-%d", i)) + return storage.ChunkRef{Key: r.ChunkKey(i, layout.Sum(data)), SHA256: layout.Sum(data), Encoding: "full"} +} + +// expectRefusedAndHeadKept asserts err is a precondition refusal and HEAD +// is still the one the commit was proposed against. +func expectRefusedAndHeadKept(t *testing.T, ctx context.Context, r *LayoutRemote, err error, before storage.ChunkHead) { + t.Helper() + if !errors.Is(err, storage.ErrChunkHeadPrecondition) { + t.Fatalf("the commit was not refused as not prepared: %v", err) + } + h, gerr := r.GetHead(ctx) + if gerr != nil { + t.Fatal(gerr) + } + if h.RemoteLSN != before.RemoteLSN || !reflect.DeepEqual(h.Chunks, before.Chunks) { + t.Fatalf("a refused commit changed HEAD: at %d with %d chunks", h.RemoteLSN, len(h.Chunks)) + } +} + +// A passed check of a commit by change is reused only by exactly that +// commit, in that session. Another proposal does its own preparation and +// lookups, and refuses an object no session of it wrote, rather than +// committing what was checked: the changes mutated after the check (in the +// map, or through a reference it holds), the other mode with the same +// identity, a proposal after the session was aborted, and a commit by change +// with no check first. +func TestCheckedCommitByChangeIsNotReusedByAnotherProposal(t *testing.T) { + i := uint64(layout.ShardSize + 9) + next := storage.ChunkHead{Epoch: 1, RemoteLSN: 2} + for _, c := range []struct { + name string + run func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error + }{ + {"the changes map mutated after the check", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + ref := putChunk(t, ctx, r, i, "checked") + changes := map[uint64]*storage.ChunkRef{i: &ref} + if err := r.CheckCommitDelta(ctx, exp, next, changes); err != nil { + t.Fatal(err) + } + other := unputRef(r, i) + changes[i] = &other + _, err := r.CASHeadDelta(ctx, exp, next, changes) + return err + }}, + {"a reference mutated through the caller's pointer", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + ref := putChunk(t, ctx, r, i, "checked") + changes := map[uint64]*storage.ChunkRef{i: &ref} + if err := r.CheckCommitDelta(ctx, exp, next, changes); err != nil { + t.Fatal(err) + } + ref = unputRef(r, i) + _, err := r.CASHeadDelta(ctx, exp, next, changes) + return err + }}, + {"a full check, then a commit by change", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + ref := putChunk(t, ctx, r, i, "checked") + full := next + full.Chunks = storage.ApplyChunkChanges(h1.Chunks, map[uint64]*storage.ChunkRef{i: &ref}) + if err := r.CheckCommit(ctx, exp, full); err != nil { + t.Fatal(err) + } + other := unputRef(r, i) + _, err := r.CASHeadDelta(ctx, exp, next, map[uint64]*storage.ChunkRef{i: &other}) + return err + }}, + {"a check by change, then a full commit", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + ref := putChunk(t, ctx, r, i, "checked") + if err := r.CheckCommitDelta(ctx, exp, next, map[uint64]*storage.ChunkRef{i: &ref}); err != nil { + t.Fatal(err) + } + other := unputRef(r, i) + full := next + full.Chunks = storage.ApplyChunkChanges(h1.Chunks, map[uint64]*storage.ChunkRef{i: &other}) + _, err := r.CASHead(ctx, exp, full) + return err + }}, + {"the session aborted after the check", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + ref := putChunk(t, ctx, r, i, "checked") + changes := map[uint64]*storage.ChunkRef{i: &ref} + if err := r.CheckCommitDelta(ctx, exp, next, changes); err != nil { + t.Fatal(err) + } + r.Abort() + // The chunk was written by the ended session: the commit's own + // lookups (no adoption in the HEAD step) refuse it. + _, err := r.CASHeadDelta(ctx, exp, next, changes) + return err + }}, + {"a commit by change with no check first", func(t *testing.T, ctx context.Context, r *LayoutRemote, exp storage.ChunkHeadVersion, h1 storage.ChunkHead) error { + other := unputRef(r, i) + _, err := r.CASHeadDelta(ctx, exp, next, map[uint64]*storage.ChunkRef{i: &other}) + return err + }}, + } { + t.Run(c.name, func(t *testing.T) { + ctx, _, r, h1 := threeShardVolume(t) + exp := storage.ChunkHeadVersion{Gen: h1.Gen, Epoch: 1} + expectRefusedAndHeadKept(t, ctx, r, c.run(t, ctx, r, exp, h1), h1) + }) + } +} + +// The positive controls of the same boundaries: a commit by change of +// prepared objects with no check first commits (its lookups run in the HEAD +// step), and after an abort a new check adopts the ended session's upload +// and the commit then succeeds. +func TestCommitByChangeWithoutACheckAndAfterAnAbort(t *testing.T) { + i := uint64(layout.ShardSize + 9) + next := storage.ChunkHead{Epoch: 1, RemoteLSN: 2} + t.Run("no check first", func(t *testing.T) { + ctx, _, r, h1 := threeShardVolume(t) + ref := putChunk(t, ctx, r, i, "unchecked") + changes := map[uint64]*storage.ChunkRef{i: &ref} + if _, err := r.CASHeadDelta(ctx, storage.ChunkHeadVersion{Gen: h1.Gen, Epoch: 1}, next, changes); err != nil { + t.Fatal(err) + } + h, err := r.GetHead(ctx) + if err != nil || !reflect.DeepEqual(h.Chunks, storage.ApplyChunkChanges(h1.Chunks, changes)) { + t.Fatalf("HEAD after an unchecked commit by change: %v %v", h.Chunks, err) + } + }) + t.Run("a new check after an abort", func(t *testing.T) { + ctx, _, r, h1 := threeShardVolume(t) + ref := putChunk(t, ctx, r, i, "aborted") + changes := map[uint64]*storage.ChunkRef{i: &ref} + r.Abort() + h, err := r.GetHead(ctx) // the engine's re-read after an abort + if err != nil { + t.Fatal(err) + } + exp := storage.ChunkHeadVersion{Gen: h.Gen, Epoch: 1} + if err := r.CheckCommitDelta(ctx, exp, next, changes); err != nil { + t.Fatal(err) + } + if _, err := r.CASHeadDelta(ctx, exp, next, changes); err != nil { + t.Fatal(err) + } + got, err := r.GetHead(ctx) + if err != nil || !reflect.DeepEqual(got.Chunks, storage.ApplyChunkChanges(h1.Chunks, changes)) { + t.Fatalf("HEAD after the adopted commit: %v %v", got.Chunks, err) + } + }) +} diff --git a/seaweed-block/core/frontend/durable/remote_capabilities_test.go b/seaweed-block/core/frontend/durable/remote_capabilities_test.go index 0d941f00e8..feb05bd7b2 100644 --- a/seaweed-block/core/frontend/durable/remote_capabilities_test.go +++ b/seaweed-block/core/frontend/durable/remote_capabilities_test.go @@ -165,4 +165,19 @@ func TestCloseBoundRemoteForwardsCapabilitiesAndClose(t *testing.T) { if !errors.Is(probe.got.Err(), context.Canceled) { t.Fatal("GetHeadRoot not canceled on Close") } + _ = r.CheckCommitDelta(context.Background(), expected, next, changes) + if !errors.Is(probe.got.Err(), context.Canceled) { + t.Fatal("CheckCommitDelta not canceled on Close") + } + probe.got = nil + if _, err := r.CASHeadDelta(context.Background(), expected, next, changes); !errors.Is(err, errProbeCheck) || probe.got == nil || !errors.Is(probe.got.Err(), context.Canceled) { + t.Fatalf("CASHeadDelta not forwarded with the Close binding: err=%v", err) + } +} + +// CASHeadDelta records the context it is called with (the wrapper's binding). +func (r *capabilityProbe) CASHeadDelta(c context.Context, e storage.ChunkHeadVersion, n storage.ChunkHead, changes map[uint64]*storage.ChunkRef) (uint64, error) { + r.got = c + r.changes = changes + return 0, errProbeCheck }