From 12498092839a8f6ad70a3d07a6b590bde93c7368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 3 May 2022 15:47:14 -0400 Subject: [PATCH] stores: Deduplicate parallel stat requests --- extern/sector-storage/stores/local.go | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/extern/sector-storage/stores/local.go b/extern/sector-storage/stores/local.go index 4efddca3830..e33b662d5d2 100644 --- a/extern/sector-storage/stores/local.go +++ b/extern/sector-storage/stores/local.go @@ -85,9 +85,58 @@ type path struct { reserved int64 reservations map[abi.SectorID]storiface.SectorFileType + + statLk sync.Mutex + statDone chan struct{} + + lastStat *fsutil.FsStat // nil if no stat / was error } func (p *path) stat(ls LocalStorage) (fsutil.FsStat, error) { + for { + p.statLk.Lock() + if p.statDone == nil { + p.statDone = make(chan struct{}) + p.statLk.Unlock() + + st, err := p.doStat(ls) + + p.statLk.Lock() + p.lastStat = nil + if err == nil { + p.lastStat = &st + } + close(p.statDone) + p.statDone = nil + p.statLk.Unlock() + return st, err + } + + doneCh := p.statDone + p.statLk.Unlock() + + select { + case <-doneCh: + // todo context? + } + + p.statLk.Lock() + if p.lastStat == nil { + p.statLk.Unlock() + continue + } + + st := *p.lastStat + + p.statLk.Unlock() + + return st, nil + } +} + +func (p *path) doStat(ls LocalStorage) (fsutil.FsStat, error) { + start := time.Now() + stat, err := ls.Stat(p.local) if err != nil { return fsutil.FsStat{}, xerrors.Errorf("stat %s: %w", p.local, err) @@ -155,6 +204,8 @@ func (p *path) stat(ls LocalStorage) (fsutil.FsStat, error) { } } + log.Infow("storage stat", "took", time.Now().Sub(start), "reservations", len(p.reservations)) + return stat, err }