rps-sampler_common.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/rps-sampler_common.h
  18. * @brief Code common to client and service sampler
  19. * @author Julius Bünger
  20. */
  21. #ifndef RPS_SAMPLER_COMMON_H
  22. #define RPS_SAMPLER_COMMON_H
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet-service-rps_sampler_elem.h"
  27. #include <math.h>
  28. #include <inttypes.h>
  29. #include "rps-test_util.h"
  30. /**
  31. * Callback that is called from _get_rand_peer() when the PeerID is ready.
  32. *
  33. * @param cls the closure given alongside this function.
  34. * @param id the PeerID that was returned
  35. * @param probability The probability with which this sampler has seen all ids
  36. * @param num_observed How many ids this sampler has observed
  37. */
  38. typedef void
  39. (*RPS_sampler_rand_peer_ready_cont) (void *cls,
  40. const struct GNUNET_PeerIdentity *id,
  41. double probability,
  42. uint32_t num_observed);
  43. /**
  44. * Type of function used to differentiate between modified and not modified
  45. * Sampler.
  46. */
  47. typedef void
  48. (*RPS_get_peers_type) (void *cls);
  49. /**
  50. * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
  51. *
  52. * @param cls the closure given alongside this function.
  53. * @param ids the PeerIDs that were returned
  54. * to be freed
  55. */
  56. typedef void
  57. (*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
  58. uint32_t num_peers,
  59. void *cls);
  60. /**
  61. * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
  62. *
  63. * @param cls the closure given alongside this function.
  64. * @param probability Probability with which all IDs have been observed
  65. * @param num_observed Number of observed IDs
  66. * @param ids the PeerIDs that were returned
  67. * to be freed
  68. */
  69. typedef void
  70. (*RPS_sampler_sinlge_info_ready_cb) (const struct GNUNET_PeerIdentity *ids,
  71. void *cls,
  72. double probability,
  73. uint32_t num_observed);
  74. /**
  75. * @brief Callback called each time a new peer was put into the sampler
  76. *
  77. * @param cls A possibly given closure
  78. */
  79. typedef void
  80. (*SamplerNotifyUpdateCB) (void *cls);
  81. /**
  82. * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
  83. */
  84. struct GetPeerCls
  85. {
  86. /**
  87. * DLL
  88. */
  89. struct GetPeerCls *next;
  90. struct GetPeerCls *prev;
  91. /**
  92. * The #RPS_SamplerRequestHandle this single request belongs to.
  93. */
  94. struct RPS_SamplerRequestHandle *req_handle;
  95. /**
  96. * The #RPS_SamplerRequestHandleSingleInfo this single request belongs to.
  97. */
  98. struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle;
  99. /**
  100. * The task for this function.
  101. */
  102. struct GNUNET_SCHEDULER_Task *get_peer_task;
  103. /**
  104. * @brief Context to the given callback.
  105. */
  106. struct SamplerNotifyUpdateCTX *notify_ctx;
  107. /**
  108. * The callback
  109. */
  110. RPS_sampler_rand_peer_ready_cont cont;
  111. /**
  112. * The closure to the callback @e cont
  113. */
  114. void *cont_cls;
  115. /**
  116. * The address of the id to be stored at
  117. */
  118. struct GNUNET_PeerIdentity *id;
  119. };
  120. /**
  121. * Sampler with its own array of SamplerElements
  122. */
  123. struct RPS_Sampler
  124. {
  125. /**
  126. * Number of sampler elements we hold.
  127. */
  128. unsigned int sampler_size;
  129. // size_t size;
  130. /**
  131. * All sampler elements in one array.
  132. */
  133. struct RPS_SamplerElement **sampler_elements;
  134. /**
  135. * Maximum time a round takes
  136. *
  137. * Used in the context of RPS
  138. */
  139. struct GNUNET_TIME_Relative max_round_interval;
  140. /**
  141. * @brief The estimated total number of peers in the network
  142. */
  143. uint32_t num_peers_estim;
  144. /**
  145. * @brief The desired probability with which we want to have observed all
  146. * peers.
  147. */
  148. double desired_probability;
  149. /**
  150. * @brief A factor that catches the 'bias' of a random stream of peer ids.
  151. *
  152. * As introduced by Brahms: Factor between the number of unique ids in a
  153. * truly random stream and number of unique ids in the gossip stream.
  154. */
  155. double deficiency_factor;
  156. /**
  157. * Stores the function to return peers. Which one it is depends on whether
  158. * the Sampler is the modified one or not.
  159. */
  160. RPS_get_peers_type get_peers;
  161. /**
  162. * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
  163. */
  164. struct RPS_SamplerRequestHandle *req_handle_head;
  165. struct RPS_SamplerRequestHandle *req_handle_tail;
  166. /**
  167. * Head and tail for the DLL to store the #RPS_SamplerRequestHandleSingleInfo
  168. */
  169. struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_head;
  170. struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_tail;
  171. struct SamplerNotifyUpdateCTX *notify_ctx_head;
  172. struct SamplerNotifyUpdateCTX *notify_ctx_tail;
  173. };
  174. /**
  175. * @brief Update the current estimate of the network size stored at the sampler
  176. *
  177. * Used for computing the condition when to return elements to the client
  178. *
  179. * @param sampler The sampler to update
  180. * @param num_peers The estimated value
  181. */
  182. void
  183. RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
  184. uint32_t num_peers);
  185. /**
  186. * @brief Set the probability that is needed at least with what a sampler
  187. * element has to have observed all elements from the network.
  188. *
  189. * Only used/useful with the client sampler
  190. * (Maybe move to rps-sampler_client.{h|c} ?)
  191. *
  192. * @param sampler
  193. * @param desired_probability
  194. */
  195. void
  196. RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
  197. double desired_probability);
  198. /**
  199. * @brief Set the deficiency factor.
  200. *
  201. * Only used/useful with the client sampler
  202. * (Maybe move to rps-sampler_client.{h|c} ?)
  203. *
  204. * @param sampler
  205. * @param desired_probability
  206. */
  207. void
  208. RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
  209. double deficiency_factor);
  210. /**
  211. * @brief Add a callback that will be called when the next peer is inserted
  212. * into the sampler
  213. *
  214. * @param sampler The sampler on which update it will be called
  215. * @param notify_cb The callback
  216. * @param cls Closure given to the callback
  217. *
  218. * @return The context containing callback and closure
  219. */
  220. struct SamplerNotifyUpdateCTX *
  221. sampler_notify_on_update (struct RPS_Sampler *sampler,
  222. SamplerNotifyUpdateCB notify_cb,
  223. void *cls);
  224. /**
  225. * Update every sampler element of this sampler with given peer
  226. *
  227. * @param sampler the sampler to update.
  228. * @param id the PeerID that is put in the sampler
  229. */
  230. void
  231. RPS_sampler_update (struct RPS_Sampler *sampler,
  232. const struct GNUNET_PeerIdentity *id);
  233. /**
  234. * Reinitialise all previously initialised sampler elements with the given value.
  235. *
  236. * Used to get rid of a PeerID.
  237. *
  238. * @param sampler the sampler to reinitialise a sampler element in.
  239. * @param id the id of the sampler elements to update.
  240. */
  241. void
  242. RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
  243. const struct GNUNET_PeerIdentity *id);
  244. /**
  245. * Get the size of the sampler.
  246. *
  247. * @param sampler the sampler to return the size of.
  248. * @return the size of the sampler
  249. */
  250. unsigned int
  251. RPS_sampler_get_size (struct RPS_Sampler *sampler);
  252. /**
  253. * Grow or shrink the size of the sampler.
  254. *
  255. * @param sampler the sampler to resize.
  256. * @param new_size the new size of the sampler
  257. */
  258. void
  259. RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
  260. /**
  261. * Get n random peers out of the sampled peers.
  262. *
  263. * We might want to reinitialise this sampler after giving the
  264. * corrsponding peer to the client.
  265. * Random with or without consumption?
  266. *
  267. * @param sampler the sampler to get peers from.
  268. * @param cb callback that will be called once the ids are ready.
  269. * @param cls closure given to @a cb
  270. * @param for_client #GNUNET_YES if result is used for client,
  271. * #GNUNET_NO if used internally
  272. * @param num_peers the number of peers requested
  273. */
  274. struct RPS_SamplerRequestHandle *
  275. RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
  276. uint32_t num_peers,
  277. RPS_sampler_n_rand_peers_ready_cb cb,
  278. void *cls);
  279. /**
  280. * Get one random peer with additional information.
  281. *
  282. * @param sampler the sampler to get peers from.
  283. * @param cb callback that will be called once the ids are ready.
  284. * @param cls closure given to @a cb
  285. */
  286. struct RPS_SamplerRequestHandleSingleInfo *
  287. RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
  288. RPS_sampler_sinlge_info_ready_cb cb,
  289. void *cls);
  290. /**
  291. * Counts how many Samplers currently hold a given PeerID.
  292. *
  293. * @param sampler the sampler to count ids in.
  294. * @param id the PeerID to count.
  295. *
  296. * @return the number of occurrences of id.
  297. */
  298. uint32_t
  299. RPS_sampler_count_id (struct RPS_Sampler *sampler,
  300. const struct GNUNET_PeerIdentity *id);
  301. /**
  302. * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
  303. *
  304. * @param req_handle the handle to the request
  305. */
  306. void
  307. RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
  308. /**
  309. * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
  310. *
  311. * @param req_handle the handle to the request
  312. */
  313. void
  314. RPS_sampler_request_single_info_cancel (
  315. struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle);
  316. /**
  317. * Cleans the sampler.
  318. */
  319. void
  320. RPS_sampler_destroy (struct RPS_Sampler *sampler);
  321. #endif /* RPS_SAMPLER_COMMON_H */
  322. /* end of rps-sampler_common.h */