From cafa1709b6abab21dec9900d23542efc796cf2df Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 20 Feb 2018 15:28:42 -0500 Subject: [PATCH] Add function to close all Db's and reload the databae environment 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: #12493 Rebased-From: 5d296ac --- src/wallet/db.cpp | 26 ++++++++++++++++++++++++++ src/wallet/db.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 26aeb754a..d2d8d8407 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -674,6 +674,32 @@ void BerkeleyEnvironment::ReloadDbEnv() Open(true); } +void BerkeleyEnvironment::ReloadDbEnv() +{ + // Make sure that no Db's are in use + AssertLockNotHeld(cs_db); + std::unique_lock lock(cs_db); + m_db_in_use.wait(lock, [this](){ + for (auto& count : mapFileUseCount) { + if (count.second > 0) return false; + } + return true; + }); + + std::vector 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()) { diff --git a/src/wallet/db.h b/src/wallet/db.h index 94f41eaf1..c983c1934 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -169,6 +169,8 @@ public: void ReloadDbEnv(); + void ReloadDbEnv(); + std::atomic nUpdateCounter; unsigned int nLastSeen; unsigned int nLastFlushed;