Skip to content

Commit

Permalink
gossamer_storage_tries_cached_total metric
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 7, 2022
1 parent fbd13d2 commit 3054001
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 46 deletions.
160 changes: 160 additions & 0 deletions dot/state/mock_gauge_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type StorageState struct {

// NewStorageState creates a new StorageState backed by the given trie and database located at basePath.
func NewStorageState(db chaindb.Database, blockState *BlockState,
t *trie.Trie, onlinePruner pruner.Config) (*StorageState, error) {
t *trie.Trie, onlinePruner pruner.Config) (
*StorageState, error) {
if db == nil {
return nil, fmt.Errorf("cannot have nil database")
}
Expand All @@ -52,7 +53,10 @@ func NewStorageState(db chaindb.Database, blockState *BlockState,
return nil, fmt.Errorf("cannot have nil trie")
}

tries := newTries(t)
tries, err := newTries(t)
if err != nil {
return nil, fmt.Errorf("cannot create in-memory tries: %w", err)
}

storageTable := chaindb.NewTable(db, storagePrefix)

Expand Down
21 changes: 19 additions & 2 deletions dot/state/tries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@
package state

import (
"errors"
"fmt"
"sync"

"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/prometheus/client_golang/prometheus"
)

type tries struct {
rootToTrie map[common.Hash]*trie.Trie
mapMutex sync.RWMutex
triesGauge prometheus.Gauge
}

func newTries(t *trie.Trie) *tries {
func newTries(t *trie.Trie) (trs *tries, err error) {
triesGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "gossamer_storage",
Name: "tries_cached_total",
Help: "total number of tries cached in memory",
})
err = prometheus.Register(triesGauge)
if err != nil && !errors.As(err, &prometheus.AlreadyRegisteredError{}) {
return nil, fmt.Errorf("cannot register tries gauge: %w", err)
}

return &tries{
rootToTrie: map[common.Hash]*trie.Trie{
t.MustHash(): t,
},
}
triesGauge: triesGauge,
}, nil
}

// softSet sets the given trie at the given root hash
Expand All @@ -34,13 +49,15 @@ func (t *tries) softSet(root common.Hash, trie *trie.Trie) {
return
}

t.triesGauge.Inc()
t.rootToTrie[root] = trie
}

func (t *tries) delete(root common.Hash) {
t.mapMutex.Lock()
defer t.mapMutex.Unlock()
delete(t.rootToTrie, root)
t.triesGauge.Set(float64(len(t.rootToTrie)))
}

// get retrieves the trie corresponding to the root hash given
Expand Down
Loading

0 comments on commit 3054001

Please sign in to comment.