diff --git a/seaweed-block/core/replication/component/faults.go b/seaweed-block/core/replication/component/faults.go index 07d4297c43..f979b59502 100644 --- a/seaweed-block/core/replication/component/faults.go +++ b/seaweed-block/core/replication/component/faults.go @@ -14,17 +14,14 @@ import ( // methods. The embedded interface forwards everything else // transparently — only the overridden behavior changes. // -// KNOWN WRAP-PATTERN LIMITATION: substrate-specific extension methods -// (e.g. walstore's `CheckpointLSN`) are MASKED by the wrap because -// embedding only exposes the LogicalStorage interface methods. The -// catch-up sender's `recovery_mode` label probe duck-types -// CheckpointLSN to distinguish walstore from smartwal — under a -// wrap, that probe always sees state_convergence regardless of the -// underlying substrate. Scenarios that need wrap + accurate mode -// label simultaneously cannot rely on AssertSawRecoveryMode for the -// walstore label. Forward-carry: refactor mode-label detection -// behind a substrate-reported `RecoveryMode()` method that wraps -// can forward explicitly. +// T4d-4 (round-46): the T4c-2 wrap-pattern limitation around +// `CheckpointLSN` masking is RESOLVED. `LogicalStorage.RecoveryMode()` +// is now a first-class interface method, forwarded automatically by +// embedded `storage.LogicalStorage` in the wrap structs below. The +// catch-up sender consults `e.primaryStore.RecoveryMode()` (interface +// method) instead of the duck-typed `CheckpointLSN` probe. +// `AssertSawRecoveryMode` works correctly under wraps now. T4c §I +// row 6 closure. // NewRecycledScanWrap returns a PrimaryStorageWrap that always // returns ErrWALRecycled from ScanLBAs. Use to test the rebuild- diff --git a/seaweed-block/core/replication/volume.go b/seaweed-block/core/replication/volume.go index 894ee940ed..7658e808bf 100644 --- a/seaweed-block/core/replication/volume.go +++ b/seaweed-block/core/replication/volume.go @@ -352,10 +352,35 @@ func (v *ReplicationVolume) OnLocalWrite(ctx context.Context, w LocalWrite) erro return nil } +// Stop is the T4d-4 canonical lifecycle entry point. Tears down all +// peers (their executor sessions are invalidated), serializes against +// concurrent OnLocalWrite/Sync via the volume mutex, and is +// idempotent. Does NOT close the borrowed store +// (`INV-REPL-LIFECYCLE-HANDLE-BORROWED-001` per BUG-005 discipline). +// +// Stop and Close are equivalent in T4d-4 (Stop delegates to Close); +// the rename clarifies semantic intent — "Stop the volume's lifecycle +// activity" reads more clearly than "Close the volume struct." +// Future expansions (drain pending I/O, stop background goroutines) +// land here under the Stop name. +// +// Pinned by: TestReplicationVolume_Stop_Idempotent, +// TestReplicationVolume_Stop_DoesNotCloseBorrowedStore. +// +// Called by: Provider teardown. +// Owns: peer-set teardown via peer.Close(). +// Borrows: nothing (store is BORROWED — never closed). +func (v *ReplicationVolume) Stop() error { + return v.Close() +} + // Close releases all peers' registered sessions. Idempotent. Does -// NOT close the borrowed store. ReplicationVolume.Stop is a full- -// lifecycle hardening deferred to T4d per mini-plan §5; Close is the -// minimum teardown needed for BUG-005 regression coverage. +// NOT close the borrowed store (BUG-005 / INV-REPL-LIFECYCLE-HANDLE- +// BORROWED-001). +// +// T4d-4 (round-46) renaming: Stop() is the canonical entry point; +// Close() retained for backward compatibility with existing callers. +// Both do the same thing. // // Called by: Provider teardown when the volume shuts down. // Owns: close flag; invalidation of each peer's executor session diff --git a/seaweed-block/core/replication/volume_lifecycle_test.go b/seaweed-block/core/replication/volume_lifecycle_test.go new file mode 100644 index 0000000000..e82c94f7a9 --- /dev/null +++ b/seaweed-block/core/replication/volume_lifecycle_test.go @@ -0,0 +1,71 @@ +package replication + +import ( + "testing" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// T4d-4 part A — lifecycle tests for ReplicationVolume.Stop + +// BUG-005 non-repeat. Pins INV-REPL-LIFECYCLE-HANDLE-BORROWED-001. + +// TestReplicationVolume_Stop_Idempotent — Stop must be safe to call +// multiple times; second + later calls are no-ops returning nil. +func TestReplicationVolume_Stop_Idempotent(t *testing.T) { + store := storage.NewBlockStore(64, 4096) + v := NewReplicationVolume("v1", store) + + if err := v.Stop(); err != nil { + t.Fatalf("first Stop: %v", err) + } + if err := v.Stop(); err != nil { + t.Fatalf("second Stop (must be no-op): %v", err) + } + if err := v.Close(); err != nil { + t.Fatalf("Close after Stop: %v", err) + } +} + +// TestReplicationVolume_Stop_DoesNotCloseBorrowedStore pins +// INV-REPL-LIFECYCLE-HANDLE-BORROWED-001 + BUG-005 discipline: +// ReplicationVolume.Stop MUST NOT call store.Close() on the borrowed +// substrate handle. The store is owned by the caller (Provider / +// host); Stop only releases peer-side resources. +func TestReplicationVolume_Stop_DoesNotCloseBorrowedStore(t *testing.T) { + store := storage.NewBlockStore(64, 4096) + v := NewReplicationVolume("v1", store) + + if err := v.Stop(); err != nil { + t.Fatalf("Stop: %v", err) + } + + // Store must STILL be usable after Stop — caller owns it. + data := make([]byte, 4096) + data[0] = 0xAA + if _, err := store.Write(0, data); err != nil { + t.Fatalf("FAIL: store.Write after volume.Stop: %v — Stop closed the borrowed store (BUG-005 regression!)", err) + } + if got, err := store.Read(0); err != nil || got[0] != 0xAA { + t.Fatalf("FAIL: store unusable after volume.Stop — got %v err=%v", got, err) + } +} + +// TestReplicationVolume_Stop_TearsDownPeers — Stop releases all +// peers' executor sessions. Empty-set lifecycle (no peers) is the +// minimal pin; full multi-peer teardown is exercised by integration +// scenarios. +func TestReplicationVolume_Stop_TearsDownPeers(t *testing.T) { + store := storage.NewBlockStore(64, 4096) + v := NewReplicationVolume("v1", store) + + if v.PeerCount() != 0 { + t.Errorf("fresh volume PeerCount = %d, want 0", v.PeerCount()) + } + + if err := v.Stop(); err != nil { + t.Fatalf("Stop: %v", err) + } + if v.PeerCount() != 0 { + t.Errorf("after Stop PeerCount = %d, want 0", v.PeerCount()) + } +} diff --git a/seaweed-block/core/storage/logical_storage.go b/seaweed-block/core/storage/logical_storage.go index 099ff5043b..77f996429f 100644 --- a/seaweed-block/core/storage/logical_storage.go +++ b/seaweed-block/core/storage/logical_storage.go @@ -144,6 +144,21 @@ type LogicalStorage interface { // NIL-CONTINUES. ScanLBAs(fromLSN uint64, fn func(RecoveryEntry) error) error + // RecoveryMode returns the substrate's tier-1 recovery sub-mode + // (memo §5.1 / §13.0a). Replaces the duck-typed `CheckpointLSN` + // probe that T4c-2 used to distinguish walstore from smartwal. + // Per T4c §I row 6 + T4d-4 part A: substrate-reported value + // survives the component framework's storage-wrap pattern (the + // embedded interface forwards `RecoveryMode()` cleanly, where + // `CheckpointLSN` was a substrate-extension method that wraps + // did NOT forward). + // + // Implementations: + // - walstore → RecoveryModeWALReplay (V2-faithful per-LSN) + // - smartwal → RecoveryModeStateConvergence (per-LBA dedup) + // - BlockStore → RecoveryModeStateConvergence (synthesis) + RecoveryMode() RecoveryMode + // AppliedLSNs returns the substrate's per-LBA latest-applied-LSN // snapshot. T4d-2 replica recovery apply gate calls this at // session start to seed its in-memory `appliedLSN[LBA]` map diff --git a/seaweed-block/core/storage/recovery_mode_test.go b/seaweed-block/core/storage/recovery_mode_test.go new file mode 100644 index 0000000000..3d3b2b56a0 --- /dev/null +++ b/seaweed-block/core/storage/recovery_mode_test.go @@ -0,0 +1,46 @@ +package storage + +import ( + "path/filepath" + "testing" +) + +// T4d-4 part A — RecoveryMode interface method tests (T4c §I row 6 +// closure). Substrates report their tier-1 sub-mode via the +// LogicalStorage interface. Replaces the T4c-2 duck-typed +// CheckpointLSN probe; survives the component framework's +// storage-wrap pattern. + +func TestStorageRecoveryMode_BlockStore_StateConvergence(t *testing.T) { + bs := NewBlockStore(64, 4096) + if got := bs.RecoveryMode(); got != RecoveryModeStateConvergence { + t.Errorf("BlockStore.RecoveryMode() = %q, want %q (in-memory; ScanLBAs synthesizes from current state)", + got, RecoveryModeStateConvergence) + } +} + +func TestStorageRecoveryMode_Walstore_WALReplay(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "ws.walstore") + s, err := CreateWALStore(path, 64, 4096) + if err != nil { + t.Fatalf("CreateWALStore: %v", err) + } + defer s.Close() + if got := s.RecoveryMode(); got != RecoveryModeWALReplay { + t.Errorf("walstore.RecoveryMode() = %q, want %q (V2-faithful per-LSN ScanLBAs)", + got, RecoveryModeWALReplay) + } +} + +// TestStorageRecoveryMode_LogicalStorageInterface_HasMethod is a +// compile-time fence pinning that RecoveryMode is on the interface +// (not just on substrate impls). If a future commit removes the +// interface method, this fails to compile. +func TestStorageRecoveryMode_LogicalStorageInterface_HasMethod(t *testing.T) { + var s LogicalStorage = NewBlockStore(64, 4096) + mode := s.RecoveryMode() // must compile via interface + if mode == "" { + t.Error("RecoveryMode via interface returned empty string") + } +} diff --git a/seaweed-block/core/storage/smartwal/store.go b/seaweed-block/core/storage/smartwal/store.go index d77d6343de..55db221b59 100644 --- a/seaweed-block/core/storage/smartwal/store.go +++ b/seaweed-block/core/storage/smartwal/store.go @@ -361,6 +361,13 @@ func (s *Store) ApplyEntry(lba uint32, data []byte, lsn uint64) error { return nil } +// RecoveryMode reports smartwal's recovery sub-mode (T4d-4 part A; +// T4c §I row 6). smartwal's ScanLBAs emits per-LBA last-writer-wins +// with scan-time LSN — state-convergence semantics. +func (s *Store) RecoveryMode() storage.RecoveryMode { + return storage.RecoveryModeStateConvergence +} + // AppliedLSNs returns per-LBA latest-applied-LSN derived from the // ring's valid records. smartwal's ring is capacity-bounded: records // older than (head - capacity) have been overwritten, so the returned diff --git a/seaweed-block/core/storage/store.go b/seaweed-block/core/storage/store.go index 8c8dd023c4..7e486dc5cb 100644 --- a/seaweed-block/core/storage/store.go +++ b/seaweed-block/core/storage/store.go @@ -192,6 +192,13 @@ func (s *BlockStore) ScanLBAs(fromLSN uint64, fn func(RecoveryEntry) error) erro return nil } +// RecoveryMode reports BlockStore's recovery sub-mode (T4d-4 part A; +// T4c §I row 6). BlockStore is in-memory; its ScanLBAs synthesizes +// entries from current state — closest to state-convergence. +func (s *BlockStore) RecoveryMode() RecoveryMode { + return RecoveryModeStateConvergence +} + // AppliedLSNs satisfies the T4d-1 LogicalStorage extension. BlockStore // is in-memory and does NOT track per-LBA applied LSN — explicit // not-tracked return per kickoff §2.5 #1 architect Option C hybrid. diff --git a/seaweed-block/core/storage/walstore.go b/seaweed-block/core/storage/walstore.go index 06e55e5364..bb91cccbbc 100644 --- a/seaweed-block/core/storage/walstore.go +++ b/seaweed-block/core/storage/walstore.go @@ -547,6 +547,13 @@ func (s *WALStore) ApplyEntry(lba uint32, data []byte, lsn uint64) error { return nil } +// RecoveryMode reports walstore's recovery sub-mode (T4d-4 part A; +// T4c §I row 6). walstore's ScanLBAs emits per-LSN entries from the +// retained WAL — V2-faithful per-LSN replay. +func (s *WALStore) RecoveryMode() RecoveryMode { + return RecoveryModeWALReplay +} + // AppliedLSNs returns a partial view of per-LBA applied LSN: only // LBAs whose latest write is still in the WAL (not yet flushed to // extent) are reported. Once an entry is flushed and the dirty-map diff --git a/seaweed-block/core/transport/catchup_sender.go b/seaweed-block/core/transport/catchup_sender.go index e48ae48476..2f1c0662df 100644 --- a/seaweed-block/core/transport/catchup_sender.go +++ b/seaweed-block/core/transport/catchup_sender.go @@ -204,16 +204,13 @@ func (e *BlockExecutor) doCatchUp(replicaID string, session *activeSession, from targetLSN, resp.AchievedLSN) } - // Mode label surfacing (memo §5.1). For now we report based on - // substrate type via a runtime check; T4c-3 integration tests - // will cover both substrate sub-modes through the matrix. - mode := storage.RecoveryModeStateConvergence - if _, ok := e.primaryStore.(interface{ CheckpointLSN() uint64 }); ok { - // walstore exposes CheckpointLSN — distinguishes it from - // BlockStore + smartwal. Provisional probe; T4c-3 will - // migrate this to a substrate-reported mode method. - mode = storage.RecoveryModeWALReplay - } + // Mode label surfacing (memo §5.1). T4d-4 part A: ask the + // substrate via the new RecoveryMode() interface method (T4c §I + // row 6). Replaces the T4c-2 duck-typed CheckpointLSN probe, + // which the component framework's storage-wrap pattern masked. + // Now wrap impls forward RecoveryMode() cleanly through the + // embedded LogicalStorage interface. + mode := e.primaryStore.RecoveryMode() log.Printf("executor: catch-up complete replica=%s recovery_mode=%s achieved=%d target=%d last_sent=%d", replicaID, mode, resp.AchievedLSN, targetLSN, lastSent) return resp.AchievedLSN, nil