From 76cf29e7e09b2a4204194f2e9133e76817974e99 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 16:56:04 -0800 Subject: [PATCH 1/6] test: snapshot ladder rung F1 harness (N volumes, one server id, one host) V1-SPEC r2.49/r2.52. Opt-in (F1_RUN=1, a large test, never in CI): N blockvolume processes under ONE server id, each its own replica id and ports, snapshotted at the same instant per round under write load; every snapshot restored and verified (ledger exact, load a prefix); the JSON report carries cut time and barrier pause, client write p50/p99/max inside and outside snapshot windows, request-to-commit time, host-slot waits, restore time, chunks/bytes uploaded, filer entries per round and RSS. The procedure harness's bvHost gains optional server and replica ids. Co-Authored-By: Claude Opus 5.5 --- .../planned_handoff_integration_test.go | 15 +- .../snapshot_ladder_f1_integration_test.go | 616 ++++++++++++++++++ 2 files changed, 628 insertions(+), 3 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go index a515a3174a..43d8cb8b6d 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_integration_test.go @@ -185,10 +185,13 @@ type handoffEnv struct { } // bvHost is one host's blockvolume: its server id, endpoints, local root -// and the volume it serves (v1 when empty). +// and the volume it serves (v1 when empty). server and replica, when set, +// replace id as the server id and r1 as the replica id (several volumes +// under one server id). type bvHost struct { id, data, ctrl, status, nvme, root string volume string + server, replica string } // startHandoffEnv runs the chapter's steps 1-3: a weed cluster, the cluster @@ -267,11 +270,17 @@ func (env *handoffEnv) volArgs(id string) []string { return env.volArgsFor(env.h func (env *handoffEnv) volArgsFor(b bvHost) []string { lockDir := b.root + ".locks" // each simulated host has its own _ = os.MkdirAll(lockDir, 0o700) - vol := b.volume + vol, server, replica := b.volume, b.server, b.replica if vol == "" { vol = "v1" } - return append([]string{"--volume-lock-dir-test-only", lockDir, "--master", env.h.masterAddr, "--server-id", b.id, "--volume-id", vol, "--replica-id", "r1", + if server == "" { + server = b.id + } + if replica == "" { + replica = "r1" + } + return append([]string{"--volume-lock-dir-test-only", lockDir, "--master", env.h.masterAddr, "--server-id", server, "--volume-id", vol, "--replica-id", replica, "--data-addr", b.data, "--ctrl-addr", b.ctrl, "--status-addr", b.status, "--durable-root", b.root, "--durable-impl", "walstore-chunk", "--durable-blocks", "65536", "--durable-blocksize", "4096", diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go new file mode 100644 index 0000000000..5205d5e591 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -0,0 +1,616 @@ +//go:build subprocess && chunkstore_integration + +package main_test + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "math" + "net/http" + "net/url" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/frontend/nvme" + "github.com/seaweedfs/seaweed-block/core/hostlimit" +) + +// Snapshot ladder rung F1 (V1-SPEC r2.49, r2.52): N volumes on one host, +// every volume snapshotted at the same instant, round after round, under +// write load. It must hold: every snapshot restores exactly; no write fails; +// the host limiter paces the cuts and none starves (every snapshot commits +// within ceil(N/slots)+1 times the slowest single cut). It measures, per +// volume and round: the snapshot's cut (total and guest barrier pause), the +// client write latency inside and outside snapshot windows, request to +// commit time, host-slot waits, restore time, chunks and bytes uploaded, +// filer entries per snapshot, and blockvolume RSS. The JSON report goes to +// F1_REPORT. +// +// A large test: it runs only with F1_RUN=1, on a host booked for it, never in +// CI. Knobs: F1_VOLUMES (10), F1_ROUNDS (3), F1_WRITE_SECONDS (120), +// F1_HOST_LIMIT_DIR (a local, non-temporary directory; required for the +// limiter checks, a smoke may leave it unset), F1_HOST_LIMIT_SLOTS +// (the limiter's normal slots; default hostlimit's), F1_REPORT. +// +// Each volume has two regions. The ledger region is written and flushed +// before a round's snapshot, so the snapshot must hold it exactly. The load +// region is written in LBA order during the snapshot, so the snapshot must +// hold a prefix of it (A3's one-point-in-time check). + +type f1Config struct { + Volumes, Rounds int + WriteSeconds int + HostLimitDir string + Slots int + Report string + LedgerBlocksPerRnd int + LoadSpan int +} + +func f1ConfigFromEnv(t *testing.T) f1Config { + t.Helper() + if os.Getenv("F1_RUN") != "1" { + t.Skip("rung F1 is a large test: set F1_RUN=1 on a booked host") + } + num := func(k string, def int) int { + if v := os.Getenv(k); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 { + t.Fatalf("%s=%q: want a positive integer", k, v) + } + return n + } + return def + } + c := f1Config{Volumes: num("F1_VOLUMES", 10), Rounds: num("F1_ROUNDS", 3), WriteSeconds: num("F1_WRITE_SECONDS", 120), + HostLimitDir: os.Getenv("F1_HOST_LIMIT_DIR"), Slots: num("F1_HOST_LIMIT_SLOTS", hostlimit.DefaultConfig("").NormalSlots), + Report: os.Getenv("F1_REPORT"), LedgerBlocksPerRnd: 64, LoadSpan: 4096} + if c.Rounds*(c.LedgerBlocksPerRnd+c.LoadSpan) > ladderUntouched { + t.Fatalf("F1_ROUNDS=%d does not fit below the untouched chunk", c.Rounds) + } + return c +} + +// f1Volume is one source volume: its id, host record and daemon. +type f1Volume struct { + id, node string + d *daemon +} + +// f1Sample is one client write. +type f1Sample struct { + start time.Time + d time.Duration +} + +// f1Round is one volume's measurements in one round. +type f1Round struct { + Volume string `json:"volume"` + Round int `json:"round"` + SnapshotMs float64 `json:"snapshot_request_to_commit_ms"` + CutTotalMs float64 `json:"cut_total_ms"` + BarrierPauseMs float64 `json:"cut_barrier_pause_ms"` + UploadedChunks int `json:"uploaded_chunks"` + UploadedBytes int64 `json:"uploaded_bytes"` + HostSlotWaits uint64 `json:"host_slot_waits"` + RestoreMs float64 `json:"restore_ms"` + RestoreExact bool `json:"restore_exact"` + LoadPrefix int `json:"load_prefix_blocks"` + WritesIn int `json:"writes_in_window"` + WritesOut int `json:"writes_outside_window"` + WriteP50InMs float64 `json:"write_p50_in_ms"` + WriteP99InMs float64 `json:"write_p99_in_ms"` + WriteMaxInMs float64 `json:"write_max_in_ms"` + WriteP50OutMs float64 `json:"write_p50_out_ms"` + WriteP99OutMs float64 `json:"write_p99_out_ms"` + WriteMaxOutMs float64 `json:"write_max_out_ms"` + RSSKiB int64 `json:"blockvolume_rss_kib"` + FilerEntriesNew int `json:"filer_entries_added_by_round"` +} + +type f1Report struct { + Config f1Config `json:"config"` + Limiter string `json:"limiter"` + StartedAt time.Time `json:"started_at"` + Rounds []f1Round `json:"rounds"` + CommitBound float64 `json:"commit_bound_ms"` + WriteErrors int `json:"write_errors"` + Verdict string `json:"verdict"` + Failures []string `json:"failures,omitempty"` +} + +func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { + cfg := f1ConfigFromEnv(t) + rep := &f1Report{Config: cfg, StartedAt: time.Now().UTC(), Limiter: "off"} + env := f1StartEnv(t, cfg) + env.volExtra = []string{"--chunk-capture-interval", "0", "--chunk-snapshot-schedule", "off"} + if cfg.HostLimitDir != "" { + env.volExtra = append(env.volExtra, "--host-limit-dir", cfg.HostLimitDir, "--host-limit-normal-slots", strconv.Itoa(cfg.Slots)) + rep.Limiter = fmt.Sprintf("%s, %d normal slots", cfg.HostLimitDir, cfg.Slots) + } + vols := f1StartSources(t, env, cfg.Volumes) + defer f1WriteReport(t, cfg, rep) + + for round := 1; round <= cfg.Rounds; round++ { + before := f1FilerEntries(t, env) + rounds := f1RunRound(t, env, cfg, vols, round, rep) + added := f1FilerEntries(t, env) - before + for i := range rounds { + rounds[i].FilerEntriesNew = added + } + rep.Rounds = append(rep.Rounds, rounds...) + } + f1Judge(t, cfg, rep) +} + +func f1RestoreTarget(vol, round int) string { return fmt.Sprintf("v%dr%d", vol, round) } + +// f1StartEnv is the procedure harness laid out as one host (V1-SPEC r2.52, +// claude05 00:56Z): ONE server id, s1, and one blockvolume process per +// volume, each with its own replica id and its own data/ctrl/status/NVMe +// ports. Source v is replica r; the restore target of v in round k +// is replica r on the same server. The node record carries the first +// source's pair, as the deployment page writes one node. +func f1StartEnv(t *testing.T, cfg f1Config) *handoffEnv { + t.Helper() + h := newPlannedHandoffHarness(t) + env := &handoffEnv{h: h, cluster: startWorkspaceWeed(t), chunkPath: fmt.Sprintf("/f1-%d", time.Now().UnixNano()), hosts: map[string]bvHost{}} + must, _ := fatalOn(t) + add := func(key, vol string, replica int) { + b := bvHost{id: key, server: "s1", replica: fmt.Sprintf("r%d", replica), volume: vol, root: filepath.Join(h.dir, "vol-"+key), + data: handoffAddress(t), ctrl: handoffAddress(t), status: handoffAddress(t), nvme: handoffAddress(t)} + if key == "v1" { + b.data, b.ctrl, b.status, b.nvme = h.dataAddr, h.ctrlAddr, h.statusAddr, h.nvmeAddr + } + env.hosts[key] = b + } + for i := 1; i <= cfg.Volumes; i++ { + add(fmt.Sprintf("v%d", i), fmt.Sprintf("v%d", i), i) + } + for r := 1; r <= cfg.Rounds; r++ { + for i := 1; i <= cfg.Volumes; i++ { + v := f1RestoreTarget(i, r) + add(v, v, i+cfg.Volumes*r) + } + } + var vols strings.Builder + keys := make([]string, 0, len(env.hosts)) + for k := range env.hosts { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + b := env.hosts[k] + fmt.Fprintf(&vols, " - id: %s\n size_bytes: 268435456\n replication_factor: 1\n placements:\n - server_id: s1\n replica_id: %s\n", b.volume, b.replica) + } + spec := filepath.Join(h.dir, "cluster-spec.yaml") + must(os.WriteFile(spec, []byte(fmt.Sprintf("nodes:\n - server_id: s1\n data_addr: %s\n ctrl_addr: %s\nvolumes:\n%s", h.dataAddr, h.ctrlAddr, vols.String())), 0o644)) + mstate := filepath.Join(h.dir, "master") + master := h.start(t, "blockmaster", h.master, "--authority-store", filepath.Join(mstate, "authority"), "--lifecycle-store", filepath.Join(mstate, "lifecycle"), + "--cluster-spec", spec, "--expected-slots-per-volume", "1", "--listen", h.masterAddr, + "--workspace-rpc", "--workspace-head-base-url", "http://"+env.cluster.filerHTTP+env.chunkPath) + master.waitLog(t, 30*time.Second, "blockmaster: lock acquired, reloaded=") + return env +} + +// f1Up starts the blockvolume of key and waits for its volume to be primary +// and its frontend ready. +func f1Up(t *testing.T, env *handoffEnv, key string) *daemon { + t.Helper() + b := env.hosts[key] + d := env.h.start(t, "blockvolume-"+key, env.h.volume, env.volArgsFor(b)...) + d.waitLog(t, 60*time.Second, fmt.Sprintf("blockvolume: volume %s adopted as PRIMARY at %s@", b.volume, b.replica)) + poll(t, 60*time.Second, key+" never reported its frontend ready", func() bool { return volumeFrontendReady(b.status, b.volume) }) + return d +} + +// f1StartSources starts the N source volumes. +func f1StartSources(t *testing.T, env *handoffEnv, n int) []f1Volume { + t.Helper() + var vols []f1Volume + for i := 1; i <= n; i++ { + v := fmt.Sprintf("v%d", i) + vols = append(vols, f1Volume{id: v, node: v, d: f1Up(t, env, v)}) + } + return vols +} + +// f1Restore restores snapshot snap of src into target (a new volume on the +// same server) and drives the restore; the target's blockvolume is stopped +// once verified (f1RunRound), so restores do not pile up processes. +func f1Restore(t *testing.T, env *handoffEnv, src, snap, target string) *daemon { + t.Helper() + b := env.hosts[target] + env.h.command(t, "ops", "chunk", "restore", "--source", src, "--snapshot", snap, "--target", target, + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + d := f1Up(t, env, target) + env.h.command(t, "ops", "chunk", "restore", "--volume", target, "--status-addr", b.status) + return d +} + +func f1Block(vol string, round int, kind string, lba uint64) []byte { + return pattern(fmt.Sprintf("%s r%d %s %08d|", vol, round, kind, lba)) +} + +func f1LedgerBase(cfg f1Config, round int) uint64 { + return uint64((round - 1) * (cfg.LedgerBlocksPerRnd + cfg.LoadSpan)) +} + +// f1RunRound: write and flush each volume's ledger region, start the load +// writers, snapshot every volume at the same instant, stop the load, then +// restore and verify every snapshot. +func f1RunRound(t *testing.T, env *handoffEnv, cfg f1Config, vols []f1Volume, round int, rep *f1Report) []f1Round { + t.Helper() + for _, v := range vols { + c := dialNVMe(t, env.hosts[v.node].nvme, plannedHandoffNQN) + base := f1LedgerBase(cfg, round) + for i := 0; i < cfg.LedgerBlocksPerRnd; i++ { + c.write(t, base+uint64(i), f1Block(v.id, round, "ledger", base+uint64(i))) + } + c.flush(t) + c.close() + } + type writer struct { + mu sync.Mutex + samples []f1Sample + written int + err error + } + stop := make(chan struct{}) + ws := make([]*writer, len(vols)) + var wg sync.WaitGroup + for i, v := range vols { + ws[i] = &writer{} + c := dialNVMe(t, env.hosts[v.node].nvme, plannedHandoffNQN) + wg.Add(1) + go func(w *writer, c *nvmeInitiator, vol string) { + defer wg.Done() + defer c.close() + first := f1LedgerBase(cfg, round) + uint64(cfg.LedgerBlocksPerRnd) + for n := 0; n < cfg.LoadSpan; n++ { + select { + case <-stop: + return + default: + } + lba := first + uint64(n) + s := time.Now() + err := c.tryWrite(lba, f1Block(vol, round, "load", lba)) + if err == nil && n%16 == 15 { + err = c.tryFlush() + } + w.mu.Lock() + w.samples = append(w.samples, f1Sample{start: s, d: time.Since(s)}) + if err != nil { + w.err = err + } else { + w.written = n + 1 + } + w.mu.Unlock() + if err != nil { + return + } + } + }(ws[i], c, v.id) + } + time.Sleep(time.Duration(cfg.WriteSeconds) * time.Second / 2) // load before the snapshots + + type snapRes struct { + start, end time.Time + out, err string + code int + } + res := make([]snapRes, len(vols)) + var sg sync.WaitGroup + gate := make(chan struct{}) + for i, v := range vols { + sg.Add(1) + go func(i int, v f1Volume) { + defer sg.Done() + <-gate + res[i].start = time.Now() + res[i].out, res[i].err, res[i].code = env.h.run(t, "ops", "chunk", "snapshot", "--volume", v.id, "--status-addr", env.hosts[v.node].status, "--name", fmt.Sprintf("f1-r%d", round)) + res[i].end = time.Now() + }(i, v) + } + close(gate) + sg.Wait() + time.Sleep(time.Duration(cfg.WriteSeconds) * time.Second / 2) // load after the snapshots + close(stop) + wg.Wait() + + out := make([]f1Round, len(vols)) + for i, v := range vols { + r := f1Round{Volume: v.id, Round: round} + w := ws[i] + if w.err != nil { + rep.WriteErrors++ + rep.Failures = append(rep.Failures, fmt.Sprintf("%s round %d: write error: %v", v.id, round, w.err)) + } + if res[i].code != 0 { + rep.Failures = append(rep.Failures, fmt.Sprintf("%s round %d: snapshot exit %d: %s %s", v.id, round, res[i].code, res[i].out, res[i].err)) + out[i] = r + continue + } + r.SnapshotMs = ms(res[i].end.Sub(res[i].start)) + f1Latencies(&r, w.samples, res[i].start, res[i].end) + f1CutStats(t, env, v, &r) + r.RSSKiB = f1RSS(v.d) + target := f1RestoreTarget(i+1, round) + rs := time.Now() + rd := f1Restore(t, env, v.id, fmt.Sprintf("f1-r%d", round), target) + r.RestoreMs = ms(time.Since(rs)) + r.RestoreExact, r.LoadPrefix = f1Verify(t, env, cfg, v.id, target, round, w.written) + rd.term(t, 60*time.Second) + if !r.RestoreExact { + rep.Failures = append(rep.Failures, fmt.Sprintf("%s round %d: the restored snapshot is not the volume at its point", v.id, round)) + } + out[i] = r + } + return out +} + +// f1Verify reads the restored snapshot: every ledger block of rounds 1..round +// exact; each round's load region a prefix, at most what was written. +func f1Verify(t *testing.T, env *handoffEnv, cfg f1Config, vol, target string, round, written int) (bool, int) { + t.Helper() + c := dialNVMe(t, env.hosts[target].nvme, plannedHandoffNQN) + defer c.close() + zero := make([]byte, 4096) + prefix := 0 + for r := 1; r <= round; r++ { + base := f1LedgerBase(cfg, r) + for i := 0; i < cfg.LedgerBlocksPerRnd; i++ { + if !bytes.Equal(c.read(t, base+uint64(i), 4096), f1Block(vol, r, "ledger", base+uint64(i))) { + return false, 0 + } + } + first := base + uint64(cfg.LedgerBlocksPerRnd) + ended, k := false, 0 + for n := 0; n < cfg.LoadSpan; n++ { + got := c.read(t, first+uint64(n), 4096) + switch { + case !ended && bytes.Equal(got, f1Block(vol, r, "load", first+uint64(n))): + k++ + case bytes.Equal(got, zero) && r == round: + ended = true + case r < round && n >= k && bytes.Equal(got, zero): + ended = true + default: + return false, 0 + } + } + if r == round { + prefix = k + if k > written { + return false, k + } + } + } + return true, prefix +} + +// f1Latencies splits the client write latencies at the snapshot window. +func f1Latencies(r *f1Round, samples []f1Sample, from, to time.Time) { + var in, out []time.Duration + for _, s := range samples { + if !s.start.Before(from) && !s.start.After(to) { + in = append(in, s.d) + } else { + out = append(out, s.d) + } + } + r.WritesIn, r.WritesOut = len(in), len(out) + r.WriteP50InMs, r.WriteP99InMs, r.WriteMaxInMs = pct(in, 50), pct(in, 99), pct(in, 100) + r.WriteP50OutMs, r.WriteP99OutMs, r.WriteMaxOutMs = pct(out, 50), pct(out, 99), pct(out, 100) +} + +func pct(d []time.Duration, p float64) float64 { + if len(d) == 0 { + return 0 + } + s := append([]time.Duration(nil), d...) + sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) + i := int(math.Ceil(p/100*float64(len(s)))) - 1 + if i < 0 { + i = 0 + } + return ms(s[i]) +} + +func ms(d time.Duration) float64 { return float64(d) / float64(time.Millisecond) } + +// f1CutStats reads the volume's last cut (the snapshot's) from its durable +// status. +func f1CutStats(t *testing.T, env *handoffEnv, v f1Volume, r *f1Round) { + t.Helper() + c := &http.Client{Timeout: 5 * time.Second} + resp, err := c.Get("http://" + env.hosts[v.node].status + "/status/durable?volume=" + v.id) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + var st struct { + Volumes []struct { + VolumeID string + Chunk *struct { + HostSlotWaits uint64 + Stats struct { + LastCut *struct { + TotalNs, BarrierPauseNs int64 + UploadedChunks int + UploadedBytes int64 + } + } + } + } + } + body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<20)) + if err := json.Unmarshal(body, &st); err != nil { + t.Fatalf("durable status of %s: %v", v.id, err) + } + for _, s := range st.Volumes { + if s.VolumeID != v.id || s.Chunk == nil || s.Chunk.Stats.LastCut == nil { + continue + } + l := s.Chunk.Stats.LastCut + r.CutTotalMs, r.BarrierPauseMs = float64(l.TotalNs)/1e6, float64(l.BarrierPauseNs)/1e6 + r.UploadedChunks, r.UploadedBytes, r.HostSlotWaits = l.UploadedChunks, l.UploadedBytes, s.Chunk.HostSlotWaits + return + } + t.Fatalf("durable status of %s has no last cut: %s", v.id, body) +} + +// f1RSS is the blockvolume's resident set (KiB), 0 where /proc is absent. +func f1RSS(d *daemon) int64 { + b, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", d.proc.Pid)) + if err != nil { + return 0 + } + for _, l := range strings.Split(string(b), "\n") { + if strings.HasPrefix(l, "VmRSS:") { + f := strings.Fields(l) + if len(f) >= 2 { + n, _ := strconv.ParseInt(f[1], 10, 64) + return n + } + } + } + return 0 +} + +// f1FilerEntries counts the filer entries (files and directories) under the +// chunk path, by the filer's JSON listing. +func f1FilerEntries(t *testing.T, env *handoffEnv) int { + t.Helper() + c := &http.Client{Timeout: 30 * time.Second} + var walk func(dir string) int + walk = func(dir string) int { + n, last := 0, "" + for { + u := "http://" + env.cluster.filerHTTP + dir + "/?limit=1000&lastFileName=" + url.QueryEscape(last) + req, _ := http.NewRequest(http.MethodGet, u, nil) + req.Header.Set("Accept", "application/json") + resp, err := c.Do(req) + if err != nil { + t.Fatalf("filer list %s: %v", dir, err) + } + var page struct { + Entries []struct { + FullPath string + Mode uint32 + } + ShouldDisplayLoadMore bool + LastFileName string + } + err = json.NewDecoder(resp.Body).Decode(&page) + resp.Body.Close() + if err != nil { + t.Fatalf("filer list %s: %v", dir, err) + } + for _, e := range page.Entries { + n++ + if e.Mode&uint32(os.ModeDir) != 0 { + n += walk(e.FullPath) + } + } + if !page.ShouldDisplayLoadMore || page.LastFileName == "" { + return n + } + last = page.LastFileName + } + } + return walk(env.chunkPath) +} + +// f1Judge applies the pass rule: no failure recorded, and every snapshot +// committed within ceil(N/slots)+1 times the slowest single cut (N+1 when +// the limiter is off). +func f1Judge(t *testing.T, cfg f1Config, rep *f1Report) { + t.Helper() + worst, longest := 0.0, 0.0 + for _, r := range rep.Rounds { + worst = math.Max(worst, r.CutTotalMs) + longest = math.Max(longest, r.SnapshotMs) + } + waves := float64(cfg.Volumes + 1) + if cfg.HostLimitDir != "" { + waves = math.Ceil(float64(cfg.Volumes)/float64(cfg.Slots)) + 1 + } + rep.CommitBound = waves * worst + if longest > rep.CommitBound { + rep.Failures = append(rep.Failures, fmt.Sprintf("a snapshot took %.0f ms, above the bound %.0f ms (%.0f waves x the slowest cut %.0f ms): a volume starved", longest, rep.CommitBound, waves, worst)) + } + rep.Verdict = "PASS" + if len(rep.Failures) > 0 { + rep.Verdict = "FAIL" + t.Errorf("rung F1: %s", strings.Join(rep.Failures, "; ")) + } +} + +func f1WriteReport(t *testing.T, cfg f1Config, rep *f1Report) { + b, err := json.MarshalIndent(rep, "", " ") + if err != nil { + t.Errorf("report: %v", err) + return + } + t.Logf("rung F1 report: %s", b) + if cfg.Report != "" { + if err := os.WriteFile(cfg.Report, b, 0o644); err != nil { + t.Errorf("write %s: %v", cfg.Report, err) + } + } +} + +// tryWrite and tryFlush are write and flush returning the error (the load +// writers run off the test goroutine, where t.Fatal is not allowed). +func (c *nvmeInitiator) tryWrite(lba uint64, data []byte) error { + cmd := c.ioCmd(0x01, lba, len(data)/4096) + if err := c.w.SendHeaderOnly(0x4, &cmd, 64); err != nil { + return fmt.Errorf("nvme Write: %w", err) + } + ch, err := c.r.Dequeue() + if err != nil || ch.Type != 0x9 { + return fmt.Errorf("nvme Write: want R2T, got type %v err %v", ch, err) + } + var r2t nvme.R2THeader + if err := c.r.Receive(&r2t); err != nil { + return fmt.Errorf("nvme R2T: %w", err) + } + h2c := nvme.H2CDataHeader{CCCID: cmd.CID, TAG: r2t.TAG, DATAL: uint32(len(data))} + if err := c.w.SendWithData(0x6, 0, &h2c, 16, data); err != nil { + return fmt.Errorf("nvme H2CData: %w", err) + } + return c.tryStatus("Write") +} + +func (c *nvmeInitiator) tryFlush() error { + cmd := c.ioCmd(0x00, 0, 0) + if err := c.w.SendHeaderOnly(0x4, &cmd, 64); err != nil { + return fmt.Errorf("nvme Flush: %w", err) + } + return c.tryStatus("Flush") +} + +func (c *nvmeInitiator) tryStatus(op string) error { + ch, err := c.r.Dequeue() + if err != nil || ch.Type != 0x5 { + return fmt.Errorf("nvme %s response: type %v err %v", op, ch, err) + } + var resp nvme.CapsuleResponse + if err := c.r.Receive(&resp); err != nil { + return fmt.Errorf("nvme %s response body: %w", op, err) + } + if resp.Status != 0 { + return fmt.Errorf("nvme %s status 0x%04x", op, resp.Status) + } + return nil +} From ce2ca830aeb18d865cc6ac65664ed2ed2edfa4aa Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:09:03 -0800 Subject: [PATCH 2/6] test: F1 blocks are exactly 4096 bytes Co-Authored-By: Claude Opus 5.5 --- .../blockvolume/snapshot_ladder_f1_integration_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index 5205d5e591..1189e7b31d 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -236,8 +236,15 @@ func f1Restore(t *testing.T, env *handoffEnv, src, snap, target string) *daemon return d } +// f1Block is one deterministic 4096-byte block (pattern needs a length that +// divides 4096; these labels do not, so the block is filled explicitly). func f1Block(vol string, round int, kind string, lba uint64) []byte { - return pattern(fmt.Sprintf("%s r%d %s %08d|", vol, round, kind, lba)) + s := fmt.Sprintf("%s r%d %s %08d|", vol, round, kind, lba) + b := make([]byte, 4096) + for i := range b { + b[i] = s[i%len(s)] + } + return b } func f1LedgerBase(cfg f1Config, round int) uint64 { From 80b097c87e354c42871a5803a201c3a579da359b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:10:16 -0800 Subject: [PATCH 3/6] test: F1 load paced across the write period; filer entries per snapshot Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_f1_integration_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index 1189e7b31d..766a59474d 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -36,6 +36,8 @@ import ( // // A large test: it runs only with F1_RUN=1, on a host booked for it, never in // CI. Knobs: F1_VOLUMES (10), F1_ROUNDS (3), F1_WRITE_SECONDS (120), +// F1_LOAD_SPAN (8192 blocks per volume per round, written evenly across the +// write period, so writes run before, during and after the snapshots), // F1_HOST_LIMIT_DIR (a local, non-temporary directory; required for the // limiter checks, a smoke may leave it unset), F1_HOST_LIMIT_SLOTS // (the limiter's normal slots; default hostlimit's), F1_REPORT. @@ -72,7 +74,7 @@ func f1ConfigFromEnv(t *testing.T) f1Config { } c := f1Config{Volumes: num("F1_VOLUMES", 10), Rounds: num("F1_ROUNDS", 3), WriteSeconds: num("F1_WRITE_SECONDS", 120), HostLimitDir: os.Getenv("F1_HOST_LIMIT_DIR"), Slots: num("F1_HOST_LIMIT_SLOTS", hostlimit.DefaultConfig("").NormalSlots), - Report: os.Getenv("F1_REPORT"), LedgerBlocksPerRnd: 64, LoadSpan: 4096} + Report: os.Getenv("F1_REPORT"), LedgerBlocksPerRnd: 64, LoadSpan: num("F1_LOAD_SPAN", 8192)} if c.Rounds*(c.LedgerBlocksPerRnd+c.LoadSpan) > ladderUntouched { t.Fatalf("F1_ROUNDS=%d does not fit below the untouched chunk", c.Rounds) } @@ -114,6 +116,7 @@ type f1Round struct { WriteMaxOutMs float64 `json:"write_max_out_ms"` RSSKiB int64 `json:"blockvolume_rss_kib"` FilerEntriesNew int `json:"filer_entries_added_by_round"` + FilerPerSnap float64 `json:"filer_entries_per_snapshot"` } type f1Report struct { @@ -145,6 +148,7 @@ func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { added := f1FilerEntries(t, env) - before for i := range rounds { rounds[i].FilerEntriesNew = added + rounds[i].FilerPerSnap = float64(added) / float64(len(rounds)) } rep.Rounds = append(rep.Rounds, rounds...) } @@ -282,7 +286,17 @@ func f1RunRound(t *testing.T, env *handoffEnv, cfg f1Config, vols []f1Volume, ro defer wg.Done() defer c.close() first := f1LedgerBase(cfg, round) + uint64(cfg.LedgerBlocksPerRnd) + // Paced so the span covers the whole write period. + every := time.Duration(cfg.WriteSeconds) * time.Second / time.Duration(cfg.LoadSpan) + begin := time.Now() for n := 0; n < cfg.LoadSpan; n++ { + if wait := time.Until(begin.Add(time.Duration(n) * every)); wait > 0 { + select { + case <-stop: + return + case <-time.After(wait): + } + } select { case <-stop: return From 57432c20da7bd56f4cb7bd62e69dc156e9f6c844 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:12:15 -0800 Subject: [PATCH 4/6] test: F1 comment cites the spec, not a seat Co-Authored-By: Claude Opus 5.5 --- .../cmd/blockvolume/snapshot_ladder_f1_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index 766a59474d..f082641bf5 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -158,7 +158,7 @@ func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { func f1RestoreTarget(vol, round int) string { return fmt.Sprintf("v%dr%d", vol, round) } // f1StartEnv is the procedure harness laid out as one host (V1-SPEC r2.52, -// claude05 00:56Z): ONE server id, s1, and one blockvolume process per +// the layout ruling): ONE server id, s1, and one blockvolume process per // volume, each with its own replica id and its own data/ctrl/status/NVMe // ports. Source v is replica r; the restore target of v in round k // is replica r on the same server. The node record carries the first From 1f757bb78b0dcefa5ba0632ebbfa873d4e3b0a7e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:16:28 -0800 Subject: [PATCH 5/6] test: F1 watches heartbeat health: slot expiries, their effects, superseded lines V1-SPEC r2.53. The rung polls the master's view of every source volume's replica each second (GetVolumeStatus) and records each span the master does not observe it, epoch changes and frontend-not-ready polls; it counts the master's per-slot "heartbeat superseded" lines per volume. Pass rule: zero slot expiries. Co-Authored-By: Claude Opus 5.5 --- ...pshot_ladder_f1_health_integration_test.go | 148 ++++++++++++++++++ .../snapshot_ladder_f1_integration_test.go | 14 +- 2 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/snapshot_ladder_f1_health_integration_test.go diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_health_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_health_integration_test.go new file mode 100644 index 0000000000..2bb3b27df7 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_health_integration_test.go @@ -0,0 +1,148 @@ +//go:build subprocess && chunkstore_integration + +package main_test + +import ( + "context" + "fmt" + "os" + "strings" + "sync" + "time" + + control "github.com/seaweedfs/seaweed-block/core/rpc/control" + "github.com/seaweedfs/seaweed-block/core/rpc/link" +) + +// Rung F1's heartbeat-ordering watch (V1-SPEC r2.53): with N processes under +// one server id, a volume's slot at the master can expire when its +// heartbeats are dropped. The watch polls the master's view of every source +// volume's replica each second and records each span where the master does +// not observe it (a slot expiry), and what the volume shows meanwhile: an +// epoch change or its frontend leaving ready. At the end it counts the +// master's per-slot superseded lines for the volume ('blockmaster: +// heartbeat superseded ... volume= ...'; 0 where the master does not log +// them). + +// f1Health is one source volume's record over the whole run. +type f1Health struct { + Volume string `json:"volume"` + Replica string `json:"replica"` + Polls int `json:"polls"` + PollErrors int `json:"poll_errors"` + SlotExpiries int `json:"slot_expiries"` + SlotUnobservedMs float64 `json:"slot_unobserved_ms"` + EpochChanges int `json:"epoch_changes"` + FrontendNotReady int `json:"frontend_not_ready_polls"` + DuringExpiry []string `json:"during_expiry,omitempty"` + SupersededLogLines int `json:"master_superseded_lines"` +} + +type f1Watch struct { + stop chan struct{} + done chan struct{} + mu sync.Mutex + vols map[string]*f1Health +} + +// f1StartWatch starts polling the master for every source volume. +func f1StartWatch(env *handoffEnv, vols []f1Volume) *f1Watch { + w := &f1Watch{stop: make(chan struct{}), done: make(chan struct{}), vols: map[string]*f1Health{}} + for _, v := range vols { + w.vols[v.id] = &f1Health{Volume: v.id, Replica: env.hosts[v.node].replica} + } + go func() { + defer close(w.done) + conn, err := link.Dial(env.h.masterAddr) + if err != nil { + return + } + defer conn.Close() + cl := control.NewClusterEvidenceServiceClient(conn) + type state struct { + unobservedSince time.Time + epoch uint64 + } + st := map[string]*state{} + t := time.NewTicker(time.Second) + defer t.Stop() + for { + select { + case <-w.stop: + return + case <-t.C: + } + for _, v := range vols { + h, s := w.vols[v.id], st[v.id] + if s == nil { + s = &state{} + st[v.id] = s + } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ev, err := cl.GetVolumeStatus(ctx, &control.GetVolumeStatusRequest{VolumeId: v.id}) + cancel() + w.mu.Lock() + h.Polls++ + if err != nil { + h.PollErrors++ + w.mu.Unlock() + continue + } + observed := false + for _, r := range ev.GetReplicas() { + if r.GetReplicaId() == h.Replica && r.GetObserved() { + observed = true + } + } + now := time.Now() + switch { + case !observed && s.unobservedSince.IsZero(): + s.unobservedSince = now + h.SlotExpiries++ + case observed && !s.unobservedSince.IsZero(): + h.SlotUnobservedMs += ms(now.Sub(s.unobservedSince)) + s.unobservedSince = time.Time{} + } + if e := ev.GetEpoch(); s.epoch != 0 && e != s.epoch { + h.EpochChanges++ + if !s.unobservedSince.IsZero() { + h.DuringExpiry = append(h.DuringExpiry, fmt.Sprintf("epoch %d -> %d", s.epoch, e)) + } + } + s.epoch = ev.GetEpoch() + w.mu.Unlock() + ready := volumeFrontendReady(env.hosts[v.node].status, v.id) + w.mu.Lock() + if !ready { + h.FrontendNotReady++ + if !s.unobservedSince.IsZero() { + h.DuringExpiry = append(h.DuringExpiry, "frontend not ready at "+now.UTC().Format(time.RFC3339)) + } + } + w.mu.Unlock() + } + } + }() + return w +} + +// finish stops the watch and counts the master's superseded lines per +// volume from its log. +func (w *f1Watch) finish(masterLog string) []f1Health { + close(w.stop) + <-w.done + b, _ := os.ReadFile(masterLog) + lines := strings.Split(string(b), "\n") + w.mu.Lock() + defer w.mu.Unlock() + var out []f1Health + for _, h := range w.vols { + for _, l := range lines { + if strings.Contains(l, "heartbeat superseded") && strings.Contains(l, " volume="+h.Volume+" ") { + h.SupersededLogLines++ + } + } + out = append(out, *h) + } + return out +} diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index f082641bf5..5405edcf9d 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -124,6 +124,7 @@ type f1Report struct { Limiter string `json:"limiter"` StartedAt time.Time `json:"started_at"` Rounds []f1Round `json:"rounds"` + Health []f1Health `json:"heartbeat_health"` CommitBound float64 `json:"commit_bound_ms"` WriteErrors int `json:"write_errors"` Verdict string `json:"verdict"` @@ -133,7 +134,7 @@ type f1Report struct { func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { cfg := f1ConfigFromEnv(t) rep := &f1Report{Config: cfg, StartedAt: time.Now().UTC(), Limiter: "off"} - env := f1StartEnv(t, cfg) + env, master := f1StartEnv(t, cfg) env.volExtra = []string{"--chunk-capture-interval", "0", "--chunk-snapshot-schedule", "off"} if cfg.HostLimitDir != "" { env.volExtra = append(env.volExtra, "--host-limit-dir", cfg.HostLimitDir, "--host-limit-normal-slots", strconv.Itoa(cfg.Slots)) @@ -141,6 +142,7 @@ func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { } vols := f1StartSources(t, env, cfg.Volumes) defer f1WriteReport(t, cfg, rep) + watch := f1StartWatch(env, vols) for round := 1; round <= cfg.Rounds; round++ { before := f1FilerEntries(t, env) @@ -152,6 +154,7 @@ func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { } rep.Rounds = append(rep.Rounds, rounds...) } + rep.Health = watch.finish(master.log) f1Judge(t, cfg, rep) } @@ -163,7 +166,7 @@ func f1RestoreTarget(vol, round int) string { return fmt.Sprintf("v%dr%d", vol, // ports. Source v is replica r; the restore target of v in round k // is replica r on the same server. The node record carries the first // source's pair, as the deployment page writes one node. -func f1StartEnv(t *testing.T, cfg f1Config) *handoffEnv { +func f1StartEnv(t *testing.T, cfg f1Config) (*handoffEnv, *daemon) { t.Helper() h := newPlannedHandoffHarness(t) env := &handoffEnv{h: h, cluster: startWorkspaceWeed(t), chunkPath: fmt.Sprintf("/f1-%d", time.Now().UnixNano()), hosts: map[string]bvHost{}} @@ -202,7 +205,7 @@ func f1StartEnv(t *testing.T, cfg f1Config) *handoffEnv { "--cluster-spec", spec, "--expected-slots-per-volume", "1", "--listen", h.masterAddr, "--workspace-rpc", "--workspace-head-base-url", "http://"+env.cluster.filerHTTP+env.chunkPath) master.waitLog(t, 30*time.Second, "blockmaster: lock acquired, reloaded=") - return env + return env, master } // f1Up starts the blockvolume of key and waits for its volume to be primary @@ -567,6 +570,11 @@ func f1Judge(t *testing.T, cfg f1Config, rep *f1Report) { waves = math.Ceil(float64(cfg.Volumes)/float64(cfg.Slots)) + 1 } rep.CommitBound = waves * worst + for _, h := range rep.Health { + if h.SlotExpiries > 0 { + rep.Failures = append(rep.Failures, fmt.Sprintf("%s: the master lost its slot %d time(s) for %.0f ms in total (during: %v)", h.Volume, h.SlotExpiries, h.SlotUnobservedMs, h.DuringExpiry)) + } + } if longest > rep.CommitBound { rep.Failures = append(rep.Failures, fmt.Sprintf("a snapshot took %.0f ms, above the bound %.0f ms (%.0f waves x the slowest cut %.0f ms): a volume starved", longest, rep.CommitBound, waves, worst)) } From 3f42d194011c5bb415f90e68d1553369c760506c Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:17:33 -0800 Subject: [PATCH 6/6] test: F1 negative control for the heartbeat watch (F1_CONTROL_PAUSE_SECONDS) Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_f1_integration_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go index 5405edcf9d..494b282e20 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_f1_integration_test.go @@ -16,6 +16,7 @@ import ( "strconv" "strings" "sync" + "syscall" "testing" "time" @@ -36,6 +37,8 @@ import ( // // A large test: it runs only with F1_RUN=1, on a host booked for it, never in // CI. Knobs: F1_VOLUMES (10), F1_ROUNDS (3), F1_WRITE_SECONDS (120), +// F1_CONTROL_PAUSE_SECONDS (a negative control: stop the last volume's +// process that long after the rounds; the rung must then fail), // F1_LOAD_SPAN (8192 blocks per volume per round, written evenly across the // write period, so writes run before, during and after the snapshots), // F1_HOST_LIMIT_DIR (a local, non-temporary directory; required for the @@ -55,6 +58,7 @@ type f1Config struct { Report string LedgerBlocksPerRnd int LoadSpan int + ControlPauseSecs int } func f1ConfigFromEnv(t *testing.T) f1Config { @@ -75,6 +79,9 @@ func f1ConfigFromEnv(t *testing.T) f1Config { c := f1Config{Volumes: num("F1_VOLUMES", 10), Rounds: num("F1_ROUNDS", 3), WriteSeconds: num("F1_WRITE_SECONDS", 120), HostLimitDir: os.Getenv("F1_HOST_LIMIT_DIR"), Slots: num("F1_HOST_LIMIT_SLOTS", hostlimit.DefaultConfig("").NormalSlots), Report: os.Getenv("F1_REPORT"), LedgerBlocksPerRnd: 64, LoadSpan: num("F1_LOAD_SPAN", 8192)} + if v := os.Getenv("F1_CONTROL_PAUSE_SECONDS"); v != "" { + c.ControlPauseSecs = num("F1_CONTROL_PAUSE_SECONDS", 0) + } if c.Rounds*(c.LedgerBlocksPerRnd+c.LoadSpan) > ladderUntouched { t.Fatalf("F1_ROUNDS=%d does not fit below the untouched chunk", c.Rounds) } @@ -154,6 +161,20 @@ func TestSnapshotLadderF1VolumesOneHost(t *testing.T) { } rep.Rounds = append(rep.Rounds, rounds...) } + if cfg.ControlPauseSecs > 0 { + // Negative control: the last volume's blockvolume is stopped + // (SIGSTOP) past the master's freshness window, so its heartbeats + // stop; the watch must record a slot expiry and the rung must fail. + last := vols[len(vols)-1] + if err := last.d.proc.Signal(syscall.SIGSTOP); err != nil { + t.Fatalf("control: SIGSTOP %s: %v", last.id, err) + } + time.Sleep(time.Duration(cfg.ControlPauseSecs) * time.Second) + if err := last.d.proc.Signal(syscall.SIGCONT); err != nil { + t.Fatalf("control: SIGCONT %s: %v", last.id, err) + } + time.Sleep(3 * time.Second) + } rep.Health = watch.finish(master.log) f1Judge(t, cfg, rep) }