-reduce calls in get_latest()
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pe.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/gnunet-service-fs_pe.c
23  * @brief API to manage query plan
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
31
32 /**
33  * Collect an instane number of statistics?  May cause excessive IPC.
34  */
35 #define INSANE_STATISTICS GNUNET_NO
36
37 /**
38  * List of GSF_PendingRequests this request plan
39  * participates with.
40  */
41 struct PendingRequestList;
42
43 /**
44  * Transmission plan for a peer.
45  */
46 struct PeerPlan;
47
48
49 /**
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.
55  *
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.)
60  *
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.)
65  */
66 struct GSF_PendingRequestPlanBijection
67 {
68
69   /**
70    * This is a doubly-linked list.
71    */
72   struct GSF_PendingRequestPlanBijection *next_PR;
73
74   /**
75    * This is a doubly-linked list.
76    */
77   struct GSF_PendingRequestPlanBijection *prev_PR;
78
79   /**
80    * This is a doubly-linked list.
81    */
82   struct GSF_PendingRequestPlanBijection *next_PE;
83
84   /**
85    * This is a doubly-linked list.
86    */
87   struct GSF_PendingRequestPlanBijection *prev_PE;
88
89   /**
90    * Associated request plan.
91    */
92   struct GSF_RequestPlan *rp;
93
94   /**
95    * Associated pending request.
96    */
97   struct GSF_PendingRequest *pr;
98
99 };
100
101
102 /**
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.
107  */
108 struct GSF_RequestPlan
109 {
110
111   /**
112    * This is a doubly-linked list.
113    */
114   struct GSF_RequestPlan *next;
115
116   /**
117    * This is a doubly-linked list.
118    */
119   struct GSF_RequestPlan *prev;
120
121   /**
122    * Heap node associated with this request and this peer.
123    */
124   struct GNUNET_CONTAINER_HeapNode *hn;
125
126   /**
127    * The transmission plan for a peer that this request is associated with.
128    */
129   struct PeerPlan *pp;
130
131   /**
132    * Head of list of associated pending requests.
133    */
134   struct GSF_PendingRequestPlanBijection *pe_head;
135
136   /**
137    * Tail of list of associated pending requests.
138    */
139   struct GSF_PendingRequestPlanBijection *pe_tail;
140
141   /**
142    * Earliest time we'd be happy to (re)transmit this request.
143    */
144   struct GNUNET_TIME_Absolute earliest_transmission;
145
146   /**
147    * When was the last time we transmitted this request to this peer? 0 for never.
148    */
149   struct GNUNET_TIME_Absolute last_transmission;
150
151   /**
152    * Current priority for this request for this target.
153    */
154   uint64_t priority;
155
156   /**
157    * How often did we transmit this request to this peer?
158    */
159   unsigned int transmission_counter;
160
161 };
162
163
164 /**
165  * Transmission plan for a peer.
166  */
167 struct PeerPlan
168 {
169   /**
170    * Heap with pending queries (struct GSF_RequestPlan), higher weights mean higher priority.
171    */
172   struct GNUNET_CONTAINER_Heap *priority_heap;
173
174   /**
175    * Heap with pending queries (struct GSF_RequestPlan), by transmission time, lowest first.
176    */
177   struct GNUNET_CONTAINER_Heap *delay_heap;
178
179   /**
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.
183    */
184   struct GNUNET_CONTAINER_MultiHashMap *plan_map;
185
186   /**
187    * Current transmission request handle.
188    */
189   struct GSF_PeerTransmitHandle *pth;
190
191   /**
192    * Peer for which this is the plan.
193    */
194   struct GSF_ConnectedPeer *cp;
195
196   /**
197    * Current task for executing the plan.
198    */
199   struct GNUNET_SCHEDULER_Task * task;
200 };
201
202
203 /**
204  * Hash map from peer identities to PeerPlans.
205  */
206 static struct GNUNET_CONTAINER_MultiPeerMap *plans;
207
208 /**
209  * Sum of all transmission counters (equals total delay for all plan entries).
210  */
211 static unsigned long long total_delay;
212
213 /**
214  * Number of plan entries.
215  */
216 static unsigned long long plan_count;
217
218
219 /**
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'
226  * given.
227  *
228  * @param rp a request plan
229  * @return the associated query
230  */
231 static const struct GNUNET_HashCode *
232 get_rp_key (struct GSF_RequestPlan *rp)
233 {
234   return &GSF_pending_request_get_data_ (rp->pe_head->pr)->query;
235 }
236
237
238 /**
239  * Figure out when and how to transmit to the given peer.
240  *
241  * @param cls the `struct GSF_ConnectedPeer` for transmission
242  * @param tc scheduler context
243  */
244 static void
245 schedule_peer_transmission (void *cls,
246                             const struct GNUNET_SCHEDULER_TaskContext *tc);
247
248
249 /**
250  * Insert the given request plan into the heap with the appropriate weight.
251  *
252  * @param pp associated peer's plan
253  * @param rp request to plan
254  */
255 static void
256 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
257 {
258 #define N ((double)128.0)
259   /**
260    * Running average delay we currently impose.
261    */
262   static double avg_delay;
263
264   struct GSF_PendingRequestData *prd;
265   struct GNUNET_TIME_Relative delay;
266
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);
272
273   if (rp->transmission_counter < 8)
274     delay =
275         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
276                                        rp->transmission_counter);
277   else if (rp->transmission_counter < 32)
278     delay =
279         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
280                                        8 +
281                                        (1LL << (rp->transmission_counter - 8)));
282   else
283     delay =
284         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
285                                        8 + (1LL << 24));
286   delay.rel_value_us =
287     GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
288                               delay.rel_value_us + 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_us) / N) + 0.01;
291
292   /*
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...).
302    *
303    * Using the specific calculation below, we get:
304    *
305    * delay = 0 => priority = 0;
306    * delay = avg delay => priority = running-average-observed-priority;
307    * delay >> avg_delay => priority = 2 * running-average-observed-priority;
308    *
309    * which satisfies all of the rules above.
310    *
311    * Note: M_PI_4 = PI/4 = arctan(1)
312    */
313   rp->priority =
314       round ((GSF_current_priorities +
315               1.0) * atan (delay.rel_value_us / avg_delay)) / M_PI_4;
316   /* Note: usage of 'round' and 'atan' requires -lm */
317
318   if (rp->transmission_counter != 0)
319     delay.rel_value_us += TTL_DECREMENT * 1000;
320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321               "Considering (re)transmission number %u in %s\n",
322               (unsigned int) rp->transmission_counter,
323               GNUNET_STRINGS_relative_time_to_string (delay,
324                                                       GNUNET_YES));
325   rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327               "Earliest (re)transmission for `%s' in %us\n",
328               GNUNET_h2s (&prd->query), rp->transmission_counter);
329   GNUNET_assert (rp->hn == NULL);
330   if (0 == GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value_us)
331     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
332   else
333     rp->hn =
334         GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
335                                       rp->earliest_transmission.abs_value_us);
336   GNUNET_assert (GNUNET_YES ==
337                  GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
338                                                                get_rp_key (rp),
339                                                                rp));
340   if (NULL != pp->task)
341     GNUNET_SCHEDULER_cancel (pp->task);
342   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
343 #undef N
344 }
345
346
347 /**
348  * Get the pending request with the highest TTL from the given plan.
349  *
350  * @param rp plan to investigate
351  * @return pending request with highest TTL
352  */
353 struct GSF_PendingRequest *
354 get_latest (const struct GSF_RequestPlan *rp)
355 {
356   struct GSF_PendingRequest *ret;
357   struct GSF_PendingRequestPlanBijection *bi;
358   const struct GSF_PendingRequestData *rprd;
359   const struct GSF_PendingRequestData *prd;
360
361   bi = rp->pe_head;
362   if (NULL == bi)
363     return NULL; /* should never happen */
364   ret = bi->pr;
365   rprd = GSF_pending_request_get_data_ (ret);
366   for (bi = bi->next_PE; NULL != bi; bi = bi->next_PE)
367   {
368     prd = GSF_pending_request_get_data_ (bi->pr);
369     if (prd->ttl.abs_value_us >
370         rprd->ttl.abs_value_us)
371     {
372       ret = bi->pr;
373       rprd = prd;
374     }
375   }
376   return ret;
377 }
378
379
380 /**
381  * Function called to get a message for transmission.
382  *
383  * @param cls closure
384  * @param buf_size number of bytes available in @a buf
385  * @param buf where to copy the message, NULL on error (peer disconnect)
386  * @return number of bytes copied to 'buf', can be 0 (without indicating an error)
387  */
388 static size_t
389 transmit_message_callback (void *cls, size_t buf_size, void *buf)
390 {
391   struct PeerPlan *pp = cls;
392   struct GSF_RequestPlan *rp;
393   size_t msize;
394
395   pp->pth = NULL;
396   if (NULL == buf)
397   {
398     /* failed, try again... */
399     if (NULL != pp->task)
400       GNUNET_SCHEDULER_cancel (pp->task);
401
402     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
403     GNUNET_STATISTICS_update (GSF_stats,
404                               gettext_noop
405                               ("# transmission failed (core has no bandwidth)"),
406                               1, GNUNET_NO);
407     return 0;
408   }
409   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
410   if (NULL == rp)
411   {
412     if (NULL != pp->task)
413       GNUNET_SCHEDULER_cancel (pp->task);
414     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
415     return 0;
416   }
417   msize = GSF_pending_request_get_message_ (get_latest (rp), buf_size, buf);
418   if (msize > buf_size)
419   {
420     if (NULL != pp->task)
421       GNUNET_SCHEDULER_cancel (pp->task);
422     /* buffer to small (message changed), try again */
423     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
424     return 0;
425   }
426   /* remove from root, add again elsewhere... */
427   GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
428   rp->hn = NULL;
429   rp->last_transmission = GNUNET_TIME_absolute_get ();
430   rp->transmission_counter++;
431   total_delay++;
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Executing plan %p executed %u times, planning retransmission\n",
434               rp, rp->transmission_counter);
435   plan (pp, rp);
436   GNUNET_STATISTICS_update (GSF_stats,
437                             gettext_noop
438                             ("# query messages sent to other peers"), 1,
439                             GNUNET_NO);
440   return msize;
441 }
442
443
444 /**
445  * Figure out when and how to transmit to the given peer.
446  *
447  * @param cls the `struct PeerPlan`
448  * @param tc scheduler context
449  */
450 static void
451 schedule_peer_transmission (void *cls,
452                             const struct GNUNET_SCHEDULER_TaskContext *tc)
453 {
454   struct PeerPlan *pp = cls;
455   struct GSF_RequestPlan *rp;
456   size_t msize;
457   struct GNUNET_TIME_Relative delay;
458
459   pp->task = NULL;
460   if (NULL != pp->pth)
461   {
462     GSF_peer_transmit_cancel_ (pp->pth);
463     pp->pth = NULL;
464   }
465   /* move ready requests to priority queue */
466   while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
467          (0 == GNUNET_TIME_absolute_get_remaining
468           (rp->earliest_transmission).rel_value_us))
469   {
470     GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
471     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
472   }
473   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
474   {
475     /* priority heap (still) empty, check for delay... */
476     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
477     if (NULL == rp)
478     {
479       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
480                   pp);
481       return;                   /* both queues empty */
482     }
483     delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
484     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485                 "Sleeping for %s before retrying requests on plan %p.\n",
486                 GNUNET_STRINGS_relative_time_to_string (delay,
487                                                         GNUNET_YES),
488                 pp);
489     GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout (ms)"),
490                            delay.rel_value_us / 1000LL, GNUNET_NO);
491
492     pp->task =
493         GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
494     return;
495   }
496 #if INSANE_STATISTICS
497   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
498                             1, GNUNET_NO);
499 #endif
500   /* process from priority heap */
501   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
502   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
503   GNUNET_assert (NULL != rp);
504   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
505   pp->pth =
506       GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
507                           GNUNET_TIME_UNIT_FOREVER_REL, msize,
508                           &transmit_message_callback, pp);
509   GNUNET_assert (NULL != pp->pth);
510 }
511
512
513 /**
514  * Closure for merge_pr().
515  */
516 struct MergeContext
517 {
518
519   struct GSF_PendingRequest *pr;
520
521   int merged;
522
523 };
524
525
526 /**
527  * Iterator that checks if an equivalent request is already
528  * present for this peer.
529  *
530  * @param cls closure
531  * @param query the query
532  * @param element request plan stored at the node
533  * @return #GNUNET_YES if we should continue to iterate,
534  *         #GNUNET_NO if not (merge success)
535  */
536 static int
537 merge_pr (void *cls,
538           const struct GNUNET_HashCode *query,
539           void *element)
540 {
541   struct MergeContext *mpr = cls;
542   struct GSF_RequestPlan *rp = element;
543   struct GSF_PendingRequestData *prd;
544   struct GSF_PendingRequestPlanBijection *bi;
545   struct GSF_PendingRequest *latest;
546
547   if (GNUNET_OK !=
548       GSF_pending_request_is_compatible_ (mpr->pr,
549                                           rp->pe_head->pr))
550     return GNUNET_YES;
551   /* merge new request with existing request plan */
552   bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
553   bi->rp = rp;
554   bi->pr = mpr->pr;
555   prd = GSF_pending_request_get_data_ (mpr->pr);
556   GNUNET_CONTAINER_MDLL_insert (PR,
557                                 prd->pr_head,
558                                 prd->pr_tail,
559                                 bi);
560   GNUNET_CONTAINER_MDLL_insert (PE,
561                                 rp->pe_head,
562                                 rp->pe_tail,
563                                 bi);
564   mpr->merged = GNUNET_YES;
565 #if INSANE_STATISTICS
566   GNUNET_STATISTICS_update (GSF_stats,
567                             gettext_noop ("# requests merged"), 1,
568                             GNUNET_NO);
569 #endif
570   latest = get_latest (rp);
571   if (GSF_pending_request_get_data_ (latest)->ttl.abs_value_us <
572       prd->ttl.abs_value_us)
573   {
574 #if INSANE_STATISTICS
575     GNUNET_STATISTICS_update (GSF_stats,
576                               gettext_noop ("# requests refreshed"),
577                               1, GNUNET_NO);
578 #endif
579     rp->transmission_counter = 0;       /* reset */
580   }
581   return GNUNET_NO;
582 }
583
584
585 /**
586  * Create a new query plan entry.
587  *
588  * @param cp peer with the entry
589  * @param pr request with the entry
590  */
591 void
592 GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
593                struct GSF_PendingRequest *pr)
594 {
595   const struct GNUNET_PeerIdentity *id;
596   struct PeerPlan *pp;
597   struct GSF_PendingRequestData *prd;
598   struct GSF_RequestPlan *rp;
599   struct GSF_PendingRequestPlanBijection *bi;
600   struct MergeContext mpc;
601
602   GNUNET_assert (NULL != cp);
603   id = GSF_connected_peer_get_identity2_ (cp);
604   pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
605   if (NULL == pp)
606   {
607     pp = GNUNET_new (struct PeerPlan);
608     pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
609     pp->priority_heap =
610         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
611     pp->delay_heap =
612         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
613     pp->cp = cp;
614     GNUNET_assert (GNUNET_OK ==
615                    GNUNET_CONTAINER_multipeermap_put (plans,
616                                                       id,
617                                                       pp,
618                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
619   }
620   mpc.merged = GNUNET_NO;
621   mpc.pr = pr;
622   prd = GSF_pending_request_get_data_ (pr);
623   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
624                                               &prd->query,
625                                               &merge_pr,
626                                               &mpc);
627   if (GNUNET_NO != mpc.merged)
628     return;
629   plan_count++;
630   GNUNET_STATISTICS_update (GSF_stats,
631                             gettext_noop ("# query plan entries"),
632                             1,
633                             GNUNET_NO);
634   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635               "Planning transmission of query `%s' to peer `%s'\n",
636               GNUNET_h2s (&prd->query),
637               GNUNET_i2s (id));
638   rp = GNUNET_new (struct GSF_RequestPlan);
639   bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
640   bi->rp = rp;
641   bi->pr = pr;
642   GNUNET_CONTAINER_MDLL_insert (PR,
643                                 prd->pr_head,
644                                 prd->pr_tail,
645                                 bi);
646   GNUNET_CONTAINER_MDLL_insert (PE,
647                                 rp->pe_head,
648                                 rp->pe_tail,
649                                 bi);
650   rp->pp = pp;
651   GNUNET_assert (GNUNET_YES ==
652                  GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
653                                                     get_rp_key (rp),
654                                                     rp,
655                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
656   plan (pp,
657         rp);
658 }
659
660
661 /**
662  * Notify the plan about a peer being no longer available;
663  * destroy all entries associated with this peer.
664  *
665  * @param cp connected peer
666  */
667 void
668 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
669 {
670   const struct GNUNET_PeerIdentity *id;
671   struct PeerPlan *pp;
672   struct GSF_RequestPlan *rp;
673   struct GSF_PendingRequestData *prd;
674   struct GSF_PendingRequestPlanBijection *bi;
675
676   id = GSF_connected_peer_get_identity2_ (cp);
677   pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
678   if (NULL == pp)
679     return;                     /* nothing was ever planned for this peer */
680   GNUNET_assert (GNUNET_YES ==
681                  GNUNET_CONTAINER_multipeermap_remove (plans, id,
682                                                        pp));
683   if (NULL != pp->pth)
684   {
685     GSF_peer_transmit_cancel_ (pp->pth);
686     pp->pth = NULL;
687   }
688   if (NULL != pp->task)
689   {
690     GNUNET_SCHEDULER_cancel (pp->task);
691     pp->task = NULL;
692   }
693   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
694   {
695     GNUNET_break (GNUNET_YES ==
696                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
697                                                         get_rp_key (rp), rp));
698     while (NULL != (bi = rp->pe_head))
699     {
700       GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
701       prd = GSF_pending_request_get_data_ (bi->pr);
702       GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
703       GNUNET_free (bi);
704     }
705     plan_count--;
706     GNUNET_free (rp);
707   }
708   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
709   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
710   {
711     GNUNET_break (GNUNET_YES ==
712                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
713                                                         get_rp_key (rp), rp));
714     while (NULL != (bi = rp->pe_head))
715     {
716       prd = GSF_pending_request_get_data_ (bi->pr);
717       GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
718       GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
719       GNUNET_free (bi);
720     }
721     plan_count--;
722     GNUNET_free (rp);
723   }
724   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
725                          plan_count, GNUNET_NO);
726   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
727   GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
728   GNUNET_free (pp);
729 }
730
731
732 /**
733  * Get the last transmission attempt time for the request plan list
734  * referenced by @a pr_head, that was sent to @a sender
735  *
736  * @param pr_head request plan reference list to check.
737  * @param sender the peer that we've sent the request to.
738  * @param result the timestamp to fill, set to #GNUNET_TIME_UNIT_FOREVER_ABS if never transmitted
739  * @return #GNUNET_YES if @a result was changed, #GNUNET_NO otherwise.
740  */
741 int
742 GSF_request_plan_reference_get_last_transmission_ (struct GSF_PendingRequestPlanBijection *pr_head,
743                                                    struct GSF_ConnectedPeer *sender,
744                                                    struct GNUNET_TIME_Absolute *result)
745 {
746   struct GSF_PendingRequestPlanBijection *bi;
747
748   for (bi = pr_head; NULL != bi; bi = bi->next_PR)
749   {
750     if (bi->rp->pp->cp == sender)
751     {
752       if (0 == bi->rp->last_transmission.abs_value_us)
753         *result = GNUNET_TIME_UNIT_FOREVER_ABS;
754       else
755         *result = bi->rp->last_transmission;
756       return GNUNET_YES;
757     }
758   }
759   return GNUNET_NO;
760 }
761
762
763 /**
764  * Notify the plan about a request being done; destroy all entries
765  * associated with this request.
766  *
767  * @param pr request that is done
768  */
769 void
770 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
771 {
772   struct GSF_RequestPlan *rp;
773   struct GSF_PendingRequestData *prd;
774   struct GSF_PendingRequestPlanBijection *bi;
775
776   prd = GSF_pending_request_get_data_ (pr);
777   while (NULL != (bi = prd->pr_head))
778   {
779     rp = bi->rp;
780     GNUNET_CONTAINER_MDLL_remove (PR, prd->pr_head, prd->pr_tail, bi);
781     GNUNET_CONTAINER_MDLL_remove (PE, rp->pe_head, rp->pe_tail, bi);
782     if (NULL == rp->pe_head)
783     {
784       GNUNET_CONTAINER_heap_remove_node (rp->hn);
785       plan_count--;
786       GNUNET_break (GNUNET_YES ==
787                     GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
788                                                           &GSF_pending_request_get_data_
789                                                           (bi->pr)->query,
790                                                           rp));
791       GNUNET_free (rp);
792     }
793     GNUNET_free (bi);
794   }
795   GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
796                          plan_count, GNUNET_NO);
797 }
798
799
800 /**
801  * Initialize plan subsystem.
802  */
803 void
804 GSF_plan_init ()
805 {
806   plans = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_YES);
807 }
808
809
810 /**
811  * Shutdown plan subsystem.
812  */
813 void
814 GSF_plan_done ()
815 {
816   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (plans));
817   GNUNET_CONTAINER_multipeermap_destroy (plans);
818 }
819
820
821
822 /* end of gnunet-service-fs_pe.h */