2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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 fs/gnunet-service-fs_pe.c
23 * @brief API to manage query plan
24 * @author Christian Grothoff
27 #include "gnunet-service-fs.h"
28 #include "gnunet-service-fs_cp.h"
29 #include "gnunet-service-fs_pe.h"
30 #include "gnunet-service-fs_pr.h"
33 * Collect an instane number of statistics? May cause excessive IPC.
35 #define INSANE_STATISTICS GNUNET_NO
38 * List of GSF_PendingRequests this request plan
41 struct PendingRequestList;
44 * Transmission plan for a peer.
50 * M:N binding of plans to pending requests.
51 * Each pending request can be in a number of plans,
52 * and each plan can have a number of pending requests.
53 * Objects of this type indicate a mapping of a plan to
54 * a particular pending request.
56 * The corresponding head and tail of the "PE" MDLL
57 * are stored in a `struct GSF_RequestPlan`. (We need
58 * to be able to lookup all pending requests corresponding
59 * to a given plan entry.)
61 * Similarly head and tail of the "PR" MDLL are stored
62 * with the `struct GSF_PendingRequest`. (We need
63 * to be able to lookup all plan entries corresponding
64 * to a given pending request.)
66 struct GSF_PendingRequestPlanBijection
70 * This is a doubly-linked list.
72 struct GSF_PendingRequestPlanBijection *next_PR;
75 * This is a doubly-linked list.
77 struct GSF_PendingRequestPlanBijection *prev_PR;
80 * This is a doubly-linked list.
82 struct GSF_PendingRequestPlanBijection *next_PE;
85 * This is a doubly-linked list.
87 struct GSF_PendingRequestPlanBijection *prev_PE;
90 * Associated request plan (tells us one of the peers that
91 * we plan to forward the request to).
93 struct GSF_RequestPlan *rp;
96 * Associated pending request (identifies request details
97 * and one of the origins of the request).
99 struct GSF_PendingRequest *pr;
105 * Information we keep per request per peer. This is a doubly-linked
106 * list (with head and tail in the `struct GSF_PendingRequestData`)
107 * with one entry in each heap of each `struct PeerPlan`. Each
108 * entry tracks information relevant for this request and this peer.
110 struct GSF_RequestPlan
114 * This is a doubly-linked list.
116 struct GSF_RequestPlan *next;
119 * This is a doubly-linked list.
121 struct GSF_RequestPlan *prev;
124 * Heap node associated with this request and this peer.
126 struct GNUNET_CONTAINER_HeapNode *hn;
129 * The transmission plan for a peer that this request is associated with.
134 * Head of list of associated pending requests. This tells us
135 * which incoming requests from other peers this plan entry
138 struct GSF_PendingRequestPlanBijection *pe_head;
141 * Tail of list of associated pending requests.
143 struct GSF_PendingRequestPlanBijection *pe_tail;
146 * Earliest time we'd be happy to (re)transmit this request.
148 struct GNUNET_TIME_Absolute earliest_transmission;
151 * When was the last time we transmitted this request to this peer? 0 for never.
153 struct GNUNET_TIME_Absolute last_transmission;
156 * Current priority for this request for this target.
161 * How often did we transmit this request to this peer?
163 unsigned int transmission_counter;
169 * Transmission plan for a peer.
174 * Heap with pending queries (`struct GSF_RequestPlan`), higher weights mean higher priority.
176 struct GNUNET_CONTAINER_Heap *priority_heap;
179 * Heap with pending queries (`struct GSF_RequestPlan`), by transmission time, lowest first.
181 struct GNUNET_CONTAINER_Heap *delay_heap;
184 * Map of queries to plan entries. All entries in the @e priority_heap
185 * or @e delay_heap should be in the @e plan_map. Note that it is
186 * possible for the @e plan_map to have multiple entries for the same
189 struct GNUNET_CONTAINER_MultiHashMap *plan_map;
192 * Current transmission request handle.
194 struct GSF_PeerTransmitHandle *pth;
197 * Peer for which this is the plan.
199 struct GSF_ConnectedPeer *cp;
202 * Current task for executing the plan.
204 struct GNUNET_SCHEDULER_Task *task;
209 * Hash map from peer identities to PeerPlans.
211 static struct GNUNET_CONTAINER_MultiPeerMap *plans;
214 * Sum of all transmission counters (equals total delay for all plan entries).
216 static unsigned long long total_delay;
219 * Number of plan entries.
221 static unsigned long long plan_count;
225 * Return the query (key in the plan_map) for the given request plan.
226 * Note that this key may change as there can be multiple pending
227 * requests for the same key and we just return _one_ of them; this
228 * particular one might complete while another one might still be
229 * active, hence the lifetime of the returned hash code is NOT
230 * necessarily identical to that of the `struct GSF_RequestPlan`
233 * @param rp a request plan
234 * @return the associated query
236 static const struct GNUNET_HashCode *
237 get_rp_key (struct GSF_RequestPlan *rp)
239 return &GSF_pending_request_get_data_ (rp->pe_head->pr)->query;
244 * Figure out when and how to transmit to the given peer.
246 * @param cls the `struct GSF_ConnectedPeer` for transmission
249 schedule_peer_transmission (void *cls);
253 * Insert the given request plan into the heap with the appropriate weight.
255 * @param pp associated peer's plan
256 * @param rp request to plan
259 plan (struct PeerPlan *pp,
260 struct GSF_RequestPlan *rp)
262 #define N ((double)128.0)
264 * Running average delay we currently impose.
266 static double avg_delay;
268 struct GSF_PendingRequestData *prd;
269 struct GNUNET_TIME_Relative delay;
271 GNUNET_assert (rp->pp == pp);
272 GNUNET_STATISTICS_set (GSF_stats,
273 gettext_noop ("# average retransmission delay (ms)"),
274 total_delay * 1000LL / plan_count, GNUNET_NO);
275 prd = GSF_pending_request_get_data_ (rp->pe_head->pr);
277 if (rp->transmission_counter < 8)
279 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
280 rp->transmission_counter);
281 else if (rp->transmission_counter < 32)
283 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
285 (1LL << (rp->transmission_counter - 8)));
288 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
291 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
292 delay.rel_value_us + 1);
293 /* Add 0.01 to avg_delay to avoid division-by-zero later */
294 avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value_us) / N) + 0.01;
297 * For the priority, we need to consider a few basic rules:
298 * 1) if we just started requesting (delay is small), we should
299 * virtually always have a priority of zero.
300 * 2) for requests with average latency, our priority should match
301 * the average priority observed on the network
302 * 3) even the longest-running requests should not be WAY out of
303 * the observed average (thus we bound by a factor of 2)
304 * 4) we add +1 to the observed average priority to avoid everyone
305 * staying put at zero (2 * 0 = 0...).
307 * Using the specific calculation below, we get:
309 * delay = 0 => priority = 0;
310 * delay = avg delay => priority = running-average-observed-priority;
311 * delay >> avg_delay => priority = 2 * running-average-observed-priority;
313 * which satisfies all of the rules above.
315 * Note: M_PI_4 = PI/4 = arctan(1)
318 round ((GSF_current_priorities +
319 1.0) * atan (delay.rel_value_us / avg_delay)) / M_PI_4;
320 /* Note: usage of 'round' and 'atan' requires -lm */
322 if (rp->transmission_counter != 0)
323 delay.rel_value_us += TTL_DECREMENT * 1000;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Considering (re)transmission number %u in %s\n",
326 (unsigned int) rp->transmission_counter,
327 GNUNET_STRINGS_relative_time_to_string (delay,
329 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Earliest (re)transmission for `%s' in %us\n",
332 GNUNET_h2s (&prd->query), rp->transmission_counter);
333 GNUNET_assert (rp->hn == NULL);
334 if (0 == GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value_us)
335 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
338 GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
339 rp->earliest_transmission.abs_value_us);
340 GNUNET_assert (GNUNET_YES ==
341 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
344 if (NULL != pp->task)
345 GNUNET_SCHEDULER_cancel (pp->task);
346 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
352 * Get the pending request with the highest TTL from the given plan.
354 * @param rp plan to investigate
355 * @return pending request with highest TTL
357 struct GSF_PendingRequest *
358 get_latest (const struct GSF_RequestPlan *rp)
360 struct GSF_PendingRequest *ret;
361 struct GSF_PendingRequestPlanBijection *bi;
362 const struct GSF_PendingRequestData *rprd;
363 const struct GSF_PendingRequestData *prd;
367 return NULL; /* should never happen */
369 rprd = GSF_pending_request_get_data_ (ret);
370 for (bi = bi->next_PE; NULL != bi; bi = bi->next_PE)
372 GNUNET_break (GNUNET_YES ==
373 GSF_pending_request_test_active_ (bi->pr));
374 prd = GSF_pending_request_get_data_ (bi->pr);
375 if (prd->ttl.abs_value_us > rprd->ttl.abs_value_us)
386 * Function called to get a message for transmission.
389 * @param buf_size number of bytes available in @a buf
390 * @param buf where to copy the message, NULL on error (peer disconnect)
391 * @return number of bytes copied to @a buf, can be 0 (without indicating an error)
394 transmit_message_callback (void *cls,
398 struct PeerPlan *pp = cls;
399 struct GSF_RequestPlan *rp;
405 /* failed, try again... */
406 if (NULL != pp->task)
407 GNUNET_SCHEDULER_cancel (pp->task);
409 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
410 GNUNET_STATISTICS_update (GSF_stats,
412 ("# transmission failed (core has no bandwidth)"),
416 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
419 if (NULL != pp->task)
420 GNUNET_SCHEDULER_cancel (pp->task);
421 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
424 msize = GSF_pending_request_get_message_ (get_latest (rp),
427 if (msize > buf_size)
429 if (NULL != pp->task)
430 GNUNET_SCHEDULER_cancel (pp->task);
431 /* buffer to small (message changed), try again */
432 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
435 /* remove from root, add again elsewhere... */
437 GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
439 rp->last_transmission = GNUNET_TIME_absolute_get ();
440 rp->transmission_counter++;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Executing plan %p executed %u times, planning retransmission\n",
444 rp, rp->transmission_counter);
446 GNUNET_STATISTICS_update (GSF_stats,
447 gettext_noop ("# query messages sent to other peers"),
455 * Figure out when and how to transmit to the given peer.
457 * @param cls the `struct PeerPlan`
460 schedule_peer_transmission (void *cls)
462 struct PeerPlan *pp = cls;
463 struct GSF_RequestPlan *rp;
465 struct GNUNET_TIME_Relative delay;
470 GSF_peer_transmit_cancel_ (pp->pth);
473 /* move ready requests to priority queue */
474 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
475 (0 == GNUNET_TIME_absolute_get_remaining
476 (rp->earliest_transmission).rel_value_us))
478 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
479 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
483 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
485 /* priority heap (still) empty, check for delay... */
486 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "No active requests for plan %p.\n",
492 return; /* both queues empty */
494 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Sleeping for %s before retrying requests on plan %p.\n",
497 GNUNET_STRINGS_relative_time_to_string (delay,
500 GNUNET_STATISTICS_set (GSF_stats,
501 gettext_noop ("# delay heap timeout (ms)"),
502 delay.rel_value_us / 1000LL, GNUNET_NO);
505 GNUNET_SCHEDULER_add_delayed (delay,
506 &schedule_peer_transmission,
510 #if INSANE_STATISTICS
511 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
514 /* process from priority heap */
515 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Executing query plan %p\n",
519 GNUNET_assert (NULL != rp);
520 msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
522 GSF_peer_transmit_ (pp->cp, GNUNET_YES,
524 GNUNET_TIME_UNIT_FOREVER_REL,
526 &transmit_message_callback, pp);
527 GNUNET_assert (NULL != pp->pth);
532 * Closure for merge_pr().
538 * Request we are trying to merge.
540 struct GSF_PendingRequest *pr;
543 * Set to #GNUNET_YES if we succeeded to merge.
551 * Iterator that checks if an equivalent request is already
552 * present for this peer.
555 * @param query the query
556 * @param element request plan stored at the node
557 * @return #GNUNET_YES if we should continue to iterate,
558 * #GNUNET_NO if not (merge success)
562 const struct GNUNET_HashCode *query,
565 struct MergeContext *mpr = cls;
566 struct GSF_RequestPlan *rp = element;
567 struct GSF_PendingRequestData *prd;
568 struct GSF_PendingRequestPlanBijection *bi;
569 struct GSF_PendingRequest *latest;
571 GNUNET_break (GNUNET_YES ==
572 GSF_pending_request_test_active_ (mpr->pr));
574 GSF_pending_request_is_compatible_ (mpr->pr,
577 /* merge new request with existing request plan */
578 bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
581 prd = GSF_pending_request_get_data_ (mpr->pr);
582 GNUNET_CONTAINER_MDLL_insert (PR,
586 GNUNET_CONTAINER_MDLL_insert (PE,
590 mpr->merged = GNUNET_YES;
591 #if INSANE_STATISTICS
592 GNUNET_STATISTICS_update (GSF_stats,
593 gettext_noop ("# requests merged"),
597 latest = get_latest (rp);
598 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value_us <
599 prd->ttl.abs_value_us)
601 #if INSANE_STATISTICS
602 GNUNET_STATISTICS_update (GSF_stats,
603 gettext_noop ("# requests refreshed"),
607 rp->transmission_counter = 0; /* reset */
614 * Create a new query plan entry.
616 * @param cp peer with the entry
617 * @param pr request with the entry
620 GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
621 struct GSF_PendingRequest *pr)
623 const struct GNUNET_PeerIdentity *id;
625 struct GSF_PendingRequestData *prd;
626 struct GSF_RequestPlan *rp;
627 struct GSF_PendingRequestPlanBijection *bi;
628 struct MergeContext mpc;
630 GNUNET_assert (GNUNET_YES ==
631 GSF_pending_request_test_active_ (pr));
632 GNUNET_assert (NULL != cp);
633 id = GSF_connected_peer_get_identity2_ (cp);
634 pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
637 pp = GNUNET_new (struct PeerPlan);
638 pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
640 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
642 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
644 GNUNET_assert (GNUNET_OK ==
645 GNUNET_CONTAINER_multipeermap_put (plans,
648 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
650 mpc.merged = GNUNET_NO;
652 prd = GSF_pending_request_get_data_ (pr);
653 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
657 if (GNUNET_NO != mpc.merged)
660 GNUNET_STATISTICS_update (GSF_stats,
661 gettext_noop ("# query plan entries"),
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Planning transmission of query `%s' to peer `%s'\n",
666 GNUNET_h2s (&prd->query),
668 rp = GNUNET_new (struct GSF_RequestPlan);
669 bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
672 GNUNET_CONTAINER_MDLL_insert (PR,
676 GNUNET_CONTAINER_MDLL_insert (PE,
681 GNUNET_assert (GNUNET_YES ==
682 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
692 * Notify the plan about a peer being no longer available;
693 * destroy all entries associated with this peer.
695 * @param cp connected peer
698 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
700 const struct GNUNET_PeerIdentity *id;
702 struct GSF_RequestPlan *rp;
703 struct GSF_PendingRequestData *prd;
704 struct GSF_PendingRequestPlanBijection *bi;
706 id = GSF_connected_peer_get_identity2_ (cp);
707 pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
709 return; /* nothing was ever planned for this peer */
710 GNUNET_assert (GNUNET_YES ==
711 GNUNET_CONTAINER_multipeermap_remove (plans, id,
715 GSF_peer_transmit_cancel_ (pp->pth);
718 if (NULL != pp->task)
720 GNUNET_SCHEDULER_cancel (pp->task);
723 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
725 GNUNET_break (GNUNET_YES ==
726 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
729 while (NULL != (bi = rp->pe_head))
731 GNUNET_CONTAINER_MDLL_remove (PE,
735 prd = GSF_pending_request_get_data_ (bi->pr);
736 GNUNET_CONTAINER_MDLL_remove (PR,
745 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
746 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
748 GNUNET_break (GNUNET_YES ==
749 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
752 while (NULL != (bi = rp->pe_head))
754 prd = GSF_pending_request_get_data_ (bi->pr);
755 GNUNET_CONTAINER_MDLL_remove (PE,
759 GNUNET_CONTAINER_MDLL_remove (PR,
768 GNUNET_STATISTICS_set (GSF_stats,
769 gettext_noop ("# query plan entries"),
772 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
773 GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
779 * Get the last transmission attempt time for the request plan list
780 * referenced by @a pr_head, that was sent to @a sender
782 * @param pr_head request plan reference list to check.
783 * @param sender the peer that we've sent the request to.
784 * @param result the timestamp to fill, set to #GNUNET_TIME_UNIT_FOREVER_ABS if never transmitted
785 * @return #GNUNET_YES if @a result was changed, #GNUNET_NO otherwise.
788 GSF_request_plan_reference_get_last_transmission_ (struct GSF_PendingRequestPlanBijection *pr_head,
789 struct GSF_ConnectedPeer *sender,
790 struct GNUNET_TIME_Absolute *result)
792 struct GSF_PendingRequestPlanBijection *bi;
794 for (bi = pr_head; NULL != bi; bi = bi->next_PR)
796 if (bi->rp->pp->cp == sender)
798 if (0 == bi->rp->last_transmission.abs_value_us)
799 *result = GNUNET_TIME_UNIT_FOREVER_ABS;
801 *result = bi->rp->last_transmission;
810 * Notify the plan about a request being done; destroy all entries
811 * associated with this request.
813 * @param pr request that is done
816 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
818 struct GSF_RequestPlan *rp;
819 struct GSF_PendingRequestData *prd;
820 struct GSF_PendingRequestPlanBijection *bi;
822 prd = GSF_pending_request_get_data_ (pr);
823 while (NULL != (bi = prd->pr_head))
826 GNUNET_CONTAINER_MDLL_remove (PR,
830 GNUNET_CONTAINER_MDLL_remove (PE,
834 GNUNET_assert (bi->pr == pr);
835 if (NULL == rp->pe_head)
837 GNUNET_CONTAINER_heap_remove_node (rp->hn);
839 GNUNET_break (GNUNET_YES ==
840 GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
847 GNUNET_STATISTICS_set (GSF_stats,
848 gettext_noop ("# query plan entries"),
855 * Initialize plan subsystem.
860 plans = GNUNET_CONTAINER_multipeermap_create (256,
866 * Shutdown plan subsystem.
871 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (plans));
872 GNUNET_CONTAINER_multipeermap_destroy (plans);
877 /* end of gnunet-service-fs_pe.h */