From f1bb3c98420b5359aa5563c51d269d7851485773 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 12 Oct 2026 07:14:27 +0200 Subject: [PATCH 1/3] csdb: compact pruned storage in bounded steps, never terminate on it On mainnet the first pruning cycle of a node syncing from block 0 (blocks before 99'000 removed at the 200'000 checkpoint) terminated the node: compacting the 600 MB blockchain.db in one Db::compact call needed more of the environment's buffer cache than it holds, and BerkeleyDB threw (BDB3017 unable to allocate space from the buffer cache; terminate called after throwing an instance of 'DbException', what(): Db::compact: Cannot allocate memory). DatabaseBerkeleyDB::compact now works through each database 64 pages per call, resuming from the key the previous call stopped at, and turns any exception into an error result: compaction only returns space to the file system (BerkeleyDB reuses freed pages anyway), so pruneStep logs the failure and the node carries on. Co-Authored-By: Claude Opus 5.5 --- csdb/src/database_berkeleydb.cpp | 37 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/csdb/src/database_berkeleydb.cpp b/csdb/src/database_berkeleydb.cpp index a149843b9..c265e7e4a 100644 --- a/csdb/src/database_berkeleydb.cpp +++ b/csdb/src/database_berkeleydb.cpp @@ -362,14 +362,39 @@ bool DatabaseBerkeleyDB::compact() { return false; } - // DB_FREE_SPACE gives emptied pages back to the file system - for (Db* db : { db_blocks_.get(), db_seq_no_.get() }) { - int status = db->compact(nullptr, nullptr, nullptr, nullptr, DB_FREE_SPACE, nullptr); - if (status != 0) { - set_last_error_from_berkeleydb(status); - return false; + // DB_FREE_SPACE gives emptied pages back to the file system. Compacting a large file in one call + // pins more pages than the environment's buffer cache holds (BDB3017, "Db::compact: Cannot + // allocate memory"), so it goes in bounded steps, each resuming where the previous one stopped. + // Compaction only returns space (BerkeleyDB reuses freed pages anyway): a failure is reported + // and never lets an exception terminate the node. + constexpr u_int32_t kPagesPerStep = 64; + constexpr int kMaxSteps = 1'000'000; + try { + for (Db* db : { db_blocks_.get(), db_seq_no_.get() }) { + cs::Bytes resume; + for (int step = 0; step < kMaxSteps; ++step) { + DB_COMPACT progress{}; + progress.compact_pages = kPagesPerStep; + Dbt from(resume.data(), static_cast(resume.size())); + Dbt_safe stopped; + int status = db->compact(nullptr, resume.empty() ? nullptr : &from, nullptr, &progress, DB_FREE_SPACE, &stopped); + if (status != 0) { + set_last_error_from_berkeleydb(status); + return false; + } + // pages left over in the budget: the pass reached the end of the database + if (progress.compact_pages > 0 || stopped.get_size() == 0) { + break; + } + auto begin = static_cast(stopped.get_data()); + resume.assign(begin, begin + stopped.get_size()); + } } } + catch (const std::exception& e) { + set_last_error(UnknownError, "compact: %s", e.what()); + return false; + } set_last_error(); return true; From b96c5f8a4fec26e7021207bdeed75ba9d5959334 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 12 Oct 2026 07:20:38 +0200 Subject: [PATCH 2/3] csdb: a failed compaction is a warning, not a storage error BerkeleyDB's default buffer cache (about 64 pages of 4 KB) is usually full of freshly written pages while a node syncs, and Db::compact then cannot allocate pages even in small steps (seen on mainnet at the 300'000 checkpoint). Nothing is wrong with the storage: compaction only returns space, and the pages freed by pruning are reused for new blocks (blockchain.db grew from 207 to 232 MB while 100'000 blocks were added and 100'000 removed). Log it as a warning instead of setting the storage error. Co-Authored-By: Claude Opus 5.5 --- csdb/src/storage.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/csdb/src/storage.cpp b/csdb/src/storage.cpp index c86fa80de..28ba08580 100644 --- a/csdb/src/storage.cpp +++ b/csdb/src/storage.cpp @@ -1109,7 +1109,8 @@ bool Storage::compact() { return false; } if (!d->db->compact()) { - d->set_last_error(DatabaseError, "%s: %s", funcName(), d->db->last_error_message().c_str()); + // not a fault: compaction only returns space, and freed pages are reused for new blocks anyway + cswarning() << "Storage> compaction skipped: " << d->db->last_error_message(); return false; } return true; From 5e8905c3709472b2b1a9e19964c8670fae80e27a Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Mon, 12 Oct 2026 09:53:09 +0200 Subject: [PATCH 3/3] csdb, csnode: removing a pruned block can no longer terminate the node On mainnet a pruned node syncing fast terminated in Db::del: BerkeleyDB's default buffer cache was exhausted (BDB3017) and the exception escaped DatabaseBerkeleyDB::remove. remove now turns any exception into an error result, and pruneStep keeps a block it could not remove and retries it on the next step instead of counting it as pruned. Co-Authored-By: Claude Opus 5.5 --- csdb/src/database_berkeleydb.cpp | 38 +++++++++++++++++++------------- csnode/src/blockchain.cpp | 8 ++++++- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/csdb/src/database_berkeleydb.cpp b/csdb/src/database_berkeleydb.cpp index c265e7e4a..6f7689e26 100644 --- a/csdb/src/database_berkeleydb.cpp +++ b/csdb/src/database_berkeleydb.cpp @@ -332,23 +332,31 @@ bool DatabaseBerkeleyDB::remove(const cs::Bytes &key) { return false; } - Dbt_copy db_key(key); - Dbt_copy db_seq_no; - int status = db_seq_no_->get(nullptr, &db_key, &db_seq_no, 0); - if (status != 0) { - set_last_error_from_berkeleydb(status); - return false; - } + // BerkeleyDB throws when its buffer cache is exhausted (BDB3017, seen on mainnet while a pruned + // node removed old blocks during a fast sync): report it, the caller may retry later + try { + Dbt_copy db_key(key); + Dbt_copy db_seq_no; + int status = db_seq_no_->get(nullptr, &db_key, &db_seq_no, 0); + if (status != 0) { + set_last_error_from_berkeleydb(status); + return false; + } - status = db_seq_no_->del(nullptr, &db_key, 0); - if (status != 0) { - set_last_error_from_berkeleydb(status); - return false; - } + status = db_seq_no_->del(nullptr, &db_key, 0); + if (status != 0) { + set_last_error_from_berkeleydb(status); + return false; + } - status = db_blocks_->del(nullptr, &db_seq_no, 0); - if (status != 0) { - set_last_error_from_berkeleydb(status); + status = db_blocks_->del(nullptr, &db_seq_no, 0); + if (status != 0) { + set_last_error_from_berkeleydb(status); + return false; + } + } + catch (const std::exception& e) { + set_last_error(UnknownError, "remove: %s", e.what()); return false; } diff --git a/csnode/src/blockchain.cpp b/csnode/src/blockchain.cpp index e1dfcb3db..16fcd47be 100644 --- a/csnode/src/blockchain.cpp +++ b/csnode/src/blockchain.cpp @@ -270,9 +270,15 @@ void BlockChain::pruneStep() { if (pool.is_valid() && trxIndex_) { trxIndex_->onPruneBlock(pool); } - if (hash.is_empty() || !storage_.pool_prune(hash)) { + if (hash.is_empty()) { csdebug() << kLogPrefix << "pruned storage: block " << from << " not found"; } + else if (!storage_.pool_prune(hash) && pool.is_valid()) { + // the block is there but could not be removed now (e.g. the storage is short of buffer + // cache during a fast sync): keep it and retry from here on the next step + cswarning() << kLogPrefix << "pruned storage: block " << from << " not removed yet, will retry"; + break; + } // getHashBySequence() then answers an empty hash, as for any block not stored blockHashes_->remove(from); }