-check return values, fix leak
[oweals/gnunet.git] / src / fs / gnunet-service-fs_pe.c
index d3ab5026beafe3cb873be21022bab14bed371bcf..71b0fc091571ece75c9234c176ede7fa6305aae4 100644 (file)
 
 
 /**
- * List of GSF_PendingRequests this request plan 
+ * List of GSF_PendingRequests this request plan
  * participates with.
  */
 struct PendingRequestList;
 
+/**
+ * Transmission plan for a peer.
+ */
+struct PeerPlan;
+
 
 /**
  * DLL of request plans a particular pending request is
@@ -67,7 +72,7 @@ struct GSF_RequestPlanReference
 
 
 /**
- * List of GSF_PendingRequests this request plan 
+ * List of GSF_PendingRequests this request plan
  * participates with.
  */
 struct PendingRequestList
@@ -84,7 +89,7 @@ struct PendingRequestList
   struct PendingRequestList *prev;
 
   /**
-   * Array of associated pending requests.
+   * Associated pending request.
    */
   struct GSF_PendingRequest *pr;
 
@@ -120,6 +125,11 @@ struct GSF_RequestPlan
    */
   struct GNUNET_CONTAINER_HeapNode *hn;
 
+  /**
+   * The transmission plan for a peer that this request is associated with.
+   */
+  struct PeerPlan *pp;
+
   /**
    * Head of list of associated pending requests.
    */
@@ -168,6 +178,13 @@ struct PeerPlan
    */
   struct GNUNET_CONTAINER_Heap *delay_heap;
 
+  /**
+   * Map of queries to plan entries.  All entries in the priority_heap or delay_heap
+   * should be in the plan map.  Note that it IS possible for the plan map to have
+   * multiple entries for the same query.
+   */
+  struct GNUNET_CONTAINER_MultiHashMap *plan_map;
+
   /**
    * Current transmission request handle.
    */
@@ -201,6 +218,19 @@ static unsigned long long total_delay;
 static unsigned long long plan_count;
 
 
+/**
+ * Return the query (key in the plan_map) for the given request plan.
+ *
+ * @param rp a request plan
+ * @return the associated query
+ */
+static const GNUNET_HashCode *
+get_rp_key (struct GSF_RequestPlan *rp)
+{
+  return &GSF_pending_request_get_data_ (rp->prl_head->pr)->query;
+}
+
+
 /**
  * Figure out when and how to transmit to the given peer.
  *
@@ -221,37 +251,92 @@ schedule_peer_transmission (void *cls,
 static void
 plan (struct PeerPlan *pp, struct GSF_RequestPlan *rp)
 {
+#define N ((double)128.0)
+  /**
+   * Running average delay we currently impose.
+   */
+  static double avg_delay;
+
   struct GSF_PendingRequestData *prd;
   struct GNUNET_TIME_Relative delay;
 
+  GNUNET_assert (rp->pp == pp);
   GNUNET_STATISTICS_set (GSF_stats,
                          gettext_noop ("# average retransmission delay (ms)"),
                          total_delay * 1000LL / plan_count, GNUNET_NO);
   prd = GSF_pending_request_get_data_ (rp->prl_head->pr);
-  // FIXME: calculate 'rp->priority'!  
-  if (rp->transmission_counter < 32)
-    delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
-                                           1LL << rp->transmission_counter);
+
+  if (rp->transmission_counter < 8)
+    delay =
+        GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+                                       rp->transmission_counter);
+  else if (rp->transmission_counter < 32)
+    delay =
+        GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+                                       8 +
+                                       (1LL << (rp->transmission_counter - 8)));
   else
-    delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, UINT_MAX);
+    delay =
+        GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
+                                       8 + (1LL << 24));
+  delay.rel_value =
+      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                delay.rel_value + 1);
+  /* Add 0.01 to avg_delay to avoid division-by-zero later */
+  avg_delay = (((avg_delay * (N - 1.0)) + delay.rel_value) / N) + 0.01;
+
+  /*
+   * For the priority, we need to consider a few basic rules:
+   * 1) if we just started requesting (delay is small), we should
+   * virtually always have a priority of zero.
+   * 2) for requests with average latency, our priority should match
+   * the average priority observed on the network
+   * 3) even the longest-running requests should not be WAY out of
+   * the observed average (thus we bound by a factor of 2)
+   * 4) we add +1 to the observed average priority to avoid everyone
+   * staying put at zero (2 * 0 = 0...).
+   *
+   * Using the specific calculation below, we get:
+   *
+   * delay = 0 => priority = 0;
+   * delay = avg delay => priority = running-average-observed-priority;
+   * delay >> avg_delay => priority = 2 * running-average-observed-priority;
+   *
+   * which satisfies all of the rules above.
+   *
+   * Note: M_PI_4 = PI/4 = arctan(1)
+   */
+  rp->priority =
+      round ((GSF_current_priorities +
+              1.0) * atan (delay.rel_value / avg_delay)) / M_PI_4;
+  /* Note: usage of 'round' and 'atan' requires -lm */
+
+  if (rp->transmission_counter != 0)
+    delay.rel_value += TTL_DECREMENT;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Considering (re)transmission number %u in %llu ms\n",
+              (unsigned int) rp->transmission_counter,
+              (unsigned long long) delay.rel_value);
   rp->earliest_transmission = GNUNET_TIME_relative_to_absolute (delay);
