test: snapshot ladder A1 and A2 on one volume, real binaries

A1: one block written, one snapshot, the snapshot read back block by
block holds that block and zeros around it. A2: an incremental snapshot
holds the volume at its point, the first snapshot is unchanged, and the
second snapshot uploads only the two chunks that changed. A snapshot is
read through the product's own path, a restore into a new volume, so the
tests do not depend on the chunk format. The procedure harness gains
restore-target volumes in the cluster spec, each on a node of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-22 15:12:47 -08:00
1 parent befbf85315
commit 6eed6f3c85
3 files changed
+212 -13

No files matched your search

@@ -338,3 +338,31 @@ func TestPlannedHandoffChecksReadCapturedProductOutput(t *testing.T) {
t.Error("status check does not read the epoch of a healthy primary")
}
}
// lastCutUploadedChunks reads Chunk.Stats.LastCut.UploadedChunks of vol from a
// /status/durable body.
func lastCutUploadedChunks(body []byte, vol string) (int, error) {
var st struct {
Volumes []struct {
VolumeID string
Chunk *struct {
Stats struct {
LastCut *struct{ UploadedChunks int }
}
}
}
}
if err := json.Unmarshal(body, &st); err != nil {
return 0, fmt.Errorf("durable status is not JSON: %v", err)
}
for _, v := range st.Volumes {
if v.VolumeID != vol {
continue
}
if v.Chunk == nil || v.Chunk.Stats.LastCut == nil {
return 0, fmt.Errorf("durable status of %s has no last cut", vol)
}
return v.Chunk.Stats.LastCut.UploadedChunks, nil
}
return 0, fmt.Errorf("durable status has no volume %s", vol)
}
@@ -181,15 +181,26 @@ type handoffEnv struct {
hosts map[string]bvHost
}
// bvHost is one host's blockvolume: its server id, endpoints and local root.
// bvHost is one host's blockvolume: its server id, endpoints, local root
// and the volume it serves (v1 when empty).
type bvHost struct {
id, data, ctrl, status, nvme, root string
volume string
}
// startHandoffEnv runs the chapter's steps 1-3: a weed cluster, the cluster
// spec with hosts s1..sN, blockmaster with the lifecycle store (and
// masterArgs), and the setup check.
func startHandoffEnv(t *testing.T, n int, masterArgs ...string) *handoffEnv {
t.Helper()
return startHandoffEnvWith(t, n, nil, masterArgs...)
}
// startHandoffEnvWith is startHandoffEnv with more volume records in the
// cluster spec: each of targets (a volume id) is placed on a node of its
// own, named after it, with its own endpoints and local root (a restore
// target, for example).
func startHandoffEnvWith(t *testing.T, n int, targets []string, masterArgs ...string) *handoffEnv {
t.Helper()
h := newPlannedHandoffHarness(t)
env := &handoffEnv{h: h, cluster: startWorkspaceWeed(t), chunkPath: fmt.Sprintf("/handoff-%d", time.Now().UnixNano()), hosts: map[string]bvHost{}}
@@ -199,23 +210,30 @@ func startHandoffEnv(t *testing.T, n int, masterArgs ...string) *handoffEnv {
id := fmt.Sprintf("s%d", i)
env.hosts[id] = bvHost{id: id, data: handoffAddress(t), ctrl: handoffAddress(t), status: handoffAddress(t), nvme: handoffAddress(t), root: filepath.Join(h.dir, "vol-"+id)}
}
for _, v := range targets {
id := "n-" + v
env.hosts[id] = bvHost{id: id, data: handoffAddress(t), ctrl: handoffAddress(t), status: handoffAddress(t), nvme: handoffAddress(t), root: filepath.Join(h.dir, "vol-"+id), volume: v}
}
cluster, chunkPath := env.cluster, env.chunkPath
// 1. The cluster spec.
var nodes strings.Builder
for i := 1; i <= n; i++ {
b := env.hosts[fmt.Sprintf("s%d", i)]
var nodes, volumes strings.Builder
node := func(b bvHost) {
fmt.Fprintf(&nodes, " - server_id: %s\n data_addr: %s\n ctrl_addr: %s\n", b.id, b.data, b.ctrl)
}
volume := func(v, server string) {
fmt.Fprintf(&volumes, " - id: %s\n size_bytes: 268435456\n replication_factor: 1\n placements:\n - server_id: %s\n replica_id: r1\n", v, server)
}
for i := 1; i <= n; i++ {
node(env.hosts[fmt.Sprintf("s%d", i)])
}
volume("v1", "s1")
for _, v := range targets {
node(env.hosts["n-"+v])
volume(v, "n-"+v)
}
spec := filepath.Join(h.dir, "cluster-spec.yaml")
must(os.WriteFile(spec, []byte("nodes:\n"+nodes.String()+`volumes:
- id: v1
size_bytes: 268435456
replication_factor: 1
placements:
- server_id: s1
replica_id: r1
`), 0o644))
must(os.WriteFile(spec, []byte("nodes:\n"+nodes.String()+"volumes:\n"+volumes.String()), 0o644))
// 2. blockmaster with the lifecycle store and the cluster spec.
mstate := filepath.Join(h.dir, "master")
@@ -246,7 +264,11 @@ 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)
return []string{"--volume-lock-dir-test-only", lockDir, "--master", env.h.masterAddr, "--server-id", b.id, "--volume-id", "v1", "--replica-id", "r1",
vol := b.volume
if vol == "" {
vol = "v1"
}
return []string{"--volume-lock-dir-test-only", lockDir, "--master", env.h.masterAddr, "--server-id", b.id, "--volume-id", vol, "--replica-id", "r1",
"--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",
@@ -0,0 +1,149 @@
//go:build subprocess && chunkstore_integration
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"
"time"
)
// The snapshot test ladder, rungs A (one volume): each rung adds one thing.
// It tests what a snapshot contains through the product's own read path for
// a snapshot, a restore into a new volume, so the same tests hold on any
// chunk format. Volumes are 65536 blocks of 4096 bytes in chunks of 1024
// blocks (chunk c holds LBAs c*1024 .. c*1024+1023).
const ladderChunkBlocks = 1024
// TestSnapshotLadderA1OneBlockOneSnapshot: write one block, take a snapshot,
// and read the snapshot back block by block: it holds that block and zeros
// around it, as the volume did at the snapshot.
func TestSnapshotLadderA1OneBlockOneSnapshot(t *testing.T) {
env := startHandoffEnvWith(t, 1, []string{"v2"})
src := env.start(t, "blockvolume-v1", "s1")
env.adopt(t, src, "s1")
block := pattern("ladder-a1-block!")
env.writeBlock(t, "s1", 5, block)
env.snapshot(t, "s1", "v1", "a1")
env.restoreInto(t, "v1", "a1", "v2")
want := map[uint64][]byte{5: block}
env.requireBlocks(t, "n-v2", 0, 16, want)
env.requireBlocks(t, "s1", 0, 16, want) // the source is unchanged
}
// TestSnapshotLadderA2IncrementalSnapshot: write, snapshot, write a few
// blocks in two chunks, snapshot again. Snapshot 2 holds the volume at its
// point, snapshot 1 is unchanged, and the second snapshot uploaded only the
// two chunks that changed.
func TestSnapshotLadderA2IncrementalSnapshot(t *testing.T) {
env := startHandoffEnvWith(t, 1, []string{"v2", "v3"})
src := env.start(t, "blockvolume-v1", "s1")
env.adopt(t, src, "s1")
a, b, c := pattern("ladder-a2-blk-A!"), pattern("ladder-a2-blk-B!"), pattern("ladder-a2-blk-C!")
env.writeBlock(t, "s1", 0, a)
env.snapshot(t, "s1", "v1", "s1")
if n := env.lastCutUploads(t, "s1", "v1"); n != 1 {
t.Fatalf("the first snapshot uploaded %d chunks; want 1 (chunk 0)", n)
}
env.writeBlock(t, "s1", 1, c) // chunk 0 again
env.writeBlock(t, "s1", 2*ladderChunkBlocks, b) // chunk 2
env.snapshot(t, "s1", "v1", "s2")
if n := env.lastCutUploads(t, "s1", "v1"); n != 2 {
t.Fatalf("the incremental snapshot uploaded %d chunks; want 2 (chunks 0 and 2, the ones that changed)", n)
}
env.restoreInto(t, "v1", "s1", "v2")
env.requireBlocks(t, "n-v2", 0, 4, map[uint64][]byte{0: a})
env.requireBlocks(t, "n-v2", 2*ladderChunkBlocks, 2, nil)
env.restoreInto(t, "v1", "s2", "v3")
env.requireBlocks(t, "n-v3", 0, 4, map[uint64][]byte{0: a, 1: c})
env.requireBlocks(t, "n-v3", 2*ladderChunkBlocks, 2, map[uint64][]byte{2 * ladderChunkBlocks: b})
}
// snapshot takes the named snapshot of vol on its host and checks the answer
// names it.
func (env *handoffEnv) snapshot(t *testing.T, hostID, vol, name string) {
t.Helper()
out, _ := env.h.command(t, "ops", "chunk", "snapshot", "--volume", vol, "--status-addr", env.hosts[hostID].status, "--name", name)
var info struct {
Name string `json:"name"`
}
if err := json.Unmarshal([]byte(out), &info); err != nil || info.Name != name {
t.Fatalf("snapshot %s of %s: %v\n%s", name, vol, err, out)
}
}
// lastCutUploads is the number of chunks the last cut of vol uploaded, from
// its host's durable status.
func (env *handoffEnv) lastCutUploads(t *testing.T, hostID, vol string) int {
t.Helper()
c := &http.Client{Timeout: 5 * time.Second}
resp, err := c.Get("http://" + env.hosts[hostID].status + "/status/durable?volume=" + vol)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
n, err := lastCutUploadedChunks(body, vol)
if err != nil {
t.Fatal(err)
}
return n
}
// restoreInto restores snapshot snap of src into the new volume target, as
// the deployment page says: create it, start its blockvolume, start the
// restore driver there; it serves at once.
func (env *handoffEnv) restoreInto(t *testing.T, src, snap, target string) {
t.Helper()
b := env.hosts["n-"+target]
env.h.command(t, "ops", "chunk", "restore", "--source", src, "--snapshot", snap, "--target", target,
"--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath)
d := env.h.start(t, "blockvolume-"+target, env.h.volume, env.volArgsFor(b)...)
d.waitLog(t, 60*time.Second, fmt.Sprintf("blockvolume: volume %s adopted as PRIMARY at r1@", target))
poll(t, 60*time.Second, fmt.Sprintf("%s never reported its frontend ready", target), func() bool {
return volumeFrontendReady(b.status, target)
})
env.h.command(t, "ops", "chunk", "restore", "--volume", target, "--status-addr", b.status)
}
// requireBlocks reads n blocks from lba on the host and requires each to be
// want[lba] (zeros where want has none).
func (env *handoffEnv) requireBlocks(t *testing.T, hostID string, lba uint64, n int, want map[uint64][]byte) {
t.Helper()
zero := make([]byte, 4096)
c := dialNVMe(t, env.hosts[hostID].nvme, plannedHandoffNQN)
defer c.close()
var bad []string
for i := uint64(0); i < uint64(n); i++ {
exp, ok := want[lba+i]
if !ok {
exp = zero
}
if got := c.read(t, lba+i, 4096); !bytes.Equal(got, exp) {
bad = append(bad, fmt.Sprint(lba+i))
}
}
if len(bad) > 0 {
t.Fatalf("%s: blocks %s differ from the snapshot's point", hostID, strings.Join(bad, ","))
}
}
func volumeFrontendReady(statusAddr, vol string) bool {
c := &http.Client{Timeout: 2 * time.Second}
resp, err := c.Get("http://" + statusAddr + "/status?volume=" + vol)
if err != nil {
return false
}
defer resp.Body.Close()
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
return frontendReady(b)
}