fix(native): replace the writing client before verification
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>
This commit is contained in:
1 parent
c655318c47
commit
e2c4549ade
7 files changed
+528
-109
No files matched your search
@@ -0,0 +1,302 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
||||
)
|
||||
|
||||
// diskFS is a read-only storage.FS over the local file system. Stat reports
|
||||
// the mtime as ctime and leaves the other times zero, so two copies with
|
||||
// equal mtimes compare as equal metadata. Symlinks report no times.
|
||||
type diskFS struct{ storage.FS }
|
||||
|
||||
func (diskFS) Stat(p string) (storage.Node, error) {
|
||||
var s syscall.Stat_t
|
||||
if err := syscall.Lstat(p, &s); err != nil {
|
||||
return storage.Node{}, err
|
||||
}
|
||||
|
||||
mtime := time.Unix(s.Mtim.Sec, s.Mtim.Nsec)
|
||||
if s.Mode&syscall.S_IFMT == syscall.S_IFLNK {
|
||||
mtime = time.Time{} // os.Chtimes cannot set symlink times.
|
||||
}
|
||||
|
||||
return storage.Node{Mode: s.Mode, Inode: s.Ino, Nlink: uint64(s.Nlink), Size: uint64(s.Size),
|
||||
UID: s.Uid, GID: s.Gid, Mtime: mtime, Ctime: mtime}, nil
|
||||
}
|
||||
|
||||
func (diskFS) OpenDir(p string) (storage.DirectoryStream, error) {
|
||||
d, err := os.Open(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &diskDir{file: d, path: p}, nil
|
||||
}
|
||||
|
||||
func (diskFS) Open(p string, _ int, _ uint32) (storage.File, error) {
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return diskFile{f}, nil
|
||||
}
|
||||
|
||||
func (diskFS) Readlink(p string) (string, error) { return os.Readlink(p) }
|
||||
func (diskFS) ListXattr(string) ([]string, error) { return nil, nil }
|
||||
func (diskFS) GetXattr(string, string) ([]byte, error) { return nil, syscall.ENODATA }
|
||||
|
||||
type diskFile struct{ *os.File }
|
||||
|
||||
func (diskFile) Lock(uint64) error { return nil }
|
||||
func (diskFile) PunchHole(int64, int64) error { return syscall.ENOTSUP }
|
||||
|
||||
type diskDir struct {
|
||||
file *os.File
|
||||
path string
|
||||
}
|
||||
|
||||
func (d *diskDir) Next() (*storage.Entry, error) {
|
||||
es, err := d.file.ReadDir(1)
|
||||
if len(es) == 0 {
|
||||
if err == nil {
|
||||
err = io.EOF
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
n, err := (diskFS{}).Stat(filepath.Join(d.path, es[0].Name()))
|
||||
return &storage.Entry{Name: es[0].Name(), Node: n}, err
|
||||
}
|
||||
|
||||
func (d *diskDir) Close() error { return d.file.Close() }
|
||||
|
||||
// verifyTree builds the same tree under src and dst: files spread over
|
||||
// directories, one hardlink pair and one symlink.
|
||||
func verifyTree(t *testing.T, src, dst string, files int) {
|
||||
t.Helper()
|
||||
stamp := time.Unix(1700000000, 0)
|
||||
for _, root := range []string{src, dst} {
|
||||
for i := 0; i < files; i++ {
|
||||
rel := filepath.Join(fmt.Sprintf("d%02d", i%7), fmt.Sprintf("f%04d", i))
|
||||
if err := os.MkdirAll(filepath.Join(root, filepath.Dir(rel)), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
p := filepath.Join(root, rel)
|
||||
if err := os.WriteFile(p, []byte(fmt.Sprintf("payload %d", i)), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.Chtimes(p, stamp, stamp.Add(time.Duration(i)*time.Second)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := os.Link(filepath.Join(root, "d00", "f0000"), filepath.Join(root, "hard")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.Symlink("d00/f0000", filepath.Join(root, "soft")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Directory mtimes changed while their entries were created.
|
||||
for _, root := range []string{src, dst} {
|
||||
if err := filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
|
||||
if err != nil || !d.IsDir() {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Chtimes(p, stamp, stamp)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verifyIndex records the inventory of src in a fresh index, as the copy
|
||||
// phase does: every path, the canonical path of every inode and the files
|
||||
// copied in full.
|
||||
func verifyIndex(t *testing.T, src string) *nativeIndex {
|
||||
t.Helper()
|
||||
x, err := createNativeIndex(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Cleanup(func() { x.close() })
|
||||
err = filepath.WalkDir(src, func(p string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(src, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, err := (diskFS{}).Stat(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = x.put("paths", []byte(rel), encodeEntry(baseEntry{Inode: n.Inode})); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if n.Kind() != storage.Regular {
|
||||
return nil
|
||||
}
|
||||
|
||||
if old, err := x.get("groups", nativeInodeKey(n.Inode)); err != nil || old != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = x.put("groups", nativeInodeKey(n.Inode), []byte(rel)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return x.put("full", []byte(rel), []byte{1})
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := x.put("meta", []byte("inventory_complete"), []byte{1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
func TestNativeVerifyParallel(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
damage func(t *testing.T, dst string)
|
||||
want string
|
||||
}{
|
||||
{"identical", nil, ""},
|
||||
{"content", func(t *testing.T, dst string) {
|
||||
p := filepath.Join(dst, "d03", "f0150")
|
||||
info, err := os.Stat(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(p, []byte("payload 15X"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.Chtimes(p, info.ModTime(), info.ModTime()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.Chtimes(filepath.Dir(p), time.Unix(1700000000, 0), time.Unix(1700000000, 0)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}, "content mismatch"},
|
||||
{"split hardlink", func(t *testing.T, dst string) {
|
||||
p := filepath.Join(dst, "hard")
|
||||
info, err := os.Stat(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.Remove(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(p, data, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, x := range []string{p, dst} {
|
||||
mtime := info.ModTime()
|
||||
if x == dst {
|
||||
mtime = time.Unix(1700000000, 0)
|
||||
}
|
||||
|
||||
if err := os.Chtimes(x, mtime, mtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}, "hardlink group was split"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
src, dst := t.TempDir(), t.TempDir()
|
||||
verifyTree(t, src, dst, 400)
|
||||
if tc.damage != nil {
|
||||
tc.damage(t, dst)
|
||||
}
|
||||
|
||||
b := nativeTransport{source: diskFS{}, target: diskFS{}, pending: verifyIndex(t, src),
|
||||
workspace: t.TempDir(), config: Config{Workers: 16},
|
||||
round: Round{SourcePath: src, TargetPath: dst}, verified: map[string]bool{}}
|
||||
err := b.Verify(context.Background(), src, dst)
|
||||
if tc.want == "" {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !b.verified[dst] || b.checked.Load() == 0 {
|
||||
t.Fatalf("verified=%v checked=%d", b.verified[dst], b.checked.Load())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("error %v; want %q", err, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// closingFS records whether Close was called.
|
||||
type closingFS struct {
|
||||
diskFS
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (f *closingFS) Close() error { f.closed = true; return nil }
|
||||
|
||||
func TestNativeVerifyReplacesWriter(t *testing.T) {
|
||||
src, dst := t.TempDir(), t.TempDir()
|
||||
verifyTree(t, src, dst, 20)
|
||||
writer := &closingFS{}
|
||||
var opened []*closingFS
|
||||
b := nativeTransport{source: diskFS{}, target: writer, pending: verifyIndex(t, src),
|
||||
workspace: t.TempDir(), round: Round{SourcePath: src, TargetPath: dst}, verified: map[string]bool{},
|
||||
openTarget: func() (storage.FS, error) {
|
||||
f := &closingFS{}
|
||||
opened = append(opened, f)
|
||||
return f, nil
|
||||
}}
|
||||
|
||||
if err := b.Verify(context.Background(), src, dst); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !writer.closed || len(opened) != 1 || b.target != storage.FS(opened[0]) || opened[0].closed {
|
||||
t.Fatalf("writer closed=%v, opened=%d, target replaced=%v", writer.closed, len(opened),
|
||||
len(opened) == 1 && b.target == storage.FS(opened[0]))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user