mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-07-12 23:20:23 +02:00
dc1c17c085doc: add release notes (Andrew Toth)0e10937184fuzz: add coins_view_stacked fuzz harness to test concurrent leveldb reads (Andrew Toth)ce610a6ff4fuzz: update harnesses to cover CoinsViewOverlay::StartFetching (Andrew Toth)760fb22dc3test: add unit tests for CoinsViewOverlay::StartFetching (Andrew Toth)d69a3b20dedoc: update CoinsViewOverlay docstring to describe parallel fetching (Andrew Toth)ab2a379237coins: fetch inputs in parallel (Andrew Toth)fdf283036acoins: add ready flag to InputToFetch (Andrew Toth)ede11b8314validation: collect block inputs in CoinsViewOverlay before ConnectBlock (Andrew Toth)f82043af50coins: introduce thread pool in CoinsViewOverlay (Andrew Toth)5bf1c32008validation: add -prevoutfetchthreads configuration option (Andrew Toth) Pull request description: This PR is a continuation of https://github.com/bitcoin/bitcoin/pull/31132. All outstanding issues raised there have been resolved, but the volume of stale comments can make that change difficult to review. Currently, when connecting a block, each input prevout is looked up one at a time. For every input we first check the in-memory coins cache, and on a miss we make a synchronous round-trip to the chainstate LevelDB to read the coin from disk. Because these lookups happen serially as the block is being validated, the disk read latency stacks up and dominates the time spent in `ConnectBlock` whenever many inputs are not already in the cache. This PR moves those disk reads onto a pool of worker threads that run in parallel with block connection. Before entering `ConnectBlock` the block is handed to a `CoinsViewOverlay`, which kicks off the workers to begin fetching all of the block's prevouts from disk and warming the cache. The main validation thread continues to do exactly the same work it does today, hitting the cache for each input in order. The only difference is that by the time it asks, the coin is much more likely to already be there. There are no validation logic or consensus behavior changes. This is purely a parallelization of an existing read pattern. The number of fetcher threads is configurable via `-prevoutfetchthreads=<n>`, defaulting to 8 and capped at 16. Setting it to 0 disables input fetching entirely and reverts to the previous serial behavior. We have measured large performance gains for IBD and `-reindex-chainstate`, as well as worst-case steady-state block connection at the tip. l0rinc ran many thorough benchmarking passes on the original PR across multiple machines, storage types, dbcache sizes[^1], operating systems[^2], and fetcher thread counts[^3]. Many other contributors also posted their benchmark results in the original PR. IBD speedups range from 1.18× to over 3× faster[^4]. Worst-case block connection time for network-attached storage was over 2× faster[^5]. Flamegraph comparisons before and after this change are available[^6]. On safety: `ConnectBlock` runs while holding `cs_main`, so nothing else in the node can mutate the chainstate while the fetchers are reading it. On LevelDB: [concurrent reads are fully supported](https://github.com/bitcoin/bitcoin/blob/master/src/leveldb/include/leveldb/db.h#L44) and [documented as such](https://github.com/bitcoin/bitcoin/blob/master/src/leveldb/doc/index.md#concurrency). We already rely on this in production today against our other LevelDB-backed databases. The `txindex` DB is read by multiple simultaneous HTTP RPC worker threads via the `getrawtransaction` RPC. The `blockfilterindex` DB is called concurrently from both the P2P `cfilters` / `cfheaders` / `cfcheckpt` message handlers on the `msghand` thread, and from the `getblockfilter` RPC on the HTTP RPC worker threads. We have not yet been issuing concurrent reads against the chainstate DB, but there is no LevelDB-side reason we can't. In fact, the chainstate DB is already being touched by more than one thread on master, because LevelDB schedules its own background compaction work. For reviewers: The main change is `CoinsViewOverlay` gets 1 new public and 2 new private methods. - `StartFetching`: public method called in lieu of `CreateResetGuard` before we enter `ConnectBlock`. It still returns a `ResetGuard` so the view is `Reset` before the block it is working on leaves scope. This kicks off worker threads who each just run `while (ProcessInput()) {}` and then return. - `StopFetching`: private method called on `Reset` whenever the guard leaves scope or `Flush`. Stops all threads and clears multi threaded state. - `ProcessInput`: private method that fetches a single input prevout. Returns `true` if an input was fetched and `false` otherwise. This is the only method on `CoinsViewOverlay` that is called concurrently by multiple threads. Every other method on the overlay is still called synchronously on the main thread. The `CoinsViewOverlay::FetchCoinFromBase` method is also extended to lookup the coins fetched from `ProcessInput` first before falling back to `base->PeekCoin`. Mutating methods `Reset` and `Flush` are overridden in `CoinsViewOverlay` to call `StopFetching` first. [^1]: https://github.com/bitcoin/bitcoin/pull/31132#pullrequestreview-3515011880 [^2]: https://github.com/bitcoin/bitcoin/pull/31132#issuecomment-3767758819 [^3]: https://github.com/bitcoin/bitcoin/pull/31132#issuecomment-3617721711 [^4]: https://github.com/bitcoin/bitcoin/pull/31132#issuecomment-3678847806 [^5]: https://github.com/bitcoin/bitcoin/pull/31132#issuecomment-4071032270 [^6]: https://github.com/bitcoin/bitcoin/pull/31132#issuecomment-3617315125 ACKs for top commit: l0rinc: reACKdc1c17c085willcl-ark: ACKdc1c17c085theStack: re-ACKdc1c17c085ryanofsky: Code review ACKdc1c17c085with changes to StopFetching and AllInputsConsumed checking behavior since last review. Tree-SHA512: 89c1c2890f65aac5cd546edc44504956c47b6fada256d3b86ced47e6dd8c72f633a4357753b3b9805b9ba6ed02790822090d70578aba2964baf50d7eb956864c