Files
seaweedfs/seaweed-block/cmd/sparrow/main_test.go
T
Ping Qiu 7f9232af28 storage: persistence backend with crash-safe acked writes
Adds a single-node persistence layer behind a `LogicalStorage` seam
that admits multiple implementations. The semantic engine, adapter,
and calibration are unchanged — storage is execution machinery, not
authority.

## Interface

`core/storage/logical_storage.go` defines `LogicalStorage`. Both
`BlockStore` (in-memory, existing) and the new `WALStore` satisfy
it (compile-time assertions). Methods cover Write/Read/Sync/Recover,
Boundaries (R/S/H), frontier control (AdvanceFrontier /
AdvanceWALTail / ApplyEntry / AllBlocks), and lifecycle (Close).

## WALStore — default crash-safe backend

`core/storage/walstore.go` plus supporting files
(`wal_entry.go`, `wal_writer.go`, `superblock.go`, `dirty_map.go`,
`recovery.go`, `group_commit.go`).

- Single preallocated file: superblock + circular WAL + extent
- Per-record CRC32 + EntrySize trailer detects torn writes
- Group committer batches concurrent Sync callers into one fsync
- Defensive past-head scan on Recover picks up writes that landed
  after the last superblock update
- Idempotent recovery

## smartwal subpackage — experimental alternative

`core/storage/smartwal/` (record / ring / superblock / store /
recovery / tests / README).

- 32-byte metadata records in a slot-based ring
- Data goes directly to extent; reads always hit extent (no dirty
  map needed for the read path)
- Last-writer-wins per LBA on recovery; CRC verified against
  extent block
- Distinct magic byte from WALStore so files cannot be confused
- Subpackage layout signals opt-in / experimental status; default
  remains WALStore

## Crash consistency for acked writes

`TestWALStore_AckedWritesSurviveSimulatedCrash` and
`TestSmartWAL_AckedWritesSurviveSimulatedCrash` both bypass `Close()`
by dropping the file handle directly — no implicit fsync, no
housekeeping. After reopen + Recover, every block whose Write was
followed by a successful Sync is recoverable; pre-Sync writes may
or may not be present but never corrupt acked data.

## sparrow `--persist-demo`

Reproducible single-node demo: Write 4 blocks, Sync, Close, reopen,
Recover, Read back. Removes prior demo file in the target dir
before Round 1 so repeated invocations / CI runs work cleanly. Text
and `--json` output. Exit codes documented in
`docs/bootstrap-validation.md`.

## Contract test infrastructure

`core/storage/contract_test.go` runs 11 contract assertions against
each registered implementation via a factory table. Adding a third
implementation is one entry in the table.

## Documentation

`docs/persistence.md` describes the WAL+extent design, crash-
consistency guarantees, what's NOT covered, file layout, on-disk
record format, and the **Execution-layer scope decision**:
`LogicalStorage` is the accepted persistence seam;
`DataCommunicator` / `RecoveryExecutor` / `ProgressFeed` are
deliberately deferred peer institutions. **Backing-store options**
section documents the path to raw block device, O_DIRECT, FUA, and
SPDK with concrete perf budgets — all preserve the seam.

## Required adjustments to existing code

`BlockStore.Sync` changed from `() uint64` to `() (uint64, error)` so
in-memory and on-disk implementations share one signature. Callers
that ignored the return updated to discard explicitly. Replica
barrier handler now propagates real fsync errors.

## Tests

| Package | Count |
|---|---|
| `core/storage` | 26 (11 contract × 2 backends + 4 WALStore-specific) |
| `core/storage/smartwal` | 16 (11 contract + 5 SmartWAL-specific) |
| `cmd/sparrow` | 14 (added repeatability check for the demo) |

Full repo green except a pre-existing flake on
`TestTransport_Rebuild_DoesNotCorruptLBA0` (unrelated; addressed on
a separate branch).
2026-04-14 12:22:16 -07:00

260 lines
7.0 KiB
Go

