2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
33 * Collect an instane number of statistics? May cause excessive IPC.
35 #define INSANE_STATISTICS GNUNET_NO
38 * List of GSF_PendingRequests this request plan
41 struct PendingRequestList;
44 * Transmission plan for a peer.
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.
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.)
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.)
66 struct GSF_PendingRequestPlanBijection
69 * This is a doubly-linked list.
71 struct GSF_PendingRequestPlanBijection *next_PR;
74 * This is a doubly-linked list.
76 struct GSF_PendingRequestPlanBijection *prev_PR;
79 * This is a doubly-linked list.
81 struct GSF_PendingRequestPlanBijection *next_PE;
84 * This is a doubly-linked list.
86 struct GSF_PendingRequestPlanBijection *prev_PE;
89 * Associated request plan (tells us one of the peers that
90 * we plan to forward the request to).
92 struct GSF_RequestPlan *rp;
95 * Associated pending request (identifies request details
96 * and one of the origins of the request).
98 struct GSF_PendingRequest *pr;
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.
108 struct GSF_RequestPlan
111 * This is a doubly-linked list.
113 struct GSF_RequestPlan *next;
116 * This is a doubly-linked list.
118 struct GSF_RequestPlan *prev;
121 * Heap node associated with this request and this peer.
123 struct GNUNET_CONTAINER_HeapNode *hn;
126 * The transmission plan for a peer that this request is associated with.
131 * Head of list of associated pending requests. This tells us
132 * which incoming requests from other peers this plan entry
135 struct GSF_PendingRequestPlanBijection *pe_head;
138 * Tail of list of associated pending requests.
140 struct GSF_PendingRequestPlanBijection *pe_tail;
143 * Earliest time we'd be happy to (re)transmit this request.
145 struct GNUNET_TIME_Absolute earliest_transmission;
148 * When was the last time we transmitted this request to this peer? 0 for never.
150 struct GNUNET_TIME_Absolute last_transmission;
153 * Current priority for this request for this target.
158 * How often did we transmit this request to this peer?
160 unsigned int transmission_counter;
165 * Transmission plan for a peer.
170 * Heap with pending queries (`struct GSF_RequestPlan`), higher weights mean higher priority.
172 struct GNUNET_CONTAINER_Heap *priority_heap;
175 * Heap with pending queries (`struct GSF_RequestPlan`), by transmission time, lowest first.
177 struct GNUNET_CONTAINER_Heap *delay_heap;
180 * Map of queries to plan entries. All entries in the @e priority_heap
181 * or @e delay_heap should be in the @e plan_map. Note that it is
182 * possible for the @e plan_map to have multiple entries for the same
185 struct GNUNET_CONTAINER_MultiHashMap *plan_map;
188 * Peer for which this is the plan.
190 struct GSF_ConnectedPeer *cp;
193 * Current task for executing the plan.
195 struct GNUNET_SCHEDULER_Task *task;
198 * Current message under transmission for the plan.
200 struct GNUNET_MQ_Envelope *env;
205 * Hash map from peer identities to PeerPlans.
207 static struct GNUNET_CONTAINER_MultiPeerMap *plans;
210 * Sum of all transmission counters (equals total delay for all plan entries).
212 static unsigned long long total_delay;
215 * Number of plan entries.
217 static unsigned long long plan_count;
221 * Return the query (key in the plan_map) for the given request plan.
222 * Note that this key may change as there can be multiple pending
223 * requests for the same key and we just return _one_ of them; this
224 * particular one might complete while another one might still be
225 * active, hence the lifetime of the returned hash code is NOT
226 * necessarily identical to that of the `struct GSF_RequestPlan`
229 * @param rp a request plan
230 * @return the associated query
232 static const struct GNUNET_HashCode *
233 get_rp_key (struct GSF_RequestPlan *rp)
235 return &GSF_pending_request_get_data_ (rp->pe_head->pr)->query;
240 * Insert the given request plan into the heap with the appropriate weight.
242 * @param pp associated peer's plan
243 * @param rp request to plan
246 plan (struct PeerPlan *pp,
247 struct GSF_RequestPlan *rp)
249 #define N ((double) 128.0)
251 * Running average delay we currently impose.
253 static double avg_delay;
255 struct GSF_PendingRequestData *prd;
256 struct GNUNET_TIME_Relative delay;
258 GNUNET_assert (rp->pp == pp);
259 GNUNET_STATISTICS_set (GSF_stats,
260 gettext_noop ("# average retransmission delay (ms)"),
261 total_delay * 1000LL / plan_count, GNUNET_NO);
262 prd = GSF_pending_request_get_data_ (rp->pe_head->pr);
264 if (rp->transmission_counter < 8)
266 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
267 rp->transmission_counter);
268 else if (rp->transmission_counter < 32)
270 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
272 + (1LL << (rp->transmission_counter - 8)));
275 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
278 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
279 delay.rel_value_us + 1);
280 /* Add 0.01 to avg_delay to avoid division-by-zero later */
281 avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value_us) / N) + 0.01;
284 * For the priority, we need to consider a few basic rules:
285 * 1) if we just started requesting (delay is small), we should
286 * virtually always have a priority of zero.
287 * 2) for requests with average latency, our priority should match
288 * the average priority observed on the network
289 * 3) even the longest-running requests should not be WAY out of
290 * the observed average (thus we bound by a factor of 2)
291 * 4) we add +1 to the observed average priority to avoid everyone
292 * staying put at zero (2 * 0 = 0...).
294 * Using the specific calculation below, we get:
296 * delay = 0 => priority = 0;
297 * delay = avg delay => priority = running-average-observed-priority;
298 * delay >> avg_delay => priority = 2 * running-average-observed-priority;
300 * which satisfies all of the rules above.
302 * Note: M_PI_4 = PI/4 = arctan(1)
304 round ((GSF_current_priorities
305 + 1.0) * atan (delay.rel_value_us / avg_delay)) / M_PI_4;
306 /* Note: usage of 'round' and 'atan' requires -lm */
308 if (rp->transmission_counter != 0)
309 delay.rel_value_us += TTL_DECREMENT * 1000;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Considering (re)transmission number %u in %s\n",
312 (unsigned int) rp->transmission_counter,
313 GNUNET_STRINGS_relative_time_to_string (delay,
315 rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Earliest (re)transmission for `%s' in %us\n",
318 GNUNET_h2s (&prd->query),
319 rp->transmission_counter);
320 GNUNET_assert (rp->hn == NULL);
321 if (0 == GNUNET_TIME_absolute_get_remaining (
322 rp->earliest_transmission).rel_value_us)
323 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
328 GNUNET_CONTAINER_heap_insert (pp->delay_heap,
330 rp->earliest_transmission.abs_value_us);
331 GNUNET_assert (GNUNET_YES ==
332 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
340 * Get the pending request with the highest TTL from the given plan.
342 * @param rp plan to investigate
343 * @return pending request with highest TTL
345 struct GSF_PendingRequest *
346 get_latest (const struct GSF_RequestPlan *rp)
348 struct GSF_PendingRequest *ret;
349 struct GSF_PendingRequestPlanBijection *bi;
350 const struct GSF_PendingRequestData *rprd;
351 const struct GSF_PendingRequestData *prd;
355 return NULL; /* should never happen */
357 rprd = GSF_pending_request_get_data_ (ret);
358 for (bi = bi->next_PE; NULL != bi; bi = bi->next_PE)
360 GNUNET_break (GNUNET_YES ==
361 GSF_pending_request_test_active_ (bi->pr));
362 prd = GSF_pending_request_get_data_ (bi->pr);
363 if (prd->ttl.abs_value_us > rprd->ttl.abs_value_us)
374 * Figure out when and how to transmit to the given peer.
376 * @param cls the `struct PeerPlan`
379 schedule_peer_transmission (void *cls)
381 struct PeerPlan *pp = cls;
382 struct GSF_RequestPlan *rp;
383 struct GNUNET_TIME_Relative delay;
385 if (NULL != pp->task)
391 GNUNET_assert (NULL != pp->env);
394 /* move ready requests to priority queue */
395 while ((NULL != (rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap))) &&
396 (0 == GNUNET_TIME_absolute_get_remaining
397 (rp->earliest_transmission).rel_value_us))
399 GNUNET_assert (rp == GNUNET_CONTAINER_heap_remove_root (pp->delay_heap));
400 rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap,
404 if (0 == GNUNET_CONTAINER_heap_get_size (pp->priority_heap))
406 /* priority heap (still) empty, check for delay... */
407 rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "No active requests for plan %p.\n",
413 return; /* both queues empty */
415 delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Sleeping for %s before retrying requests on plan %p.\n",
418 GNUNET_STRINGS_relative_time_to_string (delay,
421 GNUNET_STATISTICS_set (GSF_stats,
422 gettext_noop ("# delay heap timeout (ms)"),
423 delay.rel_value_us / 1000LL, GNUNET_NO);
426 = GNUNET_SCHEDULER_add_at (rp->earliest_transmission,
427 &schedule_peer_transmission,
431 #if INSANE_STATISTICS
432 GNUNET_STATISTICS_update (GSF_stats,
433 gettext_noop ("# query plans executed"),
437 /* process from priority heap */
438 rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Executing query plan %p\n",
442 GNUNET_assert (NULL != rp);
444 rp->last_transmission = GNUNET_TIME_absolute_get ();
445 rp->transmission_counter++;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Executing plan %p executed %u times, planning retransmission\n",
450 rp->transmission_counter);
451 GNUNET_assert (NULL == pp->env);
452 pp->env = GSF_pending_request_get_message_ (get_latest (rp));
453 GNUNET_MQ_notify_sent (pp->env,
454 &schedule_peer_transmission,
456 GSF_peer_transmit_ (pp->cp,
460 GNUNET_STATISTICS_update (GSF_stats,
462 "# query messages sent to other peers"),
471 * Closure for merge_pr().
476 * Request we are trying to merge.
478 struct GSF_PendingRequest *pr;
481 * Set to #GNUNET_YES if we succeeded to merge.
488 * Iterator that checks if an equivalent request is already
489 * present for this peer.
492 * @param query the query
493 * @param element request plan stored at the node
494 * @return #GNUNET_YES if we should continue to iterate,
495 * #GNUNET_NO if not (merge success)
499 const struct GNUNET_HashCode *query,
502 struct MergeContext *mpr = cls;
503 struct GSF_RequestPlan *rp = element;
504 struct GSF_PendingRequestData *prd;
505 struct GSF_PendingRequestPlanBijection *bi;
506 struct GSF_PendingRequest *latest;
508 GNUNET_break (GNUNET_YES ==
509 GSF_pending_request_test_active_ (mpr->pr));
511 GSF_pending_request_is_compatible_ (mpr->pr,
514 /* merge new request with existing request plan */
515 bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
518 prd = GSF_pending_request_get_data_ (mpr->pr);
519 GNUNET_CONTAINER_MDLL_insert (PR,
523 GNUNET_CONTAINER_MDLL_insert (PE,
527 mpr->merged = GNUNET_YES;
528 #if INSANE_STATISTICS
529 GNUNET_STATISTICS_update (GSF_stats,
530 gettext_noop ("# requests merged"),
534 latest = get_latest (rp);
535 if (GSF_pending_request_get_data_ (latest)->ttl.abs_value_us <
536 prd->ttl.abs_value_us)
538 #if INSANE_STATISTICS
539 GNUNET_STATISTICS_update (GSF_stats,
540 gettext_noop ("# requests refreshed"),
544 rp->transmission_counter = 0; /* reset */
551 * Create a new query plan entry.
553 * @param cp peer with the entry
554 * @param pr request with the entry
557 GSF_plan_add_ (struct GSF_ConnectedPeer *cp,
558 struct GSF_PendingRequest *pr)
560 const struct GNUNET_PeerIdentity *id;
562 struct GSF_PendingRequestData *prd;
563 struct GSF_RequestPlan *rp;
564 struct GSF_PendingRequestPlanBijection *bi;
565 struct MergeContext mpc;
567 GNUNET_assert (GNUNET_YES ==
568 GSF_pending_request_test_active_ (pr));
569 GNUNET_assert (NULL != cp);
570 id = GSF_connected_peer_get_identity2_ (cp);
571 pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
574 pp = GNUNET_new (struct PeerPlan);
575 pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
577 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
579 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
581 GNUNET_assert (GNUNET_OK ==
582 GNUNET_CONTAINER_multipeermap_put (plans,
585 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
586 pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission,
589 mpc.merged = GNUNET_NO;
591 prd = GSF_pending_request_get_data_ (pr);
592 GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
596 if (GNUNET_NO != mpc.merged)
599 GNUNET_STATISTICS_update (GSF_stats,
600 gettext_noop ("# query plan entries"),
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Planning transmission of query `%s' to peer `%s'\n",
605 GNUNET_h2s (&prd->query),
607 rp = GNUNET_new (struct GSF_RequestPlan);
608 bi = GNUNET_new (struct GSF_PendingRequestPlanBijection);
611 GNUNET_CONTAINER_MDLL_insert (PR,
615 GNUNET_CONTAINER_MDLL_insert (PE,
620 GNUNET_assert (GNUNET_YES ==
621 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
624 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
631 * Notify the plan about a peer being no longer available;
632 * destroy all entries associated with this peer.
634 * @param cp connected peer
637 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
639 const struct GNUNET_PeerIdentity *id;
641 struct GSF_RequestPlan *rp;
642 struct GSF_PendingRequestData *prd;
643 struct GSF_PendingRequestPlanBijection *bi;
645 id = GSF_connected_peer_get_identity2_ (cp);
646 pp = GNUNET_CONTAINER_multipeermap_get (plans, id);
648 return; /* nothing was ever planned for this peer */
649 GNUNET_assert (GNUNET_YES ==
650 GNUNET_CONTAINER_multipeermap_remove (plans, id,
652 if (NULL != pp->task)
654 GNUNET_SCHEDULER_cancel (pp->task);
657 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
659 GNUNET_break (GNUNET_YES ==
660 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
663 while (NULL != (bi = rp->pe_head))
665 GNUNET_CONTAINER_MDLL_remove (PE,
669 prd = GSF_pending_request_get_data_ (bi->pr);
670 GNUNET_CONTAINER_MDLL_remove (PR,
679 GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
680 while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
682 GNUNET_break (GNUNET_YES ==
683 GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
686 while (NULL != (bi = rp->pe_head))
688 prd = GSF_pending_request_get_data_ (bi->pr);
689 GNUNET_CONTAINER_MDLL_remove (PE,
693 GNUNET_CONTAINER_MDLL_remove (PR,
702 GNUNET_STATISTICS_set (GSF_stats,
703 gettext_noop ("# query plan entries"),
706 GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
707 GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
713 * Get the last transmission attempt time for the request plan list
714 * referenced by @a pr_head, that was sent to @a sender
716 * @param pr_head request plan reference list to check.
717 * @param sender the peer that we've sent the request to.
718 * @param result the timestamp to fill, set to #GNUNET_TIME_UNIT_FOREVER_ABS if never transmitted
719 * @return #GNUNET_YES if @a result was changed, #GNUNET_NO otherwise.
722 GSF_request_plan_reference_get_last_transmission_ (struct
723 GSF_PendingRequestPlanBijection
725 struct GSF_ConnectedPeer *
727 struct GNUNET_TIME_Absolute *
730 struct GSF_PendingRequestPlanBijection *bi;
732 for (bi = pr_head; NULL != bi; bi = bi->next_PR)
734 if (bi->rp->pp->cp == sender)
736 if (0 == bi->rp->last_transmission.abs_value_us)
737 *result = GNUNET_TIME_UNIT_FOREVER_ABS;
739 *result = bi->rp->last_transmission;
748 * Notify the plan about a request being done; destroy all entries
749 * associated with this request.
751 * @param pr request that is done
754 GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
756 struct GSF_RequestPlan *rp;
757 struct GSF_PendingRequestData *prd;
758 struct GSF_PendingRequestPlanBijection *bi;
760 prd = GSF_pending_request_get_data_ (pr);
761 while (NULL != (bi = prd->pr_head))
764 GNUNET_CONTAINER_MDLL_remove (PR,
768 GNUNET_CONTAINER_MDLL_remove (PE,
772 GNUNET_assert (bi->pr == pr);
773 if (NULL == rp->pe_head)
775 GNUNET_CONTAINER_heap_remove_node (rp->hn);
777 GNUNET_break (GNUNET_YES ==
778 GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
785 GNUNET_STATISTICS_set (GSF_stats,
786 gettext_noop ("# query plan entries"),
793 * Initialize plan subsystem.
798 plans = GNUNET_CONTAINER_multipeermap_create (256,
804 * Shutdown plan subsystem.
809 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (plans));
810 GNUNET_CONTAINER_multipeermap_destroy (plans);
814 /* end of gnunet-service-fs_pe.h */