Fix for #4553
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pe.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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  */
248 static void
249 schedule_peer_transmission (void *cls);
250
251
252 /**
253  * Insert the given request plan into the heap with the appropriate weight.
254  *
255  * @param pp associated peer's plan
256  * @param rp request to plan
257  */
258 static void
259 plan (struct PeerPlan *pp,
260       struct GSF_RequestPlan *rp)
261 {
262 #define N ((double)128.0)
263   /**
264    * Running average delay we currently impose.
265    */
266   static double avg_delay;
267
268   struct GSF_PendingRequestData *prd;
269   struct GNUNET_TIME_Relative delay;
270
271   GNUNET_assert (rp->pp == pp);
272   GNUNET_STATISTICS_set (GSF_stats,
273                          gettext_noop ("# average retransmission delay (ms)"),
274                          total_delay * 1000LL / plan_count, GNUNET_NO);
275   prd = GSF_pending_request_get_data_ (rp->pe_head->pr);
276
277   if (rp->transmission_counter < 8)
278     delay =
279         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
280                                        rp->transmission_counter);
281   else if (rp->transmission_counter < 32)
282     delay =
283         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
284                                        8 +
285                                        (1LL << (rp->transmission_counter - 8)));
286   else
287     delay =
288         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
289                                        8 + (1LL << 24));
290   delay.rel_value_us =
291     GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
292                               delay.rel_value_us + 1);
293   /* Add 0.01 to avg_delay to avoid division-by-zero later */
294   avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value_us) / N) + 0.01;
295
296   /*
297    * For the priority, we need to consider a few basic rules:
298    * 1) if we just started requesting (delay is small), we should
299    * virtually always have a priority of zero.
300    * 2) for requests with average latency, our priority should match
301    * the average priority observed on the network
302    * 3) even the longest-running requests should not be WAY out of
303    * the observed average (thus we bound by a factor of 2)
304    * 4) we add +1 to the observed average priority to avoid everyone
305    * staying put at zero (2 * 0 = 0...).
306    *
307    * Using the specific calculation below, we get:
308    *
309    * delay = 0 => priority = 0;
310    * delay = avg delay => priority = running-average-observed-priority;
311    * delay >> avg_delay => priority = 2 * running-average-observed-priority;
312    *
313    * which satisfies all of the rules above.
314    *
315    * Note: M_PI_4 = PI/4 = arctan(1)
316    */
317   rp->priority =
318       round ((GSF_current_priorities +
319               1.0) * atan (delay.rel_value_us / avg_delay)) / M_PI_4;
320   /* Note: usage of 'round' and 'atan' requires -lm */
321
322   if (rp->transmission_counter != 0)
323     delay.rel_value_us += TTL_DECREMENT * 1000;
324   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325               "Considering (re)transmission number %u in %s\n",
326               (unsigned int) rp->transmission_counter,
327               GNUNET_STRINGS_relative_time_to_string (delay,
328                                                       GNUNET_YES));
329   rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331               "Earliest (re)transmission for `%s' in %us\n",
332               GNUNET_h2s (&prd->query), rp->transmission_counter);
333   GNUNET_assert (rp->hn == NULL);
334   if (0 == GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value_us)
335     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
336   else
337     rp->hn =
338         GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
339                                       rp->earliest_transmission.abs_value_us);
340   GNUNET_assert (GNUNET_YES ==
341                  GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
342                                                                get_rp_key (rp),
343                                                                rp));
344   if (NULL != pp->task)
345     GNUNET_SCHEDULER_cancel (pp->task);
346   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
347 #undef N
348 }
349
350
351 /**
352  * Get the pending request with the highest TTL from the given plan.
353  *
354  * @param rp plan to investigate
355  * @return pending request with highest TTL
356  */
357 struct GSF_PendingRequest *
358 get_latest (const struct GSF_RequestPlan *rp)
359 {
360   struct GSF_PendingRequest *ret;
361   struct GSF_PendingRequestPlanBijection *bi;
362   const struct GSF_PendingRequestData *rprd;
363   const struct GSF_PendingRequestData *prd;
364
365   bi = rp->pe_head;
366   if (NULL == bi)
367     return NULL; /* should never happen */
368   ret = bi->pr;
369   rprd = GSF_pending_request_get_data_ (ret);
370   for (bi = bi->next_PE; NULL != bi; bi = bi->next_PE)
371   {
372     GNUNET_break (GNUNET_YES ==
373                   GSF_pending_request_test_active_ (bi->pr));
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 ==
437                  GNUNET_CONTAINER_heap_remove_root (pp->priority_heap));
438   rp->hn = NULL;
439   rp->last_transmission = GNUNET_TIME_absolute_get ();
440   rp->transmission_counter++;
441   total_delay++;
442   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443               "Executing plan %p executed %u times, planning retransmission\n",
444               rp, rp->transmission_counter);
445   plan (pp, rp);
446   GNUNET_STATISTICS_update (GSF_stats,
447                             gettext_noop ("# query messages sent to other peers"),
448                             1,
449                             GNUNET_NO);
450   return msize;
451 }
452
453
454 /**
455  * Figure out when and how to transmit to the given peer.
456  *
457  * @param cls the `struct PeerPlan`
458  */
459 static void
460 schedule_peer_transmission (void *cls)
461 {
462   struct PeerPlan *pp = cls;
463   struct GSF_RequestPlan *rp;
464   size_t msize;
465   struct GNUNET_TIME_Relative delay;
466
467   pp->task = NULL;
468   if (NULL != pp->pth)
469   {
470     GSF_peer_transmit_cancel_ (pp->pth);
471     pp->pth = NULL;
472   }
473   /* move ready requests to priority queue */
474   while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
475          (0 == GNUNET_TIME_absolute_get_remaining
476           (rp->earliest_transmission).rel_value_us))
477   {
478     GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
479     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
480                                            rp,
481                                            rp->priority);
482   }
483   if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
484   {
485     /* priority heap (still) empty, check for delay... */
486     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
487     if (NULL == rp)
488     {
489       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490                   "No active requests for plan %p.\n",
491                   pp);
492       return;                   /* both queues empty */
493     }
494     delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
495     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496                 "Sleeping for %s before retrying requests on plan %p.\n",
497                 GNUNET_STRINGS_relative_time_to_string (delay,
498                                                         GNUNET_YES),
499                 pp);
500     GNUNET_STATISTICS_set (GSF_stats,
501                            gettext_noop ("# delay heap timeout (ms)"),
502                            delay.rel_value_us / 1000LL, GNUNET_NO);
503
504     pp->task =
505         GNUNET_SCHEDULER_add_delayed (delay,
506                                       &schedule_peer_transmission,
507                                       pp);
508     return;
509   }
510 #if INSANE_STATISTICS
511   GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
512                             1, GNUNET_NO);
513 #endif
514   /* process from priority heap */
515   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
516   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517               "Executing query plan %p\n",
518               rp);
519   GNUNET_assert (NULL != rp);
520   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
521   pp->pth =
522       GSF_peer_transmit_ (pp->cp, GNUNET_YES,
523                           rp->priority,
524                           GNUNET_TIME_UNIT_FOREVER_REL,
525                           msize,
526                           &transmit_message_callback, pp);
527   GNUNET_assert (NULL != pp->pth);
528 }
529
530
531 /**
532  * Closure for merge_pr().
533  */
534 struct MergeContext
535 {
536
537   /**
538    * Request we are trying to merge.
539    */
540   struct GSF_PendingRequest *pr;
541
542   /**
543    * Set to #GNUNET_YES if we succeeded to merge.
544    */
545   int merged;
546
547 };
548
549
550 /**
551  * Iterator that checks if an equivalent request is already
552  * present for this peer.
553  *
554  * @param cls closure
555  * @param query the query
556  * @param element request plan stored at the node
557  * @return #GNUNET_YES if we should continue to iterate,
558  *         #GNUNET_NO if not (merge success)
559  */
560 static int
561 merge_pr (void *cls,
562           const struct GNUNET_HashCode *query,
563           void *element)
564 {
565   struct MergeContext *mpr = cls;
566   struct GSF_RequestPlan *rp = element;
567   struct GSF_PendingRequestData *prd;
568   struct GSF_PendingRequestPlanBijection *bi;
569   struct GSF_PendingRequest *latest;
570
571   GNUNET_break (GNUNET_YES ==
572                 GSF_pending_request_test_active_ (mpr->pr));
573   if (GNUNET_OK !=
574       GSF_pending_request_is_compatible_ (mpr->pr,
575                                           rp->pe_head->pr))
576     return GNUNET_YES;
577   /* merge new request with existing request plan */
578   bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
579   bi->rp = rp;
580   bi->pr = mpr->pr;
581   prd = GSF_pending_request_get_data_ (mpr->pr);
582   GNUNET_CONTAINER_MDLL_insert (PR,
583                                 prd->pr_head,
584                                 prd->pr_tail,
585                                 bi);
586   GNUNET_CONTAINER_MDLL_insert (PE,
587                                 rp->pe_head,
588                                 rp->pe_tail,
589                                 bi);
590   mpr->merged = GNUNET_YES;
591 #if INSANE_STATISTICS
592   GNUNET_STATISTICS_update (GSF_stats,
593                             gettext_noop ("# requests merged"),
594                             1,
595                             GNUNET_NO);
596 #endif
597   latest = get_latest (rp);
598   if (GSF_pending_request_get_data_ (latest)->ttl.abs_value_us <
599       prd->ttl.abs_value_us)
600   {
601 #if INSANE_STATISTICS
602     GNUNET_STATISTICS_update (GSF_stats,
603                               gettext_noop ("# requests refreshed"),
604                               1,
605                               GNUNET_NO);
606 #endif
607     rp->transmission_counter = 0;       /* reset */
608   }
609   return GNUNET_NO;
610 }
611
612
613 /**
614  * Create a new query plan entry.
615  *
616  * @param cp peer with the entry
617  * @param pr request with the entry
618  */
619 void
620 GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
621                struct GSF_PendingRequest *pr)
622 {
623   const struct GNUNET_PeerIdentity *id;
624   struct PeerPlan *pp;
625   struct GSF_PendingRequestData *prd;
626   struct GSF_RequestPlan *rp;
627   struct GSF_PendingRequestPlanBijection *bi;
628   struct MergeContext mpc;
629
630   GNUNET_assert (GNUNET_YES ==
631                  GSF_pending_request_test_active_ (pr));
632   GNUNET_assert (NULL != cp);
633   id = GSF_connected_peer_get_identity2_ (cp);
634   pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
635   if (NULL == pp)
636   {
637     pp = GNUNET_new (struct PeerPlan);
638     pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
639     pp->priority_heap =
640         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
641     pp->delay_heap =
642         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
643     pp->cp = cp;
644     GNUNET_assert (GNUNET_OK ==
645                    GNUNET_CONTAINER_multipeermap_put (plans,
646                                                       id,
647                                                       pp,
648                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
649   }
650   mpc.merged = GNUNET_NO;
651   mpc.pr = pr;
652   prd = GSF_pending_request_get_data_ (pr);
653   GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
654                                               &prd->query,
655                                               &merge_pr,
656                                               &mpc);
657   if (GNUNET_NO != mpc.merged)
658     return;
659   plan_count++;
660   GNUNET_STATISTICS_update (GSF_stats,
661                             gettext_noop ("# query plan entries"),
662                             1,
663                             GNUNET_NO);
664   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665               "Planning transmission of query `%s' to peer `%s'\n",
666               GNUNET_h2s (&prd->query),
667               GNUNET_i2s (id));
668   rp = GNUNET_new (struct GSF_RequestPlan);
669   bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
670   bi->rp = rp;
671   bi->pr = pr;
672   GNUNET_CONTAINER_MDLL_insert (PR,
673                                 prd->pr_head,
674                                 prd->pr_tail,
675                                 bi);
676   GNUNET_CONTAINER_MDLL_insert (PE,
677                                 rp->pe_head,
678                                 rp->pe_tail,
679                                 bi);
680   rp->pp = pp;
681   GNUNET_assert (GNUNET_YES ==
682                  GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
683                                                     get_rp_key (rp),
684                                                     rp,
685                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
686   plan (pp,
687         rp);
688 }
689
690
691 /**
692  * Notify the plan about a peer being no longer available;
693  * destroy all entries associated with this peer.
694  *
695  * @param cp connected peer
696  */
697 void
698 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
699 {
700   const struct GNUNET_PeerIdentity *id;
701   struct PeerPlan *pp;
702   struct GSF_RequestPlan *rp;
703   struct GSF_PendingRequestData *prd;
704   struct GSF_PendingRequestPlanBijection *bi;
705
706   id = GSF_connected_peer_get_identity2_ (cp);
707   pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
708   if (NULL == pp)
709     return;                     /* nothing was ever planned for this peer */
710   GNUNET_assert (GNUNET_YES ==
711                  GNUNET_CONTAINER_multipeermap_remove (plans, id,
712                                                        pp));
713   if (NULL != pp->pth)
714   {
715     GSF_peer_transmit_cancel_ (pp->pth);
716     pp->pth = NULL;
717   }
718   if (NULL != pp->task)
719   {
720     GNUNET_SCHEDULER_cancel (pp->task);
721     pp->task = NULL;
722   }
723   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
724   {
725     GNUNET_break (GNUNET_YES ==
726                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
727                                                         get_rp_key (rp),
728                                                         rp));
729     while (NULL != (bi = rp->pe_head))
730     {
731       GNUNET_CONTAINER_MDLL_remove (PE,
732                                     rp->pe_head,
733                                     rp->pe_tail,
734                                     bi);
735       prd = GSF_pending_request_get_data_ (bi->pr);
736       GNUNET_CONTAINER_MDLL_remove (PR,
737                                     prd->pr_head,
738                                     prd->pr_tail,
739                                     bi);
740       GNUNET_free (bi);
741     }
742     plan_count--;
743     GNUNET_free (rp);
744   }
745   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
746   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
747   {
748     GNUNET_break (GNUNET_YES ==
749                   GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
750                                                         get_rp_key (rp),
751                                                         rp));
752     while (NULL != (bi = rp->pe_head))
753     {
754       prd = GSF_pending_request_get_data_ (bi->pr);
755       GNUNET_CONTAINER_MDLL_remove (PE,
756                                     rp->pe_head,
757                                     rp->pe_tail,
758                                     bi);
759       GNUNET_CONTAINER_MDLL_remove (PR,
760                                     prd->pr_head,
761                                     prd->pr_tail,
762                                     bi);
763       GNUNET_free (bi);
764     }
765     plan_count--;
766     GNUNET_free (rp);
767   }
768   GNUNET_STATISTICS_set (GSF_stats,
769                          gettext_noop ("# query plan entries"),
770                          plan_count,
771                          GNUNET_NO);
772   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
773   GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
774   GNUNET_free (pp);
775 }
776
777
778 /**
779  * Get the last transmission attempt time for the request plan list
780  * referenced by @a pr_head, that was sent to @a sender
781  *
782  * @param pr_head request plan reference list to check.
783  * @param sender the peer that we've sent the request to.
784  * @param result the timestamp to fill, set to #GNUNET_TIME_UNIT_FOREVER_ABS if never transmitted
785  * @return #GNUNET_YES if @a result was changed, #GNUNET_NO otherwise.
786  */
787 int
788 GSF_request_plan_reference_get_last_transmission_ (struct GSF_PendingRequestPlanBijection *pr_head,
789                                                    struct GSF_ConnectedPeer *sender,
790                                                    struct GNUNET_TIME_Absolute *result)
791 {
792   struct GSF_PendingRequestPlanBijection *bi;
793
794   for (bi = pr_head; NULL != bi; bi = bi->next_PR)
795   {
796     if (bi->rp->pp->cp == sender)
797     {
798       if (0 == bi->rp->last_transmission.abs_value_us)
799         *result = GNUNET_TIME_UNIT_FOREVER_ABS;
800       else
801         *result = bi->rp->last_transmission;
802       return GNUNET_YES;
803     }
804   }
805   return GNUNET_NO;
806 }
807
808
809 /**
810  * Notify the plan about a request being done; destroy all entries
811  * associated with this request.
812  *
813  * @param pr request that is done
814  */
815 void
816 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
817 {
818   struct GSF_RequestPlan *rp;
819   struct GSF_PendingRequestData *prd;
820   struct GSF_PendingRequestPlanBijection *bi;
821
822   prd = GSF_pending_request_get_data_ (pr);
823   while (NULL != (bi = prd->pr_head))
824   {
825     rp = bi->rp;
826     GNUNET_CONTAINER_MDLL_remove (PR,
827                                   prd->pr_head,
828                                   prd->pr_tail,
829                                   bi);
830     GNUNET_CONTAINER_MDLL_remove (PE,
831                                   rp->pe_head,
832                                   rp->pe_tail,
833                                   bi);
834     GNUNET_assert (bi->pr == pr);
835     if (NULL == rp->pe_head)
836     {
837       GNUNET_CONTAINER_heap_remove_node (rp->hn);
838       plan_count--;
839       GNUNET_break (GNUNET_YES ==
840                     GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
841                                                           &prd->query,
842                                                           rp));
843       GNUNET_free (rp);
844     }
845     GNUNET_free (bi);
846   }
847   GNUNET_STATISTICS_set (GSF_stats,
848                          gettext_noop ("# query plan entries"),
849                          plan_count,
850                          GNUNET_NO);
851 }
852
853
854 /**
855  * Initialize plan subsystem.
856  */
857 void
858 GSF_plan_init ()
859 {
860   plans = GNUNET_CONTAINER_multipeermap_create (256,
861                                                 GNUNET_YES);
862 }
863
864
865 /**
866  * Shutdown plan subsystem.
867  */
868 void
869 GSF_plan_done ()
870 {
871   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (plans));
872   GNUNET_CONTAINER_multipeermap_destroy (plans);
873 }
874
875
876
877 /* end of gnunet-service-fs_pe.h */