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"
34 * List of GSF_PendingRequests this request plan
37 struct PendingRequestList;
40 * Transmission plan for a peer.
46 * DLL of request plans a particular pending request is
49 struct GSF_RequestPlanReference
53 * This is a doubly-linked list.
55 struct GSF_RequestPlanReference *next;
58 * This is a doubly-linked list.
60 struct GSF_RequestPlanReference *prev;
63 * Associated request plan.
65 struct GSF_RequestPlan *rp;
68 * Corresponding PendingRequestList.
70 struct PendingRequestList *prl;
75 * List of GSF_PendingRequests this request plan
78 struct PendingRequestList
82 * This is a doubly-linked list.
84 struct PendingRequestList *next;
87 * This is a doubly-linked list.
89 struct PendingRequestList *prev;
92 * Associated pending request.
94 struct GSF_PendingRequest *pr;
97 * Corresponding GSF_RequestPlanReference.
99 struct GSF_RequestPlanReference *rpr;
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.
136 struct PendingRequestList *prl_head;
139 * Tail of list of associated pending requests.
141 struct PendingRequestList *prl_tail;
144 * Earliest time we'd be happy to (re)transmit this request.
146 struct GNUNET_TIME_Absolute earliest_transmission;
149 * When was the last time we transmitted this request to this peer? 0 for never.
151 struct GNUNET_TIME_Absolute last_transmission;
154 * Current priority for this request for this target.
159 * How often did we transmit this request to this peer?
161 unsigned int transmission_counter;
167 * Transmission plan for a peer.
172 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
174 struct GNUNET_CONTAINER_Heap *priority_heap;
177 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
179 struct GNUNET_CONTAINER_Heap *delay_heap;
182 * Map of queries to plan entries. All entries in the priority_heap or delay_heap
183 * should be in the plan map. Note that it IS possible for the plan map to have
184 * multiple entries for the same query.
186 struct GNUNET_CONTAINER_MultiHashMap *plan_map;
189 * Current transmission request handle.
191 struct GSF_PeerTransmitHandle *pth;
194 * Peer for which this is the plan.
196 struct GSF_ConnectedPeer *cp;
199 * Current task for executing the plan.
201 GNUNET_SCHEDULER_TaskIdentifier task;
206 * Hash map from peer identities to PeerPlans.
208 static struct GNUNET_CONTAINER_MultiHashMap *plans;
211 * Sum of all transmission counters (equals total delay for all plan entries).
213 static unsigned long long total_delay;
216 * Number of plan entries.
218 static unsigned long long plan_count;
222 * Return the query (key in the plan_map) for the given request plan.
224 * @param rp a request plan
225 * @return the associated query
227 static const struct GNUNET_HashCode *
228 get_rp_key (struct GSF_RequestPlan *rp)
230 return &GSF_pending_request_get_data_ (rp->prl_head->pr)->query;
235 * Figure out when and how to transmit to the given peer.
237 * @param cls the 'struct GSF_ConnectedPeer' for transmission
238 * @param tc scheduler context
241 schedule_peer_transmission (void *cls,
242 const struct GNUNET_SCHEDULER_TaskContext *tc);
246 * Insert the given request plan into the heap with the appropriate weight.
248 * @param pp associated peer's plan
249 * @param rp request to plan
252 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
254 #define N ((double)128.0)
256 * Running average delay we currently impose.
258 static double avg_delay;
260 struct GSF_PendingRequestData *prd;
261 struct GNUNET_TIME_Relative delay;
263 GNUNET_assert (rp->pp == pp);
264 GNUNET_STATISTICS_set (GSF_stats,
265 gettext_noop ("# average retransmission delay (ms)"),
266 total_delay * 1000LL / plan_count, GNUNET_NO);
267 prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
269 if (rp->transmission_counter < 8)
271 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
272 rp->transmission_counter);
273 else if (rp->transmission_counter < 32)
275 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
277 (1LL << (rp->transmission_counter - 8)));
280 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
283 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
284 delay.rel_value + 1);
285 /* Add 0.01 to avg_delay to avoid division-by-zero later */
286 avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value) / N) + 0.01;
289 * For the priority, we need to consider a few basic rules:
290 * 1) if we just started requesting (delay is small), we should
291 * virtually always have a priority of zero.
292 * 2) for requests with average latency, our priority should match
293 * the average priority observed on the network
294 * 3) even the longest-running requests should not be WAY out of
295 * the observed average (thus we bound by a factor of 2)
296 * 4) we add +1 to the observed average priority to avoid everyone
297 * staying put at zero (2 * 0 = 0...).
299 * Using the specific calculation below, we get:
301 * delay = 0 => priority = 0;
302 * delay = avg delay => priority = running-average-observed-priority;
303 * delay >> avg_delay => priority = 2 * running-average-observed-priority;
305 * which satisfies all of the rules above.
307 * Note: M_PI_4 = PI/4 = arctan(1)
310 round ((GSF_current_priorities +
311 1.0) * atan (delay.rel_value / avg_delay)) / M_PI_4;
312 /* Note: usage of 'round' and 'atan' requires -lm */
314 if (rp->transmission_counter != 0)
315 delay.rel_value += TTL_DECREMENT;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Considering (re)transmission number %u in %llu ms\n",
318 (unsigned int) rp->transmission_counter,
319 (unsigned long long) delay.rel_value);
320 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Earliest (re)transmission for `%s' in %us\n",
323 GNUNET_h2s (&prd->query), rp->transmission_counter);
324 GNUNET_assert (rp->hn == NULL);
325 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
327 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
330 GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
331 rp->earliest_transmission.abs_value);
332 GNUNET_assert (GNUNET_YES ==
333 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
336 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
337 GNUNET_SCHEDULER_cancel (pp->task);
338 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
344 * Get the pending request with the highest TTL from the given plan.
346 * @param rp plan to investigate
347 * @return pending request with highest TTL
349 struct GSF_PendingRequest *
350 get_latest (const struct GSF_RequestPlan *rp)
352 struct GSF_PendingRequest *ret;
353 struct PendingRequestList *prl;
360 if (GSF_pending_request_get_data_ (prl->pr)->ttl.abs_value >
361 GSF_pending_request_get_data_ (ret)->ttl.abs_value)
370 * Function called to get a message for transmission.
373 * @param buf_size number of bytes available in buf
374 * @param buf where to copy the message, NULL on error (peer disconnect)
375 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
378 transmit_message_callback (void *cls, size_t buf_size, void *buf)
380 struct PeerPlan *pp = cls;
381 struct GSF_RequestPlan *rp;
387 /* failed, try again... */
388 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
389 GNUNET_STATISTICS_update (GSF_stats,
391 ("# transmission failed (core has no bandwidth)"),
395 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
398 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
401 msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
402 if (msize > buf_size)
404 /* buffer to small (message changed), try again */
405 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
408 /* remove from root, add again elsewhere... */
409 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
411 rp->last_transmission = GNUNET_TIME_absolute_get ();
412 rp->transmission_counter++;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Executing plan %p executed %u times, planning retransmission\n",
416 rp, rp->transmission_counter);
418 GNUNET_STATISTICS_update (GSF_stats,
420 ("# query messages sent to other peers"), 1,
427 * Figure out when and how to transmit to the given peer.
429 * @param cls the 'struct PeerPlan'
430 * @param tc scheduler context
433 schedule_peer_transmission (void *cls,
434 const struct GNUNET_SCHEDULER_TaskContext *tc)
436 struct PeerPlan *pp = cls;
437 struct GSF_RequestPlan *rp;
439 struct GNUNET_TIME_Relative delay;
441 pp->task = GNUNET_SCHEDULER_NO_TASK;
444 GSF_peer_transmit_cancel_ (pp->pth);
447 /* move ready requests to priority queue */
448 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
449 (GNUNET_TIME_absolute_get_remaining
450 (rp->earliest_transmission).rel_value == 0))
452 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
453 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
455 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
457 /* priority heap (still) empty, check for delay... */
458 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
463 return; /* both queues empty */
465 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "Sleeping for %llu ms before retrying requests on plan %p.\n",
468 (unsigned long long) delay.rel_value, pp);
469 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
470 delay.rel_value, GNUNET_NO);
473 GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
476 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
478 /* process from priority heap */
479 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
481 GNUNET_assert (NULL != rp);
482 msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
484 GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
485 GNUNET_TIME_UNIT_FOREVER_REL, msize,
486 &transmit_message_callback, pp);
487 GNUNET_assert (NULL != pp->pth);
492 * Closure for 'merge_pr'.
497 struct GSF_PendingRequest *pr;
505 * Iterator that checks if an equivalent request is already
506 * present for this peer.
509 * @param query the query
510 * @param element request plan stored at the node
511 * @return GNUNET_YES if we should continue to iterate,
512 * GNUNET_NO if not (merge success)
515 merge_pr (void *cls, const struct GNUNET_HashCode * query, void *element)
517 struct MergeContext *mpr = cls;
518 struct GSF_RequestPlan *rp = element;
519 struct GSF_PendingRequestData *prd;
520 struct GSF_RequestPlanReference *rpr;
521 struct PendingRequestList *prl;
522 struct GSF_PendingRequest *latest;
525 GSF_pending_request_is_compatible_ (mpr->pr, rp->prl_head->pr))
527 /* merge new request with existing request plan */
528 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
529 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
534 prd = GSF_pending_request_get_data_ (mpr->pr);
535 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
536 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
537 mpr->merged = GNUNET_YES;
538 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
540 latest = get_latest (rp);
541 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
544 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
546 rp->transmission_counter = 0; /* reset */
553 * Create a new query plan entry.
555 * @param cp peer with the entry
556 * @param pr request with the entry
559 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
561 struct GNUNET_PeerIdentity id;
563 struct GSF_PendingRequestData *prd;
564 struct GSF_RequestPlan *rp;
565 struct GSF_RequestPlanReference *rpr;
566 struct PendingRequestList *prl;
567 struct MergeContext mpc;
569 GNUNET_assert (NULL != cp);
570 GSF_connected_peer_get_identity_ (cp, &id);
571 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
574 pp = GNUNET_malloc (sizeof (struct PeerPlan));
575 pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128);
577 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
579 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
581 GNUNET_CONTAINER_multihashmap_put (plans, &id.hashPubKey, pp,
582 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
584 mpc.merged = GNUNET_NO;
586 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
587 &GSF_pending_request_get_data_
588 (pr)->query, &merge_pr, &mpc); // 8 MB in 'merge_pr'
589 if (mpc.merged != GNUNET_NO)
591 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
592 &GSF_pending_request_get_data_
593 (pr)->query, &merge_pr, &mpc);
594 if (mpc.merged != GNUNET_NO)
597 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
599 prd = GSF_pending_request_get_data_ (pr);
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Planning transmission of query `%s' to peer `%s'\n",
602 GNUNET_h2s (&prd->query), GNUNET_i2s (&id));
603 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan)); // 8 MB
604 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
605 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
610 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
611 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
613 GNUNET_assert (GNUNET_YES ==
614 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); // 8 MB
617 plan (pp, rp); // +5 MB (plan/heap-insert)
622 * Notify the plan about a peer being no longer available;
623 * destroy all entries associated with this peer.
625 * @param cp connected peer
628 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
630 struct GNUNET_PeerIdentity id;
632 struct GSF_RequestPlan *rp;
633 struct GSF_PendingRequestData *prd;
634 struct PendingRequestList *prl;
636 GSF_connected_peer_get_identity_ (cp, &id);
637 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
639 return; /* nothing was ever planned for this peer */
640 GNUNET_assert (GNUNET_YES ==
641 GNUNET_CONTAINER_multihashmap_remove (plans, &id.hashPubKey,
645 GSF_peer_transmit_cancel_ (pp->pth);
648 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
650 GNUNET_SCHEDULER_cancel (pp->task);
651 pp->task = GNUNET_SCHEDULER_NO_TASK;
653 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
655 GNUNET_break (GNUNET_YES ==
656 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
657 get_rp_key (rp), rp));
658 while (NULL != (prl = rp->prl_head))
660 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
661 prd = GSF_pending_request_get_data_ (prl->pr);
662 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
663 GNUNET_free (prl->rpr);
669 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
670 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_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_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
687 plan_count, GNUNET_NO);
688 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
689 GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
694 * Get the last transmission attempt time for the request plan list
695 * referenced by 'rpr_head', that was sent to 'sender'
697 * @param rpr_head request plan reference list to check.
698 * @param sender the peer that we've sent the request to.
699 * @param result the timestamp to fill.
700 * @return GNUNET_YES if 'result' was changed, GNUNET_NO otherwise.
703 GSF_request_plan_reference_get_last_transmission_ (
704 struct GSF_RequestPlanReference *rpr_head, struct GSF_ConnectedPeer *sender,
705 struct GNUNET_TIME_Absolute *result)
707 struct GSF_RequestPlanReference *rpr;
708 for (rpr = rpr_head; rpr; rpr = rpr->next)
710 if (rpr->rp->pp->cp == sender)
712 *result = rpr->rp->last_transmission;
720 * Notify the plan about a request being done; destroy all entries
721 * associated with this request.
723 * @param pr request that is done
726 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
728 struct GSF_RequestPlan *rp;
729 struct GSF_PendingRequestData *prd;
730 struct GSF_RequestPlanReference *rpr;
732 prd = GSF_pending_request_get_data_ (pr);
733 while (NULL != (rpr = prd->rpr_head))
735 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
737 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
738 if (NULL == rp->prl_head)
740 GNUNET_CONTAINER_heap_remove_node (rp->hn);
742 GNUNET_break (GNUNET_YES ==
743 GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
744 &GSF_pending_request_get_data_
745 (rpr->prl->pr)->query,
749 GNUNET_free (rpr->prl);
752 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
753 plan_count, GNUNET_NO);
758 * Initialize plan subsystem.
763 plans = GNUNET_CONTAINER_multihashmap_create (256);
768 * Shutdown plan subsystem.
773 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
774 GNUNET_CONTAINER_multihashmap_destroy (plans);
779 /* end of gnunet-service-fs_pe.h */