Files
seaweedfs/seaweed-block/core/hostlimit/fetch.go
T
pingqiuandClaude Opus 5.5 5a57e8564d hostlimit, storage: chunk fetches are capped per host
- hostlimit: Config.FetchSlots (default 64, files fetch-0..N-1 in the pool).
  HoldFetch takes a free slot without waiting, from the key's hash slot;
  with every slot held it blocks on its hash slot (the kernel queues the
  waiters) and tries the other slots every 20 ms, using whichever lock comes
  first and releasing the other; its context bounds the wait, and a lock got
  after that is released at once. lockBlocking on Unix and Windows.
- storage: BudgetFetch; fillOnce holds one slot across its remote read only
  (a read miss and a hydrate-all fill alike); a context error on the wait is
  the hydration path's pending, as before.
- blockvolume: --host-limit-fetch-slots. Invariants: INV-HOST-FETCH-CAP-001;
  M7 states it. v1.1 notes carry it.

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

121 lines
3.0 KiB
Go

package hostlimit
import (
"context"
"errors"
"fmt"
"hash/fnv"
"os"
"path/filepath"
"sync"
"time"
)
// fetchRetry is how often a waiting fetch tries the other slots while it is
// blocked on its own: the host's slots are one pool, so a slot that frees
// elsewhere is taken within one interval.
const fetchRetry = 20 * time.Millisecond
// 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 FetchSlots fetch slots (files fetch-0 ..
// fetch-(N-1) in the pool, so the cap is per host and dies with its
// holders) for one chunk transfer; release gives it back. It never refuses:
//
// 1. it takes a free slot without waiting, trying from key's hash slot;
// 2. with every slot held, it blocks on its hash slot (the kernel queues the
// waiters) and, while blocked, tries the other slots every fetchRetry;
// whichever lock comes first is used and the other is released at once;
// 3. the wait is bounded by ctx: when it ends, HoldFetch returns ctx's error,
// and a lock the blocked wait gets after that is released at once.
func (l *Limiter) HoldFetch(ctx context.Context, key string) (func(), error) {
if err := ctx.Err(); err != nil {
return nil, err
}
n := l.cfg.FetchSlots
home := fetchSlot(key, n)
open := func(i int) (*os.File, error) {
return os.OpenFile(filepath.Join(l.cfg.Directory, fmt.Sprintf("fetch-%d", i)), os.O_CREATE|os.O_RDWR, 0o600)
}
// try takes a free slot among those try covers, without waiting; nil
// when every one is held.
try := func(skip int) (*os.File, error) {
for k := 0; k < n; k++ {
i := (home + k) % n
if i == skip {
continue
}
f, err := open(i)
if err != nil {
return nil, err
}
err = tryLock(f)
if err == nil {
return f, nil
}
_ = f.Close()
if !errors.Is(err, ErrBusy) {
return nil, err
}
}
return nil, nil
}
if f, err := try(-1); f != nil || err != nil {
if err != nil {
return nil, err
}
return releaseFetch(f), nil
}
homeF, err := open(home)
if err != nil {
return nil, err
}
locked := make(chan error, 1)
go func() { locked <- lockBlocking(homeF) }()
// abandon releases the blocked wait's lock once it gets it.
abandon := func() {
go func() {
<-locked
_ = homeF.Close()
}()
}
t := time.NewTicker(fetchRetry)
defer t.Stop()
for {
select {
case err := <-locked:
if err != nil {
_ = homeF.Close()
return nil, err
}
return releaseFetch(homeF), nil
case <-ctx.Done():
abandon()
return nil, ctx.Err()
case <-t.C:
f, err := try(home)
if err != nil {
abandon()
return nil, err
}
if f != nil {
abandon()
return releaseFetch(f), nil
}
}
}
}
// releaseFetch gives a fetch slot back, once: closing the file releases its
// lock.
func releaseFetch(f *os.File) func() {
var once sync.Once
return func() { once.Do(func() { _ = f.Close() }) }
}