From a35140c4bd05d3199791cf0b42da0be47e1b98a3 Mon Sep 17 00:00:00 2001 From: William Banfield Date: Mon, 28 Nov 2022 13:11:46 -0500 Subject: [PATCH] add sample collector --- evidence/mocks/block_store.go | 2 +- state/mocks/block_store.go | 2 +- store/store.go | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/evidence/mocks/block_store.go b/evidence/mocks/block_store.go index 8872a7bfb..e61c4e0ae 100644 --- a/evidence/mocks/block_store.go +++ b/evidence/mocks/block_store.go @@ -64,7 +64,7 @@ type mockConstructorTestingTNewBlockStore interface { } // NewBlockStore creates a new instance of BlockStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewBlockStore(t mockConstructorTestingTNewBlockStore, store.BlockStoreOptions{}) *BlockStore { +func NewBlockStore(t mockConstructorTestingTNewBlockStore) *BlockStore { mock := &BlockStore{} mock.Mock.Test(t) diff --git a/state/mocks/block_store.go b/state/mocks/block_store.go index 0bd0aed3c..d449f6711 100644 --- a/state/mocks/block_store.go +++ b/state/mocks/block_store.go @@ -237,7 +237,7 @@ type mockConstructorTestingTNewBlockStore interface { } // NewBlockStore creates a new instance of BlockStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewBlockStore(t mockConstructorTestingTNewBlockStore, store.BlockStoreOptions{}) *BlockStore { +func NewBlockStore(t mockConstructorTestingTNewBlockStore) *BlockStore { mock := &BlockStore{} mock.Mock.Test(t) diff --git a/store/store.go b/store/store.go index d0bb70e2c..f795913f1 100644 --- a/store/store.go +++ b/store/store.go @@ -3,8 +3,10 @@ package store import ( "fmt" "strconv" + "time" "github.com/cosmos/gogoproto/proto" + "github.com/go-kit/kit/metrics" dbm "github.com/tendermint/tm-db" "github.com/tendermint/tendermint/evidence" @@ -104,6 +106,7 @@ func (bs *BlockStore) LoadBaseMeta() *types.BlockMeta { // LoadBlock returns the block with the given height. // If no block is found for that height, it returns nil. func (bs *BlockStore) LoadBlock(height int64) *types.Block { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block"))() var blockMeta = bs.LoadBlockMeta(height) if blockMeta == nil { return nil @@ -139,6 +142,7 @@ func (bs *BlockStore) LoadBlock(height int64) *types.Block { // If no block is found for that hash, it returns nil. // Panics if it fails to parse height associated with the given hash. func (bs *BlockStore) LoadBlockByHash(hash []byte) *types.Block { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block_by_hash"))() bz, err := bs.db.Get(calcBlockHashKey(hash)) if err != nil { panic(err) @@ -160,6 +164,7 @@ func (bs *BlockStore) LoadBlockByHash(hash []byte) *types.Block { // from the block at the given height. // If no part is found for the given height and index, it returns nil. func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block_part"))() var pbpart = new(tmproto.Part) bz, err := bs.db.Get(calcBlockPartKey(height, index)) @@ -185,6 +190,7 @@ func (bs *BlockStore) LoadBlockPart(height int64, index int) *types.Part { // LoadBlockMeta returns the BlockMeta for the given height. // If no block is found for the given height, it returns nil. func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block_meta"))() var pbbm = new(tmproto.BlockMeta) bz, err := bs.db.Get(calcBlockMetaKey(height)) @@ -212,6 +218,7 @@ func (bs *BlockStore) LoadBlockMeta(height int64) *types.BlockMeta { // LoadBlockMetaByHash returns the blockmeta who's header corresponds to the given // hash. If none is found, returns nil. func (bs *BlockStore) LoadBlockMetaByHash(hash []byte) *types.BlockMeta { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block_meta_by_hash"))() bz, err := bs.db.Get(calcBlockHashKey(hash)) if err != nil { panic(err) @@ -234,6 +241,7 @@ func (bs *BlockStore) LoadBlockMetaByHash(hash []byte) *types.BlockMeta { // and it comes from the block.LastCommit for `height+1`. // If no commit is found for the given height, it returns nil. func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_block_commit"))() var pbc = new(tmproto.Commit) bz, err := bs.db.Get(calcBlockCommitKey(height)) if err != nil { @@ -257,6 +265,7 @@ func (bs *BlockStore) LoadBlockCommit(height int64) *types.Commit { // This is useful when we've seen a commit, but there has not yet been // a new block at `height + 1` that includes this commit in its block.LastCommit. func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "load_seen_commit"))() var pbc = new(tmproto.Commit) bz, err := bs.db.Get(calcSeenCommitKey(height)) if err != nil { @@ -279,6 +288,7 @@ func (bs *BlockStore) LoadSeenCommit(height int64) *types.Commit { // PruneBlocks removes block up to (but not including) a height. It returns number of blocks pruned and the evidence retain height - the height at which data needed to prove evidence must not be removed. func (bs *BlockStore) PruneBlocks(height int64, state sm.State) (uint64, int64, error) { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "prune_blocks"))() if height <= 0 { return 0, -1, fmt.Errorf("height must be greater than 0") } @@ -379,6 +389,7 @@ func (bs *BlockStore) PruneBlocks(height int64, state sm.State) (uint64, int64, // we need this to reload the precommits to catch-up nodes to the // most recent height. Otherwise they'd stall at H-1. func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, seenCommit *types.Commit) { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "save_block"))() if block == nil { panic("BlockStore can only save a non-nil block") } @@ -466,6 +477,7 @@ func (bs *BlockStore) saveState() { // SaveSeenCommit saves a seen commit, used by e.g. the state sync reactor when bootstrapping node. func (bs *BlockStore) SaveSeenCommit(height int64, seenCommit *types.Commit) error { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "save_seen_commit"))() pbc := seenCommit.ToProto() seenCommitBytes, err := proto.Marshal(pbc) if err != nil { @@ -556,6 +568,7 @@ func mustEncode(pb proto.Message) []byte { // DeleteLatestBlock removes the block pointed to by height, // lowering height by one. func (bs *BlockStore) DeleteLatestBlock() error { + defer addTimeSample(bs.metrics.BlockStoreAccessDurationSeconds.With("method", "delete_latest_block"))() bs.mtx.RLock() targetHeight := bs.height bs.mtx.RUnlock() @@ -597,3 +610,10 @@ func (bs *BlockStore) DeleteLatestBlock() error { } return nil } + +func addTimeSample(h metrics.Histogram) func() { + start := time.Now() + return func() { + h.Observe(time.Since(start).Seconds()) + } +}