Use DeadlineTimer in NetworkOPs

This commit is contained in:
Vinnie Falco
2013-07-08 18:09:56 -07:00
parent 7ec73089aa
commit 4094316940
3 changed files with 74 additions and 84 deletions

View File

@@ -4,7 +4,6 @@
*/ */
//============================================================================== //==============================================================================
SETUP_LOG (NetworkOPs) SETUP_LOG (NetworkOPs)
// This is the primary interface into the "client" portion of the program. // This is the primary interface into the "client" portion of the program.
@@ -18,14 +17,13 @@ SETUP_LOG (NetworkOPs)
// code assumes this node is synched (and will continue to do so until // code assumes this node is synched (and will continue to do so until
// there's a functional network. // there's a functional network.
NetworkOPs::NetworkOPs (boost::asio::io_service& io_service, LedgerMaster* pLedgerMaster) NetworkOPs::NetworkOPs (LedgerMaster* pLedgerMaster)
: mMode (omDISCONNECTED) : mMode (omDISCONNECTED)
, mNeedNetworkLedger (false) , mNeedNetworkLedger (false)
, mProposing (false) , mProposing (false)
, mValidating (false) , mValidating (false)
, mFeatureBlocked (false) , mFeatureBlocked (false)
//, m_netTimer (this) , m_netTimer (this)
, mNetTimer (io_service)
, mLedgerMaster (pLedgerMaster) , mLedgerMaster (pLedgerMaster)
, mCloseTimeOffset (0) , mCloseTimeOffset (0)
, mLastCloseProposers (0) , mLastCloseProposers (0)
@@ -39,17 +37,56 @@ NetworkOPs::NetworkOPs (boost::asio::io_service& io_service, LedgerMaster* pLedg
, mLastLoadBase (256) , mLastLoadBase (256)
, mLastLoadFactor (256) , mLastLoadFactor (256)
{ {
// m_netTimer.setExpirationRecurring (LEDGER_GRANULARITY / 1000.0);
} }
void NetworkOPs::onDeadlineTimer () void NetworkOPs::onDeadlineTimer ()
{ {
// WriteLog (lsINFO, NetworkOPs) << "NetworkOPs::onDeadlineTimer ()"; ScopedLock sl (getApp().getMasterLock ());
getApp().getLoadManager ().resetDeadlockDetector ();
std::size_t const numPeers = getApp().getPeers ().getPeerVector ().size ();
// do we have sufficient peers? If not, we are disconnected.
if (numPeers < theConfig.NETWORK_QUORUM)
{
if (mMode != omDISCONNECTED)
{
setMode (omDISCONNECTED);
WriteLog (lsWARNING, NetworkOPs)
<< "Node count (" << numPeers << ") "
<< "has fallen below quorum (" << theConfig.NETWORK_QUORUM << ").";
}
return;
}
if (mMode == omDISCONNECTED)
{
setMode (omCONNECTED);
WriteLog (lsINFO, NetworkOPs) << "Node count (" << numPeers << ") is sufficient.";
}
// Check if the last validated ledger forces a change between these states
if (mMode == omSYNCING)
{
setMode (omSYNCING);
}
else if (mMode == omCONNECTED)
{
setMode (omCONNECTED);
}
if (!mConsensus)
tryStartConsensus ();
if (mConsensus)
mConsensus->timerEntry ();
} }
std::string NetworkOPs::strOperatingMode () std::string NetworkOPs::strOperatingMode ()
{ {
static const char* paStatusToken[] = static const char* paStatusToken [] =
{ {
"disconnected", "disconnected",
"connected", "connected",
@@ -73,7 +110,10 @@ std::string NetworkOPs::strOperatingMode ()
boost::posix_time::ptime NetworkOPs::getNetworkTimePT () boost::posix_time::ptime NetworkOPs::getNetworkTimePT ()
{ {
int offset = 0; int offset = 0;
getApp().getSystemTimeOffset (offset); getApp().getSystemTimeOffset (offset);
// VFALCO TODO Replace this with a beast call
return boost::posix_time::microsec_clock::universal_time () + boost::posix_time::seconds (offset); return boost::posix_time::microsec_clock::universal_time () + boost::posix_time::seconds (offset);
} }
@@ -587,8 +627,7 @@ void NetworkOPs::setFeatureBlocked ()
void NetworkOPs::setStateTimer () void NetworkOPs::setStateTimer ()
{ {
mNetTimer.expires_from_now (boost::posix_time::milliseconds (LEDGER_GRANULARITY)); m_netTimer.setRecurringExpiration (LEDGER_GRANULARITY / 1000.0);
mNetTimer.async_wait (boost::bind (&NetworkOPs::checkState, this, boost::asio::placeholders::error));
} }
class ValidationCount class ValidationCount
@@ -599,19 +638,23 @@ public:
ValidationCount () : trustedValidations (0), nodesUsing (0) ValidationCount () : trustedValidations (0), nodesUsing (0)
{ {
;
} }
bool operator> (const ValidationCount& v) bool operator> (const ValidationCount& v)
{ {
if (trustedValidations > v.trustedValidations) return true; if (trustedValidations > v.trustedValidations)
return true;
if (trustedValidations < v.trustedValidations) return false; if (trustedValidations < v.trustedValidations)
return false;
if (trustedValidations == 0) if (trustedValidations == 0)
{ {
if (nodesUsing > v.nodesUsing) return true; if (nodesUsing > v.nodesUsing)
return true;
if (nodesUsing < v.nodesUsing) return false; if (nodesUsing < v.nodesUsing) return
false;
return highNodeUsing > v.highNodeUsing; return highNodeUsing > v.highNodeUsing;
} }
@@ -620,61 +663,6 @@ public:
} }
}; };
void NetworkOPs::checkState (const boost::system::error_code& result)
{
// Network state machine
if ((result == boost::asio::error::operation_aborted) || theConfig.RUN_STANDALONE)
{
// VFALCO NOTE Should never get here. This is probably dead code.
// If RUN_STANDALONE is set then this function isn't called.
//
WriteLog (lsFATAL, NetworkOPs) << "Network state timer error: " << result;
return;
}
{
ScopedLock sl (getApp().getMasterLock ());
getApp().getLoadManager ().resetDeadlockDetector ();
std::vector<Peer::pointer> peerList = getApp().getPeers ().getPeerVector ();
// do we have sufficient peers? If not, we are disconnected.
if (peerList.size () < theConfig.NETWORK_QUORUM)
{
if (mMode != omDISCONNECTED)
{
setMode (omDISCONNECTED);
WriteLog (lsWARNING, NetworkOPs) << "Node count (" << peerList.size () <<
") has fallen below quorum (" << theConfig.NETWORK_QUORUM << ").";
}
return;
}
if (mMode == omDISCONNECTED)
{
setMode (omCONNECTED);
WriteLog (lsINFO, NetworkOPs) << "Node count (" << peerList.size () << ") is sufficient.";
}
// Check if the last validated ledger forces a change between these states
if (mMode == omSYNCING)
setMode (omSYNCING);
else if (mMode == omCONNECTED)
setMode (omCONNECTED);
if (!mConsensus)
tryStartConsensus ();
if (mConsensus)
mConsensus->timerEntry ();
}
setStateTimer ();
}
void NetworkOPs::tryStartConsensus () void NetworkOPs::tryStartConsensus ()
{ {
uint256 networkClosed; uint256 networkClosed;
@@ -1698,7 +1686,7 @@ void NetworkOPs::pubAccountTransaction (Ledger::ref lpCurrent, const AcceptedLed
{ {
BOOST_FOREACH (const RippleAddress & affectedAccount, alTx.getAffected ()) BOOST_FOREACH (const RippleAddress & affectedAccount, alTx.getAffected ())
{ {
subInfoMapIterator simiIt = mSubRTAccount.find (affectedAccount.getAccountID ()); SubInfoMapIterator simiIt = mSubRTAccount.find (affectedAccount.getAccountID ());
if (simiIt != mSubRTAccount.end ()) if (simiIt != mSubRTAccount.end ())
{ {
@@ -1767,7 +1755,7 @@ void NetworkOPs::pubAccountTransaction (Ledger::ref lpCurrent, const AcceptedLed
void NetworkOPs::subAccount (InfoSub::ref isrListener, const boost::unordered_set<RippleAddress>& vnaAccountIDs, uint32 uLedgerIndex, bool rt) void NetworkOPs::subAccount (InfoSub::ref isrListener, const boost::unordered_set<RippleAddress>& vnaAccountIDs, uint32 uLedgerIndex, bool rt)
{ {
subInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount; SubInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount;
// For the connection, monitor each account. // For the connection, monitor each account.
BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs) BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs)
@@ -1781,7 +1769,7 @@ void NetworkOPs::subAccount (InfoSub::ref isrListener, const boost::unordered_se
BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs) BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs)
{ {
subInfoMapType::iterator simIterator = subMap.find (naAccountID.getAccountID ()); SubInfoMapType::iterator simIterator = subMap.find (naAccountID.getAccountID ());
if (simIterator == subMap.end ()) if (simIterator == subMap.end ())
{ {
@@ -1800,7 +1788,7 @@ void NetworkOPs::subAccount (InfoSub::ref isrListener, const boost::unordered_se
void NetworkOPs::unsubAccount (uint64 uSeq, const boost::unordered_set<RippleAddress>& vnaAccountIDs, bool rt) void NetworkOPs::unsubAccount (uint64 uSeq, const boost::unordered_set<RippleAddress>& vnaAccountIDs, bool rt)
{ {
subInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount; SubInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount;
// For the connection, unmonitor each account. // For the connection, unmonitor each account.
// FIXME: Don't we need to unsub? // FIXME: Don't we need to unsub?
@@ -1813,7 +1801,7 @@ void NetworkOPs::unsubAccount (uint64 uSeq, const boost::unordered_set<RippleAdd
BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs) BOOST_FOREACH (const RippleAddress & naAccountID, vnaAccountIDs)
{ {
subInfoMapType::iterator simIterator = subMap.find (naAccountID.getAccountID ()); SubInfoMapType::iterator simIterator = subMap.find (naAccountID.getAccountID ());
if (simIterator == subMap.end ()) if (simIterator == subMap.end ())

View File

@@ -54,7 +54,9 @@ public:
typedef boost::unordered_map <uint64, InfoSub::wptr> SubMapType; typedef boost::unordered_map <uint64, InfoSub::wptr> SubMapType;
public: public:
NetworkOPs (boost::asio::io_service& io_service, LedgerMaster* pLedgerMaster); // VFALCO TODO Make LedgerMaster a SharedObjectPtr or a reference.
//
explicit NetworkOPs (LedgerMaster* pLedgerMaster);
// network information // network information
uint32 getNetworkTimeNC (); // Our best estimate of wall time in seconds from 1/1/2000 uint32 getNetworkTimeNC (); // Our best estimate of wall time in seconds from 1/1/2000
@@ -229,7 +231,9 @@ public:
void sweepFetchPack (); void sweepFetchPack ();
// network state machine // network state machine
void checkState (const boost::system::error_code& result);
// VFALCO TODO Try to make all these private since they seem to be...private
//
void switchLastClosedLedger (Ledger::pointer newLedger, bool duringConsensus); // Used for the "jump" case void switchLastClosedLedger (Ledger::pointer newLedger, bool duringConsensus); // Used for the "jump" case
bool checkLastClosedLedger (const std::vector<Peer::pointer>&, uint256& networkClosed); bool checkLastClosedLedger (const std::vector<Peer::pointer>&, uint256& networkClosed);
int beginConsensus (uint256 const& networkClosed, Ledger::pointer closingLedger); int beginConsensus (uint256 const& networkClosed, Ledger::pointer closingLedger);
@@ -370,9 +374,8 @@ private:
void pubServer (); void pubServer ();
private: private:
typedef boost::unordered_map <uint160, SubMapType> subInfoMapType; typedef boost::unordered_map <uint160, SubMapType> SubInfoMapType;
typedef boost::unordered_map <uint160, SubMapType>::value_type subInfoMapValue; typedef boost::unordered_map <uint160, SubMapType>::iterator SubInfoMapIterator;
typedef boost::unordered_map <uint160, SubMapType>::iterator subInfoMapIterator;
typedef boost::unordered_map<std::string, InfoSub::pointer> subRpcMapType; typedef boost::unordered_map<std::string, InfoSub::pointer> subRpcMapType;
@@ -381,8 +384,7 @@ private:
bool mProposing, mValidating; bool mProposing, mValidating;
bool mFeatureBlocked; bool mFeatureBlocked;
boost::posix_time::ptime mConnectTime; boost::posix_time::ptime mConnectTime;
//DeadlineTimer m_netTimer; DeadlineTimer m_netTimer;
boost::asio::deadline_timer mNetTimer;
boost::shared_ptr<LedgerConsensus> mConsensus; boost::shared_ptr<LedgerConsensus> mConsensus;
boost::unordered_map < uint160, boost::unordered_map < uint160,
std::list<LedgerProposal::pointer> > mStoredProposals; std::list<LedgerProposal::pointer> > mStoredProposals;
@@ -404,8 +406,8 @@ private:
// XXX Split into more locks. // XXX Split into more locks.
boost::recursive_mutex mMonitorLock; boost::recursive_mutex mMonitorLock;
subInfoMapType mSubAccount; SubInfoMapType mSubAccount;
subInfoMapType mSubRTAccount; SubInfoMapType mSubRTAccount;
subRpcMapType mRpcSubMap; subRpcMapType mRpcSubMap;

View File

@@ -280,7 +280,7 @@ Application::Application ()
#endif #endif
, mIOService ((theConfig.NODE_SIZE >= 2) ? 2 : 1) , mIOService ((theConfig.NODE_SIZE >= 2) ? 2 : 1)
, mIOWork (mIOService) , mIOWork (mIOService)
, mNetOps (mIOService, &mLedgerMaster) , mNetOps (&mLedgerMaster)
, mTempNodeCache ("NodeCache", 16384, 90) , mTempNodeCache ("NodeCache", 16384, 90)
, mHashedObjectStore (16384, 300) , mHashedObjectStore (16384, 300)
, mSLECache ("LedgerEntryCache", 4096, 120) , mSLECache ("LedgerEntryCache", 4096, 120)