Merge #10526: Force on-the-fly compaction during pertxout upgrade

efeb273 Force on-the-fly compaction during pertxout upgrade (Pieter Wuille)

Pull request description:

  It seems that LevelDB tends to leave the old "per txid" UTXO entries in the database lying around for a significant amount of time during and after the per-txout upgrade. This introduces a `CompactRange` function in the database wrapper, and invokes it after every batch of updates in `CCoinsViewDB::Upgrade()`. This lowers temporary disk usage during and after the upgrade.

Tree-SHA512: fbf964c0a33f4e73709c999c8a2bfdef974779c15820907398a2f8828f5fa3e4e153ddd9031d6fc5083be81e22b999b9bd826fd063ad8b88f55c5e8342503290
This commit is contained in:
Wladimir J. van der Laan 2017-08-01 13:01:59 +02:00 committed by Alexander Block
parent 1a9add78c9
commit 4102211a39
2 changed files with 22 additions and 1 deletions

View File

@ -323,6 +323,23 @@ public:
pdb->GetApproximateSizes(&range, 1, &size); pdb->GetApproximateSizes(&range, 1, &size);
return size; return size;
} }
/**
* Compact a certain range of keys in the database.
*/
template<typename K>
void CompactRange(const K& key_begin, const K& key_end) const
{
CDataStream ssKey1(SER_DISK, CLIENT_VERSION), ssKey2(SER_DISK, CLIENT_VERSION);
ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey1 << key_begin;
ssKey2 << key_end;
leveldb::Slice slKey1(ssKey1.data(), ssKey1.size());
leveldb::Slice slKey2(ssKey2.data(), ssKey2.size());
pdb->CompactRange(&slKey1, &slKey2);
}
}; };
#endif // BITCOIN_DBWRAPPER_H #endif // BITCOIN_DBWRAPPER_H

View File

@ -462,12 +462,13 @@ bool CCoinsViewDB::Upgrade() {
CDBBatch batch(db); CDBBatch batch(db);
uiInterface.SetProgressBreakAction(StartShutdown); uiInterface.SetProgressBreakAction(StartShutdown);
int reportDone = 0; int reportDone = 0;
std::pair<unsigned char, uint256> key;
std::pair<unsigned char, uint256> prev_key = {DB_COINS, uint256()};
while (pcursor->Valid()) { while (pcursor->Valid()) {
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
if (ShutdownRequested()) { if (ShutdownRequested()) {
break; break;
} }
std::pair<unsigned char, uint256> key;
if (pcursor->GetKey(key) && key.first == DB_COINS) { if (pcursor->GetKey(key) && key.first == DB_COINS) {
if (count++ % 256 == 0) { if (count++ % 256 == 0) {
uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1); uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1);
@ -496,6 +497,8 @@ bool CCoinsViewDB::Upgrade() {
if (batch.SizeEstimate() > batch_size) { if (batch.SizeEstimate() > batch_size) {
db.WriteBatch(batch); db.WriteBatch(batch);
batch.Clear(); batch.Clear();
db.CompactRange(prev_key, key);
prev_key = key;
} }
pcursor->Next(); pcursor->Next();
} else { } else {
@ -503,6 +506,7 @@ bool CCoinsViewDB::Upgrade() {
} }
} }
db.WriteBatch(batch); db.WriteBatch(batch);
db.CompactRange({DB_COINS, uint256()}, key);
uiInterface.SetProgressBreakAction(std::function<void(void)>()); uiInterface.SetProgressBreakAction(std::function<void(void)>());
LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE"); LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
return true; return true;