union: add min_free_space option for lfs/eplfs policies - fixes #6071

This commit is contained in:
Nick Craig-Wood 2022-04-04 09:55:05 +01:00
parent 29e37749b3
commit 1651429041
3 changed files with 22 additions and 6 deletions

View file

@ -13,4 +13,5 @@ type Options struct {
CreatePolicy string `config:"create_policy"`
SearchPolicy string `config:"search_policy"`
CacheTime int `config:"cache_time"`
MinFreeSpace fs.SizeSuffix `config:"min_free_space"`
}

View file

@ -2,6 +2,7 @@ package policy
import (
"context"
"errors"
"math"
"github.com/rclone/rclone/backend/union/upstream"
@ -18,6 +19,8 @@ type EpLfs struct {
EpAll
}
var errNoUpstreamsFound = errors.New("no upstreams found with more than min_free_space space spare")
func (p *EpLfs) lfs(upstreams []*upstream.Fs) (*upstream.Fs, error) {
var minFreeSpace int64 = math.MaxInt64
var lfsupstream *upstream.Fs
@ -27,31 +30,35 @@ func (p *EpLfs) lfs(upstreams []*upstream.Fs) (*upstream.Fs, error) {
fs.LogPrintf(fs.LogLevelNotice, nil,
"Free Space is not supported for upstream %s, treating as infinite", u.Name())
}
if space < minFreeSpace {
if space < minFreeSpace && space > int64(u.Opt.MinFreeSpace) {
minFreeSpace = space
lfsupstream = u
}
}
if lfsupstream == nil {
return nil, fs.ErrorObjectNotFound
return nil, errNoUpstreamsFound
}
return lfsupstream, nil
}
func (p *EpLfs) lfsEntries(entries []upstream.Entry) (upstream.Entry, error) {
var minFreeSpace int64
var minFreeSpace int64 = math.MaxInt64
var lfsEntry upstream.Entry
for _, e := range entries {
space, err := e.UpstreamFs().GetFreeSpace()
u := e.UpstreamFs()
space, err := u.GetFreeSpace()
if err != nil {
fs.LogPrintf(fs.LogLevelNotice, nil,
"Free Space is not supported for upstream %s, treating as infinite", e.UpstreamFs().Name())
"Free Space is not supported for upstream %s, treating as infinite", u.Name())
}
if space < minFreeSpace {
if space < minFreeSpace && space > int64(u.Opt.MinFreeSpace) {
minFreeSpace = space
lfsEntry = e
}
}
if lfsEntry == nil {
return nil, errNoUpstreamsFound
}
return lfsEntry, nil
}

View file

@ -50,6 +50,14 @@ func init() {
Name: "cache_time",
Help: "Cache time of usage and free space (in seconds).\n\nThis option is only useful when a path preserving policy is used.",
Default: 120,
}, {
Name: "min_free_space",
Help: `Minimum viable free space for lfs/eplfs policies.
If a remote has less than this much free space then it won't be
considered for use in lfs or eplfs policies.`,
Advanced: true,
Default: fs.Gibi,
}},
}
fs.Register(fsi)