JSON cache for account offers and account lines.

This commit is contained in:
JoelKatz
2013-06-12 15:25:51 -07:00
parent 4de1dcae5e
commit 987dc25a48
5 changed files with 75 additions and 24 deletions

View File

@@ -37,6 +37,7 @@
#include "src/cpp/ripple/ripple_InfoSub.h" #include "src/cpp/ripple/ripple_InfoSub.h"
// Order and indentation reflect the hierarchy of dependencies // Order and indentation reflect the hierarchy of dependencies
#include "src/cpp/ripple/JSONCache.h"
#include "src/cpp/ripple/ripple_HashedObject.h" #include "src/cpp/ripple/ripple_HashedObject.h"
#include "src/cpp/ripple/ripple_SHAMapItem.h" #include "src/cpp/ripple/ripple_SHAMapItem.h"
#include "src/cpp/ripple/ripple_SHAMapNode.h" #include "src/cpp/ripple/ripple_SHAMapNode.h"

View File

@@ -185,6 +185,7 @@
#include "src/cpp/ripple/LedgerEntrySet.h" #include "src/cpp/ripple/LedgerEntrySet.h"
#include "src/cpp/ripple/TransactionEngine.h" #include "src/cpp/ripple/TransactionEngine.h"
#include "src/cpp/ripple/ripple_CanonicalTXSet.h" #include "src/cpp/ripple/ripple_CanonicalTXSet.h"
#include "src/cpp/ripple/JSONCache.h"
#include "src/cpp/ripple/ripple_LedgerHistory.h" #include "src/cpp/ripple/ripple_LedgerHistory.h"
#include "src/cpp/ripple/LedgerMaster.h" #include "src/cpp/ripple/LedgerMaster.h"

View File

