From d7377311f2d5b64f4c5e0063fe5e175dcf09cd43 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 12:32:20 -0800 Subject: [PATCH] hostlimit: fetch test ownership and the waiting-path control - The fetch child keeps its release (and so the slot's file) reachable while it lives, with forced collections before it reports HELD. - The ended-waits test registers every release at once, and its second half frees the other slot only once each fetch is already waiting, so the waiting path's success is what is checked. - The competing fetches derive from a cancellable context, and cleanup joins them within a bound. - Comments: one fetch polls per limiter (blockvolume makes one per process); a configuration check in progress can add its bounded guard wait after the context ends. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/hostlimit/fetch.go | 12 ++-- .../core/hostlimit/fetch_review_test.go | 71 +++++++++++++------ seaweed-block/core/hostlimit/limiter.go | 2 +- 3 files changed, 59 insertions(+), 26 deletions(-) diff --git a/seaweed-block/core/hostlimit/fetch.go b/seaweed-block/core/hostlimit/fetch.go index 45d37b0207..e024f7a453 100644 --- a/seaweed-block/core/hostlimit/fetch.go +++ b/seaweed-block/core/hostlimit/fetch.go @@ -31,10 +31,12 @@ func fetchSlot(key string, n int) int { // configuration.lock, publishing it when absent, as a cut admission does: // ErrConfig when another process uses other flags; // 2. it takes a free slot without waiting, trying from key's hash slot; -// 3. with every slot held, it waits for its process's turn (one fetch per -// process polls) and then tries every slot each fetchRetry. The waiting -// is unordered, so other fetches can take a freed slot first; the wait is -// bounded by ctx alone and ends with ctx's error; +// 3. with every slot held, it waits for its limiter's turn (one fetch per +// limiter polls; blockvolume makes one limiter per process) and then +// tries every slot each fetchRetry. The waiting is unordered, so other +// fetches can take a freed slot first; it ends with ctx's error once ctx +// ends, after any file-system step in progress (a configuration check +// can add up to its bounded guard wait, guardWait); // 4. a slot taken when ctx has already ended is given back, and ctx's error // returned. // @@ -113,7 +115,7 @@ func (l *Limiter) tryFetch(home int) (*os.File, error) { } // waitFetch waits for a fetch slot while every one is held: it takes its -// process's turn, so one fetch per process polls, and tries every slot each +// limiter's turn, so one fetch per limiter polls, and tries every slot each // fetchRetry until one is free or ctx ends. func (l *Limiter) waitFetch(ctx context.Context, home int) (*os.File, error) { select { diff --git a/seaweed-block/core/hostlimit/fetch_review_test.go b/seaweed-block/core/hostlimit/fetch_review_test.go index 08365d475d..594516d980 100644 --- a/seaweed-block/core/hostlimit/fetch_review_test.go +++ b/seaweed-block/core/hostlimit/fetch_review_test.go @@ -37,9 +37,15 @@ func TestFetchProcessHelper(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err := l.HoldFetch(context.Background(), "v/child"); err != nil { + release, err := l.HoldFetch(context.Background(), "v/child") + if err != nil { t.Fatal(err) } + // The release keeps the slot's file reachable for as long as the child + // lives; the forced collections show the hold does not depend on luck. + defer release() + runtime.GC() + runtime.GC() fmt.Println("HELD") _, _ = bufio.NewReader(os.Stdin).ReadByte() } @@ -169,7 +175,9 @@ func TestHoldFetchEndedWaitsLeaveNothingBehind(t *testing.T) { key := keyOnSlot(t, 0, 2) rHome := mustHold(t, homeHolder, key) // held for the whole test defer rHome() - rOther := mustHold(t, otherHolder, keyOnSlot(t, 1, 2)) + otherKey := keyOnSlot(t, 1, 2) + rOther := mustHold(t, otherHolder, otherKey) + defer rOther() // releases are idempotent g0, f0 := runtime.NumGoroutine(), openFDs() for i := 0; i < 20; i++ { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) @@ -183,17 +191,34 @@ func TestHoldFetchEndedWaitsLeaveNothingBehind(t *testing.T) { } } settlesTo(t, "20 waits ended at their deadline", g0, f0) - rOther() - for i := 0; i < 20; i++ { - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - r, err := w.HoldFetch(ctx, key) - cancel() - if err != nil { - t.Fatalf("fetch %d with the other slot free: %v", i, err) + // Each fetch below is already waiting (both slots held) when the other + // slot is freed, so it takes that slot from the waiting path. + for i := 0; i < 10; i++ { + if i > 0 { + rOther = mustHold(t, otherHolder, otherKey) + defer rOther() + } + got := holdAsync(t, w, context.Background(), key) + select { + case h := <-got: + if h.release != nil { + h.release() + } + t.Fatalf("fetch %d returned while both slots were held (err %v)", i, h.err) + case <-time.After(60 * time.Millisecond): + } + rOther() + select { + case h := <-got: + if h.err != nil { + t.Fatalf("waiting fetch %d: %v", i, h.err) + } + h.release() + case <-time.After(2 * time.Second): + t.Fatalf("waiting fetch %d never took the freed slot", i) } - r() } - settlesTo(t, "20 fetches that took the other slot", g0, f0) + settlesTo(t, "10 waiting fetches that took the freed slot", g0, f0) } // A context that ends as a slot is taken gets its context's error, and the @@ -226,20 +251,17 @@ func TestHoldFetchCancelledAtTheHandoffHoldsNothing(t *testing.T) { // its deadline's error. func TestHoldFetchAPassedWaiterEndsByItsDeadline(t *testing.T) { c := fetchConfig(t, 1) - stop := make(chan struct{}) + // The competitors' fetches derive from stopCtx, so stopping cancels a + // fetch in progress too; the cleanup joins them within a bound. + stopCtx, stop := context.WithCancel(context.Background()) var wg sync.WaitGroup for i := 0; i < 4; i++ { l := newTest(t, c) wg.Add(1) go func(i int) { defer wg.Done() - for { - select { - case <-stop: - return - default: - } - ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) + for stopCtx.Err() == nil { + ctx, cancel := context.WithTimeout(stopCtx, 100*time.Millisecond) r, err := l.HoldFetch(ctx, fmt.Sprintf("v/c%d", i)) cancel() if err == nil { @@ -249,7 +271,16 @@ func TestHoldFetchAPassedWaiterEndsByItsDeadline(t *testing.T) { } }(i) } - defer func() { close(stop); wg.Wait() }() + defer func() { + stop() + joined := make(chan struct{}) + go func() { wg.Wait(); close(joined) }() + select { + case <-joined: + case <-time.After(5 * time.Second): + t.Error("the competing fetches did not stop after their context ended") + } + }() w := newTest(t, c) const deadline = 150 * time.Millisecond ctx, cancel := context.WithTimeout(context.Background(), deadline) diff --git a/seaweed-block/core/hostlimit/limiter.go b/seaweed-block/core/hostlimit/limiter.go index d8c7f5086c..888482df4f 100644 --- a/seaweed-block/core/hostlimit/limiter.go +++ b/seaweed-block/core/hostlimit/limiter.go @@ -153,7 +153,7 @@ type Limiter struct { stalled map[string]bool // fetchChecked is set once a fetch has checked the host's configuration; - // fetchTurn (capacity 1) is held by the one fetch of this process that + // fetchTurn (capacity 1) is held by the one fetch of this limiter that // polls for a slot while every slot is held. fetchChecked atomic.Bool fetchTurn chan struct{}