mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
Pin coverage format paging through hashed index paths
This commit is contained in:
1 parent
e3f3762e55
commit
f592634829
1 file changed
+138
@@ -0,0 +1,138 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// This independent, test-only reader pins the format's paging boundary. The
|
||||
// production reader deliberately remains eager; no cache or runtime API is added.
|
||||
func coverageEntryByPath(t *testing.T, f io.ReaderAt, rootOffset, chunk, cb uint64) ([]byte, int) {
|
||||
t.Helper()
|
||||
cache := map[uint64][]byte{}
|
||||
read := func(offset uint64, hash [32]byte, domain string) []byte {
|
||||
if p := cache[offset]; p != nil {
|
||||
return p
|
||||
}
|
||||
p := make([]byte, 4096)
|
||||
if _, err := f.ReadAt(p, int64(offset)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := mapDigestV3(domain, p[:4064])
|
||||
if !bytes.Equal(got[:], p[4064:]) || (hash != ([32]byte{}) && got != hash) {
|
||||
t.Fatal("paged reader hash mismatch")
|
||||
}
|
||||
cache[offset] = p
|
||||
return p
|
||||
}
|
||||
le := binary.LittleEndian
|
||||
root := read(rootOffset, [32]byte{}, mapRootDomainV3)
|
||||
height := le.Uint16(root[6:])
|
||||
width := uint64(16) + (cb+7)/8
|
||||
entry := make([]byte, width)
|
||||
for done := uint64(0); done < width; {
|
||||
absolute := chunk*width + done
|
||||
leaf, within := absolute/4000, absolute%4000
|
||||
page, base, first := root, uint64(96), uint64(0)
|
||||
var ref mapPageRefV3
|
||||
for remaining := height; ; remaining-- {
|
||||
span := uint64(1)
|
||||
for i := uint16(0); i < remaining; i++ {
|
||||
span *= 96
|
||||
}
|
||||
index := (leaf - first) / span
|
||||
at := base + index*40
|
||||
ref.Offset = le.Uint64(page[at:])
|
||||
copy(ref.Hash[:], page[at+8:at+40])
|
||||
first += index * span
|
||||
if ref == (mapPageRefV3{}) || remaining == 0 {
|
||||
break
|
||||
}
|
||||
page = read(ref.Offset, ref.Hash, coverageIndexDomain)
|
||||
base = 64
|
||||
}
|
||||
n := min(width-done, 4000-within)
|
||||
if ref == (mapPageRefV3{}) {
|
||||
for i := uint64(0); i < n; i++ {
|
||||
entry[done+i] = defaultCoverageByte(done+i, le.Uint64(root[88:]))
|
||||
}
|
||||
} else {
|
||||
p := read(ref.Offset, ref.Hash, mapPageDomainV3)
|
||||
copy(entry[done:done+n], p[64+within:64+within+n])
|
||||
}
|
||||
done += n
|
||||
}
|
||||
return entry, len(cache)
|
||||
}
|
||||
|
||||
func TestChunkCoveragePagedFormatMatchesWholeMap(t *testing.T) {
|
||||
const cb, chunks = uint64(1024), uint64(260000)
|
||||
d := descriptorV3{BlockSize: 4096, VolumeSize: cb * chunks * 4096, MapFormatID: mapFormatIDCoverage, MapGeneration: 3}
|
||||
d.UUID[0], d.MapLineage[0] = 1, 2
|
||||
shape, err := mapShapeForV3(d, cb)
|
||||
if err != nil || coverageTreeHeight(shape.pages) != 2 {
|
||||
t.Fatalf("need two index levels: shape=%+v err=%v", shape, err)
|
||||
}
|
||||
entries := make([]chunkLSNEntryV3, chunks)
|
||||
for i := range entries {
|
||||
entries[i] = chunkLSNEntryV3{LSN: 7, Unknown: true, Local: noChunkLocalBits}
|
||||
}
|
||||
// Entry 27 occupies bytes 3888..4031: its bitmap crosses two leaves.
|
||||
entries[27] = chunkLSNEntryV3{LSN: 11, Local: noChunkLocalBits.mark(0, cb).mark(1023, cb)}
|
||||
entries[chunks-1] = chunkLSNEntryV3{LSN: 13, Local: &chunkLocalBits{blocks: cb, all: true}}
|
||||
f, err := os.CreateTemp(t.TempDir(), "map")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { f.Close() })
|
||||
var next uint64
|
||||
write := func(p []byte) (uint64, error) {
|
||||
offset := next
|
||||
_, err := f.WriteAt(p, int64(offset))
|
||||
next += uint64(len(p))
|
||||
return offset, err
|
||||
}
|
||||
leaves := make([]mapPageRefV3, shape.pages)
|
||||
for _, leaf := range []uint64{0, 1, shape.pages - 1} {
|
||||
p, err := encodeCoverageMapPageV3(d, 3, leaf, cb, entries, 7)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
offset, err := write(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leaves[leaf] = mapPageRefV3{Offset: offset, Hash: mapDigestV3(mapPageDomainV3, p[:4064])}
|
||||
}
|
||||
root, _, err := encodeCoverageTreeRootV3(d, 3, cb, 13, leaves, nil, 7, write)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.MapRootLocator, err = write(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
whole, _, err := readCoverageMapPagesV3(f, d, cb, shape, root, next, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
chunk uint64
|
||||
reads int
|
||||
}{{27, 5}, {10000, 2}, {chunks - 1, 4}} {
|
||||
got, reads := coverageEntryByPath(t, f, d.MapRootLocator, tc.chunk, cb)
|
||||
want := make([]byte, 144)
|
||||
for i := range want {
|
||||
want[i] = coverageByte(whole[tc.chunk], uint64(i))
|
||||
}
|
||||
if !bytes.Equal(got, want) || reads != tc.reads {
|
||||
t.Fatalf("chunk %d: entry equal=%v, page reads=%d want=%d", tc.chunk, bytes.Equal(got, want), reads, tc.reads)
|
||||
}
|
||||
}
|
||||
if !whole[27].Local.has(0) || !whole[27].Local.has(1023) || whole[27].LSN != 11 || !whole[chunks-1].Local.all {
|
||||
t.Fatal("whole-map oracle lost the literal cross-leaf bitmap/position")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user