From 9118b8ed3697234333a05bc6de534eec7e452aa8 Mon Sep 17 00:00:00 2001 From: Vlad <13818348+walldiss@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:08:16 +0200 Subject: [PATCH] fix(shwap): don't cache empty eds and close recent accessor (#3608) - No need to put in cache empty eds - in-mem accessor needs to be closed to release created reference in cache. --- store/store.go | 24 ++++++++++++++++-------- store/striplock.go | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/store/store.go b/store/store.go index 7dc578d885..589cf1b9f6 100644 --- a/store/store.go +++ b/store/store.go @@ -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" @@ -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) diff --git a/store/striplock.go b/store/striplock.go index e0e6e48ba2..4738453c77 100644 --- a/store/striplock.go +++ b/store/striplock.go @@ -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)}} }