From 53e1de81205dee8567bd8327fa4519a8f09517dc Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 09:31:32 -0800 Subject: [PATCH] hostlimit, storage: red tests, a hold is renewed by its work's progress Seams: Config.NoProgress (default 60 s), Limiter.Progress (a no-op for now) and storage.BudgetProgress, the optional Budget that renews a hold. - TestStalledHoldIsRevokedWithinTheWindow: a hold with no progress is revoked within its no-progress window. Fails here: the fixed 5-minute hold limit still governs. - TestProgressRenewsTheHold: a hold whose work keeps progressing is never revoked; once the progress stops, it is revoked within the window. Fails here on the second half. - TestCutReportsProgressToItsBudget: a cut reports progress at least once per uploaded chunk. Fails here: no report. (A hydrate-all takes one permit per chunk fill, so its hold is one fetch and needs no renewal.) Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/hostlimit/limiter.go | 8 ++- .../core/hostlimit/limiter_progress_test.go | 62 +++++++++++++++++++ .../core/storage/chunk_cut_budget.go | 6 ++ .../core/storage/chunk_cut_budget_test.go | 4 ++ .../storage/chunk_cut_hold_progress_test.go | 25 ++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 seaweed-block/core/hostlimit/limiter_progress_test.go create mode 100644 seaweed-block/core/storage/chunk_cut_hold_progress_test.go diff --git a/seaweed-block/core/hostlimit/limiter.go b/seaweed-block/core/hostlimit/limiter.go index 1861fdb34b..754e8189e3 100644 --- a/seaweed-block/core/hostlimit/limiter.go +++ b/seaweed-block/core/hostlimit/limiter.go @@ -59,10 +59,12 @@ type Config struct { BytesPerSecond int64 FallbackVolumes int MaxHold time.Duration + // NoProgress is the hold's no-progress window (seam). + NoProgress time.Duration } 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, MaxHold: 5 * time.Minute} + return Config{Directory: dir, NormalSlots: 3, UrgentSlots: 1, FillSlots: 2, IdleSlots: 1, PayloadBytes: 64 << 20, COWBytes: 1 << 30, BytesPerSecond: 64 << 20, FallbackVolumes: 200, MaxHold: 5 * time.Minute, NoProgress: 60 * time.Second} } 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.MaxHold <= 0 { @@ -350,6 +352,10 @@ func (l *Limiter) Admit(ctx context.Context, payload int64, abort func(error)) ( } return id, nil } + +// Progress reports that the work holding id progressed (seam). +func (l *Limiter) Progress(id uint64) {} + func (l *Limiter) revoke(id uint64) { l.mu.Lock() x := l.live[id] diff --git a/seaweed-block/core/hostlimit/limiter_progress_test.go b/seaweed-block/core/hostlimit/limiter_progress_test.go new file mode 100644 index 0000000000..08df88edfd --- /dev/null +++ b/seaweed-block/core/hostlimit/limiter_progress_test.go @@ -0,0 +1,62 @@ +package hostlimit + +import ( + "context" + "testing" + "time" +) + +// A hold whose work reports no progress is revoked within its no-progress +// window W: not before W, and not much after it. +func TestStalledHoldIsRevokedWithinTheWindow(t *testing.T) { + c := testConfig(t) + c.NoProgress = 100 * time.Millisecond + l := newTest(t, c) + revoked := make(chan time.Time, 1) + start := time.Now() + x, err := l.Admit(context.Background(), 1, func(error) { revoked <- time.Now() }) + if err != nil { + t.Fatal(err) + } + defer l.Release(x) + select { + case at := <-revoked: + if d := at.Sub(start); d < 80*time.Millisecond || d > 400*time.Millisecond { + t.Fatalf("revoked after %s, want about the window (%s)", d, c.NoProgress) + } + case <-time.After(2 * time.Second): + t.Fatalf("a stalled hold was not revoked within its no-progress window (%s)", c.NoProgress) + } +} + +// A hold whose work keeps progressing is never revoked, however long it is +// held; once the progress stops, it is revoked within the window. +func TestProgressRenewsTheHold(t *testing.T) { + c := testConfig(t) + c.NoProgress = 100 * time.Millisecond + l := newTest(t, c) + revoked := make(chan time.Time, 1) + x, err := l.Admit(context.Background(), 1, func(error) { revoked <- time.Now() }) + if err != nil { + t.Fatal(err) + } + defer l.Release(x) + until := time.Now().Add(6 * c.NoProgress) + for time.Now().Before(until) { + l.Progress(x) + select { + case <-revoked: + t.Fatal("a progressing hold was revoked") + case <-time.After(20 * time.Millisecond): + } + } + stopped := time.Now() + select { + case at := <-revoked: + if d := at.Sub(stopped); d > 400*time.Millisecond { + t.Fatalf("revoked %s after the progress stopped, want within the window (%s)", d, c.NoProgress) + } + case <-time.After(2 * time.Second): + t.Fatal("the hold was not revoked once its progress stopped") + } +} diff --git a/seaweed-block/core/storage/chunk_cut_budget.go b/seaweed-block/core/storage/chunk_cut_budget.go index 7b3aa9a82f..1affc5d6ba 100644 --- a/seaweed-block/core/storage/chunk_cut_budget.go +++ b/seaweed-block/core/storage/chunk_cut_budget.go @@ -16,6 +16,12 @@ type Budget interface { Release(ticket uint64) } +// BudgetProgress is a Budget that renews a hold while its work progresses +// (seam). +type BudgetProgress interface { + Progress(ticket uint64) +} + type cutBudgetReservation struct { budget Budget ticket uint64 diff --git a/seaweed-block/core/storage/chunk_cut_budget_test.go b/seaweed-block/core/storage/chunk_cut_budget_test.go index b0d87fcdc9..16c4b424d5 100644 --- a/seaweed-block/core/storage/chunk_cut_budget_test.go +++ b/seaweed-block/core/storage/chunk_cut_budget_test.go @@ -16,8 +16,12 @@ type testCutBudget struct { payload, saved int64 abort func(error) probe func() + progressed int } +// Progress counts the cut's progress reports (BudgetProgress). +func (b *testCutBudget) Progress(uint64) { b.mu.Lock(); b.progressed++; b.mu.Unlock() } + func (b *testCutBudget) Admit(_ context.Context, payload int64, abort func(error)) (uint64, error) { b.mu.Lock() defer b.mu.Unlock() diff --git a/seaweed-block/core/storage/chunk_cut_hold_progress_test.go b/seaweed-block/core/storage/chunk_cut_hold_progress_test.go new file mode 100644 index 0000000000..d38f3bb6ad --- /dev/null +++ b/seaweed-block/core/storage/chunk_cut_hold_progress_test.go @@ -0,0 +1,25 @@ +package storage + +import ( + "testing" +) + +// A cut reports its progress to its budget at least once per chunk it +// uploads, so a hold renewed by progress is never revoked while it works. +func TestCutReportsProgressToItsBudget(t *testing.T) { + _, l, f, gate := cowFixture(t) + gate.unblock() + b := &testCutBudget{} + l.cfg.Budget = b + before := f.stats().puts + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + uploaded := f.stats().puts - before + b.mu.Lock() + progressed := b.progressed + b.mu.Unlock() + if uploaded < 1 || progressed < uploaded { + t.Fatalf("the cut uploaded %d chunks and reported progress %d times, want at least once per chunk", uploaded, progressed) + } +}