Skip to content

Commit

Permalink
Add function to close all Db's and reload the databae environment
Browse files Browse the repository at this point in the history
Adds a ReloadDbEnv function to BerkeleyEnvironment in order to close all Db
instances, closes the environment, resets it, and then reopens
the BerkeleyEnvironment.

Also adds a ReloadDbEnv function to BerkeleyDatabase that calls
BerkeleyEnvironment's ReloadDbEnv.

Github-Pull: bitcoin#12493
Rebased-From: 5d296ac
  • Loading branch information
achow101 authored and promag committed Mar 11, 2019
1 parent 392d138 commit f455979
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ void BerkeleyBatch::Close()
LOCK(cs_db);
--env->mapFileUseCount[strFile];
}
env->m_db_in_use.notify_all();
}

void BerkeleyEnvironment::CloseDb(const std::string& strFile)
Expand All @@ -572,6 +573,32 @@ void BerkeleyEnvironment::CloseDb(const std::string& strFile)
}
}

void BerkeleyEnvironment::ReloadDbEnv()
{
// Make sure that no Db's are in use
AssertLockNotHeld(cs_db);
std::unique_lock<CCriticalSection> lock(cs_db);
m_db_in_use.wait(lock, [this](){
for (auto& count : mapFileUseCount) {
if (count.second > 0) return false;
}
return true;
});

std::vector<std::string> filenames;
for (auto it : mapDb) {
filenames.push_back(it.first);
}
// Close the individual Db's
for (const std::string& filename : filenames) {
CloseDb(filename);
}
// Reset the environment
Flush(true); // This will flush and close the environment
Reset();
Open(true);
}

bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
{
if (database.IsDummy()) {
Expand Down Expand Up @@ -799,3 +826,10 @@ void BerkeleyDatabase::Flush(bool shutdown)
if (shutdown) env = nullptr;
}
}

void BerkeleyDatabase::ReloadDbEnv()
{
if (!IsDummy()) {
env->ReloadDbEnv();
}
}
4 changes: 4 additions & 0 deletions src/wallet/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BerkeleyEnvironment
std::unique_ptr<DbEnv> dbenv;
std::map<std::string, int> mapFileUseCount;
std::map<std::string, Db*> mapDb;
std::condition_variable_any m_db_in_use;

BerkeleyEnvironment(const fs::path& env_directory);
~BerkeleyEnvironment();
Expand Down Expand Up @@ -75,6 +76,7 @@ class BerkeleyEnvironment
void CheckpointLSN(const std::string& strFile);

void CloseDb(const std::string& strFile);
void ReloadDbEnv();

DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
{
Expand Down Expand Up @@ -145,6 +147,8 @@ class BerkeleyDatabase

void IncrementUpdateCounter();

void ReloadDbEnv();

std::atomic<unsigned int> nUpdateCounter;
unsigned int nLastSeen;
unsigned int nLastFlushed;
Expand Down

0 comments on commit f455979

Please sign in to comment.