localstorage: don't double count reserved storage

This commit is contained in:
Łukasz Magiera 2020-07-06 18:56:46 +02:00
parent 8099621cd0
commit 7279a80dfa

View File

@ -49,6 +49,7 @@ type LocalStorage interface {
SetStorage(func(*StorageConfig)) error SetStorage(func(*StorageConfig)) error
Stat(path string) (FsStat, error) Stat(path string) (FsStat, error)
DiskUsage(path string) (int64, error) // returns real disk usage for a file/directory
} }
const MetaFile = "sectorstore.json" const MetaFile = "sectorstore.json"
@ -72,15 +73,36 @@ type path struct {
reservations map[abi.SectorID]SectorFileType reservations map[abi.SectorID]SectorFileType
} }
type statFn func(path string) (FsStat, error) func (p *path) stat(ls LocalStorage) (FsStat, error) {
func (p *path) stat(st statFn) (FsStat, error) { stat, err := ls.Stat(p.local)
stat, err := st(p.local)
if err != nil { if err != nil {
return FsStat{}, err return FsStat{}, err
} }
stat.Reserved = p.reserved stat.Reserved = p.reserved
stat.Available -= p.reserved
for id, ft := range p.reservations {
for _, fileType := range PathTypes {
if fileType&ft == 0 {
continue
}
used, err := ls.DiskUsage(p.sectorPath(id, fileType))
if err != nil {
log.Errorf("getting disk usage of '%s': %+v", p.sectorPath(id, fileType), err)
continue
}
stat.Reserved -= used
}
}
if stat.Reserved < 0 {
log.Warnf("negative reserved storage: p.reserved=%d, reserved: %d", p.reserved, stat.Reserved)
stat.Reserved = 0
}
stat.Available -= stat.Reserved
if stat.Available < 0 { if stat.Available < 0 {
stat.Available = 0 stat.Available = 0
} }
@ -88,6 +110,10 @@ func (p *path) stat(st statFn) (FsStat, error) {
return stat, err return stat, err
} }
func (p *path) sectorPath(sid abi.SectorID, fileType SectorFileType) string {
return filepath.Join(p.local, fileType.String(), SectorName(sid))
}
func NewLocal(ctx context.Context, ls LocalStorage, index SectorIndex, urls []string) (*Local, error) { func NewLocal(ctx context.Context, ls LocalStorage, index SectorIndex, urls []string) (*Local, error) {
l := &Local{ l := &Local{
localStorage: ls, localStorage: ls,
@ -122,7 +148,7 @@ func (st *Local) OpenPath(ctx context.Context, p string) error {
reservations: map[abi.SectorID]SectorFileType{}, reservations: map[abi.SectorID]SectorFileType{},
} }
fst, err := out.stat(st.localStorage.Stat) fst, err := out.stat(st.localStorage)
if err != nil { if err != nil {
return err return err
} }
@ -201,7 +227,7 @@ func (st *Local) reportHealth(ctx context.Context) {
toReport := map[ID]HealthReport{} toReport := map[ID]HealthReport{}
for id, p := range st.paths { for id, p := range st.paths {
stat, err := p.stat(st.localStorage.Stat) stat, err := p.stat(st.localStorage)
toReport[id] = HealthReport{ toReport[id] = HealthReport{
Stat: stat, Stat: stat,
@ -246,7 +272,7 @@ func (st *Local) Reserve(ctx context.Context, sid abi.SectorID, spt abi.Register
return nil, errPathNotFound return nil, errPathNotFound
} }
stat, err := p.stat(st.localStorage.Stat) stat, err := p.stat(st.localStorage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -306,7 +332,7 @@ func (st *Local) AcquireSector(ctx context.Context, sid abi.SectorID, spt abi.Re
continue continue
} }
spath := filepath.Join(p.local, fileType.String(), SectorName(sid)) spath := p.sectorPath(sid, fileType)
SetPathByType(&out, fileType, spath) SetPathByType(&out, fileType, spath)
SetPathByType(&storageIDs, fileType, string(info.ID)) SetPathByType(&storageIDs, fileType, string(info.ID))
@ -348,7 +374,7 @@ func (st *Local) AcquireSector(ctx context.Context, sid abi.SectorID, spt abi.Re
// TODO: Check free space // TODO: Check free space
best = filepath.Join(p.local, fileType.String(), SectorName(sid)) best = p.sectorPath(sid, fileType)
bestID = si.ID bestID = si.ID
} }
@ -464,7 +490,7 @@ func (st *Local) removeSector(ctx context.Context, sid abi.SectorID, typ SectorF
return xerrors.Errorf("dropping sector from index: %w", err) return xerrors.Errorf("dropping sector from index: %w", err)
} }
spath := filepath.Join(p.local, typ.String(), SectorName(sid)) spath := p.sectorPath(sid, typ)
log.Infof("remove %s", spath) log.Infof("remove %s", spath)
if err := os.RemoveAll(spath); err != nil { if err := os.RemoveAll(spath); err != nil {
@ -540,7 +566,7 @@ func (st *Local) FsStat(ctx context.Context, id ID) (FsStat, error) {
return FsStat{}, errPathNotFound return FsStat{}, errPathNotFound
} }
return p.stat(st.localStorage.Stat) return p.stat(st.localStorage)
} }
var _ Store = &Local{} var _ Store = &Local{}