diff --git a/seaweed-block/core/hostlimit/limiter.go b/seaweed-block/core/hostlimit/limiter.go index 1ec81dc98d..f7dc887019 100644 --- a/seaweed-block/core/hostlimit/limiter.go +++ b/seaweed-block/core/hostlimit/limiter.go @@ -100,6 +100,7 @@ type lease struct { type Limiter struct { testBeforeConfigWrite func() // test-only crash boundary; immutable before first admission testBeforeConfigLock func(*os.File) // test-only fault boundary; immutable before first admission + testExpireRace func() // tests: runs between a hold's expiry decision and its revocation cfg Config mu sync.Mutex admitWG sync.WaitGroup @@ -385,6 +386,9 @@ func (l *Limiter) expire(id uint64) { return } l.mu.Unlock() + if h := l.testExpireRace; h != nil { + h() // tests: a progress report racing the expiry's decision + } l.revoke(id) } diff --git a/seaweed-block/core/hostlimit/limiter_review_test.go b/seaweed-block/core/hostlimit/limiter_review_test.go new file mode 100644 index 0000000000..bad81e6bc2 --- /dev/null +++ b/seaweed-block/core/hostlimit/limiter_review_test.go @@ -0,0 +1,100 @@ +package hostlimit + +import ( + "context" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" +) + +// A progress report that lands while a hold's expiry is being decided is +// honoured: the decision and the revocation are one step, re-checking the +// last progress, so a hold that progressed is never revoked for it. +func TestProgressRacingTheExpiryIsHonoured(t *testing.T) { + c := testConfig(t) + c.NoProgress = 50 * time.Millisecond + l := newTest(t, c) + revoked := make(chan struct{}, 1) + x, err := l.Admit(context.Background(), 1, func(error) { revoked <- struct{}{} }) + if err != nil { + t.Fatal(err) + } + defer l.Release(x) + raced := make(chan struct{}) + var once sync.Once + l.testExpireRace = func() { once.Do(func() { l.Progress(x); close(raced) }) } + select { + case <-raced: + case <-time.After(2 * time.Second): + t.Fatal("the expiry never ran") + } + select { + case <-revoked: + t.Fatal("a hold whose progress landed during its expiry was revoked") + case <-time.After(20 * time.Millisecond): + } +} + +// Progress never waits for the limiter's lock, which admission holds across +// file-system work: a cut's progress tick cannot stall behind an admission. +func TestProgressDoesNotWaitForTheLimiterLock(t *testing.T) { + l := newTest(t, testConfig(t)) + x, err := l.Admit(context.Background(), 1, nil) + if err != nil { + t.Fatal(err) + } + defer l.Release(x) + l.mu.Lock() + done := make(chan struct{}) + go func() { l.Progress(x); close(done) }() + select { + case <-done: + l.mu.Unlock() + case <-time.After(time.Second): + l.mu.Unlock() + <-done + t.Fatal("Progress waited for the limiter's lock") + } +} + +// The window is measured on the monotonic clock: a hold's progress stamp is +// an elapsed time since the limiter's own base, never a wall-clock timestamp +// (a wall-clock step, an NTP correction or a VM resume, would otherwise revoke +// every progressing hold, or keep a stalled one). +func TestProgressStampIsMonotonicElapsed(t *testing.T) { + l := newTest(t, testConfig(t)) + x, err := l.Admit(context.Background(), 1, nil) + if err != nil { + t.Fatal(err) + } + defer l.Release(x) + l.Progress(x) + l.mu.Lock() + stamp := l.live[x].lastProgress.Load() + l.mu.Unlock() + if d := time.Duration(stamp); d < 0 || d > time.Hour { + t.Fatalf("progress stamp %d is not an elapsed time on the limiter's monotonic base", stamp) + } +} + +// The launch check's refusal of a pool written with other flags names the +// file and the cure: after an upgrade it is the old binary's file, and no +// process may be using the directory at all. +func TestLaunchMismatchNamesTheFileAndTheCure(t *testing.T) { + c := DefaultConfig(t.TempDir()) + if err := os.WriteFile(filepath.Join(c.Directory, "configuration.json"), []byte(`{"Directory":"elsewhere"}`), 0o600); err != nil { + t.Fatal(err) + } + err := checkLaunch(c, nil, func(string) (string, bool) { return "", false }) + if err == nil { + t.Fatal("a pool with another configuration was accepted") + } + for _, want := range []string{"configuration.json", "stop every process", "remove"} { + if !strings.Contains(err.Error(), want) { + t.Fatalf("launch refusal %q does not name %q", err, want) + } + } +}