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>
165 lines
4.8 KiB
Go
165 lines
4.8 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
func nativePath(p string) (string, error) {
|
|
if !strings.HasPrefix(p, "/") || path.Clean(p) != p || p == "/" {
|
|
return "", fmt.Errorf("invalid Ceph data path %q", p)
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
// Access separates filesystem access from the shared snapshot/journal workflow.
|
|
type Access interface {
|
|
Inspect(context.Context, Config, Executor) (*Inspection, error)
|
|
Empty(context.Context, string) error
|
|
Claim(context.Context, Config, string, string) (io.Closer, error)
|
|
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{}
|
|
|
|
func (mountedAccess) Inspect(ctx context.Context, c Config, x Executor) (*Inspection, error) {
|
|
return inspectMounted(ctx, c, x)
|
|
}
|
|
|
|
func (mountedAccess) Empty(_ context.Context, p string) error { return emptyDirectory(p) }
|
|
|
|
func (mountedAccess) Claim(_ context.Context, c Config, fsid, id string) (io.Closer, error) {
|
|
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
|
|
}
|
|
|
|
return hardlinkTopology(ctx, src, dst, scratch, true)
|
|
}
|
|
|
|
func (mountedAccess) Apply(ctx context.Context, src, dst string) error {
|
|
return quotas(ctx, src, dst, false)
|
|
}
|
|
|
|
func (mountedAccess) Verify(ctx context.Context, src, dst, scratch string) error {
|
|
if err := hardlinkTopology(ctx, src, dst, scratch, false); err != nil {
|
|
return err
|
|
}
|
|
|
|
return quotas(ctx, src, dst, true)
|
|
}
|
|
|
|
func (e Engine) access() Access {
|
|
if e.Access != nil {
|
|
return e.Access
|
|
}
|
|
|
|
return mountedAccess{}
|
|
}
|
|
|
|
// Inspect checks both endpoints without changing them and reports what a
|
|
// migration would transfer.
|
|
func (e Engine) Inspect(ctx context.Context, c Config) (*Inspection, error) {
|
|
return e.access().Inspect(ctx, c, e.Exec)
|
|
}
|
|
|
|
// Round describes one snapshot transfer handed to a RoundTransport.
|
|
// BaseName and BaseSourcePath name the last completed snapshot, if any,
|
|
// which allows an incremental transfer. SkipUnchanged permits skipping
|
|
// subtrees whose recursive ctime did not change since the base.
|
|
type Round struct {
|
|
SourcePath, TargetPath string
|
|
BaseName, BaseSourcePath string
|
|
Name, StatePath string
|
|
SkipUnchanged bool
|
|
}
|
|
|
|
// RoundTransport is a Transport that keeps state across a snapshot round,
|
|
// such as the native backend's on-disk index.
|
|
type RoundTransport interface {
|
|
BeginRound(context.Context, Round) error
|
|
Published(string)
|
|
Commit(context.Context, string) error
|
|
Statistics() TransferStats
|
|
}
|
|
|
|
// TransferStats summarizes the work done for one snapshot round.
|
|
type TransferStats struct {
|
|
Mode string `json:"mode"`
|
|
BaseSnapshot string `json:"base_snapshot,omitempty"`
|
|
FilesVisited int64 `json:"files_visited"`
|
|
FilesCopied int64 `json:"files_copied"`
|
|
BytesCopied int64 `json:"bytes_copied"`
|
|
BytesVerified int64 `json:"bytes_verified"`
|
|
BlockDiffFiles int64 `json:"blockdiff_files"`
|
|
SnapDiffEntries int64 `json:"snapdiff_entries"`
|
|
SkippedSubtrees int64 `json:"skipped_subtrees,omitempty"`
|
|
// Seconds is the duration of the round from its start to the commit,
|
|
// including verification and snapshot creation.
|
|
Seconds float64 `json:"seconds"`
|
|
}
|
|
|
|
// NewEngine returns an Engine for c.Backend and a Closer that releases its
|
|
// connections. It never silently falls back from native I/O to mounted/rsync I/O.
|
|
func NewEngine(ctx context.Context, c Config, x Executor, log io.Writer) (Engine, io.Closer, error) {
|
|
if c.Backend == "native" {
|
|
return openNativeEngine(ctx, c, x, log)
|
|
}
|
|
|
|
return Engine{Exec: x, Log: log, Transport: Rsync{Exec: x, Log: log, BandwidthKiB: c.BandwidthKiB}}, nopCloser{}, nil
|
|
}
|
|
|
|
type nopCloser struct{}
|
|
|
|
func (nopCloser) Close() error { return nil }
|