Skip to content

Commit de05c9e

Browse files
author
Braydon Fuller
committed
db: add options to configure block index database
There was a previous assumption that blockindex would be quite small. With addressindex and spentindex enabled the blockindex is much larger and the amount of cache allocated for it should also increase. Furthermore, enabling compression should decrease the amount of disk space required and less data to write/read. The default leveldb max_open_files is set to 1000, for the blockindex the default is set to 1000 with compression. The 64 value that is current is kept for the utxo database and does not enable compression. Two additional options are added here to be able to configure the values for leveldb and the block index: - `-dbmaxopenfiles` A number of files for leveldb to keep open - `-dbcompression` Boolean 0 or 1 to enable snappy leveldb compression
1 parent e0d02ff commit de05c9e

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

src/dbwrapper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ void HandleError(const leveldb::Status& status) throw(dbwrapper_error)
2929
throw dbwrapper_error("Unknown database error");
3030
}
3131

32-
static leveldb::Options GetOptions(size_t nCacheSize)
32+
static leveldb::Options GetOptions(size_t nCacheSize, bool compression, int maxOpenFiles)
3333
{
3434
leveldb::Options options;
3535
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
3636
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
3737
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
38-
options.compression = leveldb::kNoCompression;
39-
options.max_open_files = 64;
38+
options.compression = compression ? leveldb::kSnappyCompression : leveldb::kNoCompression;
39+
options.max_open_files = maxOpenFiles;
4040
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
4141
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
4242
// on corruption in later versions.
@@ -45,14 +45,14 @@ static leveldb::Options GetOptions(size_t nCacheSize)
4545
return options;
4646
}
4747

48-
CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
48+
CDBWrapper::CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate, bool compression, int maxOpenFiles)
4949
{
5050
penv = NULL;
5151
readoptions.verify_checksums = true;
5252
iteroptions.verify_checksums = true;
5353
iteroptions.fill_cache = false;
5454
syncoptions.sync = true;
55-
options = GetOptions(nCacheSize);
55+
options = GetOptions(nCacheSize, compression, maxOpenFiles);
5656
options.create_if_missing = true;
5757
if (fMemory) {
5858
penv = leveldb::NewMemEnv(leveldb::Env::Default());

src/dbwrapper.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,16 @@ class CDBWrapper
169169

170170
public:
171171
/**
172-
* @param[in] path Location in the filesystem where leveldb data will be stored.
173-
* @param[in] nCacheSize Configures various leveldb cache settings.
174-
* @param[in] fMemory If true, use leveldb's memory environment.
175-
* @param[in] fWipe If true, remove all existing data.
176-
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
177-
* with a zero'd byte array.
172+
* @param[in] path Location in the filesystem where leveldb data will be stored.
173+
* @param[in] nCacheSize Configures various leveldb cache settings.
174+
* @param[in] fMemory If true, use leveldb's memory environment.
175+
* @param[in] fWipe If true, remove all existing data.
176+
* @param[in] obfuscate If true, store data obfuscated via simple XOR. If false, XOR
177+
* with a zero'd byte array.
178+
* @param[in] compression Enable snappy compression for the database
179+
* @param[in] maxOpenFiles The maximum number of open files for the database
178180
*/
179-
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false);
181+
CDBWrapper(const boost::filesystem::path& path, size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool obfuscate = false, bool compression = false, int maxOpenFiles = 64);
180182
~CDBWrapper();
181183

182184
template <typename K, typename V>

src/init.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,18 +1294,33 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
12941294
}
12951295
}
12961296

1297+
// block tree db settings
1298+
int dbMaxOpenFiles = GetArg("-dbmaxopenfiles", DEFAULT_DB_MAX_OPEN_FILES);
1299+
bool dbCompression = GetBoolArg("-dbcompression", DEFAULT_DB_COMPRESSION);
1300+
1301+
LogPrintf("Block index database configuration:\n");
1302+
LogPrintf("* Using %d max open files\n", dbMaxOpenFiles);
1303+
LogPrintf("* Compression is %s\n", dbCompression ? "enabled" : "disabled");
1304+
12971305
// cache size calculations
12981306
int64_t nTotalCache = (GetArg("-dbcache", nDefaultDbCache) << 20);
12991307
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
13001308
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greated than nMaxDbcache
13011309
int64_t nBlockTreeDBCache = nTotalCache / 8;
1302-
if (nBlockTreeDBCache > (1 << 21) && !GetBoolArg("-txindex", DEFAULT_TXINDEX))
1303-
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
1310+
if (GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX) || GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) {
1311+
// enable 3/4 of the cache if addressindex and/or spentindex is enabled
1312+
nBlockTreeDBCache = nTotalCache * 3 / 4;
1313+
} else {
1314+
if (nBlockTreeDBCache > (1 << 21) && !GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1315+
nBlockTreeDBCache = (1 << 21); // block tree db cache shouldn't be larger than 2 MiB
1316+
}
1317+
}
13041318
nTotalCache -= nBlockTreeDBCache;
13051319
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
13061320
nTotalCache -= nCoinDBCache;
13071321
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
13081322
LogPrintf("Cache configuration:\n");
1323+
LogPrintf("* Max cache setting possible %.1fMiB\n", nMaxDbCache);
13091324
LogPrintf("* Using %.1fMiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
13101325
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
13111326
LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
@@ -1326,7 +1341,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13261341
delete pcoinscatcher;
13271342
delete pblocktree;
13281343

1329-
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
1344+
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex, dbCompression, dbMaxOpenFiles);
13301345
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
13311346
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
13321347
pcoinsTip = new CCoinsViewCache(pcoinscatcher);

src/main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ static const bool DEFAULT_TXINDEX = false;
115115
static const bool DEFAULT_ADDRESSINDEX = false;
116116
static const bool DEFAULT_TIMESTAMPINDEX = false;
117117
static const bool DEFAULT_SPENTINDEX = false;
118+
static const unsigned int DEFAULT_DB_MAX_OPEN_FILES = 1000;
119+
static const bool DEFAULT_DB_COMPRESSION = true;
118120
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
119121

120122
static const bool DEFAULT_TESTSAFEMODE = false;

src/txdb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static const char DB_REINDEX_FLAG = 'R';
3333
static const char DB_LAST_BLOCK = 'l';
3434

3535

36-
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, true)
36+
CCoinsViewDB::CCoinsViewDB(size_t nCacheSize, bool fMemory, bool fWipe) : db(GetDataDir() / "chainstate", nCacheSize, fMemory, fWipe, true, false, 64)
3737
{
3838
}
3939

@@ -75,7 +75,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
7575
return db.WriteBatch(batch);
7676
}
7777

78-
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
78+
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe, bool compression, int maxOpenFiles) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe, false, compression, maxOpenFiles) {
7979
}
8080

8181
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {

src/txdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CCoinsViewDB : public CCoinsView
5454
class CBlockTreeDB : public CDBWrapper
5555
{
5656
public:
57-
CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
57+
CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool compression = true, int maxOpenFiles = 1000);
5858
private:
5959
CBlockTreeDB(const CBlockTreeDB&);
6060
void operator=(const CBlockTreeDB&);

0 commit comments

Comments
 (0)