Repository navigation
Conversation
Without this, -prune and -fastprune in extra_args have no effect.
Move the blk file aside instead of deleting it, so it can be restored.
The pruned blk/rev files are not deleted, even after a later flush succeeds.
Files selected for pruning were kept in a local, so a failed flush left them on disk until the next startup. Keep them in BlockManager and delete them after the next block index write. Take them with the dirty entries, which WriteBlockIndexDB() drops even if the write fails. Drop a file from the set once a block is written to it. Pending files wait for the next periodic, forced or shutdown flush rather than forcing one, which would retry a failing write on every block.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy 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. |
|
|
||
| // Take the pruned files with the dirty entries, which WriteBlockIndexDB() drops | ||
| // even if it fails, so files are only deleted after the write that marks them pruned. | ||
| const auto files_to_unlink{std::exchange(m_blockman.m_files_to_unlink, {})}; |
There was a problem hiding this comment.
If a flush fails after PruneOneBlockFile() clears nSize, retrying pruneblockchain won't clean up the files. FindFilesToPruneManual() skips files with nSize == 0, so fFlushForPrune stays false. With FlushStateMode::NONE, should_write is false and the function returns early without ever unlinking them.
In chainstate_write_tests.cpp:211, the retry passes only because it calls FORCE_FLUSH instead of FlushStateMode::NONE, prune_height.
There was a problem hiding this comment.
Sorry for the delay in response. You're right that the retry doesn't delete them. File 0 already has nSize == 0, so FindFilesToPruneManual() selects nothing and a NONE flush doesn't write. The files are still in m_files_to_unlink and the next flush that writes deletes them.
That's deliberate on my part and I believe it matches item 6 of ryanofsky's post-merge review where skipped writes are kept in memory until a later flush writes them. A NONE retry doesn't write the dirty block index or coins either. In the node a failed flush requests shutdown, and the shutdown flush calls ForceFlushStateToDisk(), which uses FORCE_FLUSH, which is the retry that actually runs, so it's what the test uses. Item 9 suggests documenting that kernel users should stop the same way.
Edit: item 4 of the same review fits better (arejula27 is working on it in #35731). A false from FlushStateToDisk() only means the disk write failed, and in-memory state is still valid. pruneblockchain already works that way. It logs the failure and returns the prune height from memory. The files are pruned in memory, and the disk catches up on the next write.
| } | ||
| } | ||
| // Don't delete a pruned file that is about to hold a new block. | ||
| m_files_to_unlink.erase(nFile); |
There was a problem hiding this comment.
m_files_to_unlink.erase(nFile);
Is it actually possible for nFile to be in m_files_to_unlink here? m_last_blockfile contains the tip, and rolling over advances to a new file number.
Also, if an old file were somehow reused here, opening it with rb+ would overwrite the head without truncating the existing block data on disk.
There was a problem hiding this comment.
Yes, after an assumeutxo snapshot validates. There's no single m_last_blockfile anymore. m_blockfile_cursors has a NORMAL and an ASSUMED cursor, and until restart the NORMAL one still points at the last file the background chainstate wrote to. Once the snapshot is validated, GetPruneRange() starts at 0 and that file isn't MaxBlockfileNum() so it can be pruned. A block below the snapshot height (e.g. from getblockfrompeer) then goes into it at offset 0, because PruneOneBlockFile() reset its CBlockFileInfo. If the flush that pruned the file failed, it would still be pending, and without this line the next write would delete it with that block in it. I can put this in the comment above the line.
With regard to rb+ master reuses the file the same way after a failed prune flush, since it never deletes the file. The old data after the new block isn't read, because reads use the stored positions. Overwriting the head is a problem if the node crashes before the next write, because the index on disk still lists the old blocks there. But I think that can be a fix in a separate / followup PR.
|
Concept ACK |
| BOOST_CHECK_EQUAL(blockman.GetBlockFileInfo(0)->nSize, 0U); | ||
| BOOST_CHECK_EQUAL(blockman.GetBlockFileInfo(0)->nUndoSize, 0U); | ||
| for (int height{0}; height <= prune_height; ++height) { | ||
| BOOST_CHECK(!(chainstate.m_chain[height]->nStatus & BLOCK_HAVE_DATA)); |
There was a problem hiding this comment.
test: characterize a failed prune flush (108d7ff)
We could use the helper function here.
| BOOST_CHECK(!(chainstate.m_chain[height]->nStatus & BLOCK_HAVE_DATA)); | |
| BOOST_CHECK(blockman.IsBlockPruned(*chainstate.m_chain[height])); |
|
tACK d224fb8 Left a minor, non-blocking suggestion |

If a flush that prunes block files fails before the block index write, the files are already marked pruned in memory, but
setFilesToPruneis a local inFlushStateToDisk(). The next successful flush writes the pruned file info and never deletes the blk/rev files. OnlyScanAndUnlinkAlreadyPrunedFiles()deletes them, at the next startup.This is number 7 of ryanofsky's post-merge review of #35714. The block file flush failure from #35714 and the blocks directory
CheckDiskSpace()failure both return before that write.I worked on this because it lets me test my kernel pruning refactor (#36738, for #35627 and kernel-node/kernel-node#111) against the issue he describes. The refactor moves the flush, block index write and file deletion that a pruning flush runs from
FlushStateToDisk()intoWriteBlockStorage(), and no unit test ran that sequence in prune mode. I rebased the refactor onto this branch, and the new tests pass on each of its commits.The first three commits add tests that pass on master. The unit test fixtures apply
-pruneand-fastprunefromextra_args,chainstate_flush_failure_boundarychecks that a flush after a failed one writes what was skipped, andprune_flush_failureshows that a failed prune flush still leaves its files on disk after a retry.The last commit keeps the selected files in
BlockManagerand deletes them after the next block index write. If that write fails they are dropped with the dirty entries, which means a file is only deleted once it is marked pruned on disk. Pending files don't force a write. The next flush that writes deletes them.The disk-space failure and automatic pruning take the same path but have no test. Neither does the
FindNextBlockPos()line that drops a file from the pending set when a block is written to it, which can happen after an assumeutxo snapshot validates.