diff --git a/seaweed-block/cmd/blockvolume/volume_delete_procedure_integration_test.go b/seaweed-block/cmd/blockvolume/volume_delete_procedure_integration_test.go new file mode 100644 index 0000000000..3339fb7c39 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/volume_delete_procedure_integration_test.go @@ -0,0 +1,135 @@ +//go:build subprocess && chunkstore_integration + +package main_test + +import ( + "encoding/json" + "strings" + "testing" + "time" +) + +// The volume-lifecycle page's "Delete a volume", end to end with the real +// binaries: seal, revoke and wait for the ACK, delete the snapshots, delete +// the volume; and the page's recovery when blockvolume was stopped by a +// signal after the seal (the ACK was skipped). + +// volumeDelete runs the page's step 4 and returns the answer and the exit +// status. +func (env *handoffEnv) volumeDelete(t *testing.T, request string) (string, string, int) { + t.Helper() + return env.h.run(t, "ops", "chunk", "volume-delete", "--volume", "v1", "--chunk-filer", env.cluster.filerGRPC, "--chunk-path", env.chunkPath, + "--master", env.h.masterAddr, "--request-id", request) +} + +// requireDeleteCompleted checks step 4's answer: admitted, committed, HEAD +// marked, completed; and replayed when it is a rerun of a completed delete. +func requireDeleteCompleted(t *testing.T, out, errOut string, code int, rerun bool) { + t.Helper() + var res struct { + Admitted, Committed, HeadMarked, Completed, Replayed bool + } + if code != 0 || json.Unmarshal([]byte(out), &res) != nil || !res.Admitted || !res.Committed || !res.HeadMarked || !res.Completed || res.Replayed != rerun { + t.Fatalf("volume-delete: exit %d %s %s; want admitted, committed, headMarked, completed, replayed %v", code, out, errOut, rerun) + } +} + +// headShown returns HEAD's state and chunk count from `ops chunk show`. +func (env *handoffEnv) headShown(t *testing.T) (string, int) { + t.Helper() + out, _ := env.h.command(t, env.chunkShow()...) + var body struct { + Head *struct { + State string `json:"state"` + ChunkCount int `json:"chunkCount"` + } `json:"head"` + } + if err := json.Unmarshal([]byte(out), &body); err != nil || body.Head == nil { + t.Fatalf("chunk show: %v %s", err, out) + } + return body.Head.State, body.Head.ChunkCount +} + +// requireDeletedForGood checks the page's end state ("state": "deleted", +// "chunkCount": 0) and that a rerun with the same request id completes +// again. +func (env *handoffEnv) requireDeletedForGood(t *testing.T, request string) { + t.Helper() + if state, n := env.headShown(t); state != "deleted" || n != 0 { + t.Fatalf("chunk show after the delete: state %q, chunkCount %d; want deleted, 0", state, n) + } + out, errOut, code := env.volumeDelete(t, request) + requireDeleteCompleted(t, out, errOut, code, true) +} + +// TestProcedureDeleteAVolume: the page's steps. A volume with a snapshot is +// sealed, revoked (blockvolume sends the ACK and exits 0 by itself) and its +// ACK seen; volume-delete is refused while the snapshot exists, with nothing +// deleted; the snapshot is deleted; volume-delete then answers admitted, +// committed, headMarked, completed, `chunk show` shows HEAD deleted with no +// chunks, and a rerun with the same request id completes again. +func TestProcedureDeleteAVolume(t *testing.T) { + env := startHandoffEnv(t, 1) + holder := env.start(t, "blockvolume-s1", "s1") + e := env.adopt(t, holder, "s1") + env.writeBlock(t, "s1", 0, pattern("delete-a-volume!")) + env.snapshot(t, "s1", "v1", "s1") + + g := env.seal(t, "s1", e) + env.revoke(t, e, g) + if err := holder.wait(30 * time.Second); err != nil { + t.Fatalf("s1 after the revocation: %v; want a clean exit by itself", err) + } + env.waitAck(t, e, g) + + state0, n0 := env.headShown(t) + out, errOut, code := env.volumeDelete(t, "del-v1") + if code == 0 || !strings.Contains(out+errOut, "has snapshots (s1)") { + t.Fatalf("volume-delete with a snapshot: exit %d %s %s; want refused, naming s1", code, out, errOut) + } + if state, n := env.headShown(t); state != "detached" || state != state0 || n != n0 || n == 0 { + t.Fatalf("after the refused delete HEAD is %q with %d chunks; want it as before (%q, %d chunks): nothing deleted", state, n, state0, n0) + } + env.deleteSnapshot(t, "v1", "s1") + out, errOut, code = env.volumeDelete(t, "del-v1") + requireDeleteCompleted(t, out, errOut, code, false) + env.requireDeletedForGood(t, "del-v1") +} + +// TestProcedureDeleteAfterASignalStop: the page's recovery. The volume is +// sealed and blockvolume is then stopped with SIGTERM, so the ACK is +// skipped: volume-delete is refused (live authority requires a clean +// revocation ACK) and nothing is deleted. blockvolume is started again with +// the same flags and local root; it is revoked and sends the ACK (exit 0 by +// itself); volume-delete then completes, HEAD is deleted with no chunks, and +// a rerun with the same request id completes again. +func TestProcedureDeleteAfterASignalStop(t *testing.T) { + env := startHandoffEnv(t, 1) + holder := env.start(t, "blockvolume-s1", "s1") + e := env.adopt(t, holder, "s1") + env.writeBlock(t, "s1", 0, pattern("delete-after-sig")) + + g := env.seal(t, "s1", e) + holder.term(t, 30*time.Second) + state0, n0 := env.headShown(t) + out, errOut, code := env.volumeDelete(t, "del-v1") + if code == 0 || !strings.Contains(out+errOut, "live authority requires clean revocation ACK") { + t.Fatalf("volume-delete after a signal stop: exit %d %s %s; want refused for the missing ACK", code, out, errOut) + } + if state, n := env.headShown(t); state != "detached" || state != state0 || n != n0 || n == 0 { + t.Fatalf("after the refused delete HEAD is %q with %d chunks; want it as before (%q, %d chunks): nothing deleted", state, n, state0, n0) + } + + again := env.start(t, "blockvolume-s1-again", "s1") + poll(t, 60*time.Second, "the restarted holder never listened on its status address", func() bool { + return listening(env.hosts["s1"].status) + }) + env.revoke(t, e, g) + if err := again.wait(60 * time.Second); err != nil { + t.Fatalf("the restarted s1 after the revocation: %v; want a clean exit by itself", err) + } + env.waitAck(t, e, g) + out, errOut, code = env.volumeDelete(t, "del-v1") + requireDeleteCompleted(t, out, errOut, code, false) + env.requireDeletedForGood(t, "del-v1") +} diff --git a/seaweed-block/core/frontend/chunkstore/volume_delete_crash_test.go b/seaweed-block/core/frontend/chunkstore/volume_delete_crash_test.go new file mode 100644 index 0000000000..239abeadcc --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/volume_delete_crash_test.go @@ -0,0 +1,172 @@ +package chunkstore + +import ( + "context" + "fmt" + "path" + "testing" + + "github.com/seaweedfs/seaweed-block/core/storage" +) + +// nthUpdate matches the n-th UpdateEntry (1-based) of the entry at p. +func nthUpdate(p string, n int) func(op, q string) bool { + seen := 0 + return func(op, q string) bool { + if op != "update" || q != p { + return false + } + seen++ + return seen == n + } +} + +// requireVolumeDeleted checks the end state of a completed volume delete: +// HEAD deleted, the VOLUME entry marked deleted, the volume refused for +// ever, the reservation committed. +func requireVolumeDeleted(t *testing.T, c *Catalog, rsv *fakeReservation, vol string) { + t.Helper() + ctx := context.Background() + h, ok, err := c.ReadHead(ctx, vol) + if err != nil || !ok || h.State != storage.ChunkHeadDeleted { + t.Fatalf("HEAD after the delete: %+v ok %v %v; want deleted", h, ok, err) + } + if v, ok, err := c.ReadVolume(ctx, vol); err != nil || !ok || v.State != VolumeDeleted { + t.Fatalf("VOLUME after the delete: %+v ok %v %v; want deleted", v, ok, err) + } + if _, err := c.EnsureVolume(ctx, vol, catGeo); CodeOf(err) != CodeVolumeDeleted { + t.Fatalf("attach after the delete: %v; want volume_deleted", err) + } + rsv.mu.Lock() + defer rsv.mu.Unlock() + if rsv.state != reservationCommitted { + t.Fatalf("the reservation is %q after the delete; want committed", rsv.state) + } +} + +// TestCatalogVolumeDeleteCrashAtEachStepThenRerunCompletes: a volume delete +// whose process dies at each of its filer writes (the HEAD mark, the commit +// to purging, the HEAD deleted, the VOLUME deleted), before the write reaches +// the filer or after it applied with its reply lost. A rerun with the same +// request, through a restarted catalog, completes: HEAD deleted, the volume +// refused for ever, the reservation committed. Nothing stays deleting or +// purging. +func TestCatalogVolumeDeleteCrashAtEachStepThenRerunCompletes(t *testing.T) { + headPath := path.Join(catBase, "v1", headName) + volumePath := path.Join(catBase, "v1", volumeName) + // left is the HEAD state each crash leaves for the rerun: before the + // write applied, and after. + steps := []struct { + name string + p string + n int + left [2]string + }{ + {"the HEAD mark (deleting)", headPath, 1, [2]string{storage.ChunkHeadDetached, storage.ChunkHeadDeleting}}, + {"the commit (purging)", headPath, 2, [2]string{storage.ChunkHeadDeleting, storage.ChunkHeadPurging}}, + {"HEAD deleted", headPath, 3, [2]string{storage.ChunkHeadPurging, storage.ChunkHeadDeleted}}, + {"VOLUME deleted", volumePath, 1, [2]string{storage.ChunkHeadDeleted, storage.ChunkHeadDeleted}}, + } + for _, step := range steps { + for _, after := range []bool{false, true} { + name := fmt.Sprintf("%s, %s", step.name, map[bool]string{false: "before it applied", true: "applied, reply lost"}[after]) + t.Run(name, func(t *testing.T) { + f, b, _, _ := deleteFixture(t) + ctx := context.Background() + detachHead(t, f, b, "v1") + rsv := &fakeReservation{} + crashed := &crashFiler{manifestFilerBlobs: manifestFilerBlobs{f, gcBlobs{b}}, after: after, match: nthUpdate(step.p, step.n)} + dying, err := NewCatalog(crashed, catBase) + if err != nil { + t.Fatal(err) + } + if res, err := dying.DeleteVolume(ctx, "v1", rsv.admission("d1")); err == nil { + t.Fatalf("the delete returned success although its process died: %+v", res) + } + crashed.mu.Lock() + died := crashed.dead + crashed.mu.Unlock() + if !died { + t.Fatalf("the delete never reached the step %q", name) + } + + c, err := NewCatalog(manifestFilerBlobs{f, gcBlobs{b}}, catBase) // the restarted process + if err != nil { + t.Fatal(err) + } + left := step.left[0] + if after { + left = step.left[1] + } + if got := headState(t, c, "v1"); got.State != left { + t.Fatalf("after the crash HEAD is %q; want %q", got.State, left) + } + if v, _, err := c.ReadVolume(ctx, "v1"); err != nil || (v.State == VolumeDeleted) != (step.p == volumePath && after) { + t.Fatalf("after the crash VOLUME is %q (%v); want deleted only after its own write applied", v.State, err) + } + res, err := c.DeleteVolume(ctx, "v1", rsv.admission("d1")) + if err != nil || !res.Completed { + t.Fatalf("the rerun after a crash at %s: %+v %v; want completed", name, res, err) + } + requireVolumeDeleted(t, c, rsv, "v1") + if res, err := c.DeleteVolume(ctx, "v1", rsv.admission("d1")); err != nil || !res.Completed || !res.Replayed { + t.Fatalf("a further rerun: %+v %v; want completed and replayed", res, err) + } + }) + } + } +} + +// TestCatalogVolumeDeleteCrashAtTheRevertThenRerunCompletes: a volume delete +// finds a snapshot name and reverts its mark, and its process dies at the +// revert (before it applied, or applied with its reply lost). Through a +// restarted catalog a rerun is refused while the snapshot exists +// (volume_has_snapshots) and leaves HEAD as it was before the delete; once +// the snapshot is deleted a rerun completes. +func TestCatalogVolumeDeleteCrashAtTheRevertThenRerunCompletes(t *testing.T) { + headPath := path.Join(catBase, "v1", headName) + for _, after := range []bool{false, true} { + t.Run(map[bool]string{false: "before it applied", true: "applied, reply lost"}[after], func(t *testing.T) { + f, b, c0, h := deleteFixture(t) + ctx := context.Background() + detachHead(t, f, b, "v1") + if _, err := c0.CreateSnapshot(ctx, "v1", SnapshotRequest{Name: "s1", RequestID: "r-s1"}, h); err != nil { + t.Fatal(err) + } + before := headState(t, c0, "v1") + rsv := &fakeReservation{} + crashed := &crashFiler{manifestFilerBlobs: manifestFilerBlobs{f, gcBlobs{b}}, after: after, match: nthUpdate(headPath, 2)} + dying, err := NewCatalog(crashed, catBase) + if err != nil { + t.Fatal(err) + } + if res, err := dying.DeleteVolume(ctx, "v1", rsv.admission("d1")); err == nil { + t.Fatalf("the delete returned success although its process died: %+v", res) + } + crashed.mu.Lock() + died := crashed.dead + crashed.mu.Unlock() + if !died { + t.Fatal("the delete never reached its revert") + } + + c, err := NewCatalog(manifestFilerBlobs{f, gcBlobs{b}}, catBase) + if err != nil { + t.Fatal(err) + } + if _, err := c.DeleteVolume(ctx, "v1", rsv.admission("d1")); CodeOf(err) != CodeVolumeHasSnapshots { + t.Fatalf("a rerun while the snapshot exists: %v; want volume_has_snapshots", err) + } + if got := headState(t, c, "v1"); got.State != before.State { + t.Fatalf("HEAD after the refused rerun is %q; want %q as before the delete", got.State, before.State) + } + if _, err := c.DeleteSnapshot(ctx, "v1", "s1"); err != nil { + t.Fatal(err) + } + if res, err := c.DeleteVolume(ctx, "v1", rsv.admission("d1")); err != nil || !res.Completed { + t.Fatalf("the rerun once the snapshot is deleted: %+v %v; want completed", res, err) + } + requireVolumeDeleted(t, c, rsv, "v1") + }) + } +}