2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 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/>.
20 * @file fs/gnunet-service-fs_pr.c
21 * @brief API to handle pending requests
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_load_lib.h"
27 #include "gnunet-service-fs.h"
28 #include "gnunet-service-fs_cp.h"
29 #include "gnunet-service-fs_indexing.h"
30 #include "gnunet-service-fs_pe.h"
31 #include "gnunet-service-fs_pr.h"
32 #include "gnunet-service-fs_cadet.h"
36 * Desired replication level for GETs.
38 #define DHT_GET_REPLICATION 5
41 * Maximum size of the datastore queue for P2P operations. Needs to
42 * be large enough to queue #MAX_QUEUE_PER_PEER operations for roughly
43 * the number of active (connected) peers.
45 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
48 * Bandwidth value of a 0-priority content (must be fairly high
49 * compared to query since content is typically significantly larger
50 * -- and more valueable since it can take many queries to get one
53 #define CONTENT_BANDWIDTH_VALUE 800
56 * Hard limit on the number of results we may get from the datastore per query.
58 #define MAX_RESULTS (100 * 1024)
61 * Collect an instane number of statistics? May cause excessive IPC.
63 #define INSANE_STATISTICS GNUNET_NO
66 * If obtaining a block via cadet fails, how often do we retry it before
67 * giving up for good (and sticking to non-anonymous transfer)?
69 #define CADET_RETRY_MAX 3
75 struct GSF_PendingRequest
78 * Public data for the request.
80 struct GSF_PendingRequestData public_data;
83 * Function to call if we encounter a reply.
85 GSF_PendingRequestReplyHandler rh;
93 * Array of hash codes of replies we've already seen.
95 struct GNUNET_HashCode *replies_seen;
98 * Block group for filtering replies we've already seen.
100 struct GNUNET_BLOCK_Group *bg;
103 * Entry for this pending request in the expiration heap, or NULL.
105 struct GNUNET_CONTAINER_HeapNode *hnode;
108 * Datastore queue entry for this request (or NULL for none).
110 struct GNUNET_DATASTORE_QueueEntry *qe;
113 * DHT request handle for this request (or NULL for none).
115 struct GNUNET_DHT_GetHandle *gh;
118 * Cadet request handle for this request (or NULL for none).
120 struct GSF_CadetRequest *cadet_request;
123 * Function to call upon completion of the local get
124 * request, or NULL for none.
126 GSF_LocalLookupContinuation llc_cont;
129 * Closure for @e llc_cont.
134 * Last result from the local datastore lookup evaluation.
136 enum GNUNET_BLOCK_EvaluationResult local_result;
139 * Identity of the peer that we should use for the 'sender'
140 * (recipient of the response) when forwarding (0 for none).
142 GNUNET_PEER_Id sender_pid;
145 * Identity of the peer that we should never forward this query
146 * to since it originated this query (0 for none).
148 GNUNET_PEER_Id origin_pid;
151 * Time we started the last datastore lookup.
153 struct GNUNET_TIME_Absolute qe_start;
156 * Task that warns us if the local datastore lookup takes too long.
158 struct GNUNET_SCHEDULER_Task * warn_task;
161 * Do we have a first UID yet?
166 * Have we seen a NULL result yet?
171 * Unique ID of the first result from the local datastore;
172 * used to terminate the loop.
182 * How often have we retried this request via 'cadet'?
183 * (used to bound overall retries).
185 unsigned int cadet_retry_count;
188 * Number of valid entries in the 'replies_seen' array.
190 unsigned int replies_seen_count;
193 * Length of the 'replies_seen' array.
195 unsigned int replies_seen_size;
201 * All pending requests, ordered by the query. Entries
202 * are of type 'struct GSF_PendingRequest*'.
204 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
208 * Datastore 'PUT' load tracking.
210 static struct GNUNET_LOAD_Value *datastore_put_load;
214 * Are we allowed to migrate content to this peer.
216 static int active_to_migration;
220 * Heap with the request that will expire next at the top. Contains
221 * pointers of type "struct PendingRequest*"; these will *also* be
222 * aliased from the "requests_by_peer" data structures and the
223 * "requests_by_query" table. Note that requests from our clients
224 * don't expire and are thus NOT in the "requests_by_expiration"
225 * (or the "requests_by_peer" tables).
227 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
231 * Maximum number of requests (from other peers, overall) that we're
232 * willing to have pending at any given point in time. Can be changed
233 * via the configuration file (32k is just the default).
235 static unsigned long long max_pending_requests = (32 * 1024);
240 * Recalculate our bloom filter for filtering replies. This function
241 * will create a new bloom filter from scratch, so it should only be
242 * called if we have no bloomfilter at all (and hence can create a
243 * fresh one of minimal size without problems) OR if our peer is the
244 * initiator (in which case we may resize to larger than mimimum size).
246 * @param type type of the request
247 * @param pr request for which the BF is to be recomputed
250 refresh_bloomfilter (enum GNUNET_BLOCK_Type type,
251 struct GSF_PendingRequest *pr)
255 GNUNET_BLOCK_group_destroy (pr->bg);
258 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
259 return; /* no need */
261 = GNUNET_BLOCK_group_create (GSF_block_ctx,
263 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
268 pr->replies_seen_count,
272 GNUNET_break (GNUNET_OK ==
273 GNUNET_BLOCK_group_set_seen (pr->bg,
275 pr->replies_seen_count));
280 * Create a new pending request.
282 * @param options request options
283 * @param type type of the block that is being requested
284 * @param query key for the lookup
285 * @param target preferred target for the request, NULL for none
286 * @param bf_data raw data for bloom filter for known replies, can be NULL
287 * @param bf_size number of bytes in @a bf_data
288 * @param mingle mingle value for bf
289 * @param anonymity_level desired anonymity level
290 * @param priority maximum outgoing cummulative request priority to use
291 * @param ttl current time-to-live for the request
292 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
293 * @param origin_pid peer ID of origin of query (do not loop back)
294 * @param replies_seen hash codes of known local replies
295 * @param replies_seen_count size of the @a replies_seen array
296 * @param rh handle to call when we get a reply
297 * @param rh_cls closure for @a rh
298 * @return handle for the new pending request
300 struct GSF_PendingRequest *
301 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
302 enum GNUNET_BLOCK_Type type,
303 const struct GNUNET_HashCode *query,
304 const struct GNUNET_PeerIdentity *target,
308 uint32_t anonymity_level,
311 GNUNET_PEER_Id sender_pid,
312 GNUNET_PEER_Id origin_pid,
313 const struct GNUNET_HashCode *replies_seen,
314 unsigned int replies_seen_count,
315 GSF_PendingRequestReplyHandler rh,
318 struct GSF_PendingRequest *pr;
319 struct GSF_PendingRequest *dpr;
321 struct GNUNET_HashCode *eptr;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Creating request handle for `%s' of type %d\n",
325 GNUNET_h2s (query), type);
326 #if INSANE_STATISTICS
327 GNUNET_STATISTICS_update (GSF_stats,
328 gettext_noop ("# Pending requests created"), 1,
333 extra += sizeof (struct GNUNET_PeerIdentity);
334 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
335 pr->public_data.query = *query;
336 eptr = (struct GNUNET_HashCode *) &pr[1];
339 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
342 sizeof (struct GNUNET_PeerIdentity));
344 pr->public_data.anonymity_level = anonymity_level;
345 pr->public_data.priority = priority;
346 pr->public_data.original_priority = priority;
347 pr->public_data.options = options;
348 pr->public_data.type = type;
349 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
350 pr->sender_pid = sender_pid;
351 pr->origin_pid = origin_pid;
354 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
356 pr->public_data.ttl =
357 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
358 (GNUNET_TIME_UNIT_SECONDS,
361 pr->public_data.ttl =
362 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
363 GNUNET_TIME_relative_multiply
364 (GNUNET_TIME_UNIT_SECONDS,
366 if (replies_seen_count > 0)
368 pr->replies_seen_size = replies_seen_count;
369 pr->replies_seen = GNUNET_new_array (pr->replies_seen_size,
370 struct GNUNET_HashCode);
371 GNUNET_memcpy (pr->replies_seen,
373 replies_seen_count * sizeof (struct GNUNET_HashCode));
374 pr->replies_seen_count = replies_seen_count;
376 if ( (NULL != bf_data) &&
377 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type) )
380 = GNUNET_BLOCK_group_create (GSF_block_ctx,
381 pr->public_data.type,
389 else if ((replies_seen_count > 0) &&
390 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
392 refresh_bloomfilter (pr->public_data.type,
395 GNUNET_CONTAINER_multihashmap_put (pr_map,
396 &pr->public_data.query,
398 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
399 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
402 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
404 pr->public_data.ttl.abs_value_us);
405 /* make sure we don't track too many requests */
406 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
407 max_pending_requests)
409 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
410 GNUNET_assert (NULL != dpr);
412 break; /* let the request live briefly... */
414 dpr->rh (dpr->rh_cls,
415 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
418 GNUNET_TIME_UNIT_FOREVER_ABS,
419 GNUNET_TIME_UNIT_FOREVER_ABS,
420 GNUNET_BLOCK_TYPE_ANY,
423 GSF_pending_request_cancel_ (dpr,
427 GNUNET_STATISTICS_update (GSF_stats,
428 gettext_noop ("# Pending requests active"), 1,
434 * Obtain the public data associated with a pending request
436 * @param pr pending request
437 * @return associated public data
439 struct GSF_PendingRequestData *
440 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
442 return &pr->public_data;
447 * Test if two pending requests are compatible (would generate
448 * the same query modulo filters and should thus be processed
451 * @param pra a pending request
452 * @param prb another pending request
453 * @return #GNUNET_OK if the requests are compatible
456 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
457 struct GSF_PendingRequest *prb)
459 if ( (pra->public_data.type != prb->public_data.type) ||
460 (0 != memcmp (&pra->public_data.query,
461 &prb->public_data.query,
462 sizeof (struct GNUNET_HashCode))))
469 * Update a given pending request with additional replies
470 * that have been seen.
472 * @param pr request to update
473 * @param replies_seen hash codes of replies that we've seen
474 * @param replies_seen_count size of the replies_seen array
477 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
478 const struct GNUNET_HashCode * replies_seen,
479 unsigned int replies_seen_count)
481 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
482 return; /* integer overflow */
483 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
485 /* we're responsible for the BF, full refresh */
486 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
487 GNUNET_array_grow (pr->replies_seen,
488 pr->replies_seen_size,
489 replies_seen_count + pr->replies_seen_count);
490 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
492 sizeof (struct GNUNET_HashCode) * replies_seen_count);
493 pr->replies_seen_count += replies_seen_count;
494 refresh_bloomfilter (pr->public_data.type,
501 /* we're not the initiator, but the initiator did not give us
502 * any bloom-filter, so we need to create one on-the-fly */
503 refresh_bloomfilter (pr->public_data.type,
508 GNUNET_break (GNUNET_OK ==
509 GNUNET_BLOCK_group_set_seen (pr->bg,
511 pr->replies_seen_count));
515 GNUNET_DHT_get_filter_known_results (pr->gh,
522 * Generate the message corresponding to the given pending request for
523 * transmission to other peers.
525 * @param pr request to generate the message for
526 * @return envelope with the request message
528 struct GNUNET_MQ_Envelope *
529 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
531 struct GNUNET_MQ_Envelope *env;
532 struct GetMessage *gm;
533 struct GNUNET_PeerIdentity *ext;
538 struct GNUNET_TIME_Absolute now;
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Building request message for `%s' of type %d\n",
546 GNUNET_h2s (&pr->public_data.query),
547 pr->public_data.type);
550 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
551 if ((!do_route) && (pr->sender_pid == 0))
554 do_route = GNUNET_YES;
558 bm |= GET_MESSAGE_BIT_RETURN_TO;
561 if (NULL != pr->public_data.target)
563 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
567 GNUNET_BLOCK_group_serialize (pr->bg,
575 env = GNUNET_MQ_msg_extra (gm,
576 bf_size + k * sizeof (struct GNUNET_PeerIdentity),
577 GNUNET_MESSAGE_TYPE_FS_GET);
578 gm->type = htonl (pr->public_data.type);
581 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
582 pr->public_data.priority + 1);
585 pr->public_data.priority -= prio;
586 pr->public_data.num_transmissions++;
587 pr->public_data.respect_offered += prio;
588 gm->priority = htonl (prio);
589 now = GNUNET_TIME_absolute_get ();
590 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
591 gm->ttl = htonl (ttl / 1000LL / 1000LL);
592 gm->filter_mutator = htonl (bf_nonce);
593 gm->hash_bitmap = htonl (bm);
594 gm->query = pr->public_data.query;
595 ext = (struct GNUNET_PeerIdentity *) &gm[1];
598 GNUNET_PEER_resolve (pr->sender_pid,
600 if (NULL != pr->public_data.target)
601 ext[k++] = *pr->public_data.target;
602 GNUNET_memcpy (&ext[k],
605 GNUNET_free_non_null (bf_data);
611 * Iterator to free pending requests.
613 * @param cls closure, unused
614 * @param key current key code
615 * @param value value in the hash map (pending request)
616 * @return #GNUNET_YES (we should continue to iterate)
619 clean_request (void *cls,
620 const struct GNUNET_HashCode *key,
623 struct GSF_PendingRequest *pr = value;
624 GSF_LocalLookupContinuation cont;
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Cleaning up pending request for `%s'.\n",
629 if (NULL != pr->cadet_request)
631 pr->cadet_retry_count = CADET_RETRY_MAX;
632 GSF_cadet_query_cancel (pr->cadet_request);
633 pr->cadet_request = NULL;
635 if (NULL != (cont = pr->llc_cont))
638 cont (pr->llc_cont_cls,
642 GSF_plan_notify_request_done_ (pr);
643 GNUNET_free_non_null (pr->replies_seen);
644 GNUNET_BLOCK_group_destroy (pr->bg);
646 GNUNET_PEER_change_rc (pr->sender_pid, -1);
648 GNUNET_PEER_change_rc (pr->origin_pid, -1);
650 if (NULL != pr->hnode)
652 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
657 GNUNET_DATASTORE_cancel (pr->qe);
662 GNUNET_DHT_get_stop (pr->gh);
665 if (NULL != pr->warn_task)
667 GNUNET_SCHEDULER_cancel (pr->warn_task);
668 pr->warn_task = NULL;
670 GNUNET_assert (GNUNET_OK ==
671 GNUNET_CONTAINER_multihashmap_remove (pr_map,
672 &pr->public_data.query,
674 GNUNET_STATISTICS_update (GSF_stats,
675 gettext_noop ("# Pending requests active"),
684 * Explicitly cancel a pending request.
686 * @param pr request to cancel
687 * @param full_cleanup fully purge the request
690 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
693 GSF_LocalLookupContinuation cont;
696 return; /* already cleaned up! */
697 if (GNUNET_NO == full_cleanup)
699 /* make request inactive (we're no longer interested in more results),
700 * but do NOT remove from our data-structures, we still need it there
701 * to prevent the request from looping */
703 if (NULL != pr->cadet_request)
705 pr->cadet_retry_count = CADET_RETRY_MAX;
706 GSF_cadet_query_cancel (pr->cadet_request);
707 pr->cadet_request = NULL;
709 if (NULL != (cont = pr->llc_cont))
712 cont (pr->llc_cont_cls,
716 GSF_plan_notify_request_done_ (pr);
719 GNUNET_DATASTORE_cancel (pr->qe);
724 GNUNET_DHT_get_stop (pr->gh);
727 if (NULL != pr->warn_task)
729 GNUNET_SCHEDULER_cancel (pr->warn_task);
730 pr->warn_task = NULL;
734 GNUNET_assert (GNUNET_YES ==
736 &pr->public_data.query,
742 * Iterate over all pending requests.
744 * @param it function to call for each request
745 * @param cls closure for @a it
748 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
750 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
751 (GNUNET_CONTAINER_HashMapIterator) it,
757 * Closure for process_reply() function.
759 struct ProcessReplyClosure
762 * The data for the reply.
767 * Who gave us this reply? NULL for local host (or DHT)
769 struct GSF_ConnectedPeer *sender;
772 * When the reply expires.
774 struct GNUNET_TIME_Absolute expiration;
784 enum GNUNET_BLOCK_Type type;
787 * Control flags for evaluation.
789 enum GNUNET_BLOCK_EvaluationOptions eo;
792 * How much was this reply worth to us?
797 * Anonymity requirements for this reply.
799 uint32_t anonymity_level;
802 * Evaluation result (returned).
804 enum GNUNET_BLOCK_EvaluationResult eval;
807 * Did we find a matching request?
814 * Update the performance data for the sender (if any) since
815 * the sender successfully answered one of our queries.
817 * @param prq information about the sender
818 * @param pr request that was satisfied
821 update_request_performance_data (struct ProcessReplyClosure *prq,
822 struct GSF_PendingRequest *pr)
824 if (prq->sender == NULL)
826 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
832 * We have received a reply; handle it!
834 * @param cls response (a `struct ProcessReplyClosure`)
835 * @param key our query
836 * @param value value in the hash map (info about the query)
837 * @return #GNUNET_YES (we should continue to iterate)
840 process_reply (void *cls,
841 const struct GNUNET_HashCode *key,
844 struct ProcessReplyClosure *prq = cls;
845 struct GSF_PendingRequest *pr = value;
846 struct GNUNET_HashCode chash;
847 struct GNUNET_TIME_Absolute last_transmission;
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Matched result (type %u) for query `%s' with pending request\n",
853 (unsigned int) prq->type,
855 GNUNET_STATISTICS_update (GSF_stats,
856 gettext_noop ("# replies received and matched"), 1,
859 GNUNET_BLOCK_evaluate (GSF_block_ctx,
870 case GNUNET_BLOCK_EVALUATION_OK_MORE:
871 update_request_performance_data (prq, pr);
873 case GNUNET_BLOCK_EVALUATION_OK_LAST:
874 /* short cut: stop processing early, no BF-update, etc. */
875 update_request_performance_data (prq, pr);
876 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
877 GNUNET_TIME_absolute_get_duration (pr->
878 public_data.start_time).rel_value_us);
880 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
883 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
884 /* pass on to other peers / local clients */
885 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
886 last_transmission, prq->type, prq->data, prq->size);
888 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
889 #if INSANE_STATISTICS
890 GNUNET_STATISTICS_update (GSF_stats,
892 ("# duplicate replies discarded (bloomfilter)"),
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Duplicate response, discarding.\n");
897 return GNUNET_YES; /* duplicate */
898 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
899 GNUNET_STATISTICS_update (GSF_stats,
901 ("# irrelevant replies discarded"),
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Irrelevant response, ignoring.\n");
906 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
907 return GNUNET_YES; /* wrong namespace */
908 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
911 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
914 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
916 _("Unsupported block type %u\n"),
920 /* update bloomfilter */
921 GNUNET_CRYPTO_hash (prq->data,
924 GSF_pending_request_update_ (pr,
927 if (NULL == prq->sender)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Found result for query `%s' in local datastore\n",
932 GNUNET_STATISTICS_update (GSF_stats,
933 gettext_noop ("# results found locally"),
939 GSF_dht_lookup_ (pr);
941 prq->priority += pr->public_data.original_priority;
942 pr->public_data.priority = 0;
943 pr->public_data.original_priority = 0;
944 pr->public_data.results_found++;
945 prq->request_found = GNUNET_YES;
946 /* finally, pass on to other peer / local client */
947 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
950 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
954 prq->anonymity_level,
965 * Context for put_migration_continuation().
967 struct PutMigrationContext
971 * Start time for the operation.
973 struct GNUNET_TIME_Absolute start;
978 struct GNUNET_PeerIdentity origin;
981 * #GNUNET_YES if we had a matching request for this block,
989 * Continuation called to notify client about result of the
993 * @param success #GNUNET_SYSERR on failure
994 * @param min_expiration minimum expiration time required for content to be stored
995 * @param msg NULL on success, otherwise an error message
998 put_migration_continuation (void *cls, int success,
999 struct GNUNET_TIME_Absolute min_expiration,
1002 struct PutMigrationContext *pmc = cls;
1003 struct GSF_ConnectedPeer *cp;
1004 struct GNUNET_TIME_Relative mig_pause;
1005 struct GSF_PeerPerformanceData *ppd;
1007 if (NULL != datastore_put_load)
1009 if (GNUNET_SYSERR != success)
1011 GNUNET_LOAD_update (datastore_put_load,
1012 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
1016 /* on queue failure / timeout, increase the put load dramatically */
1017 GNUNET_LOAD_update (datastore_put_load,
1018 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
1021 cp = GSF_peer_get_ (&pmc->origin);
1022 if (GNUNET_OK == success)
1026 ppd = GSF_get_peer_performance_data_ (cp);
1027 ppd->migration_delay.rel_value_us /= 2;
1032 if ( (GNUNET_NO == success) &&
1033 (GNUNET_NO == pmc->requested) &&
1036 ppd = GSF_get_peer_performance_data_ (cp);
1037 if (min_expiration.abs_value_us > 0)
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Asking to stop migration for %s because datastore is full\n",
1041 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1042 GSF_block_peer_migration_ (cp, min_expiration);
1046 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1047 ppd->migration_delay);
1048 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1049 ppd->migration_delay);
1050 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1051 ppd->migration_delay.rel_value_us);
1052 ppd->migration_delay = GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Replicated content already exists locally, asking to stop migration for %s\n",
1055 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1057 GSF_block_peer_migration_ (cp,
1058 GNUNET_TIME_relative_to_absolute (mig_pause));
1062 GNUNET_STATISTICS_update (GSF_stats,
1063 gettext_noop ("# Datastore `PUT' failures"), 1,
1069 * Test if the DATABASE (PUT) load on this peer is too high
1070 * to even consider processing the query at
1073 * @param priority the priority of the item
1074 * @return #GNUNET_YES if the load is too high to do anything (load high)
1075 * #GNUNET_NO to process normally (load normal or low)
1078 test_put_load_too_high (uint32_t priority)
1082 if (NULL == datastore_put_load)
1084 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1085 return GNUNET_NO; /* very fast */
1086 ld = GNUNET_LOAD_get_load (datastore_put_load);
1087 if (ld < 2.0 * (1 + priority))
1089 GNUNET_STATISTICS_update (GSF_stats,
1091 ("# storage requests dropped due to high load"), 1,
1098 * Iterator called on each result obtained for a DHT
1099 * operation that expects a reply
1101 * @param cls closure
1102 * @param exp when will this value expire
1103 * @param key key of the result
1104 * @param get_path peers on reply path (or NULL if not recorded)
1105 * @param get_path_length number of entries in @a get_path
1106 * @param put_path peers on the PUT path (or NULL if not recorded)
1107 * @param put_path_length number of entries in @a get_path
1108 * @param type type of the result
1109 * @param size number of bytes in @a data
1110 * @param data pointer to the result data
1113 handle_dht_reply (void *cls,
1114 struct GNUNET_TIME_Absolute exp,
1115 const struct GNUNET_HashCode *key,
1116 const struct GNUNET_PeerIdentity *get_path,
1117 unsigned int get_path_length,
1118 const struct GNUNET_PeerIdentity *put_path,
1119 unsigned int put_path_length,
1120 enum GNUNET_BLOCK_Type type,
1124 struct GSF_PendingRequest *pr = cls;
1125 struct ProcessReplyClosure prq;
1126 struct PutMigrationContext *pmc;
1128 GNUNET_STATISTICS_update (GSF_stats,
1129 gettext_noop ("# Replies received from DHT"), 1,
1131 memset (&prq, 0, sizeof (prq));
1133 prq.expiration = exp;
1134 /* do not allow migrated content to live longer than 1 year */
1135 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1139 prq.eo = GNUNET_BLOCK_EO_NONE;
1140 process_reply (&prq, key, pr);
1141 if ((GNUNET_YES == active_to_migration) &&
1142 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Replicating result for query `%s' with priority %u\n",
1146 GNUNET_h2s (key), prq.priority);
1147 pmc = GNUNET_new (struct PutMigrationContext);
1148 pmc->start = GNUNET_TIME_absolute_get ();
1149 pmc->requested = GNUNET_YES;
1151 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1153 0 /* replication */ ,
1154 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1155 &put_migration_continuation, pmc))
1157 put_migration_continuation (pmc,
1159 GNUNET_TIME_UNIT_ZERO_ABS,
1167 * Consider looking up the data in the DHT (anonymity-level permitting).
1169 * @param pr the pending request to process
1172 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1176 struct GNUNET_PeerIdentity pi;
1177 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1179 if (0 != pr->public_data.anonymity_level)
1183 GNUNET_DHT_get_stop (pr->gh);
1188 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1190 GNUNET_assert (0 != pr->sender_pid);
1191 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1192 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1193 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1196 GNUNET_DHT_get_start (GSF_dht,
1197 pr->public_data.type, &pr->public_data.query,
1198 DHT_GET_REPLICATION,
1199 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1200 xquery, xquery_size, &handle_dht_reply, pr);
1201 if ( (NULL != pr->gh) &&
1202 (0 != pr->replies_seen_count) )
1203 GNUNET_DHT_get_filter_known_results (pr->gh,
1204 pr->replies_seen_count,
1210 * Function called with a reply from the cadet.
1212 * @param cls the pending request struct
1213 * @param type type of the block, ANY on error
1214 * @param expiration expiration time for the block
1215 * @param data_size number of bytes in @a data, 0 on error
1216 * @param data reply block data, NULL on error
1219 cadet_reply_proc (void *cls,
1220 enum GNUNET_BLOCK_Type type,
1221 struct GNUNET_TIME_Absolute expiration,
1225 struct GSF_PendingRequest *pr = cls;
1226 struct ProcessReplyClosure prq;
1227 struct GNUNET_HashCode query;
1229 pr->cadet_request = NULL;
1230 if (GNUNET_BLOCK_TYPE_ANY == type)
1232 GNUNET_break (NULL == data);
1233 GNUNET_break (0 == data_size);
1234 pr->cadet_retry_count++;
1235 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1236 return; /* give up on cadet */
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Error retrieiving block via cadet\n");
1239 /* retry -- without delay, as this is non-anonymous
1240 and cadet/cadet connect will take some time anyway */
1241 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1242 &pr->public_data.query,
1243 pr->public_data.type,
1249 GNUNET_BLOCK_get_key (GSF_block_ctx,
1251 data, data_size, &query))
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Failed to derive key for block of type %d\n",
1256 GNUNET_break_op (0);
1259 GNUNET_STATISTICS_update (GSF_stats,
1260 gettext_noop ("# Replies received from CADET"), 1,
1262 memset (&prq, 0, sizeof (prq));
1264 prq.expiration = expiration;
1265 /* do not allow migrated content to live longer than 1 year */
1266 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1268 prq.size = data_size;
1270 prq.eo = GNUNET_BLOCK_EO_NONE;
1271 process_reply (&prq, &query, pr);
1276 * Consider downloading via cadet (if possible)
1278 * @param pr the pending request to process
1281 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1283 if (0 != pr->public_data.anonymity_level)
1285 if (0 == pr->public_data.target)
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Cannot do cadet-based download, target peer not known\n");
1291 if (NULL != pr->cadet_request)
1293 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1294 &pr->public_data.query,
1295 pr->public_data.type,
1302 * Task that issues a warning if the datastore lookup takes too long.
1304 * @param cls the `struct GSF_PendingRequest`
1307 warn_delay_task (void *cls)
1309 struct GSF_PendingRequest *pr = cls;
1311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1312 _("Datastore lookup already took %s!\n"),
1313 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1323 * Task that issues a warning if the datastore lookup takes too long.
1325 * @param cls the `struct GSF_PendingRequest`
1328 odc_warn_delay_task (void *cls)
1330 struct GSF_PendingRequest *pr = cls;
1332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1333 _("On-demand lookup already took %s!\n"),
1334 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1337 &odc_warn_delay_task, pr);
1341 /* Call our continuation (if we have any) */
1343 call_continuation (struct GSF_PendingRequest *pr)
1345 GSF_LocalLookupContinuation cont = pr->llc_cont;
1347 GNUNET_assert (NULL == pr->qe);
1348 if (NULL != pr->warn_task)
1350 GNUNET_SCHEDULER_cancel (pr->warn_task);
1351 pr->warn_task = NULL;
1354 return; /* no continuation */
1355 pr->llc_cont = NULL;
1356 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1358 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1360 /* Signal that we are done and that there won't be any
1361 additional results to allow client to clean up state. */
1363 GNUNET_BLOCK_EVALUATION_OK_LAST,
1366 GNUNET_TIME_UNIT_ZERO_ABS,
1367 GNUNET_TIME_UNIT_FOREVER_ABS,
1368 GNUNET_BLOCK_TYPE_ANY,
1372 /* Finally, call our continuation to signal that we are
1373 done with local processing of this request; i.e. to
1374 start reading again from the client. */
1375 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1379 cont (pr->llc_cont_cls, pr, pr->local_result);
1383 /* Update stats and call continuation */
1385 no_more_local_results (struct GSF_PendingRequest *pr)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1388 "No further local responses available.\n");
1389 #if INSANE_STATISTICS
1390 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == pr->public_data.type) ||
1391 (GNUNET_BLOCK_TYPE_FS_IBLOCK == pr->public_data.type) )
1392 GNUNET_STATISTICS_update (GSF_stats,
1393 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1397 call_continuation (pr);
1401 /* forward declaration */
1403 process_local_reply (void *cls,
1404 const struct GNUNET_HashCode *key,
1407 enum GNUNET_BLOCK_Type type,
1410 uint32_t replication,
1411 struct GNUNET_TIME_Absolute expiration,
1415 /* Start a local query */
1417 start_local_query (struct GSF_PendingRequest *pr,
1421 pr->qe_start = GNUNET_TIME_absolute_get ();
1423 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1427 GNUNET_DATASTORE_get_key (GSF_dsh,
1430 &pr->public_data.query,
1431 pr->public_data.type ==
1432 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1433 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1435 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1436 public_data.options)) ? UINT_MAX : 1
1437 /* queue priority */ ,
1439 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1440 public_data.options)) ? UINT_MAX :
1441 GSF_datastore_queue_size
1442 /* max queue size */ ,
1443 &process_local_reply, pr);
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1447 "ERROR Requesting `%s' of type %d with next_uid %llu from datastore.\n",
1448 GNUNET_h2s (&pr->public_data.query),
1449 pr->public_data.type,
1450 (unsigned long long) next_uid);
1451 GNUNET_STATISTICS_update (GSF_stats,
1452 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1455 call_continuation (pr);
1460 * We're processing (local) results for a search request
1461 * from another peer. Pass applicable results to the
1462 * peer and if we are done either clean up (operation
1463 * complete) or forward to other peers (more results possible).
1465 * @param cls our closure (`struct GSF_PendingRequest *`)
1466 * @param key key for the content
1467 * @param size number of bytes in @a data
1468 * @param data content stored
1469 * @param type type of the content
1470 * @param priority priority of the content
1471 * @param anonymity anonymity-level for the content
1472 * @param replication replication-level for the content
1473 * @param expiration expiration time for the content
1474 * @param uid unique identifier for the datum;
1475 * maybe 0 if no unique identifier is available
1478 process_local_reply (void *cls,
1479 const struct GNUNET_HashCode *key,
1482 enum GNUNET_BLOCK_Type type,
1485 uint32_t replication,
1486 struct GNUNET_TIME_Absolute expiration,
1489 struct GSF_PendingRequest *pr = cls;
1490 struct ProcessReplyClosure prq;
1491 struct GNUNET_HashCode query;
1492 unsigned int old_rf;
1494 GNUNET_SCHEDULER_cancel (pr->warn_task);
1495 pr->warn_task = NULL;
1497 goto called_from_on_demand;
1499 if ( (NULL == key) &&
1501 !pr->have_first_uid) /* We have hit the end for the 2nd time with no results */
1504 #if INSANE_STATISTICS
1505 GNUNET_STATISTICS_update (GSF_stats,
1507 ("# Datastore lookups concluded (no results)"),
1510 no_more_local_results (pr);
1513 if ( ( (NULL == key) &&
1514 pr->seen_null ) || /* We have hit the end for the 2nd time OR */
1516 pr->have_first_uid &&
1517 (uid >= pr->first_uid) ) ) /* We have hit the end and past first UID */
1519 /* Seen all results */
1520 GNUNET_STATISTICS_update (GSF_stats,
1522 ("# Datastore lookups concluded (seen all)"),
1524 no_more_local_results (pr);
1529 GNUNET_assert (!pr->seen_null);
1530 pr->seen_null = true;
1531 start_local_query (pr,
1533 false /* random */);
1536 if (!pr->have_first_uid)
1538 pr->first_uid = uid;
1539 pr->have_first_uid = true;
1542 if (pr->result_count > MAX_RESULTS)
1544 GNUNET_STATISTICS_update (GSF_stats,
1546 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1548 no_more_local_results (pr);
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1553 GNUNET_h2s (key), type, (unsigned long long) uid);
1554 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Found ONDEMAND block, performing on-demand encoding\n");
1558 GNUNET_STATISTICS_update (GSF_stats,
1560 ("# on-demand blocks matched requests"), 1,
1562 pr->qe_start = GNUNET_TIME_absolute_get ();
1564 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1565 &odc_warn_delay_task, pr);
1567 GNUNET_FS_handle_on_demand_block (key,
1576 &process_local_reply,
1579 GNUNET_STATISTICS_update (GSF_stats,
1581 ("# on-demand lookups performed successfully"),
1583 return; /* we're done */
1585 GNUNET_STATISTICS_update (GSF_stats,
1586 gettext_noop ("# on-demand lookups failed"), 1,
1588 GNUNET_SCHEDULER_cancel (pr->warn_task);
1589 start_local_query (pr,
1590 uid + 1 /* next_uid */,
1591 false /* random */);
1594 called_from_on_demand:
1595 old_rf = pr->public_data.results_found;
1596 memset (&prq, 0, sizeof (prq));
1598 prq.expiration = expiration;
1601 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1604 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1606 start_local_query (pr,
1607 uid + 1 /* next_uid */,
1608 false /* random */);
1612 prq.priority = priority;
1613 prq.request_found = GNUNET_NO;
1614 prq.anonymity_level = anonymity;
1615 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1616 GSF_update_datastore_delay_ (pr->public_data.start_time);
1617 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1618 process_reply (&prq, key, pr);
1619 pr->local_result = prq.eval;
1620 if (GNUNET_BLOCK_EVALUATION_OK_LAST == prq.eval)
1622 GNUNET_STATISTICS_update (GSF_stats,
1624 ("# Datastore lookups concluded (found last result)"),
1627 call_continuation (pr);
1630 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1631 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1632 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Load too high, done with request\n");
1636 GNUNET_STATISTICS_update (GSF_stats,
1637 gettext_noop ("# Datastore lookups concluded (load too high)"),
1640 call_continuation (pr);
1643 start_local_query (pr,
1644 uid + 1 /* next_uid */,
1645 false /* random */);
1650 * Is the given target a legitimate peer for forwarding the given request?
1654 * @return #GNUNET_YES if this request could be forwarded to the given peer
1657 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1658 const struct GNUNET_PeerIdentity *target)
1660 struct GNUNET_PeerIdentity pi;
1662 if (0 == pr->origin_pid)
1664 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1666 memcmp (&pi, target,
1667 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1673 * Look up the request in the local datastore.
1675 * @param pr the pending request to process
1676 * @param cont function to call at the end
1677 * @param cont_cls closure for @a cont
1680 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1681 GSF_LocalLookupContinuation cont,
1684 GNUNET_assert (NULL == pr->gh);
1685 GNUNET_assert (NULL == pr->cadet_request);
1686 GNUNET_assert (NULL == pr->llc_cont);
1687 pr->llc_cont = cont;
1688 pr->llc_cont_cls = cont_cls;
1689 #if INSANE_STATISTICS
1690 GNUNET_STATISTICS_update (GSF_stats,
1691 gettext_noop ("# Datastore lookups initiated"), 1,
1694 start_local_query(pr,
1702 * Handle P2P "CONTENT" message. Checks that the message is
1703 * well-formed and then checks if there are any pending requests for
1704 * this content and possibly passes it on (to local clients or other
1705 * peers). Does NOT perform migration (content caching at this peer).
1707 * @param cls the other peer involved
1708 * @param put the actual message
1711 handle_p2p_put (void *cls,
1712 const struct PutMessage *put)
1714 struct GSF_ConnectedPeer *cp = cls;
1717 enum GNUNET_BLOCK_Type type;
1718 struct GNUNET_TIME_Absolute expiration;
1719 struct GNUNET_HashCode query;
1720 struct ProcessReplyClosure prq;
1721 struct GNUNET_TIME_Relative block_time;
1723 struct PutMigrationContext *pmc;
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Received P2P PUT from %s\n",
1727 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1728 GSF_cover_content_count++;
1729 msize = ntohs (put->header.size);
1730 dsize = msize - sizeof (struct PutMessage);
1731 type = ntohl (put->type);
1732 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1733 /* do not allow migrated content to live longer than 1 year */
1734 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1737 GNUNET_BLOCK_get_key (GSF_block_ctx,
1743 GNUNET_break_op (0);
1746 GNUNET_STATISTICS_update (GSF_stats,
1747 gettext_noop ("# GAP PUT messages received"),
1750 /* now, lookup 'query' */
1751 prq.data = (const void *) &put[1];
1755 prq.expiration = expiration;
1757 prq.anonymity_level = UINT32_MAX;
1758 prq.request_found = GNUNET_NO;
1759 prq.eo = GNUNET_BLOCK_EO_NONE;
1760 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1766 GSF_connected_peer_change_preference_ (cp,
1767 CONTENT_BANDWIDTH_VALUE +
1768 1000 * prq.priority);
1769 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1771 if ((GNUNET_YES == active_to_migration) &&
1773 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Replicating result for query `%s' with priority %u\n",
1777 GNUNET_h2s (&query),
1779 pmc = GNUNET_new (struct PutMigrationContext);
1780 pmc->start = GNUNET_TIME_absolute_get ();
1781 pmc->requested = prq.request_found;
1782 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1783 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1786 GNUNET_DATASTORE_put (GSF_dsh,
1794 0 /* replication */ ,
1795 expiration, 1 + prq.priority,
1796 MAX_DATASTORE_QUEUE,
1797 &put_migration_continuation,
1800 put_migration_continuation (pmc,
1802 GNUNET_TIME_UNIT_ZERO_ABS,
1806 else if (NULL != cp)
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Choosing not to keep content `%s' (%d/%d)\n",
1810 GNUNET_h2s (&query),
1811 active_to_migration,
1812 test_put_load_too_high (prq.priority));
1814 putl = GNUNET_LOAD_get_load (datastore_put_load);
1815 if ( (NULL != cp) &&
1816 (GNUNET_NO == prq.request_found) &&
1817 ( (GNUNET_YES != active_to_migration) ||
1818 (putl > 2.5 * (1 + prq.priority)) ) )
1820 if (GNUNET_YES != active_to_migration)
1821 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1823 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1825 GNUNET_CRYPTO_random_u32
1826 (GNUNET_CRYPTO_QUALITY_WEAK,
1827 (unsigned int) (60000 * putl * putl)));
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1830 GNUNET_STRINGS_relative_time_to_string (block_time,
1833 active_to_migration,
1834 (GNUNET_NO == prq.request_found));
1835 GSF_block_peer_migration_ (cp,
1836 GNUNET_TIME_relative_to_absolute (block_time));
1842 * Check if the given request is still active.
1844 * @param pr pending request
1845 * @return #GNUNET_YES if the request is still active
1848 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1850 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1855 * Setup the subsystem.
1858 GSF_pending_request_init_ ()
1861 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1862 "MAX_PENDING_REQUESTS",
1863 &max_pending_requests))
1865 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1866 "fs", "MAX_PENDING_REQUESTS");
1868 active_to_migration =
1869 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1870 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1871 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1872 requests_by_expiration_heap =
1873 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1878 * Shutdown the subsystem.
1881 GSF_pending_request_done_ ()
1883 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1886 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1888 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1889 requests_by_expiration_heap = NULL;
1890 GNUNET_LOAD_value_free (datastore_put_load);
1891 datastore_put_load = NULL;
1895 /* end of gnunet-service-fs_pr.c */