hostlimit, durable: red tests, ticket order holds on retry and refusal

- TestChunkLoop_AConsumedAttemptRetriesAtTheBack: a cut admitted on its
  ticket that then fails after staging is retried on a fresh ticket at the
  back. Fails here: the retry's ticket is due at the schedule's old time
  (even earlier than its first one), ahead of every cut that waited.
- TestChunkLoop_NoTicketNoCut: a timer cut that cannot take a ticket must
  not cut un-ticketed. Fails here: it cut twice.
- TestRefusedUrgentAdmissionReleasesItsPendingHold: an urgent admission
  refused after it took its urgent-pending hold gives the hold back. Fails
  here: the hold leaks, keeping Normal admissions refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 10:54:03 -08:00
1 parent fd6701e86b
commit 9533d41b24
2 files changed
+183

No files matched your search

@@ -0,0 +1,130 @@
package durable
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/hostlimit"
"github.com/seaweedfs/seaweed-block/core/storage"
)
// recordingBudget is a real limiter that records each ticket's due time, and
// can fail ticket creation.
type recordingBudget struct {
*hostlimit.Limiter
mu sync.Mutex
dues []int64
fail error
}
func (b *recordingBudget) TakeTicket(due int64) (*hostlimit.Ticket, error) {
b.mu.Lock()
fail := b.fail
b.mu.Unlock()
if fail != nil {
return nil, fail
}
t, err := b.Limiter.TakeTicket(due)
if err == nil {
b.mu.Lock()
b.dues = append(b.dues, due)
b.mu.Unlock()
}
return t, err
}
func (b *recordingBudget) taken() []int64 {
b.mu.Lock()
defer b.mu.Unlock()
return append([]int64(nil), b.dues...)
}
// admittingLayer admits each cut through the budget (the limiter consumes
// its ticket), stages a chunk and then fails, as a cut that fails after
// staging does; admissions counts the admissions it made.
type admittingLayer struct {
storage.ChunkLayer // unused methods panic
budget storage.Budget
cuts, admissions atomic.Int32
staged atomic.Uint64
}
func (l *admittingLayer) Fenced() bool { return false }
func (l *admittingLayer) Stats() storage.ChunkLayerStats {
return storage.ChunkLayerStats{StagedChunks: l.staged.Load()}
}
func (l *admittingLayer) Cut(ctx context.Context) (storage.ChunkCutResult, error) {
l.cuts.Add(1)
id, err := l.budget.Admit(ctx, 1, nil)
if err != nil {
return storage.ChunkCutResult{}, err
}
l.admissions.Add(1)
l.budget.Release(id)
l.staged.Add(1)
return storage.ChunkCutResult{}, errors.New("test: the cut failed after staging")
}
// startLoop runs the cut loop and registers its end: cancel, then a bounded
// join, on every path of the test.
func startLoop(t *testing.T, p *DurableProvider, layer storage.ChunkLayer) {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go p.chunkLoop(ctx, "v1", layer, done, &cutWaitSlot{}, nil)
t.Cleanup(func() {
cancel()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Error("the cut loop did not end within 5 s of its cancel")
}
})
}
func newRecordingBudget(t *testing.T) *recordingBudget {
t.Helper()
lim, err := hostlimit.New(hostlimit.DefaultConfig(t.TempDir()))
if err != nil {
t.Fatal(err)
}
t.Cleanup(lim.Close)
return &recordingBudget{Limiter: lim}
}
// A cut admitted on its ticket that then fails is retried on a fresh ticket
// at the back of the queue (the host's now), never again at its old due
// time, which would put it ahead of every cut that waited meanwhile.
func TestChunkLoop_AConsumedAttemptRetriesAtTheBack(t *testing.T) {
b := newRecordingBudget(t)
layer := &admittingLayer{budget: b}
p := &DurableProvider{cfg: ProviderConfig{ChunkCaptureInterval: time.Millisecond, ChunkBudget: b}}
startLoop(t, p, layer)
for deadline := time.Now().Add(5 * time.Second); len(b.taken()) < 2; time.Sleep(5 * time.Millisecond) {
if time.Now().After(deadline) {
t.Fatalf("the loop took %d tickets, want a retry's second one", len(b.taken()))
}
}
dues := b.taken()
if dues[1] <= dues[0] {
t.Fatalf("the retry's ticket is due at %d, not after its first one at %d: it re-entered at its old place", dues[1], dues[0])
}
}
// A timer cut that cannot take a ticket does not cut un-ticketed (it would
// pass every earlier ticket): it waits and tries again at the next poll.
func TestChunkLoop_NoTicketNoCut(t *testing.T) {
b := newRecordingBudget(t)
b.fail = errors.New("test: the tickets directory is unavailable")
layer := &admittingLayer{budget: b}
p := &DurableProvider{cfg: ProviderConfig{ChunkCaptureInterval: time.Millisecond, ChunkBudget: b}}
startLoop(t, p, layer)
time.Sleep(300 * time.Millisecond)
if n := layer.cuts.Load(); n != 0 {
t.Fatalf("the loop cut %d times without a ticket", n)
}
}
@@ -0,0 +1,53 @@
package hostlimit
import (
"context"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
)
// cancelledAfterFirstCheck is a context whose first Err check passes and
// every later one reports cancellation: a cancel that lands between the
// admission's pool step and its final check.
type cancelledAfterFirstCheck struct {
context.Context
checks atomic.Int32
}
func (c *cancelledAfterFirstCheck) Err() error {
if c.checks.Add(1) == 1 {
return nil
}
return context.Canceled
}
// An urgent admission refused after it took its urgent-pending hold (here: a
// cancel at its final check) gives the hold back: no Normal admission stays
// refused for an urgent cut that never ran.
func TestRefusedUrgentAdmissionReleasesItsPendingHold(t *testing.T) {
c := testConfig(t)
l := newTest(t, c)
ctx := &cancelledAfterFirstCheck{Context: WithClass(context.Background(), Urgent)}
if _, err := l.Admit(ctx, 1, nil); err == nil {
t.Fatal("the control needs an urgent admission refused at its final check")
}
probe, err := os.OpenFile(filepath.Join(c.Directory, urgentPendingFile), os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer probe.Close()
deadline := time.Now().Add(time.Second)
for {
err = tryLock(probe)
if err == nil {
return
}
if time.Now().After(deadline) {
t.Fatalf("the urgent-pending file is still held after the refused admission: %v", err)
}
time.Sleep(10 * time.Millisecond)
}
}