feat: release the target claim in cleanup
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>
This commit is contained in:
1 parent
5f29328932
commit
a6e7bbb17e
10 files changed
+218
-14
No files matched your search
@@ -2,10 +2,14 @@ package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func nativePath(p string) (string, error) {
|
||||
@@ -24,6 +28,9 @@ type Access interface {
|
||||
Prepare(context.Context, string, string, string) error
|
||||
Apply(context.Context, string, string) error
|
||||
Verify(context.Context, string, string, string) error
|
||||
// Release deletes the claim of job id, and the claim directories if
|
||||
// they are empty. A missing claim is not an error.
|
||||
Release(context.Context, Config, string, string) error
|
||||
}
|
||||
|
||||
type mountedAccess struct{}
|
||||
@@ -38,6 +45,40 @@ func (mountedAccess) Claim(_ context.Context, c Config, fsid, id string) (io.Clo
|
||||
return claimTarget(c.Target.Mount, fsid, c.Target, id)
|
||||
}
|
||||
|
||||
func (mountedAccess) Release(_ context.Context, c Config, fsid, id string) error {
|
||||
dir := filepath.Join(c.Target.Mount, ".cephmig", "claims")
|
||||
p := filepath.Join(dir, claimName(fsid, c.Target))
|
||||
f, err := os.OpenFile(p, os.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(io.LimitReader(f, 256))
|
||||
f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if string(b) != id+"\n" {
|
||||
return errors.New("target claim belongs to a different job; not removed")
|
||||
}
|
||||
|
||||
if err = os.Remove(p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range []string{dir, filepath.Dir(dir)} {
|
||||
if err := os.Remove(d); err != nil && !errors.Is(err, os.ErrNotExist) && !errors.Is(err, syscall.ENOTEMPTY) && !errors.Is(err, syscall.EEXIST) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mountedAccess) Prepare(ctx context.Context, src, dst, scratch string) error {
|
||||
if err := clearQuotas(ctx, dst); err != nil {
|
||||
return err
|
||||
|
||||
@@ -98,12 +98,16 @@ func (e Engine) Cleanup(ctx context.Context, c Config, statePath string) error {
|
||||
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
|
||||
// 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()
|
||||
}
|
||||
|
||||
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 = ""
|
||||
@@ -115,6 +119,26 @@ func (e Engine) Cleanup(ctx context.Context, c Config, statePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
e.log("cleanup: removed cephmig snapshots at source and target; state=%s", abs)
|
||||
// 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
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
@@ -126,3 +127,69 @@ func TestStateRejectsInvalidRemoval(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReleaseRemovesOnlyOwnClaim(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
c := testConfig()
|
||||
c.Target.Mount = root
|
||||
other := c
|
||||
other.Target.Subvolume = "other"
|
||||
for _, x := range []struct {
|
||||
c Config
|
||||
id string
|
||||
}{{c, "job-one"}, {other, "job-two"}} {
|
||||
f, err := claimTarget(root, "fsid", x.c.Target, x.id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
f.Close()
|
||||
}
|
||||
|
||||
access := mountedAccess{}
|
||||
if err := access.Release(context.Background(), c, "fsid", "job-two"); err == nil {
|
||||
t.Fatal("removed the claim of a different job")
|
||||
}
|
||||
|
||||
if err := access.Release(context.Background(), c, "fsid", "job-one"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
claims := filepath.Join(root, ".cephmig", "claims")
|
||||
if _, err := os.Stat(filepath.Join(claims, claimName("fsid", c.Target))); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("own claim still present: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(claims, claimName("fsid", other.Target))); err != nil {
|
||||
t.Fatalf("claim of another job removed: %v", err)
|
||||
}
|
||||
|
||||
if err := access.Release(context.Background(), other, "fsid", "job-two"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(root, ".cephmig")); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("empty claim directories kept: %v", err)
|
||||
}
|
||||
|
||||
// Releasing again is a no-op.
|
||||
if err := access.Release(context.Background(), c, "fsid", "job-one"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanedUp(t *testing.T) {
|
||||
s := testState()
|
||||
s.Phase = "ready-for-cutover"
|
||||
s.Snapshots = []Snapshot{{Name: "app", Done: true}, {Name: "cephmig-x", Owned: true, Done: true, Final: true}}
|
||||
s.LiveSnapshot = "cephmig-x"
|
||||
if cleanedUp(s) {
|
||||
t.Fatal("ready job reported as cleaned up")
|
||||
}
|
||||
|
||||
s.LiveSnapshot = ""
|
||||
s.Snapshots[1].Removed = true
|
||||
if !cleanedUp(s) {
|
||||
t.Fatal("cleaned job not recognized")
|
||||
}
|
||||
}
|
||||
@@ -190,10 +190,16 @@ func emptyDirectory(path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// claimName is the file name of the claim for the target subvolume ep in
|
||||
// the cluster fsid, below .cephmig/claims at the file system root.
|
||||
func claimName(fsid string, ep Endpoint) string {
|
||||
key := sha256.Sum256([]byte(fsid + "\x00" + ep.Filesystem + "\x00" + ep.Group + "\x00" + ep.Subvolume))
|
||||
return hex.EncodeToString(key[:])
|
||||
}
|
||||
|
||||
// claimTarget locks the target subvolume for the job id. The claim is outside
|
||||
// the migrated tree and survives deletion of the local journal. O_EXCL prevents two different jobs from adopting the same target.
|
||||
func claimTarget(root, fsid string, ep Endpoint, id string) (*os.File, error) {
|
||||
key := sha256.Sum256([]byte(fsid + "\x00" + ep.Filesystem + "\x00" + ep.Group + "\x00" + ep.Subvolume))
|
||||
dir := filepath.Join(root, ".cephmig", "claims")
|
||||
if err := privateDirectories(dir); err != nil {
|
||||
return nil, err
|
||||
@@ -204,7 +210,7 @@ func claimTarget(root, fsid string, ep Endpoint, id string) (*os.File, error) {
|
||||
return nil, errors.New("claim directory contains symlinks")
|
||||
}
|
||||
|
||||
path := filepath.Join(dir, hex.EncodeToString(key[:]))
|
||||
path := filepath.Join(dir, claimName(fsid, ep))
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
|
||||
if err == nil {
|
||||
if _, err = f.WriteString(id + "\n"); err == nil {
|
||||
@@ -369,6 +375,15 @@ func (e Engine) Sync(ctx context.Context, c Config, statePath string, final bool
|
||||
return errors.New("config or cluster identity changed; refusing to reuse migration state")
|
||||
}
|
||||
|
||||
if cleanedUp(s) {
|
||||
if final {
|
||||
e.log("job is cleaned up; nothing to do")
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("job is cleaned up; sync is no longer allowed")
|
||||
}
|
||||
|
||||
claim, err := e.access().Claim(ctx, c, s.TargetFSID, s.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -118,8 +118,7 @@ func (a nativeAccess) Claim(ctx context.Context, c Config, fsid, id string) (io.
|
||||
}
|
||||
}
|
||||
|
||||
sum := sha256.Sum256([]byte(fsid + "\x00" + c.Target.Filesystem + "\x00" + c.Target.Group + "\x00" + c.Target.Subvolume))
|
||||
p := path.Join(current, hex.EncodeToString(sum[:]))
|
||||
p := path.Join(current, claimName(fsid, c.Target))
|
||||
f, err := fs.Open(p, syscall.O_CREAT|syscall.O_EXCL|syscall.O_RDWR|syscall.O_NOFOLLOW, 0600)
|
||||
if err == nil {
|
||||
if err = writeNativeAt(f, []byte(id+"\n"), 0); err == nil {
|
||||
@@ -171,6 +170,46 @@ func (a nativeAccess) Claim(ctx context.Context, c Config, fsid, id string) (io.
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func (a nativeAccess) Release(ctx context.Context, c Config, fsid, id string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fs := a.backend.target
|
||||
dir := "/.cephmig/claims"
|
||||
p := path.Join(dir, claimName(fsid, c.Target))
|
||||
f, err := fs.Open(p, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
||||
if errors.Is(err, syscall.ENOENT) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b := make([]byte, 256)
|
||||
n, err := f.ReadAt(b, 0)
|
||||
f.Close()
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return err
|
||||
}
|
||||
|
||||
if string(b[:n]) != id+"\n" {
|
||||
return errors.New("native target claim belongs to a different job; not removed")
|
||||
}
|
||||
|
||||
if err = fs.Unlink(p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, d := range []string{dir, "/.cephmig"} {
|
||||
if err := fs.Rmdir(d); err != nil && !errors.Is(err, syscall.ENOENT) && !errors.Is(err, syscall.ENOTEMPTY) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a nativeAccess) Prepare(ctx context.Context, _, target, _ string) error {
|
||||
return clearNativeQuotas(ctx, a.backend.target, target)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user