2 This file is part of GNUnet.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file rps/gnunet-service-rps_sampler.c
21 * @brief sampler implementation
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
29 #include "gnunet-service-rps_sampler.h"
30 #include "gnunet-service-rps_sampler_elem.h"
35 #include "rps-test_util.h"
37 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
40 // multiple 'clients'?
42 // TODO check for overflows
44 // TODO align message structs
46 // hist_size_init, hist_size_max
48 /***********************************************************************
49 * WARNING: This section needs to be reviewed regarding the use of
50 * functions providing (pseudo)randomness!
51 ***********************************************************************/
53 // TODO care about invalid input of the caller (size 0 or less...)
56 * Callback that is called from _get_rand_peer() when the PeerID is ready.
58 * @param cls the closure given alongside this function.
59 * @param id the PeerID that was returned
62 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
63 const struct GNUNET_PeerIdentity *id);
67 * @brief Callback called each time a new peer was put into the sampler
69 * @param cls A possibly given closure
72 (*SamplerNotifyUpdateCB) (void *cls);
75 * @brief Context for a callback. Contains callback and closure.
77 * Meant to be an entry in an DLL.
79 struct SamplerNotifyUpdateCTX
82 * @brief The Callback to call on updates
84 SamplerNotifyUpdateCB notify_cb;
87 * @brief The according closure.
92 * @brief Next element in DLL.
94 struct SamplerNotifyUpdateCTX *next;
97 * @brief Previous element in DLL.
99 struct SamplerNotifyUpdateCTX *prev;
104 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
111 struct GetPeerCls *next;
112 struct GetPeerCls *prev;
115 * The #RPS_SamplerRequestHandle this single request belongs to.
117 struct RPS_SamplerRequestHandle *req_handle;
120 * The task for this function.
122 struct GNUNET_SCHEDULER_Task *get_peer_task;
125 * @brief Context to the given callback.
127 struct SamplerNotifyUpdateCTX *notify_ctx;
132 RPS_sampler_rand_peer_ready_cont cont;
135 * The closure to the callback @e cont
140 * The address of the id to be stored at
142 struct GNUNET_PeerIdentity *id;
147 * Type of function used to differentiate between modified and not modified
151 (*RPS_get_peers_type) (void *cls);
154 * Get one random peer out of the sampled peers.
156 * We might want to reinitialise this sampler after giving the
157 * corrsponding peer to the client.
158 * Only used internally
161 sampler_get_rand_peer (void *cls);
165 * Get one random peer out of the sampled peers.
167 * We might want to reinitialise this sampler after giving the
168 * corrsponding peer to the client.
171 sampler_mod_get_rand_peer (void *cls);
175 * Sampler with its own array of SamplerElements
180 * Number of sampler elements we hold.
182 unsigned int sampler_size;
186 * All sampler elements in one array.
188 struct RPS_SamplerElement **sampler_elements;
191 * Maximum time a round takes
193 * Used in the context of RPS
195 struct GNUNET_TIME_Relative max_round_interval;
198 * Stores the function to return peers. Which one it is depends on whether
199 * the Sampler is the modified one or not.
201 RPS_get_peers_type get_peers;
204 * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
206 struct RPS_SamplerRequestHandle *req_handle_head;
207 struct RPS_SamplerRequestHandle *req_handle_tail;
209 struct SamplerNotifyUpdateCTX *notify_ctx_head;
210 struct SamplerNotifyUpdateCTX *notify_ctx_tail;
213 * File name to log to
220 * Closure to _get_n_rand_peers_ready_cb()
222 struct RPS_SamplerRequestHandle
227 struct RPS_SamplerRequestHandle *next;
228 struct RPS_SamplerRequestHandle *prev;
231 * Number of peers we are waiting for.
236 * Number of peers we currently have.
238 uint32_t cur_num_peers;
241 * Pointer to the array holding the ids.
243 struct GNUNET_PeerIdentity *ids;
246 * Head and tail for the DLL to store the tasks for single requests
248 struct GetPeerCls *gpc_head;
249 struct GetPeerCls *gpc_tail;
254 struct RPS_Sampler *sampler;
257 * Callback to be called when all ids are available.
259 RPS_sampler_n_rand_peers_ready_cb callback;
262 * Closure given to the callback
268 // * Global sampler variable.
270 //struct RPS_Sampler *sampler;
274 * The minimal size for the extended sampler elements.
276 static size_t min_size;
279 * The maximal size the extended sampler elements should grow to.
281 static size_t max_size;
284 * The size the extended sampler elements currently have.
286 //static size_t extra_size;
289 * Inedex to the sampler element that is the next to be returned
291 static uint32_t client_get_index;
295 * @brief Add a callback that will be called when the next peer is inserted
298 * @param sampler The sampler on which update it will be called
299 * @param notify_cb The callback
300 * @param cls Closure given to the callback
302 * @return The context containing callback and closure
304 struct SamplerNotifyUpdateCTX *
305 sampler_notify_on_update (struct RPS_Sampler *sampler,
306 SamplerNotifyUpdateCB notify_cb,
309 struct SamplerNotifyUpdateCTX *notify_ctx;
311 LOG (GNUNET_ERROR_TYPE_DEBUG,
312 "Inserting new context for notification\n");
313 notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX);
314 notify_ctx->notify_cb = notify_cb;
315 notify_ctx->cls = cls;
316 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
317 sampler->notify_ctx_tail,
324 * Callback to _get_rand_peer() used by _get_n_rand_peers().
326 * Checks whether all n peers are available. If they are,
330 check_n_peers_ready (void *cls,
331 const struct GNUNET_PeerIdentity *id)
333 struct RPS_SamplerRequestHandle *req_handle = cls;
336 req_handle->cur_num_peers++;
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
339 req_handle->cur_num_peers, req_handle->num_peers);
341 if (req_handle->num_peers == req_handle->cur_num_peers)
342 { /* All peers are ready -- return those to the client */
343 GNUNET_assert (NULL != req_handle->callback);
345 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "returning %" PRIX32 " peers to the client\n",
347 req_handle->num_peers);
348 req_handle->callback (req_handle->ids, req_handle->num_peers, req_handle->cls);
350 RPS_sampler_request_cancel (req_handle);
356 * Get the size of the sampler.
358 * @param sampler the sampler to return the size of.
359 * @return the size of the sampler
362 RPS_sampler_get_size (struct RPS_Sampler *sampler)
364 return sampler->sampler_size;
369 * Grow or shrink the size of the sampler.
371 * @param sampler the sampler to resize.
372 * @param new_size the new size of the sampler
375 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
377 unsigned int old_size;
380 // TODO check min and max size
382 old_size = sampler->sampler_size;
384 if (old_size > new_size)
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Shrinking sampler %d -> %d\n",
392 to_file (sampler->file_name,
393 "Shrinking sampler %d -> %d",
397 for (i = new_size ; i < old_size ; i++)
399 to_file (sampler->file_name,
402 sampler->sampler_elements[i]->file_name);
403 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
406 GNUNET_array_grow (sampler->sampler_elements,
407 sampler->sampler_size,
409 LOG (GNUNET_ERROR_TYPE_DEBUG,
410 "sampler->sampler_elements now points to %p\n",
411 sampler->sampler_elements);
414 else if (old_size < new_size)
416 LOG (GNUNET_ERROR_TYPE_DEBUG,
417 "Growing sampler %d -> %d\n",
421 to_file (sampler->file_name,
422 "Growing sampler %d -> %d",
426 GNUNET_array_grow (sampler->sampler_elements,
427 sampler->sampler_size,
430 for (i = old_size ; i < new_size ; i++)
431 { /* Add new sampler elements */
432 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
434 to_file (sampler->file_name,
437 sampler->sampler_elements[i]->file_name);
442 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
446 GNUNET_assert (sampler->sampler_size == new_size);
451 * Grow or shrink the size of the sampler.
453 * @param sampler the sampler to resize.
454 * @param new_size the new size of the sampler
457 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
459 GNUNET_assert (0 < new_size);
460 sampler_resize (sampler, new_size);
467 * @param sampler the sampler to empty.
468 * @param new_size the new size of the sampler
471 sampler_empty (struct RPS_Sampler *sampler)
473 sampler_resize (sampler, 0);
478 * Initialise a tuple of sampler elements.
480 * @param init_size the size the sampler is initialised with
481 * @param max_round_interval maximum time a round takes
482 * @return a handle to a sampler that consists of sampler elements.
485 RPS_sampler_init (size_t init_size,
486 struct GNUNET_TIME_Relative max_round_interval)
488 struct RPS_Sampler *sampler;
490 /* Initialise context around extended sampler */
491 min_size = 10; // TODO make input to _samplers_init()
492 max_size = 1000; // TODO make input to _samplers_init()
494 sampler = GNUNET_new (struct RPS_Sampler);
497 sampler->file_name = create_file ("sampler-");
499 LOG (GNUNET_ERROR_TYPE_DEBUG,
500 "Initialised sampler %s\n",
504 sampler->max_round_interval = max_round_interval;
505 sampler->get_peers = sampler_get_rand_peer;
506 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
507 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
508 RPS_sampler_resize (sampler, init_size);
510 client_get_index = 0;
512 //GNUNET_assert (init_size == sampler->sampler_size);
517 * Initialise a modified tuple of sampler elements.
519 * @param init_size the size the sampler is initialised with
520 * @param max_round_interval maximum time a round takes
521 * @return a handle to a sampler that consists of sampler elements.
524 RPS_sampler_mod_init (size_t init_size,
525 struct GNUNET_TIME_Relative max_round_interval)
527 struct RPS_Sampler *sampler;
529 sampler = RPS_sampler_init (init_size, max_round_interval);
530 sampler->get_peers = sampler_mod_get_rand_peer;
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "Initialised modified sampler %s\n",
536 to_file (sampler->file_name,
537 "This is a modified sampler");
545 * @brief Notify about update of the sampler.
547 * Call the callbacks that are waiting for notification on updates to the
550 * @param sampler The sampler the updates are waiting for
553 notify_update (struct RPS_Sampler *sampler)
555 struct SamplerNotifyUpdateCTX *tmp_notify_head;
556 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
558 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 "Calling callbacks waiting for update notification.\n");
560 tmp_notify_head = sampler->notify_ctx_head;
561 tmp_notify_tail = sampler->notify_ctx_tail;
562 sampler->notify_ctx_head = NULL;
563 sampler->notify_ctx_tail = NULL;
564 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
565 NULL != tmp_notify_head;
566 notify_iter = tmp_notify_head)
568 GNUNET_assert (NULL != notify_iter->notify_cb);
569 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
572 notify_iter->notify_cb (notify_iter->cls);
573 GNUNET_free (notify_iter);
579 * Update every sampler element of this sampler with given peer
581 * @param sampler the sampler to update.
582 * @param id the PeerID that is put in the sampler
585 RPS_sampler_update (struct RPS_Sampler *sampler,
586 const struct GNUNET_PeerIdentity *id)
588 to_file (sampler->file_name,
590 GNUNET_i2s_full (id));
592 for (uint32_t i = 0; i < sampler->sampler_size; i++)
594 RPS_sampler_elem_next (sampler->sampler_elements[i],
597 notify_update (sampler);
602 * Reinitialise all previously initialised sampler elements with the given value.
604 * Used to get rid of a PeerID.
606 * @param sampler the sampler to reinitialise a sampler element in.
607 * @param id the id of the sampler elements to update.
610 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
611 const struct GNUNET_PeerIdentity *id)
615 for (i = 0; i < sampler->sampler_size; i++)
617 if (0 == GNUNET_CRYPTO_cmp_peer_identity(id,
618 &(sampler->sampler_elements[i]->peer_id)) )
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
621 to_file (sampler->sampler_elements[i]->file_name,
623 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
630 * Get one random peer out of the sampled peers.
632 * We might want to reinitialise this sampler after giving the
633 * corrsponding peer to the client.
634 * Only used internally
637 sampler_get_rand_peer (void *cls)
639 struct GetPeerCls *gpc = cls;
641 struct RPS_Sampler *sampler;
643 gpc->get_peer_task = NULL;
644 gpc->notify_ctx = NULL;
645 sampler = gpc->req_handle->sampler;
648 * Choose the r_index of the peer we want to return
649 * at random from the interval of the gossip list
651 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
652 sampler->sampler_size);
654 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
656 //LOG (GNUNET_ERROR_TYPE_DEBUG,
657 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
660 sampler_notify_on_update (sampler,
661 &sampler_mod_get_rand_peer,
666 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
667 gpc->req_handle->gpc_tail,
669 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
670 gpc->cont (gpc->cont_cls, gpc->id);
677 * Get one random peer out of the sampled peers.
679 * This reinitialises the queried sampler element.
682 sampler_mod_get_rand_peer (void *cls)
684 struct GetPeerCls *gpc = cls;
685 struct RPS_SamplerElement *s_elem;
686 struct GNUNET_TIME_Relative last_request_diff;
687 struct RPS_Sampler *sampler;
689 gpc->get_peer_task = NULL;
690 gpc->notify_ctx = NULL;
691 sampler = gpc->req_handle->sampler;
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
695 /* Cycle the #client_get_index one step further */
696 client_get_index = (client_get_index + 1) % sampler->sampler_size;
698 s_elem = sampler->sampler_elements[client_get_index];
699 *gpc->id = s_elem->peer_id;
700 GNUNET_assert (NULL != s_elem);
702 if (EMPTY == s_elem->is_empty)
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Sampler_mod element empty, rescheduling.\n");
706 GNUNET_assert (NULL == gpc->notify_ctx);
708 sampler_notify_on_update (sampler,
709 &sampler_mod_get_rand_peer,
714 /* Check whether we may use this sampler to give it back to the client */
715 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
717 // TODO remove this condition at least for the client sampler
719 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
720 GNUNET_TIME_absolute_get ());
721 /* We're not going to give it back now if it was
722 * already requested by a client this round */
723 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
727 ///* How many time remains untile the next round has started? */
728 //inv_last_request_diff =
729 // GNUNET_TIME_absolute_get_difference (last_request_diff,
730 // sampler->max_round_interval);
731 // add a little delay
732 /* Schedule it one round later */
733 GNUNET_assert (NULL == gpc->notify_ctx);
735 sampler_notify_on_update (sampler,
736 &sampler_mod_get_rand_peer,
741 if (2 > s_elem->num_peers)
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "This s_elem saw less than two peers -- scheduling for later\n");
745 GNUNET_assert (NULL == gpc->notify_ctx);
747 sampler_notify_on_update (sampler,
748 &sampler_mod_get_rand_peer,
752 /* More reasons to wait could be added here */
754 // GNUNET_STATISTICS_set (stats,
755 // "# client sampler element input",
756 // s_elem->num_peers,
758 // GNUNET_STATISTICS_set (stats,
759 // "# client sampler element change",
760 // s_elem->num_change,
763 RPS_sampler_elem_reinit (s_elem);
764 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
766 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
767 gpc->req_handle->gpc_tail,
769 gpc->cont (gpc->cont_cls, gpc->id);
775 * Get n random peers out of the sampled peers.
777 * We might want to reinitialise this sampler after giving the
778 * corrsponding peer to the client.
779 * Random with or without consumption?
781 * @param sampler the sampler to get peers from.
782 * @param cb callback that will be called once the ids are ready.
783 * @param cls closure given to @a cb
784 * @param for_client #GNUNET_YES if result is used for client,
785 * #GNUNET_NO if used internally
786 * @param num_peers the number of peers requested
788 struct RPS_SamplerRequestHandle *
789 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
791 RPS_sampler_n_rand_peers_ready_cb cb,
795 struct RPS_SamplerRequestHandle *req_handle;
796 struct GetPeerCls *gpc;
798 GNUNET_assert (0 != sampler->sampler_size);
802 // TODO check if we have too much (distinct) sampled peers
803 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
804 req_handle->num_peers = num_peers;
805 req_handle->cur_num_peers = 0;
806 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
807 req_handle->sampler = sampler;
808 req_handle->callback = cb;
809 req_handle->cls = cls;
810 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
811 sampler->req_handle_tail,
814 LOG (GNUNET_ERROR_TYPE_DEBUG,
815 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
817 for (i = 0; i < num_peers; i++)
819 gpc = GNUNET_new (struct GetPeerCls);
820 gpc->req_handle = req_handle;
821 gpc->cont = check_n_peers_ready;
822 gpc->cont_cls = req_handle;
823 gpc->id = &req_handle->ids[i];
825 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
826 req_handle->gpc_tail,
828 // maybe add a little delay
829 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
836 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
838 * @param req_handle the handle to the request
841 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
843 struct GetPeerCls *i;
845 while (NULL != (i = req_handle->gpc_head) )
847 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
848 req_handle->gpc_tail,
850 if (NULL != i->get_peer_task)
852 GNUNET_SCHEDULER_cancel (i->get_peer_task);
854 if (NULL != i->notify_ctx)
856 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
857 req_handle->sampler->notify_ctx_tail,
859 GNUNET_free (i->notify_ctx);
863 GNUNET_free (req_handle->ids);
864 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
865 req_handle->sampler->req_handle_tail,
867 GNUNET_free (req_handle);
872 * Counts how many Samplers currently hold a given PeerID.
874 * @param sampler the sampler to count ids in.
875 * @param id the PeerID to count.
877 * @return the number of occurrences of id.
880 RPS_sampler_count_id (struct RPS_Sampler *sampler,
881 const struct GNUNET_PeerIdentity *id)
887 for ( i = 0 ; i < sampler->sampler_size ; i++ )
889 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
890 && EMPTY != sampler->sampler_elements[i]->is_empty)
898 * Cleans the sampler.
901 RPS_sampler_destroy (struct RPS_Sampler *sampler)
903 if (NULL != sampler->req_handle_head)
905 LOG (GNUNET_ERROR_TYPE_WARNING,
906 "There are still pending requests. Going to remove them.\n");
907 while (NULL != sampler->req_handle_head)
909 RPS_sampler_request_cancel (sampler->req_handle_head);
912 sampler_empty (sampler);
913 GNUNET_free (sampler);
916 /* end of gnunet-service-rps.c */