From 6f771c8169eb5b547a08e9f444362a0b0840bc2b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Fri, 20 Nov 2026 03:43:25 -0800 Subject: [PATCH] durable: wire opt-in pre-copy into settled detach lifecycle --- seaweed-block/cmd/blockvolume/chunk_remote.go | 3 +- seaweed-block/cmd/blockvolume/main.go | 2 + .../core/frontend/durable/provider.go | 3 + .../core/frontend/durable/provider_chunk.go | 75 ++++++++++++++----- .../core/frontend/durable/storage_adapter.go | 16 ++++ .../core/host/volume/chunk_control.go | 4 + 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/chunk_remote.go b/seaweed-block/cmd/blockvolume/chunk_remote.go index 4a312fa1bf..a64963c9f6 100644 --- a/seaweed-block/cmd/blockvolume/chunk_remote.go +++ b/seaweed-block/cmd/blockvolume/chunk_remote.go @@ -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 diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index ad5126c180..7185c26da4 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -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 diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 134802d749..1c59337323 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -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. diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index 3d4c7c1869..bceac9998d 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -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 diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 83545e2a54..3dc932bbe1 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -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 diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index 43db86794e..2e640d3e2e 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -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.