From 273ca809d88deb8b50f81decd45c2aa0ef8444f5 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 09:24:42 -0800 Subject: [PATCH] durable: a refused observer rebase never leaves I/O open; seed and install serialized SetWriteObserver published the observer first and only logged a refused ship-LSN rebase, so the volume kept serving writes its observer would mis-order. Now: - SetWriteObserver rebases an LSN-ordering observer to the store's next LSN before installing it. If the rebase is refused, the observer is not installed and the error is returned. The backend also records the fault and refuses every I/O until an install succeeds, so a later attach that marks the volume operational cannot reopen it. blockvolume reports the error. - The frontier seed and its observer rebase run in one critical section with observer installs (observerMu). Whichever runs second sees the other's result; a seed landing between an install's rebase and its publication can no longer leave the observer on the old LSN. A refused rebase after the seed records the same fault. - RebaseShipLSN documents that it moves only the ordering cursor: entries already dispatched to peer queues carry lower LSNs and are untouched. The composition test checks the install error. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/main.go | 5 +- .../core/frontend/durable/provider_chunk.go | 12 ++-- .../core/frontend/durable/storage_adapter.go | 68 +++++++++++++++---- .../volume/chunk_seed_replication_test.go | 8 ++- seaweed-block/core/replication/volume.go | 4 +- 5 files changed, 71 insertions(+), 26 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/main.go b/seaweed-block/cmd/blockvolume/main.go index 77fd116515..39618b976a 100644 --- a/seaweed-block/cmd/blockvolume/main.go +++ b/seaweed-block/cmd/blockvolume/main.go @@ -805,7 +805,10 @@ func run(f flags) int { // for primary-role frontend bind). Without this hook, primary // writes would land locally but never ship to peers. if sb := durableProv.Backend(f.volumeID); sb != nil { - sb.SetWriteObserver(replVolume) + if err := sb.SetWriteObserver(replVolume); err != nil { + // The backend now refuses all I/O; the volume cannot serve. + fmt.Fprintln(os.Stderr, "blockvolume: write-observer wire:", err) + } sb.SetWriteAckPolicy(writeAck) } else { fmt.Fprintln(os.Stderr, "blockvolume: write-observer wire: Backend(", f.volumeID, ") returned nil; replication fan-out disabled") diff --git a/seaweed-block/core/frontend/durable/provider_chunk.go b/seaweed-block/core/frontend/durable/provider_chunk.go index 7b878dc8ad..b92f19f225 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk.go +++ b/seaweed-block/core/frontend/durable/provider_chunk.go @@ -294,15 +294,11 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h cfg.FreshFromHead = false return nil } - if err := ws.SeedChunkFrontier(head.RemoteLSN); err != nil { - return err - } // Every component that cached the next LSN learns the seeded - // one before the takeover CAS and before the volume serves I/O. - if err := h.backend.rebaseShipLSN(); err != nil { - return fmt.Errorf("durable: rebase ship LSN after the frontier seed: %w", err) - } - return nil + // one before the takeover CAS and before the volume serves I/O, + // serialized with observer installs. A refused rebase fails the + // restore here, so HEAD is never claimed for a mis-ordered ship. + return h.backend.seedAndRebase(func() error { return ws.SeedChunkFrontier(head.RemoteLSN) }) } head, err := storage.ClaimChunkHead(ctx, remote, cfg) if err != nil { diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 82bc60cab1..424f276e28 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -169,9 +169,18 @@ type StorageBackend struct { drainMu sync.Mutex drain chan struct{} - mu sync.Mutex - closed bool - chunkFenced func() bool // installed under mu; called only after releasing mu + mu sync.Mutex + closed bool + chunkFenced func() bool // installed under mu; called only after releasing mu + // observerErr is a refused write-observer install (its ship order + // could not follow the store's next LSN). While set, every I/O is + // refused, so a volume never serves without its observer. Under mu. + observerErr error + // observerMu serializes moving the store's next LSN (the frontier + // seed) together with rebasing the observer, against installing an + // observer together with its rebase: whichever runs second sees the + // other's result. Lock order: observerMu, then mu. + observerMu sync.Mutex observer WriteObserver // optional; nil = no replication fan-out writeAck WriteAckPolicy profile writeProfile @@ -333,19 +342,39 @@ func (b *StorageBackend) Close() error { // Owns: observer pointer under b.mu. // Borrows: the observer — caller retains ownership; StorageBackend // does not close or otherwise tear down the observer. -func (b *StorageBackend) SetWriteObserver(obs WriteObserver) { - b.mu.Lock() - b.observer = obs - b.mu.Unlock() - // The store's frontier may have been seeded before the observer was - // installed: start its ship order at the store's next LSN. - if err := b.rebaseShipLSN(); err != nil { - log.Printf("durable: write observer ship LSN: %v", err) +// +// An observer that orders writes by LSN (ShipLSNRebaser) is first rebased +// to the store's next LSN: the frontier may have been seeded before the +// install. If that rebase is refused, the observer is NOT installed, the +// error is returned, and the backend refuses all I/O until an install +// succeeds: a volume never serves writes its observer would mis-order. +func (b *StorageBackend) SetWriteObserver(obs WriteObserver) error { + b.observerMu.Lock() + defer b.observerMu.Unlock() + if r, ok := obs.(ShipLSNRebaser); ok { + if err := r.RebaseShipLSN(b.storage.NextLSN()); err != nil { + err = fmt.Errorf("durable: write observer not installed: ship LSN: %w", err) + b.mu.Lock() + b.observerErr = err + b.mu.Unlock() + return err + } } + b.mu.Lock() + b.observer, b.observerErr = obs, nil + b.mu.Unlock() + return nil } -// rebaseShipLSN moves an LSN-ordering observer to the store's next LSN. -func (b *StorageBackend) rebaseShipLSN() error { +// seedAndRebase moves the store's next LSN with seed and, in the same +// critical section as observer installs, rebases the installed observer +// to it. A refused rebase also closes I/O (as a refused install does). +func (b *StorageBackend) seedAndRebase(seed func() error) error { + b.observerMu.Lock() + defer b.observerMu.Unlock() + if err := seed(); err != nil { + return err + } b.mu.Lock() obs := b.observer b.mu.Unlock() @@ -353,7 +382,14 @@ func (b *StorageBackend) rebaseShipLSN() error { if !ok { return nil } - return r.RebaseShipLSN(b.storage.NextLSN()) + if err := r.RebaseShipLSN(b.storage.NextLSN()); err != nil { + err = fmt.Errorf("durable: write observer ship LSN after the frontier seed: %w", err) + b.mu.Lock() + b.observerErr = err + b.mu.Unlock() + return err + } + return nil } // SetWriteAckPolicy configures whether observer acknowledgement participates @@ -553,10 +589,14 @@ func (b *StorageBackend) readyCheck() error { b.mu.Lock() closed := b.closed chunkFenced := b.chunkFenced + observerErr := b.observerErr b.mu.Unlock() if closed { return frontend.ErrBackendClosed } + if observerErr != nil { + return fmt.Errorf("%w: %v", frontend.ErrNotReady, observerErr) + } if chunkFenced != nil && chunkFenced() { return frontend.ErrStalePrimary } diff --git a/seaweed-block/core/host/volume/chunk_seed_replication_test.go b/seaweed-block/core/host/volume/chunk_seed_replication_test.go index abd50daf7f..efc367b340 100644 --- a/seaweed-block/core/host/volume/chunk_seed_replication_test.go +++ b/seaweed-block/core/host/volume/chunk_seed_replication_test.go @@ -99,7 +99,9 @@ func restoredHostWrites(t *testing.T, inheritedLSN uint64, observerAfterSeed boo t.Fatal("no durable backend for v1") } if !observerAfterSeed { - sb.SetWriteObserver(rv) + if err := sb.SetWriteObserver(rv); err != nil { + t.Fatalf("install the write observer: %v", err) + } } openCtx, openCancel := context.WithTimeout(ctx, 10*time.Second) @@ -119,7 +121,9 @@ func restoredHostWrites(t *testing.T, inheritedLSN uint64, observerAfterSeed boo time.Sleep(20 * time.Millisecond) } if observerAfterSeed { - sb.SetWriteObserver(rv) + if err := sb.SetWriteObserver(rv); err != nil { + t.Fatalf("install the write observer: %v", err) + } } // A frontend write and flush must complete (bounded here so a hang diff --git a/seaweed-block/core/replication/volume.go b/seaweed-block/core/replication/volume.go index b6180d9a29..3fe1433363 100644 --- a/seaweed-block/core/replication/volume.go +++ b/seaweed-block/core/replication/volume.go @@ -674,7 +674,9 @@ func (v *ReplicationVolume) enqueueLocalWrite(w LocalWrite) (*orderedLocalWrite, // restored host's frontier seed (its WAL continues at HEAD.remote_lsn+1) // moves that LSN later, and whoever moves it calls this before the volume // serves I/O. It is refused while any write is pending or in flight, never -// dropping one, and lsn never moves backwards; equal is a no-op. +// dropping one, and lsn never moves backwards; equal is a no-op. It moves +// only the ordering cursor: entries already dispatched to peer queues +// carry LSNs below it and are untouched (an RF=1 volume has no peers). func (v *ReplicationVolume) RebaseShipLSN(lsn uint64) error { v.orderMu.Lock() defer v.orderMu.Unlock()