storage: the ref table's fill replaces its segment and refuses out of range; its space test owns its source

FillSegment(first, end, refs) makes a segment's range exactly refs, so a
re-fill drops a reference the new HEAD no longer names; Set and
FillSegment refuse an index beyond the volume or outside the segment
(errRefBeyondVolume) and change nothing, so an attach refuses a malformed
HEAD. The space test drops its source HEAD before the reading, so a table
that aliased its keys would be charged for them (control: a shallow
flat-map copy measures ~287 B/chunk by the same method).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 08:44:50 -08:00
1 parent d100675112
commit ce490fdb39
2 files changed
+120 -22

No files matched your search

+37 -10
View File
@@ -2,9 +2,15 @@ package storage
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
)
// errRefBeyondVolume refuses a reference at an index the volume does not
// have: a malformed HEAD, which the attach refuses, never trims.
var errRefBeyondVolume = errors.New("storage: chunk reference beyond the volume")
// refTable is the holder's copy of the references HEAD names, one entry a
// chunk, in fixed width (r2.777): what the flat chunk map held, without a
// per-entry key string or map overhead. It sits behind a narrow interface
@@ -67,10 +73,10 @@ func canonicalSplit(i uint64, ref chunkRef) (string, bool) {
return ref.Key[:len(ref.Key)-len(suffix)], true
}
// Set names ref at chunk index i.
func (t *refTable) Set(i uint64, ref chunkRef) {
// Set names ref at chunk index i; an index beyond the volume is refused.
func (t *refTable) Set(i uint64, ref chunkRef) error {
if i >= uint64(len(t.prefix)) {
return
return fmt.Errorf("%w: index %d, the volume has %d chunks", errRefBeyondVolume, i, len(t.prefix))
}
if t.prefix[i] == 0 {
t.n++
@@ -86,13 +92,14 @@ func (t *refTable) Set(i uint64, ref chunkRef) {
delete(t.other, i)
}
t.prefix[i], t.sums[i] = id, ref.SHA256
return
return nil
}
if t.other == nil {
t.other = map[uint64]chunkRef{}
}
t.prefix[i], t.sums[i] = prefixOther, [32]byte{}
t.other[i] = ref
return nil
}
// Delete makes chunk index i named by nothing (HEAD reads it as zeros).
@@ -107,15 +114,35 @@ func (t *refTable) Delete(i uint64) {
t.n--
}
// FillSegment names every reference one segment of HEAD holds (the attach's
// fill, one segment at a time).
func (t *refTable) FillSegment(refs map[uint64]chunkRef) {
for i, r := range refs {
t.Set(i, r)
// FillSegment makes the chunk range [first, end) of one segment of HEAD
// exactly refs (the attach's fill, one segment at a time): an index of the
// range refs does not name reads as zeros afterwards, even if an earlier fill
// named it (a re-fill never keeps a reference HEAD dropped). A reference
// outside the range, or a range beyond the volume, is refused and changes
// nothing. O(end - first).
func (t *refTable) FillSegment(first, end uint64, refs map[uint64]chunkRef) error {
if first > end || end > uint64(len(t.prefix)) {
return fmt.Errorf("%w: segment range [%d, %d), the volume has %d chunks", errRefBeyondVolume, first, end, len(t.prefix))
}
for i := range refs {
if i < first || i >= end {
return fmt.Errorf("%w: index %d is outside its segment's range [%d, %d)", errRefBeyondVolume, i, first, end)
}
}
for i := first; i < end; i++ {
if r, ok := refs[i]; ok {
if err := t.Set(i, r); err != nil {
return err
}
} else {
t.Delete(i)
}
}
return nil
}
// each calls f for every named chunk, in index order.
// each calls f for every named chunk, in index order: O(S), the whole
// volume (for the paths that need the whole set, never for a lookup).
func (t *refTable) each(f func(i uint64, ref chunkRef)) {
for i := range t.prefix {
if t.prefix[i] != 0 {
@@ -2,6 +2,7 @@ package storage
import (
"crypto/sha256"
"errors"
"fmt"
"reflect"
"runtime"
@@ -24,12 +25,14 @@ func TestRefTableIsExactForEveryReferenceShape(t *testing.T) {
2: canon("src", 2, sum("c")), // a restore source: a second owner prefix
3: {Key: "chunks/e1/c3/opaque-1", SHA256: sum("d"), Encoding: "full"},
4: {Key: canon("v1", 4, sum("e")).Key, SHA256: sum("e"), Encoding: "zstd"},
5: canon("v1", 6, sum("f")), // the key names another index
5: canon("v1", 6, sum("f")), // the key names another index
6: {Key: canon("v1", 6, sum("g")).Key, SHA256: sum("h"), Encoding: "full"}, // the key names another hash
15: canon("v1", 15, sum("z")),
}
for i, r := range refs {
tab.Set(i, r)
if err := tab.Set(i, r); err != nil {
t.Fatal(err)
}
}
if tab.Len() != len(refs) {
t.Fatalf("Len %d, want %d", tab.Len(), len(refs))
@@ -45,8 +48,8 @@ func TestRefTableIsExactForEveryReferenceShape(t *testing.T) {
t.Fatalf("%d owner prefixes, %d whole refs; want 2 and 4 (only the non-canonical kept whole)", len(tab.prefixes), len(tab.other))
}
// Replacing a whole ref by a canonical one, and the reverse, keeps one entry.
tab.Set(3, canon("v1", 3, sum("d2")))
tab.Set(0, chunkRef{Key: "opaque", SHA256: sum("x"), Encoding: "full"})
_ = tab.Set(3, canon("v1", 3, sum("d2")))
_ = tab.Set(0, chunkRef{Key: "opaque", SHA256: sum("x"), Encoding: "full"})
if r, _ := tab.Get(3); r != canon("v1", 3, sum("d2")) {
t.Fatalf("chunk 3 after a canonical replacement: %+v", r)
}
@@ -67,20 +70,88 @@ func TestRefTableIsExactForEveryReferenceShape(t *testing.T) {
}
}
// heldAfterSourceDropped is the heap per chunk that build's result holds
// once the HEAD it was built from is gone: the source is allocated after the
// first reading and dropped before the second, so whatever the result still
// references of it (an aliased key string) is charged to the result.
func heldAfterSourceDropped(chunks uint64, build func(chunkHead) any) float64 {
before := heapNow()
src := fullHead(chunks, "v1")
held := build(src)
src = chunkHead{}
per := float64(int64(heapNow())-int64(before)) / float64(chunks)
runtime.KeepAlive(held)
return per
}
// The table's held heap at the 1 TiB chunk count with canonical keys: at
// most 40 B per chunk (32 B SHA-256 + a 2 B owner index, and the slices'
// headers), against the flat map's ~287 B with its keys.
// headers), against the flat map's ~287 B with its keys. The source HEAD is
// dropped before the reading, so a table that aliased its keys instead of
// holding its own bytes would be charged for them: the control, a shallow
// copy of the flat map (which shares the source's key strings), measures
// well above the bound by the same method.
func TestRefTableSpaceAt1TiBChunkCount(t *testing.T) {
const chunks = 262144
head := fullHead(chunks, "v1")
before := heapNow()
tab := newRefTable(chunks)
tab.FillSegment(head.Chunks)
per := float64(int64(heapNow())-int64(before)) / chunks
runtime.KeepAlive(tab)
runtime.KeepAlive(head) // the source map lives across both readings
var tab *refTable
per := heldAfterSourceDropped(chunks, func(h chunkHead) any {
tab = newRefTable(chunks)
if err := tab.FillSegment(0, chunks, h.Chunks); err != nil {
t.Fatal(err)
}
return tab
})
t.Logf("ref table: %.1f B/chunk at %d chunks (%d owner prefix)", per, chunks, len(tab.prefixes))
if per > 40 || tab.Len() != chunks || len(tab.other) != 0 {
t.Fatalf("ref table %.1f B/chunk, %d named, %d whole; want <= 40, %d, 0", per, tab.Len(), len(tab.other), chunks)
}
aliased := heldAfterSourceDropped(chunks, func(h chunkHead) any { return copyHead(h) })
t.Logf("control, a shallow flat-map copy aliasing the source's keys: %.1f B/chunk", aliased)
if aliased <= 40 {
t.Fatalf("the control measured %.1f B/chunk: the method would not see aliased keys", aliased)
}
}
// A segment's fill makes its range exactly the refs given: a re-fill drops a
// reference the new HEAD no longer names (it reads as zeros), and leaves the
// other segments alone. A reference beyond the volume, outside the segment's
// range, or a range beyond the volume is refused and changes nothing.
func TestRefTableFillReplacesItsSegmentAndRefusesOutOfRange(t *testing.T) {
tab := newRefTable(20)
ref := func(i uint64, tag string) chunkRef {
s := sha256.Sum256([]byte(tag))
return chunkRef{Key: fmt.Sprintf("layout:v1:0011:%d-%x", i, s), SHA256: s, Encoding: "full"}
}
if err := tab.FillSegment(0, 10, map[uint64]chunkRef{1: ref(1, "a"), 2: ref(2, "b")}); err != nil {
t.Fatal(err)
}
if err := tab.FillSegment(10, 20, map[uint64]chunkRef{15: ref(15, "c")}); err != nil {
t.Fatal(err)
}
// HEAD now names chunk 1 again and no longer chunk 2.
if err := tab.FillSegment(0, 10, map[uint64]chunkRef{1: ref(1, "a2")}); err != nil {
t.Fatal(err)
}
if _, ok := tab.Get(2); ok {
t.Fatal("a re-fill kept a reference the new HEAD dropped")
}
if r, _ := tab.Get(1); r != ref(1, "a2") {
t.Fatalf("chunk 1 after the re-fill: %+v", r)
}
if r, ok := tab.Get(15); !ok || r != ref(15, "c") || tab.Len() != 2 {
t.Fatalf("another segment changed: %+v %v, Len %d", r, ok, tab.Len())
}
before := tab.Len()
for name, err := range map[string]error{
"Set beyond the volume": tab.Set(20, ref(20, "x")),
"a ref outside its segment": tab.FillSegment(0, 10, map[uint64]chunkRef{1: ref(1, "a3"), 12: ref(12, "y")}),
"a segment beyond the volume": tab.FillSegment(10, 30, nil),
} {
if !errors.Is(err, errRefBeyondVolume) {
t.Fatalf("%s: %v; want errRefBeyondVolume", name, err)
}
}
if r, _ := tab.Get(1); r != ref(1, "a2") || tab.Len() != before {
t.Fatal("a refused fill changed the table")
}
}