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