/** Run the ledger cleaner. */ void doLedgerCleaner() { Ledger::pointer goodLedger; while (! this->threadShouldExit()) { LedgerIndex ledgerIndex; LedgerHash ledgerHash; bool doNodes; bool doTxns; while (getApp().getFeeTrack().isLoadedLocal()) { m_journal.debug << "Waiting for load to subside"; std::this_thread::sleep_for(std::chrono::seconds(5)); if (this->threadShouldExit ()) return; } { SharedState::Access state (m_state); if ((state->minRange > state->maxRange) || (state->maxRange == 0) || (state->minRange == 0)) { state->minRange = state->maxRange = 0; return; } ledgerIndex = state->maxRange; doNodes = state->checkNodes; doTxns = state->fixTxns; } ledgerHash = getHash(ledgerIndex, goodLedger); bool fail = false; if (ledgerHash.isZero()) { m_journal.info << "Unable to get hash for ledger " << ledgerIndex; fail = true; } else if (!doLedger(ledgerIndex, ledgerHash, doNodes, doTxns)) { m_journal.info << "Failed to process ledger " << ledgerIndex; fail = true; } if (fail) { { SharedState::Access state (m_state); ++state->failures; } // Wait for acquiring to catch up to us std::this_thread::sleep_for(std::chrono::seconds(2)); } else { { SharedState::Access state (m_state); if (ledgerIndex == state->minRange) ++state->minRange; if (ledgerIndex == state->maxRange) --state->maxRange; state->failures = 0; } // Reduce I/O pressure and wait for acquiring to catch up to us std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } }
/** Run the ledger cleaner. */ void doLedgerCleaner() { auto shouldExit = [this]() { std::lock_guard<std::mutex> lock(mutex_); return shouldExit_; }; std::shared_ptr<ReadView const> goodLedger; while (! shouldExit()) { LedgerIndex ledgerIndex; LedgerHash ledgerHash; bool doNodes; bool doTxns; while (app_.getFeeTrack().isLoadedLocal()) { JLOG (j_.debug()) << "Waiting for load to subside"; std::this_thread::sleep_for(std::chrono::seconds(5)); if (shouldExit()) return; } { std::lock_guard<std::mutex> lock (mutex_); if ((minRange_ > maxRange_) || (maxRange_ == 0) || (minRange_ == 0)) { minRange_ = maxRange_ = 0; state_ = State::readyToClean; return; } ledgerIndex = maxRange_; doNodes = checkNodes_; doTxns = fixTxns_; } ledgerHash = getHash(ledgerIndex, goodLedger); bool fail = false; if (ledgerHash.isZero()) { JLOG (j_.info()) << "Unable to get hash for ledger " << ledgerIndex; fail = true; } else if (!doLedger(ledgerIndex, ledgerHash, doNodes, doTxns)) { JLOG (j_.info()) << "Failed to process ledger " << ledgerIndex; fail = true; } if (fail) { { std::lock_guard<std::mutex> lock (mutex_); ++failures_; } // Wait for acquiring to catch up to us std::this_thread::sleep_for(std::chrono::seconds(2)); } else { { std::lock_guard<std::mutex> lock (mutex_); if (ledgerIndex == minRange_) ++minRange_; if (ledgerIndex == maxRange_) --maxRange_; failures_ = 0; } // Reduce I/O pressure and wait for acquiring to catch up to us std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } }