-
Notifications
You must be signed in to change notification settings - Fork 38.7k
coins: allow write to disk without cache drop #17487
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
d5a0621 to
bd44f51
Compare
maflcko
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.
Does this speed up IBD with low dbcache? If so, a benchmark would be nice
No, this PR doesn't change any existing behavior - it simply introduces the option to avoid erasing the cache (which is later used by assumeutxo's snapshot activation code). This changeset shouldn't cause any sort of performance difference. |
ariard
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.
Code Review ACK bd44f51
Can you slightly note in commit message than this change is not used right now ?
CCoinsViewCache::Flush is called in FlushStateToDisk, DisconnectTip, ConnectTip, ReplayBlocks but as new default argument is true there shouldn't be behavior change.
src/coins.h
Outdated
| * If false is returned, the state of this cache (and its backing view) will be undefined. | ||
| */ | ||
| bool Flush(); | ||
| bool Flush(bool clear_cache = 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.
Isn't passing the argument explicitly better than with a default one? Also it's worthy to extend method comment a bit with effect of argument on cache.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. 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. |
|
Code Review ACK bd44f51 |
bd44f51 to
a22b1fb
Compare
|
Thanks for the looks, everyone. I've incorporated feedback from @MarcoFalke and @ariard. (diff) |
|
Why is it that you don't need this bit in your followup PR? https://github.com/bitcoin/bitcoin/pull/15265/files#diff-cd7b305fd4b4280f22ae88960e60398eR210-R222 |
|
Given that this does not improve performance (see #15265 (comment)), is this needed at all? If so, sharing a benchmark with steps to reproduce would be helpful. Anyway, I'd like to see the tests in |
|
Concept ACK, but:
|
|
Alright, given the controversy here now is probably as good a time as any to back up and figure out exactly what
I'll get back with some results in the next few days, and hopefully will write up an elucidation on where exactly CCoinsViewCache does or doesn't provide benefit. (Though I wonder how cross-platform differences beyond SSD-or-not play into this, e.g. the cache may or may not show importance based on leveldb's use of mmap & |
FWIW, #17398 adds leveldb mmap support on Windows, might want to try with and without that PR when doing benchmarking on Windows. |
|
After benching, I stand by this change as being significantly useful. I compare HEAD of the assumeutxo parent PR ( The result is a significant degradation in performance on spinning disk hosts (4.5 hours vs. 1.5 hours) - see the This benchmark is reproducible by running this script: ./bin/run_remote.py au --hosts [hostnames ...]I'll add tests to make sure this change works as-advertised, but I think it's pretty clearly a useful option for us to have. |
|
@sdaftuar just pointed out to me that this code is very wrong - because I don't wipe the flags of flushed coins, this code ends up with an incorrect on-disk UTXO set (since a coin with the |
|
(FWIW @Sjors wins Employee of the Month for spotting this bug in a previous comment.) |
|
Took me a while to understand what you're doing here. To rephrase in my own words: when you load the snapshot from disk you end up with a coin cache at e.g. 600,000. Depending on the users dbcache setting, part of this is already in RAM, which is potentially nice when continuing IBD. jamesob@85a73a0#diff-24efdb00bfbe56b140fb006b562cc70bL5514-L5530 If -dbcache is less than the size of the snapshot, the only the most recent (?) coins of the snapshot will in RAM, the rest would already have been flushed. This is probably similar to what happens during a normal IBD when dbcache overflows. (we could use some heuristics to sort coins by life expectancy) For some reason (why?) you need to flush at the end of loading the snapshot, which normally means no coins are in RAM. This PR changes that last flush to keep stuff around. IIUC the main benefit of this cache is to reduce the number of unnecessary writes, i.e. when a coin is created and then destroyed we save 2 disk writes. But when we flush, even without deleting the coins from RAM, we expect 1 write if the coin is spent before the tip, otherwise no write. Looking forward to the new benchmark. I suggest doing this with a ~1 month old snapshot (realistic for users who download immediately after a 1 month release candidates) and a 4 month old snapshot (the average age if we ship every 6 months). |
|
I'm surprised to report that even after fixing the (consensus!) bug that @Sjors and @sdaftuar pointed out, this change still shows considerable improvement for the assumeutxo snapshot-load use. We're still seeing 3x reduction in time with this change applied for spinning disks while doing a from-600k snapshot IBD. Applying @sdaftuar's fix (here in
Thanks for the nice summary, @Sjors. I'm not sure that we necessarily need to flush after loading the snapshot, but I figured it was prudent to ensure that we'd resume the snapshot-based sync even after an unclean shutdown - maybe it's better to just omit the Tangentially, @sdaftuar mentioned the other day a potential technique for partial flushes that may accomplish the kind of optimization that this branch and #15265 are aiming for. The idea would be to allocate some of the I think this is a promising idea and I'll be experimenting with it soon - though of course that's outside the scope of this PR. |
|
What about using |
a22b1fb to
1e68aad
Compare
|
I've pushed some test coverage for the new Throughout the course of writing (and debugging) the tests, I found yet another bug that had to do with |
src/coins.cpp
Outdated
| } else { | ||
| // Instead of clearing the cache, just clear the FRESH/DIRTY | ||
| // flags, and erase any spent coins | ||
| for (auto it = cacheCoins.begin(); it != cacheCoins.end(); ) { |
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.
Is it true that this loop is very slow with high dbcache? If so, a comment would be appropriate.
Other than that I also agree with @ryanofsky comment to have "separate methods like Flush() and Sync() or Flush() and Write()". As a rule of thumb, if two functions have a completely different implementation, they should not be bundled in one and switched by a boolean.
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 my own benchmarking, I haven't seen anything to support the notion that this loop takes a long time. In the newest benchmarks (to be posted), calling Flush(erase=true) on a cache of size 3826 MB takes 7:17 and calling Flush(erase=false) on a cache of the same size takes 6:03 so the difference is either negligible or favorable.
I got the times by subtracting timestamps from the last and second-to-last loglines below for each branch during snapshot activation.
Before this change
2019-12-03T23:18:26Z [snapshot] 63000000 coins loaded (99.39%, 3826 MB)
2019-12-03T23:18:26Z [snapshot] loaded 63389760 (3877 MB) coins from snapshot 00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91
2019-12-03T23:18:26Z [snapshot] flushing snapshot chainstate to disk
2019-12-03T23:25:43Z [snapshot] validated snapshot (592 MB)
After this change
2019-12-03T23:48:09Z [snapshot] 63000000 coins loaded (99.39%, 3826 MB)
2019-12-03T23:48:10Z [snapshot] loaded 63389760 (3877 MB) coins from snapshot 00000000000000000007316856900e76b4f7a9139cfbfba89842c8d196cd5f91
2019-12-03T23:48:10Z [snapshot] flushing snapshot chainstate to disk
2019-12-03T23:54:13Z [snapshot] validated snapshot (3877 MB)
I'll make the Flush() + Sync() change.
|
Latest round of benchmarks for the current tip of this branch continues to show good performance improvements for UTXO snapshot load, even on SSD. (same setup as #17487 (comment)) |
1e68aad to
eebaca7
Compare
Benchmark results available here: bitcoin#17487 (comment)
Benchmark results available here: bitcoin#17487 (comment)
… >2x block connection speed 4a6d1d1 validation: don't clear cache on periodic flush (Andrew Toth) Pull request description: Since bitcoin#17487 we no longer need to clear the coins cache when syncing to disk. A warm coins cache significantly speeds up block connection, and only needs to be fully flushed when nearing the `dbcache` limit. Periodic flushes occur every 24 hours, which empties the cache and causes block connection to slow down. By keeping the cache through periodic flushes a node can run for several days with an increasingly hotter cache and connect blocks much more quickly. Now not only can setting a higher `dbcache` value be beneficial for IBD, it can also be beneficial for connecting blocks faster. To benchmark in real world usage, I spun up 6 identical `t2.small` AWS EC2 instances, all running in the same region in the same VPC. I configured 2 instances to run master, 2 instances to run the change in this PR, and 2 instances to run the change in this PR but with `dbcache=1000`. All instances had `prune=5000` and a 20 GB `gp2` `EBS` volume. A 7th EC2 instance in the same VPC ran master and connected only to some trusted nodes in the outside network. Each of the 6 nodes under test only connected directly to this 7th instance. I manually pruned as much as possible and uploaded the same `blocks`, `chainstate` and `mempool.dat` to all instances. I started all 6 peers simultaneously at block height `835245` and ran them for over a week until block `836534`. The results were much faster block connection times for this branch compared to master, and much faster for this branch with `dbcache=1000` compared to default `dbcache`. | branch |speed | |-----------:|----------:| | master 1 | 1995.49ms/blk | | master 2 | 2129.78ms/blk | | branch default dbcache 1 | 1189.65ms/blk | | branch default dbcache 2 | 1037.74ms/blk | | branch dbcache=1000 1 | 393.69ms/blk | | branch dbcache=1000 2 | 427.77ms/blk | The log files of all 6 instances are [here](https://gist.github.com/andrewtoth/03c95033e7581d5dbc5be028639a1a91). There is a lot of noise with the exact times of blocks being connected, so I plotted the rolling 20 block connect time averages. The large dots are the times where the cache is emptied. For the red master nodes, this happens every 24 hours. The blue branch nodes with default `dbcache` only filled up and emptied the caches once, which is seen in the middle. The green branch nodes with 1000 `dbcache` never emptied the cache. It is very clear from the chart that whenever the cache is emptied, connect block speed degrades significantly.  Also note that this still clears the cache for pruning flushes. Having frequent pruning flushes with a large cache that doesn't clear is less performant than the status quo bitcoin#15265 (comment). See bitcoin#28280. ACKs for top commit: sipa: utACK 4a6d1d1 achow101: ACK 4a6d1d1 brunoerg: crACK 4a6d1d1 Tree-SHA512: 05dbc677bc309bbcf89c52a6c5e853e2816b0ef0b5ee3719b30696df315a0427e244bb82da9ad828ec0e7ea8764552f8affe14c0184b52adf1909f5d8c1b4f9e
589db87 validation: don't erase coins cache on prune flushes (Andrew Toth) 0e89187 Add linked-list test to CCoinsViewCache::SanityCheck (Pieter Wuille) 05cf4e1 coins: move Sync logic to CoinsViewCacheCursor (Andrew Toth) 7825b8b coins: pass linked list of flagged entries to BatchWrite (Andrew Toth) a14edad test: add cache entry linked list tests (Andrew Toth) 24ce37c coins: track flagged cache entries in linked list (Andrew Toth) 58b7ed1 coins: call ClearFlags in CCoinsCacheEntry destructor (Andrew Toth) 8bd3959 refactor: require self and sentinel parameters for AddFlags (Andrew Toth) 75f36d2 refactor: add CoinsCachePair alias (Andrew Toth) f08faea refactor: move flags to private uint8_t and rename to m_flags (Andrew Toth) 4e4fb4c refactor: disallow setting flags in CCoinsCacheEntry constructors (Andrew Toth) 8737c0c refactor: encapsulate flags setting with AddFlags and ClearFlags (Andrew Toth) 9715d3b refactor: encapsulate flags get access for all other checks (Andrew Toth) df34a94 refactor: encapsulate flags access for dirty and fresh checks (Andrew Toth) Pull request description: Since #17487 we no longer need to clear the coins cache when syncing to disk. A warm coins cache significantly speeds up block connection, and only needs to be fully flushed when nearing the `dbcache` limit. For frequent pruning flushes there's no need to empty the cache and kill connect block speed. However, simply using `Sync` in place of `Flush` actually slows down a pruned full IBD with a high `dbcache` value. This is because as the cache grows, sync takes longer since every coin in the cache is scanned to check if it's dirty. For frequent prune flushes and a large cache this constant scanning starts to really slow IBD down, and just emptying the cache on every prune becomes faster. To fix this, we can add two pointers to each cache entry and construct a doubly linked list of dirty entries. We can then only iterate through all dirty entries on each `Sync`, and simply clear the pointers after. With this approach a full IBD with `dbcache=16384` and `prune=550` was 32% faster than master. For default `dbcache=450` speedup was ~9%. All benchmarks were run with `stopatheight=800000`. | | prune | dbcache | time | max RSS | speedup | |-----------:|----------:|------------:|--------:|-------------:|--------------:| | master | 550 | 16384 | 8:52:57 | 2,417,464k | - | | branch | 550 | 16384 | 6:01:00 | 16,216,736k | 32% | | branch | 550 | 450 | 8:05:08 | 2,818,072k | 8.8% | | master | 10000 | 5000 | 8:19:59 | 2,962,752k | - | | branch | 10000 | 5000| 5:56:39 | 6,179,764k | 28.8% | | master | 0 | 16384 | 4:51:53 | 14,726,408k | - | | branch | 0 | 16384 | 4:43:11 | 16,526,348k | 2.7% | | master | 0 | 450 | 7:08:07 | 3,005,892k | - | | branch | 0 | 450 | 6:57:24 | 3,013,556k |2.6%| While the 2 pointers add memory to each cache entry, it did not slow down IBD. For non-pruned IBD results were similar for this branch and master. When I performed the initial IBD, the full UTXO set could be held in memory when using the max `dbcache` value. For non-pruned IBD with max `dbcache` to tip ended up using 12% more memory, but it was also 2.7% faster somehow. For smaller `dbcache` values the `dbcache` limit is respected so does not consume more memory, and the potentially more frequent flushes were not significant enough to cause any slowdown. For reviewers, the commits in order do the following: First 4 commits encapsulate all accesses to `flags` on cache entries, and then the 5th makes `flags` private. Commits `refactor: add CoinsCachePair alias` to `coins: call ClearFlags in CCoinsCacheEntry destructor` create the linked list head nodes and cache entry self references and pass them into `AddFlags`. Commit `coins: track flagged cache entries in linked list` actually adds the entries into a linked list when they are flagged DIRTY or FRESH and removes them from the linked list when they are destroyed or the flags are cleared manually. However, the linked list is not yet used anywhere. Commit `test: add cache entry linked list tests` adds unit tests for the linked list. Commit `coins: pass linked list of flagged entries to BatchWrite` uses the linked list to iterate through DIRTY entries instead of using the entire coins cache. Commit `validation: don't erase coins cache on prune flushes` uses `Sync` instead of `Flush` for pruning flushes, so the cache is no longer cleared. Inspired by [this comment](#15265 (comment)). Fixes #11315. ACKs for top commit: paplorinc: ACK 589db87 sipa: reACK 589db87 achow101: ACK 589db87 mzumsande: re-ACK 589db87 Tree-SHA512: 23b2bc01c83edacb5b39aa60bb0b766de9a74ce17f0c59bf13b97b4328a7b758ad9aff6581c3ca88e2973f7658380651530d497444f48d6e22ea0bfc51cc921d
See related discussions: * bitcoin#28280 (comment) * bitcoin#28280 (comment) * bitcoin#17487 (comment) And reproducer that demonstrates the behavior of all 3 cases: * https://gist.github.com/paplorinc/66f13938d867d82893d7ab7a2ebc5717
, bitcoin#24177, bitcoin#24299, bitcoin#24917, bitcoin#22564, bitcoin#25349, bitcoin#25571, bitcoin#17487, bitcoin#26999, bitcoin#27011 (blockstorage backports: part 2) b658637 merge bitcoin#27011: Add simulation-based `CCoinsViewCache` fuzzer (Kittywhiskers Van Gogh) 1d0e410 merge bitcoin#26999: A few follow-ups to bitcoin#17487 (Kittywhiskers Van Gogh) 7d837ea merge bitcoin#17487: allow write to disk without cache drop (Kittywhiskers Van Gogh) 2c758f4 merge bitcoin#25571: Make mapBlocksUnknownParent local, and rename it (Kittywhiskers Van Gogh) 70a91e1 merge bitcoin#25349: CBlockIndex/CDiskBlockIndex improvements for safety, consistent behavior (Kittywhiskers Van Gogh) eca0a64 merge bitcoin#22564: Move mutable globals cleared in ::UnloadBlockIndex to BlockManager (Kittywhiskers Van Gogh) 916b3f0 merge bitcoin#24917: Make BlockManager::LoadBlockIndex private (Kittywhiskers Van Gogh) e10ca27 merge bitcoin#24299: UnloadBlockIndex and ChainstateManager::Reset thread safety cleanups (Kittywhiskers Van Gogh) 18aa55b merge bitcoin#24177: add missing thread safety lock assertions (Kittywhiskers Van Gogh) 678e67c merge bitcoin#24235: use stronger EXCLUSIVE_LOCKS_REQUIRED() (Kittywhiskers Van Gogh) edc665c merge bitcoin#24103: Replace RecursiveMutex m_cs_chainstate with Mutex, and rename it (Kittywhiskers Van Gogh) d19ffd6 merge bitcoin#22278: Add LIFETIMEBOUND to CScript where needed (Kittywhiskers Van Gogh) Pull request description: ## Additional Information * Dependent on #6097 * Dependency for #6067 * Test failures in `linux64_multiprocess-build` ([build](https://gitlab.com/dashpay/dash/-/jobs/7550924662)) and `linux64_tsan-test` ([build](https://gitlab.com/dashpay/dash/-/jobs/7550924666)) do not stem from this PR but are pre-existing failures in `develop` ([build](https://gitlab.com/dashpay/dash/-/jobs/7550859495), [build](https://gitlab.com/dashpay/dash/-/jobs/7550859499)). A fix for the build failures has been opened as a separate PR. ## Breaking Changes None observed. ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas **(note: N/A)** - [x] I have added or updated relevant unit/integration/functional/e2e tests - [x] I have made corresponding changes to the documentation **(note: N/A)** - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ ACKs for top commit: UdjinM6: LGTM, utACK b658637 PastaPastaPasta: utACK b658637 Tree-SHA512: 1a9c3a41617af274db169db47a9c9fce7ba7ce0b2d68aa75617640a55da11a0fa095cb25ce6a2d38f06d3f6a6cc4c08cb4cf82dca4bdc74192e8882fd5f7052f
Summary: Adds comments, slight refactor clarifications to make the code easier to follow. This is a partial backport of [[bitcoin/bitcoin#17487 | core#17487]] bitcoin/bitcoin@6d8affc Depends on D16156 Test Plan: `ninja check` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D16161
Summary: In certain circumstances, we may want to flush to disk without emptying `cacheCoins`, which affects performance. UTXO snapshot activation is one such case. This method is currently unused except in unit tests, and this commit does not change any behavior. See also [[bitcoin/bitcoin#15265 | core#15265]], which makes the case that under normal operation a flush-without-erase doesn't necessarily add much benefit. I open this PR even in light of the previous discussion because (i) flush-without-erase almost certainly provides benefit in the case of snapshot activation (especially on spinning disk hardware) and (ii) this diff is fairly small and gives us convenient options for more granular cache management without changing existing policy. Incorporates feedback from John Newbery. Co-authored-by: Suhas Daftuar <[email protected]> Thanks to Marco Falke for help with move semantics. This concludes backport of [[bitcoin/bitcoin#17487 | core#17487]] and [[bitcoin/bitcoin#26999 | core#26999]] bitcoin/bitcoin@79cedc3 - coins: add Sync() method to allow flush without cacheCoins drop bitcoin/bitcoin@2c3cbd6 - test: add use of Sync() to coins tests bitcoin/bitcoin@1d7935b - test: add test for coins view flush behavior using Sync() bitcoin/bitcoin@bb00357 - Make test/fuzz/coins_view exercise CCoinsViewCache::Sync() bitcoin/bitcoin@941feb6 - Avoid unclear {it = ++it;} bitcoin/bitcoin@2e16054 - Add assertions that BatchWrite(erase=true) erases Depends on D16161 Test Plan: `ninja all check-all` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D16162
This is part of the assumeutxo project:
Parent PR: #15606
Issue: #15605
Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal
In certain circumstances, we may want to flush chainstate data to disk without
emptying
cacheCoins, which affects performance. UTXO snapshotactivation is one such case, as we populate
cacheCoinswith the snapshotcontents and want to persist immediately afterwards but also enter IBD.
See also #15265, which makes the case that under normal operation a
flush-without-erase doesn't necessarily add much benefit. I open this PR
even in light of the previous discussion because (i) flush-without-erase
almost certainly provides benefit in the case of snapshot activation (especially
on spinning disk hardware) and (ii) this diff is fairly small and gives us convenient
options for more granular cache management without changing existing policy.
See also #15218.