diff --git a/seaweed-block/core/hostlimit/fetch.go b/seaweed-block/core/hostlimit/fetch.go new file mode 100644 index 0000000000..009a5308a4 --- /dev/null +++ b/seaweed-block/core/hostlimit/fetch.go @@ -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() +} diff --git a/seaweed-block/core/hostlimit/fetch_test.go b/seaweed-block/core/hostlimit/fetch_test.go new file mode 100644 index 0000000000..143e02d451 --- /dev/null +++ b/seaweed-block/core/hostlimit/fetch_test.go @@ -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") + } +} diff --git a/seaweed-block/core/hostlimit/limiter.go b/seaweed-block/core/hostlimit/limiter.go index 4b60b65831..6107d5024b 100644 --- a/seaweed-block/core/hostlimit/limiter.go +++ b/seaweed-block/core/hostlimit/limiter.go @@ -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 diff --git a/seaweed-block/core/storage/chunk_cut_budget.go b/seaweed-block/core/storage/chunk_cut_budget.go index b68022a1db..ec1d9de652 100644 --- a/seaweed-block/core/storage/chunk_cut_budget.go +++ b/seaweed-block/core/storage/chunk_cut_budget.go @@ -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 diff --git a/seaweed-block/core/storage/chunk_fetch_cap_test.go b/seaweed-block/core/storage/chunk_fetch_cap_test.go new file mode 100644 index 0000000000..6d0d2a1018 --- /dev/null +++ b/seaweed-block/core/storage/chunk_fetch_cap_test.go @@ -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) + } +}