feat: remove cephmig snapshots during sync and add cleanup command
Users of a subvolume should not see traces of the migration. Each sync round now deletes the older cephmig-* round snapshots at source and target and keeps only the newest one as base for the next round. The new command `cephmig cleanup` deletes the remaining one after the cutover. Application snapshots are never touched. The journal marks a snapshot as removed before deleting it, so an interrupted run finishes the deletion instead of reporting a vanished snapshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cc2dff46a0
commit
907d400765
9 files changed
+322
-19
No files matched your search
@@ -0,0 +1,120 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
)
|
||||
|
||||
// removeOwnedSnapshots deletes the completed snapshots that cephmig created
|
||||
// itself, except keep, at source and target. Snapshots of the application
|
||||
// are never touched.
|
||||
//
|
||||
// Every snapshot is first marked Removed in the journal and deleted after
|
||||
// that, so an interrupted run finishes the deletion next time instead of
|
||||
// reporting a vanished snapshot.
|
||||
func removeOwnedSnapshots(ctx context.Context, src, dst ceph, statePath string, s *State, keep string) error {
|
||||
marked := false
|
||||
for i := range s.Snapshots {
|
||||
snap := &s.Snapshots[i]
|
||||
if snap.Owned && snap.Done && !snap.Removed && snap.Name != keep {
|
||||
snap.Removed = true
|
||||
marked = true
|
||||
}
|
||||
}
|
||||
|
||||
if marked {
|
||||
if err := SaveState(statePath, s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, side := range []ceph{dst, src} {
|
||||
names, err := side.names(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, snap := range s.Snapshots {
|
||||
if !snap.Removed || !slices.Contains(names, snap.Name) {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err = side.snap(ctx, "rm", snap.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cleanup deletes all snapshots cephmig created, at source and target, after
|
||||
// the job is ready for cutover. Afterwards the subvolumes hold only the
|
||||
// snapshots of the application. This includes the final round snapshot, so
|
||||
// run it only after the cutover has been checked.
|
||||
func (e Engine) Cleanup(ctx context.Context, c Config, statePath string) error {
|
||||
if err := c.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if e.Exec == nil {
|
||||
return errors.New("missing executor")
|
||||
}
|
||||
|
||||
abs, err := filepath.Abs(statePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockFile(abs + ".lock")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer lock.Close()
|
||||
s, err := LoadState(abs)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return errors.New("no migration state; nothing to clean up")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.Phase != "ready-for-cutover" {
|
||||
return fmt.Errorf("cleanup requires phase ready-for-cutover, not %q; run finalize first", s.Phase)
|
||||
}
|
||||
|
||||
inspection, err := e.Inspect(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !sameMigrationIdentity(s.Config, c) || s.SourceFSID != inspection.SourceFSID || s.TargetFSID != inspection.TargetFSID ||
|
||||
s.SourcePath != inspection.SourcePath || s.TargetPath != inspection.TargetPath {
|
||||
return errors.New("config or cluster identity changed; refusing to use migration state")
|
||||
}
|
||||
|
||||
claim, err := e.access().Claim(ctx, c, s.TargetFSID, s.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer claim.Close()
|
||||
native := c.Backend == "native"
|
||||
src, dst := ceph{endpoint: c.Source, exec: e.Exec, native: native}, ceph{endpoint: c.Target, exec: e.Exec, native: native}
|
||||
s.LiveSnapshot = ""
|
||||
if err = removeOwnedSnapshots(ctx, src, dst, abs, s, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = SaveState(abs, s); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
e.log("cleanup: removed cephmig snapshots at source and target; state=%s", abs)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// snapshotCeph fakes "ceph fs subvolume snapshot ls|rm" for two clusters,
|
||||
// told apart by the --conf argument.
|
||||
type snapshotCeph struct {
|
||||
snaps map[string][]string
|
||||
failRm string
|
||||
removed []string
|
||||
}
|
||||
|
||||
func (f *snapshotCeph) Exec(_ context.Context, out io.Writer, _ string, args ...string) error {
|
||||
conf := args[1]
|
||||
i := slices.Index(args, "snapshot")
|
||||
switch args[i+1] {
|
||||
case "ls":
|
||||
var list []map[string]string
|
||||
for _, n := range f.snaps[conf] {
|
||||
list = append(list, map[string]string{"name": n})
|
||||
}
|
||||
|
||||
return json.NewEncoder(out).Encode(list)
|
||||
case "rm":
|
||||
name := args[i+4]
|
||||
if name == f.failRm {
|
||||
return errors.New("injected failure")
|
||||
}
|
||||
|
||||
f.snaps[conf] = slices.DeleteFunc(f.snaps[conf], func(n string) bool { return n == name })
|
||||
f.removed = append(f.removed, conf+":"+name)
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("unexpected command")
|
||||
}
|
||||
|
||||
func TestRemoveOwnedSnapshots(t *testing.T) {
|
||||
all := []string{"app-1", "cephmig-a", "cephmig-b", "cephmig-c"}
|
||||
newState := func() *State {
|
||||
s := testState()
|
||||
for _, n := range all {
|
||||
s.Snapshots = append(s.Snapshots, Snapshot{Name: n, Owned: n != "app-1", Done: true,
|
||||
CreatedAt: "2026-01-01 00:00:00", TargetCreatedAt: "2026-01-01 00:00:01"})
|
||||
}
|
||||
|
||||
s.LiveSnapshot = "cephmig-c"
|
||||
return s
|
||||
}
|
||||
|
||||
statePath := filepath.Join(t.TempDir(), "state.json")
|
||||
f := &snapshotCeph{snaps: map[string][]string{"src.conf": slices.Clone(all), "dst.conf": slices.Clone(all)}}
|
||||
src := ceph{endpoint: Endpoint{Config: "src.conf", Filesystem: "fs", Subvolume: "v"}, exec: f}
|
||||
dst := ceph{endpoint: Endpoint{Config: "dst.conf", Filesystem: "fs", Subvolume: "v"}, exec: f}
|
||||
|
||||
// An interrupted run leaves the journal marked, and the retry finishes.
|
||||
s := newState()
|
||||
f.failRm = "cephmig-b"
|
||||
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, s, s.LiveSnapshot); err == nil {
|
||||
t.Fatal("injected failure not reported")
|
||||
}
|
||||
|
||||
loaded, err := LoadState(statePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !loaded.Snapshots[1].Removed || !loaded.Snapshots[2].Removed || loaded.Snapshots[3].Removed || loaded.Snapshots[0].Removed {
|
||||
t.Fatalf("journal marks: %+v", loaded.Snapshots)
|
||||
}
|
||||
|
||||
f.failRm = ""
|
||||
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, loaded, loaded.LiveSnapshot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for conf, names := range f.snaps {
|
||||
if !slices.Equal(names, []string{"app-1", "cephmig-c"}) {
|
||||
t.Fatalf("%s keeps %v", conf, names)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup removes the base too, but never application snapshots.
|
||||
loaded.LiveSnapshot = ""
|
||||
if err := removeOwnedSnapshots(context.Background(), src, dst, statePath, loaded, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for conf, names := range f.snaps {
|
||||
if !slices.Equal(names, []string{"app-1"}) {
|
||||
t.Fatalf("%s keeps %v", conf, names)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := LoadState(statePath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateRejectsInvalidRemoval(t *testing.T) {
|
||||
done := Snapshot{Name: "a", Owned: true, Done: true, Removed: true, CreatedAt: "x", TargetCreatedAt: "y"}
|
||||
for name, modify := range map[string]func(*State){
|
||||
"application snapshot": func(s *State) { d := done; d.Owned = false; s.Snapshots = []Snapshot{d} },
|
||||
"incomplete": func(s *State) { s.Snapshots = []Snapshot{{Name: "a", Owned: true, Removed: true}} },
|
||||
"removed live base": func(s *State) { s.Snapshots = []Snapshot{done}; s.LiveSnapshot = "a" },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), "state.json")
|
||||
s := testState()
|
||||
modify(s)
|
||||
if err := SaveState(p, s); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := LoadState(p); err == nil {
|
||||
t.Fatal("accepted invalid removal")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -450,6 +450,10 @@ func (e Engine) Sync(ctx context.Context, c Config, statePath string, final bool
|
||||
|
||||
// Resume all intent records before capturing another live snapshot.
|
||||
for i := range s.Snapshots {
|
||||
if s.Snapshots[i].Removed {
|
||||
continue
|
||||
}
|
||||
|
||||
if s.Snapshots[i].Done {
|
||||
snap := &s.Snapshots[i]
|
||||
if !slices.Contains(targetNames, snap.Name) {
|
||||
@@ -504,6 +508,11 @@ func (e Engine) Sync(ctx context.Context, c Config, statePath string, final bool
|
||||
return err
|
||||
}
|
||||
|
||||
// Only the newest round snapshot serves as base for the next round.
|
||||
if err = removeOwnedSnapshots(ctx, src, dst, abs, s, s.LiveSnapshot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
metadata, err := src.metadata(ctx, "")
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
+14
-10
@@ -16,14 +16,17 @@ const stateVersion = 1
|
||||
// Snapshot is the journal record of one source snapshot and its transfer
|
||||
// to the target.
|
||||
type Snapshot struct {
|
||||
Name string `json:"name"`
|
||||
CreatedAt string `json:"source_created_at"`
|
||||
TargetCreatedAt string `json:"target_created_at,omitempty"`
|
||||
Owned bool `json:"owned"`
|
||||
Final bool `json:"final,omitempty"`
|
||||
Done bool `json:"done"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
Stats *TransferStats `json:"transfer,omitempty"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt string `json:"source_created_at"`
|
||||
TargetCreatedAt string `json:"target_created_at,omitempty"`
|
||||
Owned bool `json:"owned"`
|
||||
Final bool `json:"final,omitempty"`
|
||||
Done bool `json:"done"`
|
||||
// Removed marks an owned snapshot that is deleted, or is about to be
|
||||
// deleted, at source and target.
|
||||
Removed bool `json:"removed,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
Stats *TransferStats `json:"transfer,omitempty"`
|
||||
}
|
||||
|
||||
// State is the persistent migration journal. It is written atomically
|
||||
@@ -73,7 +76,8 @@ func LoadState(path string) (*State, error) {
|
||||
|
||||
names := map[string]bool{}
|
||||
for _, snap := range s.Snapshots {
|
||||
if !validName(snap.Name) || names[snap.Name] || (snap.Done && (snap.CreatedAt == "" || snap.TargetCreatedAt == "")) {
|
||||
if !validName(snap.Name) || names[snap.Name] || (snap.Done && (snap.CreatedAt == "" || snap.TargetCreatedAt == "")) ||
|
||||
(snap.Removed && (!snap.Owned || !snap.Done)) {
|
||||
return nil, errors.New("invalid snapshot state")
|
||||
}
|
||||
|
||||
@@ -83,7 +87,7 @@ func LoadState(path string) (*State, error) {
|
||||
if s.LiveSnapshot != "" {
|
||||
confirmed := false
|
||||
for _, snap := range s.Snapshots {
|
||||
if snap.Name == s.LiveSnapshot && snap.Done {
|
||||
if snap.Name == s.LiveSnapshot && snap.Done && !snap.Removed {
|
||||
confirmed = true
|
||||
break
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user