Files
CephfsMig/internal/migration/cleanup.go
T
Sebastian UnterschützandClaude Opus 5.5 330a3ec12c feat!: remove the rsync backend
The native libcephfs backend is the only transfer path. The rsync
transport, the mounted-filesystem access and its helpers (mount check,
path resolution below a mount, mount claim, quota and hardlink repair
through the mount) are removed, together with the rsync lab test and
the client image that only added rsync.

The claim and release tests now exercise the native claim through a
local test file system with real flock locks.

BREAKING CHANGE: "backend": "rsync" and the endpoint field "mount" are
rejected. "backend" is optional and only accepts "native"; jobs created
with "native" resume without it. Journals of rsync jobs cannot be
resumed. Builds without the build tag ceph only support doctor and
status.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:03:22 +01:00

144 lines
3.4 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()
}
src, dst := ceph{endpoint: c.Source, exec: e.Exec}, ceph{endpoint: c.Target, exec: e.Exec}
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
}