Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const std::string PARAM_NAME_STORAGE_CHECKPOINT_KEEP = "checkpoint_keep";
const std::string PARAM_NAME_STORAGE_CHECKPOINT_EVERY = "checkpoint_every";
const std::string PARAM_NAME_STORAGE_CHECKPOINT_EVERY_MINUTES = "checkpoint_every_minutes";
const std::string PARAM_NAME_STORAGE_PRUNE_KEEP_BLOCKS = "prune_keep_blocks";
const std::string PARAM_NAME_STORAGE_BLOCK_DB = "block_db";

const std::string PARAM_NAME_API_PORT = "port";
const std::string PARAM_NAME_AJAX_PORT = "ajax_port";
Expand Down Expand Up @@ -964,6 +965,15 @@ void Config::readStorageData(const boost::property_tree::ptree& config) {
}
checkAndSaveValue(data, block, PARAM_NAME_STORAGE_CHECKPOINT_EVERY_MINUTES, storageData_.checkpointEveryMinutes);
checkAndSaveValue(data, block, PARAM_NAME_STORAGE_PRUNE_KEEP_BLOCKS, storageData_.pruneKeepBlocks);
if (data.count(PARAM_NAME_STORAGE_BLOCK_DB)) {
const auto engine = data.get<std::string>(PARAM_NAME_STORAGE_BLOCK_DB);
if (engine == "lmdb") {
storageData_.lmdbBlocks = true;
}
else if (engine != "berkeleydb") {
cswarning() << "config: unknown block_db=" << engine << ", using berkeleydb";
}
}
if (storageData_.pruneKeepBlocks > 0) {
// contracts refer to blocks up to 100 rounds back; keep a margin above that
if (storageData_.pruneKeepBlocks < 200) {
Expand Down
1 change: 1 addition & 0 deletions client/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct StorageData {
size_t checkpointEvery = 500'000; // blocks between periodic checkpoints (rolling history depth = checkpointEvery * checkpointKeep)
size_t checkpointEveryMinutes = 0; // wall-clock fallback: also save if this many minutes elapsed since last save (0 = disabled; opt-in for slow networks)
size_t pruneKeepBlocks = 0; // pruned storage: blocks kept before the previous periodic checkpoint (0 = keep every block)
bool lmdbBlocks = false; // block database on LMDB (block_db=lmdb) instead of BerkeleyDB; meant for pruned storage
};

struct ApiData {
Expand Down
2 changes: 2 additions & 0 deletions csdb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(${PROJECT_NAME} STATIC
src/priv_crypto.hpp
src/database.cpp
src/database_berkeleydb.cpp
src/database_lmdb.cpp
src/user_field.cpp
include/csdb/internal/shared_data.hpp
include/csdb/internal/shared_data_ptr_implementation.hpp
Expand Down Expand Up @@ -79,6 +80,7 @@ target_link_libraries(
Boost::filesystem
Boost::disable_autolinking
BerkeleyDB
lmdb
lz4
lib
)
Expand Down
50 changes: 50 additions & 0 deletions csdb/include/csdb/database_lmdb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @file database_lmdb.hpp
* Block storage on LMDB, for pruned nodes ([storage] block_db=lmdb).
*/

#ifndef _CREDITS_CSDB_DATABASE_LMDB_H_INCLUDED_
#define _CREDITS_CSDB_DATABASE_LMDB_H_INCLUDED_

#include <csdb/database.hpp>

struct MDB_env;

namespace csdb {

// Same contract as DatabaseBerkeleyDB: blocks by 0-based sequence, a hash -> sequence index and
// contract data. BerkeleyDB's Recno keeps a placeholder for every removed record, so the pages of
// pruned blocks are never reused and its file keeps growing; LMDB frees the pages of removed
// records and reuses them, so a pruned node's storage stays the size of the blocks it keeps.
class DatabaseLmdb : public Database {
public:
DatabaseLmdb();
~DatabaseLmdb() override;

bool open(const std::string& path);

private:
bool is_open() const final;
bool put(const cs::Bytes& key, uint32_t seq_no, const cs::Bytes& value) final;
bool get(const cs::Bytes& key, cs::Bytes* value) final;
bool get(const uint32_t seq_no, cs::Bytes* value) final;
bool remove(const cs::Bytes& key) final;
bool seq_no(const cs::Bytes& key, uint32_t* value) final;
bool write_batch(const ItemList&) final;
IteratorPtr new_iterator() final;

bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) final;
bool getContractData(const cs::Bytes& key, cs::Bytes& data) final;

class Iterator;

bool fail(int status, const char* what);

MDB_env* env_ = nullptr;
unsigned int blocks_ = 0; // MDB_dbi: sequence (uint32, MDB_INTEGERKEY) -> block
unsigned int seqNo_ = 0; // MDB_dbi: block hash -> sequence
unsigned int contracts_ = 0; // MDB_dbi: contract address -> contract data
};

} // namespace csdb
#endif // _CREDITS_CSDB_DATABASE_LMDB_H_INCLUDED_
10 changes: 9 additions & 1 deletion csdb/include/csdb/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ class Storage final {
* In case of fail the error info can be received with methods: \ref last_error,
* \ref last_error_message, \ref db_last_error() and \ref db_last_error_message()
*/
// block database engine: BerkeleyDB (every node so far), or LMDB in <path>/lmdb for pruned
// storage, whose removed blocks LMDB reuses the space of
enum class Engine {
BerkeleyDB,
Lmdb
};

bool open(const ::std::string& path_to_base = ::std::string{},
OpenCallback callback = nullptr,
cs::Sequence newBlockchainTop = cs::kWrongSequence,
cs::Sequence startReadFrom = 0);
cs::Sequence startReadFrom = 0,
Engine engine = Engine::BerkeleyDB);

/**
* @brief Creating the storage using the parameters set.
Expand Down
75 changes: 54 additions & 21 deletions csdb/src/database_berkeleydb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,31 @@ bool DatabaseBerkeleyDB::remove(const cs::Bytes &key) {
return false;
}

Dbt_copy<cs::Bytes> db_key(key);
Dbt_copy<uint32_t> 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<cs::Bytes> db_key(key);
Dbt_copy<uint32_t> 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;
}

Expand All @@ -362,14 +370,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<u_int32_t>(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<uint8_t*>(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;
Expand Down
Loading
Loading