Skip to content

prune, import: allow pruning to work during loadblock import - #24957

Merged
achow101 merged 1 commit into
bitcoin:masterfrom
mruddy:issue_23852_import_prune
May 3, 2023
Merged

achow101 merged 1 commit into
bitcoin:masterfrom
mruddy:issue_23852_import_prune

Conversation

@mruddy

@mruddy mruddy commented Apr 24, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #23852

This allows pruning to work during the -loadblock import process.

An example use case is where you have a clean set of block files and you want to create a pruned node from them, but you don't want to alter the input set of block files.

#23852 noted that pruning was not working reliably during the loadblock import process. The reason why the loadblock process was not pruning regularly as it progressed is that the pruning process (BlockManager::FindFilesToPrune) checks the tip height of the active chainstate, and CChainState::ActivateBestChain was not called (which updates that tip height) in ThreadImport until after all the import files were processed.

An example bash command line that makes it easy to import a bunch of block files:

./src/qt/bitcoin-qt -debug -logthreadnames -datadir=/tmp/btc -prune=550 -loadblock=/readonly/btc/main/blk{00000..00043}.dat

One interesting side note is that CChainState::ActivateBestChain can be called while the import process is running (in the loadblk thread) by concurrent network message processing activity in the msghand thread. For example, one way to reproduce this easily is with the getblockfrompeer RPC (requesting a block with height greater than 100000) run from a node connected to an importing node. There are other ways too, but this is an easy way. I only mention this to explain how the max_prune_height=225719 log message in the original issue came to occur.

@mruddy mruddy changed the title prune, import: fixes #23852 prune, import: allow pruning to work during loadblock import Apr 24, 2022

@vincenzopalazzo vincenzopalazzo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Concept ACK

@luke-jr luke-jr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code safety utACK. Did not verify if it fixes a bug or impacts performance.

luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request May 21, 2022
allows pruning to work during the loadblock import process.

Github-Pull: bitcoin#24957
Rebased-From: da8e95c01401edd5e33c04ad412a6df2997f58c9
@mruddy mruddy closed this Jun 15, 2022
@jamesob

jamesob commented Jun 17, 2022

Copy link
Copy Markdown
Contributor

Concept ACK. I think this should be reopened, as it's a small diff and a good change. I can review more thoroughly next week.

@laanwj

laanwj commented Jun 17, 2022 •

Copy link
Copy Markdown
Member

Yeah, concept ACK
(adding up for grabs label just in case)

@mruddy mruddy reopened this Jun 18, 2022
@mruddy

mruddy commented Jun 18, 2022

Copy link
Copy Markdown
Contributor Author

I closed this to garbage collect because I thought it was dead. But, since there's interest I've reopened it. Thanks!

@mruddy mruddy closed this Sep 9, 2022
@mruddy
mruddy deleted the issue_23852_import_prune branch September 9, 2022 19:23
@mruddy
mruddy restored the issue_23852_import_prune branch October 21, 2022 13:01
@mruddy

mruddy commented Oct 27, 2022 •

Copy link
Copy Markdown
Contributor Author

Reopening since I have time to usher this to completion now.
Same patch, just rebased onto current master.

@mruddy mruddy reopened this Oct 27, 2022
@mruddy
mruddy force-pushed the issue_23852_import_prune branch 5 times, most recently from 347664e to 2f62704 Compare October 30, 2022 12:48
@aureleoules

Copy link
Copy Markdown
Contributor

I'm not sure how to test this?
I let a fresh bitcoind node sync (enough to generate 20 blk.dat files) and tried importing on another fresh bitcoind node these blk files using ./src/bitcoind -debug -logthreadnames -datadir=/tmp/btc -prune=550 -loadblock=/tmp/btc1/blocks/blk{00000..00020}.dat

where /tmp/btc1 contains the 20 blk.dat files

but I get these logs for every block (both on master and this PR):

2022-10-31T16:30:15Z [loadblk] [reindex] LoadExternalBlockFile: Out of order block 000000000000061a9e7540272dd4cb607350b5f704d89bf87d4757078e0d2aa5, parent 0000000000000627a077a5b46189adeb9b345d3f5b9a4c8cfd847f5ba67af26c not known
...
2022-10-31T16:30:16Z [loadblk] Loaded 0 blocks from external file in 1135ms

Comment thread src/validation.cpp Resolved
@mruddy

mruddy commented Oct 31, 2022

Copy link
Copy Markdown
Contributor Author

