Refactor TaggedCache

This commit is contained in:
Vinnie Falco
2014-01-07 20:52:03 -08:00
parent eecd305efd
commit 62516ef07f
26 changed files with 700 additions and 707 deletions

View File

@@ -17,7 +17,13 @@
*/
//==============================================================================
TaggedCacheType <uint256, AcceptedLedger, UptimeTimerAdapter> AcceptedLedger::s_cache ("AcceptedLedger", 4, 60);
// VFALCO TODO Remove this global and make it a member of the App
// Use a dependency injection to give AcceptedLedger access.
//
TaggedCacheType <uint256, AcceptedLedger> AcceptedLedger::s_cache (
"AcceptedLedger", 4, 60,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ());
AcceptedLedger::AcceptedLedger (Ledger::ref ledger) : mLedger (ledger)
{

View File

@@ -83,7 +83,7 @@ private:
void insert (AcceptedLedgerTx::ref);
private:
static TaggedCacheType <uint256, AcceptedLedger, UptimeTimerAdapter> s_cache;
static TaggedCacheType <uint256, AcceptedLedger> s_cache;
Ledger::pointer mLedger;
map_t mMap;

View File

@@ -30,8 +30,12 @@
// FIXME: Need to clean up ledgers by index at some point
LedgerHistory::LedgerHistory ()
: mLedgersByHash ("LedgerCache", CACHED_LEDGER_NUM, CACHED_LEDGER_AGE)
, mConsensusValidated ("ConsensusValidated", 64, 300)
: m_ledgers_by_hash ("LedgerCache", CACHED_LEDGER_NUM, CACHED_LEDGER_AGE,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
, m_consensus_validated ("ConsensusValidated", 64, 300,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
{
;
}
@@ -41,16 +45,16 @@ void LedgerHistory::addLedger (Ledger::pointer ledger, bool validated)
assert (ledger && ledger->isImmutable ());
assert (ledger->peekAccountStateMap ()->getHash ().isNonZero ());
TaggedCache::ScopedLockType sl (mLedgersByHash.peekMutex (), __FILE__, __LINE__);
LedgersByHash::ScopedLockType sl (m_ledgers_by_hash.peekMutex ());
mLedgersByHash.canonicalize (ledger->getHash(), ledger, true);
m_ledgers_by_hash.canonicalize (ledger->getHash(), ledger, true);
if (validated)
mLedgersByIndex[ledger->getLedgerSeq()] = ledger->getHash();
}
uint256 LedgerHistory::getLedgerHash (uint32 index)
{
TaggedCache::ScopedLockType sl (mLedgersByHash.peekMutex (), __FILE__, __LINE__);
LedgersByHash::ScopedLockType sl (m_ledgers_by_hash.peekMutex ());
std::map<uint32, uint256>::iterator it (mLedgersByIndex.find (index));
if (it != mLedgersByIndex.end ())
@@ -61,17 +65,17 @@ uint256 LedgerHistory::getLedgerHash (uint32 index)
Ledger::pointer LedgerHistory::getLedgerBySeq (uint32 index)
{
TaggedCache::ScopedLockType sl (mLedgersByHash.peekMutex (), __FILE__, __LINE__);
std::map<uint32, uint256>::iterator it (mLedgersByIndex.find (index));
if (it != mLedgersByIndex.end ())
{
uint256 hash = it->second;
sl.unlock ();
return getLedgerByHash (hash);
}
LedgersByHash::ScopedLockType sl (m_ledgers_by_hash.peekMutex ());
std::map <uint32, uint256>::iterator it (mLedgersByIndex.find (index));
sl.unlock ();
if (it != mLedgersByIndex.end ())
{
uint256 hash = it->second;
sl.unlock ();
return getLedgerByHash (hash);
}
}
Ledger::pointer ret (Ledger::loadByIndex (index));
@@ -80,16 +84,19 @@ Ledger::pointer LedgerHistory::getLedgerBySeq (uint32 index)
assert (ret->getLedgerSeq () == index);
sl.lock (__FILE__, __LINE__);
assert (ret->isImmutable ());
mLedgersByHash.canonicalize (ret->getHash (), ret);
mLedgersByIndex[ret->getLedgerSeq ()] = ret->getHash ();
return (ret->getLedgerSeq () == index) ? ret : Ledger::pointer ();
{
LedgersByHash::ScopedLockType sl (m_ledgers_by_hash.peekMutex ());
assert (ret->isImmutable ());
m_ledgers_by_hash.canonicalize (ret->getHash (), ret);
mLedgersByIndex[ret->getLedgerSeq ()] = ret->getHash ();
return (ret->getLedgerSeq () == index) ? ret : Ledger::pointer ();
}
}
Ledger::pointer LedgerHistory::getLedgerByHash (uint256 const& hash)
{
Ledger::pointer ret = mLedgersByHash.fetch (hash);
Ledger::pointer ret = m_ledgers_by_hash.fetch (hash);
if (ret)
{
@@ -105,7 +112,7 @@ Ledger::pointer LedgerHistory::getLedgerByHash (uint256 const& hash)
assert (ret->isImmutable ());
assert (ret->getHash () == hash);
mLedgersByHash.canonicalize (ret->getHash (), ret);
m_ledgers_by_hash.canonicalize (ret->getHash (), ret);
assert (ret->getHash () == hash);
return ret;
@@ -116,10 +123,11 @@ void LedgerHistory::builtLedger (Ledger::ref ledger)
LedgerIndex index = ledger->getLedgerSeq();
LedgerHash hash = ledger->getHash();
assert (!hash.isZero());
TaggedCache::ScopedLockType sl(mConsensusValidated.peekMutex(), __FILE__, __LINE__);
ConsensusValidated::ScopedLockType sl (
m_consensus_validated.peekMutex());
boost::shared_ptr< std::pair< LedgerHash, LedgerHash > > entry = boost::make_shared<std::pair< LedgerHash, LedgerHash >>();
mConsensusValidated.canonicalize(index, entry, false);
m_consensus_validated.canonicalize(index, entry, false);
if (entry->first != hash)
{
@@ -140,10 +148,11 @@ void LedgerHistory::validatedLedger (Ledger::ref ledger)
LedgerIndex index = ledger->getLedgerSeq();
LedgerHash hash = ledger->getHash();
assert (!hash.isZero());
TaggedCache::ScopedLockType sl(mConsensusValidated.peekMutex(), __FILE__, __LINE__);
ConsensusValidated::ScopedLockType sl (
m_consensus_validated.peekMutex());
boost::shared_ptr< std::pair< LedgerHash, LedgerHash > > entry = boost::make_shared<std::pair< LedgerHash, LedgerHash >>();
mConsensusValidated.canonicalize(index, entry, false);
m_consensus_validated.canonicalize(index, entry, false);
if (entry->second != hash)
{
@@ -159,11 +168,11 @@ void LedgerHistory::validatedLedger (Ledger::ref ledger)
}
}
/** Ensure mLedgersByHash doesn't have the wrong hash for a particular index
/** Ensure m_ledgers_by_hash doesn't have the wrong hash for a particular index
*/
bool LedgerHistory::fixIndex (LedgerIndex ledgerIndex, LedgerHash const& ledgerHash)
{
TaggedCache::ScopedLockType sl (mLedgersByHash.peekMutex (), __FILE__, __LINE__);
LedgersByHash::ScopedLockType sl (m_ledgers_by_hash.peekMutex ());
std::map<uint32, uint256>::iterator it (mLedgersByIndex.find (ledgerIndex));
if ((it != mLedgersByIndex.end ()) && (it->second != ledgerHash) )
@@ -176,8 +185,6 @@ bool LedgerHistory::fixIndex (LedgerIndex ledgerIndex, LedgerHash const& ledgerH
void LedgerHistory::tune (int size, int age)
{
mLedgersByHash.setTargetSize (size);
mLedgersByHash.setTargetAge (age);
m_ledgers_by_hash.setTargetSize (size);
m_ledgers_by_hash.setTargetAge (age);
}
// vim:ts=4

View File

@@ -30,7 +30,7 @@ public:
float getCacheHitRate ()
{
return mLedgersByHash.getHitRate ();
return m_ledgers_by_hash.getHitRate ();
}
Ledger::pointer getLedgerBySeq (LedgerIndex ledgerIndex);
@@ -43,8 +43,8 @@ public:
void sweep ()
{
mLedgersByHash.sweep ();
mConsensusValidated.sweep ();
m_ledgers_by_hash.sweep ();
m_consensus_validated.sweep ();
}
void builtLedger (Ledger::ref);
@@ -53,8 +53,14 @@ public:
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
private:
TaggedCacheType <LedgerHash, Ledger, UptimeTimerAdapter> mLedgersByHash;
TaggedCacheType <LedgerIndex, std::pair< LedgerHash, LedgerHash >, UptimeTimerAdapter> mConsensusValidated;
typedef TaggedCacheType <LedgerHash, Ledger> LedgersByHash;
LedgersByHash m_ledgers_by_hash;
//typedef std::pair <LedgerHash, LedgerHash>
typedef TaggedCacheType <LedgerIndex,
std::pair< LedgerHash, LedgerHash >> ConsensusValidated;
ConsensusValidated m_consensus_validated;
// Maps ledger indexes to the corresponding hash.

View File

@@ -48,6 +48,9 @@ template <> char const* LogPartition::getPartitionName <ResourceManagerLog> () {
template <> char const* LogPartition::getPartitionName <CollectorManager> () { return "Collector"; }
struct TaggedCacheLog;
template <> char const* LogPartition::getPartitionName <TaggedCacheLog> () { return "TaggedCache"; }
//
//------------------------------------------------------------------------------
@@ -73,8 +76,14 @@ public:
ApplicationImp ()
: RootStoppable ("Application")
, m_journal (LogPartition::getJournal <ApplicationLog> ())
, m_tempNodeCache ("NodeCache", 16384, 90)
, m_sleCache ("LedgerEntryCache", 4096, 120)
, m_tempNodeCache ("NodeCache", 16384, 90,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
, m_sleCache ("LedgerEntryCache", 4096, 120,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
, m_collectorManager (CollectorManager::New (
getConfig().insightSettings,

View File

@@ -48,8 +48,8 @@ class LocalCredentials;
class DatabaseCon;
typedef TaggedCacheType <uint256, Blob , UptimeTimerAdapter> NodeCache;
typedef TaggedCacheType <uint256, SerializedLedgerEntry, UptimeTimerAdapter> SLECache;
typedef TaggedCacheType <uint256, Blob> NodeCache;
typedef TaggedCacheType <uint256, SerializedLedgerEntry> SLECache;
class Application : public PropertyStream::Source
{

View File

@@ -50,7 +50,9 @@ public:
, mLastCloseConvergeTime (1000 * LEDGER_IDLE_INTERVAL)
, mLastCloseTime (0)
, mLastValidationTime (0)
, mFetchPack ("FetchPack", 65536, 45)
, mFetchPack ("FetchPack", 65536, 45,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
, mFetchSeq (0)
, mLastLoadBase (256)
, mLastLoadFactor (256)
@@ -453,7 +455,7 @@ private:
SubMapType mSubTransactions; // all accepted transactions
SubMapType mSubRTTransactions; // all proposed and accepted transactions
TaggedCacheType< uint256, Blob , UptimeTimerAdapter > mFetchPack;
TaggedCacheType< uint256, Blob> mFetchPack;
uint32 mFetchSeq;
uint32 mLastLoadBase;

View File

@@ -32,7 +32,7 @@ private:
typedef LockType::ScopedUnlockType ScopedUnlockType;
LockType mLock;
TaggedCacheType<uint256, ValidationSet, UptimeTimerAdapter> mValidations;
TaggedCacheType<uint256, ValidationSet> mValidations;
boost::unordered_map<uint160, SerializedValidation::pointer> mCurrentValidations;
std::vector<SerializedValidation::pointer> mStaleValidations;
@@ -60,7 +60,10 @@ private:
public:
ValidationsImp ()
: mLock (this, "Validations", __FILE__, __LINE__)
, mValidations ("Validations", 128, 600), mWriting (false)
, mValidations ("Validations", 128, 600,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
, mWriting (false)
{
mStaleValidations.reserve (512);
}

View File

@@ -62,18 +62,22 @@
//
#include "peers/PackedMessage.h"
namespace ripple {
// Order matters here. If you get compile errors,
// reorder the include lines until the order is correct.
namespace ripple {
#include "data/Database.h"
#include "data/DatabaseCon.h"
#include "data/SqliteDatabase.h"
#include "data/DBInit.h"
#include "shamap/SHAMapItem.h"
}
// VFALCO NOTE Have to step outside the ripple namespace to
// get the specialization for std::hash, et. al.
#include "shamap/SHAMapNode.h"
namespace ripple {
#include "shamap/SHAMapTreeNode.h"
#include "shamap/SHAMapMissingNode.h"
#include "shamap/SHAMapSyncFilter.h"
@@ -140,12 +144,11 @@ namespace ripple {
#include "tx/AccountSetTransactor.h"
#include "tx/TrustSetTransactor.h"
#include "tx/WalletAddTransactor.h"
// VFALCO NOTE These contracts files are bunk
#include "contracts/ScriptData.h"
#include "contracts/Contract.h"
#include "contracts/Interpreter.h"
#include "contracts/Operation.h"
}
#endif

View File

@@ -59,8 +59,10 @@ SHAMap::SHAMap (SHAMapType t, uint256 const& hash,
mTNByID.replace(*root, root);
}
TaggedCacheType< SHAMap::TNIndex, SHAMapTreeNode, UptimeTimerAdapter>
SHAMap::treeNodeCache ("TreeNodeCache", 65536, 60);
TaggedCacheType< SHAMap::TNIndex, SHAMapTreeNode>
SHAMap::treeNodeCache ("TreeNodeCache", 65536, 60,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ());
SHAMap::~SHAMap ()
{

View File

@@ -237,11 +237,12 @@ public:
treeNodeCache.setTargetAge (age);
}
typedef std::pair<uint256, SHAMapNode> TNIndex;
private:
static KeyCache <uint256, UptimeTimerAdapter> fullBelowCache;
typedef std::pair<uint256, SHAMapNode> TNIndex;
static TaggedCacheType <TNIndex, SHAMapTreeNode, UptimeTimerAdapter> treeNodeCache;
static TaggedCacheType <TNIndex, SHAMapTreeNode> treeNodeCache;
void dirtyUp (std::stack<SHAMapTreeNode::pointer>& stack, uint256 const & target, uint256 prevHash);
std::stack<SHAMapTreeNode::pointer> getStack (uint256 const & id, bool include_nonmatching_leaf);

View File

@@ -20,6 +20,10 @@
#ifndef RIPPLE_SHAMAPNODE_H
#define RIPPLE_SHAMAPNODE_H
#include <functional>
namespace ripple {
// Identifies a node in a SHA256 hash map
class SHAMapNode
{
@@ -127,4 +131,32 @@ inline std::ostream& operator<< (std::ostream& out, const SHAMapNode& node)
return out << node.getString ();
}
}
//------------------------------------------------------------------------------
namespace std {
template <>
struct hash <ripple::SHAMapNode>
{
std::size_t operator() (ripple::SHAMapNode const& value) const
{
return value.getMHash ();
}
};
}
//------------------------------------------------------------------------------
namespace boost {
template <>
struct hash <ripple::SHAMapNode> : std::hash <ripple::SHAMapNode>
{
};
}
#endif

View File

@@ -17,7 +17,8 @@
*/
//==============================================================================
ConsensusTransSetSF::ConsensusTransSetSF ()
ConsensusTransSetSF::ConsensusTransSetSF (NodeCache& nodeCache)
: m_nodeCache (nodeCache)
{
}
@@ -27,7 +28,7 @@ void ConsensusTransSetSF::gotNode (bool fromFilter, const SHAMapNode& id, uint25
if (fromFilter)
return;
getApp().getTempNodeCache ().store (nodeHash, nodeData);
m_nodeCache.insert (nodeHash, nodeData);
if ((type == SHAMapTreeNode::tnTRANSACTION_NM) && (nodeData.size () > 16))
{
@@ -53,9 +54,10 @@ void ConsensusTransSetSF::gotNode (bool fromFilter, const SHAMapNode& id, uint25
bool ConsensusTransSetSF::haveNode (const SHAMapNode& id, uint256 const& nodeHash,
Blob& nodeData)
{
if (getApp().getTempNodeCache ().retrieve (nodeHash, nodeData))
if (m_nodeCache.retrieve (nodeHash, nodeData))
return true;
// VFALCO TODO Use a dependency injection here
Transaction::pointer txn = getApp().getMasterTransaction().fetch(nodeHash, false);
if (txn)

View File

@@ -28,7 +28,10 @@
class ConsensusTransSetSF : public SHAMapSyncFilter
{
public:
ConsensusTransSetSF ();
typedef TaggedCacheType <uint256, Blob> NodeCache;
// VFALCO TODO Use a dependency injection to get the temp node cache
ConsensusTransSetSF (NodeCache& nodeCache);
// Note that the nodeData is overwritten by this call
void gotNode (bool fromFilter,
@@ -40,6 +43,9 @@ public:
bool haveNode (SHAMapNode const& id,
uint256 const& nodeHash,
Blob& nodeData);
private:
NodeCache& m_nodeCache;
};
// This class is only needed on add functions

View File

@@ -150,7 +150,8 @@ void TransactionAcquire::trigger (Peer::ref peer)
{
std::vector<SHAMapNode> nodeIDs;
std::vector<uint256> nodeHashes;
ConsensusTransSetSF sf;
// VFALCO TODO Use a dependency injection on the temp node cache
ConsensusTransSetSF sf (getApp().getTempNodeCache ());
mMap->getMissingNodes (nodeIDs, nodeHashes, 256, &sf);
if (nodeIDs.empty ())
@@ -201,7 +202,7 @@ SHAMapAddNode TransactionAcquire::takeNodes (const std::list<SHAMapNode>& nodeID
std::list<SHAMapNode>::const_iterator nodeIDit = nodeIDs.begin ();
std::list< Blob >::const_iterator nodeDatait = data.begin ();
ConsensusTransSetSF sf;
ConsensusTransSetSF sf (getApp().getTempNodeCache ());
while (nodeIDit != nodeIDs.end ())
{

View File

@@ -17,16 +17,10 @@
*/
//==============================================================================
#ifndef CACHED_TRANSACTION_NUM
#define CACHED_TRANSACTION_NUM 65536
#endif
#ifndef CACHED_TRANSACTION_AGE
#define CACHED_TRANSACTION_AGE 1800
#endif
TransactionMaster::TransactionMaster ()
: mCache ("TransactionCache", CACHED_TRANSACTION_NUM, CACHED_TRANSACTION_AGE)
: mCache ("TransactionCache", 65536, 1800,
get_abstract_clock <std::chrono::steady_clock, std::chrono::seconds> (),
LogPartition::getJournal <TaggedCacheLog> ())
{
;
}

View File

@@ -37,7 +37,7 @@ public:
void sweep (void);
private:
TaggedCacheType <uint256, Transaction, UptimeTimerAdapter> mCache;
TaggedCacheType <uint256, Transaction> mCache;
};
#endif