reflector-cleaner/main.go

167 lines
4 KiB
Go
Raw Normal View History

2020-05-14 01:15:14 +02:00
package main
import (
2020-05-14 03:09:56 +02:00
"fmt"
"io/ioutil"
2020-05-14 01:15:14 +02:00
"os"
2020-05-14 03:09:56 +02:00
"path/filepath"
"runtime"
2020-05-14 01:15:14 +02:00
"sort"
"strconv"
2020-05-14 03:09:56 +02:00
"sync"
2020-05-14 01:15:14 +02:00
"sync/atomic"
"syscall"
"time"
"reflector-cleaner/atime"
"github.com/google/gops/agent"
"github.com/karrick/godirwalk"
"github.com/lbryio/lbry.go/v2/extras/errors"
"github.com/sirupsen/logrus"
)
var blobsDir string
func main() {
if err := agent.Listen(agent.Options{}); err != nil {
panic(err)
}
if len(os.Args) != 2 {
panic("you must pass 1 argument: the path of the blobs directory")
}
blobsDir = os.Args[1]
thresholdStr := os.Getenv("DISK_THRESHOLD")
threshold := 0.90
var err error
if thresholdStr != "" {
threshold, err = strconv.ParseFloat(thresholdStr, 64)
if err != nil {
panic(err)
}
}
if _, err := os.Stat(blobsDir); os.IsNotExist(err) {
panic(errors.Err("directory doesn't exist: %s", blobsDir))
}
2020-05-14 03:09:56 +02:00
used, _, err := getUsedSpace()
2020-05-14 01:15:14 +02:00
if err != nil {
logrus.Errorln(err.Error())
return
}
logrus.Infof("disk usage: %.2f%%\n", used*100)
if used > threshold {
logrus.Infof("over %.2f%%, cleaning up", threshold*100)
err = WipeOldestBlobs()
if err != nil {
logrus.Errorln(err.Error())
return
}
2020-05-14 03:09:56 +02:00
usage, _, err := getUsedSpace()
2020-05-14 01:15:14 +02:00
if err != nil {
logrus.Errorln(err.Error())
return
}
2020-05-14 03:09:56 +02:00
logrus.Infof("disk usage: %.2f%%\n", usage*100)
2020-05-14 01:15:14 +02:00
logrus.Infoln("Done cleaning up")
}
}
// GetUsedSpace returns a value between 0 and 1, with 0 being completely empty and 1 being full, for the disk that holds the provided path
2020-05-14 03:09:56 +02:00
func getUsedSpace() (usage float64, used uint64, err error) {
2020-05-14 01:15:14 +02:00
var stat syscall.Statfs_t
2020-05-14 03:09:56 +02:00
err = syscall.Statfs(blobsDir, &stat)
2020-05-14 01:15:14 +02:00
if err != nil {
2020-05-14 03:09:56 +02:00
return 0, 0, err
2020-05-14 01:15:14 +02:00
}
// Available blocks * size per block = available space in bytes
all := stat.Blocks * uint64(stat.Bsize)
free := stat.Bfree * uint64(stat.Bsize)
2020-05-14 03:09:56 +02:00
used = all - free
2020-05-14 01:15:14 +02:00
2020-05-14 03:09:56 +02:00
return float64(used) / float64(all), used, nil
2020-05-14 01:15:14 +02:00
}
func WipeOldestBlobs() (err error) {
type datedFile struct {
Atime time.Time
FullPath string
}
datedFiles := make([]datedFile, 0, 5000)
checkedBlobs := int32(0)
2020-05-14 03:09:56 +02:00
dirs, err := ioutil.ReadDir(blobsDir)
2020-05-14 01:15:14 +02:00
if err != nil {
return err
}
2020-05-14 03:09:56 +02:00
lock := &sync.Mutex{}
wg := &sync.WaitGroup{}
startTime := time.Now()
usage, used, err := getUsedSpace()
if err != nil {
logrus.Errorln(err.Error())
return
}
for i, dir := range dirs {
if (i+1)%(runtime.NumCPU()-1) == 0 {
wg.Wait()
}
wg.Add(1)
go func(dir string, lock *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()
err = godirwalk.Walk(filepath.Join(blobsDir, dir), &godirwalk.Options{
Callback: func(osPathname string, de *godirwalk.Dirent) error {
if !de.IsDir() {
i := atomic.AddInt32(&checkedBlobs, 1)
if i%100 == 0 {
logrus.Infof("checked %d blobs", i)
}
if i%100 == 0 {
processedSize := float64(i * 2)
speed := processedSize / time.Since(startTime).Seconds()
eta, err := time.ParseDuration(fmt.Sprintf("%ds", int(float64(used)/1024./1024./speed)))
if err != nil {
return err
}
logrus.Infof("%.2f GB checked (speed: %.2f MB/s) ETA: %s", processedSize/1024.0, speed, eta.String())
}
if de.IsRegular() {
stat, err := os.Stat(osPathname)
if err != nil {
return err
}
lock.Lock()
datedFiles = append(datedFiles, datedFile{
Atime: atime.Atime(stat),
FullPath: osPathname,
})
lock.Unlock()
}
}
return nil
},
Unsorted: true, // (optional) set true for faster yet non-deterministic enumeration (see godoc)
})
if err != nil {
logrus.Errorf(errors.FullTrace(err))
}
}(dir.Name(), lock, wg)
}
wg.Wait()
2020-05-14 01:15:14 +02:00
sort.Slice(datedFiles, func(i, j int) bool {
return datedFiles[i].Atime.Before(datedFiles[j].Atime)
})
2020-05-14 03:09:56 +02:00
spaceToFreeUp := ((float64(used) / usage) * 0.1) / 1024. / 1024.
blobsToDelete := int(spaceToFreeUp / 2)
logrus.Infof("spaceToFreeUp: %.2f MB - %d blobs", spaceToFreeUp, blobsToDelete)
2020-05-14 01:15:14 +02:00
for i, df := range datedFiles {
2020-05-14 03:09:56 +02:00
if i >= blobsToDelete {
2020-05-14 01:15:14 +02:00
break
}
2020-05-14 03:09:56 +02:00
err = os.Remove(df.FullPath)
if err != nil {
return err
2020-05-14 01:15:14 +02:00
}
}
return nil
}