diff --git a/seaweed-block/core/frontend/chunkstore/layout_manifest_work_test.go b/seaweed-block/core/frontend/chunkstore/layout_manifest_work_test.go index a149493951..77997e0bba 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_manifest_work_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout_manifest_work_test.go @@ -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) + } + }) +} diff --git a/seaweed-block/core/frontend/chunkstore/layout_remote.go b/seaweed-block/core/frontend/chunkstore/layout_remote.go index 8b1b504ead..dd2e0f8623 100644 --- a/seaweed-block/core/frontend/chunkstore/layout_remote.go +++ b/seaweed-block/core/frontend/chunkstore/layout_remote.go @@ -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 } diff --git a/seaweed-block/core/frontend/durable/cut_manifest_readback_test.go b/seaweed-block/core/frontend/durable/cut_manifest_readback_test.go index 4be14aa18d..c23e3ff5c3 100644 --- a/seaweed-block/core/frontend/durable/cut_manifest_readback_test.go +++ b/seaweed-block/core/frontend/durable/cut_manifest_readback_test.go @@ -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) } }