swarm/storage: Use struct chan for gc busy indicator

This commit is contained in:
lash 2018-10-12 10:30:08 +02:00
parent 8ea4de83c4
commit 82ca35d666
2 changed files with 31 additions and 28 deletions

View file

@ -101,7 +101,7 @@ type garbage struct {
count int // number of chunks deleted in running round count int // number of chunks deleted in running round
target int // number of chunks to delete in running round target int // number of chunks to delete in running round
batch *dbBatch // the delete batch batch *dbBatch // the delete batch
running bool runC chan struct{}
} }
type LDBStore struct { type LDBStore struct {
@ -187,6 +187,8 @@ func NewLDBStore(params *LDBStoreParams) (s *LDBStore, err error) {
maxRound: defaultMaxGCRound, maxRound: defaultMaxGCRound,
ratio: defaultGCRatio, ratio: defaultGCRatio,
} }
s.gc.runC = make(chan struct{}, 1)
s.gc.runC <- struct{}{}
return s, nil return s, nil
} }
@ -310,18 +312,13 @@ func decodeData(addr Address, data []byte) (*chunk, error) {
func (s *LDBStore) collectGarbage() error { func (s *LDBStore) collectGarbage() error {
// the running param prevents duplicate gc from starting when one is already running // prevent duplicate gc from starting when one is already running
s.lock.Lock() // runC contains a struct{} as long as we are NOT running
if s.gc.running { if len(s.gc.runC) == 0 {
s.lock.Unlock()
return nil return nil
} }
s.gc.running = true <-s.gc.runC
defer func() {
s.lock.Lock() s.lock.Lock()
s.gc.running = false
s.lock.Unlock()
}()
entryCnt := s.entryCnt entryCnt := s.entryCnt
s.lock.Unlock() s.lock.Unlock()
@ -370,6 +367,8 @@ func (s *LDBStore) collectGarbage() error {
it.Release() it.Release()
log.Trace("garbage collect batch done", "batch", singleIterationCount, "total", s.gc.count) log.Trace("garbage collect batch done", "batch", singleIterationCount, "total", s.gc.count)
} }
s.gc.runC <- struct{}{}
log.Debug("garbage collect done", "c", s.gc.count) log.Debug("garbage collect done", "c", s.gc.count)
metrics.GetOrRegisterCounter("ldbstore.collectgarbage.delete", nil).Inc(int64(totalDeleted)) metrics.GetOrRegisterCounter("ldbstore.collectgarbage.delete", nil).Inc(int64(totalDeleted))

View file

@ -19,7 +19,6 @@ package storage
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -586,20 +585,25 @@ func TestLDBStoreCollectGarbageAccessUnlikeIndex(t *testing.T) {
log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt) log.Info("ldbstore", "total", n, "missing", missing, "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
} }
func waitGc(ctx context.Context, ldb *LDBStore) error { func waitGc(ctx context.Context, ldb *LDBStore) {
ticker := time.Tick(time.Millisecond * 100) <-ldb.gc.runC
for { ldb.gc.runC <- struct{}{}
select {
case <-ctx.Done():
return errors.New("timeout")
case <-ticker:
ldb.lock.Lock()
running := ldb.gc.running
ldb.lock.Unlock()
if !running {
return nil
}
}
}
} }
//func waitGc(ctx context.Context, ldb *LDBStore) error {
// ticker := time.Tick(time.Millisecond * 100)
// for {
// select {
//
// case <-ctx.Done():
// return errors.New("timeout")
// case <-ticker:
// ldb.lock.Lock()
// running := ldb.gc.running
// ldb.lock.Unlock()
// if !running {
// return nil
// }
// }
// }
//}