From 4812d30c1695a2cc853d18cbbc2ccdaac106eb69 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 25 Apr 2013 19:11:18 -0700 Subject: [PATCH] Be smarter about when we grab fetch packs. --- src/cpp/ripple/LedgerMaster.cpp | 2 +- src/cpp/ripple/NetworkOPs.cpp | 17 ++++++++++++----- src/cpp/ripple/NetworkOPs.h | 5 +++-- src/cpp/ripple/Peer.cpp | 2 +- src/cpp/ripple/TaggedCache.h | 7 +++++++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/cpp/ripple/LedgerMaster.cpp b/src/cpp/ripple/LedgerMaster.cpp index 6d2cd820f1..377650973a 100644 --- a/src/cpp/ripple/LedgerMaster.cpp +++ b/src/cpp/ripple/LedgerMaster.cpp @@ -284,7 +284,7 @@ bool LedgerMaster::acquireMissingLedger(Ledger::ref origLedger, const uint256& l } } - if (theApp->getOPs().shouldFetchPack() && (ledgerSeq > 40000)) + if (theApp->getOPs().shouldFetchPack(ledgerSeq) && (ledgerSeq > 40000)) { // refill our fetch pack Ledger::pointer nextLedger = mLedgerHistory.getLedgerBySeq(ledgerSeq + 1); if (nextLedger) diff --git a/src/cpp/ripple/NetworkOPs.cpp b/src/cpp/ripple/NetworkOPs.cpp index 4d660cdc48..6df40d787b 100644 --- a/src/cpp/ripple/NetworkOPs.cpp +++ b/src/cpp/ripple/NetworkOPs.cpp @@ -36,7 +36,7 @@ NetworkOPs::NetworkOPs(boost::asio::io_service& io_service, LedgerMaster* pLedge mMode(omDISCONNECTED), mNeedNetworkLedger(false), mProposing(false), mValidating(false), mNetTimer(io_service), mLedgerMaster(pLedgerMaster), mCloseTimeOffset(0), mLastCloseProposers(0), mLastCloseConvergeTime(1000 * LEDGER_IDLE_INTERVAL), mLastValidationTime(0), - mFetchPack("FetchPack", 2048, 30), mLastFetchPack(0), + mFetchPack("FetchPack", 2048, 30), mLastFetchPack(0), mFetchSeq(static_cast(-1)), mLastLoadBase(256), mLastLoadFactor(256) { } @@ -2091,13 +2091,19 @@ bool NetworkOPs::getFetchPack(const uint256& hash, std::vector& d return true; } -bool NetworkOPs::shouldFetchPack() +bool NetworkOPs::shouldFetchPack(uint32 seq) { uint32 now = getNetworkTimeNC(); if ((mLastFetchPack == now) || ((mLastFetchPack + 1) == now)) return false; - mFetchPack.sweep(); - if (mFetchPack.getCacheSize() > 384) + if (seq < mFetchSeq) // fetch pack has only data for ledgers ahead of where we are + mFetchPack.clear(); + else + mFetchPack.sweep(); + int size = mFetchPack.getCacheSize(); + if (size == 0) + mFetchSeq = static_cast(-1); + else if (mFetchPack.getCacheSize() > 384) return false; mLastFetchPack = now; return true; @@ -2108,9 +2114,10 @@ int NetworkOPs::getFetchSize() return mFetchPack.getCacheSize(); } -void NetworkOPs::gotFetchPack(bool progress) +void NetworkOPs::gotFetchPack(bool progress, uint32 seq) { mLastFetchPack = 0; + mFetchSeq = seq; // earliest pack we have data on theApp->getJobQueue().addJob(jtLEDGER_DATA, "gotFetchPack", boost::bind(&LedgerAcquireMaster::gotFetchPack, &theApp->getMasterLedgerAcquire(), _1)); } diff --git a/src/cpp/ripple/NetworkOPs.h b/src/cpp/ripple/NetworkOPs.h index 095ef27a33..6469d8ff5e 100644 --- a/src/cpp/ripple/NetworkOPs.h +++ b/src/cpp/ripple/NetworkOPs.h @@ -130,6 +130,7 @@ protected: TaggedCache< uint256, std::vector > mFetchPack; uint32 mLastFetchPack; + uint32 mFetchSeq; uint32 mLastLoadBase; uint32 mLastLoadFactor; @@ -262,8 +263,8 @@ public: bool stillNeedTXSet(const uint256& hash); void makeFetchPack(Job&, boost::weak_ptr peer, boost::shared_ptr request, Ledger::pointer wantLedger, Ledger::pointer haveLedger); - bool shouldFetchPack(); - void gotFetchPack(bool progress); + bool shouldFetchPack(uint32 seq); + void gotFetchPack(bool progress, uint32 seq); void addFetchPack(const uint256& hash, boost::shared_ptr< std::vector >& data); bool getFetchPack(const uint256& hash, std::vector& data); int getFetchSize(); diff --git a/src/cpp/ripple/Peer.cpp b/src/cpp/ripple/Peer.cpp index c9a7a7d2a7..98c7cbd38e 100644 --- a/src/cpp/ripple/Peer.cpp +++ b/src/cpp/ripple/Peer.cpp @@ -1274,7 +1274,7 @@ void Peer::recvGetObjectByHash(const boost::shared_ptrgetOPs().gotFetchPack(progress); + theApp->getOPs().gotFetchPack(progress, pLSeq); } } diff --git a/src/cpp/ripple/TaggedCache.h b/src/cpp/ripple/TaggedCache.h index c7fb419028..03999099be 100644 --- a/src/cpp/ripple/TaggedCache.h +++ b/src/cpp/ripple/TaggedCache.h @@ -77,6 +77,7 @@ public: void setTargetSize(int size); void setTargetAge(int age); void sweep(); + void clear(); bool touch(const key_type& key); bool del(const key_type& key, bool valid); @@ -128,6 +129,12 @@ template int TaggedCache::getTra return mCache.size(); } +template void TaggedCache::clear() +{ + mCache.clear(); + mCacheCount = 0; +} + template void TaggedCache::sweep() { boost::recursive_mutex::scoped_lock sl(mLock);