package main
import (
"context"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweed-block/core/engine"
)
func TestParseFlags_Defaults(t *testing.T) {
opts, err := parseFlags(nil)
if err != nil {
t.Fatal(err)
}
if opts.json {
t.Fatal("default --json should be false")
}
if opts.runs != 1 {
t.Fatalf("default --runs = %d, want 1", opts.runs)
}
if opts.httpAddr != "" {
t.Fatalf("default --http = %q, want empty", opts.httpAddr)
}
}
func TestParseFlags_AllFlagsSet(t *testing.T) {
opts, err := parseFlags([]string{"--json", "--runs", "5", "--http", ":9090"})
if err != nil {
t.Fatal(err)
}
if !opts.json {
t.Fatal("--json not set")
}
if opts.runs != 5 {
t.Fatalf("--runs = %d, want 5", opts.runs)
}
if opts.httpAddr != ":9090" {
t.Fatalf("--http = %q, want :9090", opts.httpAddr)
}
}
func TestParseFlags_RejectsBadRuns(t *testing.T) {
_, err := parseFlags([]string{"--runs", "0"})
if err == nil {
t.Fatal("expected error for --runs 0")
}
_, err = parseFlags([]string{"--runs", "-3"})
if err == nil {
t.Fatal("expected error for --runs -3")
}
}
func TestScopeStatement_HonestAboutUnsupported(t *testing.T) {
// The scope statement is the authoritative capability claim shown
// by --help and echoed by /status. Verify it lists every
// unsupported item we care about.
mustContain := []string{
"Supported:",
"Not supported",
"persistence",
"master service",
"RF3",
"iSCSI",
"NVMe-oF",
"concurrent writer",
"real operator CLI",
}
for _, s := range mustContain {
if !strings.Contains(ScopeStatement, s) {
t.Fatalf("ScopeStatement missing %q", s)
}
}
}
func TestVersion_HasPhaseTag(t *testing.T) {
// The version string should make the pre-production status obvious
// (zero-dot prefix) and tag the current phase explicitly.
if !strings.HasPrefix(Version, "0.") {
t.Fatalf("Version %q should start with 0. (pre-production)", Version)
}
if !strings.Contains(Version, "phase") {
t.Fatalf("Version %q should tag the phase explicitly", Version)
}
}
func TestRunSparrow_AllThreePathsPass(t *testing.T) {
// Exercise the full validation path once in non-verbose mode.
// All three demos should PASS; exit code 0.
code := runSparrow(options{json: true, runs: 1})
if code != 0 {
t.Fatalf("runSparrow exit code = %d, want 0 (all pass)", code)
}
}
func TestRunSparrow_RepeatableAcrossRuns(t *testing.T) {
// Repeat runs must all pass. If any run flakes, the exit code
// becomes non-zero. This is the deterministic-validation proof.
code := runSparrow(options{json: true, runs: 3})
if code != 0 {
t.Fatalf("runSparrow --runs=3 exit code = %d, want 0", code)
}
}
func TestBuildResult_PassWhenHealthy(t *testing.T) {
healthy := engine.ReplicaProjection{
Mode: engine.ModeHealthy, R: 100, S: 10, H: 100,
}
r := buildResult("x", healthy)
if !r.Pass {
t.Fatal("expected Pass=true for healthy mode")
}
if r.Reason != "" {
t.Fatalf("healthy result should have no reason, got %q", r.Reason)
}
}
func TestStartHTTPOps_BindsAndServes(t *testing.T) {
// Happy path: bind succeeds on :0, returns a real address, the
// server actually answers /status requests.
srv, boundAddr, err := startHTTPOps("127.0.0.1:0")
if err != nil {
t.Fatalf("unexpected bind error: %v", err)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}()
// Bound address must be concrete — not ":0" and not empty.
if boundAddr == "" || strings.HasSuffix(boundAddr, ":0") {
t.Fatalf("boundAddr should resolve to a real port, got %q", boundAddr)
}
// Server must actually answer. Retry briefly for goroutine schedule.
var resp *http.Response
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
resp, err = http.Get("http://" + boundAddr + "/status")
if err == nil {
break
}
time.Sleep(20 * time.Millisecond)
}
if err != nil {
t.Fatalf("GET /status never succeeded: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("/status: got %d, want 200", resp.StatusCode)
}
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body), "phase") {
t.Fatalf("/status body should contain version tag, got: %s", body)
}
}
func TestStartHTTPOps_FailsOnPortConflict(t *testing.T) {
// Hold a port so the ops server cannot bind it. This is the
// regression test for the honesty bug: the old code printed
// "listening" even when ListenAndServe() failed asynchronously.
blocker, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("could not reserve port: %v", err)
}
defer blocker.Close()
busyAddr := blocker.Addr().String()
_, _, err = startHTTPOps(busyAddr)
if err == nil {
t.Fatalf("expected bind failure on occupied port %s, got nil", busyAddr)
}
// Must be a real net error, not a wrapped log line.
if !strings.Contains(err.Error(), "bind") && !strings.Contains(err.Error(), "address") {
t.Logf("note: error is %v (platform-dependent message; this is not a failure)", err)
}
}
func TestRunPersistDemo_HappyPath(t *testing.T) {
dir := t.TempDir()
code := runPersistDemo(options{
json: true,
persistDemo: true,
persistDir: dir,
})
if code != 0 {
t.Fatalf("runPersistDemo exit code = %d, want 0 (data should survive restart)", code)
}
}
func TestRunPersistDemo_RequiresPersistDir(t *testing.T) {
code := runPersistDemo(options{
persistDemo: true,
persistDir: "", // missing
})
if code != 2 {
t.Fatalf("runPersistDemo without --persist-dir: code=%d, want 2 (usage)", code)
}
}
// TestRunPersistDemo_RepeatableInSameDir locks in that running the
// demo twice in the same dir works. The first run creates the file;
// the second run must remove the prior file and recreate cleanly.
// Without the prior-file cleanup, CreateWALStore's O_EXCL would fail
// on the second invocation.
func TestRunPersistDemo_RepeatableInSameDir(t *testing.T) {
dir := t.TempDir()
for i := 0; i < 2; i++ {
code := runPersistDemo(options{
json: true,
persistDemo: true,
persistDir: dir,
})
if code != 0 {
t.Fatalf("run %d: exit code = %d, want 0", i+1, code)
}
}
}
func TestRunSparrow_HTTPBindFailure_ReturnsExit3(t *testing.T) {
// End-to-end honesty check: when --http can't bind, runSparrow
// must exit with code 3 (runtime error), NOT silently proceed
// to run demos as if the server were live.
blocker, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("could not reserve port: %v", err)
}
defer blocker.Close()
busyAddr := blocker.Addr().String()
code := runSparrow(options{
json: true,
runs: 1,
httpAddr: busyAddr,
})
if code != 3 {
t.Fatalf("exit code = %d, want 3 (http bind failure)", code)
}
}
func TestBuildResult_FailWhenNotHealthy(t *testing.T) {
degraded := engine.ReplicaProjection{
Mode: engine.ModeDegraded,
RecoveryDecision: engine.DecisionUnknown,
R: 0, S: 10, H: 100,
}
r := buildResult("x", degraded)
if r.Pass {
t.Fatal("expected Pass=false for degraded mode")
}
if r.Reason == "" {
t.Fatal("failing result should carry a reason")
}
}