@aureleoules thanks for looking into this. your block files have blocks out of order. it's nothing that you did wrong. it's just that with the -loadblock option, there is no unknown-parent tracking like there is with -reindex. i forget the historical reason for why that is at the moment. but, that's how it is. for reference:
https://github.com/bitcoin/bitcoin/blob/master/src/node/blockstorage.cpp#L885
https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp#L4431-L4439

There is a tool to make some block files for testing where the blocks are in order and loadable with -loadblock at https://github.com/bitcoin/bitcoin/tree/master/contrib/linearize

If I remember correctly, another snag you'll come across is that you need to have at least 100,000 blocks for mainnet due to nPruneAfterHeight see https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp#L109

I'll make a todo item for myself to see how I can make testing this easier via a follow-up comment, and maybe a set of test input block files or something.

@mruddy
mruddy force-pushed the issue_23852_import_prune branch from 2f62704 to 488682e Compare November 1, 2022 01:50
mruddy added a commit to mruddy/test_blk_files that referenced this pull request Nov 1, 2022
@mruddy

mruddy commented Nov 1, 2022

Copy link
Copy Markdown
Contributor Author

@aureleoules I put a set of linearized block files that are useful for testing this at:

https://github.com/mruddy/test_blk_files

A way to use the files and be able to watch what is going on in the debugger is with:

mkdir -p /tmp/btc && gdb --tui --args ./src/qt/bitcoin-qt -debug -logthreadnames -datadir=/tmp/btc -prune=550 -loadblock=/readonly/btc/main/blk{00000..00014}.dat -connect=0

The debug log will log related messages and can be found easily with grep like the following examples:

grep 'removed 1 blk/rev pairs' /tmp/btc/debug.log
2022-11-01T14:50:40Z [loadblk] [prune] target=550MiB actual=388MiB diff=161MiB max_prune_height=140633 removed 1 blk/rev pairs

grep 'unlink pruned files' /tmp/btc/debug.log
2022-11-01T14:50:46Z [loadblk] [bench] FlushStateToDisk: unlink pruned files started
2022-11-01T14:50:46Z [loadblk] [bench] FlushStateToDisk: unlink pruned files completed (28.22ms)

Depending on what you're looking to understand, various breakpoints can be set within gdb, for example:

b ThreadImport
b LoadExternalBlockFile
b FlushStateToDisk
b FindFilesToPrune
b PruneOneBlockFile

@aureleoules aureleoules left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 488682e785194f807f21b4dc6826df7714d78a25

Thanks @mruddy for the linearized block files!
I was able to verify with the given block files that pruning occurs during the import process in this PR and not at the end (master).
If I understand correctly, this would allow cloning a full node into a pruned node without needing the double amount of disk space during the import process and without needing a workaround (-connect=localhost).

@luke-jr

luke-jr commented Nov 3, 2022

Copy link
Copy Markdown
Contributor

In the future, please don't rebase for no reason

@luke-jr

luke-jr commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

Should we be doing this check later on when processing out of order successor blocks?

@luke-jr

luke-jr commented Nov 16, 2022

Copy link
Copy Markdown
Contributor

P.S. Needs rebase. Silent conflict with #16981

