Skip to content

Commit

Permalink
Merge branch 'tony/maindb-v4' of https://github.com/vechain/thor into…
Browse files Browse the repository at this point in the history
… miguel/object-write-read
  • Loading branch information
freemanzMrojo committed Dec 9, 2024
2 parents a3335ee + 4e02448 commit f9b9426
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
16 changes: 11 additions & 5 deletions chain/metrics.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// 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 metricBlockRepositoryCounter = metrics.LazyLoadCounterVec("block_repository_count", []string{"type", "target"})

var metricTransactionRepositoryCounter = metrics.LazyLoadCounterVec("transaction_repository_count", []string{"type", "target"})

var metricReceiptRepositoryCounter = metrics.LazyLoadCounterVec("receipt_repository_count", []string{"type", "target"})
var (
metricCacheHitMiss = metrics.LazyLoadCounterVec("repo_cache_hit_miss_count", []string{"type", "event"})
metricBlockRepositoryCounter = metrics.LazyLoadCounterVec("block_repository_count", []string{"type", "target"})
metricTransactionRepositoryCounter = metrics.LazyLoadCounterVec("transaction_repository_count", []string{"type", "target"})
metricReceiptRepositoryCounter = metrics.LazyLoadCounterVec("receipt_repository_count", []string{"type", "target"})
)
20 changes: 15 additions & 5 deletions chain/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,22 +285,28 @@ 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{}
result := "hit"
if blk, err = r.caches.summaries.GetOrLoad(id, func() (interface{}, error) {
result = "miss"
return loadBlockSummary(r.hdrStore, id)
}); err != nil {
return
}
return cached.(*BlockSummary), nil
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "block-summary", "event": result})
return blk.(*BlockSummary), nil
}

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

Expand Down Expand Up @@ -348,14 +354,18 @@ 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) {
result := "hit"
receipt, err := r.caches.receipts.GetOrLoad(string(key), func() (interface{}, error) {
result = "miss"
return loadReceipt(r.bodyStore, key)
})
if err != nil {
return nil, err
}
return cached.(*tx.Receipt), nil
metricCacheHitMiss().AddWithLabel(1, map[string]string{"type": "receipt", "event": result})
return receipt.(*tx.Receipt), nil
}

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

0 comments on commit f9b9426

Please sign in to comment.