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>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 11:43:59 -08:00
1 parent cf31d2ec4e
commit 9391166b25
5 files changed
+266 -2

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
package hostlimit
import (
"context"
"hash/fnv"
)
// fetchSlot is the fetch slot a key waits on when every slot is held: its
// hash, modulo the slot count.
func fetchSlot(key string, n int) int {
h := fnv.New32a()
_, _ = h.Write([]byte(key))
return int(h.Sum32() % uint32(n))
}
// HoldFetch holds one of the host's fetch slots for one chunk transfer
// (seam: no cap yet).
func (l *Limiter) HoldFetch(ctx context.Context, key string) (func(), error) {
return func() {}, ctx.Err()
}
+162
View File
@@ -0,0 +1,162 @@
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")
}
}
+4 -2
View File
@@ -63,13 +63,15 @@ type Config struct {
// only after this long with no progress of its work (Progress). Work
// that keeps progressing is never revoked, however long it holds.
NoProgress time.Duration
// FetchSlots caps the chunk fetches in flight on the host (seam).
FetchSlots int
}
func DefaultConfig(dir string) Config {
return Config{Directory: dir, NormalSlots: 3, UrgentSlots: 1, FillSlots: 2, IdleSlots: 1, PayloadBytes: 64 << 20, COWBytes: 1 << 30, BytesPerSecond: 64 << 20, FallbackVolumes: 200, NoProgress: 60 * time.Second}
return Config{Directory: dir, NormalSlots: 3, UrgentSlots: 1, FillSlots: 2, IdleSlots: 1, PayloadBytes: 64 << 20, COWBytes: 1 << 30, BytesPerSecond: 64 << 20, FallbackVolumes: 200, NoProgress: 60 * time.Second, FetchSlots: 64}
}
func (c Config) Validate() error {
if !filepath.IsAbs(c.Directory) || c.NormalSlots < 1 || c.NormalSlots > 256 || c.UrgentSlots < 1 || c.UrgentSlots > 256 || c.FillSlots < 0 || c.FillSlots > 256 || c.IdleSlots < 0 || c.IdleSlots > 256 || c.NormalSlots+c.UrgentSlots+c.FillSlots+c.IdleSlots > 256 || c.PayloadBytes <= 0 || c.COWBytes <= 0 || c.BytesPerSecond <= 0 || c.FallbackVolumes < 1 || c.FallbackVolumes > 65536 || c.NoProgress <= 0 {
if !filepath.IsAbs(c.Directory) || c.NormalSlots < 1 || c.NormalSlots > 256 || c.UrgentSlots < 1 || c.UrgentSlots > 256 || c.FillSlots < 0 || c.FillSlots > 256 || c.IdleSlots < 0 || c.IdleSlots > 256 || c.NormalSlots+c.UrgentSlots+c.FillSlots+c.IdleSlots > 256 || c.PayloadBytes <= 0 || c.COWBytes <= 0 || c.BytesPerSecond <= 0 || c.FallbackVolumes < 1 || c.FallbackVolumes > 65536 || c.NoProgress <= 0 || c.FetchSlots < 1 || c.FetchSlots > 1024 {
return errors.New("hostlimit: invalid configuration")
}
return nil
@@ -32,6 +32,12 @@ func budgetProgress(b Budget, ticket uint64) func() {
return func() {}
}
// BudgetFetch is a Budget that caps the chunk fetches in flight on the host
// (seam).
type BudgetFetch interface {
HoldFetch(ctx context.Context, key string) (release func(), err error)
}
type cutBudgetReservation struct {
budget Budget
ticket uint64
@@ -0,0 +1,74 @@
package storage
import (
"context"
"sync"
"testing"
"time"
)
// fetchSlots is a BudgetFetch that counts the fetch slots held, and the most
// held at once.
type fetchSlots struct {
Budget
mu sync.Mutex
active, holds int
keys []string
}
func (f *fetchSlots) HoldFetch(ctx context.Context, key string) (func(), error) {
f.mu.Lock()
f.active++
f.holds++
f.keys = append(f.keys, key)
f.mu.Unlock()
var once sync.Once
return func() {
once.Do(func() {
f.mu.Lock()
f.active--
f.mu.Unlock()
})
}, nil
}
func (f *fetchSlots) state() (active, holds int) {
f.mu.Lock()
defer f.mu.Unlock()
return f.active, f.holds
}
// A chunk fetch holds one host fetch slot exactly across its remote read:
// held while the read is in flight, released once the read has returned (the
// install and the reader's wake-up hold none).
func TestChunkFetchHoldsAHostSlotAroundItsRemoteRead(t *testing.T) {
s, layer, remote, _ := hydrationFixture(t, 2, 64)
slots := &fetchSlots{}
layer.cfg.Budget = slots
entered, release := holdGets(t, remote)
read := make(chan error, 1)
go func() {
_, err := s.Read(0)
read <- err
}()
select {
case <-entered:
case <-time.After(ctLocalBound):
t.Fatal("the read miss never reached the remote")
}
if active, holds := slots.state(); active != 1 || holds != 1 {
t.Fatalf("during the remote read %d slots are held (%d taken), want exactly one", active, holds)
}
release()
select {
case err := <-read:
if err != nil {
t.Fatal(err)
}
case <-time.After(ctLocalBound):
t.Fatal("the read did not complete")
}
if active, _ := slots.state(); active != 0 {
t.Fatalf("%d fetch slots still held after the read returned", active)
}
}