Initial import of the existing CephFS subvolume migration prototype, including the native libcephfs transport, rsync backend, local Ceph lab environment and integration tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Transport interface {
|
|
Copy(context.Context, string, string) error
|
|
Verify(context.Context, string, string) error
|
|
}
|
|
|
|
type Rsync struct {
|
|
Exec Executor
|
|
Log io.Writer
|
|
BandwidthKiB int64
|
|
}
|
|
|
|
func (r Rsync) args(src, dst string) []string {
|
|
a := []string{"-aHAXS", "--numeric-ids", "--checksum", "--no-whole-file",
|
|
"--delete-during", "--partial", "--fsync", "--one-file-system",
|
|
"--filter=- .snap/", "--filter=-x ceph.*", "--filter=-x system.*"}
|
|
if r.BandwidthKiB > 0 {
|
|
a = append(a, "--bwlimit="+strconv.FormatInt(r.BandwidthKiB, 10))
|
|
}
|
|
return append(a, "--", src+"/", dst+"/")
|
|
}
|
|
func (r Rsync) Copy(ctx context.Context, src, dst string) error {
|
|
for attempt := 0; attempt < 2; attempt++ {
|
|
err := r.Exec.Exec(ctx, r.Log, "rsync", r.args(src, dst)...)
|
|
var processError *ProcessError
|
|
var exitError *exec.ExitError
|
|
// EEXIST was observed on Ceph-FUSE while rsync rebuilt a hardlink group.
|
|
// One idempotent retry repairs the partial group. All other errors
|
|
// are returned immediately; publication still requires full verification.
|
|
if attempt == 0 && errors.As(err, &processError) && errors.As(err, &exitError) && exitError.ExitCode() == 23 && strings.Contains(processError.Diagnostic, "link ") && strings.Contains(processError.Diagnostic, "File exists (17)") {
|
|
if r.Log != nil {
|
|
fmt.Fprintln(r.Log, "rsync hardlink EEXIST: retrying once before snapshot verification")
|
|
}
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
return errors.New("rsync retry limit reached")
|
|
}
|
|
|
|
type differences struct{ n int64 }
|
|
|
|
func (d *differences) Write(p []byte) (int, error) { d.n += int64(len(p)); return len(p), nil }
|
|
|
|
func (r Rsync) Verify(ctx context.Context, src, dst string) error {
|
|
args := append([]string{"--dry-run", "--itemize-changes", "--out-format=%i"}, r.args(src, dst)...)
|
|
var d differences
|
|
if err := r.Exec.Exec(ctx, &d, "rsync", args...); err != nil {
|
|
return err
|
|
}
|
|
if d.n != 0 {
|
|
return fmt.Errorf("verification found differences between %s and %s", src, dst)
|
|
}
|
|
return nil
|
|
}
|