-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Cache m_cached_finished_ibd where SetTip is called. #32885
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
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32885. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
c5084ac to
5e85698
Compare
0709758 to
f3ee281
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
f3ee281 to
2e3fefb
Compare
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conceptually not a bad idea to cache and lock less, but imo this makes the code more brittle (and harder to understand), e.g. if any tip updates happen without the cache being updated separately.
Do you have any data as to the actual performance improvements from this PR?
furszy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like this, if properly implemented (I haven't thought much about the code yet), would reduce the GUI freezes during IBD in a noticeable manner.
I'm (very) open to suggestions on how to make the caching call more robust. (Indeed I expected some.) There's no performance improvement from this PR, it's the first in a series of proposed changes I'll be making to remove locking where it's not necessary, with the end goal being some form of concurrency being possible in message processing. |
I hadn't even considered that, but certainly that's a possible direct improvement. |
|
tight polling of is_ibd seems like a mistake in the first place, so i am not sure if this is something to optimize for. Looking at the remaining call sites of the ibd check, most have cs_main already, so they won't be affected by this? The remaining ones (I only found |
luke-jr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK, but I'm not convinced this implementation is safe as-is. If we want to maintain the current behaviour, it's not sufficient to update only when the tip changes. We also need to re-check when importing/reindexing completes, and schedule an update timer if max_tip_age is the final cause of not exiting IBD.
This made me revisit the function and consider what we're trying to achieve. The function is only interesting when it can latch to the IBD finished state. That's only possible when all four conditions are met, which can only happen when the tip is updated. The final time based condition can only change when the tip changes as it gets further away with time, not closer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function is only interesting when it can latch to the IBD finished state.
That's only possible when all four conditions are met, which can only happen when the tip is updated.
The final time based condition can only change when the tip changes as it gets further away with time, not closer.
I think that @luke-jr is right. If we reindex, we set m_importing to true in ImportBlocks, so any blocks we connect there can never result in getting out of IBD due to the m_blockman.LoadingBlocks() early return.
Therefore we need a call to CacheIsInitialBlockDownload() after ImportingNow goes out of scope in ImportBlocks().
src/validation.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First I thought this was not necessary because disconnecting a block shouldn't usually get you out of IBD, but I guess there are edge cases (starting up, with the old tip having a lower timestamp than it's parent block) where this could lead to get us out of IBD?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's technically possible for disconnecting a block to get us out of IBD, though I really don't think that particular edge case is super important.
I was just trying to be thorough.
src/validation.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the function is annotated with EXCLUSIVE_LOCKS_REQUIRED(cs_main) anyway, why not put it to the beginning of the function, as it is done in most other places?
2e3fefb to
116bdc7
Compare
|
Ok I thought about it and it just wasn't obviously correct enough. So I've rewritten into three commits to be simpler. |
…ntipRecent. On systems with sane clocks the chain tip checks can only change when the tip changes. The gap between the chain tip and the current time only grows.
116bdc7 to
99df6bf
Compare
|
🐙 This pull request conflicts with the target branch and needs rebase. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK, makes sense to push the burden to the writer instead of the reader.
But we need to restructure it slightly so that it tells a story of what we're extracting, delegating and caching exactly.
I have implemented an example in l0rinc#60 (prototype, may not pass all tests yet).
| void TestChainstateManager::ResetIbd() | ||
| { | ||
| m_cached_finished_ibd = false; | ||
| m_cached_chaintip_recent = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the existence of his whole method very hacky, we're testing something that cannot happen in reality so if the test passes or fails after this, it won't increase my confidence in the product.
But if you insist on updating it (which we likely have to), we should update JumpOutOfIbd as well for symmetry.
| return false; | ||
| } | ||
|
|
||
| void ChainstateManager::UpdateCachedChaintipRecent() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're introducing dead code in the first commit without context about where these values are coming from.
What if instead we extract the internal checks from IsInitialBlockDownload and slowly migrate that behavior away from there.
Note also that ActiveTip() already returns the tip we need.
I'm also not exactly sure why we're calling the current state "cached".
And we're already in ChainstateManager, simply referring to "tip" is already unambiguous.
The first commit could lay the groundwork by extracting-and-reusing the recency check only, the second commit could route active chain SetTip through ChainstateManager to make sure each state change updates this as well, the third commit could cache the locked recency calculations, and the last one could finally eliminate the lock from the reader side.
|
|
||
| /** Check whether we are doing an initial block download (synchronizing from disk or network) */ | ||
| bool IsInitialBlockDownload() const; | ||
| void UpdateCachedChaintipRecent() EXCLUSIVE_LOCKS_REQUIRED(cs_main); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be const getter instead and it could use some comment (and I'd specialize it to just return the value instead of mutating the state, we can do that in the SetTip method instead)
| void UpdateCachedChaintipRecent() EXCLUSIVE_LOCKS_REQUIRED(cs_main); | |
| /** Check whether the active chain tip exists, has enough work, and is recent. */ | |
| bool IsTipRecent() const EXCLUSIVE_LOCKS_REQUIRED(cs_main); |
| } | ||
| } | ||
|
|
||
| m_chain.SetTip(*pindexDelete->pprev); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the reason for separating this work from SetTip, if it's related to it? We could move it to the manager which would call both method, the tip update, followed by the IBD state update
| if (chain.Tip()->nChainWork < MinimumChainWork()) return; | ||
| if (chain.Tip()->Time() < Now<NodeSeconds>() - m_options.max_tip_age) return; | ||
|
|
||
| m_cached_chaintip_recent = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we guard this method with this being false?
| * const, which latches this for caching purposes. | ||
| */ | ||
| mutable std::atomic<bool> m_cached_finished_ibd{false}; | ||
| mutable std::atomic<bool> m_cached_chaintip_recent{false}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to mention chain here and we can use std::atomic_bool instead and should add some description to it
|
@pstratem, are you still working on this or would you like me to take over? |
|
There hasn't been progress here in many months. Maybe time to re-open it? |
|
I will open an alternative PR for this today |
As IsInitialBlockDownload latches to false only once the Tip is sufficiently
advanced there is no need to check the Tip everytime IsIBD is called.
By caching this in advance we can avoid extra work and more importantly a lock.