Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trie storage statistics component #5401

Merged
merged 45 commits into from
Nov 6, 2023
Merged

Conversation

ssd04
Copy link
Contributor

@ssd04 ssd04 commented Jul 6, 2023

Reasoning behind the pull request

  • Added storage statistics components in order to monitor load operations from trie, cache and storage

Testing procedure

  • Enable state statistics by setting StateStatisticsEnabled = true in [StateTriesConfig] section from config.toml
  • Checking (providing) logs for trie storage statistics

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

@ssd04 ssd04 self-assigned this Jul 6, 2023
@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Attention: 29 lines in your changes are missing coverage. Please review.

Comparison is base (7d118bb) 80.34% compared to head (b5f6e9a) 80.34%.
Report is 1 commits behind head on rc/v1.7.0.

❗ Current head b5f6e9a differs from pull request most recent head d2f3f82. Consider uploading reports for the commit d2f3f82 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           rc/v1.7.0    #5401    +/-   ##
===========================================
  Coverage      80.34%   80.34%            
===========================================
  Files            708      709     +1     
  Lines          93768    93922   +154     
===========================================
+ Hits           75335    75459   +124     
- Misses         13149    13173    +24     
- Partials        5284     5290     +6     
Files Coverage Δ
...uestersContainer/baseRequestersContainerFactory.go 75.43% <100.00%> (+0.43%) ⬆️
...uestersContainer/metaRequestersContainerFactory.go 77.40% <100.00%> (+0.38%) ⬆️
...estersContainer/shardRequestersContainerFactory.go 77.61% <100.00%> (+0.33%) ⬆️
epochStart/bootstrap/common.go 100.00% <100.00%> (ø)
epochStart/bootstrap/metaStorageHandler.go 73.97% <100.00%> (+0.17%) ⬆️
epochStart/bootstrap/process.go 84.44% <100.00%> (+0.13%) ⬆️
epochStart/bootstrap/shardStorageHandler.go 88.00% <100.00%> (+0.02%) ⬆️
epochStart/bootstrap/storageProcess.go 73.97% <100.00%> (+0.17%) ⬆️
factory/api/apiResolverFactory.go 83.40% <100.00%> (+0.03%) ⬆️
factory/bootstrap/bootstrapComponents.go 91.97% <100.00%> (+0.04%) ⬆️
... and 19 more

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ssd04 ssd04 marked this pull request as ready for review August 4, 2023 07:19
func (tr *patriciaMerkleTrie) GetStats() string {
dbWithStats, ok := tr.trieStorage.(storageManagerWithStats)
if !ok {
log.Warn("invalid trie storage type %T", tr.trieStorage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will print invalid trie storage type %T.
fmt.Sprintf("invalid trie storage type %T", tr.trieStorage) will print the storage type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

@@ -269,6 +269,17 @@ func (tr *patriciaMerkleTrie) Commit() error {
return nil
}

// GetStats will returns trie storage operations statistics as string
func (tr *patriciaMerkleTrie) GetStats() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add tests for this func

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -116,10 +117,17 @@ type StorageManager interface {
IsInterfaceNil() bool
}

// StorageManagerWithStats defines the behaviour of a storage manager component with stats collector
type StorageManagerWithStats interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed.

@@ -56,6 +56,7 @@ type Trie interface {
VerifyProof(rootHash []byte, key []byte, proof [][]byte) (bool, error)
GetStorageManager() StorageManager
IsMigratedToLatestVersion() (bool, error)
GetStats() string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetStorageStats() ?

@@ -91,6 +91,12 @@ type StorerWithPutInEpoch interface {
SetEpochForPutOperation(epoch uint32)
}

// StorerWithPutInEpoch is an extended storer with the ability to set the epoch which will be used for put operations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StorerWithStats ....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated comment

v, ok := ps.cacher.Get(key)
if ok {
return v.([]byte), nil
return v.([]byte), true, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On L447 replace with if errors.Is(err, storage.ErrDBIsClosed) please

@@ -877,6 +877,7 @@ func (adb *AccountsDB) CommitInEpoch(currentEpoch uint32, epochToCommit uint32)
adb.mutOp.Lock()
defer func() {
adb.mainTrie.GetStorageManager().SetEpochForPutOperation(currentEpoch)
adb.printTrieStorageStatistics()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to also be called on Commit().

The stats need to be reset after each commit if we want to know the stats for each block without adding extra operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move it inside func (adb *AccountsDB) commit() ([]byte, error) . This will remove duplicated code.

if stats != "" {
log.Debug("trie storage statistics",
"storage manager", adb.mainTrie.GetStorageManager().GetIdentifier(),
"stats", adb.mainTrie.GetStats(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use stats instead of adb.mainTrie.GetStats()

@@ -41,3 +50,8 @@ func (tsm *trieStorageManagerWithoutCheckpoints) SetCheckpoint(
func (tsm *trieStorageManagerWithoutCheckpoints) AddDirtyCheckpointHashes(_ []byte, _ common.ModifiedHashes) bool {
return false
}

// GetStatsCollector will return the stats collector component
func (tsm *trieStorageManagerWithoutCheckpoints) GetStatsCollector() common.StateStatisticsHandler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed, as this func is exported on common.StorageManager interface. By removing this, there is no need to have storerWithStats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right 👍

@@ -59,7 +60,7 @@ func (c *chainStorer) GetStorer(unitType dataRetriever.UnitType) (storage.Storer
_, ok := c.mapStorages[unitType]
if !ok {
log.Debug("created new mem storer", "key", unitType)
c.mapStorages[unitType] = CreateMemUnit()
c.mapStorages[unitType] = testscommon.CreateMemStorerWithStats(CreateMemUnit())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we have the MemStorerWithStats somewhere else, not in the testscommon? please create it somewhere else, so that we do not import testscommon in the production code.

@@ -97,59 +97,59 @@ func TestNewSystemSCProcessor(t *testing.T) {
cfg := config.EnableEpochs{
StakingV2EnableEpoch: 100,
}
args, _ := createFullArgumentsForSystemSCProcessing(cfg, createMemUnit())
args, _ := createFullArgumentsForSystemSCProcessing(cfg, testscommon.CreateStorerWithStats())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this creates a memory-StorerWithStats and from the name it is not obvious any more. Could it be renamed to something like: CreateDefaultMemStorerWithStats?

@@ -269,6 +269,17 @@ func (tr *patriciaMerkleTrie) Commit() error {
return nil
}

// GetStorageStats will returns trie storage operations statistics as string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will return, not will returnS

@@ -269,6 +269,17 @@ func (tr *patriciaMerkleTrie) Commit() error {
return nil
}

// GetStorageStats will returns trie storage operations statistics as string
func (tr *patriciaMerkleTrie) GetStorageStats() string {
dbWithStats, ok := tr.trieStorage.(storageManagerWithStats)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this be called many times and fail? there will be a lot of warns in the log.
could all StorageManagers have a GetStatsCollector() that returns "" on ToString()?
if not, could you also add a unit test for the branch !ok?

return nil, storage.ErrNilStatsCollector
}

storerWithStats, ok := args.MainStorer.(common.StorerWithStats)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the args.MainStorer be of type common.StorerWithStats? This way this check is not needed anymore, if the MainStorer has to be always StorerWithStats

@@ -184,17 +197,28 @@ func (tsm *trieStorageManager) Get(key []byte) ([]byte, error) {
return nil, core.ErrContextClosing
}

val, err := tsm.mainStorer.Get(key)
val, foundInCache, err := tsm.mainStorer.GetWithStats(key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the trieStorageManager should know about the caches... could the statsCollector be injected one level lower in a PruningStorerWithStats for example?

}

return nil
return n.resolveCollapsed(pos, db)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the number of resolveCollapsed can be computed from the trieOps and cacheOps?

return v, false, err
}

// CreateMemStorerWithStats -
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper comments, this is productive code

@@ -40,6 +40,7 @@ type patriciaMerkleTrie struct {
hasher hashing.Hasher
enableEpochsHandler common.EnableEpochsHandler
trieNodeVersionVerifier core.TrieNodeVersionVerifier
stateStatsHandler common.StateStatisticsHandler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere

raduchis
raduchis previously approved these changes Oct 26, 2023
@ssd04 ssd04 changed the base branch from rc/v1.6.0 to rc/v1.7.0 October 26, 2023 13:33
@ssd04 ssd04 dismissed stale reviews from BeniaminDrasovean and raduchis October 26, 2023 13:33

The base branch was changed.

gabi-vuls
gabi-vuls previously approved these changes Nov 3, 2023
Copy link
Contributor

@gabi-vuls gabi-vuls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normal allin test: v1.5.13-dev-config-8c685c8e00 -> trie-storage-statistics-co-4c31571a2d

--- Specific errors ---

block hash does not match 6173
wrong nonce in block 2582
miniblocks does not match 0
num miniblocks does not match 0
miniblock hash does not match 0
block bodies does not match 0
receipts hash missmatch 1

/------/

--- Statistics ---

Nr. of all ERRORS: 1
Nr. of all WARNS: 1442
Nr. of new ERRORS: 1
Nr. of new WARNS: 8
Nr. of PANICS: 0

/------/

--- ERRORS ---

/------/

raduchis
raduchis previously approved these changes Nov 3, 2023
@ssd04 ssd04 dismissed stale reviews from raduchis, BeniaminDrasovean, and gabi-vuls via 6a4be57 November 3, 2023 15:01
@ssd04 ssd04 merged commit e6e79c4 into rc/v1.7.0 Nov 6, 2023
4 checks passed
@ssd04 ssd04 deleted the trie-storage-statistics-component branch November 6, 2023 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants