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
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -332,19 +333,23 @@ func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if b.openVerifier != nil {
|
||||
v, err := b.openVerifier()
|
||||
if b.openTarget != nil {
|
||||
// Replace the writing client before reading the target. Its cache
|
||||
// may hold a ctime that differs from the MDS, and the MDS would have
|
||||
// to recall its caps on every entry it wrote last, which in the lab
|
||||
// cost about five seconds per lookup. Closing it returns all caps at
|
||||
// once. The engine calls transport methods one at a time, and the
|
||||
// claim lock is held by a client of its own.
|
||||
v, err := b.openTarget()
|
||||
if err != nil {
|
||||
return fmt.Errorf("native verification client: %w", err)
|
||||
}
|
||||
|
||||
// The engine calls transport methods one at a time.
|
||||
writer := b.target
|
||||
err = b.target.Close()
|
||||
b.target = v
|
||||
defer func() {
|
||||
b.target = writer
|
||||
v.Close()
|
||||
}()
|
||||
if err != nil {
|
||||
return fmt.Errorf("close native writing client: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
skipData := b.verified[to]
|
||||
@@ -354,7 +359,40 @@ func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
|
||||
}
|
||||
|
||||
defer scratch.cleanup()
|
||||
index := scratch.index
|
||||
v := &nativeVerification{b: b, to: to, skipData: skipData, index: scratch.index}
|
||||
|
||||
// The walk and the directories stay serial, so subtree skips apply in walk
|
||||
// order. Every other entry is checked by one of the workers.
|
||||
ctx, cancel := context.WithCancelCause(ctx)
|
||||
defer cancel(nil)
|
||||
workers := b.config.Workers
|
||||
if workers == 0 {
|
||||
workers = 8
|
||||
}
|
||||
|
||||
type entry struct {
|
||||
p, rel string
|
||||
n storage.Node
|
||||
}
|
||||
|
||||
jobs := make(chan entry, workers*4)
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < workers; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for job := range jobs {
|
||||
if ctx.Err() != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := v.entry(ctx, job.p, job.rel, job.n); err != nil {
|
||||
cancel(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
lastProgress := time.Now()
|
||||
var verifiedPaths uint64
|
||||
err = walkNative(ctx, b.source, from, func(p string, n storage.Node, _ int) error {
|
||||
@@ -369,101 +407,27 @@ func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := path.Join(to, rel)
|
||||
target, err := b.target.Stat(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = syncNativeMetadataKnown(b.source, b.target, p, targetPath, n, target, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if n.Kind() == storage.Directory {
|
||||
if err = v.entry(ctx, p, rel, n); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return b.skipped(rel)
|
||||
}
|
||||
|
||||
if n.Kind() != storage.Directory {
|
||||
if n.Nlink > 1 {
|
||||
same, err := index.observe("source", nodeKey(n.Inode), nodeKey(target.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
return fmt.Errorf("native hardlink group was split at %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
if target.Nlink > 1 {
|
||||
same, err := index.observe("target", nodeKey(target.Inode), nodeKey(n.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
return fmt.Errorf("native unrelated files became hardlinked at %s", rel)
|
||||
}
|
||||
}
|
||||
select {
|
||||
case jobs <- entry{p: p, rel: rel, n: n}:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return context.Cause(ctx)
|
||||
}
|
||||
|
||||
switch n.Kind() {
|
||||
case storage.Directory:
|
||||
case storage.Regular:
|
||||
if n.Size != target.Size {
|
||||
return fmt.Errorf("native size mismatch at %s", targetPath)
|
||||
}
|
||||
|
||||
canonical, err := b.pending.get("groups", nativeInodeKey(n.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !skipData && string(canonical) == rel {
|
||||
full, err := b.pending.get("full", []byte(rel))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if full != nil {
|
||||
if err = b.compareRange(ctx, p, targetPath, 0, n.Size); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = b.pending.ranges(rel, func(off, length uint64) error { return b.compareRange(ctx, p, targetPath, off, length) }); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only live target identities may be reused as the next blockdiff base.
|
||||
if to == b.round.TargetPath && !skipData {
|
||||
return b.pending.putEntryIfChanged(rel, baseEntry{Inode: n.Inode, Version: n.Version, Size: n.Size, TargetInode: target.Inode})
|
||||
}
|
||||
|
||||
case storage.Symlink:
|
||||
a, err := b.source.Readlink(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
v, err := b.target.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if a != v {
|
||||
return fmt.Errorf("native symlink mismatch at %s", rel)
|
||||
}
|
||||
|
||||
default:
|
||||
if n.Rdev != target.Rdev {
|
||||
return fmt.Errorf("native device mismatch at %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
close(jobs)
|
||||
wg.Wait()
|
||||
if cause := context.Cause(ctx); cause != nil && (err == nil || errors.Is(err, context.Canceled)) {
|
||||
err = cause
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -475,6 +439,120 @@ func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// nativeVerification holds the state of one Verify call that its workers
|
||||
// share.
|
||||
type nativeVerification struct {
|
||||
b *nativeTransport
|
||||
to string
|
||||
skipData bool
|
||||
mu sync.Mutex // guards index, which holds one write transaction
|
||||
index *linkIndex
|
||||
}
|
||||
|
||||
// observe records a hardlink relation in the scratch index.
|
||||
func (v *nativeVerification) observe(bucket string, key, value [16]byte) (bool, error) {
|
||||
v.mu.Lock()
|
||||
defer v.mu.Unlock()
|
||||
return v.index.observe(bucket, key, value)
|
||||
}
|
||||
|
||||
// entry checks one source entry p against its target copy. It is safe for
|
||||
// concurrent use.
|
||||
func (v *nativeVerification) entry(ctx context.Context, p, rel string, n storage.Node) error {
|
||||
b := v.b
|
||||
targetPath := path.Join(v.to, rel)
|
||||
target, err := b.target.Stat(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = syncNativeMetadataKnown(b.source, b.target, p, targetPath, n, target, true); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if n.Kind() == storage.Directory {
|
||||
return nil
|
||||
}
|
||||
|
||||
if n.Nlink > 1 {
|
||||
same, err := v.observe("source", nodeKey(n.Inode), nodeKey(target.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
return fmt.Errorf("native hardlink group was split at %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
if target.Nlink > 1 {
|
||||
same, err := v.observe("target", nodeKey(target.Inode), nodeKey(n.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
return fmt.Errorf("native unrelated files became hardlinked at %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
switch n.Kind() {
|
||||
case storage.Regular:
|
||||
if n.Size != target.Size {
|
||||
return fmt.Errorf("native size mismatch at %s", targetPath)
|
||||
}
|
||||
|
||||
canonical, err := b.pending.get("groups", nativeInodeKey(n.Inode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !v.skipData && string(canonical) == rel {
|
||||
full, err := b.pending.get("full", []byte(rel))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if full != nil {
|
||||
if err = b.compareRange(ctx, p, targetPath, 0, n.Size); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = b.pending.ranges(rel, func(off, length uint64) error { return b.compareRange(ctx, p, targetPath, off, length) }); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only live target identities may be reused as the next blockdiff base.
|
||||
if v.to == b.round.TargetPath && !v.skipData {
|
||||
return b.pending.putEntryIfChanged(rel, baseEntry{Inode: n.Inode, Version: n.Version, Size: n.Size, TargetInode: target.Inode})
|
||||
}
|
||||
|
||||
case storage.Symlink:
|
||||
a, err := b.source.Readlink(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t, err := b.target.Readlink(targetPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if a != t {
|
||||
return fmt.Errorf("native symlink mismatch at %s", rel)
|
||||
}
|
||||
|
||||
default:
|
||||
if n.Rdev != target.Rdev {
|
||||
return fmt.Errorf("native device mismatch at %s", rel)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *nativeTransport) verifyInventory(ctx context.Context, to string) error {
|
||||
if err := b.pending.requireInventory(); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user