Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Transport copies a snapshot tree to the target and verifies the result.
|
|
type Transport interface {
|
|
Copy(context.Context, string, string) error
|
|
Verify(context.Context, string, string) error
|
|
}
|
|
|
|
// Rsync is the Transport that copies between mounted CephFS trees with
|
|
// rsync. BandwidthKiB limits the transfer rate when positive.
|
|
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+"/")
|
|
}
|
|
|
|
// Copy mirrors src into dst, including hard links, ACLs and xattrs.
|
|
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
|
|
}
|
|
|
|
// Verify runs rsync as a dry run and fails if any difference remains.
|
|
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
|
|
}
|