Skip to content

Commit 1e8d689

Browse files
committed
Merge bitcoin/bitcoin#30517: refactor: Add FlatFileSeq member variables in BlockManager
7aa8994 refactor: Add FlatFileSeq member variables in BlockManager (TheCharlatan) Pull request description: Instead of constructing a new class every time a file operation is done, construct them once for each of the undo and block file when a new BlockManager is created. In future, this might make it easier to introduce an abstract block store. Historically, this was not easily possible prior to #27125. ACKs for top commit: danielabrozzoni: ACK 7aa8994 tdb3: ACK 7aa8994 stickies-v: ACK 7aa8994 brunoerg: utACK 7aa8994 Tree-SHA512: 7c181968c270956c90fa0f3687562239912a973b6a35ddbf49fc58733247ea9d986303cbf6f8fc16e8c2d9bf4505e866aed37f030a8c9be72e95bf3752902aa6
2 parents 02c76ad + 7aa8994 commit 1e8d689

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

src/flatfile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const
3030
return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));
3131
}
3232

33-
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only)
33+
FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const
3434
{
3535
if (pos.IsNull()) {
3636
return nullptr;
@@ -52,7 +52,7 @@ FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only)
5252
return file;
5353
}
5454

55-
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space)
55+
size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const
5656
{
5757
out_of_space = false;
5858

@@ -78,7 +78,7 @@ size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_
7878
return 0;
7979
}
8080

81-
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize)
81+
bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const
8282
{
8383
FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos
8484
if (!file) {

src/flatfile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class FlatFileSeq
6363
fs::path FileName(const FlatFilePos& pos) const;
6464

6565
/** Open a handle to the file at the given position. */
66-
FILE* Open(const FlatFilePos& pos, bool read_only = false);
66+
FILE* Open(const FlatFilePos& pos, bool read_only = false) const;
6767

6868
/**
6969
* Allocate additional space in a file after the given starting position. The amount allocated
@@ -74,7 +74,7 @@ class FlatFileSeq
7474
* @param[out] out_of_space Whether the allocation failed due to insufficient disk space.
7575
* @return The number of bytes successfully allocated.
7676
*/
77-
size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space);
77+
size_t Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const;
7878

7979
/**
8080
* Commit a file to disk, and optionally truncate off extra pre-allocated bytes if final.
@@ -83,7 +83,7 @@ class FlatFileSeq
8383
* @param[in] finalize True if no more data will be written to this file.
8484
* @return true on success, false on failure.
8585
*/
86-
bool Flush(const FlatFilePos& pos, bool finalize = false);
86+
bool Flush(const FlatFilePos& pos, bool finalize = false) const;
8787
};
8888

8989
#endif // BITCOIN_FLATFILE_H

src/node/blockstorage.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
734734
bool BlockManager::FlushUndoFile(int block_file, bool finalize)
735735
{
736736
FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
737-
if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
737+
if (!m_undo_file_seq.Flush(undo_pos_old, finalize)) {
738738
m_opts.notifications.flushError(_("Flushing undo file to disk failed. This is likely the result of an I/O error."));
739739
return false;
740740
}
@@ -756,7 +756,7 @@ bool BlockManager::FlushBlockFile(int blockfile_num, bool fFinalize, bool finali
756756
assert(static_cast<int>(m_blockfile_info.size()) > blockfile_num);
757757

758758
FlatFilePos block_pos_old(blockfile_num, m_blockfile_info[blockfile_num].nSize);
759-
if (!BlockFileSeq().Flush(block_pos_old, fFinalize)) {
759+
if (!m_block_file_seq.Flush(block_pos_old, fFinalize)) {
760760
m_opts.notifications.flushError(_("Flushing block file to disk failed. This is likely the result of an I/O error."));
761761
success = false;
762762
}
@@ -808,38 +808,28 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
808808
std::error_code ec;
809809
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
810810
FlatFilePos pos(*it, 0);
811-
const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
812-
const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
811+
const bool removed_blockfile{fs::remove(m_block_file_seq.FileName(pos), ec)};
812+
const bool removed_undofile{fs::remove(m_undo_file_seq.FileName(pos), ec)};
813813
if (removed_blockfile || removed_undofile) {
814814
LogPrint(BCLog::BLOCKSTORAGE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
815815
}
816816
}
817817
}
818818

819-
FlatFileSeq BlockManager::BlockFileSeq() const
820-
{
821-
return FlatFileSeq(m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
822-
}
823-
824-
FlatFileSeq BlockManager::UndoFileSeq() const
825-
{
826-
return FlatFileSeq(m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE);
827-
}
828-
829819
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
830820
{
831-
return AutoFile{BlockFileSeq().Open(pos, fReadOnly)};
821+
return AutoFile{m_block_file_seq.Open(pos, fReadOnly)};
832822
}
833823

834824
/** Open an undo file (rev?????.dat) */
835825
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
836826
{
837-
return AutoFile{UndoFileSeq().Open(pos, fReadOnly)};
827+
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly)};
838828
}
839829

840830
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
841831
{
842-
return BlockFileSeq().FileName(pos);
832+
return m_block_file_seq.FileName(pos);
843833
}
844834

845835
FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int nHeight, uint64_t nTime)
@@ -919,7 +909,7 @@ FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int n
919909
m_blockfile_info[nFile].nSize += nAddSize;
920910

921911
bool out_of_space;
922-
size_t bytes_allocated = BlockFileSeq().Allocate(pos, nAddSize, out_of_space);
912+
size_t bytes_allocated = m_block_file_seq.Allocate(pos, nAddSize, out_of_space);
923913
if (out_of_space) {
924914
m_opts.notifications.fatalError(_("Disk space is too low!"));
925915
return {};
@@ -965,7 +955,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
965955
m_dirty_fileinfo.insert(nFile);
966956

967957
bool out_of_space;
968-
size_t bytes_allocated = UndoFileSeq().Allocate(pos, nAddSize, out_of_space);
958+
size_t bytes_allocated = m_undo_file_seq.Allocate(pos, nAddSize, out_of_space);
969959
if (out_of_space) {
970960
return FatalError(m_opts.notifications, state, _("Disk space is too low!"));
971961
}

src/node/blockstorage.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ class BlockManager
166166
[[nodiscard]] bool FlushChainstateBlockFile(int tip_height);
167167
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
168168

169-
FlatFileSeq BlockFileSeq() const;
170-
FlatFileSeq UndoFileSeq() const;
171-
172169
AutoFile OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false) const;
173170

174171
/**
@@ -261,12 +258,17 @@ class BlockManager
261258

262259
const kernel::BlockManagerOpts m_opts;
263260

261+
const FlatFileSeq m_block_file_seq;
262+
const FlatFileSeq m_undo_file_seq;
263+
264264
public:
265265
using Options = kernel::BlockManagerOpts;
266266

267267
explicit BlockManager(const util::SignalInterrupt& interrupt, Options opts)
268268
: m_prune_mode{opts.prune_target > 0},
269269
m_opts{std::move(opts)},
270+
m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
271+
m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},
270272
m_interrupt{interrupt} {}
271273

272274
const util::SignalInterrupt& m_interrupt;

0 commit comments

Comments
 (0)