diff --git a/src/validation.cpp b/src/validation.cpp
index 40d15b60d63..285aeb16d4e 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4366,6 +4366,7 @@ void Chainstate::LoadExternalBlockFile(
                 // next block, but it's still possible to rewind to the start of the current block (without a disk read).
                 nRewind = nBlockPos + nSize;
                 blkdat.SkipTo(nRewind);
+                std::shared_ptr<CBlock> recent_block_maybe_not_this_block;
                 {
                     LOCK(cs_main);
                     // detect out of order blocks, and store them for later
@@ -4383,7 +4384,7 @@ void Chainstate::LoadExternalBlockFile(
                     if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
                         // This block can be processed immediately; rewind to its start, read and deserialize it.
                         blkdat.SetPos(nBlockPos);
-                        std::shared_ptr<CBlock> pblock{std::make_shared<CBlock>()};
+                        auto& pblock = recent_block_maybe_not_this_block = std::make_shared<CBlock>();
                         blkdat >> *pblock;
                         nRewind = blkdat.GetPos();
 
@@ -4416,7 +4417,7 @@ void Chainstate::LoadExternalBlockFile(
                     // called by concurrent network message processing. but, that is not
                     // reliable for the purpose of pruning while importing.
                     BlockValidationState state;
-                    if (!ActivateBestChain(state, pblock)) {
+                    if (!ActivateBestChain(state, recent_block_maybe_not_this_block)) {
                         LogPrint(BCLog::REINDEX, "failed to activate chain (%s)\n", state.ToString());
                         break;
                     }

@mruddy
mruddy force-pushed the issue_23852_import_prune branch from 488682e to f5c1649 Compare November 18, 2022 08:42
@DrahtBot

DrahtBot commented Nov 18, 2022 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK achow101
Concept ACK vincenzopalazzo, jamesob, laanwj
Stale ACK luke-jr, aureleoules

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@mruddy

mruddy commented Nov 18, 2022 •

Copy link
Copy Markdown
Contributor Author

@luke-jr Thanks! I just rebased and had to make a small change due to the #16981 updates. Those updates moved the block data read from disk into the code block that is guarded by cs_main. I couldn't move my updates for this PR into that code block because ActivateBestChain asserts that cs_main is not held. So, I brought pblock one level back up out of that cs_main guarded scope (leaving the read where #16981 moved it) so that I could access it without the lock being held.

Should we be doing this check later on when processing out of order successor blocks?

No, because -loadblock doesn't support the out of order processing, and I don't want to make -prune work with -reindex for the reasons that I listed at #23852 (comment)

@mruddy
mruddy force-pushed the issue_23852_import_prune branch from f5c1649 to 734355b Compare November 18, 2022 08:56

@luke-jr luke-jr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

utACK 734355b470764c523ef08b25320a5fabb73358a6

Comment thread src/validation.cpp Resolved
@mruddy

mruddy commented Dec 7, 2022

Copy link
Copy Markdown
Contributor Author

Thanks to those that have reviewed/tested this already, I think it's ready to go.

@jamesob Is there a chance that you might be able to review or test this PR? Thanks!

@aureleoules aureleoules left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

re-tACK 734355b470764c523ef08b25320a5fabb73358a6

@achow101

Copy link
Copy Markdown
Member

ACK 734355b470764c523ef08b25320a5fabb73358a6

@achow101

Copy link
Copy Markdown
Member

Looks like there's a silent merge conflict:

../../../src/validation.cpp: In member function ‘void Chainstate::LoadExternalBlockFile(FILE*, FlatFilePos*, std::multimap<uint256, FlatFilePos>*)’:
../../../src/validation.cpp:4518:21: error: ‘fPruneMode’ was not declared in this scope; did you mean ‘node::fPruneMode’?
 4518 |                 if (fPruneMode && !fReindex && pblock) {
      |                     ^~~~~~~~~~
      |                     node::fPruneMode
In file included from ../../../src/validation.h:22,
                 from ../../../src/validation.cpp:6:
../../../src/node/blockstorage.h:51:13: note: ‘node::fPruneMode’ declared here
   51 | extern bool fPruneMode;
      |             ^~~~~~~~~~
make[2]: *** [Makefile:10619: libbitcoin_node_a-validation.o] Error 1

allows pruning to work during the loadblock import process.
@mruddy
mruddy force-pushed the issue_23852_import_prune branch from 734355b to c4981e7 Compare February 22, 2023 10:28
@mruddy

mruddy commented Feb 22, 2023

Copy link
Copy Markdown
Contributor Author

@achow101 Thanks! I rebased and then resolved the silent merge conflict by changing fPruneMode to m_blockman.IsPruneMode().

@achow101

Copy link
Copy Markdown
Member

re-ACK c4981e7

@fanquake
fanquake requested a review from jamesob February 22, 2023 18:13
@achow101
achow101 merged commit aebcd18 into bitcoin:master May 3, 2023
@mruddy
mruddy deleted the issue_23852_import_prune branch May 5, 2023 12:08
luke-jr pushed a commit to luke-jr/bitcoin that referenced this pull request Jun 27, 2023
allows pruning to work during the loadblock import process.

Github-Pull: bitcoin#24957
Rebased-From: 488682e785194f807f21b4dc6826df7714d78a25
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 19, 2023
…cess

Summary:
allows pruning to work during the loadblock import process.

This is a backport of [[bitcoin/bitcoin#24957 | core#24957]]

Depends on D15003

Test Plan:
Backup the eCash data dir, create a bootstrap.dat file by following the instructions in contrib/linearize, then delete the content of the data dir, comment out config options that are incompatible with pruning (txindex=1) then
`src/bitcoind -prune=550 -loadblock=/data/bootstrap.dat`

Check while the node is importing the blocks from bootstrap.dat that the new data dir does not grow significantly larger than 550 MB (I saw it oscillates between that target and 800 MB, while the bootstrap.dat file was 21 GB of blocks 0--313000)

`ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15004
@bitcoin bitcoin locked and limited conversation to collaborators May 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-prune doesn't work during import

9 participants