durable: wire opt-in pre-copy into settled detach lifecycle

This commit is contained in:
pingqiu committed 2026-11-20 03:43:25 -08:00
1 parent c64244c61d
commit 6f771c8169
6 files changed
+85 -18

No files matched your search

@@ -29,6 +29,7 @@ const (
)
type chunkFlags struct {
detachPrecopy bool
filer string
path string
collection string
@@ -47,7 +48,7 @@ func validateChunkFlags(f flags) error {
if f.durableImpl != "walstore-chunk" {
if c.filer != "" || c.collection != "" || c.replication != "" || c.path != defaultChunkPath ||
c.captureInterval != defaultChunkCaptureInterval || c.dirtyThreshold != defaultChunkDirtyThreshold ||
c.uploadConcurrency != 8 || c.hydrationWorkers != 2 || c.hydrationQueueDepth != 64 || c.controlEndpoint {
c.uploadConcurrency != 8 || c.hydrationWorkers != 2 || c.hydrationQueueDepth != 64 || c.controlEndpoint || c.detachPrecopy {
return errors.New("--chunk-* flags require --durable-impl=walstore-chunk")
}
return nil
+2
View File
@@ -198,6 +198,7 @@ func parseFlags(args []string) (flags, error) {
fs.StringVar(&f.chunk.path, "chunk-path", defaultChunkPath, "walstore-chunk: filer directory holding one directory per volume (HEAD and chunk key bindings)")
fs.StringVar(&f.chunk.collection, "chunk-collection", "", "walstore-chunk: collection for chunk needles (empty = filer default / storage rule)")
fs.StringVar(&f.chunk.replication, "chunk-replication", "", "walstore-chunk: replication for chunk needles (empty = filer default / storage rule)")
fs.BoolVar(&f.chunk.detachPrecopy, "chunk-detach-precopy", false, "walstore-chunk: converge rolling cuts before final detach; refuse cutover if measured tail does not fit")
fs.DurationVar(&f.chunk.captureInterval, "chunk-capture-interval", defaultChunkCaptureInterval, "walstore-chunk: automatic cut once this long has passed since the last successful cut; 0 (default) disables the timer, e.g. 15m as a host-loss safety net")
fs.IntVar(&f.chunk.uploadConcurrency, "chunk-upload-concurrency", 8, "walstore-chunk: maximum concurrent dirty-chunk uploads (1..64)")
fs.IntVar(&f.chunk.hydrationWorkers, "chunk-hydration-workers", 2, "walstore-chunk: concurrent lazy chunk fetches per volume (1..64)")
@@ -655,6 +656,7 @@ func run(f flags) int {
if cfg.Impl == durable.ImplWALStoreChunk {
chunk, chunkErr = newChunkWiring(f.chunk)
cfg.ChunkCaptureInterval = f.chunk.captureInterval
cfg.ChunkDetachPrecopy = f.chunk.detachPrecopy
cfg.ChunkDirtyThreshold = f.chunk.dirtyThreshold
cfg.ChunkUploadConcurrency = f.chunk.uploadConcurrency
cfg.ChunkHydrationWorkers = f.chunk.hydrationWorkers
@@ -179,6 +179,9 @@ type ProviderConfig struct {
// host's 30 s I/O timeout; a freeze past the 5 s target is logged as an
// alarm. Cooperative: a syscall that never returns is not bounded.
DetachFreezeTimeout time.Duration
// ChunkDetachPrecopy attempts bounded rolling cuts before stopping I/O.
// Nonconvergence leaves service live; no fallback to an unbounded tail.
ChunkDetachPrecopy bool
// ChunkDirtyThreshold is the number of candidate chunks above which
// a cut pre-copies (stages) chunks before its barrier, in rounds of
// this size; copy-before-write copies the rest outside the barrier.
@@ -869,30 +869,71 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st
// One freeze budget covers the drain, the final cut AND the settlement:
// the cut gets three quarters, the settlement what is left. The bound is
// cooperative (contexts): a syscall that never returns is not bounded here.
start := time.Now()
cutCtx, cancel := context.WithDeadline(ctx, start.Add(freeze*3/4))
var start time.Time
cutCtx, cancel := context.WithCancel(ctx)
defer cancel()
var timer *time.Timer
beginFreeze := func() {
start = time.Now()
timer = time.AfterFunc(freeze*3/4, cancel)
}
defer func() {
if d := time.Since(start); d > detachFreezeTarget {
log.Printf("durable: ALARM detach of volume=%s held guest I/O for %s (target %s, bound %s)", volumeID, d.Round(time.Millisecond), detachFreezeTarget, freeze)
if timer != nil {
timer.Stop()
}
if !start.IsZero() {
if d := time.Since(start); d > detachFreezeTarget {
log.Printf("durable: ALARM detach of volume=%s held guest I/O for %s (target %s, bound %s)", volumeID, d.Round(time.Millisecond), detachFreezeTarget, freeze)
}
}
}()
if trace := p.testDetachTrace; trace != nil {
dl, _ := cutCtx.Deadline()
trace("cut", dl)
}
h.chunkDetach = true
if stop := p.testDetachStopIO; stop != nil {
err = stop(cutCtx)
} else {
err = h.backend.StopIO(cutCtx, "chunk detach: I/O stopped until a new attach")
}
if err == nil {
var r storage.ChunkCutResult
if p.cfg.ChunkDetachPrecopy {
// Cancel/join the periodic owner before any final freeze, while serving.
p.stopChunkLoop(h)
var r storage.ChunkCutResult
if r, err = layer.Detach(cutCtx); err == nil {
owner, ok := layer.(interface {
PrecopyDetach(context.Context, func() bool) (storage.ChunkCutResult, error)
})
if !ok {
return r, 0, fmt.Errorf("%w: layer lacks pre-copy", storage.ErrPrecopyUnknown)
}
r, err = owner.PrecopyDetach(cutCtx, func() bool {
if !h.backend.TryStopIO("chunk pre-copy: final detached cut") {
return false
}
h.chunkDetach = true
beginFreeze()
return true
})
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 {
return r, h.storage.NextLSN() - 1, nil
}
if !r.Precopy.Frozen {
p.mu.Lock()
if !p.closed && h.chunk == layer {
p.startChunkLoop(volumeID, h, layer)
}
p.mu.Unlock()
return r, 0, err
}
} else {
beginFreeze()
if trace := p.testDetachTrace; trace != nil {
trace("cut", start.Add(freeze*3/4))
}
h.chunkDetach = true
if stop := p.testDetachStopIO; stop != nil {
err = stop(cutCtx)
} else {
err = h.backend.StopIO(cutCtx, "chunk detach: I/O stopped until a new attach")
}
if err == nil {
p.stopChunkLoop(h)
if r, err = layer.Detach(cutCtx); err == nil {
return r, h.storage.NextLSN() - 1, nil
}
}
}
// Every failure, a failed drain included, is settled before anything
// resumes: an earlier detach that applied, an unresolved detach CAS or a
@@ -471,6 +471,22 @@ func (b *StorageBackend) SetOperational(ok bool, evidence string) {
b.mu.Unlock()
}
// TryStopIO closes admission only if every admitted I/O has drained already.
// Used under the capture barrier: never wait for ioMu there, since an I/O
// holding its read side may itself be queued on that barrier. Failed attempts
// leave the frontend unchanged. Lock order: try ioMu -> mu; no store calls.
func (b *StorageBackend) TryStopIO(evidence string) bool {
if !b.ioMu.TryLock() {
return false
}
defer b.ioMu.Unlock()
if !b.operational.Load() {
return false
}
b.SetOperational(false, evidence)
return true
}
// StopIO makes the backend refuse every new Read, Write and Sync with
// ErrNotReady (evidence), then waits, bounded by ctx, for the I/O admitted
// before the stop to finish. After a nil return no I/O admitted before the
@@ -234,6 +234,10 @@ func writeChunkFailure(w http.ResponseWriter, err error) {
func chunkErrorCode(err error) (int, string, bool) {
var ce *chunkstore.CatalogError
switch {
case errors.Is(err, storage.ErrPrecopyUnknown):
return http.StatusConflict, "precopy_unknown", true
case errors.Is(err, storage.ErrPrecopyNotConverged):
return http.StatusConflict, "precopy_not_converged", true
case errors.Is(err, durable.ErrDetachAborted):
// Before any cause (a catalog error included): nothing was
// published and serving resumed here.