mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
The change of this PR's 22 commits, carried onto current main as one commit (its base was 1884 commits behind). Fitted to main: - pacing: main's rule that fallback-only leases are not paced stays; a healthy lease is paced at the budget over the held normal and urgent slots (this PR's rule) instead of over the configured slot count. TestFallbackDoesNotUnpaceAHealthyLease keeps its property (a healthy lease beside a fallback lease is paced) with a 500 ms deadline, since a lone holder now gets the whole budget. - the cut loop keeps main's attempted hook and afterStaging (a failed attempt that staged new chunks retries at the next poll), with the ticket handling; the random retry delay is gone, as this PR intends. - imports and constants: neither side's removed ones return. - invariants: main's new rows and this PR's new and restated rows; the slot-wait row is this PR's (ticket order, the next poll). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package hostlimit
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
// urgentHoldWait bounds how long urgent work waits for its shared hold on
|
|
// urgent-pending. A waiter holds the file exclusively only from its probe
|
|
// to its slot try-lock; one that stalled there must not block urgent work.
|
|
const urgentHoldWait = 200 * time.Millisecond
|
|
|
|
// HoldUrgentPending tells every limiter using dir that urgent work is
|
|
// pending on the host: until release is called, Normal admissions there
|
|
// are refused with ErrUrgentPending. Callers hold it across a stop or a
|
|
// drain. If the hold cannot be taken within 200 ms (a waiter stalled in its
|
|
// probe), held is false and release does nothing: the caller proceeds
|
|
// without it, since the slots themselves are the exclusion.
|
|
func HoldUrgentPending(dir string) (release func(), held bool, err error) {
|
|
f, held, err := holdSharedBounded(filepath.Join(dir, urgentPendingFile))
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
if !held {
|
|
log.Printf("hostlimit: urgent-pending in %s stayed held exclusively for %s; proceeding without the hold", dir, urgentHoldWait)
|
|
return func() {}, false, nil
|
|
}
|
|
return func() { _ = f.Close() }, true, nil
|
|
}
|
|
|
|
// holdSharedBounded opens path and takes a shared lock on it, retrying for
|
|
// up to urgentHoldWait. It reports held=false (and no error) when the lock
|
|
// stayed taken exclusively for that long.
|
|
func holdSharedBounded(path string) (*os.File, bool, error) {
|
|
if i, err := os.Lstat(path); err == nil && !i.Mode().IsRegular() {
|
|
return nil, false, &os.PathError{Op: "open", Path: path, Err: os.ErrInvalid}
|
|
}
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
deadline := time.Now().Add(urgentHoldWait)
|
|
for {
|
|
err = tryLockShared(f)
|
|
if err == nil {
|
|
return f, true, nil
|
|
}
|
|
if !errors.Is(err, ErrBusy) {
|
|
_ = f.Close()
|
|
return nil, false, err
|
|
}
|
|
if time.Now().After(deadline) {
|
|
_ = f.Close()
|
|
return nil, false, nil
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|