mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
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>
172 lines
5.6 KiB
Go
172 lines
5.6 KiB
Go
package mount
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/go-fuse/v2/fuse"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
const blockSize = 512
|
|
|
|
// statsRefreshInterval is how old the stats may be before StatFs asks the
|
|
// filer for new ones.
|
|
const statsRefreshInterval = 20 * time.Second
|
|
|
|
type statsCache struct {
|
|
sync.Mutex
|
|
filer_pb.StatisticsResponse
|
|
lastChecked int64 // unix time in seconds the last refresh started
|
|
refreshed bool // a refresh has finished, answered or not
|
|
refreshing chan struct{} // closed when the refresh in flight finishes; nil if none
|
|
}
|
|
|
|
// diskSizes reports the sizes df and the quota work from. By default that is
|
|
// the space the cluster gives up to the data, counting every replica and every
|
|
// EC shard. Under -df.logical it is the data itself, with the free space
|
|
// converted to how much more of it the mount's replication setting allows.
|
|
func (wfs *WFS) diskSizes() (totalSize, usedSize uint64) {
|
|
wfs.stats.Lock()
|
|
defer wfs.stats.Unlock()
|
|
// a filer older than the logical sizes sends zeros, so keep reporting the
|
|
// raw ones rather than an empty filesystem
|
|
if wfs.option.LogicalDiskUsage && wfs.stats.LogicalTotalSize > 0 {
|
|
return wfs.stats.LogicalTotalSize, wfs.stats.LogicalUsedSize
|
|
}
|
|
return wfs.stats.TotalSize, wfs.stats.UsedSize
|
|
}
|
|
|
|
// refreshStats starts a refresh of the stats from the filer when they are
|
|
// older than statsRefreshInterval and no refresh is in flight. It returns a
|
|
// channel to wait on until the first refresh has finished, and nil after.
|
|
func (wfs *WFS) refreshStats() <-chan struct{} {
|
|
wfs.stats.Lock()
|
|
defer wfs.stats.Unlock()
|
|
now := time.Now().Unix()
|
|
if wfs.stats.refreshing == nil && wfs.stats.lastChecked < now-int64(statsRefreshInterval/time.Second) {
|
|
wfs.stats.lastChecked = now
|
|
wfs.stats.refreshing = make(chan struct{})
|
|
go wfs.fetchStats(wfs.stats.refreshing)
|
|
}
|
|
if wfs.stats.refreshed {
|
|
return nil
|
|
}
|
|
return wfs.stats.refreshing
|
|
}
|
|
|
|
// fetchStats asks the filer for the stats and closes done when it is through.
|
|
// When the filer does not answer, the last known stats stay.
|
|
func (wfs *WFS) fetchStats(done chan struct{}) {
|
|
request := &filer_pb.StatisticsRequest{
|
|
Collection: wfs.option.Collection,
|
|
Replication: wfs.option.Replication,
|
|
Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
|
|
DiskType: string(wfs.option.DiskType),
|
|
}
|
|
var resp *filer_pb.StatisticsResponse
|
|
err := wfs.withFilerClient(context.Background(), false, wfs.filerRPCWait(), func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
|
|
glog.V(4).Infof("reading filer stats: %+v", request)
|
|
var err error
|
|
resp, err = client.Statistics(ctx, request)
|
|
return err
|
|
})
|
|
|
|
wfs.stats.Lock()
|
|
if err == nil {
|
|
glog.V(4).Infof("read filer stats: %+v", resp)
|
|
wfs.stats.TotalSize = resp.TotalSize
|
|
wfs.stats.UsedSize = resp.UsedSize
|
|
wfs.stats.LogicalTotalSize = resp.LogicalTotalSize
|
|
wfs.stats.LogicalUsedSize = resp.LogicalUsedSize
|
|
wfs.stats.FileCount = resp.FileCount
|
|
} else {
|
|
// the last known sizes beat the empty filesystem a bare return reports
|
|
glog.V(0).Infof("reading filer stats %v: %v", request, err)
|
|
}
|
|
wfs.stats.refreshed = true
|
|
wfs.stats.refreshing = nil
|
|
wfs.stats.Unlock()
|
|
close(done)
|
|
}
|
|
|
|
func (wfs *WFS) StatFs(cancel <-chan struct{}, in *fuse.InHeader, out *fuse.StatfsOut) (code fuse.Status) {
|
|
|
|
// Answer from the stats at hand and refresh them in the background, so a
|
|
// slow filer does not hold statfs: a node agent that probes the mount with
|
|
// statfs would take the mount for hung. Only the first statfs waits, for
|
|
// the first refresh.
|
|
if first := wfs.refreshStats(); first != nil {
|
|
select {
|
|
case <-first:
|
|
case <-cancel:
|
|
return fuse.EINTR
|
|
}
|
|
}
|
|
|
|
totalDiskSize, usedDiskSize := wfs.diskSizes()
|
|
wfs.stats.Lock()
|
|
actualFileCount := wfs.stats.FileCount
|
|
wfs.stats.Unlock()
|
|
|
|
if wfs.option.Quota > 0 && totalDiskSize > uint64(wfs.option.Quota) {
|
|
totalDiskSize = uint64(wfs.option.Quota)
|
|
if usedDiskSize > totalDiskSize {
|
|
totalDiskSize = usedDiskSize
|
|
}
|
|
}
|
|
|
|
// http://man.he.net/man2/statfs
|
|
/*
|
|
struct statfs {
|
|
__fsword_t f_type; // Type of filesystem (see below)
|
|
__fsword_t f_bsize; // Optimal transfer block size
|
|
fsblkcnt_t f_blocks; // Total data blocks in filesystem
|
|
fsblkcnt_t f_bfree; // Free blocks in filesystem
|
|
fsblkcnt_t f_bavail; // Free blocks available to
|
|
unprivileged user
|
|
fsfilcnt_t f_files; // Total file nodes in filesystem
|
|
fsfilcnt_t f_ffree; // Free file nodes in filesystem
|
|
fsid_t f_fsid; // Filesystem ID
|
|
__fsword_t f_namelen; // Maximum length of filenames
|
|
__fsword_t f_frsize; // Fragment size (since Linux 2.6)
|
|
__fsword_t f_flags; // Mount flags of filesystem
|
|
(since Linux 2.6.36)
|
|
__fsword_t f_spare[xxx];
|
|
// Padding bytes reserved for future use
|
|
};
|
|
*/
|
|
|
|
// Compute the total number of available blocks
|
|
out.Blocks = totalDiskSize / blockSize
|
|
if out.Blocks <= 0 {
|
|
out.Blocks = 1
|
|
}
|
|
// Compute the number of used blocks
|
|
numBlocks := usedDiskSize / blockSize
|
|
|
|
remainingBlocks := int64(out.Blocks) - int64(numBlocks)
|
|
if remainingBlocks < 0 {
|
|
remainingBlocks = 0
|
|
}
|
|
|
|
// Report the number of free and available blocks for the block size
|
|
out.Bfree = uint64(remainingBlocks)
|
|
out.Bavail = uint64(remainingBlocks)
|
|
out.Bsize = uint32(blockSize)
|
|
|
|
// Report the total number of possible files in the file system (and those free)
|
|
out.Files = math.MaxInt64
|
|
out.Ffree = math.MaxInt64 - actualFileCount
|
|
|
|
// Report the maximum length of a name and the minimum fragment size
|
|
out.NameLen = 255
|
|
out.Frsize = uint32(blockSize)
|
|
|
|
return fuse.OK
|
|
}
|