PageRenderTime 31ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libretroshare/src/gxs/rsgxsutil.cc

https://gitlab.com/g10h4ck/RetroShare
C++ | 340 lines | 233 code | 68 blank | 39 comment | 45 complexity | c44e1032091b280379e7d0894f3bcf73 MD5 | raw file
Possible License(s): 0BSD, GPL-2.0, AGPL-1.0
  1. /*
  2. * libretroshare/src/gxs: rsgxsutil.cc
  3. *
  4. * RetroShare C++ Interface. Generic routines that are useful in GXS
  5. *
  6. * Copyright 2013-2013 by Christopher Evi-Parker
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License Version 2 as published by the Free Software Foundation.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. * USA.
  21. *
  22. * Please report all bugs and problems to "retroshare@lunamutt.com".
  23. *
  24. */
  25. #include <time.h>
  26. #include "rsgxsutil.h"
  27. #include "retroshare/rsgxsflags.h"
  28. #include "retroshare/rspeers.h"
  29. #include "pqi/pqihash.h"
  30. #include "gxs/rsgixs.h"
  31. static const uint32_t MAX_GXS_IDS_REQUESTS_NET = 10 ; // max number of requests from cache/net (avoids killing the system!)
  32. //#define GXSUTIL_DEBUG 1
  33. RsGxsMessageCleanUp::RsGxsMessageCleanUp(RsGeneralDataService* const dataService, uint32_t messageStorePeriod, uint32_t chunkSize)
  34. : mDs(dataService), MESSAGE_STORE_PERIOD(messageStorePeriod), CHUNK_SIZE(chunkSize)
  35. {
  36. std::map<RsGxsGroupId, RsGxsGrpMetaData*> grpMeta;
  37. mDs->retrieveGxsGrpMetaData(grpMeta);
  38. std::map<RsGxsGroupId, RsGxsGrpMetaData*>::iterator cit = grpMeta.begin();
  39. for(;cit != grpMeta.end(); ++cit)
  40. {
  41. mGrpMeta.push_back(cit->second);
  42. }
  43. }
  44. bool RsGxsMessageCleanUp::clean()
  45. {
  46. int i = 1;
  47. time_t now = time(NULL);
  48. while(!mGrpMeta.empty())
  49. {
  50. RsGxsGrpMetaData* grpMeta = mGrpMeta.back();
  51. const RsGxsGroupId& grpId = grpMeta->mGroupId;
  52. mGrpMeta.pop_back();
  53. GxsMsgReq req;
  54. GxsMsgMetaResult result;
  55. req[grpId] = std::vector<RsGxsMessageId>();
  56. mDs->retrieveGxsMsgMetaData(req, result);
  57. GxsMsgMetaResult::iterator mit = result.begin();
  58. req.clear();
  59. for(; mit != result.end(); ++mit)
  60. {
  61. std::vector<RsGxsMsgMetaData*>& metaV = mit->second;
  62. std::vector<RsGxsMsgMetaData*>::iterator vit = metaV.begin();
  63. for(; vit != metaV.end(); )
  64. {
  65. RsGxsMsgMetaData* meta = *vit;
  66. // check if expired
  67. bool remove = (meta->mPublishTs + MESSAGE_STORE_PERIOD) < now;
  68. // check client does not want the message kept regardless of age
  69. remove &= !(meta->mMsgStatus & GXS_SERV::GXS_MSG_STATUS_KEEP);
  70. // if not subscribed remove messages (can optimise this really)
  71. remove = remove || (grpMeta->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_NOT_SUBSCRIBED);
  72. if( remove )
  73. {
  74. req[grpId].push_back(meta->mMsgId);
  75. }
  76. delete meta;
  77. vit = metaV.erase(vit);
  78. }
  79. }
  80. mDs->removeMsgs(req);
  81. delete grpMeta;
  82. i++;
  83. if(i > CHUNK_SIZE) break;
  84. }
  85. return mGrpMeta.empty();
  86. }
  87. RsGxsIntegrityCheck::RsGxsIntegrityCheck(RsGeneralDataService* const dataService, RsGixs *gixs) :
  88. mDs(dataService), mDone(false), mIntegrityMutex("integrity"),mGixs(gixs)
  89. { }
  90. void RsGxsIntegrityCheck::run()
  91. {
  92. check();
  93. }
  94. bool RsGxsIntegrityCheck::check()
  95. {
  96. // first take out all the groups
  97. std::map<RsGxsGroupId, RsNxsGrp*> grp;
  98. mDs->retrieveNxsGrps(grp, true, true);
  99. std::vector<RsGxsGroupId> grpsToDel;
  100. GxsMsgReq msgIds;
  101. GxsMsgReq grps;
  102. std::set<RsGxsId> used_gxs_ids ;
  103. std::set<RsGxsGroupId> subscribed_groups ;
  104. // compute hash and compare to stored value, if it fails then simply add it
  105. // to list
  106. std::map<RsGxsGroupId, RsNxsGrp*>::iterator git = grp.begin();
  107. for(; git != grp.end(); ++git)
  108. {
  109. RsNxsGrp* grp = git->second;
  110. RsFileHash currHash;
  111. pqihash pHash;
  112. pHash.addData(grp->grp.bin_data, grp->grp.bin_len);
  113. pHash.Complete(currHash);
  114. if(currHash == grp->metaData->mHash)
  115. {
  116. // get all message ids of group
  117. if (mDs->retrieveMsgIds(grp->grpId, msgIds[grp->grpId]) == 1)
  118. {
  119. // store the group for retrieveNxsMsgs
  120. grps[grp->grpId];
  121. if(grp->metaData->mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED)
  122. {
  123. subscribed_groups.insert(git->first) ;
  124. if(!grp->metaData->mAuthorId.isNull())
  125. {
  126. #ifdef GXSUTIL_DEBUG
  127. std::cerr << "TimeStamping group authors' key ID " << grp->metaData->mAuthorId << " in group ID " << grp->grpId << std::endl;
  128. #endif
  129. used_gxs_ids.insert(grp->metaData->mAuthorId) ;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. msgIds.erase(msgIds.find(grp->grpId));
  136. // grpsToDel.push_back(grp->grpId);
  137. }
  138. }
  139. else
  140. {
  141. grpsToDel.push_back(grp->grpId);
  142. }
  143. delete grp;
  144. }
  145. mDs->removeGroups(grpsToDel);
  146. // now messages
  147. GxsMsgReq msgsToDel;
  148. GxsMsgResult msgs;
  149. mDs->retrieveNxsMsgs(grps, msgs, false, true);
  150. // check msg ids and messages
  151. GxsMsgReq::iterator msgIdsIt;
  152. for (msgIdsIt = msgIds.begin(); msgIdsIt != msgIds.end(); ++msgIdsIt)
  153. {
  154. const RsGxsGroupId& grpId = msgIdsIt->first;
  155. std::vector<RsGxsMessageId> &msgIdV = msgIdsIt->second;
  156. std::vector<RsGxsMessageId>::iterator msgIdIt;
  157. for (msgIdIt = msgIdV.begin(); msgIdIt != msgIdV.end(); ++msgIdIt)
  158. {
  159. const RsGxsMessageId& msgId = *msgIdIt;
  160. std::vector<RsNxsMsg*> &nxsMsgV = msgs[grpId];
  161. std::vector<RsNxsMsg*>::iterator nxsMsgIt;
  162. for (nxsMsgIt = nxsMsgV.begin(); nxsMsgIt != nxsMsgV.end(); ++nxsMsgIt)
  163. {
  164. RsNxsMsg *nxsMsg = *nxsMsgIt;
  165. if (nxsMsg && msgId == nxsMsg->msgId)
  166. {
  167. break;
  168. }
  169. }
  170. if (nxsMsgIt == nxsMsgV.end())
  171. {
  172. msgsToDel[grpId].push_back(msgId);
  173. }
  174. }
  175. }
  176. GxsMsgResult::iterator mit = msgs.begin();
  177. for(; mit != msgs.end(); ++mit)
  178. {
  179. std::vector<RsNxsMsg*>& msgV = mit->second;
  180. std::vector<RsNxsMsg*>::iterator vit = msgV.begin();
  181. for(; vit != msgV.end(); ++vit)
  182. {
  183. RsNxsMsg* msg = *vit;
  184. RsFileHash currHash;
  185. pqihash pHash;
  186. pHash.addData(msg->msg.bin_data, msg->msg.bin_len);
  187. pHash.Complete(currHash);
  188. if(msg->metaData == NULL || currHash != msg->metaData->mHash)
  189. {
  190. std::cerr << "(EE) deleting message data with wrong hash or null meta data. meta=" << (void*)msg->metaData << std::endl;
  191. msgsToDel[msg->grpId].push_back(msg->msgId);
  192. }
  193. else if(!msg->metaData->mAuthorId.isNull() && subscribed_groups.find(msg->metaData->mGroupId)!=subscribed_groups.end())
  194. {
  195. #ifdef GXSUTIL_DEBUG
  196. std::cerr << "TimeStamping message authors' key ID " << msg->metaData->mAuthorId << " in message " << msg->msgId << ", group ID " << msg->grpId<< std::endl;
  197. #endif
  198. used_gxs_ids.insert(msg->metaData->mAuthorId) ;
  199. }
  200. delete msg;
  201. }
  202. }
  203. mDs->removeMsgs(msgsToDel);
  204. RsStackMutex stack(mIntegrityMutex);
  205. mDone = true;
  206. std::vector<RsGxsGroupId>::iterator grpIt;
  207. for(grpIt = grpsToDel.begin(); grpIt != grpsToDel.end(); ++grpIt)
  208. {
  209. mDeletedGrps.push_back(*grpIt);
  210. }
  211. mDeletedMsgs = msgsToDel;
  212. #ifdef GXSUTIL_DEBUG
  213. std::cerr << "At end of pass, this is the list used GXS ids: " << std::endl;
  214. std::cerr << " requesting them to GXS identity service to enforce loading." << std::endl;
  215. #endif
  216. std::list<RsPeerId> connected_friends ;
  217. rsPeers->getOnlineList(connected_friends) ;
  218. std::vector<RsGxsId> gxs_ids ;
  219. for(std::set<RsGxsId>::const_iterator it(used_gxs_ids.begin());it!=used_gxs_ids.end();++it)
  220. {
  221. gxs_ids.push_back(*it) ;
  222. #ifdef GXSUTIL_DEBUG
  223. std::cerr << " " << *it << std::endl;
  224. #endif
  225. }
  226. int nb_requested_not_in_cache = 0;
  227. #ifdef GXSUTIL_DEBUG
  228. std::cerr << " issuing random get on friends for non existing IDs" << std::endl;
  229. #endif
  230. // now request a cache update for them, which triggers downloading from friends, if missing.
  231. for(;nb_requested_not_in_cache<MAX_GXS_IDS_REQUESTS_NET && gxs_ids.size()>0;)
  232. {
  233. uint32_t n = RSRandom::random_u32() % gxs_ids.size() ;
  234. #ifdef GXSUTIL_DEBUG
  235. std::cerr << " requesting ID " << gxs_ids[n] ;
  236. #endif
  237. if(!mGixs->haveKey(gxs_ids[n])) // checks if we have it already in the cache (conservative way to ensure that we atually have it)
  238. {
  239. mGixs->requestKey(gxs_ids[n],connected_friends);
  240. ++nb_requested_not_in_cache ;
  241. #ifdef GXSUTIL_DEBUG
  242. std::cerr << " ... from cache/net" << std::endl;
  243. #endif
  244. }
  245. else
  246. {
  247. #ifdef GXSUTIL_DEBUG
  248. std::cerr << " ... already in cache" << std::endl;
  249. #endif
  250. // Note: we could time_stamp even in the case where the id is not cached. Anyway, it's not really a problem here, since IDs have a high chance of
  251. // behing eventually stamped.
  252. mGixs->timeStampKey(gxs_ids[n]) ;
  253. }
  254. gxs_ids[n] = gxs_ids[gxs_ids.size()-1] ;
  255. gxs_ids.pop_back() ;
  256. }
  257. #ifdef GXSUTIL_DEBUG
  258. std::cerr << " total actual cache requests: "<< nb_requested_not_in_cache << std::endl;
  259. #endif
  260. return true;
  261. }
  262. bool RsGxsIntegrityCheck::isDone()
  263. {
  264. RsStackMutex stack(mIntegrityMutex);
  265. return mDone;
  266. }
  267. void RsGxsIntegrityCheck::getDeletedIds(std::list<RsGxsGroupId>& grpIds, std::map<RsGxsGroupId, std::vector<RsGxsMessageId> >& msgIds)
  268. {
  269. RsStackMutex stack(mIntegrityMutex);
  270. grpIds = mDeletedGrps;
  271. msgIds = mDeletedMsgs;
  272. }