Browse Source

light: fix light store deadlock (#5901)

pull/5913/head
Callum Waters 4 years ago
committed by GitHub
parent
commit
ca285844ea
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions
  1. +6
    -3
      light/store/db/db.go
  2. +14
    -4
      light/store/db/db_test.go

+ 6
- 3
light/store/db/db.go View File

@ -243,12 +243,14 @@ func (s *dbs) Prune(size uint16) error {
b := s.db.NewBatch() b := s.db.NewBatch()
defer b.Close() defer b.Close()
var pruned uint16 = 0
for itr.Valid() && numToPrune > 0 { for itr.Valid() && numToPrune > 0 {
if err = b.Delete(itr.Key()); err != nil { if err = b.Delete(itr.Key()); err != nil {
return err return err
} }
itr.Next() itr.Next()
numToPrune-- numToPrune--
pruned++
} }
if err = itr.Error(); err != nil { if err = itr.Error(); err != nil {
return err return err
@ -256,11 +258,12 @@ func (s *dbs) Prune(size uint16) error {
// 3) // update size // 3) // update size
s.mtx.Lock() s.mtx.Lock()
s.size = size
s.size -= pruned
newSize := s.size
s.mtx.Unlock() s.mtx.Unlock()
if wErr := b.Set(s.sizeKey(), marshalSize(size)); wErr != nil {
return fmt.Errorf("failed to persist size: %w", wErr)
if err := b.Set(s.sizeKey(), marshalSize(newSize)); err != nil {
return fmt.Errorf("failed to persist size: %w", err)
} }
// 4) write batch deletion to disk // 4) write batch deletion to disk


+ 14
- 4
light/store/db/db_test.go View File

@ -148,6 +148,13 @@ func Test_Concurrency(t *testing.T) {
t.Log(err) t.Log(err)
} }
if i > 2 {
_, err = dbStore.LightBlockBefore(i - 1)
if err != nil {
t.Log(err)
}
}
_, err = dbStore.LastLightBlockHeight() _, err = dbStore.LastLightBlockHeight()
if err != nil { if err != nil {
t.Log(err) t.Log(err)
@ -157,16 +164,19 @@ func Test_Concurrency(t *testing.T) {
t.Log(err) t.Log(err)
} }
err = dbStore.Prune(2)
err = dbStore.Prune(3)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
_ = dbStore.Size() _ = dbStore.Size()
err = dbStore.DeleteLightBlock(1)
if err != nil {
t.Log(err)
if i > 2 && i%2 == 0 {
err = dbStore.DeleteLightBlock(i - 1)
if err != nil {
t.Log(err)
}
} }
}(int64(i)) }(int64(i))
} }


Loading…
Cancel
Save