//go:build linux package fuse_handover import ( "fmt" "os" "path/filepath" "regexp" "strconv" "sync" "sync/atomic" "testing" "time" "github.com/stretchr/testify/require" ) var stateBytesRe = regexp.MustCompile(`state_bytes=(\d+)`) // TestHandoverPauseByInodes measures the pause of a handover against the // number of inodes the kernel holds: the inode table is the bulk of the state // that crosses. Creating the files takes minutes, so it only runs with // HANDOVER_SCALE_FILES=. func TestHandoverPauseByInodes(t *testing.T) { files, _ := strconv.Atoi(os.Getenv("HANDOVER_SCALE_FILES")) if files <= 0 { t.Skip("set HANDOVER_SCALE_FILES= to run") } c := startCluster(t) m := newMountSetup(c) a := c.start(m.name(), m.env, m.args()...) require.NoError(t, waitMounted(a, m.mnt, 60*time.Second)) _, err := a.waitLog("handover: listening on", 30*time.Second) require.NoError(t, err) waitWritable(t, m.mnt) // Empty files in directories of 1000. The kernel keeps the inode of each // file it created, so the mount has one table entry per file. const perDir, workers = 1000, 16 dirs := (files + perDir - 1) / perDir start := time.Now() var next, failed atomic.Int64 var wg sync.WaitGroup for w := 0; w < workers; w++ { wg.Add(1) go func() { defer wg.Done() for { d := int(next.Add(1)) - 1 if d >= dirs { return } dir := filepath.Join(m.mnt, fmt.Sprintf("scale/d%05d", d)) if err := os.MkdirAll(dir, 0o755); err != nil { failed.Add(1) return } for i := 0; i < perDir && d*perDir+i < files; i++ { f, err := os.Create(filepath.Join(dir, fmt.Sprintf("f%04d", i))) if err != nil { failed.Add(1) return } f.Close() } } }() } wg.Wait() require.Zero(t, failed.Load(), "creating the files") t.Logf("created %d files in %d directories in %v", files, dirs, time.Since(start).Round(time.Millisecond)) b, pause := m.takeover(t, a) line, err := b.waitLog("handover: serving", 30*time.Second) require.NoError(t, err) stateBytes := int64(0) if sm := stateBytesRe.FindStringSubmatch(line); sm != nil { stateBytes, _ = strconv.ParseInt(sm[1], 10, 64) } t.Logf("%d files: pause %v, state %d bytes (%.1f bytes per file)", files, pause, stateBytes, float64(stateBytes)/float64(files)) // The new process resolves the inodes the kernel still holds: every file // of a few directories stats without a lookup failure. for _, d := range []int{0, dirs / 2, dirs - 1} { entries, err := os.ReadDir(filepath.Join(m.mnt, fmt.Sprintf("scale/d%05d", d))) require.NoError(t, err) require.NotEmpty(t, entries) for _, e := range entries { _, err := e.Info() require.NoError(t, err, "stat %s after the handover", e.Name()) } } terminate(b) if _, ok := mountAt(m.mnt); ok { unmountLazily(m.mnt) } require.Eventually(t, func() bool { _, ok := mountAt(m.mnt); return !ok }, 20*time.Second, 100*time.Millisecond) }