-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from SparqNet/state_dump
State dump
- Loading branch information
Showing
41 changed files
with
1,943 additions
and
1,524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright (c) [2023-2024] [Sparq Network] | ||
This software is distributed under the MIT License. | ||
See the LICENSE.txt file in the project root for more information. | ||
*/ | ||
|
||
#include "dump.h" | ||
|
||
DumpManager::DumpManager(const Storage& storage, const Options& options, std::shared_mutex& stateMutex) | ||
: storage_(storage), | ||
options_(options), | ||
stateMutex_(stateMutex) | ||
{ | ||
} | ||
|
||
void DumpManager::pushBack(Dumpable* dumpable) | ||
{ | ||
// Check if latest Dumpable* is the same as the one we trying to append | ||
if (this->dumpables_.size() > 0 && | ||
this->dumpables_.back() == dumpable) { | ||
return; | ||
} | ||
dumpables_.push_back(dumpable); | ||
} | ||
|
||
void DumpManager::dumpAll() | ||
{ | ||
std::vector<DBBatch> batches; | ||
{ | ||
// state mutex lock | ||
std::unique_lock lock(stateMutex_); | ||
// Emplace DBBatch operations | ||
Logger::logToDebug(LogType::INFO, | ||
Log::dump, | ||
__func__, | ||
"Emplace DBBatch operations"); | ||
for (const auto dumpable: dumpables_) { | ||
// call dump functions and put the operations ate the database | ||
batches.emplace_back(dumpable->dump()); | ||
} | ||
} | ||
// Logs | ||
Logger::logToDebug(LogType::INFO, | ||
Log::dump, | ||
__func__, | ||
"Write to state database."); | ||
// Write to the database | ||
std::string dbName = options_.getRootPath() + "/stateDb/" + std::to_string(this->storage_.latest()->getNHeight()); | ||
DB stateDb(dbName); | ||
for (const auto& batch: batches) { | ||
stateDb.putBatch(batch); | ||
} | ||
} | ||
|
||
DumpWorker::DumpWorker(const Options& options, const Storage& storage, | ||
DumpManager& dumpManager) | ||
: options_(options), | ||
storage_(storage), | ||
dumpManager_(dumpManager) | ||
{ | ||
Logger::logToDebug(LogType::INFO, Log::dump, __func__, "DumpWorker Started."); | ||
} | ||
|
||
DumpWorker::~DumpWorker() | ||
{ | ||
Logger::logToDebug(LogType::INFO, Log::dump, __func__, "DumpWorker Stopped."); | ||
} | ||
|
||
bool DumpWorker::workerLoop() | ||
{ | ||
uint64_t latestBlock = 0; | ||
while (!this->stopWorker_) { | ||
if (latestBlock + 100 < this->storage_.currentChainSize()) { | ||
Logger::logToDebug(LogType::INFO, | ||
Log::dump, | ||
__func__, | ||
"Current size >= 100"); | ||
latestBlock = this->storage_.currentChainSize(); | ||
dumpManager_.dumpAll(); | ||
} | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
} | ||
return true; | ||
} | ||
|
||
void DumpWorker::startWorker() | ||
{ | ||
if (!this->workerFuture_.valid()) { | ||
this->workerFuture_ = std::async(std::launch::async, | ||
&DumpWorker::workerLoop, | ||
this); | ||
} | ||
} | ||
|
||
void DumpWorker::stopWorker() | ||
{ | ||
if (this->workerFuture_.valid()) { | ||
this->stopWorker_ = true; | ||
this->workerFuture_.wait(); | ||
this->workerFuture_.get(); | ||
} | ||
} |
Oops, something went wrong.