diff --git a/seaweed-block/cmd/blockvolume/handoff_cli_output_test.go b/seaweed-block/cmd/blockvolume/handoff_cli_output_test.go new file mode 100644 index 0000000000..895d5705b2 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/handoff_cli_output_test.go @@ -0,0 +1,76 @@ +package main_test + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" +) + +// handoffHeadJSON adapts the CLI's typed show envelope to the canonical HEAD +// bytes the process tests inspect. A public volume name is not its directory. +func handoffHeadJSON(out string) (string, error) { + var result struct { + Head *layout.Head `json:"head"` + } + if err := json.Unmarshal([]byte(out), &result); err != nil { + return "", err + } + if result.Head == nil { + return "", fmt.Errorf("show returned no HEAD") + } + raw, err := result.Head.Encode() + return string(raw), err +} + +func handoffDeleteCompleted(out string) bool { + var result struct { + Volume string `json:"volume"` + State string `json:"state"` + } + return json.Unmarshal([]byte(out), &result) == nil && result.Volume != "" && result.State == layout.StateDeleted +} + +func TestHandoffShowUsesTheNestedLayoutHead(t *testing.T) { + for _, state := range []string{layout.StateLive, layout.StateDeleted} { + t.Run(state, func(t *testing.T) { + head := layout.Head{VolumeID: layout.VolumeID{1}, State: state, Activity: layout.Activity{Kind: layout.ActIdle}} + // This is chunkShow's public JSON shape, not HEAD's on-filer codec. + raw, err := json.Marshal(struct { + Volume any `json:"volume"` + Head *layout.Head `json:"head"` + }{Head: &head}) + if err != nil { + t.Fatal(err) + } + got, err := handoffHeadJSON(string(raw)) + if err != nil || !strings.Contains(got, `"epoch":0,"holder":""`) || (state == layout.StateDeleted && !strings.Contains(got, `"state":"deleted"`)) { + t.Fatalf("show adapter: %s, %v", got, err) + } + }) + } + for _, raw := range []string{`{}`, `{"head":null}`, `{"head":{"State":"deleted"}}`, `not JSON`} { + if got, err := handoffHeadJSON(raw); err == nil { + t.Errorf("invalid/absent HEAD accepted: %s -> %s", raw, got) + } + } +} + +func TestHandoffDeleteUsesTheCLIState(t *testing.T) { + for _, row := range []struct { + raw string + want bool + }{ + {`{"volume":"v2-34071d5f","state":"deleted"}`, true}, + {`{"volume":"v2-34071d5f","state":"deleting"}`, false}, + {`{"completed":true}`, false}, + {`{"state":"deleted"}`, false}, + {`not JSON`, false}, + } { + if got := handoffDeleteCompleted(row.raw); got != row.want { + t.Errorf("delete completion for %s: %v, want %v", row.raw, got, row.want) + } + } +} diff --git a/seaweed-block/cmd/blockvolume/headroom_restore_fork_integration_test.go b/seaweed-block/cmd/blockvolume/headroom_restore_fork_integration_test.go index 865a291353..39cac9a21e 100644 --- a/seaweed-block/cmd/blockvolume/headroom_restore_fork_integration_test.go +++ b/seaweed-block/cmd/blockvolume/headroom_restore_fork_integration_test.go @@ -152,8 +152,7 @@ func (env *handoffEnv) deleteServingTarget(t *testing.T, vol string, b bvHost, d }) out, errOut, code := env.h.run(t, "ops", "chunk", "volume-delete", "--volume", vol, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--master", env.h.masterAddr, "--request-id", "del-"+vol) - var del struct{ Completed bool } - if code != 0 || json.Unmarshal([]byte(out), &del) != nil || !del.Completed { + if code != 0 || !handoffDeleteCompleted(out) { t.Fatalf("volume-delete of %s after the way out: exit %d %s %s; want completed", vol, code, out, errOut) } } @@ -223,7 +222,7 @@ func TestHeadroomRefusesRestoreTargetsThenServesThem(t *testing.T) { env.requireImage(t, "n-v5", img) env.requireImage(t, "s1", img) // the source is intact - if l := env.snapshots(t, "v1", "base"); len(l) != 1 || l[0].State != "active" { + if l := env.snapshots(t, "v1", "base"); len(l) != 1 { t.Fatalf("the source's snapshot after the refusals: %+v", l) } diff --git a/seaweed-block/cmd/blockvolume/master_crash_integration_test.go b/seaweed-block/cmd/blockvolume/master_crash_integration_test.go index 4ed789162f..e38f77ea25 100644 --- a/seaweed-block/cmd/blockvolume/master_crash_integration_test.go +++ b/seaweed-block/cmd/blockvolume/master_crash_integration_test.go @@ -45,7 +45,7 @@ func TestProcedureMasterKilledAfterMoveBeforeTargetStarts(t *testing.T) { } } out, _ = env.h.command(t, env.chunkShow()...) - if got := mustU(sealedHeadLSN(out, e, g)); got != l { + if got := mustU(sealedLayoutHeadPosition(out, e, g)); got != l { t.Fatalf("sealed HEAD LSN %d; want %d", got, l) } nw := env.start(t, "blockvolume-s2", "s2") diff --git a/seaweed-block/cmd/blockvolume/neverstarted_delete_integration_test.go b/seaweed-block/cmd/blockvolume/neverstarted_delete_integration_test.go index 3db9b56a5d..b3cf20b4c1 100644 --- a/seaweed-block/cmd/blockvolume/neverstarted_delete_integration_test.go +++ b/seaweed-block/cmd/blockvolume/neverstarted_delete_integration_test.go @@ -4,7 +4,6 @@ package main_test import ( "context" - "encoding/json" "fmt" "os" "path/filepath" @@ -34,11 +33,10 @@ func TestNeverAttachedRestoreTargetIsDeletable(t *testing.T) { out, errOut, code := env.h.run(t, "ops", "chunk", "volume-delete", "--volume", "v2", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--master", env.h.masterAddr, "--request-id", "del-v2-never-started") - var del struct{ Completed bool } - if code != 0 || json.Unmarshal([]byte(out), &del) != nil || !del.Completed { + if code != 0 || !handoffDeleteCompleted(out) { t.Fatalf("volume-delete of a never-attached target must complete; exit %d:\n%s\n%s", code, out, errOut) } - shown, _, _ := env.h.run(t, "ops", "chunk", "show", "--master", env.h.masterAddr, "--volume", "v2", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + shown := env.headOf(t, "v2") if !strings.Contains(shown, `"state":"deleted"`) { t.Fatalf("the deleted target's HEAD is not a deleted tombstone:\n%s", shown) } @@ -73,7 +71,7 @@ func TestNeverStartedDeleteFencesALateStart(t *testing.T) { late := env.h.start(t, "blockvolume-v2-late", env.h.volume, env.volArgsFor(b)...) err := late.wait(60 * time.Second) served := late.has("durable: HEAD claim for volume=v2 done") || volumeFrontendReady(b.status, "v2") - shown, _, _ := env.h.run(t, "ops", "chunk", "show", "--master", env.h.masterAddr, "--volume", "v2", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + shown := env.headOf(t, "v2") t.Logf("late start after the delete: exit %v, served %v, HEAD %s", err, served, firstLine(shown)) if served || err == nil || strings.Contains(fmt.Sprint(err), "still running") || !strings.Contains(shown, `"state":"deleted"`) { t.Fatalf("1.0.1: a start after the delete must exit without claiming or serving and leave the volume deleted; exit %v, served %v, HEAD:\n%s", err, served, shown) @@ -118,7 +116,7 @@ func TestNeverStartedDeleteRacesAStart(t *testing.T) { // claim) or its frontend became ready; adopting the assignment comes // before the claim and is not serving. served := d.has("durable: HEAD claim for volume="+v+" done") || volumeFrontendReady(b.status, v) - shown, _, _ := env.h.run(t, "ops", "chunk", "show", "--master", env.h.masterAddr, "--volume", v, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + shown := env.headOf(t, v) isDeleted := strings.Contains(shown, `"state":"deleted"`) t.Logf("%s: delete exit %d, served %v, HEAD deleted %v: %s", v, r.code, served, isDeleted, firstLine(r.out)) switch { @@ -253,8 +251,7 @@ func TestNeverStartedLineIsDeletableWithoutARevoke(t *testing.T) { t.Helper() before := env.workspaceShow(t, v) out, code := env.deleteNoRevoke(context.Background(), v, request) - var del struct{ Completed bool } - completed := code == 0 && json.Unmarshal([]byte(firstLine(out)), &del) == nil && del.Completed + completed := code == 0 && handoffDeleteCompleted(out) head, after := env.headOf(t, v), env.workspaceShow(t, v) t.Logf("delete exit %d, completed %v, HEAD %s\nworkspace show before:\n%s\nafter:\n%s\ndelete output:\n%s", code, completed, head, before, after, out) if !completed { diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go index 938d736167..59cdaafe6e 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go @@ -138,12 +138,16 @@ func sealedLayoutHeadPosition(out string, epoch, gen uint64) (uint64, error) { } func checkLayoutHeadReopened(out string, epoch, sealedGen, position uint64) error { + return checkLayoutHeadReopenedBy(out, epoch, sealedGen, position, "s1") +} + +func checkLayoutHeadReopenedBy(out string, epoch, sealedGen, position uint64, host string) error { h, err := parseLayoutHead(out) if err != nil { return err } - if h.Gen <= sealedGen || h.Epoch != epoch || h.Holder != "s1/r1" || h.ClaimID == "" || h.State != "" || h.Position != position || len(h.Current) == 0 || string(h.Current) == "null" { - return fmt.Errorf("layout HEAD after claim %+v; want live image above gen %d at epoch %d position %d holder s1/r1", h, sealedGen, epoch, position) + if h.Gen <= sealedGen || h.Epoch != epoch || h.Holder != host+"/r1" || h.ClaimID == "" || h.State != "" || h.Position != position || len(h.Current) == 0 || string(h.Current) == "null" { + return fmt.Errorf("layout HEAD after claim %+v; want live image above gen %d at epoch %d position %d holder %s/r1", h, sealedGen, epoch, position, host) } return nil } diff --git a/seaweed-block/cmd/blockvolume/procedure_crash_io_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_crash_io_integration_test.go index 1d7e7eb38f..059ea0146d 100644 --- a/seaweed-block/cmd/blockvolume/procedure_crash_io_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_crash_io_integration_test.go @@ -386,7 +386,7 @@ func TestProcedureCrashDuringACut(t *testing.T) { verify := []string{"ops", "chunk", "verify", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"} headGen := func() uint64 { out, _ := env.h.command(t, env.chunkShow()...) - h, err := parseHead(out) + h, err := parseLayoutHead(out) if err != nil { t.Fatal(err) } diff --git a/seaweed-block/cmd/blockvolume/procedure_diskfull_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_diskfull_integration_test.go index 46e465b9ca..f901473a9e 100644 --- a/seaweed-block/cmd/blockvolume/procedure_diskfull_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_diskfull_integration_test.go @@ -76,7 +76,7 @@ func TestProcedureLocalDiskFull(t *testing.T) { verify := []string{"ops", "chunk", "verify", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"} headGen := func() uint64 { out, _ := env.h.command(t, env.chunkShow()...) - hd, err := parseHead(out) + hd, err := parseLayoutHead(out) if err != nil { t.Fatal(err) } diff --git a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go index 668e914ad0..6bca32185b 100644 --- a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go @@ -58,7 +58,7 @@ func TestProcedurePlannedHandoffToAnotherHost(t *testing.T) { must(wantLines(out, fmt.Sprintf("holder: r1@%d (logical replica)", e2), fmt.Sprintf("move: request=move-v1-1 kind=clean issued=true s1/r1@%d -> s2/r1@%d HEAD generation %d remote LSN %d", e, e2, g, l))) out, _ = h.command(t, env.chunkShow()...) - must(checkHeadReopenedBy(out, e2, g, l, "s2")) + must(checkLayoutHeadReopenedBy(out, e2, g, l, "s2")) if !bytes.Equal(env.readBlock(t, "s2", 0), block) { t.Fatal("the block written on s1 does not read back on s2") } @@ -387,12 +387,12 @@ func (env *handoffEnv) servingNow(id string) bool { func (env *handoffEnv) requireHeadHolder(t *testing.T, e uint64, host string) { t.Helper() out, _ := env.h.command(t, env.chunkShow()...) - head, err := parseHead(out) + head, err := parseLayoutHead(out) if err != nil { t.Fatal(err) } - if head.State != "active" || head.Epoch != e || head.Holder != host+"/r1" { - t.Fatalf("HEAD %+v; want active at epoch %d held by %s/r1", head, e, host) + if head.State != "" || head.ClaimID == "" || head.Epoch != e || head.Holder != host+"/r1" { + t.Fatalf("HEAD %+v; want live at epoch %d held by %s/r1", head, e, host) } } @@ -464,7 +464,7 @@ func (env *handoffEnv) sealedLSN(t *testing.T, e, g uint64) uint64 { t.Helper() _, mustU := fatalOn(t) out, _ := env.h.command(t, env.chunkShow()...) - return mustU(sealedHeadLSN(out, e, g)) + return mustU(sealedLayoutHeadPosition(out, e, g)) } func (env *handoffEnv) revoke(t *testing.T, e, g uint64) { diff --git a/seaweed-block/cmd/blockvolume/procedure_layout_head_checks_test.go b/seaweed-block/cmd/blockvolume/procedure_layout_head_checks_test.go new file mode 100644 index 0000000000..abb8110b1b --- /dev/null +++ b/seaweed-block/cmd/blockvolume/procedure_layout_head_checks_test.go @@ -0,0 +1,61 @@ +package main_test + +import ( + "encoding/json" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout" +) + +// chunk show marshals layout.Head, not the on-store headDoc or the retired +// storage HEAD. Keep the procedure checks tied to that actual output type. +func TestProcedureLayoutHeadChecksReadProductShape(t *testing.T) { + show := func(h layout.Head) string { + t.Helper() + b, err := json.Marshal(struct { + Head layout.Head `json:"head"` + }{h}) + if err != nil { + t.Fatal(err) + } + return string(b) + } + sealed := layout.Head{Gen: 7, Epoch: 3, Holder: "s1/r1", ClaimID: "claim", + Position: 91, State: layout.StateDetached, Current: &layout.RootName{Hash: layout.Hash{1}}} + if got, err := sealedLayoutHeadPosition(show(sealed), 3, 7); err != nil || got != 91 { + t.Fatalf("detached layout image: position=%d err=%v", got, err) + } + live := sealed + live.Gen, live.Epoch, live.Holder, live.ClaimID, live.State = 8, 4, "s2/r1", "next-claim", layout.StateLive + if err := checkLayoutHeadReopenedBy(show(live), 4, 7, 91, "s2"); err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + mutate func(*layout.Head) + }{ + {"generation", func(h *layout.Head) { h.Gen = 6 }}, + {"epoch", func(h *layout.Head) { h.Epoch++ }}, + {"holder", func(h *layout.Head) { h.Holder = "foreign/r1" }}, + {"claim", func(h *layout.Head) { h.ClaimID = "" }}, + {"image", func(h *layout.Head) { h.Current = nil }}, + {"state", func(h *layout.Head) { h.State = layout.StateDeleting }}, + } { + t.Run(tc.name, func(t *testing.T) { + h := sealed + tc.mutate(&h) + if _, err := sealedLayoutHeadPosition(show(h), 3, 7); err == nil { + t.Fatal("invalid detached proof accepted") + } + h = live + tc.mutate(&h) + if err := checkLayoutHeadReopenedBy(show(h), 4, 7, 91, "s2"); err == nil { + t.Fatal("invalid successor accepted") + } + }) + } + live.Position++ + if err := checkLayoutHeadReopenedBy(show(live), 4, 7, 91, "s2"); err == nil { + t.Fatal("changed committed position accepted") + } +} diff --git a/seaweed-block/cmd/blockvolume/procedure_plain_detach_crash_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_plain_detach_crash_integration_test.go index d4d681de15..85041eaa8a 100644 --- a/seaweed-block/cmd/blockvolume/procedure_plain_detach_crash_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_plain_detach_crash_integration_test.go @@ -26,8 +26,8 @@ func TestProcedureKillDuringPlainDetachThenCompleteHandoff(t *testing.T) { env.writeCutCrashImage(t, 'A') env.h.command(t, "ops", "chunk", "cut", "--volume", "v1", "--status-addr", env.hosts["s1"].status) out, _ := env.h.command(t, env.chunkShow()...) - before, err := parseHead(out) - if err != nil || before.State != "active" || before.Epoch != e { + before, err := parseLayoutHead(out) + if err != nil || before.State != "" || before.ClaimID == "" || before.Epoch != e { t.Fatalf("baseline HEAD: %+v: %v", before, err) } env.writeCutCrashImage(t, 'B') @@ -55,11 +55,11 @@ func TestProcedureKillDuringPlainDetachThenCompleteHandoff(t *testing.T) { t.Fatal(err) } out, _ = env.h.command(t, env.chunkShow()...) - atKill, err := parseHead(out) - if err != nil || atKill.Gen != before.Gen || atKill.State != "active" || atKill.RemoteLSN != before.RemoteLSN { + atKill, err := parseLayoutHead(out) + if err != nil || atKill.Gen != before.Gen || atKill.State != "" || atKill.ClaimID != before.ClaimID || atKill.Position != before.Position { t.Fatalf("missed mid-upload window: before=%+v stopped=%+v: %v", before, atKill, err) } - t.Logf("kill witness: immutable keys %d -> %d, HEAD gen=%d LSN=%d remains active", keys, plainDetachStoredKeys(t, env), atKill.Gen, atKill.RemoteLSN) + t.Logf("kill witness: immutable keys %d -> %d, HEAD gen=%d LSN=%d remains active", keys, plainDetachStoredKeys(t, env), atKill.Gen, atKill.Position) select { case <-cli.done: t.Fatal("detach completed or aborted before the verified kill window") @@ -91,8 +91,8 @@ func TestProcedureKillDuringPlainDetachThenCompleteHandoff(t *testing.T) { env.requireCutCrashImage(t, "s1", 'B') g := env.seal(t, "s1", e) l := env.sealedLSN(t, e, g) - if l <= before.RemoteLSN { - t.Fatalf("retry did not publish the acknowledged image: remote LSN %d <= baseline %d", l, before.RemoteLSN) + if l <= before.Position { + t.Fatalf("retry did not publish the acknowledged image: remote LSN %d <= baseline %d", l, before.Position) } if again := env.seal(t, "s1", e); again != g { t.Fatalf("plain seal replay changed generation %d -> %d", g, again) diff --git a/seaweed-block/cmd/blockvolume/procedure_powerloss_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_powerloss_integration_test.go index 622d96c4a0..f31b5b2479 100644 --- a/seaweed-block/cmd/blockvolume/procedure_powerloss_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_powerloss_integration_test.go @@ -52,7 +52,7 @@ func TestPowerLossReplayChunkVolume(t *testing.T) { verify := []string{"ops", "chunk", "verify", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"} headGen := func() uint64 { out, _ := env.h.command(t, env.chunkShow()...) - hd, err := parseHead(out) + hd, err := parseLayoutHead(out) if err != nil { t.Fatal(err) } diff --git a/seaweed-block/cmd/blockvolume/procedure_store_down_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_store_down_integration_test.go index ffbde08a48..8c864c6c48 100644 --- a/seaweed-block/cmd/blockvolume/procedure_store_down_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_store_down_integration_test.go @@ -69,7 +69,7 @@ func storeDownDuringACut(t *testing.T, down string, cowBytes int64, cowWindow bo verify := []string{"ops", "chunk", "verify", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, "--depth", "data", "--timeout", "5m"} headGen := func() uint64 { out, _ := env.h.command(t, env.chunkShow()...) - hd, err := parseHead(out) + hd, err := parseLayoutHead(out) if err != nil { t.Fatal(err) } 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 0f5919c59c..9b6ed5acd0 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_d_integration_test.go @@ -75,10 +75,10 @@ func (env *handoffEnv) requireSnapshotNames(t *testing.T, vol string, names ...s 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" + ok = list[i].Name == names[i] } if !ok { - t.Fatalf("snapshots of %s: %+v; want exactly %v, active", vol, list, names) + t.Fatalf("snapshots of %s: %+v; want exactly %v", vol, list, names) } } diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_e_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_e_integration_test.go index cc69782ff1..422bc8d740 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_e_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_e_integration_test.go @@ -207,17 +207,9 @@ func TestSnapshotLadderE1CrashDuringASnapshot(t *testing.T) { switch { case len(pre) > 1: t.Fatalf("round %d: %s listed %d times: %+v", k, name, len(pre), pre) - case len(pre) == 1 && pre[0].State != "active": - t.Fatalf("round %d: %s listed but not active: %+v", k, name, pre[0]) case answered && len(pre) == 0: t.Fatalf("round %d: %s answered success before the kill but is not listed: %s", k, name, r.out) } - if answered { - var a snapshotInfo - if err := json.Unmarshal([]byte(r.out), &a); err != nil || a.CutLSN != pre[0].CutLSN || a.HeadGen != pre[0].HeadGen { - t.Fatalf("round %d: %s answered %s before the kill but is listed as %+v", k, name, r.out, pre[0]) - } - } // 16 bytes: pattern repeats it to exactly one block. post := map[uint64][]byte{uint64(50+k) * ladderChunkBlocks: pattern(fmt.Sprintf("e1-post-%02d-blk!!", k))} env.writeImage(t, "s1", post) @@ -228,12 +220,13 @@ func TestSnapshotLadderE1CrashDuringASnapshot(t *testing.T) { if err := json.Unmarshal([]byte(out), &info); err != nil || info.Name != name { t.Fatalf("round %d: retry of %s: %v %s", k, name, err, out) } - if len(pre) == 1 && (info.CutLSN != pre[0].CutLSN || info.HeadGen != pre[0].HeadGen) { - t.Fatalf("round %d: %s was listed as %+v before the retry, which answered %+v: a listed snapshot was taken again", k, name, pre[0], info) - } - if l := env.snapshots(t, "v1", name); len(l) != 1 || l[0].State != "active" || l[0].CutLSN != info.CutLSN { + l := env.snapshots(t, "v1", name) + if len(l) != 1 || l[0].Root == "" { t.Fatalf("round %d: after the retry %s is listed as %+v; its retry answered %+v", k, name, l, info) } + if len(pre) == 1 && l[0].Root != pre[0].Root { + t.Fatalf("round %d: %s was listed as %+v before the retry and as %+v after it: a listed snapshot was taken again", k, name, pre[0], l[0]) + } if len(pre) == 1 { images = append(images, cum) // a replay: the post-restart block is not in it } else { diff --git a/seaweed-block/cmd/blockvolume/snapshot_ladder_integration_test.go b/seaweed-block/cmd/blockvolume/snapshot_ladder_integration_test.go index 66ae8b50cf..490efbe452 100644 --- a/seaweed-block/cmd/blockvolume/snapshot_ladder_integration_test.go +++ b/seaweed-block/cmd/blockvolume/snapshot_ladder_integration_test.go @@ -205,9 +205,17 @@ func (env *handoffEnv) snapshot(t *testing.T, hostID, vol, name string) snapshot return info } +// snapshotEntry is one entry of `sw-block ops chunk snapshots`: a published +// snapshot, its root and the restore targets that depend on it. +type snapshotEntry struct { + Name string `json:"name"` + Root string `json:"root"` + Dependents []string `json:"dependents"` +} + // listSnapshots lists vol's snapshots (with a name prefix when given) under // ctx, without failing the test. -func (env *handoffEnv) listSnapshots(ctx context.Context, vol, prefix string) ([]snapshotInfo, error) { +func (env *handoffEnv) listSnapshots(ctx context.Context, vol, prefix string) ([]snapshotEntry, error) { args := []string{"ops", "chunk", "snapshots", "--master", env.h.masterAddr, "--volume", vol, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath} if prefix != "" { args = append(args, "--prefix", prefix) @@ -216,30 +224,26 @@ func (env *handoffEnv) listSnapshots(ctx context.Context, vol, prefix string) ([ if err != nil { return nil, err } - var body struct { - Snapshots []snapshotInfo `json:"snapshots"` - } - if code != 0 || json.Unmarshal([]byte(out), &body) != nil { + var list []snapshotEntry + if code != 0 || json.Unmarshal([]byte(out), &list) != nil { return nil, fmt.Errorf("snapshots of %s: exit %d: %s %s", vol, code, out, errOut) } - return body.Snapshots, nil + return list, nil } // snapshots lists vol's snapshots (with a name prefix when given). -func (env *handoffEnv) snapshots(t *testing.T, vol, prefix string) []snapshotInfo { +func (env *handoffEnv) snapshots(t *testing.T, vol, prefix string) []snapshotEntry { t.Helper() args := []string{"ops", "chunk", "snapshots", "--master", env.h.masterAddr, "--volume", vol, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath} if prefix != "" { args = append(args, "--prefix", prefix) } out, _ := env.h.command(t, args...) - var body struct { - Snapshots []snapshotInfo `json:"snapshots"` - } - if err := json.Unmarshal([]byte(out), &body); err != nil { + var list []snapshotEntry + if err := json.Unmarshal([]byte(out), &list); err != nil { t.Fatalf("snapshots of %s: %v\n%s", vol, err, out) } - return body.Snapshots + return list } // restoreAttached creates target from snapshot snap of src and starts its @@ -258,15 +262,15 @@ func (env *handoffEnv) restoreAttached(t *testing.T, src, snap, target string) { } // TestSnapshotLadderB1SnapshotQuery: three snapshots with writes between -// them. The list is exactly the committed snapshots, in order, each active, -// with the point (cut LSN), HEAD generation and size (chunk count) its -// creation answered; a prefix shows one. A snapshot being taken appears in -// the list only committed, with the fields its creation answers, and stays; -// the test fails as inconclusive unless at least one listing ran while the -// snapshot was in flight and did not show it. A snapshot request that is refused -// (a restore volume not yet independent) leaves no name in the list. +// them. The list is exactly the published snapshots, in name order, each +// with its own root; a prefix shows one. A snapshot being taken appears in +// the list only once published, and then stays with the same root; the test +// fails as inconclusive unless at least one listing ran while the snapshot +// was in flight and did not show it. A snapshot of a restore target that is +// not fully local is taken, and a restore from it is refused: it names the +// source's chunks. func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { - env := startLadderEnv(t, "v2") + env := startLadderEnv(t, "v2", "v3") var made []snapshotInfo for i, w := range []struct { lba uint64 @@ -277,23 +281,17 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { } list := env.snapshots(t, "v1", "") if len(list) != len(made) { - t.Fatalf("list has %d snapshots; want exactly the %d committed: %+v", len(list), len(made), list) + t.Fatalf("list has %d snapshots; want exactly the %d published: %+v", len(list), len(made), list) } - wantChunks := []int{1, 2, 2} + roots := map[string]bool{} for i, got := range list { - m := made[i] - if got.Name != m.Name || got.State != "active" || got.CutLSN != m.CutLSN || got.HeadGen != m.HeadGen || got.ChunkCount != m.ChunkCount { - t.Fatalf("list entry %d %+v; want %+v, active", i, got, m) - } - if got.ChunkCount != wantChunks[i] { - t.Fatalf("%s holds %d chunks; want %d", got.Name, got.ChunkCount, wantChunks[i]) - } - if i > 0 && (got.CutLSN <= list[i-1].CutLSN || got.HeadGen <= list[i-1].HeadGen) { - t.Fatalf("list not in order of points: %+v after %+v", got, list[i-1]) + if got.Name != made[i].Name || got.Root == "" || roots[got.Root] { + t.Fatalf("list entry %d %+v; want %s with a root of its own (list %+v)", i, got, made[i].Name, list) } + roots[got.Root] = true } - if one := env.snapshots(t, "v1", "s2"); len(one) != 1 || one[0].Name != "s2" || one[0].CutLSN != made[1].CutLSN { - t.Fatalf("show s2: %+v", one) + if one := env.snapshots(t, "v1", "s2"); len(one) != 1 || one[0].Name != "s2" || one[0].Root != list[1].Root { + t.Fatalf("show s2: %+v; want %+v", one, list[1]) } // A snapshot being taken: listed only committed, and then it stays. The @@ -324,7 +322,7 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { done <- result{out, errOut, code, err} }() env.writeBlock(t, "s1", 4, pattern("ladder-b1-blk-E!")) - var observed []snapshotInfo + var observed []snapshotEntry var r result inFlightUnlisted := 0 // listings begun before s4 answered that did not show it // The observation bound: every list probe runs within what is left of it, @@ -345,7 +343,7 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { if err != nil || time.Now().After(deadline) { t.Fatalf("listing s4 within the observation bound: %v", err) } - var s4 *snapshotInfo + var s4 *snapshotEntry for _, e := range list { e := e s4 = &e @@ -356,8 +354,8 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { switch { case s4 == nil && len(observed) > 0: t.Fatal("s4 was listed, then disappeared") - case s4 != nil && (s4.State != "active" || s4.CutLSN <= made[2].CutLSN || s4.HeadGen <= made[2].HeadGen): - t.Fatalf("s4 listed before it was committed: %+v", *s4) + case s4 != nil && (s4.Root == "" || roots[s4.Root]): + t.Fatalf("s4 listed without a root of its own: %+v", *s4) case s4 != nil: observed = append(observed, *s4) } @@ -368,7 +366,7 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { if inFlightUnlisted == 0 { t.Fatalf("inconclusive: no listing ran while s4 was in flight and unlisted (%d listings saw it committed)", len(observed)) } - t.Logf("s4: %d listings while in flight did not show it; %d showed it committed", inFlightUnlisted, len(observed)) + t.Logf("s4: %d listings while in flight did not show it; %d showed it published", inFlightUnlisted, len(observed)) var created snapshotInfo if err := json.Unmarshal([]byte(r.out), &created); err != nil || created.Name != "s4" { t.Fatalf("snapshot s4 answer: %v\n%s", err, r.out) @@ -377,20 +375,26 @@ func TestSnapshotLadderB1SnapshotQuery(t *testing.T) { if len(final) != 1 { t.Fatalf("s4 not listed after its snapshot returned: %+v", final) } - for _, o := range append(observed, final[0]) { - if o.State != "active" || o.CutLSN != created.CutLSN || o.HeadGen != created.HeadGen || o.ChunkCount != created.ChunkCount { - t.Fatalf("s4 listed as %+v; its creation answered %+v", o, created) + for _, o := range observed { + if o.Root != final[0].Root { + t.Fatalf("s4 listed as %+v while in flight, then as %+v: a listed snapshot changed", o, final[0]) } } - // A refused snapshot is not a snapshot. + // A snapshot of a restore target that is not fully local is taken, and + // names the source's chunks, so a restore from it is refused. env.restoreAttached(t, "v1", "s1", "v2") - out, errOut, code := env.h.run(t, "ops", "chunk", "snapshot", "--volume", "v2", "--status-addr", env.hosts["n-v2"].status, "--name", "x") - if code == 0 || !strings.Contains(out+errOut, "restore_incomplete") { - t.Fatalf("snapshot of a restore volume not yet independent: exit %d, want restore_incomplete\n%s\n%s", code, out, errOut) + if st := kzReadStats(t, env.hosts["n-v2"]); st.AbsentChunks == 0 { + t.Fatalf("setup: v2 holds every chunk already; want a restore target not fully local") } - if l := env.snapshots(t, "v2", ""); len(l) != 0 { - t.Fatalf("a refused snapshot left %+v in the list", l) + env.snapshot(t, "n-v2", "v2", "x") + if l := env.snapshots(t, "v2", ""); len(l) != 1 || l[0].Name != "x" { + t.Fatalf("v2's snapshot x is not listed: %+v", l) + } + out, errOut, code := env.h.run(t, "ops", "chunk", "restore", "--master", env.h.masterAddr, "--source", "v2", "--snapshot", "x", "--target", "v3", + "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + if code == 0 || !strings.Contains(out+errOut, "restore_source_not_own") { + t.Fatalf("restore from a restore target's snapshot: exit %d, want restore_source_not_own\n%s\n%s", code, out, errOut) } } diff --git a/seaweed-block/cmd/blockvolume/wrong_geometry_cure_integration_test.go b/seaweed-block/cmd/blockvolume/wrong_geometry_cure_integration_test.go index bb39c94423..f349664e1a 100644 --- a/seaweed-block/cmd/blockvolume/wrong_geometry_cure_integration_test.go +++ b/seaweed-block/cmd/blockvolume/wrong_geometry_cure_integration_test.go @@ -95,8 +95,13 @@ func startSourceWithSnapshot(t *testing.T, targets ...string) *handoffEnv { // headOf is the first line of `sw-block ops chunk show --volume v`. func (env *handoffEnv) headOf(t *testing.T, v string) string { - out, _, _ := env.h.run(t, "ops", "chunk", "show", "--master", env.h.masterAddr, "--volume", v, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) - return firstLine(out) + t.Helper() + out, _ := env.h.command(t, "ops", "chunk", "show", "--master", env.h.masterAddr, "--volume", v, "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath) + head, err := handoffHeadJSON(out) + if err != nil { + t.Fatalf("show HEAD for %s: %v\n%s", v, err, out) + } + return head } func listTree(root string) string {