-#if DEBUG_FS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Earliest (re)transmission for `%s' in %us\n",
               GNUNET_h2s (&prd->query), rp->transmission_counter);
-#endif
-
   GNUNET_assert (rp->hn == NULL);
   if (GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission).rel_value
       == 0)
     rp->hn = GNUNET_CONTAINER_heap_insert (pp->priority_heap, rp, rp->priority);
   else
-    rp->hn = GNUNET_CONTAINER_heap_insert (pp->delay_heap,
-                                           rp,
-                                           rp->earliest_transmission.abs_value);
+    rp->hn =
+        GNUNET_CONTAINER_heap_insert (pp->delay_heap, rp,
+                                      rp->earliest_transmission.abs_value);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_contains_value (pp->plan_map,
+                                                               get_rp_key (rp),
+                                                               rp));
   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
     GNUNET_SCHEDULER_cancel (pp->task);
   pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
+#undef N
 }
 
 
@@ -301,6 +386,10 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
   {
     /* failed, try again... */
     pp->task = GNUNET_SCHEDULER_add_now (&schedule_peer_transmission, pp);
+    GNUNET_STATISTICS_update (GSF_stats,
+                              gettext_noop
+                              ("# transmission failed (core has no bandwidth)"),
+                              1, GNUNET_NO);
     return 0;
   }
   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
@@ -322,15 +411,13 @@ transmit_message_callback (void *cls, size_t buf_size, void *buf)
   rp->last_transmission = GNUNET_TIME_absolute_get ();
   rp->transmission_counter++;
   total_delay++;
-#if DEBUG_FS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Executing plan %p executed %u times, planning retransmission\n",
               rp, rp->transmission_counter);
-#endif
   plan (pp, rp);
   GNUNET_STATISTICS_update (GSF_stats,
                             gettext_noop
-                            ("# queries messages sent to other peers"), 1,
+                            ("# query messages sent to other peers"), 1,
                             GNUNET_NO);
   return msize;
 }
@@ -349,6 +436,7 @@ schedule_peer_transmission (void *cls,
   struct PeerPlan *pp = cls;
   struct GSF_RequestPlan *rp;
   size_t msize;
+  struct GNUNET_TIME_Relative delay;
 
   pp->task = GNUNET_SCHEDULER_NO_TASK;
   if (pp->pth != NULL)
@@ -370,37 +458,32 @@ schedule_peer_transmission (void *cls,
     rp = GNUNET_CONTAINER_heap_peek (pp->delay_heap);
     if (NULL == rp)
     {
-#if DEBUG_FS
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "No active requests for plan %p.\n", pp);
-#endif
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No active requests for plan %p.\n",
+                  pp);
       return;                   /* both queues empty */
     }
-#if DEBUG_FS
+    delay = GNUNET_TIME_absolute_get_remaining (rp->earliest_transmission);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Sleeping for %llu ms before retrying requests on plan %p.\n",
-                (unsigned long long)
-                GNUNET_TIME_absolute_get_remaining
-                (rp->earliest_transmission).rel_value, pp);
-#endif
+                (unsigned long long) delay.rel_value, pp);
+    GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# delay heap timeout"),
+                           delay.rel_value, GNUNET_NO);
+
     pp->task =
-        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                      (rp->earliest_transmission),
-                                      &schedule_peer_transmission, pp);
+        GNUNET_SCHEDULER_add_delayed (delay, &schedule_peer_transmission, pp);
     return;
   }
+  GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plans executed"),
+                            1, GNUNET_NO);
   /* process from priority heap */
   rp = GNUNET_CONTAINER_heap_peek (pp->priority_heap);
-#if DEBUG_FS > 1
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing query plan %p\n", rp);
-#endif
   GNUNET_assert (NULL != rp);
   msize = GSF_pending_request_get_message_ (get_latest (rp), 0, NULL);
