chunkstore: EachSegment reads HEAD one segment at a time; one segment walker

LayoutRemote.EachSegment is GetHead one segment at a time: HEAD's
identity, and each segment its root names delivered with its shard's
range and references, only one held at a time (for the streaming attach,
V4-B PR 4 commit 4). The manifest read and the attach's bounded read now
share one walker (walkSegments: the root and each segment, counted, each
fetch bounded within an attach).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 10:27:12 -08:00
1 parent 7e5f1fce7d
commit de256fc5aa
2 files changed
+128 -50

No files matched your search

@@ -0,0 +1,51 @@
package chunkstore
import (
"reflect"
"testing"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// EachSegment is GetHead one segment at a time: one call per segment HEAD's
// root names, each with its shard's range and exactly the references GetHead
// lists in that range, and HEAD's identity with no chunk listed. It reads the
// root once and each segment once, and holds one segment at a time.
func TestEachSegmentIsGetHeadOneSegmentAtATime(t *testing.T) {
ctx, _, r, _ := threeShardVolume(t)
full, err := r.GetHead(ctx)
if err != nil {
t.Fatal(err)
}
type call struct{ first, end uint64 }
var calls []call
got := map[uint64]storage.ChunkRef{}
octx, count := storage.ObserveCutWork(ctx)
id, err := r.EachSegment(octx, func(first, end uint64, refs map[uint64]storage.ChunkRef) error {
calls = append(calls, call{first, end})
for i, ref := range refs {
if i < first || i >= end {
t.Errorf("chunk %d delivered outside its range [%d, %d)", i, first, end)
}
got[i] = ref
}
return nil
})
if err != nil {
t.Fatal(err)
}
want := []call{{0, layout.ShardSize}, {layout.ShardSize, 2 * layout.ShardSize}, {2 * layout.ShardSize, 3 * layout.ShardSize}}
if !reflect.DeepEqual(calls, want) {
t.Fatalf("calls %v, want %v", calls, want)
}
if !reflect.DeepEqual(got, full.Chunks) {
t.Fatalf("EachSegment's references differ from GetHead's: %d vs %d", len(got), len(full.Chunks))
}
if id.Chunks != nil || id.Gen != full.Gen || id.Epoch != full.Epoch || id.Holder != full.Holder || id.RemoteLSN != full.RemoteLSN {
t.Fatalf("identity %+v, GetHead %+v", id, full)
}
if roots, segs := count(storage.CountRootReads), count(storage.CountSegmentReads); roots != 1 || segs != 3 {
t.Fatalf("%d root reads, %d segment reads; want 1 and 3", roots, segs)
}
}
@@ -412,30 +412,94 @@ func (l *LayoutRemote) headIdentityLocked(ctx context.Context) (layout.Head, sto
// no bound on the read as a whole (the segment count grows with the volume).
func (l *LayoutRemote) manifest(ctx context.Context, rn layout.RootName) (map[uint64]layout.Ref, error) {
defer storage.MeasureCutWork(ctx, storage.CutManifestRead)()
if af, ok := storage.AttachFetchFrom(ctx); ok {
m, err := boundedManifest(ctx, l.r, rn, af)
if err != nil {
return nil, layoutRemoteErr(err)
m := map[uint64]layout.Ref{}
err := walkSegments(ctx, l.r, rn, func(_ uint64, seg map[uint64]layout.Ref) error {
for idx, ref := range seg {
m[idx] = ref
}
return m, nil
}
img, err := l.r.Open(ctx, rn)
return nil
})
if err != nil {
return nil, layoutRemoteErr(err)
}
return m, nil
}
// walkSegments reads root rn and then each segment it names, one at a time,
// handing each to fn (its shard and its references by chunk index): the
// root and every segment, each read counted; within an attach each fetch is
// bounded by the attach's fetch bound and the completed references are
// reported. Only one segment is held at a time.
func walkSegments(ctx context.Context, r *layout.Reader, rn layout.RootName, fn func(shard uint64, seg map[uint64]layout.Ref) error) error {
af, attach := storage.AttachFetchFrom(ctx)
fetchCtx := func() (context.Context, context.CancelFunc) {
if attach && af.Bound > 0 {
return context.WithTimeout(ctx, af.Bound)
}
return context.WithCancel(ctx)
}
c, cancel := fetchCtx()
img, err := r.Open(c, rn)
cancel()
if err != nil {
return err
}
storage.CountCutWork(ctx, storage.CountRootReads, 1)
m := map[uint64]layout.Ref{}
for _, sh := range img.Shards() {
seg, _, err := img.Segment(ctx, sh)
c, cancel := fetchCtx()
seg, _, err := img.Segment(c, sh)
cancel()
if err != nil {
return nil, layoutRemoteErr(err)
return err
}
storage.CountCutWork(ctx, storage.CountSegmentReads, 1)
for idx, ref := range seg.Chunks {
m[idx] = ref
if err := fn(sh, seg.Chunks); err != nil {
return err
}
if attach && af.Done != nil {
af.Done(uint64(len(seg.Chunks)))
}
}
return m, nil
return nil
}
// boundedManifest is the whole manifest of rn read under an attach's fetch
// bound af (walkSegments within that attach).
func boundedManifest(ctx context.Context, r *layout.Reader, rn layout.RootName, af storage.AttachFetch) (map[uint64]layout.Ref, error) {
m := map[uint64]layout.Ref{}
err := walkSegments(storage.WithAttachFetch(ctx, af), r, rn, func(_ uint64, seg map[uint64]layout.Ref) error {
for idx, ref := range seg {
m[idx] = ref
}
return nil
})
return m, err
}
// EachSegment is GetHead one segment at a time (storage.ChunkSegmentReader):
// HEAD's identity as GetHead returns it (no chunk listed), and fn called for
// each segment HEAD's root names with its chunk range [first, end) (a
// shard's range: the caller clamps it to its volume) and its references.
// Only one segment is held at a time. A HEAD with no image calls fn never.
func (l *LayoutRemote) EachSegment(ctx context.Context, fn func(first, end uint64, refs map[uint64]storage.ChunkRef) error) (storage.ChunkHead, error) {
l.mu.Lock()
defer l.mu.Unlock()
h, out, err := l.headIdentityLocked(ctx)
out.Chunks = nil
if err != nil || h.Current == nil {
return out, err
}
err = walkSegments(ctx, l.r, *h.Current, func(shard uint64, seg map[uint64]layout.Ref) error {
refs := make(map[uint64]storage.ChunkRef, len(seg))
for idx, ref := range seg {
refs[idx] = storage.ChunkRef{Key: LayoutKey(ref), SHA256: ref.Chunk.Hash, Encoding: "full"}
}
return fn(shard*layout.ShardSize, (shard+1)*layout.ShardSize, refs)
})
if err != nil {
return storage.ChunkHead{}, layoutRemoteErr(err)
}
return out, nil
}
// attachFetchBound bounds one object call of an attach's manifest read by
@@ -447,43 +511,6 @@ func attachFetchBound(ctx context.Context) (context.Context, context.CancelFunc)
return context.WithCancel(ctx)
}
// boundedManifest reads root rn's chunk map one fetch at a time: the root,
// then each segment, every fetch under af.Bound on its own, reporting each
// completed segment's entries to af.Done. A fetch that cannot finish within
// its bound fails the read (never a partial map).
func boundedManifest(ctx context.Context, r *layout.Reader, rn layout.RootName, af storage.AttachFetch) (map[uint64]layout.Ref, error) {
fetchCtx := func() (context.Context, context.CancelFunc) {
if af.Bound > 0 {
return context.WithTimeout(ctx, af.Bound)
}
return context.WithCancel(ctx)
}
c, cancel := fetchCtx()
img, err := r.Open(c, rn)
cancel()
if err != nil {
return nil, err
}
storage.CountCutWork(ctx, storage.CountRootReads, 1)
m := map[uint64]layout.Ref{}
for _, sh := range img.Shards() {
c, cancel := fetchCtx()
seg, _, err := img.Segment(c, sh)
cancel()
if err != nil {
return nil, err
}
storage.CountCutWork(ctx, storage.CountSegmentReads, 1)
for idx, ref := range seg.Chunks {
m[idx] = ref
}
if af.Done != nil {
af.Done(uint64(len(seg.Chunks)))
}
}
return m, nil
}
// CASHead commits next as this cut's session: the root of next's chunk
// map, position next.RemoteLSN, detached when next is, and the pending
// snapshot name. It uses the engine view from before this session's Begin.