@@ -17,7 +17,7 @@ NetworkOPs::NetworkOPs(boost::asio::io_service& io_service, LedgerMaster* pLedge
mFeatureBlocked(false), mFeatureBlocked(false),
mNetTimer(io_service), mLedgerMaster(pLedgerMaster), mCloseTimeOffset(0), mLastCloseProposers(0), mNetTimer(io_service), mLedgerMaster(pLedgerMaster), mCloseTimeOffset(0), mLastCloseProposers(0),
mLastCloseConvergeTime(1000 * LEDGER_IDLE_INTERVAL), mLastCloseTime(0), mLastValidationTime(0), mLastCloseConvergeTime(1000 * LEDGER_IDLE_INTERVAL), mLastCloseTime(0), mLastValidationTime(0),
mFetchPack("FetchPack", 2048, 20), mLastFetchPack(0), mFetchSeq(static_cast<uint32>(-1)), mJSONCache(120), mFetchPack("FetchPack", 2048, 20), mLastFetchPack(0), mFetchSeq(static_cast<uint32>(-1)),
mLastLoadBase(256), mLastLoadFactor(256) mLastLoadBase(256), mLastLoadFactor(256)
{ {
} }
@@ -2074,6 +2074,7 @@ void NetworkOPs::makeFetchPack(Job&, boost::weak_ptr<Peer> wPeer,
void NetworkOPs::sweepFetchPack() void NetworkOPs::sweepFetchPack()
{ {
mFetchPack.sweep(); mFetchPack.sweep();
mJSONCache.sweep();
} }
void NetworkOPs::addFetchPack(uint256 const& hash, boost::shared_ptr< Blob >& data) void NetworkOPs::addFetchPack(uint256 const& hash, boost::shared_ptr< Blob >& data)

View File

@@ -176,6 +176,16 @@ public:
int getFetchSize(); int getFetchSize();
void sweepFetchPack(); void sweepFetchPack();
float getJSONHitRate() { return mJSONCache.getHitRate(); }
int getJSONEntries() { return mJSONCache.getCount(); }
void storeJSONCache(int operation, const uint256& ledger, const uint160& object,
const boost::shared_ptr<Json::Value>& data)
{ mJSONCache.storeEntry(operation, ledger, object, data); }
boost::shared_ptr<Json::Value> getJSONCache(int operation, const uint256& ledger, const uint160& object)
{ return mJSONCache.getEntry(operation, ledger, object); }
// network state machine // network state machine
void checkState(const boost::system::error_code& result); void checkState(const boost::system::error_code& result);
void switchLastClosedLedger(Ledger::pointer newLedger, bool duringConsensus); // Used for the "jump" case void switchLastClosedLedger(Ledger::pointer newLedger, bool duringConsensus); // Used for the "jump" case
@@ -303,6 +313,8 @@ private:
subMapType mSubTransactions; // all accepted transactions subMapType mSubTransactions; // all accepted transactions
subMapType mSubRTTransactions; // all proposed and accepted transactions subMapType mSubRTTransactions; // all proposed and accepted transactions
JSONCache<UptimeTimerAdapter> mJSONCache;
TaggedCache< uint256, Blob , UptimeTimerAdapter > mFetchPack; TaggedCache< uint256, Blob , UptimeTimerAdapter > mFetchPack;
uint32 mLastFetchPack; uint32 mLastFetchPack;
uint32 mFetchSeq; uint32 mFetchSeq;

View File

@@ -1049,8 +1049,12 @@ Json::Value RPCHandler::doAccountLines(Json::Value jvRequest, int& cost, ScopedL
if (!lpLedger) if (!lpLedger)
return jvResult; return jvResult;
bool bUnlocked = false;
if (lpLedger->isImmutable()) if (lpLedger->isImmutable())
{
MasterLockHolder.unlock(); MasterLockHolder.unlock();
bUnlocked = true;
}
if (!jvRequest.isMember("account")) if (!jvRequest.isMember("account"))
return rpcError(rpcINVALID_PARAMS); return rpcError(rpcINVALID_PARAMS);
@@ -1088,32 +1092,47 @@ Json::Value RPCHandler::doAccountLines(Json::Value jvRequest, int& cost, ScopedL
{ {
jvResult["account"] = raAccount.humanAccountID(); jvResult["account"] = raAccount.humanAccountID();
AccountItems rippleLines(raAccount.getAccountID(), lpLedger, AccountItem::pointer(new RippleState())); boost::shared_ptr<Json::Value> jvsLines =
theApp->getOPs().getJSONCache(JC_OP_ACCOUNT_LINES, lpLedger->getHash(), raAccount.getAccountID());
Json::Value& jsonLines = (jvResult["lines"] = Json::arrayValue); if (!jvsLines)
BOOST_FOREACH(AccountItem::ref item, rippleLines.getItems())
{ {
RippleState* line=(RippleState*)item.get(); jvsLines = boost::make_shared<Json::Value>(Json::arrayValue);
Json::Value& jsonLines = *jvsLines;
if (!raPeer.isValid() || raPeer.getAccountID() == line->getAccountIDPeer()) AccountItems rippleLines(raAccount.getAccountID(), lpLedger, AccountItem::pointer(new RippleState()));
BOOST_FOREACH(AccountItem::ref item, rippleLines.getItems())
{ {
const STAmount& saBalance = line->getBalance(); RippleState* line=(RippleState*)item.get();
const STAmount& saLimit = line->getLimit();
const STAmount& saLimitPeer = line->getLimitPeer();
Json::Value& jPeer = jsonLines.append(Json::objectValue); if (!raPeer.isValid() || raPeer.getAccountID() == line->getAccountIDPeer())
{
const STAmount& saBalance = line->getBalance();
const STAmount& saLimit = line->getLimit();
const STAmount& saLimitPeer = line->getLimitPeer();
jPeer["account"] = RippleAddress::createHumanAccountID(line->getAccountIDPeer()); Json::Value& jPeer = jsonLines.append(Json::objectValue);
// Amount reported is positive if current account holds other account's IOUs.
// Amount reported is negative if other account holds current account's IOUs. jPeer["account"] = RippleAddress::createHumanAccountID(line->getAccountIDPeer());
jPeer["balance"] = saBalance.getText(); // Amount reported is positive if current account holds other account's IOUs.
jPeer["currency"] = saBalance.getHumanCurrency(); // Amount reported is negative if other account holds current account's IOUs.
jPeer["limit"] = saLimit.getText(); jPeer["balance"] = saBalance.getText();
jPeer["limit_peer"] = saLimitPeer.getText(); jPeer["currency"] = saBalance.getHumanCurrency();
jPeer["quality_in"] = static_cast<Json::UInt>(line->getQualityIn()); jPeer["limit"] = saLimit.getText();
jPeer["quality_out"] = static_cast<Json::UInt>(line->getQualityOut()); jPeer["limit_peer"] = saLimitPeer.getText();
jPeer["quality_in"] = static_cast<Json::UInt>(line->getQualityIn());
jPeer["quality_out"] = static_cast<Json::UInt>(line->getQualityOut());
}
} }
theApp->getOPs().storeJSONCache(JC_OP_ACCOUNT_LINES, lpLedger->getHash(),
raAccount.getAccountID(), jvsLines);
} }
if (!bUnlocked)
MasterLockHolder.unlock();
jvResult["lines"] = *jvsLines;
} }
else else
{ {
@@ -1149,8 +1168,12 @@ Json::Value RPCHandler::doAccountOffers(Json::Value jvRequest, int& cost, Scoped
if (!lpLedger) if (!lpLedger)
return jvResult; return jvResult;
bool bUnlocked = false;
if (lpLedger->isImmutable()) if (lpLedger->isImmutable())
{
MasterLockHolder.unlock(); MasterLockHolder.unlock();
bUnlocked = true;
}
if (!jvRequest.isMember("account")) if (!jvRequest.isMember("account"))
return rpcError(rpcINVALID_PARAMS); return rpcError(rpcINVALID_PARAMS);
@@ -1172,11 +1195,22 @@ Json::Value RPCHandler::doAccountOffers(Json::Value jvRequest, int& cost, Scoped
if (bIndex) if (bIndex)
jvResult["account_index"] = iIndex; jvResult["account_index"] = iIndex;
if (lpLedger->hasAccount(raAccount)) if (!lpLedger->hasAccount(raAccount))
lpLedger->visitAccountItems(raAccount.getAccountID(), return rpcError(rpcACT_NOT_FOUND);
BIND_TYPE(&offerAdder, boost::ref(jvResult["offers"] = Json::arrayValue), P_1));
else boost::shared_ptr<Json::Value> jvsOffers =
jvResult = rpcError(rpcACT_NOT_FOUND); theApp->getOPs().getJSONCache(JC_OP_ACCOUNT_OFFERS, lpLedger->getHash(), raAccount.getAccountID());
if (!jvsOffers)
{
jvsOffers = boost::make_shared<Json::Value>(Json::arrayValue);
lpLedger->visitAccountItems(raAccount.getAccountID(), BIND_TYPE(&offerAdder, boost::ref(*jvsOffers), P_1));
theApp->getOPs().storeJSONCache(JC_OP_ACCOUNT_OFFERS, lpLedger->getHash(), raAccount.getAccountID(), jvsOffers);
}
if (!bUnlocked)
MasterLockHolder.unlock();
jvResult["offers"] = *jvsOffers;
return jvResult; return jvResult;
} }
@@ -2343,6 +2377,8 @@ Json::Value RPCHandler::doGetCounts(Json::Value jvRequest, int& cost, ScopedLock
ret["node_hit_rate"] = theApp->getHashedObjectStore().getCacheHitRate(); ret["node_hit_rate"] = theApp->getHashedObjectStore().getCacheHitRate();
ret["ledger_hit_rate"] = theApp->getLedgerMaster().getCacheHitRate(); ret["ledger_hit_rate"] = theApp->getLedgerMaster().getCacheHitRate();
ret["AL_hit_rate"] = AcceptedLedger::getCacheHitRate(); ret["AL_hit_rate"] = AcceptedLedger::getCacheHitRate();
ret["JC_hit_rate"] = theApp->getOPs().getJSONHitRate();
ret["JC_size"] = theApp->getOPs().getJSONEntries();
ret["fullbelow_size"] = SHAMap::getFullBelowSize(); ret["fullbelow_size"] = SHAMap::getFullBelowSize();