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 * DLL of request plans a particular pending request is
53 struct GSF_RequestPlanReference
57 * This is a doubly-linked list.
59 struct GSF_RequestPlanReference *next;
62 * This is a doubly-linked list.
64 struct GSF_RequestPlanReference *prev;
67 * Associated request plan.
69 struct GSF_RequestPlan *rp;
72 * Corresponding PendingRequestList.
74 struct PendingRequestList *prl;
79 * List of GSF_PendingRequests this request plan
82 struct PendingRequestList
86 * This is a doubly-linked list.
88 struct PendingRequestList *next;
91 * This is a doubly-linked list.
93 struct PendingRequestList *prev;
96 * Associated pending request.
98 struct GSF_PendingRequest *pr;
101 * Corresponding GSF_RequestPlanReference.
103 struct GSF_RequestPlanReference *rpr;
109 * Information we keep per request per peer. This is a doubly-linked
110 * list (with head and tail in the 'struct GSF_PendingRequestData')
111 * with one entry in each heap of each 'struct PeerPlan'. Each
112 * entry tracks information relevant for this request and this peer.
114 struct GSF_RequestPlan
118 * This is a doubly-linked list.
120 struct GSF_RequestPlan *next;
123 * This is a doubly-linked list.
125 struct GSF_RequestPlan *prev;
128 * Heap node associated with this request and this peer.
130 struct GNUNET_CONTAINER_HeapNode *hn;
133 * The transmission plan for a peer that this request is associated with.
138 * Head of list of associated pending requests.
140 struct PendingRequestList *prl_head;
143 * Tail of list of associated pending requests.
145 struct PendingRequestList *prl_tail;
148 * Earliest time we'd be happy to (re)transmit this request.
150 struct GNUNET_TIME_Absolute earliest_transmission;
153 * When was the last time we transmitted this request to this peer? 0 for never.
155 struct GNUNET_TIME_Absolute last_transmission;
158 * Current priority for this request for this target.
163 * How often did we transmit this request to this peer?
165 unsigned int transmission_counter;
171 * Transmission plan for a peer.
176 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
178 struct GNUNET_CONTAINER_Heap *priority_heap;
181 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
183 struct GNUNET_CONTAINER_Heap *delay_heap;
186 * Map of queries to plan entries. All entries in the priority_heap or delay_heap
187 * should be in the plan map. Note that it IS possible for the plan map to have
188 * multiple entries for the same query.
190 struct GNUNET_CONTAINER_MultiHashMap *plan_map;
193 * Current transmission request handle.
195 struct GSF_PeerTransmitHandle *pth;
198 * Peer for which this is the plan.
200 struct GSF_ConnectedPeer *cp;
203 * Current task for executing the plan.
205 GNUNET_SCHEDULER_TaskIdentifier task;
210 * Hash map from peer identities to PeerPlans.
212 static struct GNUNET_CONTAINER_MultiHashMap *plans;
215 * Sum of all transmission counters (equals total delay for all plan entries).
217 static unsigned long long total_delay;
220 * Number of plan entries.
222 static unsigned long long plan_count;
226 * Return the query (key in the plan_map) for the given request plan.
227 * Note that this key may change as there can be multiple pending
228 * requests for the same key and we just return _one_ of them; this
229 * particular one might complete while another one might still be
230 * active, hence the lifetime of the returned hash code is NOT
231 * necessarily identical to that of the 'struct GSF_RequestPlan'
234 * @param rp a request plan
235 * @return the associated query
237 static const struct GNUNET_HashCode *
238 get_rp_key (struct GSF_RequestPlan *rp)
240 return &GSF_pending_request_get_data_ (rp->prl_head->pr)->query;
245 * Figure out when and how to transmit to the given peer.
247 * @param cls the 'struct GSF_ConnectedPeer' for transmission
248 * @param tc scheduler context
251 schedule_peer_transmission (void *cls,
252 const struct GNUNET_SCHEDULER_TaskContext *tc);
256 * Insert the given request plan into the heap with the appropriate weight.
258 * @param pp associated peer's plan
259 * @param rp request to plan
262 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
264 #define N ((double)128.0)
266 * Running average delay we currently impose.
268 static double avg_delay;
270 struct GSF_PendingRequestData *prd;
271 struct GNUNET_TIME_Relative delay;
273 GNUNET_assert (rp->pp == pp);
274 GNUNET_STATISTICS_set (GSF_stats,
275 gettext_noop ("# average retransmission delay (ms)"),
276 total_delay * 1000LL / plan_count, GNUNET_NO);
277 prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
279 if (rp->transmission_counter < 8)
281 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
282 rp->transmission_counter);
283 else if (rp->transmission_counter < 32)
285 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
287 (1LL << (rp->transmission_counter - 8)));
290 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
293 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
294 delay.rel_value + 1);
295 /* Add 0.01 to avg_delay to avoid division-by-zero later */
296 avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value) / N) + 0.01;
299 * For the priority, we need to consider a few basic rules:
300 * 1) if we just started requesting (delay is small), we should
301 * virtually always have a priority of zero.
302 * 2) for requests with average latency, our priority should match
303 * the average priority observed on the network
304 * 3) even the longest-running requests should not be WAY out of
305 * the observed average (thus we bound by a factor of 2)
306 * 4) we add +1 to the observed average priority to avoid everyone
307 * staying put at zero (2 * 0 = 0...).
309 * Using the specific calculation below, we get:
311 * delay = 0 => priority = 0;
312 * delay = avg delay => priority = running-average-observed-priority;
313 * delay >> avg_delay => priority = 2 * running-average-observed-priority;
315 * which satisfies all of the rules above.
317 * Note: M_PI_4 = PI/4 = arctan(1)
320 round ((GSF_current_priorities +
321 1.0) * atan (delay.rel_value / avg_delay)) / M_PI_4;
322 /* Note: usage of 'round' and 'atan' requires -lm */
324 if (rp->transmission_counter != 0)
325 delay.rel_value += TTL_DECREMENT;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Considering (re)transmission number %u in %llu ms\n",
328 (unsigned int) rp->transmission_counter,
329 (unsigned long long) delay.rel_value);
330 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Earliest (re)transmission for `%s' in %us\n",
333 GNUNET_h2s (&prd->query), rp->transmission_counter);
334 GNUNET_assert (rp->hn == NULL);
335 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
337 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
340 GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
341 rp->earliest_transmission.abs_value);
342 GNUNET_assert (GNUNET_YES ==
343 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
346 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
347 GNUNET_SCHEDULER_cancel (pp->task);
348 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
354 * Get the pending request with the highest TTL from the given plan.
356 * @param rp plan to investigate
357 * @return pending request with highest TTL
359 struct GSF_PendingRequest *
360 get_latest (const struct GSF_RequestPlan *rp)
362 struct GSF_PendingRequest *ret;
363 struct PendingRequestList *prl;
370 if (GSF_pending_request_get_data_ (prl->pr)->ttl.abs_value >
371 GSF_pending_request_get_data_ (ret)->ttl.abs_value)
380 * Function called to get a message for transmission.
383 * @param buf_size number of bytes available in buf
384 * @param buf where to copy the message, NULL on error (peer disconnect)
385 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
388 transmit_message_callback (void *cls, size_t buf_size, void *buf)
390 struct PeerPlan *pp = cls;
391 struct GSF_RequestPlan *rp;
397 /* failed, try again... */
398 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
399 GNUNET_STATISTICS_update (GSF_stats,
401 ("# transmission failed (core has no bandwidth)"),
405 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
408 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
411 msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
412 if (msize > buf_size)
414 /* buffer to small (message changed), try again */
415 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
418 /* remove from root, add again elsewhere... */
419 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
421 rp->last_transmission = GNUNET_TIME_absolute_get ();
422 rp->transmission_counter++;
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Executing plan %p executed %u times, planning retransmission\n",
426 rp, rp->transmission_counter);
428 GNUNET_STATISTICS_update (GSF_stats,
430 ("# query messages sent to other peers"), 1,
437 * Figure out when and how to transmit to the given peer.
439 * @param cls the 'struct PeerPlan'
440 * @param tc scheduler context
443 schedule_peer_transmission (void *cls,
444 const struct GNUNET_SCHEDULER_TaskContext *tc)
446 struct PeerPlan *pp = cls;
447 struct GSF_RequestPlan *rp;
449 struct GNUNET_TIME_Relative delay;
451 pp->task = GNUNET_SCHEDULER_NO_TASK;
454 GSF_peer_transmit_cancel_ (pp->pth);
457 /* move ready requests to priority queue */
458 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
459 (GNUNET_TIME_absolute_get_remaining
460 (rp->earliest_transmission).rel_value == 0))
462 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
463 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
465 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
467 /* priority heap (still) empty, check for delay... */
468 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
473 return; /* both queues empty */
475 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Sleeping for %llu ms before retrying requests on plan %p.\n",
478 (unsigned long long) delay.rel_value, pp);
479 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
480 delay.rel_value, GNUNET_NO);
483 GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
486 #if INSANE_STATISTICS
487 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
490 /* process from priority heap */
491 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
493 GNUNET_assert (NULL != rp);
494 msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
496 GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
497 GNUNET_TIME_UNIT_FOREVER_REL, msize,
498 &transmit_message_callback, pp);
499 GNUNET_assert (NULL != pp->pth);
504 * Closure for 'merge_pr'.
509 struct GSF_PendingRequest *pr;
517 * Iterator that checks if an equivalent request is already
518 * present for this peer.
521 * @param query the query
522 * @param element request plan stored at the node
523 * @return GNUNET_YES if we should continue to iterate,
524 * GNUNET_NO if not (merge success)
527 merge_pr (void *cls, const struct GNUNET_HashCode * query, void *element)
529 struct MergeContext *mpr = cls;
530 struct GSF_RequestPlan *rp = element;
531 struct GSF_PendingRequestData *prd;
532 struct GSF_RequestPlanReference *rpr;
533 struct PendingRequestList *prl;
534 struct GSF_PendingRequest *latest;
537 GSF_pending_request_is_compatible_ (mpr->pr, rp->prl_head->pr))
539 /* merge new request with existing request plan */
540 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
541 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
546 prd = GSF_pending_request_get_data_ (mpr->pr);
547 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
548 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
549 mpr->merged = GNUNET_YES;
550 #if INSANE_STATISTICS
551 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
554 latest = get_latest (rp);
555 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
558 #if INSANE_STATISTICS
559 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
562 rp->transmission_counter = 0; /* reset */
569 * Create a new query plan entry.
571 * @param cp peer with the entry
572 * @param pr request with the entry
575 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
577 const struct GNUNET_PeerIdentity *id;
579 struct GSF_PendingRequestData *prd;
580 struct GSF_RequestPlan *rp;
581 struct GSF_RequestPlanReference *rpr;
582 struct PendingRequestList *prl;
583 struct MergeContext mpc;
585 GNUNET_assert (NULL != cp);
586 id = GSF_connected_peer_get_identity2_ (cp);
587 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id->hashPubKey);
590 pp = GNUNET_malloc (sizeof (struct PeerPlan));
591 pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
593 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
595 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
597 GNUNET_CONTAINER_multihashmap_put (plans,
599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
601 mpc.merged = GNUNET_NO;
603 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
604 &GSF_pending_request_get_data_
605 (pr)->query, &merge_pr, &mpc); // 8 MB in 'merge_pr'
606 if (GNUNET_NO != mpc.merged)
608 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
609 &GSF_pending_request_get_data_
610 (pr)->query, &merge_pr, &mpc);
611 if (GNUNET_NO != mpc.merged)
614 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
616 prd = GSF_pending_request_get_data_ (pr);
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Planning transmission of query `%s' to peer `%s'\n",
619 GNUNET_h2s (&prd->query), GNUNET_i2s (id));
620 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan)); // 8 MB
621 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
622 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
627 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
628 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
630 GNUNET_assert (GNUNET_YES ==
631 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); // 8 MB
634 plan (pp, rp); // +5 MB (plan/heap-insert)
639 * Notify the plan about a peer being no longer available;
640 * destroy all entries associated with this peer.
642 * @param cp connected peer
645 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
647 const struct GNUNET_PeerIdentity *id;
649 struct GSF_RequestPlan *rp;
650 struct GSF_PendingRequestData *prd;
651 struct PendingRequestList *prl;
653 id = GSF_connected_peer_get_identity2_ (cp);
654 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id->hashPubKey);
656 return; /* nothing was ever planned for this peer */
657 GNUNET_assert (GNUNET_YES ==
658 GNUNET_CONTAINER_multihashmap_remove (plans, &id->hashPubKey,
662 GSF_peer_transmit_cancel_ (pp->pth);
665 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
667 GNUNET_SCHEDULER_cancel (pp->task);
668 pp->task = GNUNET_SCHEDULER_NO_TASK;
670 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
672 GNUNET_break (GNUNET_YES ==
673 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
674 get_rp_key (rp), rp));
675 while (NULL != (prl = rp->prl_head))
677 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
678 prd = GSF_pending_request_get_data_ (prl->pr);
679 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
680 GNUNET_free (prl->rpr);
686 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
687 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
689 GNUNET_break (GNUNET_YES ==
690 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
691 get_rp_key (rp), rp));
692 while (NULL != (prl = rp->prl_head))
694 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
695 prd = GSF_pending_request_get_data_ (prl->pr);
696 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
697 GNUNET_free (prl->rpr);
703 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
704 plan_count, GNUNET_NO);
705 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
706 GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
711 * Get the last transmission attempt time for the request plan list
712 * referenced by 'rpr_head', that was sent to 'sender'
714 * @param rpr_head request plan reference list to check.
715 * @param sender the peer that we've sent the request to.
716 * @param result the timestamp to fill.
717 * @return GNUNET_YES if 'result' was changed, GNUNET_NO otherwise.
720 GSF_request_plan_reference_get_last_transmission_ (
721 struct GSF_RequestPlanReference *rpr_head, struct GSF_ConnectedPeer *sender,
722 struct GNUNET_TIME_Absolute *result)
724 struct GSF_RequestPlanReference *rpr;
725 for (rpr = rpr_head; rpr; rpr = rpr->next)
727 if (rpr->rp->pp->cp == sender)
729 *result = rpr->rp->last_transmission;
737 * Notify the plan about a request being done; destroy all entries
738 * associated with this request.
740 * @param pr request that is done
743 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
745 struct GSF_RequestPlan *rp;
746 struct GSF_PendingRequestData *prd;
747 struct GSF_RequestPlanReference *rpr;
749 prd = GSF_pending_request_get_data_ (pr);
750 while (NULL != (rpr = prd->rpr_head))
752 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
754 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
755 if (NULL == rp->prl_head)
757 GNUNET_CONTAINER_heap_remove_node (rp->hn);
759 GNUNET_break (GNUNET_YES ==
760 GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
761 &GSF_pending_request_get_data_
762 (rpr->prl->pr)->query,
766 GNUNET_free (rpr->prl);
769 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
770 plan_count, GNUNET_NO);
775 * Initialize plan subsystem.
780 plans = GNUNET_CONTAINER_multihashmap_create (256, GNUNET_YES);
785 * Shutdown plan subsystem.
790 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
791 GNUNET_CONTAINER_multihashmap_destroy (plans);
796 /* end of gnunet-service-fs_pe.h */