diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index d115bc07af..7453e9f7b2 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -165,10 +165,12 @@ type ProviderConfig struct { // passed since the volume's last successful cut (of any kind). Zero // disables the timer. ChunkCaptureInterval time.Duration - // DetachFreezeTimeout bounds how long DetachVolume keeps guest I/O - // stopped for its drain and final cut (the caller's context can only - // shorten it). Zero: 20 s, below the host's 30 s I/O timeout; a freeze - // past the 5 s target is logged as an alarm. + // DetachFreezeTimeout bounds how long a failing DetachVolume keeps guest + // I/O stopped before it resumes or settles: the drain and final cut get + // three quarters of it, the settlement of a failed cut the rest (the + // caller's context can only shorten the cut part). Zero: 20 s, below the + // 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 // ChunkDirtyThreshold is the bound on one cut's barrier copy, in // chunks. It never triggers a cut: the remote holds snapshots, and diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index 95add9aadc..da0da1948f 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -780,25 +780,31 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st if freeze <= 0 { freeze = defaultDetachFreezeTimeout } - fctx, cancel := context.WithTimeout(ctx, freeze) - defer cancel() + // 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)) + defer 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) } }() h.chunkDetach = true - if err := h.backend.StopIO(fctx, "chunk detach: I/O stopped until a new attach"); err != nil { - return storage.ChunkCutResult{}, 0, p.resumeAfterDetach(volumeID, h, layer, err) // nothing was cut - } - p.stopChunkLoop(h) - r, err := layer.Detach(fctx) + err = h.backend.StopIO(cutCtx, "chunk detach: I/O stopped until a new attach") if err == nil { - return r, h.storage.NextLSN() - 1, nil + p.stopChunkLoop(h) + var r storage.ChunkCutResult + if r, err = layer.Detach(cutCtx); err == nil { + return r, h.storage.NextLSN() - 1, nil + } } - // Settle with time of its own: the freeze bound may be what ended the cut. - sctx, scancel := context.WithTimeout(context.WithoutCancel(ctx), detachSettleTimeout) + // Every failure, a failed drain included, is settled before anything + // 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)) defer scancel() how, dr, serr := layer.SettleDetach(sctx) switch { @@ -816,17 +822,22 @@ func (p *DurableProvider) DetachVolume(ctx context.Context, volumeID string) (st const ( defaultDetachFreezeTimeout = 20 * time.Second detachFreezeTarget = 5 * time.Second - detachSettleTimeout = 10 * time.Second ) // resumeAfterDetach undoes a detach that provably did not happen: the // periodic cuts restart, the stop is lifted, and the volume serves again. // The caller holds h.attachMu. func (p *DurableProvider) resumeAfterDetach(volumeID string, h *volHandle, layer storage.ChunkLayer, cause error) error { - h.chunkDetach = false p.mu.Lock() - p.startChunkLoop(volumeID, h, layer) + closed := p.closed || h.chunk != layer + if !closed { + p.startChunkLoop(volumeID, h, layer) + } p.mu.Unlock() + if closed { // a concurrent Close or detach of the layer owns the volume now + return fmt.Errorf("durable: detach of %s aborted while the provider closed: %w", volumeID, cause) + } + h.chunkDetach = false h.backend.SetOperational(true, "chunk detach aborted: serving resumed") log.Printf("durable: detach of volume=%s aborted, nothing published; serving resumed: %v", volumeID, cause) return fmt.Errorf("%w: %w", ErrDetachAborted, cause) diff --git a/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go b/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go index 556dabe8bd..aeeb8c7a31 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_detach_abort_test.go @@ -28,6 +28,11 @@ type faultRemote struct { casMode string // "", "precondition", "lost" (not applied, error), "applied-lost" (applied, error), "hold" getHeadErr error // returned by every GetHead while set getHeadFail int // the next N GetHead calls fail + getHeadHold bool // every GetHead blocks until its caller gives up + // "delayed": the CAS's reply is lost now, the CAS itself reaches the + // remote only when release closes; its own result goes to delayedErr. + release chan struct{} + delayedErr chan error } var errInjected = errors.New("injected remote fault") @@ -46,12 +51,17 @@ func (r *faultRemote) Put(ctx context.Context, key string, data []byte) error { func (r *faultRemote) GetHead(ctx context.Context) (storage.ChunkHead, error) { r.mu.Lock() + hold := r.getHeadHold err := r.getHeadErr if err == nil && r.getHeadFail > 0 { r.getHeadFail-- err = errInjected } r.mu.Unlock() + if hold { + <-ctx.Done() + return storage.ChunkHead{}, ctx.Err() + } if err != nil { return storage.ChunkHead{}, err } @@ -71,6 +81,16 @@ func (r *faultRemote) CASHead(ctx context.Context, expected storage.ChunkHeadVer case "hold": // held until the caller gives up: not applied <-ctx.Done() return 0, ctx.Err() + case "delayed": + r.mu.Lock() + release, out := r.release, r.delayedErr + r.mu.Unlock() + go func() { + <-release + _, err := r.MemChunkRemote.CASHead(context.Background(), expected, next) + out <- err + }() + return 0, errInjected case "applied-lost": if _, err := r.MemChunkRemote.CASHead(ctx, expected, next); err != nil { return 0, err @@ -243,8 +263,9 @@ func TestWALStoreChunk_DetachFreezeIsBoundedThenResumes(t *testing.T) { if !errors.Is(err, durable.ErrDetachAborted) || !errors.Is(err, context.DeadlineExceeded) { t.Fatalf("detach with a hung CAS: %v; want an aborted detach at the freeze bound", err) } - if took < 300*time.Millisecond || took > 3*time.Second { - t.Fatalf("detach with a hung CAS took %v; want about the 300 ms freeze bound", took) + // The cut gets three quarters of the bound, the settlement the rest. + if took < 225*time.Millisecond || took > 300*time.Millisecond+200*time.Millisecond { + t.Fatalf("detach with a hung CAS took %v; want the cut ended at 3/4 of the 300 ms freeze bound and the total within it", took) } assertServing(t, p, b, remote) } @@ -270,3 +291,103 @@ func TestWALStoreChunk_DetachAgainstNewerEpochStaysStopped(t *testing.T) { t.Fatalf("write after a fenced detach: %v; want it refused (not ready / stale primary)", err) } } + +// P1 (review): a repeated detach after a successful one, with a cancelled +// context (its StopIO fails at once), returns the first result and never +// resumes serving. +func TestWALStoreChunk_RepeatDetachWithCancelledContextNeverResumes(t *testing.T) { + _, p, b := detachFixture(t) + r1, _, err := p.DetachVolume(context.Background(), "v1") + if err != nil { + t.Fatal(err) + } + cctx, cancel := context.WithCancel(context.Background()) + cancel() + r2, _, err := p.DetachVolume(cctx, "v1") + if err != nil || r2.Head.Gen != r1.Head.Gen || r2.Head.State != storage.ChunkHeadDetached { + t.Fatalf("repeated detach with a cancelled context: %+v %v; want the first result %+v", r2, err, r1) + } + if _, err := b.Write(context.Background(), 12288, block(3)); !errors.Is(err, frontend.ErrNotReady) { + t.Fatalf("write after a repeated detach: %v; want ErrNotReady (never resumed)", err) + } +} + +// P1 (review): after an undecidable detach, a retry whose StopIO fails (a +// cancelled context) settles first and stays stopped while the outcome is +// still unknown. +func TestWALStoreChunk_RetryAfterUndecidableDetachNeverResumes(t *testing.T) { + remote, p, b := detachFixture(t) + remote.set(func(r *faultRemote) { r.casMode = "lost"; r.getHeadErr = errInjected }) + if _, _, err := p.DetachVolume(context.Background(), "v1"); !errors.Is(err, durable.ErrDetachOutcomeUnknown) { + t.Fatalf("first detach: %v; want ErrDetachOutcomeUnknown", err) + } + cctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, _, err := p.DetachVolume(cctx, "v1"); !errors.Is(err, durable.ErrDetachOutcomeUnknown) || errors.Is(err, durable.ErrDetachAborted) { + t.Fatalf("retry over an unresolved detach CAS: %v; want still unknown, never aborted", err) + } + if _, err := b.Write(context.Background(), 12288, block(3)); !errors.Is(err, frontend.ErrNotReady) { + t.Fatalf("write after a retry over an unknown detach: %v; want ErrNotReady", err) + } +} + +// P1 (review): one freeze budget covers the cut AND the settlement: with the +// CAS and every HEAD read hung, DetachVolume returns within the bound (here +// undecided, so the volume stays stopped). +func TestWALStoreChunk_DetachFreezeBoundCoversTheSettle(t *testing.T) { + const freeze = 400 * time.Millisecond + remote := &faultRemote{MemChunkRemote: storage.NewMemChunkRemote()} + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStoreChunk, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, ChunkBlocks: pcChunkBlocks, + DetachFreezeTimeout: freeze, + ChunkRemoteFor: func(string) (storage.ChunkRemote, error) { return remote, nil }, + }, v1View()) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = p.Close() }) + openRecovered(t, p) + b, err := p.Open(context.Background(), "v1") + if err != nil { + t.Fatal(err) + } + if _, err := b.Write(context.Background(), 4096, block(1)); err != nil { + t.Fatal(err) + } + remote.set(func(r *faultRemote) { r.casMode = "hold"; r.getHeadHold = true }) + start := time.Now() + _, _, err = p.DetachVolume(context.Background(), "v1") + took := time.Since(start) + if !errors.Is(err, durable.ErrDetachOutcomeUnknown) { + t.Fatalf("detach with a hung CAS and hung HEAD reads: %v; want the outcome unknown", err) + } + if took > freeze+200*time.Millisecond { + t.Fatalf("detach took %v; the %v freeze bound must cover the settlement too", took, freeze) + } + if _, err := b.Write(context.Background(), 8192, block(2)); !errors.Is(err, frontend.ErrNotReady) { + t.Fatalf("write after an undecided detach: %v; want ErrNotReady", err) + } +} + +// P2 (review): the lost detach CAS really is in flight. It reaches the remote +// only after the cancel landed, and then fails its precondition: HEAD stays +// active. +func TestWALStoreChunk_DelayedDetachCASCannotLandAfterTheCancel(t *testing.T) { + remote, p, b := detachFixture(t) + release, delayed := make(chan struct{}), make(chan error, 1) + var once sync.Once + unblock := func() { once.Do(func() { close(release) }) } + t.Cleanup(unblock) + remote.set(func(r *faultRemote) { r.casMode = "delayed"; r.release, r.delayedErr = release, delayed }) + if _, _, err := p.DetachVolume(context.Background(), "v1"); !errors.Is(err, durable.ErrDetachAborted) { + t.Fatalf("detach with a delayed CAS: %v; want ErrDetachAborted after the cancel", err) + } + unblock() + if err := <-delayed; !errors.Is(err, storage.ErrChunkHeadPrecondition) { + t.Fatalf("the delayed detach CAS after the cancel: %v; want a precondition failure", err) + } + if head, _ := remote.MemChunkRemote.GetHead(context.Background()); head.State == storage.ChunkHeadDetached { + t.Fatal("the delayed detach CAS landed after the cancel") + } + assertServing(t, p, b, remote) +} diff --git a/seaweed-block/core/frontend/durable/provider_chunk_detach_stop_test.go b/seaweed-block/core/frontend/durable/provider_chunk_detach_stop_test.go index b52c96f1ec..d5d511af1f 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_detach_stop_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_detach_stop_test.go @@ -152,14 +152,19 @@ func TestWALStoreChunk_DetachDrainTimeoutAbortsAndResumes(t *testing.T) { // detach itself). nerr := make(chan error, 1) go func() { _, err := b.Write(ctx, 8192, block(6)); nerr <- err }() + select { + case err := <-nerr: + t.Fatalf("new write completed (%v) ahead of the write admitted before the stop", err) + case <-time.After(100 * time.Millisecond): // queued behind the admitted write, as the drain orders it + } view.unpark() acked := <-werr == nil if err := <-nerr; err != nil { t.Fatalf("new write after an aborted detach: %v; want it accepted", err) } - if _, err := p.RecoverVolume(ctx, "v1"); errors.Is(err, storage.ErrChunkLayerDetached) { - t.Fatalf("recovery after an aborted detach: %v; want the volume serving", err) + if _, err := p.RecoverVolume(ctx, "v1"); err != nil { + t.Fatalf("recovery after an aborted detach: %v; want it to succeed", err) } o := detachOutcome{} o.r, o.last, o.err = p.DetachVolume(ctx, "v1") diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index d9365811c2..97525d2391 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -235,6 +235,9 @@ func chunkErrorCode(err error) (int, string, bool) { case errors.Is(err, durable.ErrDetachAborted): // Before its cause: nothing was published and serving resumed here. return http.StatusConflict, codeDetachAborted, true + case errors.Is(err, durable.ErrDetachOutcomeUnknown): + // Before its cause: the detach may still have landed; I/O stays stopped. + return http.StatusServiceUnavailable, chunkstore.CodeOutcomeUnknown, true case errors.Is(err, storage.ErrChunkLayerFenced): return http.StatusConflict, codeFenced, false case errors.Is(err, storage.ErrChunkCASOutcomeUnknown):