2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
92 struct GSF_RequestPlan *rp;
95 * Associated pending request.
97 struct GSF_PendingRequest *pr;
103 * Information we keep per request per peer. This is a doubly-linked
104 * list (with head and tail in the 'struct GSF_PendingRequestData')
105 * with one entry in each heap of each 'struct PeerPlan'. Each
106 * entry tracks information relevant for this request and this peer.
108 struct GSF_RequestPlan
112 * This is a doubly-linked list.
114 struct GSF_RequestPlan *next;
117 * This is a doubly-linked list.
119 struct GSF_RequestPlan *prev;
122 * Heap node associated with this request and this peer.
124 struct GNUNET_CONTAINER_HeapNode *hn;
127 * The transmission plan for a peer that this request is associated with.
132 * Head of list of associated pending requests.
134 struct GSF_PendingRequestPlanBijection *pe_head;
137 * Tail of list of associated pending requests.
139 struct GSF_PendingRequestPlanBijection *pe_tail;
142 * Earliest time we'd be happy to (re)transmit this request.
144 struct GNUNET_TIME_Absolute earliest_transmission;
147 * When was the last time we transmitted this request to this peer? 0 for never.
149 struct GNUNET_TIME_Absolute last_transmission;
152 * Current priority for this request for this target.
157 * How often did we transmit this request to this peer?
159 unsigned int transmission_counter;
165 * Transmission plan for a peer.
170 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
172 struct GNUNET_CONTAINER_Heap *priority_heap;
175 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
177 struct GNUNET_CONTAINER_Heap *delay_heap;
180 * Map of queries to plan entries. All entries in the priority_heap or delay_heap
181 * should be in the plan map. Note that it IS possible for the plan map to have
182 * multiple entries for the same query.
184 struct GNUNET_CONTAINER_MultiHashMap *plan_map;
187 * Current transmission request handle.
189 struct GSF_PeerTransmitHandle *pth;
192 * Peer for which this is the plan.
194 struct GSF_ConnectedPeer *cp;
197 * Current task for executing the plan.
199 GNUNET_SCHEDULER_TaskIdentifier task;
204 * Hash map from peer identities to PeerPlans.
206 static struct GNUNET_CONTAINER_MultiHashMap *plans;
209 * Sum of all transmission counters (equals total delay for all plan entries).
211 static unsigned long long total_delay;
214 * Number of plan entries.
216 static unsigned long long plan_count;
220 * Return the query (key in the plan_map) for the given request plan.
221 * Note that this key may change as there can be multiple pending
222 * requests for the same key and we just return _one_ of them; this
223 * particular one might complete while another one might still be
224 * active, hence the lifetime of the returned hash code is NOT
225 * necessarily identical to that of the 'struct GSF_RequestPlan'
228 * @param rp a request plan
229 * @return the associated query
231 static const struct GNUNET_HashCode *
232 get_rp_key (struct GSF_RequestPlan *rp)
234 return &GSF_pending_request_get_data_ (rp->pe_head->pr)->query;
239 * Figure out when and how to transmit to the given peer.
241 * @param cls the 'struct GSF_ConnectedPeer' for transmission
242 * @param tc scheduler context
245 schedule_peer_transmission (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc);
250 * Insert the given request plan into the heap with the appropriate weight.
252 * @param pp associated peer's plan
253 * @param rp request to plan
256 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
258 #define N ((double)128.0)
260 * Running average delay we currently impose.
262 static double avg_delay;
264 struct GSF_PendingRequestData *prd;
265 struct GNUNET_TIME_Relative delay;
267 GNUNET_assert (rp->pp == pp);
268 GNUNET_STATISTICS_set (GSF_stats,
269 gettext_noop ("# average retransmission delay (ms)"),
270 total_delay * 1000LL / plan_count, GNUNET_NO);
271 prd = GSF_pending_request_get_data_ (rp->pe_head->pr);
273 if (rp->transmission_counter < 8)
275 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
276 rp->transmission_counter);
277 else if (rp->transmission_counter < 32)
279 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
281 (1LL << (rp->transmission_counter - 8)));
284 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
287 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
288 delay.rel_value + 1);
289 /* Add 0.01 to avg_delay to avoid division-by-zero later */
290 avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value) / N) + 0.01;
293 * For the priority, we need to consider a few basic rules:
294 * 1) if we just started requesting (delay is small), we should
295 * virtually always have a priority of zero.
296 * 2) for requests with average latency, our priority should match
297 * the average priority observed on the network
298 * 3) even the longest-running requests should not be WAY out of
299 * the observed average (thus we bound by a factor of 2)
300 * 4) we add +1 to the observed average priority to avoid everyone
301 * staying put at zero (2 * 0 = 0...).
303 * Using the specific calculation below, we get:
305 * delay = 0 => priority = 0;
306 * delay = avg delay => priority = running-average-observed-priority;
307 * delay >> avg_delay => priority = 2 * running-average-observed-priority;
309 * which satisfies all of the rules above.
311 * Note: M_PI_4 = PI/4 = arctan(1)
314 round ((GSF_current_priorities +
315 1.0) * atan (delay.rel_value / avg_delay)) / M_PI_4;
316 /* Note: usage of 'round' and 'atan' requires -lm */
318 if (rp->transmission_counter != 0)
319 delay.rel_value += TTL_DECREMENT;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Considering (re)transmission number %u in %llu ms\n",
322 (unsigned int) rp->transmission_counter,
323 (unsigned long long) delay.rel_value);
324 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Earliest (re)transmission for `%s' in %us\n",
327 GNUNET_h2s (&prd->query), rp->transmission_counter);
328 GNUNET_assert (rp->hn == NULL);
329 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
331 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
334 GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
335 rp->earliest_transmission.abs_value);
336 GNUNET_assert (GNUNET_YES ==
337 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
340 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
341 GNUNET_SCHEDULER_cancel (pp->task);
342 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
348 * Get the pending request with the highest TTL from the given plan.
350 * @param rp plan to investigate
351 * @return pending request with highest TTL
353 struct GSF_PendingRequest *
354 get_latest (const struct GSF_RequestPlan *rp)
356 struct GSF_PendingRequest *ret;
357 struct GSF_PendingRequestPlanBijection *bi;
361 return NULL; /* should never happen */
366 if (GSF_pending_request_get_data_ (bi->pr)->ttl.abs_value >
367 GSF_pending_request_get_data_ (ret)->ttl.abs_value)
376 * Function called to get a message for transmission.
379 * @param buf_size number of bytes available in buf
380 * @param buf where to copy the message, NULL on error (peer disconnect)
381 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
384 transmit_message_callback (void *cls, size_t buf_size, void *buf)
386 struct PeerPlan *pp = cls;
387 struct GSF_RequestPlan *rp;
393 /* failed, try again... */
394 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
395 GNUNET_SCHEDULER_cancel (pp->task);
397 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
398 GNUNET_STATISTICS_update (GSF_stats,
400 ("# transmission failed (core has no bandwidth)"),
404 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
407 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
408 GNUNET_SCHEDULER_cancel (pp->task);
409 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
412 msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
413 if (msize > buf_size)
415 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
416 GNUNET_SCHEDULER_cancel (pp->task);
417 /* buffer to small (message changed), try again */
418 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
421 /* remove from root, add again elsewhere... */
422 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
424 rp->last_transmission = GNUNET_TIME_absolute_get ();
425 rp->transmission_counter++;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Executing plan %p executed %u times, planning retransmission\n",
429 rp, rp->transmission_counter);
431 GNUNET_STATISTICS_update (GSF_stats,
433 ("# query messages sent to other peers"), 1,
440 * Figure out when and how to transmit to the given peer.
442 * @param cls the 'struct PeerPlan'
443 * @param tc scheduler context
446 schedule_peer_transmission (void *cls,
447 const struct GNUNET_SCHEDULER_TaskContext *tc)
449 struct PeerPlan *pp = cls;
450 struct GSF_RequestPlan *rp;
452 struct GNUNET_TIME_Relative delay;
454 pp->task = GNUNET_SCHEDULER_NO_TASK;
457 GSF_peer_transmit_cancel_ (pp->pth);
460 /* move ready requests to priority queue */
461 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
462 (GNUNET_TIME_absolute_get_remaining
463 (rp->earliest_transmission).rel_value == 0))
465 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
466 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
468 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
470 /* priority heap (still) empty, check for delay... */
471 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
476 return; /* both queues empty */
478 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Sleeping for %llu ms before retrying requests on plan %p.\n",
481 (unsigned long long) delay.rel_value, pp);
482 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
483 delay.rel_value, GNUNET_NO);
486 GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
489 #if INSANE_STATISTICS
490 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
493 /* process from priority heap */
494 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
496 GNUNET_assert (NULL != rp);
497 msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
499 GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
500 GNUNET_TIME_UNIT_FOREVER_REL, msize,
501 &transmit_message_callback, pp);
502 GNUNET_assert (NULL != pp->pth);
507 * Closure for 'merge_pr'.
512 struct GSF_PendingRequest *pr;
520 * Iterator that checks if an equivalent request is already
521 * present for this peer.
524 * @param query the query
525 * @param element request plan stored at the node
526 * @return GNUNET_YES if we should continue to iterate,
527 * GNUNET_NO if not (merge success)
530 merge_pr (void *cls, const struct GNUNET_HashCode * query, void *element)
532 struct MergeContext *mpr = cls;
533 struct GSF_RequestPlan *rp = element;
534 struct GSF_PendingRequestData *prd;
535 struct GSF_PendingRequestPlanBijection *bi;
536 struct GSF_PendingRequest *latest;
539 GSF_pending_request_is_compatible_ (mpr->pr, rp->pe_head->pr))
541 /* merge new request with existing request plan */
542 bi = GNUNET_malloc (sizeof (struct GSF_PendingRequestPlanBijection));
545 prd = GSF_pending_request_get_data_ (mpr->pr);
546 GNUNET_CONTAINER_MDLL_insert (PR, prd->pr_head, prd->pr_tail, bi);
547 GNUNET_CONTAINER_MDLL_insert (PE, rp->pe_head, rp->pe_tail, bi);
548 mpr->merged = GNUNET_YES;
549 #if INSANE_STATISTICS
550 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
553 latest = get_latest (rp);
554 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
557 #if INSANE_STATISTICS
558 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
561 rp->transmission_counter = 0; /* reset */
568 * Create a new query plan entry.
570 * @param cp peer with the entry
571 * @param pr request with the entry
574 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
576 const struct GNUNET_PeerIdentity *id;
578 struct GSF_PendingRequestData *prd;
579 struct GSF_RequestPlan *rp;
580 struct GSF_PendingRequestPlanBijection *bi;
581 struct MergeContext mpc;
583 GNUNET_assert (NULL != cp);
584 id = GSF_connected_peer_get_identity2_ (cp);
585 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id->hashPubKey);
588 pp = GNUNET_malloc (sizeof (struct PeerPlan));
589 pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
591 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
593 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
595 GNUNET_CONTAINER_multihashmap_put (plans,
597 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
599 mpc.merged = GNUNET_NO;
601 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
602 &GSF_pending_request_get_data_
603 (pr)->query, &merge_pr, &mpc); // 8 MB in 'merge_pr'
604 if (GNUNET_NO != mpc.merged)
606 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
607 &GSF_pending_request_get_data_
608 (pr)->query, &merge_pr, &mpc);
609 if (GNUNET_NO != mpc.merged)
612 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
614 prd = GSF_pending_request_get_data_ (pr);
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Planning transmission of query `%s' to peer `%s'\n",
617 GNUNET_h2s (&prd->query), GNUNET_i2s (id));
618 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan)); // 8 MB
619 bi = GNUNET_malloc (sizeof (struct GSF_PendingRequestPlanBijection));
622 GNUNET_CONTAINER_MDLL_insert (PR, prd->pr_head, prd->pr_tail, bi);
623 GNUNET_CONTAINER_MDLL_insert (PE, rp->pe_head, rp->pe_tail, bi);
625 GNUNET_assert (GNUNET_YES ==
626 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); // 8 MB
629 plan (pp, rp); // +5 MB (plan/heap-insert)
634 * Notify the plan about a peer being no longer available;
635 * destroy all entries associated with this peer.
637 * @param cp connected peer
640 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
642 const struct GNUNET_PeerIdentity *id;
644 struct GSF_RequestPlan *rp;
645 struct GSF_PendingRequestData *prd;
646 struct GSF_PendingRequestPlanBijection *bi;
648 id = GSF_connected_peer_get_identity2_ (cp);
649 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id->hashPubKey);
651 return; /* nothing was ever planned for this peer */
652 GNUNET_assert (GNUNET_YES ==
653 GNUNET_CONTAINER_multihashmap_remove (plans, &id->hashPubKey,
657 GSF_peer_transmit_cancel_ (pp->pth);
660 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
662 GNUNET_SCHEDULER_cancel (pp->task);
663 pp->task = GNUNET_SCHEDULER_NO_TASK;
665 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
667 GNUNET_break (GNUNET_YES ==
668 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
669 get_rp_key (rp), rp));
670 while (NULL != (bi = rp->pe_head))
672 GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
673 prd = GSF_pending_request_get_data_ (bi->pr);
674 GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
680 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
681 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
683 GNUNET_break (GNUNET_YES ==
684 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
685 get_rp_key (rp), rp));
686 while (NULL != (bi = rp->pe_head))
688 prd = GSF_pending_request_get_data_ (bi->pr);
689 GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
690 GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
696 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
697 plan_count, GNUNET_NO);
698 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
699 GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
705 * Get the last transmission attempt time for the request plan list
706 * referenced by 'pr_head', that was sent to 'sender'
708 * @param pr_head request plan reference list to check.
709 * @param sender the peer that we've sent the request to.
710 * @param result the timestamp to fill, set to "FOREVER" if never transmitted
711 * @return GNUNET_YES if 'result' was changed, GNUNET_NO otherwise.
714 GSF_request_plan_reference_get_last_transmission_ (
715 struct GSF_PendingRequestPlanBijection *pr_head, struct GSF_ConnectedPeer *sender,
716 struct GNUNET_TIME_Absolute *result)
718 struct GSF_PendingRequestPlanBijection *bi;
720 for (bi = pr_head; NULL != bi; bi = bi->next_PR)
722 if (bi->rp->pp->cp == sender)
724 if (0 == bi->rp->last_transmission.abs_value)
725 *result = GNUNET_TIME_UNIT_FOREVER_ABS;
727 *result = bi->rp->last_transmission;
736 * Notify the plan about a request being done; destroy all entries
737 * associated with this request.
739 * @param pr request that is done
742 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
744 struct GSF_RequestPlan *rp;
745 struct GSF_PendingRequestData *prd;
746 struct GSF_PendingRequestPlanBijection *bi;
748 prd = GSF_pending_request_get_data_ (pr);
749 while (NULL != (bi = prd->pr_head))
752 GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
753 GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
754 if (NULL == rp->pe_head)
756 GNUNET_CONTAINER_heap_remove_node (rp->hn);
758 GNUNET_break (GNUNET_YES ==
759 GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
760 &GSF_pending_request_get_data_
767 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
768 plan_count, GNUNET_NO);
773 * Initialize plan subsystem.
778 plans = GNUNET_CONTAINER_multihashmap_create (256, GNUNET_YES);
783 * Shutdown plan subsystem.
788 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
789 GNUNET_CONTAINER_multihashmap_destroy (plans);
794 /* end of gnunet-service-fs_pe.h */