-  pp->pth = GSF_peer_transmit_ (pp->cp,
-                                GNUNET_YES,
-                                rp->priority,
-                                GNUNET_TIME_UNIT_FOREVER_REL,
-                                msize, &transmit_message_callback, pp);
+  pp->pth =
+      GSF_peer_transmit_ (pp->cp, GNUNET_YES, rp->priority,
+                          GNUNET_TIME_UNIT_FOREVER_REL, msize,
+                          &transmit_message_callback, pp);
   GNUNET_assert (NULL != pp->pth);
 }
 
@@ -423,16 +506,13 @@ struct MergeContext
  * present for this peer.
  *
  * @param cls closure
- * @param node internal node of the heap (ignored)
+ * @param query the query
  * @param element request plan stored at the node
- * @param cost cost associated with the node (ignored)
  * @return GNUNET_YES if we should continue to iterate,
  *         GNUNET_NO if not (merge success)
  */
 static int
-merge_pr (void *cls,
-          struct GNUNET_CONTAINER_HeapNode *node,
-          void *element, GNUNET_CONTAINER_HeapCostType cost)
+merge_pr (void *cls, const GNUNET_HashCode * query, void *element)
 {
   struct MergeContext *mpr = cls;
   struct GSF_RequestPlan *rp = element;
@@ -455,14 +535,13 @@ merge_pr (void *cls,
   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
   mpr->merged = GNUNET_YES;
-  GNUNET_STATISTICS_update (GSF_stats,
-                            gettext_noop ("# requests merged"), 1, GNUNET_NO);
+  GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests merged"), 1,
+                            GNUNET_NO);
   latest = get_latest (rp);
   if (GSF_pending_request_get_data_ (latest)->ttl.abs_value <
       prd->ttl.abs_value)
   {
-    GNUNET_STATISTICS_update (GSF_stats,
-                              gettext_noop ("# requests refreshed"),
+    GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# requests refreshed"),
                               1, GNUNET_NO);
     rp->transmission_counter = 0;       /* reset */
   }
@@ -493,34 +572,34 @@ GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
   if (NULL == pp)
   {
     pp = GNUNET_malloc (sizeof (struct PeerPlan));
+    pp->plan_map = GNUNET_CONTAINER_multihashmap_create (128);
     pp->priority_heap =
         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
     pp->delay_heap =
         GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
     pp->cp = cp;
-    GNUNET_CONTAINER_multihashmap_put (plans,
-                                       &id.hashPubKey,
-                                       pp,
+    GNUNET_CONTAINER_multihashmap_put (plans, &id.hashPubKey, pp,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
   }
   mpc.merged = GNUNET_NO;
   mpc.pr = pr;
-  GNUNET_CONTAINER_heap_iterate (pp->priority_heap, &merge_pr, &mpc);
+  GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
+                                              &GSF_pending_request_get_data_
+                                              (pr)->query, &merge_pr, &mpc);
   if (mpc.merged != GNUNET_NO)
     return;
-  GNUNET_CONTAINER_heap_iterate (pp->delay_heap, &merge_pr, &mpc);
+  GNUNET_CONTAINER_multihashmap_get_multiple (pp->plan_map,
+                                              &GSF_pending_request_get_data_
+                                              (pr)->query, &merge_pr, &mpc);
   if (mpc.merged != GNUNET_NO)
     return;
   plan_count++;
-  GNUNET_STATISTICS_update (GSF_stats,
-                            gettext_noop ("# query plan entries"),
-                            1, GNUNET_NO);
+  GNUNET_STATISTICS_update (GSF_stats, gettext_noop ("# query plan entries"), 1,
+                            GNUNET_NO);
   prd = GSF_pending_request_get_data_ (pr);
-#if DEBUG_FS
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Planning transmission of query `%s' to peer `%s'\n",
               GNUNET_h2s (&prd->query), GNUNET_i2s (&id));
-#endif
   rp = GNUNET_malloc (sizeof (struct GSF_RequestPlan));
   rpr = GNUNET_malloc (sizeof (struct GSF_RequestPlanReference));
   prl = GNUNET_malloc (sizeof (struct PendingRequestList));
@@ -530,6 +609,11 @@ GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
   prl->pr = pr;
   GNUNET_CONTAINER_DLL_insert (prd->rpr_head, prd->rpr_tail, rpr);
   GNUNET_CONTAINER_DLL_insert (rp->prl_head, rp->prl_tail, prl);
+  rp->pp = pp;
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multihashmap_put (pp->plan_map,
+                                                    get_rp_key (rp), rp,
+                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
   plan (pp, rp);
 }
 
@@ -538,7 +622,7 @@ GSF_plan_add_ (struct GSF_ConnectedPeer *cp, struct GSF_PendingRequest *pr)
  * Notify the plan about a peer being no longer available;
  * destroy all entries associated with this peer.
  *
- * @param cp connected peer 
+ * @param cp connected peer
  */
 void
 GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
@@ -554,8 +638,8 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
   if (NULL == pp)
     return;                     /* nothing was ever planned for this peer */
   GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multihashmap_remove (plans,
-                                                       &id.hashPubKey, pp));
+                 GNUNET_CONTAINER_multihashmap_remove (plans, &id.hashPubKey,
+                                                       pp));
   if (NULL != pp->pth)
     GSF_peer_transmit_cancel_ (pp->pth);
   if (GNUNET_SCHEDULER_NO_TASK != pp->task)
