Skip to content

Commit

Permalink
chore(chain): add repo cache metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Dec 6, 2024
1 parent f090234 commit 55e8f01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
11 changes: 7 additions & 4 deletions chain/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ func newCache(maxSize int) *cache {
return &cache{c}
}

func (c *cache) GetOrLoad(key interface{}, load func() (interface{}, error)) (interface{}, error) {
// GetOrLoad returns the value associated with the key if it exists in the cache.
// Otherwise, it calls the load function to get the value and adds it to the cache.
// It returns the value, a boolean indicating whether the value was cached, and an error if any.
func (c *cache) GetOrLoad(key interface{}, load func() (interface{}, error)) (interface{}, bool, error) {
if value, ok := c.Get(key); ok {
return value, nil
return value, true, nil
}
value, err := load()
if err != nil {
return nil, err
return nil, false, err
}
c.Add(key, value)
return value, nil
return value, false, nil
}
12 changes: 12 additions & 0 deletions chain/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2024 The VeChainThor developers
//
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

package chain

import "github.com/vechain/thor/v2/metrics"

var (
metricCacheHitMiss = metrics.LazyLoadCounterVec("repo_cache_hit_miss_count", []string{"type", "event"})
)
29 changes: 23 additions & 6 deletions chain/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,33 @@ func (r *Repository) GetMaxBlockNum() (uint32, error) {

// GetBlockSummary get block summary by block id.
func (r *Repository) GetBlockSummary(id thor.Bytes32) (summary *BlockSummary, err error) {
var cached interface{}
if cached, err = r.caches.summaries.GetOrLoad(id, func() (interface{}, error) {
var blk interface{}
var cached bool
if blk, cached, err = r.caches.summaries.GetOrLoad(id, func() (interface{}, error) {
return loadBlockSummary(r.hdrStore, id)
}); err != nil {
return
}
return cached.(*BlockSummary), nil
if cached {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "blocks", "event": "hit"})
} else {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "blocks", "event": "miss"})
}
return blk.(*BlockSummary), nil
}

func (r *Repository) getTransaction(key []byte) (*tx.Transaction, error) {
trx, err := r.caches.txs.GetOrLoad(string(key), func() (interface{}, error) {
trx, cached, err := r.caches.txs.GetOrLoad(string(key), func() (interface{}, error) {
return loadTransaction(r.bodyStore, key)
})
if err != nil {
return nil, err
}
if cached {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "transaction", "event": "hit"})
} else {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "transaction", "event": "miss"})
}
return trx.(*tx.Transaction), nil
}

Expand Down Expand Up @@ -346,14 +357,20 @@ func (r *Repository) GetBlock(id thor.Bytes32) (*block.Block, error) {
return block.Compose(summary.Header, txs), nil
}


func (r *Repository) getReceipt(key []byte) (*tx.Receipt, error) {
cached, err := r.caches.receipts.GetOrLoad(string(key), func() (interface{}, error) {
receipt, cached, err := r.caches.receipts.GetOrLoad(string(key), func() (interface{}, error) {
return loadReceipt(r.bodyStore, key)
})
if err != nil {
return nil, err
}
return cached.(*tx.Receipt), nil
if cached {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "receipt", "event": "hit"})
} else {
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "receipt", "event": "miss"})
}
return receipt.(*tx.Receipt), nil
}

func loadReceipt(r kv.Getter, key []byte) (*tx.Receipt, error) {
Expand Down

0 comments on commit 55e8f01

Please sign in to comment.