Merge #8065: Addrman offline attempts

6182d10 Do not increment nAttempts by more than one for every Good connection. (Gregory Maxwell)
c769c4a Avoid counting failed connect attempts when probably offline. (Gregory Maxwell)
This commit is contained in:
Wladimir J. van der Laan 2016-06-08 12:58:21 +02:00
commit 67c91f8c4c
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
5 changed files with 31 additions and 17 deletions

View File

@ -197,6 +197,9 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
void CAddrMan::Good_(const CService& addr, int64_t nTime) void CAddrMan::Good_(const CService& addr, int64_t nTime)
{ {
int nId; int nId;
nLastGood = nTime;
CAddrInfo* pinfo = Find(addr, &nId); CAddrInfo* pinfo = Find(addr, &nId);
// if not found, bail out // if not found, bail out
@ -311,7 +314,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
return fNew; return fNew;
} }
void CAddrMan::Attempt_(const CService& addr, int64_t nTime) void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
{ {
CAddrInfo* pinfo = Find(addr); CAddrInfo* pinfo = Find(addr);
@ -327,7 +330,10 @@ void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
// update info // update info
info.nLastTry = nTime; info.nLastTry = nTime;
info.nAttempts++; if (fCountFailure && info.nLastCountAttempt < nLastGood) {
info.nLastCountAttempt = nTime;
info.nAttempts++;
}
} }
CAddrInfo CAddrMan::Select_(bool newOnly) CAddrInfo CAddrMan::Select_(bool newOnly)

View File

@ -29,6 +29,9 @@ public:
//! last try whatsoever by us (memory only) //! last try whatsoever by us (memory only)
int64_t nLastTry; int64_t nLastTry;
//! last counted attempt (memory only)
int64_t nLastCountAttempt;
private: private:
//! where knowledge about this address first came from //! where knowledge about this address first came from
CNetAddr source; CNetAddr source;
@ -66,6 +69,7 @@ public:
{ {
nLastSuccess = 0; nLastSuccess = 0;
nLastTry = 0; nLastTry = 0;
nLastCountAttempt = 0;
nAttempts = 0; nAttempts = 0;
nRefCount = 0; nRefCount = 0;
fInTried = false; fInTried = false;
@ -200,6 +204,9 @@ private:
//! list of "new" buckets //! list of "new" buckets
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]; int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
//! last time Good was called (memory only)
int64_t nLastGood;
protected: protected:
//! secret key to randomize bucket select with //! secret key to randomize bucket select with
uint256 nKey; uint256 nKey;
@ -230,7 +237,7 @@ protected:
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty); bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
//! Mark an entry as attempted to connect. //! Mark an entry as attempted to connect.
void Attempt_(const CService &addr, int64_t nTime); void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime);
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from. //! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
CAddrInfo Select_(bool newOnly); CAddrInfo Select_(bool newOnly);
@ -458,6 +465,7 @@ public:
nIdCount = 0; nIdCount = 0;
nTried = 0; nTried = 0;
nNew = 0; nNew = 0;
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
} }
CAddrMan() CAddrMan()
@ -532,12 +540,12 @@ public:
} }
//! Mark an entry as connection attempted to. //! Mark an entry as connection attempted to.
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime()) void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
{ {
{ {
LOCK(cs); LOCK(cs);
Check(); Check();
Attempt_(addr, nTime); Attempt_(addr, fCountFailure, nTime);
Check(); Check();
} }
} }

View File

