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 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 09:33:04 -08:00
1 parent 3228a6802b
commit 53e1de8120
5 files changed
+104 -1

No files matched your search

+7 -1
View File
@@ -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]
@@ -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")
}
}
@@ -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
@@ -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()
@@ -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)
}
}