The writing client kept the caps of the files it wrote last. Every lookup of the verification client on one of them made the MDS recall the caps entry by entry, which cost about five seconds per lookup in the lab and stalled a 100,000-file verification near its end. Verify now opens a fresh target client, closes the writer, which returns all caps at once, and keeps writing through the fresh client. The claim lock is held through a client of its own, so it survives the change. Verify also checks non-directory entries in parallel with the configured number of workers. The walk and the directories stay serial, so skipped subtrees are handled in walk order. Transfer statistics report the duration of each round in seconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
279 lines
6.5 KiB
Go
279 lines
6.5 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
type nativeAccess struct{ backend *nativeTransport }
|
|
|
|
func (a nativeAccess) Inspect(ctx context.Context, c Config, x Executor) (*Inspection, error) {
|
|
if err := c.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sf, si, err := queryEndpoint(ctx, ceph{endpoint: c.Source, exec: x, native: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tf, ti, err := queryEndpoint(ctx, ceph{endpoint: c.Target, exec: x, native: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if sf == tf {
|
|
return nil, errors.New("native source and target must be different clusters")
|
|
}
|
|
|
|
for _, p := range []struct {
|
|
fs storage.FS
|
|
path, fsid string
|
|
}{{a.backend.source, si.Path, sf}, {a.backend.target, ti.Path, tf}} {
|
|
if _, err := nativePath(p.path); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := validateNativeDirectory(p.fs, p.path); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := p.fs.GetXattr(p.path, "ceph.cluster_fsid")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if string(id) != p.fsid {
|
|
return nil, errors.New("native connection belongs to the wrong cluster")
|
|
}
|
|
}
|
|
|
|
names, err := (ceph{endpoint: c.Source, exec: x, native: true}).names(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fingerprint := sha256.Sum256([]byte(sf + "\x00" + tf + "\x00" + si.Path + "\x00" + ti.Path))
|
|
a.backend.identity = hex.EncodeToString(fingerprint[:])
|
|
return &Inspection{SourceFSID: sf, TargetFSID: tf, SourcePath: si.Path, TargetPath: ti.Path,
|
|
SourceBytes: si.BytesUsed, SourceSnapshots: names, Notes: []string{SnapshotIdentityNote}}, nil
|
|
}
|
|
|
|
func (a nativeAccess) Empty(ctx context.Context, p string) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
d, err := a.backend.target.OpenDir(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer d.Close()
|
|
_, err = d.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return errors.New("new native migration requires an empty target subvolume")
|
|
}
|
|
|
|
func (a nativeAccess) Claim(ctx context.Context, c Config, fsid, id string) (io.Closer, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if a.backend.openTarget == nil {
|
|
return claimNative(a.backend.target, c, fsid, id)
|
|
}
|
|
|
|
// The lock lives as long as this client; Verify replaces target.
|
|
client, err := a.backend.openTarget()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("native claim client: %w", err)
|
|
}
|
|
|
|
f, err := claimNative(client, c, fsid, id)
|
|
if err != nil {
|
|
client.Close()
|
|
return nil, err
|
|
}
|
|
|
|
return nativeClaim{file: f, client: client}, nil
|
|
}
|
|
|
|
// nativeClaim is a claim lock held through a client of its own.
|
|
type nativeClaim struct {
|
|
file storage.File
|
|
client storage.FS
|
|
}
|
|
|
|
func (c nativeClaim) Close() error {
|
|
return errors.Join(c.file.Close(), c.client.Close())
|
|
}
|
|
|
|
// claimNative creates the claim file for id if needed, locks it through fs
|
|
// and checks that it names id. The returned file holds the lock.
|
|
func claimNative(fs storage.FS, c Config, fsid, id string) (storage.File, error) {
|
|
current := "/"
|
|
for _, component := range []string{".cephmig", "claims"} {
|
|
current = path.Join(current, component)
|
|
n, err := fs.Stat(current)
|
|
if errors.Is(err, syscall.ENOENT) {
|
|
if err = fs.Mkdir(current, 0700); err != nil && !errors.Is(err, syscall.EEXIST) {
|
|
return nil, err
|
|
}
|
|
|
|
n, err = fs.Stat(current)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return nil, errors.New("native claim directory contains a symlink or non-directory")
|
|
}
|
|
}
|
|
|
|
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 {
|
|
err = f.Sync()
|
|
}
|
|
|
|
closeErr := f.Close()
|
|
if err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else if !errors.Is(err, syscall.EEXIST) {
|
|
return nil, err
|
|
}
|
|
|
|
n, err := fs.Stat(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if n.Kind() != storage.Regular {
|
|
return nil, errors.New("native target claim is not a regular file")
|
|
}
|
|
|
|
f, err = fs.Open(p, syscall.O_RDWR|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = f.Lock(1); err != nil {
|
|
f.Close()
|
|
return nil, fmt.Errorf("native target is locked: %w", err)
|
|
}
|
|
|
|
b := make([]byte, 256)
|
|
count, readErr := f.ReadAt(b, 0)
|
|
if readErr != nil && !errors.Is(readErr, io.EOF) {
|
|
f.Close()
|
|
return nil, readErr
|
|
}
|
|
|
|
if string(b[:count]) != id+"\n" {
|
|
f.Close()
|
|
return nil, errors.New("native target belongs to a different job")
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (a nativeAccess) Apply(context.Context, string, string) error { return a.backend.target.Sync() }
|
|
func (a nativeAccess) Verify(context.Context, string, string, string) error { return nil } // Transport verifies all native metadata and links.
|
|
|
|
type nativeVerifyIndex struct {
|
|
index *linkIndex
|
|
dir string
|
|
}
|
|
|
|
func osNativeVerifyIndex(parent string) (*nativeVerifyIndex, error) {
|
|
dir, err := os.MkdirTemp(parent, "verify-")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
index, err := openLinkIndex(filepath.Join(dir, "links.bolt"))
|
|
if err != nil {
|
|
os.RemoveAll(dir)
|
|
return nil, err
|
|
}
|
|
|
|
return &nativeVerifyIndex{index: index, dir: dir}, nil
|
|
}
|
|
|
|
func (i *nativeVerifyIndex) cleanup() {
|
|
_ = i.index.close()
|
|
_ = os.RemoveAll(i.dir)
|
|
}
|
|
|
|
func options(ep Endpoint) storage.Options {
|
|
return storage.Options{Config: ep.Config, Keyring: ep.Keyring, User: strings.TrimPrefix(ep.User, "client."), Filesystem: ep.Filesystem}
|
|
}
|