feat: track WAL hold inactivity separately for each command

This commit is contained in:
pingqiu committed 2026-12-02 04:12:10 -08:00
1 parent 84111fd66f
commit cf98b6928c
2 files changed
+110

No files matched your search

@@ -0,0 +1,43 @@
package storage
import "time"
const walReclaimWindow = 5 * time.Second
type walHoldReason string
const (
walReclaimStalled walHoldReason = "wal_reclaim_stalled"
walHoldCap walHoldReason = "wal_hold_cap"
)
// walHoldWindow belongs to one command. Global reclaim observations may renew
// its inactivity deadline, but can never shorten its initial window or move
// its absolute command cap. The admission waiter owns it; it needs no lock.
type walHoldWindow struct {
capAt time.Time
progressAt time.Time
}
func newWALHoldWindow(arrival, holdStarted time.Time, maxHold time.Duration) walHoldWindow {
return walHoldWindow{capAt: arrival.Add(maxHold), progressAt: holdStarted}
}
// observeProgress is called only when a same-scope checkpoint advanced or
// the current flusher cycle successfully wrote new extent bytes. An unchanged
// sample, failed write, phase heartbeat or empty cycle must not call it.
func (w *walHoldWindow) observeProgress(at time.Time) {
if at.After(w.progressAt) {
w.progressAt = at
}
}
func (w *walHoldWindow) expired(now time.Time) walHoldReason {
if !now.Before(w.capAt) {
return walHoldCap
}
if !now.Before(w.progressAt.Add(walReclaimWindow)) {
return walReclaimStalled
}
return ""
}
@@ -0,0 +1,67 @@
package storage
import (
"testing"
"time"
)
func TestWALHoldWindowRetryGetsOwnStallWindow(t *testing.T) {
now := time.Now()
oldProgress := now.Add(-20 * time.Second)
w := newWALHoldWindow(now, now, 25*time.Second)
w.observeProgress(oldProgress)
if got := w.expired(now); got != "" {
t.Fatalf("retry immediately stalled from global sample: %s", got)
}
if got := w.expired(now.Add(5*time.Second - time.Nanosecond)); got != "" {
t.Fatalf("premature stall: %s", got)
}
if got := w.expired(now.Add(5 * time.Second)); got != walReclaimStalled {
t.Fatalf("stall boundary: %s", got)
}
// A later command cannot inherit an earlier command's elapsed window.
later := newWALHoldWindow(now.Add(3*time.Second), now.Add(3*time.Second), 25*time.Second)
later.observeProgress(now)
if got := later.expired(now.Add(5 * time.Second)); got != "" {
t.Fatalf("shared window stalled newer command: %s", got)
}
}
func TestWALHoldWindowProgressDoesNotRenewCap(t *testing.T) {
now := time.Now()
w := newWALHoldWindow(now, now, 25*time.Second)
for sec := 4; sec <= 24; sec += 4 {
at := now.Add(time.Duration(sec) * time.Second)
if got := w.expired(at); got != "" {
t.Fatalf("live reclaim stalled at %ds: %s", sec, got)
}
w.observeProgress(at)
}
if got := w.expired(now.Add(25*time.Second - time.Nanosecond)); got != "" {
t.Fatalf("early cap: %s", got)
}
if got := w.expired(now.Add(25 * time.Second)); got != walHoldCap {
t.Fatalf("progress extended cap: %s", got)
}
}
func TestWALHoldWindowStartsCapAtCommandArrival(t *testing.T) {
now := time.Now()
w := newWALHoldWindow(now, now.Add(23*time.Second), 25*time.Second)
if got := w.expired(now.Add(25 * time.Second)); got != walHoldCap {
t.Fatalf("queue time escaped cap: %s", got)
}
}
func TestWALHoldWindowOlderObservationCannotMoveWindowBack(t *testing.T) {
now := time.Now()
w := newWALHoldWindow(now, now, 25*time.Second)
w.observeProgress(now.Add(4 * time.Second))
w.observeProgress(now.Add(time.Second))
if got := w.expired(now.Add(8 * time.Second)); got != "" {
t.Fatalf("older observation shortened window: %s", got)
}
if got := w.expired(now.Add(9 * time.Second)); got != walReclaimStalled {
t.Fatalf("no-progress window: %s", got)
}
}