swarm/storage: wait for garbage collection

This commit is contained in:
Anton Evangelatov 2018-08-17 15:25:00 +02:00
parent 60571122d5
commit 54dc7328df
2 changed files with 9 additions and 2 deletions

View file

@ -243,6 +243,8 @@ func decodeOldData(data []byte, chunk *Chunk) {
} }
func (s *LDBStore) collectGarbage(ratio float32) { func (s *LDBStore) collectGarbage(ratio float32) {
log.Trace("collectGarbage", "ratio", ratio)
metrics.GetOrRegisterCounter("ldbstore.collectgarbage", nil).Inc(1) metrics.GetOrRegisterCounter("ldbstore.collectgarbage", nil).Inc(1)
it := s.db.NewIterator() it := s.db.NewIterator()
@ -582,11 +584,13 @@ mainLoop:
} }
close(c) close(c)
for e > s.capacity { for e > s.capacity {
log.Trace("for >", "e", e, "s.capacity", s.capacity)
// Collect garbage in a separate goroutine // Collect garbage in a separate goroutine
// to be able to interrupt this loop by s.quit. // to be able to interrupt this loop by s.quit.
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
s.collectGarbage(gcArrayFreeRatio) s.collectGarbage(gcArrayFreeRatio)
log.Trace("collectGarbage closing done")
close(done) close(done)
}() }()

View file

@ -27,8 +27,8 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/chunk" "github.com/ethereum/go-ethereum/swarm/chunk"
"github.com/ethereum/go-ethereum/swarm/log"
"github.com/ethereum/go-ethereum/swarm/storage/mock/mem" "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
ldberrors "github.com/syndtr/goleveldb/leveldb/errors" ldberrors "github.com/syndtr/goleveldb/leveldb/errors"
@ -362,7 +362,7 @@ func TestLDBStoreCollectGarbage(t *testing.T) {
log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt) log.Info("ldbstore", "entrycnt", ldb.entryCnt, "accesscnt", ldb.accessCnt)
// wait for garbage collection to kick in on the responsible actor // wait for garbage collection to kick in on the responsible actor
time.Sleep(5 * time.Second) time.Sleep(1 * time.Second)
var missing int var missing int
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -492,6 +492,9 @@ func TestLDBStoreRemoveThenCollectGarbage(t *testing.T) {
<-chunks[i].dbStoredC <-chunks[i].dbStoredC
} }
// wait for garbage collection
time.Sleep(1 * time.Second)
// expect for first chunk to be missing, because it has the smallest access value // expect for first chunk to be missing, because it has the smallest access value
idx := 0 idx := 0
ret, err := ldb.Get(context.TODO(), chunks[idx].Addr) ret, err := ldb.Get(context.TODO(), chunks[idx].Addr)