From c1a986a63ecde8cf3486cf0a5e75d9cc20271a08 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 03:32:47 -0800 Subject: [PATCH] durable: walstore-chunk authority wait is bound to its recovery generation A deferred authority wait kept acting after a newer RecoverVolume. If that newer recovery failed, the old wait still attached the layer and set the backend operational once authority arrived, overriding the failed recovery. Each RecoverVolume of a walstore-chunk volume now takes the next recovery generation (under the per-volume serialization), and an authority wait carries the generation that registered it. A wait whose generation is no longer the latest exits. It checks on every tick, and again under the per-volume serialization right before attaching, so it never attaches or touches the backend for a superseded recovery. After a failed recovery the backend stays refused until a later recovery succeeds; that recovery attaches directly or starts its own wait. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider.go | 23 ++++++--- .../core/frontend/durable/provider_chunk.go | 51 ++++++++++++------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 6405255ed0..087fcdadb3 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -167,12 +167,16 @@ type volHandle struct { // is still fresh after a restart. fresh bool attachMu sync.Mutex // serializes recovery and attach of this volume - // awaitingAuthority: recovered, layer not attached yet because the - // projection carries no master authority; a wait goroutine attaches. - awaitingAuthority bool - chunk storage.ChunkLayer // after RecoverVolume - chunkStop context.CancelFunc - chunkDone chan struct{} + // recoverGen numbers this volume's RecoverVolume calls (written under + // attachMu and p.mu). waitGen is the generation of the live authority + // wait, 0 if none: a recovered volume whose layer waits for master + // authority. A wait only acts while its generation is still the + // latest recovery's. + recoverGen uint64 + waitGen uint64 + chunk storage.ChunkLayer // after RecoverVolume + chunkStop context.CancelFunc + chunkDone chan struct{} } // VolumeStatus is a read-only diagnostic snapshot of one durable volume @@ -689,10 +693,15 @@ func (p *DurableProvider) RecoverVolume(ctx context.Context, volumeID string) (R if !ok { return RecoveryReport{VolumeID: volumeID}, ErrVolumeNotOpen } + var gen uint64 if chunked { defer p.inflight.Done() h.attachMu.Lock() defer h.attachMu.Unlock() + p.mu.Lock() + h.recoverGen++ // any earlier recovery's authority wait is now stale + gen = h.recoverGen + p.mu.Unlock() } report := Recover(h.storage, volumeID) if report.Err != nil { @@ -700,7 +709,7 @@ func (p *DurableProvider) RecoverVolume(ctx context.Context, volumeID string) (R return report, report.Err } if chunked { - attached, err := p.recoverChunk(ctx, volumeID, h) + attached, err := p.recoverChunk(ctx, volumeID, h, gen) if err != nil { h.backend.SetOperational(false, "chunk layer attach failed: "+err.Error()) report.Err = err diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index e641c28231..0b2b55cfe7 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -58,39 +58,49 @@ func chunkAuthority(proj frontend.Projection, volumeID string) bool { return proj.VolumeID == volumeID && proj.Healthy && proj.Epoch > 0 } -// recoverChunk runs after a successful local Recover. It attaches the -// layer when the projection already carries master authority and reports -// true. Otherwise it starts the volume's authority wait (which attaches -// as soon as the assignment is applied) and reports false: the volume is -// recovered but stays non-operational until then. The caller holds -// h.attachMu and is registered with Close. -func (p *DurableProvider) recoverChunk(ctx context.Context, volumeID string, h *volHandle) (bool, error) { +// recoverChunk runs after a successful local Recover, generation gen. It +// attaches the layer when the projection already carries master authority +// and reports true. Otherwise it starts an authority wait bound to gen +// (which attaches as soon as the assignment is applied) and reports +// false: the volume is recovered but stays non-operational until then. +// The caller holds h.attachMu and is registered with Close. +func (p *DurableProvider) recoverChunk(ctx context.Context, volumeID string, h *volHandle, gen uint64) (bool, error) { err := p.attachChunk(ctx, volumeID, h) if errors.Is(err, errChunkNoAuthority) { - p.startChunkAuthorityWait(volumeID, h) + p.startChunkAuthorityWait(volumeID, h, gen) return false, nil } return err == nil, err } -// startChunkAuthorityWait starts at most one authority wait per volume, -// registered with Close like a recovery. -func (p *DurableProvider) startChunkAuthorityWait(volumeID string, h *volHandle) { +// startChunkAuthorityWait starts the authority wait of recovery gen, +// registered with Close like a recovery. A wait of an earlier recovery +// sees it is stale and exits on its own. +func (p *DurableProvider) startChunkAuthorityWait(volumeID string, h *volHandle, gen uint64) { p.mu.Lock() defer p.mu.Unlock() - if p.closed || h.awaitingAuthority { + if p.closed || h.waitGen == gen { return } - h.awaitingAuthority = true + h.waitGen = gen p.inflight.Add(1) - go p.chunkAuthorityWait(volumeID, h) + go p.chunkAuthorityWait(volumeID, h, gen) } -func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle) { +// current reports whether gen is still the latest recovery of h. +func (p *DurableProvider) current(h *volHandle, gen uint64) bool { + p.mu.Lock() + defer p.mu.Unlock() + return h.recoverGen == gen +} + +func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle, gen uint64) { defer p.inflight.Done() defer func() { p.mu.Lock() - h.awaitingAuthority = false + if h.waitGen == gen { + h.waitGen = 0 + } p.mu.Unlock() }() t := time.NewTicker(chunkAuthorityPoll) @@ -101,10 +111,17 @@ func (p *DurableProvider) chunkAuthorityWait(volumeID string, h *volHandle) { return case <-t.C: } + if !p.current(h, gen) { + return // a later recovery (successful or failed) owns the volume + } if !chunkAuthority(p.view.Projection(), volumeID) { continue } h.attachMu.Lock() + if !p.current(h, gen) { // re-checked under the per-volume serialization + h.attachMu.Unlock() + return + } ctx, cancel := context.WithTimeout(p.closeCtx, chunkAttachTimeout) err := p.attachChunk(ctx, volumeID, h) cancel() @@ -360,7 +377,7 @@ func (p *DurableProvider) chunkLayer(volumeID string) (storage.ChunkLayer, error return nil, ErrVolumeNotOpen } p.mu.Lock() - layer, awaiting := h.chunk, h.awaitingAuthority + layer, awaiting := h.chunk, h.waitGen != 0 p.mu.Unlock() if layer == nil && awaiting { return nil, ErrChunkAwaitingAuthority