From 57e30e11c0176eaaa5133debfd2c062dfbb54acf Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 21 Nov 2026 16:58:33 -0800 Subject: [PATCH] durable: a timer cut refused for held host slots waits instead of backing off Host admission is a try-lock. With one cut slot per host, every volume whose timer fires while another volume's cut holds the slot was refused and backed off 1 s doubling to 30 s, adding up to 30 s to its interval and favouring whichever volume happened to win. hostlimit returns ErrSlotsHeld (an ErrBusy) when every slot is held. The cut policy treats that refusal as a wait: no backoff, no minimum spacing, the timer is not reset, and the retry comes after the volume's stable slot jitter (at most 200 ms). Other errors, an oversized payload or an unusable pool included, keep the backoff. Status reports CutSchedule.HostSlotWaits and HostSlotWaitSinceNs, the start of the current wait (0 when not waiting). Ledger row INV-CUT-TIMER-SLOT-WAIT-001. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider.go | 10 +++- .../core/frontend/durable/provider_chunk.go | 60 +++++++++++++++++-- seaweed-block/docs/design/invariants.md | 3 +- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index fcc6756528..d8bcc6e4ca 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -299,6 +299,9 @@ type volHandle struct { // snapshots is the scheduled-snapshot state of the running loop, nil // when the schedule is off. snapshots *snapshotSlot + // cutWait is the automatic-cut timer's host slot waits, nil when the + // timer is off. + cutWait *cutWaitSlot } // VolumeStatus is a read-only diagnostic snapshot of one durable volume @@ -606,6 +609,7 @@ func (p *DurableProvider) DurableStatuses() []VolumeStatus { layer storage.ChunkLayer awaiting bool snapshots *snapshotSlot + cutWait *cutWaitSlot } p.mu.Lock() handles := make(map[string]*volHandle, len(p.volumes)) @@ -613,7 +617,7 @@ func (p *DurableProvider) DurableStatuses() []VolumeStatus { for volumeID, h := range p.volumes { handles[volumeID] = h if p.cfg.Impl == ImplWALStoreChunk { - chunks[volumeID] = chunkView{layer: h.chunk, awaiting: h.waitGen != 0, snapshots: h.snapshots} + chunks[volumeID] = chunkView{layer: h.chunk, awaiting: h.waitGen != 0, snapshots: h.snapshots, cutWait: h.cutWait} } } p.mu.Unlock() @@ -633,6 +637,10 @@ func (p *DurableProvider) DurableStatuses() []VolumeStatus { ss := cv.snapshots.status(time.Now()) cs.ScheduledSnapshot = &ss } + if cv.cutWait != nil { + cw := cv.cutWait.status() + cs.CutSchedule = &cw + } st.Chunk = cs } out = append(out, st) diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index e09347215a..7957c66377 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -333,9 +333,14 @@ func (p *DurableProvider) startChunkLoop(volumeID string, h *volHandle, layer st } else if h.snapshots == nil { h.snapshots = &snapshotSlot{s: newSnapshotSchedule(volumeID, p.cfg.ChunkSnapshotPeriod, time.Now())} } + if p.cfg.ChunkCaptureInterval <= 0 { + h.cutWait = nil + } else if h.cutWait == nil { + h.cutWait = &cutWaitSlot{} + } lin := h.chunkLineage current := func() bool { return p.maintenanceCurrent(volumeID, h, layer, lin) } - go p.chunkBackground(loopCtx, cancel, volumeID, layer, h.chunkDone, current, h.snapshots) + go p.chunkBackground(loopCtx, cancel, volumeID, layer, h.chunkDone, current, h.snapshots, h.cutWait) } func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h *volHandle) (storage.ChunkLayer, error) { @@ -646,6 +651,7 @@ const ( chunkCutMinSpacing = time.Second chunkCutBackoffMin = time.Second chunkCutBackoffMax = 30 * time.Second + chunkSlotJitterMax = 200 * time.Millisecond ) // cutPolicy decides when an automatic cut is due. The remote holds @@ -660,7 +666,11 @@ const ( // - a failed automatic cut resets nothing and is retried after a backoff // doubling from backoffMin to backoffMax; any later successful cut (a // no-op cut and an explicit cut included) clears the backoff and -// restarts the timer. +// restarts the timer; +// - an automatic cut refused because every host slot is held ran +// nothing: it is retried after the volume's slot jitter, with no +// backoff and no minimum spacing, and the wait lasts until a cut +// succeeds. type cutPolicy struct { interval, minSpacing, backoffMin, backoffMax time.Duration slotJitter time.Duration // this volume's delay before retrying a cut refused for held host slots @@ -696,6 +706,7 @@ func (s cutSchedule) observe(seq uint64, now time.Time) cutSchedule { s.lastSuccess = now } s.firstDue = time.Time{} + s.slotWaitSince = time.Time{} if !s.failedAt.IsZero() && seq != s.failedSeenSeq { s.backoff, s.retryAt, s.failedAt = 0, time.Time{}, time.Time{} } @@ -725,10 +736,19 @@ func (pol cutPolicy) due(s cutSchedule, now time.Time, reconciling bool) (string // after records the outcome of an automatic cut started at start, with // the layer's CutSeq read after it. func (pol cutPolicy) after(s cutSchedule, start, end time.Time, err error, seq uint64) cutSchedule { + if errors.Is(err, hostlimit.ErrSlotsHeld) { + s.slotWaits++ + if s.slotWaitSince.IsZero() { + s.slotWaitSince = start + } + s.retryAt = end.Add(pol.slotJitter) + return s + } s.lastAuto = start if err == nil { s.seenSeq = seq s.firstDue = time.Time{} + s.slotWaitSince = time.Time{} s.backoff, s.retryAt, s.failedAt = 0, time.Time{}, time.Time{} if end.After(s.lastSuccess) { s.lastSuccess = end @@ -770,8 +790,12 @@ func (p *DurableProvider) chunkLoop(ctx context.Context, volumeID string, layer minSpacing: chunkCutMinSpacing, backoffMin: chunkCutBackoffMin, backoffMax: chunkCutBackoffMax, + slotJitter: hostlimit.Phase(volumeID+":slot", chunkSlotJitterMax), } s := initialCutSchedule(volumeID, pol.interval, time.Now(), p.cfg.ChunkStagger, layer.Stats().CutSeq) + if wait != nil { + s.slotWaits = wait.status().HostSlotWaits + } t := time.NewTicker(chunkSchedulePoll) defer t.Stop() for { @@ -786,6 +810,7 @@ func (p *DurableProvider) chunkLoop(ctx context.Context, volumeID string, layer ls := layer.Stats() start := time.Now() s = s.observe(ls.CutSeq, start) + wait.record(s) reason, ok := pol.due(s, start, ls.Reconciling) if !ok { continue @@ -816,6 +841,7 @@ func (p *DurableProvider) chunkLoop(ctx context.Context, volumeID string, layer } cancel() s = pol.after(s, start, time.Now(), err, layer.Stats().CutSeq) + wait.record(s) if err != nil && !errors.Is(err, storage.ErrChunkCaptureRefused) && ctx.Err() == nil { log.Printf("durable: %s chunk cut for volume=%s: %v (retry in %s)", reason, volumeID, err, s.backoff) } @@ -825,7 +851,7 @@ func (p *DurableProvider) chunkLoop(ctx context.Context, volumeID string, layer // chunkBackground owns both workers and their cancellation. A fence is observed // even while either worker is waiting on a cooperative remote call. INV-MAINT-JOIN: // done closes only after every admitted worker has returned. -func (p *DurableProvider) chunkBackground(ctx context.Context, cancel context.CancelFunc, volumeID string, layer storage.ChunkLayer, done chan struct{}, current func() bool, snapshots *snapshotSlot) { +func (p *DurableProvider) chunkBackground(ctx context.Context, cancel context.CancelFunc, volumeID string, layer storage.ChunkLayer, done chan struct{}, current func() bool, snapshots *snapshotSlot, cutWait *cutWaitSlot) { defer close(done) defer cancel() var workers sync.WaitGroup @@ -834,7 +860,7 @@ func (p *DurableProvider) chunkBackground(ctx context.Context, cancel context.Ca go func() { defer workers.Done() cutDone := make(chan struct{}) - p.chunkLoop(ctx, volumeID, layer, cutDone, nil) + p.chunkLoop(ctx, volumeID, layer, cutDone, cutWait) }() } if p.cfg.ChunkMaintenanceInterval > 0 && p.cfg.ChunkMaintenance != nil { @@ -1365,5 +1391,27 @@ func (p *DurableProvider) publishSettledHook(volumeID string) func(context.Conte } // cutWaitSlot publishes the automatic-cut timer's host slot waits to -// status. -type cutWaitSlot struct{} +// status. A nil slot records nothing. +type cutWaitSlot struct { + mu sync.Mutex + st CutScheduleStatus +} + +func (w *cutWaitSlot) record(s cutSchedule) { + if w == nil { + return + } + st := CutScheduleStatus{HostSlotWaits: s.slotWaits} + if !s.slotWaitSince.IsZero() { + st.HostSlotWaitSinceNs = s.slotWaitSince.UnixNano() + } + w.mu.Lock() + w.st = st + w.mu.Unlock() +} + +func (w *cutWaitSlot) status() CutScheduleStatus { + w.mu.Lock() + defer w.mu.Unlock() + return w.st +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 7847a9c0a8..62e440807b 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -403,7 +403,7 @@ Tests: `TestSnapshotSchedule_SlotsAreOffsetPerVolume`, `_FailureIsTypedAndBacksO ## Part B. Invariant ledger -185 rows: 163 ACTIVE, 19 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 170 new. +186 rows: 164 ACTIVE, 19 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 171 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -599,6 +599,7 @@ Notes: | `INV-CUT-READMISS-001` | A block fetched from HEAD after a move is installed only if it does not overwrite a newer local extent version; a newer WAL-only write still wins on reads, and HEAD's value may be installed beneath it as the backing value, which is correct if a crash loses that unsynced write. | M4 Holder (lazy restore) | `core/storage/chunk_layer.go` `OnReadMiss`, `installFetchedBlock`; `core/storage/walstore.go` `readFromExtent` | `TestChunkLayer_WALOnlyOverlapReadNeverReturnsUntouchedZero` (`core/storage/chunk_layer_races_test.go`); `TestWALStore_PostReadMissRechecksWALFormat` (`core/storage/walstore_read_miss_v3_test.go`; real-store v2/v3 dispatch, v3 remains dark) | — | ACTIVE | | `INV-CUT-TIMER-RESTART-001` | A successful cut of any kind restarts the periodic timer, which runs on in-process monotonic time (a restart starts a fresh schedule; wall-clock steps move nothing). | M5 periodic trigger (before Admit) | `core/frontend/durable/provider_chunk.go` `chunkLoop` | `TestWALStoreChunk_ExplicitCutRestartsTheTimer`, `TestWALStoreChunk_ExplicitCutOverlappingTheTimerRestartsIt` (`core/frontend/durable/provider_chunk_sched_test.go`); `TestCutPolicy_TimerRunsOnObservationTime` (`core/frontend/durable/cut_policy_test.go`) | — | ACTIVE | | `INV-CUT-TIMER-SKIP-001` | A periodic cut decided from an older last cut is skipped under the capture lock: no automatic cut follows another within the interval. | M5 periodic trigger (before Admit) | `core/storage/chunk_periodic_cut.go` `CutUnlessCutSince`; `core/frontend/durable/provider_chunk.go` `chunkLoop` | `TestChunkPeriodicCut_SkipsAfterANewerCut`, `TestChunkPeriodicCut_SkipsANewerCutWithAnOlderStamp` (`core/storage/chunk_periodic_cut_test.go`); `TestWALStoreChunk_ExplicitCutOverlappingTheTimerRestartsIt` (`core/frontend/durable/provider_chunk_sched_test.go`) | — | ACTIVE | +| `INV-CUT-TIMER-SLOT-WAIT-001` | A periodic cut refused because every host cut slot is held is a wait, not a failure: no backoff and no minimum spacing; it retries after the volume's slot jitter (at most 200 ms), and status reports the wait count and start until a cut succeeds. Other errors keep the backoff. | M5 periodic trigger (before Admit) | `core/frontend/durable/provider_chunk.go` `cutPolicy.after`, `chunkLoop`; `core/hostlimit/limiter.go` `ErrSlotsHeld` | `TestCutPolicy_HostSlotBusyIsAWaitNotAFailure` (`core/frontend/durable/cut_policy_test.go`); `TestTimerCutRunsSoonAfterHostSlotFrees` (`core/frontend/durable/host_slot_wait_test.go`) | — | ACTIVE | A guest read has a cooperative arrival budget; internal contextless readers keep the owned fill timeout (default 30 s). With five immediate host retries, cold fills slower than roughly 20–24 s can still end in guest EIO despite shared work; retry-delay advertisement remains separate. A changed generation replaces its promise without abandoning the old worker. `AllBlocks` returns an error and no partial image on a failed read; the rebuild sender propagates it before sending blocks.