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

use goroutines manager for trie commit #6611

Merged
merged 14 commits into from
Jan 14, 2025

Conversation

BeniaminDrasovean
Copy link
Contributor

@BeniaminDrasovean BeniaminDrasovean commented Nov 14, 2024

Reasoning behind the pull request

  • There are several public trie functions that are all called only when committing the state: GetObsoleteHashes, GetDirtyHashes, GetOldRoot. These can be extracted in a new component

Proposed changes

  • Remove this functions and use a hashesCollector which gathers all the necessary data for a Commit
  • Use goroutines when committing dirty data from a trie.

Testing procedure

  • System tests when all the PRs are merged in the feat branch

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?

@BeniaminDrasovean BeniaminDrasovean marked this pull request as ready for review December 19, 2024 13:36
Copy link
Contributor

Choose a reason for hiding this comment

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

add unit tests on this file

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.

@@ -353,23 +354,30 @@ func (tr *patriciaMerkleTrie) Commit() error {
return nil
}

oldRootHash := tr.GetOldRootHash()
if log.GetLevel() == logger.LogTrace {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this really needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was helpful for some debugging scenarios. I would leave it like this for now.

Comment on lines 237 to 250
encNode, err := bn.getEncodedNode()
if err != nil {
return err
goRoutinesManager.SetError(err)
return
}
hash := bn.hasher.Compute(string(encNode))
bn.hash = hash
hashesCollector.AddDirtyHash(hash)

err = targetDb.Put(hash, encNode)
if err != nil {
goRoutinesManager.SetError(err)
return
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we have like a common function for this code?

Copy link
Contributor

Choose a reason for hiding this comment

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

it seems similar with extention and leaf node code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, extracted in a separate function.

ssd04
ssd04 previously approved these changes Jan 8, 2025
@@ -1,6 +1,7 @@
package syncer

import (
"github.com/multiversx/mx-chain-go/state/hashesCollector"
Copy link
Contributor

Choose a reason for hiding this comment

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

goimport

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.

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/multiversx/mx-chain-go/state/hashesCollector"
Copy link
Contributor

Choose a reason for hiding this comment

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

goimport

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.

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/multiversx/mx-chain-go/state/hashesCollector"
Copy link
Contributor

Choose a reason for hiding this comment

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

goimport

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.


// NewDataTrieHashesCollector creates a new instance of dataTrieHashesCollector.
// This collector is used to collect hashes related to the data trie.
func NewDataTrieHashesCollector() *dataTrieHashesCollector {
Copy link
Contributor

Choose a reason for hiding this comment

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

You make a new one for this every single time ? and that is why you do not have delete function ?

I think adding some capacity on the creation of the oldHashes / newHashes can make processing faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Added a capacity of 10k. That means 10000 hashes => 320kB

oldHashes := make(common.ModifiedHashes)
newHashes := make(common.ModifiedHashes)
hc := hashesCollector.NewDisabledHashesCollector()
if adb.mainTrie.GetStorageManager().IsPruningEnabled() {
Copy link
Contributor

Choose a reason for hiding this comment

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

make a separate function for this 3 lines -> and could we have without NEW at every single commit? like you could have a cleanup function here -> might be faster.

Copy link
Contributor

Choose a reason for hiding this comment

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

for this if you could have a factory - which creates the good structure by default.

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 a cleanup function. There is no need for a factory after the changes since we create the component on the constructor.

}
}

func createTheHashesCollector(mainTrie common.Trie) common.TrieHashesCollector {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func createTheHashesCollector(mainTrie common.Trie) common.TrieHashesCollector {
func createTrieHashesCollector(mainTrie common.Trie) common.TrieHashesCollector {

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed

Base automatically changed from refactor-trie-set-hash to feat/trie-mutex-refactor January 14, 2025 08:23
@BeniaminDrasovean BeniaminDrasovean dismissed ssd04’s stale review January 14, 2025 08:23

The base branch was changed.

@BeniaminDrasovean BeniaminDrasovean merged commit 3e43c9c into feat/trie-mutex-refactor Jan 14, 2025
5 checks passed
@BeniaminDrasovean BeniaminDrasovean deleted the refactor-trie-commit branch January 14, 2025 11:46
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.

3 participants