2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
30 #include "gnunet-service-rps_sampler.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...)
62 * A sampler element sampling one PeerID at a time.
64 struct RPS_SamplerElement
67 * Min-wise linear permutation used by this sampler.
69 * This is an key later used by a hmac.
71 struct GNUNET_CRYPTO_AuthKey auth_key;
74 * The PeerID this sampler currently samples.
76 struct GNUNET_PeerIdentity peer_id;
79 * The according hash value of this PeerID.
81 struct GNUNET_HashCode peer_id_hash;
85 * Time of last request.
87 struct GNUNET_TIME_Absolute last_client_request;
90 * Flag that indicates that we are not holding a valid PeerID right now.
92 enum RPS_SamplerEmpty is_empty;
97 struct GNUNET_TIME_Absolute birth;
100 * How many times a PeerID was put in this sampler.
105 * How many times this sampler changed the peer_id.
110 * The file name this sampler element should log to
118 * Callback that is called from _get_rand_peer() when the PeerID is ready.
120 * @param cls the closure given alongside this function.
121 * @param id the PeerID that was returned
124 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
125 const struct GNUNET_PeerIdentity *id);
129 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
136 struct GetPeerCls *next;
141 struct GetPeerCls *prev;
144 * The sampler this function operates on.
146 struct RPS_Sampler *sampler;
149 * The task for this function.
151 struct GNUNET_SCHEDULER_Task *get_peer_task;
156 RPS_sampler_rand_peer_ready_cont cont;
159 * The closure to the callback @e cont
164 * The address of the id to be stored at
166 struct GNUNET_PeerIdentity *id;
171 * Type of function used to differentiate between modified and not modified
175 (*RPS_get_peers_type) (void *cls,
176 const struct GNUNET_SCHEDULER_TaskContext *tc);
179 * Get one random peer out of the sampled peers.
181 * We might want to reinitialise this sampler after giving the
182 * corrsponding peer to the client.
183 * Only used internally
186 sampler_get_rand_peer (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc);
190 * Get one random peer out of the sampled peers.
192 * We might want to reinitialise this sampler after giving the
193 * corrsponding peer to the client.
196 sampler_mod_get_rand_peer (void *cls,
197 const struct GNUNET_SCHEDULER_TaskContext *tc);
201 * Sampler with its own array of SamplerElements
206 * Number of sampler elements we hold.
208 unsigned int sampler_size;
212 * All sampler elements in one array.
214 struct RPS_SamplerElement **sampler_elements;
217 * Number of sampler elements trash can holds.
219 unsigned int trash_can_size;
222 * Trash can for old sampler elements.
223 * We need this to evaluate the sampler.
224 * TODO remove after evaluation
225 * and undo changes in
230 * sampler_reinitialise_by_value
233 struct RPS_SamplerElement **trash_can;
236 * Maximum time a round takes
238 * Used in the context of RPS
240 struct GNUNET_TIME_Relative max_round_interval;
243 * Stores the function to return peers. Which one it is depends on whether
244 * the Sampler is the modified one or not.
246 RPS_get_peers_type get_peers;
249 * Head for the DLL to store the closures to pending requests.
251 struct GetPeerCls *gpc_head;
254 * Tail for the DLL to store the closures to pending requests.
256 struct GetPeerCls *gpc_tail;
260 * File name to log to
267 * Closure to _get_n_rand_peers_ready_cb()
269 struct NRandPeersReadyCls
272 * Number of peers we are waiting for.
277 * Number of peers we currently have.
279 uint32_t cur_num_peers;
282 * Pointer to the array holding the ids.
284 struct GNUNET_PeerIdentity *ids;
287 * Callback to be called when all ids are available.
289 RPS_sampler_n_rand_peers_ready_cb callback;
292 * Closure given to the callback
298 // * Global sampler variable.
300 //struct RPS_Sampler *sampler;
304 * The minimal size for the extended sampler elements.
306 static size_t min_size;
309 * The maximal size the extended sampler elements should grow to.
311 static size_t max_size;
314 * The size the extended sampler elements currently have.
316 //static size_t extra_size;
319 * Inedex to the sampler element that is the next to be returned
321 static uint32_t client_get_index;
325 * Callback to _get_rand_peer() used by _get_n_rand_peers().
327 * Checks whether all n peers are available. If they are,
331 check_n_peers_ready (void *cls,
332 const struct GNUNET_PeerIdentity *id)
334 struct NRandPeersReadyCls *n_peers_cls = cls;
336 n_peers_cls->cur_num_peers++;
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
339 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
341 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
342 { /* All peers are ready -- return those to the client */
343 GNUNET_assert (NULL != n_peers_cls->callback);
345 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "returning %" PRIX32 " peers to the client\n",
347 n_peers_cls->num_peers);
348 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
350 GNUNET_free (n_peers_cls);
356 * Reinitialise a previously initialised sampler element.
358 * @param sampler pointer to the memory that keeps the value.
361 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
363 sampler_el->is_empty = EMPTY;
365 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
366 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
367 &(sampler_el->auth_key.key),
368 GNUNET_CRYPTO_HASH_LENGTH);
371 /* Create a file(-name) to store internals to */
373 name_buf = auth_key_to_string (sampler_el->auth_key);
375 sampler_el->file_name = create_file (name_buf);
376 GNUNET_free (name_buf);
379 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
381 sampler_el->birth = GNUNET_TIME_absolute_get ();
382 sampler_el->num_peers = 0;
383 sampler_el->num_change = 0;
388 * (Re)Initialise given Sampler with random min-wise independent function.
390 * In this implementation this means choosing an auth_key for later use in
393 * @return a newly created RPS_SamplerElement which currently holds no id.
395 struct RPS_SamplerElement *
396 RPS_sampler_elem_create (void)
398 struct RPS_SamplerElement *s;
400 s = GNUNET_new (struct RPS_SamplerElement);
402 RPS_sampler_elem_reinit (s);
409 * Input an PeerID into the given sampler element.
411 * @param sampler the sampler the @a s_elem belongs to.
415 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
416 struct RPS_Sampler *sampler, /* TODO remove? */
417 const struct GNUNET_PeerIdentity *other)
419 struct GNUNET_HashCode other_hash;
423 to_file (s_elem->file_name,
425 GNUNET_i2s_full (other));
427 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
429 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
431 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
432 GNUNET_i2s (&(s_elem->peer_id)));
436 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
438 sizeof(struct GNUNET_PeerIdentity),
441 if (EMPTY == s_elem->is_empty)
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Got PeerID %s; Simply accepting (was empty previously).\n",
446 s_elem->peer_id = *other;
447 s_elem->peer_id_hash = other_hash;
449 s_elem->num_change++;
451 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
453 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
455 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
456 GNUNET_i2s (&s_elem->peer_id));
457 s_elem->peer_id = *other;
458 s_elem->peer_id_hash = other_hash;
460 s_elem->num_change++;
464 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
466 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
467 GNUNET_i2s (&s_elem->peer_id));
470 s_elem->is_empty = NOT_EMPTY;
472 to_file (s_elem->file_name,
474 GNUNET_i2s_full (&s_elem->peer_id));
479 * Get the size of the sampler.
481 * @param sampler the sampler to return the size of.
482 * @return the size of the sampler
485 RPS_sampler_get_size (struct RPS_Sampler *sampler)
487 return sampler->sampler_size;
492 * Grow or shrink the size of the sampler.
494 * @param sampler the sampler to resize.
495 * @param new_size the new size of the sampler
498 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
500 unsigned int old_size;
503 // TODO check min and max size
505 old_size = sampler->sampler_size;
507 if (old_size > new_size)
510 LOG (GNUNET_ERROR_TYPE_DEBUG,
511 "Shrinking sampler %d -> %d\n",
515 to_file (sampler->file_name,
516 "Shrinking sampler %d -> %d",
520 /* TODO Temporary store those to properly call the removeCB on those later? */
521 GNUNET_array_grow (sampler->trash_can,
522 sampler->trash_can_size,
523 old_size - new_size);
524 for (i = new_size ; i < old_size ; i++)
526 to_file (sampler->file_name,
529 sampler->sampler_elements[i]->file_name);
530 to_file (sampler->sampler_elements[i]->file_name,
532 sampler->trash_can[i - new_size] = sampler->sampler_elements[i];
535 GNUNET_array_grow (sampler->sampler_elements,
536 sampler->sampler_size,
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "sampler->sampler_elements now points to %p\n",
540 sampler->sampler_elements);
543 else if (old_size < new_size)
545 LOG (GNUNET_ERROR_TYPE_DEBUG,
546 "Growing sampler %d -> %d\n",
550 to_file (sampler->file_name,
551 "Growing sampler %d -> %d",
555 GNUNET_array_grow (sampler->sampler_elements,
556 sampler->sampler_size,
559 for (i = old_size ; i < new_size ; i++)
560 { /* Add new sampler elements */
561 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
563 to_file (sampler->file_name,
566 sampler->sampler_elements[i]->file_name);
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
575 GNUNET_assert (sampler->sampler_size == new_size);
580 * Grow or shrink the size of the sampler.
582 * @param sampler the sampler to resize.
583 * @param new_size the new size of the sampler
586 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
588 GNUNET_assert (0 < new_size);
589 sampler_resize (sampler, new_size);
596 * @param sampler the sampler to empty.
597 * @param new_size the new size of the sampler
600 sampler_empty (struct RPS_Sampler *sampler)
602 sampler_resize (sampler, 0);
607 * Initialise a tuple of sampler elements.
609 * @param init_size the size the sampler is initialised with
610 * @param max_round_interval maximum time a round takes
611 * @return a handle to a sampler that consists of sampler elements.
614 RPS_sampler_init (size_t init_size,
615 struct GNUNET_TIME_Relative max_round_interval)
617 struct RPS_Sampler *sampler;
619 /* Initialise context around extended sampler */
620 min_size = 10; // TODO make input to _samplers_init()
621 max_size = 1000; // TODO make input to _samplers_init()
623 sampler = GNUNET_new (struct RPS_Sampler);
626 sampler->file_name = create_file ("sampler-");
628 LOG (GNUNET_ERROR_TYPE_DEBUG,
629 "Initialised sampler %s\n",
633 sampler->sampler_size = 0;
634 sampler->sampler_elements = NULL;
635 sampler->trash_can_size = 0;
636 sampler->trash_can = NULL;
637 sampler->max_round_interval = max_round_interval;
638 sampler->get_peers = sampler_get_rand_peer;
639 sampler->gpc_head = NULL;
640 sampler->gpc_tail = NULL;
641 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
642 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
643 RPS_sampler_resize (sampler, init_size);
645 client_get_index = 0;
647 //GNUNET_assert (init_size == sampler->sampler_size);
652 * Initialise a modified tuple of sampler elements.
654 * @param init_size the size the sampler is initialised with
655 * @param max_round_interval maximum time a round takes
656 * @return a handle to a sampler that consists of sampler elements.
659 RPS_sampler_mod_init (size_t init_size,
660 struct GNUNET_TIME_Relative max_round_interval)
662 struct RPS_Sampler *sampler;
664 sampler = RPS_sampler_init (init_size, max_round_interval);
665 sampler->get_peers = sampler_mod_get_rand_peer;
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Initialised modified sampler %s\n",
670 to_file (sampler->file_name,
671 "This is a modified sampler");
678 * A fuction to update every sampler in the given list
680 * @param sampler the sampler to update.
681 * @param id the PeerID that is put in the sampler
684 RPS_sampler_update (struct RPS_Sampler *sampler,
685 const struct GNUNET_PeerIdentity *id)
689 to_file (sampler->file_name,
691 GNUNET_i2s_full (id));
693 for (i = 0 ; i < sampler->sampler_size ; i++)
695 RPS_sampler_elem_next (sampler->sampler_elements[i],
700 for (i = 0 ; i < sampler->trash_can_size ; i++)
702 RPS_sampler_elem_next (sampler->trash_can[i],
710 * Reinitialise all previously initialised sampler elements with the given value.
712 * Used to get rid of a PeerID.
714 * @param sampler the sampler to reinitialise a sampler element in.
715 * @param id the id of the sampler elements to update.
718 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
719 const struct GNUNET_PeerIdentity *id)
722 struct RPS_SamplerElement *trash_entry;
724 for ( i = 0 ; i < sampler->sampler_size ; i++ )
726 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
728 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
729 trash_entry = GNUNET_new (struct RPS_SamplerElement);
730 *trash_entry = *(sampler->sampler_elements[i]);
731 GNUNET_array_append (sampler->trash_can,
732 sampler->trash_can_size,
734 to_file (trash_entry->file_name,
736 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
743 * Get one random peer out of the sampled peers.
745 * We might want to reinitialise this sampler after giving the
746 * corrsponding peer to the client.
747 * Only used internally
750 sampler_get_rand_peer (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc)
753 struct GetPeerCls *gpc = cls;
756 gpc->get_peer_task = NULL;
757 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
761 * Choose the r_index of the peer we want to return
762 * at random from the interval of the gossip list
764 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
765 gpc->sampler->sampler_size);
767 if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty)
769 //LOG (GNUNET_ERROR_TYPE_DEBUG,
770 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
772 /* FIXME no active wait - get notified, when new id arrives?
773 * Might also be a freshly emptied one. Others might still contain ids.
777 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
778 GNUNET_TIME_UNIT_SECONDS, 0.1),
779 &sampler_get_rand_peer,
784 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
786 gpc->cont (gpc->cont_cls, gpc->id);
788 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
789 gpc->sampler->gpc_tail,
797 * Get one random peer out of the sampled peers.
799 * We might want to reinitialise this sampler after giving the
800 * corrsponding peer to the client.
803 sampler_mod_get_rand_peer (void *cls,
804 const struct GNUNET_SCHEDULER_TaskContext *tc)
806 struct GetPeerCls *gpc = cls;
807 struct RPS_SamplerElement *s_elem;
808 struct GNUNET_TIME_Relative last_request_diff;
810 gpc->get_peer_task = NULL;
811 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
816 /* Cycle the #client_get_index one step further */
817 client_get_index = (client_get_index + 1) % gpc->sampler->sampler_size;
819 s_elem = gpc->sampler->sampler_elements[client_get_index];
820 *gpc->id = s_elem->peer_id;
824 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sampler_mod element empty, rescheduling.\n");
825 GNUNET_assert (NULL == gpc->get_peer_task);
827 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
828 &sampler_mod_get_rand_peer,
833 /* Check whether we may use this sampler to give it back to the client */
834 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
837 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
838 GNUNET_TIME_absolute_get ());
839 /* We're not going to give it back now if it was
840 * already requested by a client this round */
841 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
843 LOG (GNUNET_ERROR_TYPE_DEBUG,
844 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
845 ///* How many time remains untile the next round has started? */
846 //inv_last_request_diff =
847 // GNUNET_TIME_absolute_get_difference (last_request_diff,
848 // sampler->max_round_interval);
849 // add a little delay
850 /* Schedule it one round later */
851 GNUNET_assert (NULL == gpc->get_peer_task);
853 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
854 &sampler_mod_get_rand_peer,
858 // TODO add other reasons to wait here
861 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
863 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
864 gpc->sampler->gpc_tail,
866 gpc->cont (gpc->cont_cls, gpc->id);
872 * Get n random peers out of the sampled peers.
874 * We might want to reinitialise this sampler after giving the
875 * corrsponding peer to the client.
876 * Random with or without consumption?
878 * @param sampler the sampler to get peers from.
879 * @param cb callback that will be called once the ids are ready.
880 * @param cls closure given to @a cb
881 * @param for_client #GNUNET_YES if result is used for client,
882 * #GNUNET_NO if used internally
883 * @param num_peers the number of peers requested
886 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
887 RPS_sampler_n_rand_peers_ready_cb cb,
888 void *cls, uint32_t num_peers)
890 GNUNET_assert (0 != sampler->sampler_size);
894 // TODO check if we have too much (distinct) sampled peers
896 struct NRandPeersReadyCls *cb_cls;
897 struct GetPeerCls *gpc;
899 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
900 cb_cls->num_peers = num_peers;
901 cb_cls->cur_num_peers = 0;
902 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
903 cb_cls->callback = cb;
906 LOG (GNUNET_ERROR_TYPE_DEBUG,
907 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
909 for (i = 0 ; i < num_peers ; i++)
911 gpc = GNUNET_new (struct GetPeerCls);
912 gpc->sampler = sampler;
913 gpc->cont = check_n_peers_ready;
914 gpc->cont_cls = cb_cls;
915 gpc->id = &cb_cls->ids[i];
917 // maybe add a little delay
918 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
920 GNUNET_CONTAINER_DLL_insert (sampler->gpc_head,
928 * Counts how many Samplers currently hold a given PeerID.
930 * @param sampler the sampler to count ids in.
931 * @param id the PeerID to count.
933 * @return the number of occurrences of id.
936 RPS_sampler_count_id (struct RPS_Sampler *sampler,
937 const struct GNUNET_PeerIdentity *id)
943 for ( i = 0 ; i < sampler->sampler_size ; i++ )
945 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
946 && EMPTY != sampler->sampler_elements[i]->is_empty)
954 * Cleans the sampler.
957 RPS_sampler_destroy (struct RPS_Sampler *sampler)
959 struct GetPeerCls *i;
961 for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head)
963 GNUNET_CONTAINER_DLL_remove (sampler->gpc_head,
966 GNUNET_SCHEDULER_cancel (i->get_peer_task);
970 sampler_empty (sampler);
971 GNUNET_free (sampler);
974 /* end of gnunet-service-rps.c */