mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* mount: leave -cacheMinFreeSpace free on the chunk cache's disk weed mount sizes its on-disk chunk cache by -cacheCapacityMB alone and preallocates each cache file in full when it creates it. With a capacity larger than the free space of the disk holding -cacheDir, the cache takes that disk down to nothing: the preallocations run until fallocate fails, and chunk writes fill the rest. The mount's metadata cache (a leveldb store under -cacheDir) and the swap files of dirty pages (-cacheDirWrite, which defaults to -cacheDir) live on the same disk, so once it is full their writes fail with ENOSPC, and writes whose dirty pages spill to swap return EIO to the application. The same happens when several mounts share a cache disk and together are configured beyond it. -cacheMinFreeSpace is the free space the cache leaves on that disk, written like the volume server's -minFreeSpace: a percentage like 1 or a size like 10GiB, 1 by default, as for volume servers. - When the cache opens, its disk layers shrink to the space above the floor, counting the cache files already in the directory as the cache's own. With no room the disk tier keeps no files. - Each new cache file preallocates only what the disk holds above the floor at that moment, so the files of one cache, or the caches of several mounts opening together, stop reserving at the floor. - A chunk write that lands in blocks its file already holds, preallocated or written, goes ahead as before, even with the disk under the floor. A write that takes new disk space, into a file a rotation truncated or where nothing could be preallocated, goes ahead only while the space above the floor covers it. The disk is read at most once a second, and the cache counts what it takes in between, so it cannot run past the floor between readings. A rotation gives the truncated file's blocks back, so the write after it reads the disk again. A chunk the cache cannot place is not cached, like any other miss. The webdav server's cache passes no floor and behaves as before. * mount: read the cache disk on every write that takes space, leave the index files room Review follow-ups. - take reads the disk each time, instead of once a second less what the cache took since, so a write sees what other mounts sharing the disk wrote, and what a rotation gave back. It runs only for writes past the blocks a file already holds, and a statfs costs little next to the write. - The index files (.idx and the leveldb maps) grow by a few dozen bytes a chunk beside the data files that the cache size counts. Sizing leaves them a sixteenth of the room above the floor. - The cache's own files count as room by the blocks they hold, so a file that was preallocated but not yet written when the mount exited counts in full when the cache reopens. - weed fuse checks -o cacheMinFreeSpace in the parent as well: the parent waits for the child's signal, and a child that rejects the value never sends it. * mount: keep the resolveCacheDirs doc comment on resolveCacheDirs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: say -cacheMinFreeSpace stops new disk space, not every chunk Under the floor the cache still fills the blocks its files already hold. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.6 KiB
Go
148 lines
4.6 KiB
Go
package chunk_cache
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
// diskStatusOf reads the disk holding the cache directory. It is a variable
|
|
// so a test can stand in for a disk that is full or near its floor.
|
|
var diskStatusOf = stats.DiskUsage
|
|
|
|
// diskSpaceGuard keeps the disk cache from taking the disk that holds it
|
|
// under a minimum free space, which the mount's metadata cache and write swap
|
|
// files share with it. It sizes the cache to fit above that floor when the
|
|
// cache opens, lets each new cache file preallocate only what the disk holds
|
|
// above the floor, and lets a chunk write take new disk space only while the
|
|
// space above the floor covers it. A nil guard limits nothing.
|
|
type diskSpaceGuard struct {
|
|
dir string
|
|
minFreeSpace util.MinFreeSpace
|
|
|
|
// Guarded by the TieredChunkCache write lock.
|
|
refusing bool
|
|
warnedAt time.Time
|
|
}
|
|
|
|
func newDiskSpaceGuard(dir string, minFreeSpace *util.MinFreeSpace) *diskSpaceGuard {
|
|
if minFreeSpace == nil {
|
|
return nil
|
|
}
|
|
return &diskSpaceGuard{dir: dir, minFreeSpace: *minFreeSpace}
|
|
}
|
|
|
|
var errDiskSizeUnknown = errors.New("the platform does not report disk sizes")
|
|
|
|
// available is how many more bytes the disk holds above the floor. When the
|
|
// disk cannot be read the caller limits nothing.
|
|
func (g *diskSpaceGuard) available() (uint64, error) {
|
|
status, err := diskStatusOf(g.dir)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if status.All == 0 {
|
|
return 0, errDiskSizeUnknown
|
|
}
|
|
return g.minFreeSpace.AvailableSpace(status.Free, status.All), nil
|
|
}
|
|
|
|
// capacity cuts a disk cache of size bytes to what fits above the floor: the
|
|
// free space over it, plus what the cache's files in dir already hold, since
|
|
// a cache reopened after an unclean exit reuses them. A sixteenth of that is
|
|
// left to the index files (.idx and the leveldb maps), which grow by a few
|
|
// dozen bytes a chunk beside the data files the size counts.
|
|
func (g *diskSpaceGuard) capacity(size int64) int64 {
|
|
if g == nil || size <= 0 {
|
|
return size
|
|
}
|
|
available, err := g.available()
|
|
if err != nil {
|
|
glog.Warningf("chunk cache %s: keeping no free space on the disk, cannot read it: %v", g.dir, err)
|
|
return size
|
|
}
|
|
budget := available + cacheFilesSize(g.dir)
|
|
budget -= budget / 16
|
|
if uint64(size) <= budget {
|
|
return size
|
|
}
|
|
glog.Warningf("chunk cache %s: %s on disk instead of %s, to keep %s of the disk free",
|
|
g.dir, util.BytesToHumanReadable(budget), util.BytesToHumanReadable(uint64(size)), g.minFreeSpace.String())
|
|
return int64(budget)
|
|
}
|
|
|
|
// preallocation is how much of want a new cache file may reserve: no more
|
|
// than the disk holds above the floor now, so the files of one cache, or of
|
|
// several caches opening on the same disk, stop reserving at the floor.
|
|
func (g *diskSpaceGuard) preallocation(want int64) int64 {
|
|
if g == nil || want <= 0 {
|
|
return want
|
|
}
|
|
available, err := g.available()
|
|
if err != nil || uint64(want) <= available {
|
|
return want
|
|
}
|
|
return int64(available)
|
|
}
|
|
|
|
// take reports whether a chunk write may take n more bytes of the disk and
|
|
// leave the floor free. It reads the disk each time, so it sees what other
|
|
// mounts sharing the disk wrote, and what a rotation gave back. The caller
|
|
// holds the cache's write lock.
|
|
func (g *diskSpaceGuard) take(n int64) bool {
|
|
if g == nil || n <= 0 {
|
|
return true
|
|
}
|
|
available, err := g.available()
|
|
if err != nil {
|
|
return true // the disk cannot be read: limit nothing
|
|
}
|
|
ok := available >= uint64(n)
|
|
if ok == g.refusing {
|
|
g.refusing = !ok
|
|
if ok {
|
|
glog.V(1).Infof("chunk cache %s: taking disk space again", g.dir)
|
|
} else if now := time.Now(); now.Sub(g.warnedAt) >= time.Minute {
|
|
g.warnedAt = now
|
|
glog.Warningf("chunk cache %s: taking no more disk space, %s left above the %s of the disk kept free",
|
|
g.dir, util.BytesToHumanReadable(available), g.minFreeSpace.String())
|
|
}
|
|
}
|
|
return ok
|
|
}
|
|
|
|
// cacheFilesSize adds up the disk the files of the disk layers already in dir
|
|
// hold, preallocated blocks past their ends included.
|
|
func cacheFilesSize(dir string) (size uint64) {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
for _, entry := range entries {
|
|
if !entry.Type().IsRegular() || !isDiskLayerFile(entry.Name()) {
|
|
continue
|
|
}
|
|
if info, err := entry.Info(); err == nil {
|
|
size += uint64(max(info.Size(), allocatedSizeOf(info)))
|
|
}
|
|
}
|
|
return size
|
|
}
|
|
|
|
func isDiskLayerFile(name string) bool {
|
|
if !strings.HasSuffix(name, ".dat") && !strings.HasSuffix(name, ".idx") {
|
|
return false
|
|
}
|
|
for _, layerName := range diskLayerNames {
|
|
if strings.HasPrefix(name, layerName+"_") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|