From 0eb8f0e1741792bb7075276caed8c3d11f2e4162 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 10:51:10 -0800 Subject: [PATCH] storage: sameProposalByChange, the O(C) "proposal landed" decision (not wired yet) A commit by change landed iff HEAD's generation is past the expected base, every identity field the full comparison has is equal, every changed index names the new reference (nothing for a zeroing) and every candidate the cut marked equal names the base reference its mark assumed; unchanged, unmarked indices are not compared. The rule is SegCut's existing decision (no new row). TestSameProposalByChange: each condition broken alone is not landed, a replay is landed. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_proposal_by_change.go | 55 +++++++++++++++ .../storage/chunk_proposal_by_change_test.go | 69 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_proposal_by_change.go create mode 100644 seaweed-block/core/storage/chunk_proposal_by_change_test.go diff --git a/seaweed-block/core/storage/chunk_proposal_by_change.go b/seaweed-block/core/storage/chunk_proposal_by_change.go new file mode 100644 index 0000000000..3726bf7382 --- /dev/null +++ b/seaweed-block/core/storage/chunk_proposal_by_change.go @@ -0,0 +1,55 @@ +package storage + +// sameProposalByChange decides whether a commit by change landed in cur, +// without comparing whole chunk maps (V4-B PR 4; the rule is SegCut's +// "proposal landed" decision, the model's existing row): O(C) in the cut's +// changed and equal-marked chunks. The proposal landed iff +// +// 1. cur's generation is past the base the CAS expected (baseGen); +// 2. every identity field the full comparison has is equal: epoch, holder, +// position, the detached state and who detached, the chunk size and the +// manifest's chunk count; +// 3. for every changed index cur names exactly the new reference (nothing, +// for a chunk the change made zeros); +// 4. for every candidate the cut marked equal to its base reference, cur +// names that base reference (a mark assumes it; without the check an +// index another writer changed would be marked published under a hash +// HEAD does not name). +// +// Indices the cut neither changed nor marked carry marks set by earlier +// landings and are not compared. cur's chunks are read through ref (a lookup +// in whatever representation the caller has: a map, the ref table). +func sameProposalByChange(cur, ident chunkHead, baseGen uint64, ref func(idx uint64) (chunkRef, bool), + changes map[uint64]*chunkRef, marked map[uint64]chunkRef) bool { + if cur.Gen <= baseGen { + return false + } + if cur.Epoch != ident.Epoch || cur.Holder != ident.Holder || cur.RemoteLSN != ident.RemoteLSN || + cur.ChunkBytes != ident.ChunkBytes || cur.ManifestChunkCount != ident.ManifestChunkCount { + return false + } + if chunkHeadIsDetached(cur) != chunkHeadIsDetached(ident) || (cur.DetachedBy == nil) != (ident.DetachedBy == nil) || + (cur.DetachedBy != nil && (cur.DetachedBy.Holder != ident.DetachedBy.Holder || cur.DetachedBy.Epoch != ident.DetachedBy.Epoch)) { + return false + } + same := func(a, b chunkRef) bool { return a.Key == b.Key && a.SHA256 == b.SHA256 && a.Encoding == b.Encoding } + for idx, want := range changes { + got, ok := ref(idx) + if want == nil { + if ok { + return false + } + continue + } + if !ok || !same(got, *want) { + return false + } + } + for idx, base := range marked { + got, ok := ref(idx) + if !ok || !same(got, base) { + return false + } + } + return true +} diff --git a/seaweed-block/core/storage/chunk_proposal_by_change_test.go b/seaweed-block/core/storage/chunk_proposal_by_change_test.go new file mode 100644 index 0000000000..41470508e2 --- /dev/null +++ b/seaweed-block/core/storage/chunk_proposal_by_change_test.go @@ -0,0 +1,69 @@ +package storage + +import ( + "crypto/sha256" + "fmt" + "testing" +) + +// sameProposalByChange is landed iff the generation advanced past the base, +// every identity field equals, every changed index names the new reference +// (nothing for a zeroing) and every equal-marked candidate names the base +// reference its mark assumed; unchanged, unmarked indices are not compared. +// Each condition broken alone is not landed; a replay of the identical +// proposal is landed whichever copy won. +func TestSameProposalByChange(t *testing.T) { + r := func(tag string) chunkRef { + s := sha256.Sum256([]byte(tag)) + return chunkRef{Key: fmt.Sprintf("k-%s", tag), SHA256: s, Encoding: "full"} + } + newRef, base5, other := r("new"), r("base5"), r("other") + ident := chunkHead{Epoch: 3, Holder: "a", RemoteLSN: 90, ChunkBytes: 4096, ManifestChunkCount: 64, State: chunkHeadActive} + changes := map[uint64]*chunkRef{2: &newRef, 4: nil} // chunk 2 rewritten, chunk 4 zeroed + marked := map[uint64]chunkRef{5: base5} // chunk 5 compared equal to its base ref + landedHead := func() (chunkHead, map[uint64]chunkRef) { + h := ident + h.Gen = 8 + return h, map[uint64]chunkRef{2: newRef, 5: base5, 7: other} // 7: neither changed nor marked + } + check := func(h chunkHead, m map[uint64]chunkRef) bool { + return sameProposalByChange(h, ident, 7, func(i uint64) (chunkRef, bool) { r, ok := m[i]; return r, ok }, changes, marked) + } + h, m := landedHead() + if !check(h, m) { + t.Fatal("the landed proposal is not found landed") + } + m[7] = r("elsewhere") // an index the cut neither changed nor marked: not compared + if !check(h, m) { + t.Fatal("an unchanged, unmarked index decided the outcome") + } + for name, mutate := range map[string]func(h *chunkHead, m map[uint64]chunkRef){ + "generation not past the base": func(h *chunkHead, _ map[uint64]chunkRef) { h.Gen = 7 }, + "another epoch": func(h *chunkHead, _ map[uint64]chunkRef) { h.Epoch = 4 }, + "another holder": func(h *chunkHead, _ map[uint64]chunkRef) { h.Holder = "b" }, + "another position": func(h *chunkHead, _ map[uint64]chunkRef) { h.RemoteLSN = 91 }, + "another chunk size": func(h *chunkHead, _ map[uint64]chunkRef) { h.ChunkBytes = 8192 }, + "another manifest count": func(h *chunkHead, _ map[uint64]chunkRef) { h.ManifestChunkCount = 65 }, + "detached": func(h *chunkHead, _ map[uint64]chunkRef) { + h.State = chunkHeadDetached + h.DetachedBy = &chunkDetachedBy{Holder: "a", Epoch: 3} + }, + "a changed index not written": func(_ *chunkHead, m map[uint64]chunkRef) { m[2] = other }, + "a changed index missing": func(_ *chunkHead, m map[uint64]chunkRef) { delete(m, 2) }, + "a zeroed index still named": func(_ *chunkHead, m map[uint64]chunkRef) { m[4] = other }, + "an equal-marked index changed": func(_ *chunkHead, m map[uint64]chunkRef) { m[5] = other }, + "an equal-marked index now nothing": func(_ *chunkHead, m map[uint64]chunkRef) { delete(m, 5) }, + } { + h, m := landedHead() + mutate(&h, m) + if check(h, m) { + t.Errorf("%s: found landed", name) + } + } + // A replay of the identical proposal: whichever copy won, cur is the same. + h1, m1 := landedHead() + h1.Gen = 9 // the re-proposal landed one generation later + if !check(h1, m1) { + t.Fatal("a replay of the identical proposal is not found landed") + } +}