@ -365,7 +365,7 @@ CNode* FindNode(const CService& addr)
return NULL; return NULL;
} }
CNode* ConnectNode(CAddress addrConnect, const char *pszDest) CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
{ {
if (pszDest == NULL) { if (pszDest == NULL) {
if (IsLocal(addrConnect)) if (IsLocal(addrConnect))
@ -397,7 +397,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
return NULL; return NULL;
} }
addrman.Attempt(addrConnect); addrman.Attempt(addrConnect, fCountFailure);
// Add node // Add node
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false); CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
@ -414,7 +414,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
} else if (!proxyConnectionFailed) { } else if (!proxyConnectionFailed) {
// If connecting to the node failed, and failure is not caused by a problem connecting to // If connecting to the node failed, and failure is not caused by a problem connecting to
// the proxy, mark this as an attempt. // the proxy, mark this as an attempt.
addrman.Attempt(addrConnect); addrman.Attempt(addrConnect, fCountFailure);
} }
return NULL; return NULL;
@ -1531,7 +1531,7 @@ void static ProcessOneShot()
CAddress addr; CAddress addr;
CSemaphoreGrant grant(*semOutbound, true); CSemaphoreGrant grant(*semOutbound, true);
if (grant) { if (grant) {
if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true)) if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
AddOneShot(strDest); AddOneShot(strDest);
} }
} }
@ -1547,7 +1547,7 @@ void ThreadOpenConnections()
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"]) BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
{ {
CAddress addr; CAddress addr;
OpenNetworkConnection(addr, NULL, strAddr.c_str()); OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
for (int i = 0; i < 10 && i < nLoop; i++) for (int i = 0; i < 10 && i < nLoop; i++)
{ {
MilliSleep(500); MilliSleep(500);
@ -1631,7 +1631,7 @@ void ThreadOpenConnections()
} }
if (addrConnect.IsValid()) if (addrConnect.IsValid())
OpenNetworkConnection(addrConnect, &grant); OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant);
} }
} }
@ -1653,7 +1653,7 @@ void ThreadOpenAddedConnections()
BOOST_FOREACH(const std::string& strAddNode, lAddresses) { BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
CAddress addr; CAddress addr;
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(addr, &grant, strAddNode.c_str()); OpenNetworkConnection(addr, false, &grant, strAddNode.c_str());
MilliSleep(500); MilliSleep(500);
} }
MilliSleep(120000); // Retry every 2 minutes MilliSleep(120000); // Retry every 2 minutes
@ -1692,7 +1692,7 @@ void ThreadOpenAddedConnections()
BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd) BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd)
{ {
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant); OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), false, &grant);
MilliSleep(500); MilliSleep(500);
} }
MilliSleep(120000); // Retry every 2 minutes MilliSleep(120000); // Retry every 2 minutes
@ -1700,7 +1700,7 @@ void ThreadOpenAddedConnections()
} }
// if successful, this moves the passed grant to the constructed node // if successful, this moves the passed grant to the constructed node
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot) bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
{ {
// //
// Initiate outbound network connection // Initiate outbound network connection
@ -1714,7 +1714,7 @@ bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOu
} else if (FindNode(std::string(pszDest))) } else if (FindNode(std::string(pszDest)))
return false; return false;
CNode* pnode = ConnectNode(addrConnect, pszDest); CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
if (!pnode) if (!pnode)

View File

@ -84,7 +84,7 @@ CNode* FindNode(const CNetAddr& ip);
CNode* FindNode(const CSubNet& subNet); CNode* FindNode(const CSubNet& subNet);
CNode* FindNode(const std::string& addrName); CNode* FindNode(const std::string& addrName);
CNode* FindNode(const CService& ip); CNode* FindNode(const CService& ip);
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false); bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
void MapPort(bool fUseUPnP); void MapPort(bool fUseUPnP);
unsigned short GetListenPort(); unsigned short GetListenPort();
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false); bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);

View File

@ -219,7 +219,7 @@ UniValue addnode(const UniValue& params, bool fHelp)
if (strCommand == "onetry") if (strCommand == "onetry")
{ {
CAddress addr; CAddress addr;
OpenNetworkConnection(addr, NULL, strNode.c_str()); OpenNetworkConnection(addr, false, NULL, strNode.c_str());
return NullUniValue; return NullUniValue;
} }