2018-01-31 02:15:21 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-05-09 03:06:51 +02:00
|
|
|
"path/filepath"
|
2019-11-14 00:50:49 +01:00
|
|
|
"sort"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2019-11-14 00:50:49 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2018-01-31 02:15:21 +01:00
|
|
|
)
|
|
|
|
|
2019-10-03 22:24:59 +02:00
|
|
|
// DiskBlobStore stores blobs on a local disk
|
|
|
|
type DiskBlobStore struct {
|
2019-10-03 22:12:49 +02:00
|
|
|
// the location of blobs on disk
|
|
|
|
blobDir string
|
|
|
|
// store files in subdirectories based on the first N chars in the filename. 0 = don't create subdirectories.
|
|
|
|
prefixLength int
|
2018-01-31 02:15:21 +01:00
|
|
|
|
2019-11-14 00:50:49 +01:00
|
|
|
initialized bool
|
|
|
|
lastChecked time.Time
|
|
|
|
diskCleanupBusy chan bool
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:24:59 +02:00
|
|
|
// NewDiskBlobStore returns an initialized file disk store pointer.
|
|
|
|
func NewDiskBlobStore(dir string, prefixLength int) *DiskBlobStore {
|
2019-11-14 00:50:49 +01:00
|
|
|
dbs := DiskBlobStore{blobDir: dir, prefixLength: prefixLength, diskCleanupBusy: make(chan bool, 1)}
|
|
|
|
dbs.diskCleanupBusy <- true
|
|
|
|
return &dbs
|
2019-10-03 22:12:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) dir(hash string) string {
|
|
|
|
if d.prefixLength <= 0 || len(hash) < d.prefixLength {
|
|
|
|
return d.blobDir
|
2019-10-03 22:12:49 +02:00
|
|
|
}
|
2019-10-03 22:34:57 +02:00
|
|
|
return path.Join(d.blobDir, hash[:d.prefixLength])
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 00:50:49 +01:00
|
|
|
// 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
|
|
|
|
func (d *DiskBlobStore) getUsedSpace() (float32, error) {
|
|
|
|
var stat syscall.Statfs_t
|
|
|
|
err := syscall.Statfs(d.blobDir, &stat)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// Available blocks * size per block = available space in bytes
|
|
|
|
all := stat.Blocks * uint64(stat.Bsize)
|
|
|
|
free := stat.Bfree * uint64(stat.Bsize)
|
|
|
|
used := all - free
|
|
|
|
|
|
|
|
return float32(used) / float32(all), nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) path(hash string) string {
|
|
|
|
return path.Join(d.dir(hash), hash)
|
2019-10-03 22:12:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) ensureDirExists(dir string) error {
|
2019-10-03 22:12:49 +02:00
|
|
|
return errors.Err(os.MkdirAll(dir, 0755))
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) initOnce() error {
|
|
|
|
if d.initialized {
|
2018-01-31 02:15:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
err := d.ensureDirExists(d.blobDir)
|
2019-10-03 22:12:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-07-26 16:25:47 +02:00
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
d.initialized = true
|
2018-01-31 02:15:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Has returns T/F or Error if it the blob stored already. It will error with any IO disk error.
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) Has(hash string) (bool, error) {
|
|
|
|
err := d.initOnce()
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
_, err = os.Stat(d.path(hash))
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
// Get returns the blob or an error if the blob doesn't exist.
|
|
|
|
func (d *DiskBlobStore) Get(hash string) (stream.Blob, error) {
|
|
|
|
err := d.initOnce()
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
2019-10-03 22:12:49 +02:00
|
|
|
return nil, err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
file, err := os.Open(d.path(hash))
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
2018-02-07 21:21:20 +01:00
|
|
|
if os.IsNotExist(err) {
|
2019-10-03 22:12:49 +02:00
|
|
|
return nil, errors.Err(ErrBlobNotFound)
|
2018-02-07 21:21:20 +01:00
|
|
|
}
|
2019-10-03 22:12:49 +02:00
|
|
|
return nil, err
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.ReadAll(file)
|
|
|
|
}
|
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// Put stores the blob on disk
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) Put(hash string, blob stream.Blob) error {
|
|
|
|
err := d.initOnce()
|
2018-01-31 02:15:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
err = d.ensureDirExists(d.dir(hash))
|
2019-10-03 22:12:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-14 00:50:49 +01:00
|
|
|
select {
|
|
|
|
case <-d.diskCleanupBusy:
|
|
|
|
if time.Since(d.lastChecked) > 5*time.Minute {
|
|
|
|
go d.ensureDiskSpace()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2019-10-03 22:12:49 +02:00
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
return ioutil.WriteFile(d.path(hash), blob, 0644)
|
2018-01-31 02:15:21 +01:00
|
|
|
}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2018-07-26 16:25:47 +02:00
|
|
|
// PutSD stores the sd blob on the disk
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) PutSD(hash string, blob stream.Blob) error {
|
|
|
|
return d.Put(hash, blob)
|
2018-02-02 22:49:20 +01:00
|
|
|
}
|
2018-09-11 13:41:29 +02:00
|
|
|
|
|
|
|
// Delete deletes the blob from the store
|
2019-10-03 22:34:57 +02:00
|
|
|
func (d *DiskBlobStore) Delete(hash string) error {
|
|
|
|
err := d.initOnce()
|
2018-09-11 13:41:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
has, err := d.Has(hash)
|
2018-09-11 13:41:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:34:57 +02:00
|
|
|
return os.Remove(d.path(hash))
|
2018-09-11 13:41:29 +02:00
|
|
|
}
|
2019-11-14 00:50:49 +01:00
|
|
|
|
|
|
|
func (d *DiskBlobStore) ensureDiskSpace() {
|
|
|
|
defer func() {
|
|
|
|
d.lastChecked = time.Now()
|
|
|
|
d.diskCleanupBusy <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
used, err := d.getUsedSpace()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("disk usage: %.2f%%\n", used*100)
|
|
|
|
if used > 0.90 {
|
|
|
|
log.Infoln("over 0.90, cleaning up")
|
|
|
|
err = d.WipeOldestBlobs()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infoln("Done cleaning up")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DiskBlobStore) WipeOldestBlobs() (err error) {
|
|
|
|
dirs, err := ioutil.ReadDir(d.blobDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
type datedFile struct {
|
|
|
|
Atime time.Time
|
|
|
|
File *os.FileInfo
|
|
|
|
FullPath string
|
|
|
|
}
|
2020-05-09 03:06:51 +02:00
|
|
|
datedFiles := make([]datedFile, 0, 5000)
|
2019-11-14 00:50:49 +01:00
|
|
|
for _, dir := range dirs {
|
|
|
|
if dir.IsDir() {
|
2020-05-09 03:06:51 +02:00
|
|
|
files, err := ioutil.ReadDir(filepath.Join(d.blobDir, dir.Name()))
|
2019-11-14 00:50:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if file.Mode().IsRegular() && !file.IsDir() {
|
|
|
|
datedFiles = append(datedFiles, datedFile{
|
|
|
|
Atime: atime(file),
|
|
|
|
File: &file,
|
2020-05-09 03:06:51 +02:00
|
|
|
FullPath: filepath.Join(d.blobDir, dir.Name(), file.Name()),
|
2019-11-14 00:50:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(datedFiles, func(i, j int) bool {
|
|
|
|
return datedFiles[i].Atime.Before(datedFiles[j].Atime)
|
|
|
|
})
|
|
|
|
//delete the first 50000 blobs
|
|
|
|
for i, df := range datedFiles {
|
|
|
|
if i >= 50000 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Infoln(df.FullPath)
|
|
|
|
log.Infoln(df.Atime.String())
|
|
|
|
err = os.Remove(df.FullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|