improved strategy for merging pending ID load requests, fixing bug in previous commit

This commit is contained in:
csoler 2016-07-15 22:28:36 -04:00
parent 59d08591d2
commit 8fdd255185
2 changed files with 1043 additions and 1031 deletions

View File

@ -700,8 +700,37 @@ bool p3IdService::havePrivateKey(const RsGxsId &id)
return mKeyCache.is_cached(id) ;
}
static void mergeIds(std::map<RsGxsId,std::list<RsPeerId> >& idmap,const RsGxsId& id,const std::list<RsPeerId>& peers)
{
// merge the two lists (I use a std::set to make it more efficient)
#ifdef DEBUG_IDS
std::cerr << "p3IdService::requestKey(): merging list with existing pending request." << std::endl;
#endif
std::list<RsPeerId>& old_peers(idmap[id]) ; // create if necessary
std::set<RsPeerId> new_peers ;
for(std::list<RsPeerId>::const_iterator it(peers.begin());it!=peers.end();++it)
new_peers.insert(*it) ;
for(std::list<RsPeerId>::iterator it(old_peers.begin());it!=old_peers.end();++it)
new_peers.insert(*it) ;
old_peers.clear();
for(std::set<RsPeerId>::iterator it(new_peers.begin());it!=new_peers.end();++it)
old_peers.push_back(*it) ;
}
bool p3IdService::requestKey(const RsGxsId &id, const std::list<RsPeerId>& peers)
{
if(id.isNull())
{
std::cerr << "(EE) nul ID requested to p3IdService. This should not happen. Callstack:" << std::endl;
print_stacktrace();
return false ;
}
if (haveKey(id))
return true;
else
@ -712,14 +741,13 @@ bool p3IdService::requestKey(const RsGxsId &id, const std::list<RsPeerId>& peers
if(rit != mIdsNotPresent.end())
{
locked_mergeIdsToRequestFromNet(id,peers) ;
if(!peers.empty())
mergeIds(mIdsNotPresent,id,peers) ;
return true ;
}
}
if(peers.empty())
std::cerr << "(EE) empty peer list in ID request from net. This is bound to fail!" << std::endl;
return cache_request_load(id, peers);
}
@ -734,27 +762,6 @@ bool p3IdService::isPendingNetworkRequest(const RsGxsId& gxsId)
return false;
}
void p3IdService::locked_mergeIdsToRequestFromNet(const RsGxsId& id,const std::list<RsPeerId>& peers)
{
// merge the two lists (I use a std::set to make it more efficient)
std::cerr << "p3IdService::requestKey(): merging list with existing pending request." << std::endl;
std::list<RsPeerId>& old_peers(mIdsNotPresent[id]) ; // create if necessary
std::set<RsPeerId> new_peers ;
for(std::list<RsPeerId>::const_iterator it(peers.begin());it!=peers.end();++it)
new_peers.insert(*it) ;
for(std::list<RsPeerId>::iterator it(old_peers.begin());it!=old_peers.end();++it)
new_peers.insert(*it) ;
old_peers.clear();
for(std::set<RsPeerId>::iterator it(new_peers.begin());it!=new_peers.end();++it)
old_peers.push_back(*it) ;
}
bool p3IdService::getKey(const RsGxsId &id, RsTlvPublicRSAKey &key)
{
{
@ -996,7 +1003,7 @@ bool p3IdService::getReputation(const RsGxsId &id, GixsReputation &rep)
#if 0
class RegistrationRequest
{
public:
public:
RegistrationRequest(uint32_t token, RsGxsId &id, int score)
:m_extToken(token), m_id(id), m_score(score) { return; }
@ -1306,17 +1313,17 @@ bool p3IdService::deleteGroup(uint32_t& token, RsGxsIdGroup &group)
if (lit != mOwnIds.end())
{
mOwnIds.remove((RsGxsId)*lit);
#ifdef DEBUG_IDS
#ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Removed from OwnIds";
std::cerr << std::endl;
#endif
#endif
}
else
{
#ifdef DEBUG_IDS
#ifdef DEBUG_IDS
std::cerr << "p3IdService::deleteGroup() Not in OwnIds";
std::cerr << std::endl;
#endif
#endif
}
}
@ -1477,7 +1484,7 @@ void SSGxsIdRecognTags::setTags(bool processed, bool pending, uint32_t flags)
GxsReputation::GxsReputation()
:mOverallScore(0), mIdScore(0), mOwnOpinion(0), mPeerOpinion(0)
:mOverallScore(0), mIdScore(0), mOwnOpinion(0), mPeerOpinion(0)
{
updateIdScore(false, false);
update();
@ -2048,7 +2055,8 @@ bool p3IdService::cache_request_load(const RsGxsId &id, const std::list<RsPeerId
{
RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/
mCacheLoad_ToCache.insert(std::make_pair(id, peers));
mergeIds(mCacheLoad_ToCache,id,peers) ; // merge, even if peers is empty
}
if (RsTickEvent::event_count(GXSID_EVENT_CACHELOAD) > 0)
@ -2087,10 +2095,12 @@ bool p3IdService::cache_start_load()
std::cerr << "p3IdService::cache_start_load() GroupId: " << it->first;
std::cerr << std::endl;
#endif // DEBUG_IDS
groupIds.push_back(RsGxsGroupId(it->first.toStdString())); // might need conversion?
groupIds.push_back(RsGxsGroupId(it->first)); // might need conversion?
}
mPendingCache.insert(mCacheLoad_ToCache.begin(), mCacheLoad_ToCache.end());
for(std::map<RsGxsId,std::list<RsPeerId> >::const_iterator it(mCacheLoad_ToCache.begin());it!=mCacheLoad_ToCache.end();++it)
mergeIds(mPendingCache,it->first,it->second) ;
mCacheLoad_ToCache.clear();
}
@ -2160,8 +2170,11 @@ bool p3IdService::cache_load_for_token(uint32_t token)
RsStackMutex stack(mIdMtx);
// No need to merge empty peers since the request would fail.
for(std::map<RsGxsId,std::list<RsPeerId> >::const_iterator itt(mPendingCache.begin());itt!=mPendingCache.end();++itt)
locked_mergeIdsToRequestFromNet(itt->first,itt->second) ;
if(!itt->second.empty())
mergeIds(mIdsNotPresent,itt->first,itt->second) ;
mPendingCache.clear();

View File

@ -351,7 +351,6 @@ private:
bool cache_update_if_cached(const RsGxsId &id, std::string serviceString);
bool isPendingNetworkRequest(const RsGxsId& gxsId);
void locked_mergeIdsToRequestFromNet(const RsGxsId& id,const std::list<RsPeerId>& peers);
void requestIdsFromNet();
// Mutex protected.