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

update trie on goroutines #6463

Merged
merged 11 commits into from
Dec 9, 2024

Conversation

BeniaminDrasovean
Copy link
Contributor

@BeniaminDrasovean BeniaminDrasovean commented Sep 17, 2024

Reasoning behind the pull request

  • All the trie changes are put in a batch. When a commit occurs, the changes batch is added to the trie, but the changes are done in serial.

Proposed changes

  • When the changes from the batch need to be added to the trie, use goroutines where possible in order to speed up the update of the trie

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 changed the title refactor the trie nodes insert functions to use goroutines update trie on goroutines Sep 25, 2024
@BeniaminDrasovean BeniaminDrasovean marked this pull request as ready for review October 1, 2024 12:36
Comment on lines 495 to 503
newModifiedHashes, bnModified := bn.insertOnChild(dataForInsertion[childPos], childPos, goRoutinesManager, db)
if bnModified {
bnHasBeenModified.SetValue(true)
}
if len(newModifiedHashes) != 0 {
hashesMutex.Lock()
modifiedHashes = append(modifiedHashes, newModifiedHashes...)
hashesMutex.Unlock()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

not a strong opinion on this, but we might consider extracting this block to a function even if there are a lot of parameters to pass

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, will extract in a different function in order to avoid duplicated 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.

Refactored

func (bn *branchNode) deleteChild(
dataForRemoval []core.TrieData,
childPos int,
hasBeenModified *atomic.Flag,
Copy link
Contributor

Choose a reason for hiding this comment

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

return hasBeenModified as a variable similar to insertOnChild and set atomic flag as in insert case? this way the dataForInsertion(Removal) flows are very similar, they might be able to be extracted in a common function or component since insetOnChild and deleteChild would have the same singature

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored.

Comment on lines +785 to +786
bn.mutex.Lock()
defer bn.mutex.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

for which variable(s) is the lock? i think we need to add lock also in other places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are for dirty, hash, version. Indeed, this will need to be added in other places. Will do that in another PR, in which I will also write some tests for this cases.

Comment on lines 629 to 631
th, _ := throttler.NewNumGoRoutinesThrottler(5)
goRoutinesManager, err := NewGoroutinesManager(th, errChan.NewErrChanWrapper(), make(chan struct{}))
assert.Nil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

add a function for creating goroutinesManager?

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.

Comment on lines +59 to +61
gm.mutex.Lock()
defer gm.mutex.Unlock()

Copy link
Contributor

Choose a reason for hiding this comment

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

do we need mutex here?

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, because the same manager will be passed on multiple goroutines.

Comment on lines +72 to +73
gm.mutex.Lock()
defer gm.mutex.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

here also?

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, because the same manager will be passed on multiple goroutines.


err = errCh.ReadFromChanNonBlocking()
assert.Equal(t, expectedErr, err)
assert.False(t, manager.(*goroutinesManager).canProcess)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe better expose canProcess in export_test.go to avoid casting here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, the problem was that the constructor was returning an interface, not the actual type. Changed the constructor.

// NewModifiedHashesSlice is used to create a new instance of modifiedHashesSlice
func NewModifiedHashesSlice() *modifiedHashesSlice {
return &modifiedHashesSlice{
hashes: make([][]byte, 0),
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to allocate some capacity to the hashes.

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.

@@ -80,6 +83,12 @@ func NewTrie(
return nil, err
}

// TODO give this as an argument
trieThrottler, err := throttler.NewNumGoRoutinesThrottler(20)
Copy link
Contributor

Choose a reason for hiding this comment

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

only the config of how many go routines is enough for the arg.

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, that is what I was also thinking, but phrased it wrong. Rephrased the TODO. It will be implemented in a future PR.

@@ -203,7 +213,14 @@ func (tr *patriciaMerkleTrie) insertBatch(sortedDataForInsertion []core.TrieData
tr.oldRoot = tr.root.getHash()
}

newRoot, oldHashes, err := tr.root.insert(sortedDataForInsertion, tr.trieStorage)
manager, err := NewGoroutinesManager(tr.goroutinesThrottler, errChan.NewErrChanWrapper(), tr.chanClose)
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need to recreate this manager every time ?

can't you create it once and only clean up here ?

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.

@@ -232,11 +250,20 @@ func (tr *patriciaMerkleTrie) deleteBatch(data []core.TrieData) error {
tr.oldRoot = tr.root.getHash()
}

_, newRoot, oldHashes, err := tr.root.delete(data, tr.trieStorage)
manager, err := NewGoroutinesManager(tr.goroutinesThrottler, errChan.NewErrChanWrapper(), tr.chanClose)
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above ? can't you have the manager created once, and only clean it up here ?

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.

sasurobert
sasurobert previously approved these changes Dec 5, 2024
mhs.Lock()
defer mhs.Unlock()

mhs.hashes = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

set to empty slice as in constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed func as it was not used anywhere. Will add it again if needed.

assert.NotEqual(t, retrievedHashes, newRetrievedHashes)
}

func TestModifiedHashesSlice_Reset(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

add a test with all these operations combined? append, reset, append

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the Reset() func

@@ -1,5 +1,7 @@
package common

import "sync"

// ModifiedHashes is used to memorize all old hashes and new hashes from when a trie is committed
type ModifiedHashes map[string]struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

do we still need this struct?

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, it is still used in other places.

Base automatically changed from update-trie-from-batch to feat/trie-mutex-refactor December 5, 2024 13:55
@BeniaminDrasovean BeniaminDrasovean dismissed sasurobert’s stale review December 5, 2024 13:55

The base branch was changed.

@BeniaminDrasovean BeniaminDrasovean merged commit c856942 into feat/trie-mutex-refactor Dec 9, 2024
5 checks passed
@BeniaminDrasovean BeniaminDrasovean deleted the update-trie-on-goroutines branch December 9, 2024 09:30
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