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

[R4R]: Redesign triePrefetcher to make it thread safe #972

Merged
merged 10 commits into from
Jul 7, 2022

Conversation

setunapo
Copy link
Contributor

@setunapo setunapo commented Jun 30, 2022

Description

There are 2 types of triePrefetcher instances.
1.New created triePrefetcher: it is the one to do trie prefetch to speed up validation phase.
2.Copied triePrefetcher: it just do a copy of the prefetched trie, it won't do prefetch at all. The copied tries are all kept in p.fetches.

In thie PR, we try to improve the new created one, to make it concurrent safe, while the copied one's behavior stay unchanged(its logic is very simple).

Rationale

As commented in triePrefetcher struct, its APIs are not thread safe. So callers should make sure the created triePrefetcher should be used within a single routine. As we are trying to improve triePrefetcher, we would use it concurrently, so it is necessary to redesign it for concurrent access.

The redesigned workflow:
1.start a mainLoop to do the real trie prefetch work, caller could schedule prefetch request through channel message.
2.as the trie prefetch close operation is only allowed to do once and will be processed in the mainLoop too, since it will update the fetchers informantion.
3.The other operations will be done outside of the mainLoop for performance concern, since mainLoop could be too busy if all the works are done within the mainLoop. The action lists are: trie(),used(), copy(), these actions can be done in parallel with a simple read lock.

There is no obvious performance impact.
image

Changes

No impact to users.

@setunapo setunapo changed the title [WIP]: Redesign triePrefetch to make it thread safe [WIP]: Redesign triePrefetcher to make it thread safe Jun 30, 2022
@setunapo setunapo force-pushed the redesign_triePrefetch branch 4 times, most recently from a8ee0a1 to 244f26c Compare July 1, 2022 02:17
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
@qinglin89
Copy link
Contributor

too many channels and messages communication, actually only fetchers need to be taken care if want the trie_prefetecher be concurrent access.

Copy link
Contributor Author

@setunapo setunapo left a comment

Choose a reason for hiding this comment

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

Replied

@setunapo setunapo force-pushed the redesign_triePrefetch branch 3 times, most recently from 843785c to faf01f0 Compare July 4, 2022 02:09
@setunapo setunapo changed the title [WIP]: Redesign triePrefetcher to make it thread safe [R4R]: Redesign triePrefetcher to make it thread safe Jul 4, 2022
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
core/state/trie_prefetcher.go Outdated Show resolved Hide resolved
@setunapo setunapo dismissed a stale review via bdf21e8 July 4, 2022 12:33
@setunapo setunapo force-pushed the redesign_triePrefetch branch 2 times, most recently from 1cd350f to 8900d55 Compare July 5, 2022 01:18
core/state/trie_prefetcher.go Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
core/state/trie_prefetcher.go Show resolved Hide resolved
qinglin89
qinglin89 previously approved these changes Jul 5, 2022
forcodedancing
forcodedancing previously approved these changes Jul 5, 2022
yutianwu
yutianwu previously approved these changes Jul 6, 2022
unclezoro
unclezoro previously approved these changes Jul 6, 2022
j75689
j75689 previously approved these changes Jul 6, 2022
qinglin89
qinglin89 previously approved these changes Jul 6, 2022
There are 2 types of triePrefetcher instances:
1.New created triePrefetcher: it is key to do trie prefetch to speed up validation phase.
2.Copied triePrefetcher: it only copy the prefetched trie information, actually it won't do
  prefetch at all, the copied tries are all kept in p.fetches.

Here we try to improve the new created one, to make it concurrent safe, while the copied one's
behavior stay unchanged(its logic is very simple).
As commented in triePrefetcher struct, its APIs are not thread safe. So callers should make sure
the created triePrefetcher should be used within a single routine.
As we are trying to improve triePrefetcher, we would use it concurrently, so it is necessary to
redesign it for concurrent access.

The design is simple:
** start a mainLoop to do all the work, APIs just send channel message.

Others:
** remove the metrics copy, since it is useless for copied triePrefetcher
** for trie(), only get subfetcher through channel to reduce the workload of mainloop
** make close concurrent safe
** fix potential deadlock
For APIs like: trie(), copy(), used(), it is simpler and more efficient to
use a RWMutex instead of channel communicaton.
Since the mainLoop would be busy handling trie request, while these trie request
can be processed in parallism.

We would only keep prefetch and close within the mainLoop, since they could update
the fetchers
trie prefetcher’s behavior has changed, prefetch() won't create subfetcher immediately.
it is reasonable, but break the UT, to fix the failed UT
Copy link
Collaborator

@brilliant-lx brilliant-lx left a comment

Choose a reason for hiding this comment

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

SGTM

@unclezoro unclezoro merged commit 8e74562 into bnb-chain:develop Jul 7, 2022
This was referenced Jul 28, 2022
@setunapo setunapo deleted the redesign_triePrefetch branch February 5, 2023 04:41
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.

8 participants