Skip to content

Commit

Permalink
move struct cache to blockDAO (iotexproject#4226)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderZhi authored Apr 22, 2024
1 parent bb7a6d7 commit 1c161a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
37 changes: 30 additions & 7 deletions blockchain/blockdao/blockdao.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type (
timerFactory *prometheustimer.TimerFactory
lifecycle lifecycle.Lifecycle
headerCache cache.LRUCache
bodyCache cache.LRUCache
footerCache cache.LRUCache
receiptCache cache.LRUCache
blockCache cache.LRUCache
tipHeight uint64
}
)
Expand All @@ -83,8 +84,9 @@ func NewBlockDAOWithIndexersAndCache(blkStore BlockDAO, indexers []BlockIndexer,
}
if cacheSize > 0 {
blockDAO.headerCache = cache.NewThreadSafeLruCache(cacheSize)
blockDAO.bodyCache = cache.NewThreadSafeLruCache(cacheSize)
blockDAO.footerCache = cache.NewThreadSafeLruCache(cacheSize)
blockDAO.receiptCache = cache.NewThreadSafeLruCache(cacheSize)
blockDAO.blockCache = cache.NewThreadSafeLruCache(cacheSize)
}
timerFactory, err := prometheustimer.New(
"iotex_block_dao_perf",
Expand Down Expand Up @@ -153,9 +155,20 @@ func (dao *blockDAO) GetBlockHeight(hash hash.Hash256) (uint64, error) {
}

func (dao *blockDAO) GetBlock(hash hash.Hash256) (*block.Block, error) {
if blk, ok := lruCacheGet(dao.blockCache, hash); ok {
_cacheMtc.WithLabelValues("hit_block").Inc()
return blk.(*block.Block), nil
}
_cacheMtc.WithLabelValues("miss_block").Inc()

timer := dao.timerFactory.NewTimer("get_block")
defer timer.End()
return dao.blockStore.GetBlock(hash)
blk, err := dao.blockStore.GetBlock(hash)
if err != nil {
return nil, err
}
lruCachePut(dao.blockCache, hash, blk)
return blk, nil
}

func (dao *blockDAO) GetBlockByHeight(height uint64) (*block.Block, error) {
Expand All @@ -170,7 +183,7 @@ func (dao *blockDAO) HeaderByHeight(height uint64) (*block.Header, error) {
return v.(*block.Header), nil
}
_cacheMtc.WithLabelValues("miss_header").Inc()

// TODO: fetch from blockCache
header, err := dao.blockStore.HeaderByHeight(height)
if err != nil {
return nil, err
Expand All @@ -186,7 +199,7 @@ func (dao *blockDAO) FooterByHeight(height uint64) (*block.Footer, error) {
return v.(*block.Footer), nil
}
_cacheMtc.WithLabelValues("miss_footer").Inc()

// TODO: fetch from blockCache
footer, err := dao.blockStore.FooterByHeight(height)
if err != nil {
return nil, err
Expand All @@ -206,7 +219,7 @@ func (dao *blockDAO) Header(h hash.Hash256) (*block.Header, error) {
return header.(*block.Header), nil
}
_cacheMtc.WithLabelValues("miss_header").Inc()

// TODO: fetch from blockCache
header, err := dao.blockStore.Header(h)
if err != nil {
return nil, err
Expand All @@ -217,9 +230,19 @@ func (dao *blockDAO) Header(h hash.Hash256) (*block.Header, error) {
}

func (dao *blockDAO) GetReceipts(height uint64) ([]*action.Receipt, error) {
if receipts, ok := lruCacheGet(dao.receiptCache, height); ok {
_cacheMtc.WithLabelValues("hit_receipts").Inc()
return receipts.([]*action.Receipt), nil
}
_cacheMtc.WithLabelValues("miss_receipts").Inc()
timer := dao.timerFactory.NewTimer("get_receipt")
defer timer.End()
return dao.blockStore.GetReceipts(height)
receipts, err := dao.blockStore.GetReceipts(height)
if err != nil {
return nil, err
}
lruCachePut(dao.receiptCache, height, receipts)
return receipts, nil
}

func (dao *blockDAO) ContainsTransactionLog() bool {
Expand Down
6 changes: 0 additions & 6 deletions blockchain/filedao/filedao_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ type (
tip *FileTip
blkBuffer *stagingBuffer
blkStorePbCache cache.LRUCache
blkCache cache.LRUCache
receiptCache cache.LRUCache
kvStore db.KVStore
batch batch.KVStoreBatch
hashStore db.CountingIndex // store block hash
Expand Down Expand Up @@ -71,8 +69,6 @@ func newFileDAOv2(bottom uint64, cfg db.Config, deser *block.Deserializer) (*fil
Height: bottom - 1,
},
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
Expand All @@ -85,8 +81,6 @@ func openFileDAOv2(cfg db.Config, deser *block.Deserializer) *fileDAOv2 {
return &fileDAOv2{
filename: cfg.DbPath,
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewBoltDB(cfg),
batch: batch.NewBatch(),
deser: deser,
Expand Down
24 changes: 2 additions & 22 deletions blockchain/filedao/filedao_v2_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,12 @@ func (fd *fileDAOv2) getBlock(height uint64) (*block.Block, error) {
}
return blkStore.Block, nil
}
// check whether block in read cache or not
if value, ok := fd.blkCache.Get(height); ok {
return value.(*block.Block), nil
}
// read from storage DB
blockStore, err := fd.getBlockStore(height)
if err != nil {
return nil, err
}
blk, err := fd.deser.BlockFromBlockStoreProto(blockStore)
if err != nil {
return nil, err
}
// add to read cache
fd.blkCache.Add(height, blk)
return blk, nil
return fd.deser.BlockFromBlockStoreProto(blockStore)
}

func (fd *fileDAOv2) getReceipt(height uint64) ([]*action.Receipt, error) {
Expand All @@ -217,22 +207,12 @@ func (fd *fileDAOv2) getReceipt(height uint64) ([]*action.Receipt, error) {
}
return blkStore.Receipts, nil
}
// check whether receipts in read cache or not
if value, ok := fd.receiptCache.Get(height); ok {
return value.([]*action.Receipt), nil
}
// read from storage DB
blockStore, err := fd.getBlockStore(height)
if err != nil {
return nil, err
}
receipts, err := fd.deser.ReceiptsFromBlockStoreProto(blockStore)
if err != nil {
return nil, err
}
// add to read cache
fd.receiptCache.Add(height, receipts)
return receipts, nil
return fd.deser.ReceiptsFromBlockStoreProto(blockStore)
}

func (fd *fileDAOv2) getBlockStore(height uint64) (*iotextypes.BlockStore, error) {
Expand Down
2 changes: 0 additions & 2 deletions blockchain/filedao/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ func newFileDAOv2InMem(bottom uint64) (*fileDAOv2, error) {
Height: bottom - 1,
},
blkStorePbCache: cache.NewThreadSafeLruCache(16),
blkCache: cache.NewThreadSafeLruCache(256),
receiptCache: cache.NewThreadSafeLruCache(256),
kvStore: db.NewMemKVStore(),
batch: batch.NewBatch(),
deser: block.NewDeserializer(_defaultEVMNetworkID),
Expand Down

0 comments on commit 1c161a1

Please sign in to comment.