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 if (NULL != sampler->notify_ctx_head)
318 for (struct SamplerNotifyUpdateCTX *notify_iter = sampler->notify_ctx_head;
319 NULL != notify_iter->next;
320 notify_iter = notify_iter->next)
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
327 sampler->notify_ctx_tail,
329 for (struct SamplerNotifyUpdateCTX *notify_iter = sampler->notify_ctx_head;
331 notify_iter = notify_iter->next)
333 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 * Callback to _get_rand_peer() used by _get_n_rand_peers().
343 * Checks whether all n peers are available. If they are,
347 check_n_peers_ready (void *cls,
348 const struct GNUNET_PeerIdentity *id)
350 struct RPS_SamplerRequestHandle *req_handle = cls;
353 req_handle->cur_num_peers++;
354 LOG (GNUNET_ERROR_TYPE_DEBUG,
355 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
356 req_handle->cur_num_peers, req_handle->num_peers);
358 if (req_handle->num_peers == req_handle->cur_num_peers)
359 { /* All peers are ready -- return those to the client */
360 GNUNET_assert (NULL != req_handle->callback);
362 LOG (GNUNET_ERROR_TYPE_DEBUG,
363 "returning %" PRIX32 " peers to the client\n",
364 req_handle->num_peers);
365 req_handle->callback (req_handle->ids, req_handle->num_peers, req_handle->cls);
367 RPS_sampler_request_cancel (req_handle);
373 * Get the size of the sampler.
375 * @param sampler the sampler to return the size of.
376 * @return the size of the sampler
379 RPS_sampler_get_size (struct RPS_Sampler *sampler)
381 return sampler->sampler_size;
386 * Grow or shrink the size of the sampler.
388 * @param sampler the sampler to resize.
389 * @param new_size the new size of the sampler
392 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
394 unsigned int old_size;
397 // TODO check min and max size
399 old_size = sampler->sampler_size;
401 if (old_size > new_size)
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "Shrinking sampler %d -> %d\n",
409 to_file (sampler->file_name,
410 "Shrinking sampler %d -> %d",
414 for (i = new_size ; i < old_size ; i++)
416 to_file (sampler->file_name,
419 sampler->sampler_elements[i]->file_name);
420 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
423 GNUNET_array_grow (sampler->sampler_elements,
424 sampler->sampler_size,
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "sampler->sampler_elements now points to %p\n",
428 sampler->sampler_elements);
431 else if (old_size < new_size)
433 LOG (GNUNET_ERROR_TYPE_DEBUG,
434 "Growing sampler %d -> %d\n",
438 to_file (sampler->file_name,
439 "Growing sampler %d -> %d",
443 GNUNET_array_grow (sampler->sampler_elements,
444 sampler->sampler_size,
447 for (i = old_size ; i < new_size ; i++)
448 { /* Add new sampler elements */
449 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
451 to_file (sampler->file_name,
454 sampler->sampler_elements[i]->file_name);
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
463 GNUNET_assert (sampler->sampler_size == new_size);
468 * Grow or shrink the size of the sampler.
470 * @param sampler the sampler to resize.
471 * @param new_size the new size of the sampler
474 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
476 GNUNET_assert (0 < new_size);
477 sampler_resize (sampler, new_size);
484 * @param sampler the sampler to empty.
485 * @param new_size the new size of the sampler
488 sampler_empty (struct RPS_Sampler *sampler)
490 sampler_resize (sampler, 0);
495 * Initialise a tuple of sampler elements.
497 * @param init_size the size the sampler is initialised with
498 * @param max_round_interval maximum time a round takes
499 * @return a handle to a sampler that consists of sampler elements.
502 RPS_sampler_init (size_t init_size,
503 struct GNUNET_TIME_Relative max_round_interval)
505 struct RPS_Sampler *sampler;
507 /* Initialise context around extended sampler */
508 min_size = 10; // TODO make input to _samplers_init()
509 max_size = 1000; // TODO make input to _samplers_init()
511 sampler = GNUNET_new (struct RPS_Sampler);
514 sampler->file_name = create_file ("sampler-");
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "Initialised sampler %s\n",
521 sampler->max_round_interval = max_round_interval;
522 sampler->get_peers = sampler_get_rand_peer;
523 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
524 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
525 RPS_sampler_resize (sampler, init_size);
527 client_get_index = 0;
529 //GNUNET_assert (init_size == sampler->sampler_size);
534 * Initialise a modified tuple of sampler elements.
536 * @param init_size the size the sampler is initialised with
537 * @param max_round_interval maximum time a round takes
538 * @return a handle to a sampler that consists of sampler elements.
541 RPS_sampler_mod_init (size_t init_size,
542 struct GNUNET_TIME_Relative max_round_interval)
544 struct RPS_Sampler *sampler;
546 sampler = RPS_sampler_init (init_size, max_round_interval);
547 sampler->get_peers = sampler_mod_get_rand_peer;
550 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Initialised modified sampler %s\n",
553 to_file (sampler->file_name,
554 "This is a modified sampler");
562 * Update every sampler element of this sampler with given peer
564 * @param sampler the sampler to update.
565 * @param id the PeerID that is put in the sampler
568 RPS_sampler_update (struct RPS_Sampler *sampler,
569 const struct GNUNET_PeerIdentity *id)
571 struct SamplerNotifyUpdateCTX *tmp_notify_head;
572 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
574 to_file (sampler->file_name,
576 GNUNET_i2s_full (id));
578 for (uint32_t i = 0; i < sampler->sampler_size; i++)
580 RPS_sampler_elem_next (sampler->sampler_elements[i],
583 tmp_notify_head = sampler->notify_ctx_head;
584 tmp_notify_tail = sampler->notify_ctx_tail;
585 sampler->notify_ctx_head = NULL;
586 sampler->notify_ctx_tail = NULL;
587 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
588 NULL != tmp_notify_head;
589 notify_iter = tmp_notify_head)
591 GNUNET_assert (NULL != notify_iter->notify_cb);
592 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
595 notify_iter->notify_cb (notify_iter->cls);
596 GNUNET_free (notify_iter);
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)
718 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
719 GNUNET_TIME_absolute_get ());
720 /* We're not going to give it back now if it was
721 * already requested by a client this round */
722 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
724 LOG (GNUNET_ERROR_TYPE_DEBUG,
725 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
726 ///* How many time remains untile the next round has started? */
727 //inv_last_request_diff =
728 // GNUNET_TIME_absolute_get_difference (last_request_diff,
729 // sampler->max_round_interval);
730 // add a little delay
731 /* Schedule it one round later */
732 GNUNET_assert (NULL == gpc->notify_ctx);
734 sampler_notify_on_update (sampler,
735 &sampler_mod_get_rand_peer,
740 if (2 > s_elem->num_peers)
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "This s_elem saw less than two peers -- scheduling for later\n");
744 GNUNET_assert (NULL == gpc->notify_ctx);
746 sampler_notify_on_update (sampler,
747 &sampler_mod_get_rand_peer,
751 /* More reasons to wait could be added here */
753 // GNUNET_STATISTICS_set (stats,
754 // "# client sampler element input",
755 // s_elem->num_peers,
757 // GNUNET_STATISTICS_set (stats,
758 // "# client sampler element change",
759 // s_elem->num_change,
762 RPS_sampler_elem_reinit (s_elem);
763 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
765 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
766 gpc->req_handle->gpc_tail,
768 gpc->cont (gpc->cont_cls, gpc->id);
774 * Get n random peers out of the sampled peers.
776 * We might want to reinitialise this sampler after giving the
777 * corrsponding peer to the client.
778 * Random with or without consumption?
780 * @param sampler the sampler to get peers from.
781 * @param cb callback that will be called once the ids are ready.
782 * @param cls closure given to @a cb
783 * @param for_client #GNUNET_YES if result is used for client,
784 * #GNUNET_NO if used internally
785 * @param num_peers the number of peers requested
787 struct RPS_SamplerRequestHandle *
788 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
790 RPS_sampler_n_rand_peers_ready_cb cb,
794 struct RPS_SamplerRequestHandle *req_handle;
795 struct GetPeerCls *gpc;
797 GNUNET_assert (0 != sampler->sampler_size);
801 // TODO check if we have too much (distinct) sampled peers
802 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
803 req_handle->num_peers = num_peers;
804 req_handle->cur_num_peers = 0;
805 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
806 req_handle->sampler = sampler;
807 req_handle->callback = cb;
808 req_handle->cls = cls;
809 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
810 sampler->req_handle_tail,
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
816 for (i = 0; i < num_peers; i++)
818 gpc = GNUNET_new (struct GetPeerCls);
819 gpc->req_handle = req_handle;
820 gpc->cont = check_n_peers_ready;
821 gpc->cont_cls = req_handle;
822 gpc->id = &req_handle->ids[i];
824 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
825 req_handle->gpc_tail,
827 // maybe add a little delay
828 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
835 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
837 * @param req_handle the handle to the request
840 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
842 struct GetPeerCls *i;
844 while (NULL != (i = req_handle->gpc_head) )
846 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
847 req_handle->gpc_tail,
849 if (NULL != i->get_peer_task)
851 GNUNET_SCHEDULER_cancel (i->get_peer_task);
853 if (NULL != i->notify_ctx)
855 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
856 req_handle->sampler->notify_ctx_tail,
858 GNUNET_free (i->notify_ctx);
862 GNUNET_free (req_handle->ids);
863 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
864 req_handle->sampler->req_handle_tail,
866 GNUNET_free (req_handle);
871 * Counts how many Samplers currently hold a given PeerID.
873 * @param sampler the sampler to count ids in.
874 * @param id the PeerID to count.
876 * @return the number of occurrences of id.
879 RPS_sampler_count_id (struct RPS_Sampler *sampler,
880 const struct GNUNET_PeerIdentity *id)
886 for ( i = 0 ; i < sampler->sampler_size ; i++ )
888 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
889 && EMPTY != sampler->sampler_elements[i]->is_empty)
897 * Cleans the sampler.
900 RPS_sampler_destroy (struct RPS_Sampler *sampler)
902 if (NULL != sampler->req_handle_head)
904 LOG (GNUNET_ERROR_TYPE_WARNING,
905 "There are still pending requests. Going to remove them.\n");
906 while (NULL != sampler->req_handle_head)
908 RPS_sampler_request_cancel (sampler->req_handle_head);
911 sampler_empty (sampler);
912 GNUNET_free (sampler);
915 /* end of gnunet-service-rps.c */