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;
41 * DLL of request plans a particular pending request is
44 struct GSF_RequestPlanReference
48 * This is a doubly-linked list.
50 struct GSF_RequestPlanReference *next;
53 * This is a doubly-linked list.
55 struct GSF_RequestPlanReference *prev;
58 * Associated request plan.
60 struct GSF_RequestPlan *rp;
63 * Corresponding PendingRequestList.
65 struct PendingRequestList *prl;
70 * List of GSF_PendingRequests this request plan
73 struct PendingRequestList
77 * This is a doubly-linked list.
79 struct PendingRequestList *next;
82 * This is a doubly-linked list.
84 struct PendingRequestList *prev;
87 * Array of associated pending requests.
89 struct GSF_PendingRequest *pr;
92 * Corresponding GSF_RequestPlanReference.
94 struct GSF_RequestPlanReference *rpr;
100 * Information we keep per request per peer. This is a doubly-linked
101 * list (with head and tail in the 'struct GSF_PendingRequestData')
102 * with one entry in each heap of each 'struct PeerPlan'. Each
103 * entry tracks information relevant for this request and this peer.
105 struct GSF_RequestPlan
109 * This is a doubly-linked list.
111 struct GSF_RequestPlan *next;
114 * This is a doubly-linked list.
116 struct GSF_RequestPlan *prev;
119 * Heap node associated with this request and this peer.
121 struct GNUNET_CONTAINER_HeapNode *hn;
124 * Head of list of associated pending requests.
126 struct PendingRequestList *prl_head;
129 * Tail of list of associated pending requests.
131 struct PendingRequestList *prl_tail;
134 * Earliest time we'd be happy to (re)transmit this request.
136 struct GNUNET_TIME_Absolute earliest_transmission;
139 * When was the last time we transmitted this request to this peer? 0 for never.
141 struct GNUNET_TIME_Absolute last_transmission;
144 * Current priority for this request for this target.
149 * How often did we transmit this request to this peer?
151 unsigned int transmission_counter;
157 * Transmission plan for a peer.
162 * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
164 struct GNUNET_CONTAINER_Heap *priority_heap;
167 * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
169 struct GNUNET_CONTAINER_Heap *delay_heap;
172 * Current transmission request handle.
174 struct GSF_PeerTransmitHandle *pth;
177 * Peer for which this is the plan.
179 struct GSF_ConnectedPeer *cp;
182 * Current task for executing the plan.
184 GNUNET_SCHEDULER_TaskIdentifier task;
189 * Hash map from peer identities to PeerPlans.
191 static struct GNUNET_CONTAINER_MultiHashMap *plans;
194 * Sum of all transmission counters (equals total delay for all plan entries).
196 static unsigned long long total_delay;
199 * Number of plan entries.
201 static unsigned long long plan_count;
205 * Figure out when and how to transmit to the given peer.
207 * @param cls the 'struct GSF_ConnectedPeer' for transmission
208 * @param tc scheduler context
211 schedule_peer_transmission (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc);
216 * Insert the given request plan into the heap with the appropriate weight.
218 * @param pp associated peer's plan
219 * @param rp request to plan
222 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
224 struct GSF_PendingRequestData *prd;
225 struct GNUNET_TIME_Relative delay;
227 GNUNET_STATISTICS_set (GSF_stats,
228 gettext_noop ("# average retransmission delay (ms)"),
229 total_delay * 1000LL / plan_count, GNUNET_NO);
230 prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
231 // FIXME: calculate 'rp->priority'!
232 if (rp->transmission_counter < 32)
234 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
235 1LL << rp->transmission_counter);
237 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, UINT_MAX);
238 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Earliest (re)transmission for `%s' in %us\n",
242 GNUNET_h2s (&prd->query), rp->transmission_counter);
245 GNUNET_assert (rp->hn == NULL);
246 if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
248 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
251 GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
252 rp->earliest_transmission.abs_value);
253 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
254 GNUNET_SCHEDULER_cancel (pp->task);
255 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
260 * Get the pending request with the highest TTL from the given plan.
262 * @param rp plan to investigate
263 * @return pending request with highest TTL
265 struct GSF_PendingRequest *
266 get_latest (const struct GSF_RequestPlan *rp)
268 struct GSF_PendingRequest *ret;
269 struct PendingRequestList *prl;
276 if (GSF_pending_request_get_data_ (prl->pr)->ttl.abs_value >
277 GSF_pending_request_get_data_ (ret)->ttl.abs_value)
286 * Function called to get a message for transmission.
289 * @param buf_size number of bytes available in buf
290 * @param buf where to copy the message, NULL on error (peer disconnect)
291 * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
294 transmit_message_callback (void *cls, size_t buf_size, void *buf)
296 struct PeerPlan *pp = cls;
297 struct GSF_RequestPlan *rp;
303 /* failed, try again... */
304 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
305 GNUNET_STATISTICS_update (GSF_stats,
306 gettext_noop ("# transmission failed (core has no bandwidth)"), 1,
310 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
313 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
316 msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
317 if (msize > buf_size)
319 /* buffer to small (message changed), try again */
320 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
323 /* remove from root, add again elsewhere... */
324 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
326 rp->last_transmission = GNUNET_TIME_absolute_get ();
327 rp->transmission_counter++;
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Executing plan %p executed %u times, planning retransmission\n",
332 rp, rp->transmission_counter);
335 GNUNET_STATISTICS_update (GSF_stats,
337 ("# queries messages sent to other peers"), 1,
344 * Figure out when and how to transmit to the given peer.
346 * @param cls the 'struct PeerPlan'
347 * @param tc scheduler context
350 schedule_peer_transmission (void *cls,
351 const struct GNUNET_SCHEDULER_TaskContext *tc)
353 struct PeerPlan *pp = cls;
354 struct GSF_RequestPlan *rp;
356 struct GNUNET_TIME_Relative delay;
358 pp->task = GNUNET_SCHEDULER_NO_TASK;
361 GSF_peer_transmit_cancel_ (pp->pth);
364 GNUNET_STATISTICS_set (GSF_stats,
365 gettext_noop ("# query delay heap size"),
366 GNUNET_CONTAINER_heap_get_size (pp->delay_heap),
368 GNUNET_STATISTICS_set (GSF_stats,
369 gettext_noop ("# query priority heap size"),
370 GNUNET_CONTAINER_heap_get_size (pp->priority_heap),
372 /* move ready requests to priority queue */
373 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
374 (GNUNET_TIME_absolute_get_remaining
375 (rp->earliest_transmission).rel_value == 0))
377 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
378 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
380 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
382 /* priority heap (still) empty, check for delay... */
383 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
390 return; /* both queues empty */
392 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Sleeping for %llu ms before retrying requests on plan %p.\n",
397 delay.rel_value, pp);
399 GNUNET_STATISTICS_set (GSF_stats,
400 gettext_noop ("# delay heap timeout"),
405 GNUNET_SCHEDULER_add_delayed (delay,
406 &schedule_peer_transmission, pp);
409 GNUNET_STATISTICS_update (GSF_stats,
410 gettext_noop ("# query plans executed"), 1,
412 /* process from priority heap */
413 rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
417 GNUNET_assert (NULL != rp);
418 msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
420 GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
421 GNUNET_TIME_UNIT_FOREVER_REL, msize,
422 &transmit_message_callback, pp);
423 GNUNET_assert (NULL != pp->pth);
428 * Closure for 'merge_pr'.
433 struct GSF_PendingRequest *pr;
441 * Iterator that checks if an equivalent request is already
442 * present for this peer.
445 * @param node internal node of the heap (ignored)
446 * @param element request plan stored at the node
447 * @param cost cost associated with the node (ignored)
448 * @return GNUNET_YES if we should continue to iterate,
449 * GNUNET_NO if not (merge success)
452 merge_pr (void *cls, struct GNUNET_CONTAINER_HeapNode *node, void *element,
453 GNUNET_CONTAINER_HeapCostType cost)
455 struct MergeContext *mpr = cls;
456 struct GSF_RequestPlan *rp = element;
457 struct GSF_PendingRequestData *prd;
458 struct GSF_RequestPlanReference *rpr;
459 struct PendingRequestList *prl;
460 struct GSF_PendingRequest *latest;
463 GSF_pending_request_is_compatible_ (mpr->pr, rp->prl_head->pr))
465 /* merge new request with existing request plan */
466 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
467 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
472 prd = GSF_pending_request_get_data_ (mpr->pr);
473 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
474 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
475 mpr->merged = GNUNET_YES;
476 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
478 latest = get_latest (rp);
479 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
482 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
484 rp->transmission_counter = 0; /* reset */
491 * Create a new query plan entry.
493 * @param cp peer with the entry
494 * @param pr request with the entry
497 GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
499 struct GNUNET_PeerIdentity id;
501 struct GSF_PendingRequestData *prd;
502 struct GSF_RequestPlan *rp;
503 struct GSF_RequestPlanReference *rpr;
504 struct PendingRequestList *prl;
505 struct MergeContext mpc;
507 GNUNET_assert (NULL != cp);
508 GSF_connected_peer_get_identity_ (cp, &id);
509 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
512 pp = GNUNET_malloc (sizeof (struct PeerPlan));
514 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
516 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
518 GNUNET_CONTAINER_multihashmap_put (plans, &id.hashPubKey, pp,
519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
521 mpc.merged = GNUNET_NO;
523 /* FIXME: O(n) call here, LRN reports this is a performance
524 problem. Try using hash map!? */
525 GNUNET_CONTAINER_heap_iterate (pp->priority_heap, &merge_pr, &mpc);
526 if (mpc.merged != GNUNET_NO)
528 GNUNET_CONTAINER_heap_iterate (pp->delay_heap, &merge_pr, &mpc);
529 if (mpc.merged != GNUNET_NO)
532 GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
534 prd = GSF_pending_request_get_data_ (pr);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Planning transmission of query `%s' to peer `%s'\n",
538 GNUNET_h2s (&prd->query), GNUNET_i2s (&id));
540 rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
541 rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
542 prl = GNUNET_malloc (sizeof (struct PendingRequestList));
547 GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
548 GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
554 * Notify the plan about a peer being no longer available;
555 * destroy all entries associated with this peer.
557 * @param cp connected peer
560 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
562 struct GNUNET_PeerIdentity id;
564 struct GSF_RequestPlan *rp;
565 struct GSF_PendingRequestData *prd;
566 struct PendingRequestList *prl;
568 GSF_connected_peer_get_identity_ (cp, &id);
569 pp = GNUNET_CONTAINER_multihashmap_get (plans, &id.hashPubKey);
571 return; /* nothing was ever planned for this peer */
572 GNUNET_assert (GNUNET_YES ==
573 GNUNET_CONTAINER_multihashmap_remove (plans, &id.hashPubKey,
576 GSF_peer_transmit_cancel_ (pp->pth);
577 if (GNUNET_SCHEDULER_NO_TASK != pp->task)
579 GNUNET_SCHEDULER_cancel (pp->task);
580 pp->task = GNUNET_SCHEDULER_NO_TASK;
582 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
584 while (NULL != (prl = rp->prl_head))
586 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
587 prd = GSF_pending_request_get_data_ (prl->pr);
588 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
589 GNUNET_free (prl->rpr);
594 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
595 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
597 while (NULL != (prl = rp->prl_head))
599 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
600 prd = GSF_pending_request_get_data_ (prl->pr);
601 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, prl->rpr);
602 GNUNET_free (prl->rpr);
607 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
608 plan_count, GNUNET_NO);
610 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
616 * Notify the plan about a request being done; destroy all entries
617 * associated with this request.
619 * @param pr request that is done
622 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
624 struct GSF_RequestPlan *rp;
625 struct GSF_PendingRequestData *prd;
626 struct GSF_RequestPlanReference *rpr;
628 prd = GSF_pending_request_get_data_ (pr);
629 while (NULL != (rpr = prd->rpr_head))
631 GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
633 GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
634 GNUNET_free (rpr->prl);
636 if (rp->prl_head == 0)
638 GNUNET_CONTAINER_heap_remove_node (rp->hn);
643 GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
644 plan_count, GNUNET_NO);
649 * Initialize plan subsystem.
654 plans = GNUNET_CONTAINER_multihashmap_create (256);
659 * Shutdown plan subsystem.
664 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (plans));
665 GNUNET_CONTAINER_multihashmap_destroy (plans);
670 /* end of gnunet-service-fs_pe.h */