-
Notifications
You must be signed in to change notification settings - Fork 37.4k
coins: allow cache resize after init #18637
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
a363733
to
03e5fe6
Compare
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. 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. |
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.
Code review ACK 03e5fe6. Very clean, straightforward changes. Feel free to ignore suggestions below, but one I'd consider more is passing up FlushStateToDisk return false status, to be able to log more context information if it fails for some reason
src/txdb.cpp
Outdated
// XXX this should never be called without holding cs_main. Maybe figure out | ||
// a better way of articulating this (lock annotations can't be used due to | ||
// circular dep. with validation). |
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.
In commit "coins: add Sync() method to allow flush without cacheCoins drop" (b2abb39)
AssertLockHeld would be one option
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 just ended up using an extern
declaration for the lock and adding an annotation. Not sure why I didn't do that in the first place.
|
||
//! Test resizing coins-related CChainState caches during runtime. | ||
//! | ||
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches) |
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.
In commit "test: add test for CChainState::ResizeCoinsCaches()" (03e5fe6)
Nice test!
@@ -116,7 +116,6 @@ bool fPruneMode = false; | |||
bool fRequireStandard = true; | |||
bool fCheckBlockIndex = false; | |||
bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED; | |||
size_t nCoinCacheUsage = 5000 * 300; |
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.
In commit "Add CChainState::ResizeCoinsCaches" (df0f720)
Just confirming but looks like this 1.5MB default cache size was only used by test cases previously, which are now using 8MB or 1KB caches depending on the case.
int old_coinstip_size = m_coinstip_cache_size_bytes; | ||
m_coinstip_cache_size_bytes = coinstip_size; | ||
m_coinsdb_cache_size_bytes = coinsdb_size; | ||
CoinsDB().ResizeCache(coinsdb_size); |
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.
In commit "Add CChainState::ResizeCoinsCaches" (df0f720)
Just curious, but would it make sense to call FlushStateToDisk before resetting CoinsDB instead of after? Thought would be that maybe live CDBWrapper object has cached state that would make the flush faster, or maybe it could be good to start with a new CDBWrapper after the flush
03e5fe6
to
bd350f7
Compare
Thanks for the review and good suggestions, @ryanofsky. I've incorporated your feedback in the latest revision. |
bd350f7
to
4cc7b61
Compare
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.
Code review ACK 4cc7b61. Changes since previous review were suggested things: using MakeUnique instead of new, renaming db to m_db while all lines are changing anyway, returning failure from FlushStateToDisk, and adding rebalance test
Should this be added to in progress column https://github.com/bitcoin/bitcoin/projects/11? Also maybe if there's a main assumeutxo pr you'd prioritize for review now it could go on high priority reviews https://github.com/bitcoin/bitcoin/projects/8 |
ee91979
to
4f8e999
Compare
This doesn't compile
|
It does compile locally, so I am not sure how to reproduce this failure :( |
ENTER_CRITICAL_SECTION(cs_main); | ||
CChainState& c1 = manager.InitializeChainstate(); | ||
LEAVE_CRITICAL_SECTION(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.
style-nit in commit 1e6e0b9:
Now that you have to force push anyway, might as well remove the clumsy ENTER_CRITICAL_SECTION
?
6DAF | ENTER_CRITICAL_SECTION(cs_main); |
CChainState& c1 = manager.InitializeChainstate(); | |
LEAVE_CRITICAL_SECTION(cs_main); | |
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate()); |
ENTER_CRITICAL_SECTION(cs_main); | ||
CChainState& c2 = manager.InitializeChainstate(GetRandHash()); | ||
LEAVE_CRITICAL_SECTION(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.
ENTER_CRITICAL_SECTION(cs_main); | |
CChainState& c2 = manager.InitializeChainstate(GetRandHash()); | |
LEAVE_CRITICAL_SECTION(cs_main); | |
CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(GetRandHash())); |
Same
4f8e999
to
d055659
Compare
d055659
to
bb6ba5f
Compare
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.
Code looks great but the warning should be fixed. Will do another pass then.
@@ -139,7 +139,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const | |||
::ChainstateActive().InitCoinsDB( | |||
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); | |||
assert(!::ChainstateActive().CanFlushToDisk()); | |||
::ChainstateActive().InitCoinsCache(); | |||
::ChainstateActive().InitCoinsCache(1 << 23); |
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 looks like it could be a constant?
|
||
{ | ||
LOCK(::cs_main); | ||
c1.InitCoinsCache(1 << 23); |
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.
In the test I think 1 << 23
could also be a variable to help a little bit with readability.
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.
Code review ACK bb6ba5f. Only changes since last review are rebase, replacing int with size_t, replacing ENTER_CRITICAL_SECTION with WITH_LOCK.
There are some open review suggestions above but they seem pretty minor and fine to save for a followup
We'll need this to dynamically update the cache size of the existing CCoinsViewDB instance when we create a new one during snapshot activation. This requires us to keep the CDBWrapper instance as a pointer instead of a reference so that we're able to destruct it and create a new instance when the cache size changes. Also renames `db` to `m_db` since we're already modifying each usage. Includes feedback from Russ Yanofsky.
Also adds CCoinsViewCache::ReallocateCache() to attempt to free memory that the cacheCoins's allocator may be hanging onto when downsizing the cache. Adds `CChainState::m_coins{tip,db}_cache_size_bytes` data members so that we can reference cache size on a per-chainstate basis for flushing.
Aside from in unittests, this method is unused at the moment. It will be used in upcoming commits that enable utxo snapshot activation.
bb6ba5f
to
f19fdd4
Compare
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.
weak utACK f19fdd4 -- didn't find any major problems, but not super confident that I didn't miss anything
EDIT: (update commit fixes init order)
@@ -60,6 +65,9 @@ class CCoinsViewDB final : public CCoinsView | |||
//! Attempt to update from an older database format. Returns whether an error occurred. | |||
bool Upgrade(); | |||
size_t EstimateSize() const override; | |||
|
|||
//! Dynamically alter the underlying leveldb cache size. | |||
void ResizeCache(size_t new_cache_size) 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 seems a bit of an anti-pattern -- it's not ResizeCache() that needs cs_main, it's the fact that when it's called it ends up operating on m_coins_views->m_dbview
which does need cs_main, but that's not enforced because the constraint is thrown away by CoinsDB() returning a reference to the guarded object.
I think it might be better to replace CoinsDB()
by a GetCoinsViews()
that returns a reference to m_coins_views
, and then have auto& x = GetCoinsViews(); LOCK(cs_main); x.m_dbview.ResizeCache(y);
? No need to address in this PR though, I think.
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.
Code review ACK f19fdd4
m_snapshot_chainstate->ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache); | ||
} | ||
else if (m_ibd_chainstate && m_snapshot_chainstate) { | ||
// If both chainstates exist, determine who needs more cache based on IBD status. |
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.
super-nit: A bit more info on the rationale behind 0.05/0.95 would be nice. Also, the values could be constants. But that can be considered for a follow-up.
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.
Code review ACK f19fdd4. Only change since last review is constructor cleanup (no change in behavior). I think the suggestions here from ajtowns and others are good, but shouldn't delay merging the PR (and hold up assumeutxo)
This looks ready to merge |
ENTER_CRITICAL_SECTION(cs_main); | ||
CChainState& c2 = manager.InitializeChainstate(GetRandHash()); | ||
CChainState& c2 = *WITH_LOCK(::cs_main, | ||
return &manager.InitializeChainstate(GetRandHash())); |
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.
Doesn't this lock cs_main
twice ?
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 test nits are fixed in the first commit of #19604
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.
Yep. Brainfart. Just noticed while rebasing #19556 and commented without thinking.
f19fdd4 test: add test for CChainState::ResizeCoinsCaches() (James O'Beirne) 8ac3ef4 add ChainstateManager::MaybeRebalanceCaches() (James O'Beirne) f36aaa6 Add CChainState::ResizeCoinsCaches (James O'Beirne) b223111 txdb: add CCoinsViewDB::ChangeCacheSize (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: bitcoin#15606 Issue: bitcoin#15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the assumeutxo implementation draft (bitcoin#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per `dbcache=`) to repurpose for the snapshot chainstate. Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate. This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces `ActivateSnapshot`). `ChainstateManager::MaybeRebalanceCaches()` defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there. ACKs for top commit: ajtowns: weak utACK f19fdd4 -- didn't find any major problems, but not super confident that I didn't miss anything fjahr: Code review ACK f19fdd4 ryanofsky: Code review ACK f19fdd4. Only change since last review is constructor cleanup (no change in behavior). I think the suggestions here from ajtowns and others are good, but shouldn't delay merging the PR (and hold up assumeutxo) Tree-SHA512: fffb7847fb6993dd4a1a41cf11179b211b0b20b7eb5f7cf6266442136bfe9d43b830bbefcafd475bfd4af273f5573500594aa41fff03e0ed5c2a1e8562ff9269
Summary: f19fdd47a6371dcbe0760ef6f3c3c5adb31b1bb4 test: add test for CChainState::ResizeCoinsCaches() (James O'Beirne) 8ac3ef46999ed676ca3775f7b2f461d92f09a542 add ChainstateManager::MaybeRebalanceCaches() (James O'Beirne) f36aaa6392fdbdac6891d92202d3efeff98754f4 Add CChainState::ResizeCoinsCaches (James O'Beirne) b223111da2e0e9ceccef75df8a20252b0094b7bc txdb: add CCoinsViewDB::ChangeCacheSize (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the assumeutxo implementation draft (#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per `dbcache=`) to repurpose for the snapshot chainstate. Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate. This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces `ActivateSnapshot`). `ChainstateManager::MaybeRebalanceCaches()` defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there. --- Backport of [[bitcoin/bitcoin#18637 | core#18637]] Test Plan: ninja all check check-functional Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Subscribers: PiRK Differential Revision: https://reviews.bitcoinabc.org/D9259
Summary: f19fdd47a6371dcbe0760ef6f3c3c5adb31b1bb4 test: add test for CChainState::ResizeCoinsCaches() (James O'Beirne) 8ac3ef46999ed676ca3775f7b2f461d92f09a542 add ChainstateManager::MaybeRebalanceCaches() (James O'Beirne) f36aaa6392fdbdac6891d92202d3efeff98754f4 Add CChainState::ResizeCoinsCaches (James O'Beirne) b223111da2e0e9ceccef75df8a20252b0094b7bc txdb: add CCoinsViewDB::ChangeCacheSize (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the assumeutxo implementation draft (#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per `dbcache=`) to repurpose for the snapshot chainstate. Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate. This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces `ActivateSnapshot`). `ChainstateManager::MaybeRebalanceCaches()` defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there. --- Backport of [[bitcoin/bitcoin#18637 | core#18637]] Test Plan: ninja all check check-functional Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Subscribers: PiRK Differential Revision: https://reviews.bitcoinabc.org/D9259
Summary: f19fdd47a6371dcbe0760ef6f3c3c5adb31b1bb4 test: add test for CChainState::ResizeCoinsCaches() (James O'Beirne) 8ac3ef46999ed676ca3775f7b2f461d92f09a542 add ChainstateManager::MaybeRebalanceCaches() (James O'Beirne) f36aaa6392fdbdac6891d92202d3efeff98754f4 Add CChainState::ResizeCoinsCaches (James O'Beirne) b223111da2e0e9ceccef75df8a20252b0094b7bc txdb: add CCoinsViewDB::ChangeCacheSize (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the assumeutxo implementation draft (#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per `dbcache=`) to repurpose for the snapshot chainstate. Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate. This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces `ActivateSnapshot`). `ChainstateManager::MaybeRebalanceCaches()` defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there. --- Backport of [[bitcoin/bitcoin#18637 | core#18637]] Test Plan: ninja all check check-functional Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Subscribers: PiRK Differential Revision: https://reviews.bitcoinabc.org/D9259
Summary: f19fdd47a6371dcbe0760ef6f3c3c5adb31b1bb4 test: add test for CChainState::ResizeCoinsCaches() (James O'Beirne) 8ac3ef46999ed676ca3775f7b2f461d92f09a542 add ChainstateManager::MaybeRebalanceCaches() (James O'Beirne) f36aaa6392fdbdac6891d92202d3efeff98754f4 Add CChainState::ResizeCoinsCaches (James O'Beirne) b223111da2e0e9ceccef75df8a20252b0094b7bc txdb: add CCoinsViewDB::ChangeCacheSize (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the assumeutxo implementation draft (#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per `dbcache=`) to repurpose for the snapshot chainstate. Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate. This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces `ActivateSnapshot`). `ChainstateManager::MaybeRebalanceCaches()` defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there. --- Backport of [[bitcoin/bitcoin#18637 | core#18637]] Test Plan: ninja all check check-functional Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Subscribers: PiRK Differential Revision: https://reviews.bitcoinabc.org/D9259
This is part of the assumeutxo project:
Parent PR: #15606
Issue: #15605
Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal
In the assumeutxo implementation draft (#15056), once a UTXO snapshot is loaded, a new chainstate object is created after initialization. This means that we have to reclaim some of the cache that we've allocated to the original chainstate (per
dbcache=
) to repurpose for the snapshot chainstate.Furthermore, it makes sense to have different cache allocations depending on which chainstate is more active. While the snapshot chainstate is working to get to the network tip (and the background validation chainstate is idle), it makes sense that the snapshot chainstate should have the majority of cache allocation. And contrariwise once the snapshot has reached network tip, most of the cache should be given to the background validation chainstate.
This set of changes (detailed in the commit messages) allows us to dynamically resize the various coins caches. None of the functionality introduced here is used at the moment, but will be in the next AU PR (which introduces
ActivateSnapshot
).ChainstateManager::MaybeRebalanceCaches()
defines the (somewhat normative) cache allocations between the snapshot and background validation chainstates. I'd be interested in feedback if anyone has thoughts on the proportions I've set there.