storage: report freeze and extra CAS failure-path allowance

This commit is contained in:
pingqiu committed 2026-11-20 03:52:05 -08:00
1 parent 9faf91a36b
commit 1a97fc9638
3 files changed
+67 -8

No files matched your search

@@ -870,11 +870,14 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st
// the cut gets three quarters, the settlement what is left. The bound is
// cooperative (contexts): a syscall that never returns is not bounded here.
var start time.Time
cutCtx, cancel := context.WithCancelCause(ctx)
accountCtx, countCAS := storage.WithChunkCASAccounting(ctx)
var casAtFreeze uint64
cutCtx, cancel := context.WithCancelCause(accountCtx)
defer cancel(context.Canceled)
var timer *time.Timer
beginFreeze := func() {
start = time.Now()
casAtFreeze = countCAS()
timer = time.AfterFunc(freeze*3/4, func() { cancel(context.DeadlineExceeded) })
}
defer func() {
@@ -907,6 +910,9 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st
})
log.Printf("durable: pre-copy volume=%s attempts=%d tail=%d rate=%.0f sample_age=%s fixed=%s estimate=%s frozen=%t reason=%s err=%v", volumeID, r.Precopy.Attempts, r.Precopy.TailBytes, r.Precopy.BytesPerSecond, r.Precopy.SampleAge, r.Precopy.Fixed, r.Precopy.Estimate, r.Precopy.Frozen, r.Precopy.Reason, err)
if err == nil {
if !start.IsZero() {
r.Precopy.Finish(time.Since(start), 0, false)
}
return r, h.storage.NextLSN() - 1, nil
}
if !r.Precopy.Frozen {
@@ -940,13 +946,23 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st
// resumes: an earlier detach that applied, an unresolved detach CAS or a
// fenced layer must never resume serving. The settlement keeps the rest
// of the freeze budget even when the caller's context is gone.
sctx, scancel := context.WithDeadline(context.WithoutCancel(ctx), start.Add(freeze))
sctx, scancel := context.WithDeadline(context.WithoutCancel(cutCtx), start.Add(freeze))
defer scancel()
if trace := p.testDetachTrace; trace != nil {
dl, _ := sctx.Deadline()
trace("settle", dl)
}
how, dr, serr := layer.SettleDetach(sctx)
if p.cfg.ChunkDetachPrecopy {
attempts := countCAS() - casAtFreeze
extra := uint64(0)
if attempts > 0 {
extra = attempts - 1
}
r.Precopy.Finish(time.Since(start), extra, true)
log.Printf("durable: pre-copy settlement volume=%s elapsed=%s extra_cas=%d declared_worst=%s latency_gate_failed=%t outcome=%v err=%v", volumeID, r.Precopy.FreezeElapsed, extra, r.Precopy.DeclaredWorst, r.Precopy.LatencyGateFailed, how, serr)
dr.Precopy = r.Precopy
}
switch {
case serr != nil:
return storage.ChunkCutResult{}, 0, fmt.Errorf("%w: %w (settle: %w)", ErrDetachOutcomeUnknown, err, serr)
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"sync/atomic"
"time"
)
@@ -19,9 +20,25 @@ func validateHeadCoolingWindow(window time.Duration) error {
return nil
}
type chunkCASCountKey struct{}
// WithChunkCASAccounting counts actual CAS attempts through final publication
// and its settlement. Reusing its context preserves the same operation scope.
func WithChunkCASAccounting(ctx context.Context) (context.Context, func() uint64) {
counter, _ := ctx.Value(chunkCASCountKey{}).(*atomic.Uint64)
if counter == nil {
counter = &atomic.Uint64{}
ctx = context.WithValue(ctx, chunkCASCountKey{}, counter)
}
return ctx, counter.Load
}
func casChunkHead(ctx context.Context, remote chunkRemote, expected chunkHeadVersion, next chunkHead) (uint64, error) {
ctx, cancel := context.WithTimeout(ctx, chunkHeadOperationTimeout)
defer cancel()
if counter, _ := ctx.Value(chunkCASCountKey{}).(*atomic.Uint64); counter != nil {
counter.Add(1)
}
return remote.CASHead(ctx, expected, next)
}
+32 -6
View File
@@ -27,12 +27,28 @@ type precopySample struct {
// PrecopyReport records admission estimates, not a worst-case latency proof.
type PrecopyReport struct {
Attempts int
TailBytes uint64
Fixed, Estimate, SampleAge time.Duration
BytesPerSecond float64
Reason string
Frozen bool
Attempts int `json:"attempts"`
TailBytes uint64 `json:"tailBytes"`
Fixed time.Duration `json:"fixedNs"`
Estimate time.Duration `json:"estimateNs"`
SampleAge time.Duration `json:"sampleAgeNs"`
BytesPerSecond float64 `json:"bytesPerSecond"`
Reason string `json:"reason"`
Frozen bool `json:"frozen"`
FreezeElapsed time.Duration `json:"freezeElapsedNs"`
ExtraCASAttempts uint64 `json:"extraCASAttempts"`
CASFailureTimeout time.Duration `json:"casFailureTimeoutNs"`
DeclaredWorst time.Duration `json:"declaredWorstNs"`
LatencyGateFailed bool `json:"latencyGateFailed"`
}
// Finish records the whole freeze, including any owner-driven settlement.
// D_cas is failure allowance only; it never enters successful admission.
func (r *PrecopyReport) Finish(elapsed time.Duration, extraCAS uint64, failed bool) {
r.FreezeElapsed, r.ExtraCASAttempts = elapsed, extraCAS
r.CASFailureTimeout = chunkHeadOperationTimeout
r.DeclaredWorst = r.Estimate + time.Duration(extraCAS)*chunkHeadOperationTimeout
r.LatencyGateFailed = failed || elapsed > precopyAllowance
}
// PrecopyDetach runs rolling cuts before the final detached cut. stopIO is
@@ -52,9 +68,12 @@ func (l *chunkLayerImpl) PrecopyDetach(ctx context.Context, stopIO func() bool)
r.Precopy.Frozen = true
return r, nil
}
ctx, countCAS := WithChunkCASAccounting(ctx)
ctx, cancel := context.WithTimeout(ctx, precopyDuration)
defer cancel()
var report PrecopyReport
var frozenAt time.Time
var casAtFreeze uint64
l.precopyFreeze = func(tail uint64) bool {
report = l.precopyEstimate(tail, report.Attempts)
if report.Reason != "fit" {
@@ -65,6 +84,7 @@ func (l *chunkLayerImpl) PrecopyDetach(ctx context.Context, stopIO func() bool)
return false
}
report.Frozen = true
frozenAt, casAtFreeze = time.Now(), countCAS()
return true
}
defer func() { l.precopyFreeze = nil }()
@@ -76,6 +96,12 @@ func (l *chunkLayerImpl) PrecopyDetach(ctx context.Context, stopIO func() bool)
r, err := l.captureLocked(ctx, false)
r.Precopy = report
if report.Frozen {
attempts := countCAS() - casAtFreeze
extra := uint64(0)
if attempts > 0 {
extra = attempts - 1
}
r.Precopy.Finish(time.Since(frozenAt), extra, err != nil)
return r, err
}
if err != nil {