Files
seaweedfs/weed/mount/weedfs_stats_test.go
T
Yip LeoandClaude Opus 5.5 71b1911e44 mount: answer statfs from the stats at hand, refresh them in the background (#13337)
StatFs asked the filer for Statistics whenever its stats were older than
20 s and waited for the answer, up to the 20 s filer RPC timeout, so a
slow filer or master held statfs for that long. A node agent that probes
mounts with a bounded statfs, as the CSI driver's health monitor does
with 5 s, took such a mount for hung and restarted it, on every node at
once when the filer was slow for all of them. Every concurrent statfs
also sent its own request while the stats were stale.

StatFs now answers from the stats it has and starts a refresh in the
background when they are older than 20 s, one at a time. Only the first
statfs waits, for the first refresh. A refresh the filer does not answer
keeps the last known stats, as before, and the next one starts 20 s after
it.

The stats are now read and written under a lock: the refresh and the
quota check write them while statfs and the quota check read them.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 10:39:23 -08:00

117 lines
3.5 KiB
Go

package mount
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestDiskSizes(t *testing.T) {
wfs := &WFS{option: &Option{}}
wfs.stats.TotalSize = 3000
wfs.stats.UsedSize = 2000
wfs.stats.LogicalTotalSize = 1500
wfs.stats.LogicalUsedSize = 1000
total, used := wfs.diskSizes()
if total != 3000 || used != 2000 {
t.Errorf("raw sizes: got %d/%d, want 3000/2000", total, used)
}
wfs.option.LogicalDiskUsage = true
total, used = wfs.diskSizes()
if total != 1500 || used != 1000 {
t.Errorf("logical sizes: got %d/%d, want 1500/1000", total, used)
}
// a filer that does not report logical sizes must not read as an empty
// filesystem
wfs.stats.LogicalTotalSize = 0
wfs.stats.LogicalUsedSize = 0
total, used = wfs.diskSizes()
if total != 3000 || used != 2000 {
t.Errorf("sizes from a filer without logical support: got %d/%d, want 3000/2000", total, used)
}
}
// statsFiler answers Statistics with total as the total size, once release is
// closed if it is set.
type statsFiler struct {
filer_pb.UnimplementedSeaweedFilerServer
calls atomic.Int32
total uint64
release chan struct{}
}
func (f *statsFiler) Statistics(ctx context.Context, req *filer_pb.StatisticsRequest) (*filer_pb.StatisticsResponse, error) {
f.calls.Add(1)
if f.release != nil {
select {
case <-f.release:
case <-ctx.Done():
return nil, ctx.Err()
}
}
return &filer_pb.StatisticsResponse{TotalSize: f.total}, nil
}
func statfsBlocks(t *testing.T, wfs *WFS) uint64 {
t.Helper()
var out fuse.StatfsOut
if status := wfs.StatFs(nil, &fuse.InHeader{}, &out); status != fuse.OK {
t.Fatalf("statfs: %v", status)
}
return out.Blocks
}
// The first statfs waits for the first stats from the filer.
func TestStatFsWaitsForTheFirstStats(t *testing.T) {
filer := &statsFiler{total: 1 << 30}
wfs := newPagingWFS(t, util.FullPath("/"), nil, 0)
startFakeFiler(t, wfs, filer)
if blocks := statfsBlocks(t, wfs); blocks != (1<<30)/blockSize {
t.Fatalf("statfs reported %d blocks, want the filer's %d", blocks, (1<<30)/blockSize)
}
}
// Once it has stats, statfs answers from them while the filer is slow to give
// newer ones, and asks the filer once, not once per statfs.
func TestStatFsDoesNotWaitForASlowFiler(t *testing.T) {
filer := &statsFiler{total: 1 << 30, release: make(chan struct{})}
wfs := newPagingWFS(t, util.FullPath("/"), nil, 0)
startFakeFiler(t, wfs, filer)
wfs.filerCallTimeout = 3 * time.Second
// Stats from an earlier refresh, old enough to be refreshed.
wfs.stats.TotalSize = 1 << 20
wfs.stats.refreshed = true
start := time.Now()
for i := 0; i < 3; i++ {
if blocks := statfsBlocks(t, wfs); blocks != (1<<20)/blockSize {
t.Fatalf("statfs %d reported %d blocks, want the last known %d", i, blocks, (1<<20)/blockSize)
}
}
if took := time.Since(start); took > 2*time.Second {
t.Fatalf("three statfs took %v: they waited for the filer", took)
}
waitUntil(t, "the refresh to reach the filer", func() bool { return filer.calls.Load() > 0 })
close(filer.release)
waitUntil(t, "the refreshed stats", func() bool {
total, _ := wfs.diskSizes()
return total == 1<<30
})
if blocks := statfsBlocks(t, wfs); blocks != (1<<30)/blockSize {
t.Fatalf("statfs after the refresh reported %d blocks, want %d", blocks, (1<<30)/blockSize)
}
if calls := filer.calls.Load(); calls != 1 {
t.Fatalf("%d stats requests reached the filer, want 1", calls)
}
}