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 }