Skip to content

Commit

Permalink
fix(shwap): don't cache empty eds and close recent accessor (#3608)
Browse files Browse the repository at this point in the history
- No need to put in cache empty eds
- in-mem accessor needs to be closed to release created reference in cache.
  • Loading branch information
walldiss authored Jul 31, 2024
1 parent 8d5ae0a commit 9118b8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/celestiaorg/rsmt2d"

"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/share"
eds "github.com/celestiaorg/celestia-node/share/new_eds"
"github.com/celestiaorg/celestia-node/store/cache"
Expand Down Expand Up @@ -101,24 +102,31 @@ func (s *Store) Put(
height uint64,
square *rsmt2d.ExtendedDataSquare,
) error {
datahash := share.DataHash(roots.Hash())
// we don't need to store empty EDS, just link the height to the empty file
if datahash.IsEmptyEDS() {
lock := s.stripLock.byHeight(height)
lock.Lock()
err := s.ensureHeightLink(roots.Hash(), height)
lock.Unlock()
return err
}

// put to cache before writing to make it accessible while write is happening
accessor := &eds.Rsmt2D{ExtendedDataSquare: square}
_, err := s.cache.First().GetOrLoad(ctx, height, accessorLoader(accessor))
acc, err := s.cache.First().GetOrLoad(ctx, height, accessorLoader(accessor))
if err != nil {
log.Warnf("failed to put Accessor in the recent cache: %s", err)
} else {
// release the ref link to the accessor
utils.CloseAndLog(log, "recent accessor", acc)
}

tNow := time.Now()
datahash := share.DataHash(roots.Hash())
lock := s.stripLock.byDatahashAndHeight(datahash, height)
lock := s.stripLock.byHashAndHeight(datahash, height)
lock.lock()
defer lock.unlock()

if datahash.IsEmptyEDS() {
err := s.ensureHeightLink(roots.Hash(), height)
return err
}

exists, err := s.createFile(square, roots, height)
if exists {
s.metrics.observePutExist(ctx)
Expand Down
2 changes: 1 addition & 1 deletion store/striplock.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (l *striplock) byHash(datahash share.DataHash) *sync.RWMutex {
return l.datahashes[lkIdx]
}

func (l *striplock) byDatahashAndHeight(datahash share.DataHash, height uint64) *multiLock {
func (l *striplock) byHashAndHeight(datahash share.DataHash, height uint64) *multiLock {
return &multiLock{[]*sync.RWMutex{l.byHash(datahash), l.byHeight(height)}}
}

Expand Down

0 comments on commit 9118b8e

Please sign in to comment.