cleanup now deletes the claim file under .cephmig/claims as its last step, if it holds the job's own ID, and removes the claim directories when they are empty. A cleaned-up job is finished: cleanup and finalize do nothing and do not claim the target again, and sync is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
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")
|
|
}
|
|
|
|
// A completed cleanup has released the claim; do not create it again.
|
|
if !cleanedUp(s) {
|
|
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
|
|
}
|
|
|
|
// Last step: the job no longer protects the target.
|
|
if err = e.access().Release(ctx, c, s.TargetFSID, s.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
e.log("cleanup: removed cephmig snapshots and the target claim; state=%s", abs)
|
|
return nil
|
|
}
|
|
|
|
// cleanedUp reports whether Cleanup completed its snapshot removal for s.
|
|
func cleanedUp(s *State) bool {
|
|
if s.Phase != "ready-for-cutover" || s.LiveSnapshot != "" {
|
|
return false
|
|
}
|
|
|
|
for _, snap := range s.Snapshots {
|
|
if snap.Owned && !snap.Removed {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|