Files
seaweedfs/seaweed-block/core/hostlimit/fetch_test.go
T
pingqiuandClaude Opus 5.5 9391166b25 hostlimit, storage: red tests, a per-host cap on chunk fetches
Seams: Config.FetchSlots (default 64), fetchSlot (a key's hash slot),
Limiter.HoldFetch (no cap yet) and storage.BudgetFetch.

- TestHoldFetchCapHoldsAcrossProcesses: with every slot held by other
  processes, a fetch waits until one is released, then takes it. Fails here.
- TestHoldFetchAWaiterThatGivesUpLeavesNoSlotHeld: a waiter past its context
  returns the context's error, and a slot it gets after is released at once.
  Fails here.
- TestHoldFetchTakesAnotherSlotThatFreesFirst: a waiter behind a slow holder
  of its own hash slot takes another slot within one retry interval of its
  release. Fails here.
- TestHoldFetchTakesAFreeSlotAtOnce: a fetch with a free slot does not wait
  (holds here, must keep holding).
- TestChunkFetchHoldsAHostSlotAroundItsRemoteRead: a chunk fetch holds one
  slot exactly across its remote read. Fails here: none is held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:43:59 -08:00

163 lines
4.6 KiB
Go

package hostlimit
import (
"context"
"errors"
"fmt"
"testing"
"time"
)
func fetchConfig(t *testing.T, slots int) Config {
t.Helper()
c := testConfig(t)
c.FetchSlots = slots
return c
}
// keyOnSlot is a key whose hash slot is s among n.
func keyOnSlot(t *testing.T, s, n int) string {
t.Helper()
for i := 0; i < 10000; i++ {
k := fmt.Sprintf("v/%d", i)
if fetchSlot(k, n) == s {
return k
}
}
t.Fatalf("no key on slot %d of %d", s, n)
return ""
}
type held struct {
release func()
err error
}
// holdAsync runs HoldFetch in the background; its result arrives on the
// channel. The test's cleanup releases a slot it got.
func holdAsync(t *testing.T, l *Limiter, ctx context.Context, key string) <-chan held {
t.Helper()
got := make(chan held, 1)
go func() {
r, err := l.HoldFetch(ctx, key)
got <- held{r, err}
}()
return got
}
func mustHold(t *testing.T, l *Limiter, key string) func() {
t.Helper()
r, err := l.HoldFetch(context.Background(), key)
if err != nil {
t.Fatal(err)
}
return r
}
// While slots are free, a fetch takes one without waiting.
func TestHoldFetchTakesAFreeSlotAtOnce(t *testing.T) {
c := fetchConfig(t, 2)
a, b := newTest(t, c), newTest(t, c)
r1 := mustHold(t, a, keyOnSlot(t, 0, 2))
defer r1()
start := time.Now()
r2 := mustHold(t, b, keyOnSlot(t, 0, 2)) // its hash slot is held: it takes the other one
defer r2()
if d := time.Since(start); d > 200*time.Millisecond {
t.Fatalf("a fetch with a free slot waited %s", d)
}
}
// The cap holds across processes: with every slot held by others, a fetch
// waits until one is released, and then takes it.
func TestHoldFetchCapHoldsAcrossProcesses(t *testing.T) {
c := fetchConfig(t, 2)
a, b, w := newTest(t, c), newTest(t, c), newTest(t, c)
r1 := mustHold(t, a, keyOnSlot(t, 0, 2))
r2 := mustHold(t, b, keyOnSlot(t, 1, 2))
defer r2()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
got := holdAsync(t, w, ctx, "v/third")
select {
case h := <-got:
if h.release != nil {
h.release()
}
t.Fatalf("a third fetch held a slot while both were held (err %v)", h.err)
case <-time.After(150 * time.Millisecond):
}
r1()
select {
case h := <-got:
if h.err != nil {
t.Fatalf("the waiting fetch: %v", h.err)
}
h.release()
case <-time.After(2 * time.Second):
t.Fatal("the waiting fetch did not take the released slot")
}
}
// A waiter whose context ends returns its context's error, and a slot it
// gets after that is released at once: a later fetch is not held back by it.
func TestHoldFetchAWaiterThatGivesUpLeavesNoSlotHeld(t *testing.T) {
c := fetchConfig(t, 1)
a, w, later := newTest(t, c), newTest(t, c), newTest(t, c)
r1 := mustHold(t, a, "v/a")
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if _, err := w.HoldFetch(ctx, "v/w"); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("a waiter past its context answered %v, want its deadline", err)
}
r1() // the abandoned wait now gets the slot, and must release it at once
ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel2()
select {
case h := <-holdAsync(t, later, ctx2, "v/later"):
if h.err != nil {
t.Fatalf("a later fetch was held back by an abandoned wait: %v", h.err)
}
h.release()
case <-time.After(3 * time.Second):
t.Fatal("a later fetch never got the slot")
}
}
// A waiter blocked behind a slow holder on its own hash slot is not delayed
// beyond one retry interval once another slot is free: the host's slots are
// one pool, not N separate queues.
func TestHoldFetchTakesAnotherSlotThatFreesFirst(t *testing.T) {
c := fetchConfig(t, 2)
slow, other, w := newTest(t, c), newTest(t, c), newTest(t, c)
key := keyOnSlot(t, 0, 2)
rSlow := mustHold(t, slow, key) // the waiter's hash slot, held for the whole test
defer rSlow()
rOther := mustHold(t, other, keyOnSlot(t, 1, 2))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
got := holdAsync(t, w, ctx, key)
select {
case h := <-got:
if h.release != nil {
h.release()
}
t.Fatalf("the waiter held a slot while both were held (err %v)", h.err)
case <-time.After(150 * time.Millisecond):
}
freed := time.Now()
rOther()
select {
case h := <-got:
if h.err != nil {
t.Fatalf("the waiter: %v", h.err)
}
if d := time.Since(freed); d > 300*time.Millisecond {
t.Fatalf("the waiter took the freed slot %s after it was freed, behind a slow holder of its own slot", d)
}
h.release()
case <-time.After(2 * time.Second):
t.Fatal("the waiter never took the other slot while its own stayed held")
}
}