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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 11:22:37 -08:00
1 parent de256fc5aa
commit 0eb8f0e174
2 files changed
+124

No files matched your search

@@ -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
}
@@ -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")
}
}