Commit a30ce14b authored by acud's avatar acud Committed by GitHub

localstore: fix data race in test (#1452)

* localstore: fix data race in test

* fix goroutine leak in test

* undo

* remove superfluous done
parent bfa93925
...@@ -131,9 +131,9 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { ...@@ -131,9 +131,9 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
collectedCount++ collectedCount++
if collectedCount >= gcBatchSize { if collectedCount >= gcBatchSize {
// batch size limit reached, // batch size limit reached, however we don't
// another gc run is needed // know whether another gc run is needed until
done = false // we weed out the dirty entries below
return true, nil return true, nil
} }
return false, nil return false, nil
...@@ -162,9 +162,6 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { ...@@ -162,9 +162,6 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
for _, item := range candidates { for _, item := range candidates {
if swarm.NewAddress(item.Address).MemberOf(db.dirtyAddresses) { if swarm.NewAddress(item.Address).MemberOf(db.dirtyAddresses) {
collectedCount-- collectedCount--
if gcSize-collectedCount > target {
done = false
}
continue continue
} }
...@@ -189,6 +186,10 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { ...@@ -189,6 +186,10 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
return 0, false, err return 0, false, err
} }
} }
if gcSize-collectedCount > target {
done = false
}
db.metrics.GCCommittedCounter.Add(float64(collectedCount)) db.metrics.GCCommittedCounter.Add(float64(collectedCount))
db.gcSize.PutInBatch(batch, gcSize-collectedCount) db.gcSize.PutInBatch(batch, gcSize-collectedCount)
......
...@@ -725,21 +725,26 @@ func TestGC_NoEvictDirty(t *testing.T) { ...@@ -725,21 +725,26 @@ func TestGC_NoEvictDirty(t *testing.T) {
// lower the maximal number of chunks in a single // lower the maximal number of chunks in a single
// gc batch to ensure multiple batches. // gc batch to ensure multiple batches.
defer func(s uint64) { gcBatchSize = s }(gcBatchSize) defer func(s uint64) { gcBatchSize = s }(gcBatchSize)
gcBatchSize = 2 gcBatchSize = 1
chunkCount := 15 chunkCount := 10
db := newTestDB(t, &Options{ db := newTestDB(t, &Options{
Capacity: 10, Capacity: 10,
}) })
closed := db.close
testHookCollectGarbageChan := make(chan uint64) testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) { t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
// don't trigger if we haven't collected anything - this may
// result in a race condition when we inspect the gcsize below,
// causing the database to shut down while the cleanup to happen
// before the correct signal has been communicated here.
if collectedCount == 0 {
return
}
select { select {
case testHookCollectGarbageChan <- collectedCount: case testHookCollectGarbageChan <- collectedCount:
case <-closed: case <-db.close:
} }
})) }))
...@@ -749,6 +754,7 @@ func TestGC_NoEvictDirty(t *testing.T) { ...@@ -749,6 +754,7 @@ func TestGC_NoEvictDirty(t *testing.T) {
incomingChan <- struct{}{} incomingChan <- struct{}{}
<-dirtyChan <-dirtyChan
})) }))
defer close(incomingChan)
addrs := make([]swarm.Address, 0) addrs := make([]swarm.Address, 0)
mtx := new(sync.Mutex) mtx := new(sync.Mutex)
online := make(chan struct{}) online := make(chan struct{})
...@@ -773,7 +779,6 @@ func TestGC_NoEvictDirty(t *testing.T) { ...@@ -773,7 +779,6 @@ func TestGC_NoEvictDirty(t *testing.T) {
} }
dirtyChan <- struct{}{} dirtyChan <- struct{}{}
} }
}() }()
<-online <-online
// upload random chunks // upload random chunks
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment