cut counters: a bounded HEAD re-read counts its reads; disjointness oracle

boundedManifest (the attach-fetch HEAD read the cut's settlement uses)
counted no root or segment read: each successful fetch is now counted as
it completes, so a later failed fetch keeps the earlier ones. Controls: a
real GetHead under WithAttachFetch counts 1 root and 3 segments on three
shards, and 1 and 1 when shard 1's fetch fails. The lost-answer test
asserts the disjointness that always holds (ManifestNs + HeadReadbackNs <=
TotalNs; the old sum exceeds it by the re-read's 100 ms) and that the read
counts include the re-read's, instead of a timing bound on the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-30 10:49:03 -08:00
1 parent cdf18c1988
commit eeaa90de7d
3 files changed
+50 -9

No files matched your search

@@ -5,6 +5,7 @@ import (
"errors"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
"github.com/seaweedfs/seaweed-block/core/storage"
@@ -130,3 +131,36 @@ func TestCutWorkCountsOutsideAnAttempt(t *testing.T) {
t.Fatalf("segment reads %d, unknown kind %d; want 2 and 0", get(storage.CountSegmentReads), get(storage.CutWorkCount(250)))
}
}
// A HEAD read under an attach fetch (the cut's own HEAD re-read after a lost
// answer reads HEAD this way) counts the root and each segment as it reads
// them: 1 and 3 on three shards. When a later segment fetch fails, the reads
// that completed before it stay counted.
func TestBoundedHeadReadCountsItsRootAndSegments(t *testing.T) {
t.Run("all fetches", func(t *testing.T) {
ctx, _, r, _ := threeShardVolume(t)
octx, get := storage.ObserveCutWork(ctx)
if _, err := r.GetHead(storage.WithAttachFetch(octx, storage.AttachFetch{Bound: 5 * time.Second})); err != nil {
t.Fatal(err)
}
if got, want := readWork(get), (manifestWork{rootReads: 1, segReads: 3}); got != want {
t.Fatalf("bounded HEAD read: %+v; want %+v", got, want)
}
})
t.Run("a later fetch fails", func(t *testing.T) {
ctx, ff, r, _ := threeShardVolume(t)
ff.failLookup = func(p string) error {
if strings.Contains(p, "/seg/1-") {
return errors.New("segment fetch failed")
}
return nil
}
octx, get := storage.ObserveCutWork(ctx)
if _, err := r.GetHead(storage.WithAttachFetch(octx, storage.AttachFetch{Bound: 5 * time.Second})); err == nil {
t.Fatal("setup: the HEAD read with a failing segment fetch succeeded")
}
if got, want := readWork(get), (manifestWork{rootReads: 1, segReads: 1}); got != want {
t.Fatalf("bounded HEAD read failing at shard 1: %+v; want %+v (the root and shard 0 completed)", got, want)
}
})
}
@@ -464,6 +464,7 @@ func boundedManifest(ctx context.Context, r *layout.Reader, rn layout.RootName,
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()
@@ -472,6 +473,7 @@ func boundedManifest(ctx context.Context, r *layout.Reader, rn layout.RootName,
if err != nil {
return nil, err
}
storage.CountCutWork(ctx, storage.CountSegmentReads, 1)
for idx, ref := range seg.Chunks {
m[idx] = ref
}
@@ -15,9 +15,9 @@ import (
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
)
// lostAnswerFiler applies the first HEAD update after it is armed and then
// reports the answer lost; from then on each root or segment lookup takes
// 30 ms, so only a HEAD re-read after the lost answer reads the manifest
// lostAnswerFiler applies the commit's HEAD update after it is armed and
// then reports the answer lost; from then on each root or segment lookup
// takes 50 ms, so the HEAD re-read after the lost answer reads the manifest
// slowly.
type lostAnswerFiler struct {
*chunkstore.MemFiler
@@ -39,15 +39,17 @@ func (f *lostAnswerFiler) UpdateEntry(ctx context.Context, dir string, e *filerp
func (f *lostAnswerFiler) LookupEntry(ctx context.Context, dir, name string) (*filerpb.Entry, error) {
if p := path.Join(dir, name); f.lost.Load() && (strings.Contains(p, "/root/") || strings.Contains(p, "/seg/")) {
time.Sleep(30 * time.Millisecond)
time.Sleep(50 * time.Millisecond)
}
return f.MemFiler.LookupEntry(ctx, dir, name)
}
// A cut whose HEAD update answer is lost settles by reading HEAD again, and
// that re-read reads the manifest inside HeadReadbackNs. ManifestNs is the
// commit check's manifest work only, so the two pre-copy parts stay
// disjoint: the slow re-read is in HeadReadbackNs and not in ManifestNs.
// commit check's manifest work only, so the two parts are disjoint intervals
// of the attempt: their sum never exceeds the attempt's total (counting the
// slow re-read in both makes it exceed it by the re-read's 100 ms). The read
// counts are the whole attempt's: the check's and the re-read's.
func TestManifestNsLeavesAHeadReReadToHeadReadback(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
@@ -91,10 +93,13 @@ func TestManifestNsLeavesAHeadReReadToHeadReadback(t *testing.T) {
t.Fatalf("the cut did not settle its lost answer: %v", err)
}
st := res.Stats
if st.HeadReadbackNs < int64(30*time.Millisecond) {
if st.HeadReadbackNs < int64(100*time.Millisecond) {
t.Fatalf("setup: the settlement did not read the manifest again (HeadReadbackNs %v)", time.Duration(st.HeadReadbackNs))
}
if st.ManifestNs >= int64(30*time.Millisecond) {
t.Fatalf("ManifestNs %v includes the HEAD re-read's manifest (HeadReadbackNs %v): the parts overlap", time.Duration(st.ManifestNs), time.Duration(st.HeadReadbackNs))
if st.ManifestNs+st.HeadReadbackNs > st.TotalNs {
t.Fatalf("ManifestNs %v + HeadReadbackNs %v exceed the attempt's total %v: the re-read's manifest is counted in both", time.Duration(st.ManifestNs), time.Duration(st.HeadReadbackNs), time.Duration(st.TotalNs))
}
if st.ManifestRootReads < 2 || st.ManifestSegmentReads < 2 {
t.Fatalf("read counts %d roots, %d segments; want the commit check's and the re-read's (at least 2 each)", st.ManifestRootReads, st.ManifestSegmentReads)
}
}