@@ -565,6 +649,9 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
   }
   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->priority_heap)))
   {
+    GNUNET_break (GNUNET_YES ==
+                  GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
+                                                        get_rp_key (rp), rp));
     while (NULL != (prl = rp->prl_head))
     {
       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
@@ -578,6 +665,9 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
   GNUNET_CONTAINER_heap_destroy (pp->priority_heap);
   while (NULL != (rp = GNUNET_CONTAINER_heap_remove_root (pp->delay_heap)))
   {
+    GNUNET_break (GNUNET_YES ==
+                  GNUNET_CONTAINER_multihashmap_remove (pp->plan_map,
+                                                        get_rp_key (rp), rp));
     while (NULL != (prl = rp->prl_head))
     {
       GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, prl);
@@ -588,14 +678,39 @@ GSF_plan_notify_peer_disconnect_ (const struct GSF_ConnectedPeer *cp)
     }
     GNUNET_free (rp);
   }
-  GNUNET_STATISTICS_set (GSF_stats,
-                         gettext_noop ("# query plan entries"),
+  GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
                          plan_count, GNUNET_NO);
 
   GNUNET_CONTAINER_heap_destroy (pp->delay_heap);
+  GNUNET_CONTAINER_multihashmap_destroy (pp->plan_map);
   GNUNET_free (pp);
 }
 
+/**
+ * Get the last transmission attempt time for the request plan list
+ * referenced by 'rpr_head', that was sent to 'sender'
+ *
+ * @param rpr_head request plan reference list to check.
+ * @param sender the peer that we've sent the request to.
+ * @param result the timestamp to fill.
+ * @return GNUNET_YES if 'result' was changed, GNUNET_NO otherwise.
+ */
+int
+GSF_request_plan_reference_get_last_transmission_ (
+    struct GSF_RequestPlanReference *rpr_head, struct GSF_ConnectedPeer *sender,
+    struct GNUNET_TIME_Absolute *result)
+{
+  struct GSF_RequestPlanReference *rpr;
+  for (rpr = rpr_head; rpr; rpr = rpr->next)
+  {
+    if (rpr->rp->pp->cp == sender)
+    {
+      *result = rpr->rp->last_transmission;
+      return GNUNET_YES;
+    }
+  }
+  return GNUNET_NO;
+}
 
 /**
  * Notify the plan about a request being done; destroy all entries
@@ -616,17 +731,21 @@ GSF_plan_notify_request_done_ (struct GSF_PendingRequest *pr)
     GNUNET_CONTAINER_DLL_remove (prd->rpr_head, prd->rpr_tail, rpr);
     rp = rpr->rp;
     GNUNET_CONTAINER_DLL_remove (rp->prl_head, rp->prl_tail, rpr->prl);
-    GNUNET_free (rpr->prl);
-    GNUNET_free (rpr);
-    if (rp->prl_head == 0)
+    if (NULL == rp->prl_head)
     {
       GNUNET_CONTAINER_heap_remove_node (rp->hn);
       plan_count--;
+      GNUNET_break (GNUNET_YES ==
+                    GNUNET_CONTAINER_multihashmap_remove (rp->pp->plan_map,
+                                                          &GSF_pending_request_get_data_
+                                                          (rpr->prl->pr)->query,
+                                                          rp));
       GNUNET_free (rp);
     }
+    GNUNET_free (rpr->prl);
+    GNUNET_free (rpr);
   }
-  GNUNET_STATISTICS_set (GSF_stats,
-                         gettext_noop ("# query plan entries"),
+  GNUNET_STATISTICS_set (GSF_stats, gettext_noop ("# query plan entries"),
                          plan_count, GNUNET_NO);
 }