From b9d304317156308426251465c4c4be409a3d6c65 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 16:06:47 -0800 Subject: [PATCH 1/5] test: snapshot ladder D1-D4 (deletes and the collector), not yet gated Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_d_integration_test.go | 203 ++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go new file mode 100644 index 0000000000..908bc1a075 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -0,0 +1,203 @@ +//go:build subprocess && chunkstore_integration + +package main_test + +import ( + "encoding/json" + "fmt" + "testing" + "time" +) + +// Rung D of the snapshot ladder: deletes. A deleted snapshot leaves every +// other snapshot restorable, whichever one is deleted (the newest, a middle +// one, one whose content recurs later), and after the collector has run the +// store holds only what the remaining manifests reference. +// +// The collector's grace may not be shorter than the operation deadline plus +// the clock skew plus the CAS apply bound (11 minutes with the defaults), and +// an unreferenced chunk is reclaimed only by a second mark at least the grace +// after the first, on a chunk older than the grace: D4 runs for about 25 +// minutes. + +const ( + ladderGCGrace = 11 * time.Minute + // ladderGCWait is the grace plus the collector's clock skew allowance + // and its one-second timestamp resolution, with a margin. + ladderGCWait = ladderGCGrace + time.Minute + 15*time.Second +) + +// deleteSnapshot deletes vol's snapshot name and checks the answer names it. +func (env *handoffEnv) deleteSnapshot(t *testing.T, vol, name string) { + t.Helper() + out, _ := env.h.command(t, "ops", "chunk", "snapshot-delete", "--volume", vol, "--name", name, + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + var body struct { + VolumeID string `json:"volumeID"` + Name string `json:"name"` + } + if err := json.Unmarshal([]byte(out), &body); err != nil || body.Name != name { + t.Fatalf("delete %s of %s: %v\n%s", name, vol, err, out) + } +} + +// requireSnapshotNames checks vol's snapshot list is exactly names, in order. +func (env *handoffEnv) requireSnapshotNames(t *testing.T, vol string, names ...string) { + t.Helper() + list := env.snapshots(t, vol, "") + ok := len(list) == len(names) + for i := 0; ok && i < len(list); i++ { + ok = list[i].Name == names[i] && list[i].State == "active" + } + if !ok { + t.Fatalf("snapshots of %s: %+v; want exactly %v, active", vol, list, names) + } +} + +// ladderUsage is what the usage command says about a volume's store. +type ladderUsage struct { + ManifestBindings int `json:"manifestBindings"` + StoredBindings int `json:"storedBindings"` + StoredUpperBound bool `json:"storedUpperBound"` +} + +func (env *handoffEnv) usage(t *testing.T, vol string) ladderUsage { + t.Helper() + out, _ := env.h.command(t, "ops", "chunk", "usage", "--volume", vol, + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + var u ladderUsage + if err := json.Unmarshal([]byte(out), &u); err != nil { + t.Fatalf("usage of %s: %v\n%s", vol, err, out) + } + return u +} + +// gc runs one applying collector pass and returns how many chunks it +// reclaimed. +func (env *handoffEnv) gc(t *testing.T) int { + t.Helper() + out, _ := env.h.command(t, "ops", "chunk", "gc", "--master", env.h.masterAddr, "--apply", "--grace", ladderGCGrace.String(), + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + var body struct { + MarkSaved bool `json:"mark_saved"` + UnknownOutcomes []string `json:"unknown_outcomes"` + FailedDeletes []any `json:"failed_deletes"` + Candidates int + Reclaimed int + } + if err := json.Unmarshal([]byte(out), &body); err != nil || !body.MarkSaved || len(body.UnknownOutcomes) != 0 || len(body.FailedDeletes) != 0 { + t.Fatalf("gc pass: %v\n%s", err, out) + } + t.Logf("gc pass: %d candidates, %d reclaimed", body.Candidates, body.Reclaimed) + return body.Reclaimed +} + +// restoreChecked restores snap of v1 into target to independence and checks +// it equals img. +func (env *handoffEnv) restoreChecked(t *testing.T, snap, target string, img map[uint64][]byte) { + t.Helper() + env.restoreInto(t, "v1", snap, target) + env.waitIndependent(t, target) + env.requireImage(t, "n-"+target, img) +} + +// TestSnapshotLadderD1DeleteTheNewest: three snapshots over overlapping +// chunks; the newest is deleted; the other two restore as they were. +func TestSnapshotLadderD1DeleteTheNewest(t *testing.T) { + env := startLadderEnv(t, "v2", "v3") + img1 := ladderImage("d1one", []uint64{0, 4}, 4) + img2 := ladderImage("d1two", []uint64{4, 8}, 3) + img3 := ladderImage("d1three", []uint64{0, 8}, 2) + for i, img := range []map[uint64][]byte{img1, img2, img3} { + env.writeImage(t, "s1", img) + env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) + } + env.deleteSnapshot(t, "v1", "s3") + env.requireSnapshotNames(t, "v1", "s1", "s2") + env.restoreChecked(t, "s1", "v2", img1) + env.restoreChecked(t, "s2", "v3", mergeImages(img1, img2)) +} + +// TestSnapshotLadderD2DeleteAMiddleOne: four snapshots, each sharing chunks +// with its neighbours; the second is deleted; the first, the third and the +// fourth restore as they were (the deleted one's chunks that a neighbour or a +// later snapshot still uses are kept). +func TestSnapshotLadderD2DeleteAMiddleOne(t *testing.T) { + env := startLadderEnv(t, "v2", "v3", "v4") + img1 := ladderImage("d2one", []uint64{0, 2}, 4) + img2 := ladderImage("d2two", []uint64{2, 5}, 3) // chunk 2 shared with s1, chunk 5 kept by s3 + img3 := ladderImage("d2three", []uint64{0, 9}, 2) + img4 := ladderImage("d2four", []uint64{9, 12}, 2) + for i, img := range []map[uint64][]byte{img1, img2, img3, img4} { + env.writeImage(t, "s1", img) + env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) + } + env.deleteSnapshot(t, "v1", "s2") + env.requireSnapshotNames(t, "v1", "s1", "s3", "s4") + env.restoreChecked(t, "s1", "v2", img1) + env.restoreChecked(t, "s3", "v3", mergeImages(img1, img2, img3)) + env.restoreChecked(t, "s4", "v4", mergeImages(img1, img2, img3, img4)) +} + +// TestSnapshotLadderD3DeleteWhenContentRecurs: the A, B, A volume of A4 +// (one block holds A, then B, then A again, a snapshot after each); the +// first snapshot is deleted; the third, whose content equals the first's, +// and the second restore as they were. +func TestSnapshotLadderD3DeleteWhenContentRecurs(t *testing.T) { + env := startLadderEnv(t, "v2", "v3") + a, b := pattern("ladder-d3-blk-A!"), pattern("ladder-d3-blk-B!") + for i, content := range [][]byte{a, b, a} { + env.writeBlock(t, "s1", 7, content) + env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) + } + env.deleteSnapshot(t, "v1", "s1") + env.requireSnapshotNames(t, "v1", "s2", "s3") + env.restoreChecked(t, "s3", "v2", map[uint64][]byte{7: a}) + env.restoreChecked(t, "s2", "v3", map[uint64][]byte{7: b}) +} + +// TestSnapshotLadderD4DeleteAllButOneThenCollect: three snapshots, each +// overwriting the same chunks; the second and the third are deleted. The +// second's chunks are then referenced by nothing (the third's are still +// HEAD's), and the store holds more than the manifests reference. Two +// collector passes the grace apart reclaim exactly that surplus: the store +// then holds only what the remaining snapshot and HEAD reference, and the +// remaining snapshot and HEAD read as they were. +func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { + env := startLadderEnv(t, "v2") + chunks := []uint64{0, 1, 2, 3} + imgs := []map[uint64][]byte{ladderImage("d4one", chunks, 4), ladderImage("d4two", chunks, 4), ladderImage("d4three", chunks, 4)} + for i, img := range imgs { + env.writeImage(t, "s1", img) + env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) + } + env.deleteSnapshot(t, "v1", "s3") + env.deleteSnapshot(t, "v1", "s2") + env.requireSnapshotNames(t, "v1", "s1") + written := time.Now() + + before := env.usage(t, "v1") + surplus := before.StoredBindings - before.ManifestBindings + t.Logf("before the collector: %+v", before) + if surplus < len(chunks) || !before.StoredUpperBound { + t.Fatalf("after the deletes the store holds %d unreferenced chunks; want at least the %d of s2", surplus, len(chunks)) + } + + time.Sleep(time.Until(written.Add(ladderGCWait))) // the chunks are older than the grace + reclaimed := env.gc(t) + first := time.Now() + time.Sleep(time.Until(first.Add(ladderGCWait))) // a second mark the grace after the first + reclaimed += env.gc(t) + + after := env.usage(t, "v1") + t.Logf("after the collector: %+v", after) + if after.StoredBindings != after.ManifestBindings || after.StoredUpperBound || reclaimed != surplus { + t.Fatalf("after the collector the store holds %d chunks, the manifests reference %d (upper bound %v); reclaimed %d of %d", + after.StoredBindings, after.ManifestBindings, after.StoredUpperBound, reclaimed, surplus) + } + if after.ManifestBindings != before.ManifestBindings { + t.Fatalf("the collector changed what the manifests reference: %d, was %d", after.ManifestBindings, before.ManifestBindings) + } + env.requireImage(t, "s1", imgs[2]) + env.restoreChecked(t, "s1", "v2", imgs[0]) +} From 754fc54404a74307922fb5989ce2f08c0f912d2e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 16:28:23 -0800 Subject: [PATCH 2/5] test: snapshot ladder D4 measures reclaimed chunk data in the store Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_d_integration_test.go | 220 +++++++++++++----- 1 file changed, 157 insertions(+), 63 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go index 908bc1a075..d79fd6b58c 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -5,14 +5,17 @@ package main_test import ( "encoding/json" "fmt" + "io" + "net/http" + "strings" "testing" "time" ) // Rung D of the snapshot ladder: deletes. A deleted snapshot leaves every // other snapshot restorable, whichever one is deleted (the newest, a middle -// one, one whose content recurs later), and after the collector has run the -// store holds only what the remaining manifests reference. +// one, one whose content recurs later), and the collector then reclaims the +// chunk data only the deleted snapshots referenced. // // The collector's grace may not be shorter than the operation deadline plus // the clock skew plus the CAS apply bound (11 minutes with the defaults), and @@ -54,44 +57,6 @@ func (env *handoffEnv) requireSnapshotNames(t *testing.T, vol string, names ...s } } -// ladderUsage is what the usage command says about a volume's store. -type ladderUsage struct { - ManifestBindings int `json:"manifestBindings"` - StoredBindings int `json:"storedBindings"` - StoredUpperBound bool `json:"storedUpperBound"` -} - -func (env *handoffEnv) usage(t *testing.T, vol string) ladderUsage { - t.Helper() - out, _ := env.h.command(t, "ops", "chunk", "usage", "--volume", vol, - "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) - var u ladderUsage - if err := json.Unmarshal([]byte(out), &u); err != nil { - t.Fatalf("usage of %s: %v\n%s", vol, err, out) - } - return u -} - -// gc runs one applying collector pass and returns how many chunks it -// reclaimed. -func (env *handoffEnv) gc(t *testing.T) int { - t.Helper() - out, _ := env.h.command(t, "ops", "chunk", "gc", "--master", env.h.masterAddr, "--apply", "--grace", ladderGCGrace.String(), - "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) - var body struct { - MarkSaved bool `json:"mark_saved"` - UnknownOutcomes []string `json:"unknown_outcomes"` - FailedDeletes []any `json:"failed_deletes"` - Candidates int - Reclaimed int - } - if err := json.Unmarshal([]byte(out), &body); err != nil || !body.MarkSaved || len(body.UnknownOutcomes) != 0 || len(body.FailedDeletes) != 0 { - t.Fatalf("gc pass: %v\n%s", err, out) - } - t.Logf("gc pass: %d candidates, %d reclaimed", body.Candidates, body.Reclaimed) - return body.Reclaimed -} - // restoreChecked restores snap of v1 into target to independence and checks // it equals img. func (env *handoffEnv) restoreChecked(t *testing.T, snap, target string, img map[uint64][]byte) { @@ -156,47 +121,176 @@ func TestSnapshotLadderD3DeleteWhenContentRecurs(t *testing.T) { env.restoreChecked(t, "s2", "v3", map[uint64][]byte{7: b}) } +// gcObject is one chunk object a collector pass names as eligible. +type gcObject struct { + Volume string `json:"volume"` + Key string `json:"key"` + FID string `json:"fid"` +} + +// gc runs one collector pass (applying or a preview) and returns the chunk +// objects it found eligible and how many it reclaimed. +func (env *handoffEnv) gc(t *testing.T, apply bool) ([]gcObject, int) { + t.Helper() + args := []string{"ops", "chunk", "gc", "--master", env.h.masterAddr, "--grace", ladderGCGrace.String(), + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath} + if apply { + args = append(args, "--apply") + } + out, _ := env.h.command(t, args...) + var body struct { + MarkSaved bool `json:"mark_saved"` + UnknownOutcomes []string `json:"unknown_outcomes"` + FailedDeletes []any `json:"failed_deletes"` + WouldDelete []gcObject `json:"would_delete"` + Candidates int + Reclaimed int + } + if err := json.Unmarshal([]byte(out), &body); err != nil || (apply && !body.MarkSaved) || len(body.UnknownOutcomes) != 0 || len(body.FailedDeletes) != 0 { + t.Fatalf("gc pass (apply %v): %v\n%s", apply, err, out) + } + t.Logf("gc pass (apply %v): %d candidates, %d eligible, %d reclaimed", apply, body.Candidates, len(body.WouldDelete), body.Reclaimed) + return body.WouldDelete, body.Reclaimed +} + +// exclusiveBytes is what the usage command says deleting snapshot name of vol +// makes eligible for collection: the chunk bytes no other holder references. +func (env *handoffEnv) exclusiveBytes(t *testing.T, vol, name string) uint64 { + t.Helper() + out, _ := env.h.command(t, "ops", "chunk", "usage", "--volume", vol, + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + var u struct { + Snapshots []struct { + Name string `json:"name"` + ExclusiveBytes uint64 `json:"exclusiveBytes"` + } `json:"snapshots"` + } + if err := json.Unmarshal([]byte(out), &u); err != nil { + t.Fatalf("usage of %s: %v\n%s", vol, err, out) + } + for _, s := range u.Snapshots { + if s.Name == name { + return s.ExclusiveBytes + } + } + t.Fatalf("usage of %s has no snapshot %s\n%s", vol, name, out) + return 0 +} + +// blobStored reports whether the weed store still holds the chunk data of +// fid (a volume server answers 200 for it, 404 once it is deleted). +func (env *handoffEnv) blobStored(t *testing.T, fid string) bool { + t.Helper() + c := &http.Client{Timeout: 10 * time.Second} + vid, _, ok := strings.Cut(fid, ",") + if !ok { + t.Fatalf("chunk object fid %q has no volume id", fid) + } + resp, err := c.Get("http://" + env.cluster.master + "/dir/lookup?volumeId=" + vid) + if err != nil { + t.Fatal(err) + } + var lookup struct { + Locations []struct { + URL string `json:"url"` + } `json:"locations"` + } + err = json.NewDecoder(resp.Body).Decode(&lookup) + resp.Body.Close() + if err != nil || len(lookup.Locations) == 0 { + t.Fatalf("weed lookup of volume %s: %v %+v", vid, err, lookup) + } + resp, err = c.Get("http://" + lookup.Locations[0].URL + "/" + fid) + if err != nil { + t.Fatal(err) + } + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + switch resp.StatusCode { + case http.StatusOK: + return true + case http.StatusNotFound: + return false + } + t.Fatalf("weed read of %s: status %d", fid, resp.StatusCode) + return false +} + // TestSnapshotLadderD4DeleteAllButOneThenCollect: three snapshots, each -// overwriting the same chunks; the second and the third are deleted. The -// second's chunks are then referenced by nothing (the third's are still -// HEAD's), and the store holds more than the manifests reference. Two -// collector passes the grace apart reclaim exactly that surplus: the store -// then holds only what the remaining snapshot and HEAD reference, and the -// remaining snapshot and HEAD read as they were. +// overwriting the same four chunks; the third and then the second are +// deleted. The third's chunks are still HEAD's, so only the chunks the +// second's cut uploaded are referenced by nothing; usage says deleting it +// makes chunk bytes eligible. Two collector passes the grace apart reclaim +// the CHUNK DATA of exactly those chunks: a preview before the second pass +// names one eligible object per chunk the second's cut uploaded, each still +// readable from the weed store; the applying pass reclaims those and only +// those, and each is then gone from the store. HEAD still reads the newest +// content and the remaining snapshot restores as it was. +// +// What is measured is chunk data in the weed store, not filer entries, and +// the bound is exact only for objects the collector can enumerate. Excluded, +// known limits of today's format: +// - a reclaimed binding's filer entry is kept for ever (marked reclaimed), +// so filer entry counts and usage's stored bindings and stored bytes do +// not fall after collection and are not compared; +// - a needle uploaded before its binding was written (a retried upload) +// cannot be enumerated and is never reclaimed; a run without upload +// retries makes none, and the test does not look for them. +// +// So after collection the store's enumerable chunk data is at most what HEAD +// and the remaining snapshot reference; unenumerable needles are not bounded. func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { env := startLadderEnv(t, "v2") chunks := []uint64{0, 1, 2, 3} imgs := []map[uint64][]byte{ladderImage("d4one", chunks, 4), ladderImage("d4two", chunks, 4), ladderImage("d4three", chunks, 4)} + uploads := make([]int, len(imgs)) for i, img := range imgs { env.writeImage(t, "s1", img) env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) + uploads[i] = env.lastCutUploads(t, "s1", "v1") + } + if uploads[1] != len(chunks) { + t.Fatalf("the second snapshot's cut uploaded %d chunks; want the %d it overwrote", uploads[1], len(chunks)) } env.deleteSnapshot(t, "v1", "s3") + if b := env.exclusiveBytes(t, "v1", "s2"); b == 0 { + t.Fatal("usage says deleting s2 frees no chunk bytes; its four overwritten chunks are referenced by nothing else") + } else { + t.Logf("deleting s2 makes %d chunk bytes eligible", b) + } env.deleteSnapshot(t, "v1", "s2") env.requireSnapshotNames(t, "v1", "s1") - written := time.Now() + deleted := time.Now() - before := env.usage(t, "v1") - surplus := before.StoredBindings - before.ManifestBindings - t.Logf("before the collector: %+v", before) - if surplus < len(chunks) || !before.StoredUpperBound { - t.Fatalf("after the deletes the store holds %d unreferenced chunks; want at least the %d of s2", surplus, len(chunks)) + time.Sleep(time.Until(deleted.Add(ladderGCWait))) // the chunks are older than the grace + if _, n := env.gc(t, true); n != 0 { + t.Fatalf("the first mark reclaimed %d chunks; a chunk is reclaimed only by a second mark", n) } - - time.Sleep(time.Until(written.Add(ladderGCWait))) // the chunks are older than the grace - reclaimed := env.gc(t) first := time.Now() time.Sleep(time.Until(first.Add(ladderGCWait))) // a second mark the grace after the first - reclaimed += env.gc(t) - after := env.usage(t, "v1") - t.Logf("after the collector: %+v", after) - if after.StoredBindings != after.ManifestBindings || after.StoredUpperBound || reclaimed != surplus { - t.Fatalf("after the collector the store holds %d chunks, the manifests reference %d (upper bound %v); reclaimed %d of %d", - after.StoredBindings, after.ManifestBindings, after.StoredUpperBound, reclaimed, surplus) + eligible, _ := env.gc(t, false) + if len(eligible) != uploads[1] { + t.Fatalf("%d chunk objects eligible: %+v; want the %d the second snapshot's cut uploaded", len(eligible), eligible, uploads[1]) } - if after.ManifestBindings != before.ManifestBindings { - t.Fatalf("the collector changed what the manifests reference: %d, was %d", after.ManifestBindings, before.ManifestBindings) + want := map[string]bool{} + for _, o := range eligible { + if o.Volume != "v1" || !env.blobStored(t, o.FID) { + t.Fatalf("eligible object %+v is not v1 chunk data in the store", o) + } + want[o.FID] = true + } + reclaimedObjs, n := env.gc(t, true) + if n != len(want) || len(reclaimedObjs) != len(want) { + t.Fatalf("the second mark reclaimed %d of %d eligible: %+v", n, len(want), reclaimedObjs) + } + for _, o := range reclaimedObjs { + if !want[o.FID] { + t.Fatalf("the second mark reclaimed %+v, which the preview did not name", o) + } + if env.blobStored(t, o.FID) { + t.Fatalf("reclaimed chunk %+v is still in the store", o) + } } env.requireImage(t, "s1", imgs[2]) env.restoreChecked(t, "s1", "v2", imgs[0]) From 8fb56ea977a1d6bec1e3b0e74c0e4744fb061f17 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:14:09 -0800 Subject: [PATCH 3/5] test: snapshot ladder D folds: later writes read zero, D3 live HEAD differs, D4 expected set from the filer, budget preflight Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_d_integration_test.go | 170 ++++++++++++++---- 1 file changed, 131 insertions(+), 39 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go index d79fd6b58c..8e45fc8f1b 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -7,6 +7,8 @@ import ( "fmt" "io" "net/http" + "net/url" + "path" "strings" "testing" "time" @@ -57,13 +59,14 @@ func (env *handoffEnv) requireSnapshotNames(t *testing.T, vol string, names ...s } } -// restoreChecked restores snap of v1 into target to independence and checks -// it equals img. -func (env *handoffEnv) restoreChecked(t *testing.T, snap, target string, img map[uint64][]byte) { +// restoreChecked restores snap of v1 into target until its status says +// independent and checks it equals img, with every block of later that img +// does not hold reading zero. +func (env *handoffEnv) restoreChecked(t *testing.T, snap, target string, img map[uint64][]byte, later ...map[uint64][]byte) { t.Helper() env.restoreInto(t, "v1", snap, target) env.waitIndependent(t, target) - env.requireImage(t, "n-"+target, img) + env.requireImage(t, "n-"+target, img, later...) } // TestSnapshotLadderD1DeleteTheNewest: three snapshots over overlapping @@ -79,8 +82,8 @@ func TestSnapshotLadderD1DeleteTheNewest(t *testing.T) { } env.deleteSnapshot(t, "v1", "s3") env.requireSnapshotNames(t, "v1", "s1", "s2") - env.restoreChecked(t, "s1", "v2", img1) - env.restoreChecked(t, "s2", "v3", mergeImages(img1, img2)) + env.restoreChecked(t, "s1", "v2", img1, img2, img3) + env.restoreChecked(t, "s2", "v3", mergeImages(img1, img2), img3) } // TestSnapshotLadderD2DeleteAMiddleOne: four snapshots, each sharing chunks @@ -99,22 +102,25 @@ func TestSnapshotLadderD2DeleteAMiddleOne(t *testing.T) { } env.deleteSnapshot(t, "v1", "s2") env.requireSnapshotNames(t, "v1", "s1", "s3", "s4") - env.restoreChecked(t, "s1", "v2", img1) - env.restoreChecked(t, "s3", "v3", mergeImages(img1, img2, img3)) + env.restoreChecked(t, "s1", "v2", img1, img2, img3, img4) + env.restoreChecked(t, "s3", "v3", mergeImages(img1, img2, img3), img4) env.restoreChecked(t, "s4", "v4", mergeImages(img1, img2, img3, img4)) } // TestSnapshotLadderD3DeleteWhenContentRecurs: the A, B, A volume of A4 -// (one block holds A, then B, then A again, a snapshot after each); the -// first snapshot is deleted; the third, whose content equals the first's, -// and the second restore as they were. +// (one block holds A, then B, then A again, a snapshot after each), then the +// live block is overwritten with C; the first snapshot is deleted; the +// third, whose content equals the first's, and the second restore as they +// were, not as HEAD is now. No collector runs here: this is the logical +// delete and restore, not the reclamation of a recurring chunk. func TestSnapshotLadderD3DeleteWhenContentRecurs(t *testing.T) { env := startLadderEnv(t, "v2", "v3") - a, b := pattern("ladder-d3-blk-A!"), pattern("ladder-d3-blk-B!") + a, b, c := pattern("ladder-d3-blk-A!"), pattern("ladder-d3-blk-B!"), pattern("ladder-d3-blk-C!") for i, content := range [][]byte{a, b, a} { env.writeBlock(t, "s1", 7, content) env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) } + env.writeBlock(t, "s1", 7, c) // HEAD now differs from every snapshot env.deleteSnapshot(t, "v1", "s1") env.requireSnapshotNames(t, "v1", "s2", "s3") env.restoreChecked(t, "s3", "v2", map[uint64][]byte{7: a}) @@ -128,6 +134,73 @@ type gcObject struct { FID string `json:"fid"` } +// chunkKeys lists the names (unescaped) of v1's chunk-key bindings, read +// straight from the filer's directory listing, not through the collector. +func (env *handoffEnv) chunkKeys(t *testing.T, vol string) map[string]bool { + t.Helper() + c := &http.Client{Timeout: 10 * time.Second} + dir := path.Join(env.chunkPath, vol, "keys") + "/" + keys := map[string]bool{} + for last := ""; ; { + req, _ := http.NewRequest("GET", "http://"+env.cluster.filerHTTP+dir+"?limit=1000&lastFileName="+url.QueryEscape(last), nil) + req.Header.Set("Accept", "application/json") + resp, err := c.Do(req) + if err != nil { + t.Fatal(err) + } + var page struct { + Entries []struct { + FullPath string `json:"FullPath"` + } `json:"Entries"` + LastFileName string `json:"LastFileName"` + ShouldDisplayLoadMore bool `json:"ShouldDisplayLoadMore"` + } + err = json.NewDecoder(resp.Body).Decode(&page) + resp.Body.Close() + if err != nil || resp.StatusCode != http.StatusOK { + t.Fatalf("filer listing of %s: status %d %v", dir, resp.StatusCode, err) + } + for _, e := range page.Entries { + name, err := url.PathUnescape(path.Base(e.FullPath)) + if err != nil { + t.Fatalf("chunk key %q: %v", e.FullPath, err) + } + keys[name] = true + } + if !page.ShouldDisplayLoadMore || page.LastFileName == "" || page.LastFileName == last { + return keys + } + last = page.LastFileName + } +} + +// uniqueObjects checks every object is v1's and named once (by key and by +// fid), and returns them by key. +func uniqueObjects(t *testing.T, what string, objs []gcObject) map[string]gcObject { + t.Helper() + byKey, fids := map[string]gcObject{}, map[string]bool{} + for _, o := range objs { + if o.Volume != "v1" || o.Key == "" || o.FID == "" || fids[o.FID] { + t.Fatalf("%s: object %+v is not a distinct v1 chunk object: %+v", what, o, objs) + } + if _, dup := byKey[o.Key]; dup { + t.Fatalf("%s: key %q named twice: %+v", what, o.Key, objs) + } + byKey[o.Key], fids[o.FID] = o, true + } + return byKey +} + +// requireBudget fails the test before it starts anything when the test +// binary's deadline leaves less than need: a wait cut short by the package +// timeout would skip the harness's cleanup. +func requireBudget(t *testing.T, need time.Duration) { + t.Helper() + if dl, ok := t.Deadline(); ok && time.Until(dl) < need { + t.Fatalf("this test needs %v; the test deadline leaves %v (run with -timeout 45m)", need, time.Until(dl).Round(time.Second)) + } +} + // gc runs one collector pass (applying or a preview) and returns the chunk // objects it found eligible and how many it reclaimed. func (env *handoffEnv) gc(t *testing.T, apply bool) ([]gcObject, int) { @@ -218,36 +291,50 @@ func (env *handoffEnv) blobStored(t *testing.T, fid string) bool { // TestSnapshotLadderD4DeleteAllButOneThenCollect: three snapshots, each // overwriting the same four chunks; the third and then the second are -// deleted. The third's chunks are still HEAD's, so only the chunks the -// second's cut uploaded are referenced by nothing; usage says deleting it -// makes chunk bytes eligible. Two collector passes the grace apart reclaim -// the CHUNK DATA of exactly those chunks: a preview before the second pass -// names one eligible object per chunk the second's cut uploaded, each still -// readable from the weed store; the applying pass reclaims those and only -// those, and each is then gone from the store. HEAD still reads the newest -// content and the remaining snapshot restores as it was. +// deleted. The third's chunks are still HEAD's, so only the bindings the +// second's cut created are referenced by nothing; usage says deleting it +// makes chunk bytes eligible. The expected set is taken independently of +// the collector: the chunk-key bindings that appear in the filer between the +// first and the second snapshot. Two collector passes the grace apart then +// reclaim the CHUNK DATA of exactly that set: a preview before the second +// pass names exactly those keys, each once, each still readable from the +// weed store; the applying pass reclaims exactly those, and each is then +// gone from the store. HEAD still reads the newest content and the +// remaining snapshot restores as it was. // -// What is measured is chunk data in the weed store, not filer entries, and -// the bound is exact only for objects the collector can enumerate. Excluded, -// known limits of today's format: +// What is measured is chunk data in the weed store for the bindings the +// deleted snapshot alone created, not filer entries and not the whole store. +// Excluded, known limits of today's format: // - a reclaimed binding's filer entry is kept for ever (marked reclaimed), // so filer entry counts and usage's stored bindings and stored bytes do // not fall after collection and are not compared; // - a needle uploaded before its binding was written (a retried upload) // cannot be enumerated and is never reclaimed; a run without upload -// retries makes none, and the test does not look for them. -// -// So after collection the store's enumerable chunk data is at most what HEAD -// and the remaining snapshot reference; unenumerable needles are not bounded. +// retries makes none, and the test does not look for them; +// - disk space is not measured: a deleted needle's space returns only +// when weed compacts its volume. func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { + requireBudget(t, 2*ladderGCWait+10*time.Minute) env := startLadderEnv(t, "v2") chunks := []uint64{0, 1, 2, 3} imgs := []map[uint64][]byte{ladderImage("d4one", chunks, 4), ladderImage("d4two", chunks, 4), ladderImage("d4three", chunks, 4)} uploads := make([]int, len(imgs)) + keysAfter := make([]map[string]bool, len(imgs)) for i, img := range imgs { env.writeImage(t, "s1", img) env.snapshot(t, "s1", "v1", fmt.Sprintf("s%d", i+1)) uploads[i] = env.lastCutUploads(t, "s1", "v1") + keysAfter[i] = env.chunkKeys(t, "v1") + } + expected := map[string]bool{} // the bindings the second snapshot's cut created + for k := range keysAfter[1] { + if !keysAfter[0][k] { + expected[k] = true + } + } + t.Logf("the second snapshot's cut uploaded %d chunks and created %d bindings", uploads[1], len(expected)) + if len(expected) < len(chunks) { + t.Fatalf("the second snapshot's cut created %d bindings; want at least one per overwritten chunk (%d)", len(expected), len(chunks)) } if uploads[1] != len(chunks) { t.Fatalf("the second snapshot's cut uploaded %d chunks; want the %d it overwrote", uploads[1], len(chunks)) @@ -262,30 +349,35 @@ func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { env.requireSnapshotNames(t, "v1", "s1") deleted := time.Now() + requireBudget(t, time.Until(deleted.Add(ladderGCWait))+ladderGCWait+5*time.Minute) time.Sleep(time.Until(deleted.Add(ladderGCWait))) // the chunks are older than the grace if _, n := env.gc(t, true); n != 0 { t.Fatalf("the first mark reclaimed %d chunks; a chunk is reclaimed only by a second mark", n) } first := time.Now() + requireBudget(t, ladderGCWait+5*time.Minute) time.Sleep(time.Until(first.Add(ladderGCWait))) // a second mark the grace after the first eligible, _ := env.gc(t, false) - if len(eligible) != uploads[1] { - t.Fatalf("%d chunk objects eligible: %+v; want the %d the second snapshot's cut uploaded", len(eligible), eligible, uploads[1]) + want := uniqueObjects(t, "preview", eligible) + if len(want) != len(expected) { + t.Fatalf("%d chunk objects eligible: %+v; want exactly the %d bindings the second snapshot's cut created", len(want), eligible, len(expected)) } - want := map[string]bool{} - for _, o := range eligible { - if o.Volume != "v1" || !env.blobStored(t, o.FID) { - t.Fatalf("eligible object %+v is not v1 chunk data in the store", o) + for k, o := range want { + if !expected[k] { + t.Fatalf("eligible object %+v is not one the second snapshot's cut created", o) + } + if !env.blobStored(t, o.FID) { + t.Fatalf("eligible object %+v is not in the store before the applying pass", o) } - want[o.FID] = true } reclaimedObjs, n := env.gc(t, true) - if n != len(want) || len(reclaimedObjs) != len(want) { - t.Fatalf("the second mark reclaimed %d of %d eligible: %+v", n, len(want), reclaimedObjs) + got := uniqueObjects(t, "apply", reclaimedObjs) + if n != len(expected) || len(got) != len(expected) { + t.Fatalf("the second mark reclaimed %d (%d distinct) of the %d expected: %+v", n, len(got), len(expected), reclaimedObjs) } - for _, o := range reclaimedObjs { - if !want[o.FID] { + for k, o := range got { + if w, ok := want[k]; !ok || w.FID != o.FID { t.Fatalf("the second mark reclaimed %+v, which the preview did not name", o) } if env.blobStored(t, o.FID) { @@ -293,5 +385,5 @@ func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { } } env.requireImage(t, "s1", imgs[2]) - env.restoreChecked(t, "s1", "v2", imgs[0]) + env.restoreChecked(t, "s1", "v2", imgs[0], imgs[1], imgs[2]) } From a0227a27f7edab52f8749164f2d39a8886520a86 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 17:23:44 -0800 Subject: [PATCH 4/5] test: snapshot ladder D4 reports its collection as a measurement Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_d_integration_test.go | 106 +++++++++--------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go index 8e45fc8f1b..265cccfc06 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -174,21 +174,20 @@ func (env *handoffEnv) chunkKeys(t *testing.T, vol string) map[string]bool { } } -// uniqueObjects checks every object is v1's and named once (by key and by -// fid), and returns them by key. -func uniqueObjects(t *testing.T, what string, objs []gcObject) map[string]gcObject { - t.Helper() - byKey, fids := map[string]gcObject{}, map[string]bool{} +// objectsByKey indexes collector objects by key and counts the records that +// are not distinct v1 chunk objects (another volume, an empty field, or a +// key or fid named again). +func objectsByKey(objs []gcObject) (map[string]gcObject, int) { + byKey, fids, odd := map[string]gcObject{}, map[string]bool{}, 0 for _, o := range objs { - if o.Volume != "v1" || o.Key == "" || o.FID == "" || fids[o.FID] { - t.Fatalf("%s: object %+v is not a distinct v1 chunk object: %+v", what, o, objs) - } - if _, dup := byKey[o.Key]; dup { - t.Fatalf("%s: key %q named twice: %+v", what, o.Key, objs) + _, dup := byKey[o.Key] + if o.Volume != "v1" || o.Key == "" || o.FID == "" || fids[o.FID] || dup { + odd++ + continue } byKey[o.Key], fids[o.FID] = o, true } - return byKey + return byKey, odd } // requireBudget fails the test before it starts anything when the test @@ -289,30 +288,30 @@ func (env *handoffEnv) blobStored(t *testing.T, fid string) bool { return false } -// TestSnapshotLadderD4DeleteAllButOneThenCollect: three snapshots, each -// overwriting the same four chunks; the third and then the second are -// deleted. The third's chunks are still HEAD's, so only the bindings the -// second's cut created are referenced by nothing; usage says deleting it -// makes chunk bytes eligible. The expected set is taken independently of -// the collector: the chunk-key bindings that appear in the filer between the -// first and the second snapshot. Two collector passes the grace apart then -// reclaim the CHUNK DATA of exactly that set: a preview before the second -// pass names exactly those keys, each once, each still readable from the -// weed store; the applying pass reclaims exactly those, and each is then -// gone from the store. HEAD still reads the newest content and the -// remaining snapshot restores as it was. +// TestSnapshotLadderD4DeleteAllButOneThenCollect is a MEASUREMENT, not a +// pass/fail rung: what the collector reclaims after deletes, against an +// expected set taken independently of it. Three snapshots, each overwriting +// the same four chunks; the third and then the second are deleted. The +// third's chunks are still HEAD's, so the bindings the second's cut created +// (those that appear in the filer between the first and the second +// snapshot) are referenced by nothing. Two collector passes the grace apart +// run; the test reports, in one MEASUREMENT line, how many of the expected +// bindings a preview names and the applying pass reclaims, how many of +// those are gone from the weed store, what else either pass named, and the +// surplus left. It fails only when the harness cannot measure (the setup +// did not create the expected bindings, a command failed) or when data is +// lost: HEAD must still read the newest content and the remaining snapshot +// restore as it was. // // What is measured is chunk data in the weed store for the bindings the // deleted snapshot alone created, not filer entries and not the whole store. -// Excluded, known limits of today's format: +// The known surplus of today's format, not measured: // - a reclaimed binding's filer entry is kept for ever (marked reclaimed), // so filer entry counts and usage's stored bindings and stored bytes do -// not fall after collection and are not compared; +// not fall after collection; // - a needle uploaded before its binding was written (a retried upload) -// cannot be enumerated and is never reclaimed; a run without upload -// retries makes none, and the test does not look for them; -// - disk space is not measured: a deleted needle's space returns only -// when weed compacts its volume. +// cannot be enumerated and is never reclaimed; +// - disk space returns only when weed compacts its volume. func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { requireBudget(t, 2*ladderGCWait+10*time.Minute) env := startLadderEnv(t, "v2") @@ -340,50 +339,49 @@ func TestSnapshotLadderD4DeleteAllButOneThenCollect(t *testing.T) { t.Fatalf("the second snapshot's cut uploaded %d chunks; want the %d it overwrote", uploads[1], len(chunks)) } env.deleteSnapshot(t, "v1", "s3") - if b := env.exclusiveBytes(t, "v1", "s2"); b == 0 { - t.Fatal("usage says deleting s2 frees no chunk bytes; its four overwritten chunks are referenced by nothing else") - } else { - t.Logf("deleting s2 makes %d chunk bytes eligible", b) - } + exclusive := env.exclusiveBytes(t, "v1", "s2") env.deleteSnapshot(t, "v1", "s2") env.requireSnapshotNames(t, "v1", "s1") deleted := time.Now() requireBudget(t, time.Until(deleted.Add(ladderGCWait))+ladderGCWait+5*time.Minute) time.Sleep(time.Until(deleted.Add(ladderGCWait))) // the chunks are older than the grace - if _, n := env.gc(t, true); n != 0 { - t.Fatalf("the first mark reclaimed %d chunks; a chunk is reclaimed only by a second mark", n) - } + _, firstReclaimed := env.gc(t, true) first := time.Now() requireBudget(t, ladderGCWait+5*time.Minute) time.Sleep(time.Until(first.Add(ladderGCWait))) // a second mark the grace after the first eligible, _ := env.gc(t, false) - want := uniqueObjects(t, "preview", eligible) - if len(want) != len(expected) { - t.Fatalf("%d chunk objects eligible: %+v; want exactly the %d bindings the second snapshot's cut created", len(want), eligible, len(expected)) - } - for k, o := range want { + named, oddPreview := objectsByKey(eligible) + namedExpected, namedOther, storedBefore := 0, 0, 0 + for k, o := range named { if !expected[k] { - t.Fatalf("eligible object %+v is not one the second snapshot's cut created", o) + namedOther++ + continue } - if !env.blobStored(t, o.FID) { - t.Fatalf("eligible object %+v is not in the store before the applying pass", o) + namedExpected++ + if env.blobStored(t, o.FID) { + storedBefore++ } } reclaimedObjs, n := env.gc(t, true) - got := uniqueObjects(t, "apply", reclaimedObjs) - if n != len(expected) || len(got) != len(expected) { - t.Fatalf("the second mark reclaimed %d (%d distinct) of the %d expected: %+v", n, len(got), len(expected), reclaimedObjs) - } - for k, o := range got { - if w, ok := want[k]; !ok || w.FID != o.FID { - t.Fatalf("the second mark reclaimed %+v, which the preview did not name", o) + reclaimed, oddApply := objectsByKey(reclaimedObjs) + reclaimedExpected, reclaimedOther, goneAfter := 0, 0, 0 + for k, o := range reclaimed { + if !expected[k] { + reclaimedOther++ + continue } - if env.blobStored(t, o.FID) { - t.Fatalf("reclaimed chunk %+v is still in the store", o) + reclaimedExpected++ + if !env.blobStored(t, o.FID) { + goneAfter++ } } + t.Logf("D4 MEASUREMENT: expected %d bindings (s2's cut uploaded %d chunks; usage: deleting s2 frees %d bytes); "+ + "first mark reclaimed %d; preview named %d of them (%d still in the store), %d others, %d malformed; "+ + "apply reclaimed %d (%d of them, %d now gone from the store), %d others, %d malformed; surplus left: %d of the expected", + len(expected), uploads[1], exclusive, firstReclaimed, namedExpected, storedBefore, namedOther, oddPreview, + n, reclaimedExpected, goneAfter, reclaimedOther, oddApply, len(expected)-goneAfter) env.requireImage(t, "s1", imgs[2]) env.restoreChecked(t, "s1", "v2", imgs[0], imgs[1], imgs[2]) } From 53563342eaa534f8cd4e0d2da182a8bf3ae18484 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 21:33:24 -0800 Subject: [PATCH 5/5] test: snapshot ladder D1-D3 comments claim the logical delete only Co-Authored-By: Claude Opus 5.5 --- .../snapshot_ladder_d_integration_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go index 265cccfc06..f746773426 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -14,10 +14,12 @@ import ( "time" ) -// Rung D of the snapshot ladder: deletes. A deleted snapshot leaves every -// other snapshot restorable, whichever one is deleted (the newest, a middle -// one, one whose content recurs later), and the collector then reclaims the -// chunk data only the deleted snapshots referenced. +// Rung D of the snapshot ladder: deletes. D1-D3 are the logical delete: a +// deleted snapshot leaves every other snapshot restorable, whichever one is +// deleted (the newest, a middle one, one whose content recurs later). No +// collector runs in them (a delete only marks the snapshot and frees its +// name), so they prove nothing about reclamation. D4 runs the collector and +// measures what it reclaims. // // The collector's grace may not be shorter than the operation deadline plus // the clock skew plus the CAS apply bound (11 minutes with the defaults), and @@ -88,8 +90,9 @@ func TestSnapshotLadderD1DeleteTheNewest(t *testing.T) { // TestSnapshotLadderD2DeleteAMiddleOne: four snapshots, each sharing chunks // with its neighbours; the second is deleted; the first, the third and the -// fourth restore as they were (the deleted one's chunks that a neighbour or a -// later snapshot still uses are kept). +// fourth restore as they were. No collector runs: whether the deleted one's +// chunks that a neighbour still uses would survive collection is not tested +// here. func TestSnapshotLadderD2DeleteAMiddleOne(t *testing.T) { env := startLadderEnv(t, "v2", "v3", "v4") img1 := ladderImage("d2one", []uint64{0, 2}, 4)