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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file fs/gnunet-service-fs_pr.c
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_load_lib.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_indexing.h"
32 #include "gnunet-service-fs_pe.h"
33 #include "gnunet-service-fs_pr.h"
34 #include "gnunet-service-fs_cadet.h"
38 * Desired replication level for GETs.
40 #define DHT_GET_REPLICATION 5
43 * Maximum size of the datastore queue for P2P operations. Needs to
44 * be large enough to queue #MAX_QUEUE_PER_PEER operations for roughly
45 * the number of active (connected) peers.
47 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
50 * Bandwidth value of a 0-priority content (must be fairly high
51 * compared to query since content is typically significantly larger
52 * -- and more valueable since it can take many queries to get one
55 #define CONTENT_BANDWIDTH_VALUE 800
58 * Hard limit on the number of results we may get from the datastore per query.
60 #define MAX_RESULTS (100 * 1024)
63 * Collect an instane number of statistics? May cause excessive IPC.
65 #define INSANE_STATISTICS GNUNET_NO
68 * If obtaining a block via cadet fails, how often do we retry it before
69 * giving up for good (and sticking to non-anonymous transfer)?
71 #define CADET_RETRY_MAX 3
77 struct GSF_PendingRequest
80 * Public data for the request.
82 struct GSF_PendingRequestData public_data;
85 * Function to call if we encounter a reply.
87 GSF_PendingRequestReplyHandler rh;
95 * Array of hash codes of replies we've already seen.
97 struct GNUNET_HashCode *replies_seen;
100 * Block group for filtering replies we've already seen.
102 struct GNUNET_BLOCK_Group *bg;
105 * Entry for this pending request in the expiration heap, or NULL.
107 struct GNUNET_CONTAINER_HeapNode *hnode;
110 * Datastore queue entry for this request (or NULL for none).
112 struct GNUNET_DATASTORE_QueueEntry *qe;
115 * DHT request handle for this request (or NULL for none).
117 struct GNUNET_DHT_GetHandle *gh;
120 * Cadet request handle for this request (or NULL for none).
122 struct GSF_CadetRequest *cadet_request;
125 * Function to call upon completion of the local get
126 * request, or NULL for none.
128 GSF_LocalLookupContinuation llc_cont;
131 * Closure for @e llc_cont.
136 * Last result from the local datastore lookup evaluation.
138 enum GNUNET_BLOCK_EvaluationResult local_result;
141 * Identity of the peer that we should use for the 'sender'
142 * (recipient of the response) when forwarding (0 for none).
144 GNUNET_PEER_Id sender_pid;
147 * Identity of the peer that we should never forward this query
148 * to since it originated this query (0 for none).
150 GNUNET_PEER_Id origin_pid;
153 * Time we started the last datastore lookup.
155 struct GNUNET_TIME_Absolute qe_start;
158 * Task that warns us if the local datastore lookup takes too long.
160 struct GNUNET_SCHEDULER_Task *warn_task;
163 * Do we have a first UID yet?
168 * Have we seen a NULL result yet?
173 * Unique ID of the first result from the local datastore;
174 * used to terminate the loop.
184 * How often have we retried this request via 'cadet'?
185 * (used to bound overall retries).
187 unsigned int cadet_retry_count;
190 * Number of valid entries in the 'replies_seen' array.
192 unsigned int replies_seen_count;
195 * Length of the 'replies_seen' array.
197 unsigned int replies_seen_size;
202 * All pending requests, ordered by the query. Entries
203 * are of type 'struct GSF_PendingRequest*'.
205 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
209 * Datastore 'PUT' load tracking.
211 static struct GNUNET_LOAD_Value *datastore_put_load;
215 * Are we allowed to migrate content to this peer.
217 static int active_to_migration;
221 * Heap with the request that will expire next at the top. Contains
222 * pointers of type "struct PendingRequest*"; these will *also* be
223 * aliased from the "requests_by_peer" data structures and the
224 * "requests_by_query" table. Note that requests from our clients
225 * don't expire and are thus NOT in the "requests_by_expiration"
226 * (or the "requests_by_peer" tables).
228 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
232 * Maximum number of requests (from other peers, overall) that we're
233 * willing to have pending at any given point in time. Can be changed
234 * via the configuration file (32k is just the default).
236 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, struct GSF_PendingRequest *pr)
254 GNUNET_BLOCK_group_destroy (pr->bg);
257 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
258 return; /* no need */
260 GNUNET_BLOCK_group_create (GSF_block_ctx,
262 GNUNET_CRYPTO_random_u32 (
263 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",
327 #if INSANE_STATISTICS
328 GNUNET_STATISTICS_update (GSF_stats,
329 gettext_noop ("# Pending requests created"),
335 extra += sizeof(struct GNUNET_PeerIdentity);
336 pr = GNUNET_malloc (sizeof(struct GSF_PendingRequest) + extra);
337 pr->public_data.query = *query;
338 eptr = (struct GNUNET_HashCode *) &pr[1];
341 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
342 GNUNET_memcpy (eptr, target, 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 = GNUNET_TIME_relative_to_absolute (
357 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (uint32_t) ttl));
359 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (
360 pr->public_data.start_time,
361 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
363 if (replies_seen_count > 0)
365 pr->replies_seen_size = replies_seen_count;
367 GNUNET_new_array (pr->replies_seen_size, struct GNUNET_HashCode);
368 GNUNET_memcpy (pr->replies_seen,
370 replies_seen_count * sizeof(struct GNUNET_HashCode));
371 pr->replies_seen_count = replies_seen_count;
373 if ((NULL != bf_data) &&
374 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type))
376 pr->bg = GNUNET_BLOCK_group_create (GSF_block_ctx,
377 pr->public_data.type,
385 else if ((replies_seen_count > 0) &&
386 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
388 refresh_bloomfilter (pr->public_data.type, pr);
390 GNUNET_CONTAINER_multihashmap_put (pr_map,
391 &pr->public_data.query,
393 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
394 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
396 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
398 pr->public_data.ttl.abs_value_us);
399 /* make sure we don't track too many requests */
400 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
401 max_pending_requests)
403 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
404 GNUNET_assert (NULL != dpr);
406 break; /* let the request live briefly... */
408 dpr->rh (dpr->rh_cls,
409 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
412 GNUNET_TIME_UNIT_FOREVER_ABS,
413 GNUNET_TIME_UNIT_FOREVER_ABS,
414 GNUNET_BLOCK_TYPE_ANY,
417 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
420 GNUNET_STATISTICS_update (GSF_stats,
421 gettext_noop ("# Pending requests active"),
429 * Obtain the public data associated with a pending request
431 * @param pr pending request
432 * @return associated public data
434 struct GSF_PendingRequestData *
435 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
437 return &pr->public_data;
442 * Test if two pending requests are compatible (would generate
443 * the same query modulo filters and should thus be processed
446 * @param pra a pending request
447 * @param prb another pending request
448 * @return #GNUNET_OK if the requests are compatible
451 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
452 struct GSF_PendingRequest *prb)
454 if ((pra->public_data.type != prb->public_data.type) ||
455 (0 != memcmp (&pra->public_data.query,
456 &prb->public_data.query,
457 sizeof(struct GNUNET_HashCode))))
464 * Update a given pending request with additional replies
465 * that have been seen.
467 * @param pr request to update
468 * @param replies_seen hash codes of replies that we've seen
469 * @param replies_seen_count size of the replies_seen array
472 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
473 const struct GNUNET_HashCode *replies_seen,
474 unsigned int replies_seen_count)
476 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
477 return; /* integer overflow */
478 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
480 /* we're responsible for the BF, full refresh */
481 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
482 GNUNET_array_grow (pr->replies_seen,
483 pr->replies_seen_size,
484 replies_seen_count + pr->replies_seen_count);
485 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
487 sizeof(struct GNUNET_HashCode) * replies_seen_count);
488 pr->replies_seen_count += replies_seen_count;
489 refresh_bloomfilter (pr->public_data.type, pr);
495 /* we're not the initiator, but the initiator did not give us
496 * any bloom-filter, so we need to create one on-the-fly */
497 refresh_bloomfilter (pr->public_data.type, pr);
501 GNUNET_break (GNUNET_OK ==
502 GNUNET_BLOCK_group_set_seen (pr->bg,
504 pr->replies_seen_count));
508 GNUNET_DHT_get_filter_known_results (pr->gh,
515 * Generate the message corresponding to the given pending request for
516 * transmission to other peers.
518 * @param pr request to generate the message for
519 * @return envelope with the request message
521 struct GNUNET_MQ_Envelope *
522 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
524 struct GNUNET_MQ_Envelope *env;
525 struct GetMessage *gm;
526 struct GNUNET_PeerIdentity *ext;
531 struct GNUNET_TIME_Absolute now;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Building request message for `%s' of type %d\n",
539 GNUNET_h2s (&pr->public_data.query),
540 pr->public_data.type);
543 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
544 if ((! do_route) && (pr->sender_pid == 0))
547 do_route = GNUNET_YES;
551 bm |= GET_MESSAGE_BIT_RETURN_TO;
554 if (NULL != pr->public_data.target)
556 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
560 GNUNET_BLOCK_group_serialize (pr->bg, &bf_nonce, &bf_data, &bf_size))
565 env = GNUNET_MQ_msg_extra (gm,
566 bf_size + k * sizeof(struct GNUNET_PeerIdentity),
567 GNUNET_MESSAGE_TYPE_FS_GET);
568 gm->type = htonl (pr->public_data.type);
570 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
571 pr->public_data.priority + 1);
574 pr->public_data.priority -= prio;
575 pr->public_data.num_transmissions++;
576 pr->public_data.respect_offered += prio;
577 gm->priority = htonl (prio);
578 now = GNUNET_TIME_absolute_get ();
579 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
580 gm->ttl = htonl (ttl / 1000LL / 1000LL);
581 gm->filter_mutator = htonl (bf_nonce);
582 gm->hash_bitmap = htonl (bm);
583 gm->query = pr->public_data.query;
584 ext = (struct GNUNET_PeerIdentity *) &gm[1];
587 GNUNET_PEER_resolve (pr->sender_pid, &ext[k++]);
588 if (NULL != pr->public_data.target)
589 ext[k++] = *pr->public_data.target;
590 GNUNET_memcpy (&ext[k], bf_data, bf_size);
591 GNUNET_free_non_null (bf_data);
597 * Iterator to free pending requests.
599 * @param cls closure, unused
600 * @param key current key code
601 * @param value value in the hash map (pending request)
602 * @return #GNUNET_YES (we should continue to iterate)
605 clean_request (void *cls, const struct GNUNET_HashCode *key, void *value)
607 struct GSF_PendingRequest *pr = value;
608 GSF_LocalLookupContinuation cont;
610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 "Cleaning up pending request for `%s'.\n",
613 if (NULL != pr->cadet_request)
615 pr->cadet_retry_count = CADET_RETRY_MAX;
616 GSF_cadet_query_cancel (pr->cadet_request);
617 pr->cadet_request = NULL;
619 if (NULL != (cont = pr->llc_cont))
622 cont (pr->llc_cont_cls, pr, pr->local_result);
624 GSF_plan_notify_request_done_ (pr);
625 GNUNET_free_non_null (pr->replies_seen);
626 GNUNET_BLOCK_group_destroy (pr->bg);
628 GNUNET_PEER_change_rc (pr->sender_pid, -1);
630 GNUNET_PEER_change_rc (pr->origin_pid, -1);
632 if (NULL != pr->hnode)
634 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
639 GNUNET_DATASTORE_cancel (pr->qe);
644 GNUNET_DHT_get_stop (pr->gh);
647 if (NULL != pr->warn_task)
649 GNUNET_SCHEDULER_cancel (pr->warn_task);
650 pr->warn_task = NULL;
654 GNUNET_CONTAINER_multihashmap_remove (pr_map, &pr->public_data.query, pr));
655 GNUNET_STATISTICS_update (GSF_stats,
656 gettext_noop ("# Pending requests active"),
665 * Explicitly cancel a pending request.
667 * @param pr request to cancel
668 * @param full_cleanup fully purge the request
671 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
673 GSF_LocalLookupContinuation cont;
676 return; /* already cleaned up! */
677 if (GNUNET_NO == full_cleanup)
679 /* make request inactive (we're no longer interested in more results),
680 * but do NOT remove from our data-structures, we still need it there
681 * to prevent the request from looping */
683 if (NULL != pr->cadet_request)
685 pr->cadet_retry_count = CADET_RETRY_MAX;
686 GSF_cadet_query_cancel (pr->cadet_request);
687 pr->cadet_request = NULL;
689 if (NULL != (cont = pr->llc_cont))
692 cont (pr->llc_cont_cls, pr, pr->local_result);
694 GSF_plan_notify_request_done_ (pr);
697 GNUNET_DATASTORE_cancel (pr->qe);
702 GNUNET_DHT_get_stop (pr->gh);
705 if (NULL != pr->warn_task)
707 GNUNET_SCHEDULER_cancel (pr->warn_task);
708 pr->warn_task = NULL;
712 GNUNET_assert (GNUNET_YES ==
713 clean_request (NULL, &pr->public_data.query, pr));
718 * Iterate over all pending requests.
720 * @param it function to call for each request
721 * @param cls closure for @a it
724 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
726 GNUNET_CONTAINER_multihashmap_iterate (
728 (GNUNET_CONTAINER_MulitHashMapIteratorCallback) it,
734 * Closure for process_reply() function.
736 struct ProcessReplyClosure
739 * The data for the reply.
744 * Who gave us this reply? NULL for local host (or DHT)
746 struct GSF_ConnectedPeer *sender;
749 * When the reply expires.
751 struct GNUNET_TIME_Absolute expiration;
761 enum GNUNET_BLOCK_Type type;
764 * Control flags for evaluation.
766 enum GNUNET_BLOCK_EvaluationOptions eo;
769 * How much was this reply worth to us?
774 * Anonymity requirements for this reply.
776 uint32_t anonymity_level;
779 * Evaluation result (returned).
781 enum GNUNET_BLOCK_EvaluationResult eval;
784 * Did we find a matching request?
791 * Update the performance data for the sender (if any) since
792 * the sender successfully answered one of our queries.
794 * @param prq information about the sender
795 * @param pr request that was satisfied
798 update_request_performance_data (struct ProcessReplyClosure *prq,
799 struct GSF_PendingRequest *pr)
801 if (prq->sender == NULL)
803 GSF_peer_update_performance_ (prq->sender,
804 pr->public_data.start_time,
810 * We have received a reply; handle it!
812 * @param cls response (a `struct ProcessReplyClosure`)
813 * @param key our query
814 * @param value value in the hash map (info about the query)
815 * @return #GNUNET_YES (we should continue to iterate)
818 process_reply (void *cls, const struct GNUNET_HashCode *key, void *value)
820 struct ProcessReplyClosure *prq = cls;
821 struct GSF_PendingRequest *pr = value;
822 struct GNUNET_HashCode chash;
823 struct GNUNET_TIME_Absolute last_transmission;
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Matched result (type %u) for query `%s' with pending request\n",
829 (unsigned int) prq->type,
831 GNUNET_STATISTICS_update (GSF_stats,
832 gettext_noop ("# replies received and matched"),
835 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
846 case GNUNET_BLOCK_EVALUATION_OK_MORE:
847 update_request_performance_data (prq, pr);
850 case GNUNET_BLOCK_EVALUATION_OK_LAST:
851 /* short cut: stop processing early, no BF-update, etc. */
852 update_request_performance_data (prq, pr);
853 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
854 GNUNET_TIME_absolute_get_duration (
855 pr->public_data.start_time)
858 GSF_request_plan_reference_get_last_transmission_ (pr->public_data
862 last_transmission.abs_value_us =
863 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
864 /* pass on to other peers / local clients */
868 prq->anonymity_level,
876 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
877 #if INSANE_STATISTICS
878 GNUNET_STATISTICS_update (GSF_stats,
880 "# duplicate replies discarded (bloomfilter)"),
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Duplicate response, discarding.\n");
885 return GNUNET_YES; /* duplicate */
887 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
888 GNUNET_STATISTICS_update (GSF_stats,
889 gettext_noop ("# irrelevant replies discarded"),
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Irrelevant response, ignoring.\n");
895 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
896 return GNUNET_YES; /* wrong namespace */
898 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
902 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
906 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
907 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
908 _ ("Unsupported block type %u\n"),
912 /* update bloomfilter */
913 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
914 GSF_pending_request_update_ (pr, &chash, 1);
915 if (NULL == prq->sender)
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Found result for query `%s' in local datastore\n",
920 GNUNET_STATISTICS_update (GSF_stats,
921 gettext_noop ("# results found locally"),
927 GSF_dht_lookup_ (pr);
929 prq->priority += pr->public_data.original_priority;
930 pr->public_data.priority = 0;
931 pr->public_data.original_priority = 0;
932 pr->public_data.results_found++;
933 prq->request_found = GNUNET_YES;
934 /* finally, pass on to other peer / local client */
935 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data
939 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
943 prq->anonymity_level,
954 * Context for put_migration_continuation().
956 struct PutMigrationContext
959 * Start time for the operation.
961 struct GNUNET_TIME_Absolute start;
966 struct GNUNET_PeerIdentity origin;
969 * #GNUNET_YES if we had a matching request for this block,
977 * Continuation called to notify client about result of the
981 * @param success #GNUNET_SYSERR on failure
982 * @param min_expiration minimum expiration time required for content to be stored
983 * @param msg NULL on success, otherwise an error message
986 put_migration_continuation (void *cls,
988 struct GNUNET_TIME_Absolute min_expiration,
991 struct PutMigrationContext *pmc = cls;
992 struct GSF_ConnectedPeer *cp;
993 struct GNUNET_TIME_Relative mig_pause;
994 struct GSF_PeerPerformanceData *ppd;
996 if (NULL != datastore_put_load)
998 if (GNUNET_SYSERR != success)
1000 GNUNET_LOAD_update (datastore_put_load,
1001 GNUNET_TIME_absolute_get_duration (pmc->start)
1006 /* on queue failure / timeout, increase the put load dramatically */
1007 GNUNET_LOAD_update (datastore_put_load,
1008 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
1011 cp = GSF_peer_get_ (&pmc->origin);
1012 if (GNUNET_OK == success)
1016 ppd = GSF_get_peer_performance_data_ (cp);
1017 ppd->migration_delay.rel_value_us /= 2;
1022 if ((GNUNET_NO == success) && (GNUNET_NO == pmc->requested) && (NULL != cp))
1024 ppd = GSF_get_peer_performance_data_ (cp);
1025 if (min_expiration.abs_value_us > 0)
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Asking to stop migration for %s because datastore is full\n",
1029 GNUNET_STRINGS_relative_time_to_string (
1030 GNUNET_TIME_absolute_get_remaining (min_expiration),
1032 GSF_block_peer_migration_ (cp, min_expiration);
1036 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1037 ppd->migration_delay);
1038 ppd->migration_delay =
1039 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS, ppd->migration_delay);
1040 mig_pause.rel_value_us =
1041 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1042 ppd->migration_delay.rel_value_us);
1043 ppd->migration_delay =
1044 GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1046 GNUNET_ERROR_TYPE_DEBUG,
1047 "Replicated content already exists locally, asking to stop migration for %s\n",
1048 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
1049 GSF_block_peer_migration_ (cp,
1050 GNUNET_TIME_relative_to_absolute (mig_pause));
1054 GNUNET_STATISTICS_update (GSF_stats,
1055 gettext_noop ("# Datastore `PUT' failures"),
1062 * Test if the DATABASE (PUT) load on this peer is too high
1063 * to even consider processing the query at
1066 * @param priority the priority of the item
1067 * @return #GNUNET_YES if the load is too high to do anything (load high)
1068 * #GNUNET_NO to process normally (load normal or low)
1071 test_put_load_too_high (uint32_t priority)
1075 if (NULL == datastore_put_load)
1077 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1078 return GNUNET_NO; /* very fast */
1079 ld = GNUNET_LOAD_get_load (datastore_put_load);
1080 if (ld < 2.0 * (1 + priority))
1082 GNUNET_STATISTICS_update (GSF_stats,
1084 "# storage requests dropped due to high load"),
1092 * Iterator called on each result obtained for a DHT
1093 * operation that expects a reply
1095 * @param cls closure
1096 * @param exp when will this value expire
1097 * @param key key of the result
1098 * @param get_path peers on reply path (or NULL if not recorded)
1099 * @param get_path_length number of entries in @a get_path
1100 * @param put_path peers on the PUT path (or NULL if not recorded)
1101 * @param put_path_length number of entries in @a get_path
1102 * @param type type of the result
1103 * @param size number of bytes in @a data
1104 * @param data pointer to the result data
1107 handle_dht_reply (void *cls,
1108 struct GNUNET_TIME_Absolute exp,
1109 const struct GNUNET_HashCode *key,
1110 const struct GNUNET_PeerIdentity *get_path,
1111 unsigned int get_path_length,
1112 const struct GNUNET_PeerIdentity *put_path,
1113 unsigned int put_path_length,
1114 enum GNUNET_BLOCK_Type type,
1118 struct GSF_PendingRequest *pr = cls;
1119 struct ProcessReplyClosure prq;
1120 struct PutMigrationContext *pmc;
1122 GNUNET_STATISTICS_update (GSF_stats,
1123 gettext_noop ("# Replies received from DHT"),
1126 memset (&prq, 0, sizeof(prq));
1128 prq.expiration = exp;
1129 /* do not allow migrated content to live longer than 1 year */
1130 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1131 GNUNET_TIME_UNIT_YEARS),
1135 prq.eo = GNUNET_BLOCK_EO_NONE;
1136 process_reply (&prq, key, pr);
1137 if ((GNUNET_YES == active_to_migration) &&
1138 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Replicating result for query `%s' with priority %u\n",
1144 pmc = GNUNET_new (struct PutMigrationContext);
1145 pmc->start = GNUNET_TIME_absolute_get ();
1146 pmc->requested = GNUNET_YES;
1147 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1155 0 /* replication */,
1158 MAX_DATASTORE_QUEUE,
1159 &put_migration_continuation,
1162 put_migration_continuation (pmc,
1164 GNUNET_TIME_UNIT_ZERO_ABS,
1172 * Consider looking up the data in the DHT (anonymity-level permitting).
1174 * @param pr the pending request to process
1177 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1181 struct GNUNET_PeerIdentity pi;
1182 char buf[sizeof(struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1184 if (0 != pr->public_data.anonymity_level)
1188 GNUNET_DHT_get_stop (pr->gh);
1193 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1195 GNUNET_assert (0 != pr->sender_pid);
1196 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1197 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof(struct GNUNET_PeerIdentity));
1198 xquery_size += sizeof(struct GNUNET_PeerIdentity);
1200 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1201 pr->public_data.type,
1202 &pr->public_data.query,
1203 DHT_GET_REPLICATION,
1204 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1209 if ((NULL != pr->gh) && (0 != pr->replies_seen_count))
1210 GNUNET_DHT_get_filter_known_results (pr->gh,
1211 pr->replies_seen_count,
1217 * Function called with a reply from the cadet.
1219 * @param cls the pending request struct
1220 * @param type type of the block, ANY on error
1221 * @param expiration expiration time for the block
1222 * @param data_size number of bytes in @a data, 0 on error
1223 * @param data reply block data, NULL on error
1226 cadet_reply_proc (void *cls,
1227 enum GNUNET_BLOCK_Type type,
1228 struct GNUNET_TIME_Absolute expiration,
1232 struct GSF_PendingRequest *pr = cls;
1233 struct ProcessReplyClosure prq;
1234 struct GNUNET_HashCode query;
1236 pr->cadet_request = NULL;
1237 if (GNUNET_BLOCK_TYPE_ANY == type)
1239 GNUNET_break (NULL == data);
1240 GNUNET_break (0 == data_size);
1241 pr->cadet_retry_count++;
1242 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1243 return; /* give up on cadet */
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error retrieiving block via cadet\n");
1245 /* retry -- without delay, as this is non-anonymous
1246 and cadet/cadet connect will take some time anyway */
1247 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1248 &pr->public_data.query,
1249 pr->public_data.type,
1255 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, data_size, &query))
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Failed to derive key for block of type %d\n",
1260 GNUNET_break_op (0);
1263 GNUNET_STATISTICS_update (GSF_stats,
1264 gettext_noop ("# Replies received from CADET"),
1267 memset (&prq, 0, sizeof(prq));
1269 prq.expiration = expiration;
1270 /* do not allow migrated content to live longer than 1 year */
1271 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1272 GNUNET_TIME_UNIT_YEARS),
1274 prq.size = data_size;
1276 prq.eo = GNUNET_BLOCK_EO_NONE;
1277 process_reply (&prq, &query, pr);
1282 * Consider downloading via cadet (if possible)
1284 * @param pr the pending request to process
1287 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1289 if (0 != pr->public_data.anonymity_level)
1291 if (0 == pr->public_data.target)
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Cannot do cadet-based download, target peer not known\n");
1297 if (NULL != pr->cadet_request)
1299 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1300 &pr->public_data.query,
1301 pr->public_data.type,
1308 * Task that issues a warning if the datastore lookup takes too long.
1310 * @param cls the `struct GSF_PendingRequest`
1313 warn_delay_task (void *cls)
1315 struct GSF_PendingRequest *pr = cls;
1317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1318 _ ("Datastore lookup already took %s!\n"),
1319 GNUNET_STRINGS_relative_time_to_string (
1320 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1322 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1329 * Task that issues a warning if the datastore lookup takes too long.
1331 * @param cls the `struct GSF_PendingRequest`
1334 odc_warn_delay_task (void *cls)
1336 struct GSF_PendingRequest *pr = cls;
1338 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1339 _ ("On-demand lookup already took %s!\n"),
1340 GNUNET_STRINGS_relative_time_to_string (
1341 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1343 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1344 &odc_warn_delay_task,
1349 /* Call our continuation (if we have any) */
1351 call_continuation (struct GSF_PendingRequest *pr)
1353 GSF_LocalLookupContinuation cont = pr->llc_cont;
1355 GNUNET_assert (NULL == pr->qe);
1356 if (NULL != pr->warn_task)
1358 GNUNET_SCHEDULER_cancel (pr->warn_task);
1359 pr->warn_task = NULL;
1362 return; /* no continuation */
1363 pr->llc_cont = NULL;
1364 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1366 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1368 /* Signal that we are done and that there won't be any
1369 additional results to allow client to clean up state. */
1371 GNUNET_BLOCK_EVALUATION_OK_LAST,
1374 GNUNET_TIME_UNIT_ZERO_ABS,
1375 GNUNET_TIME_UNIT_FOREVER_ABS,
1376 GNUNET_BLOCK_TYPE_ANY,
1380 /* Finally, call our continuation to signal that we are
1381 done with local processing of this request; i.e. to
1382 start reading again from the client. */
1383 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1387 cont (pr->llc_cont_cls, pr, pr->local_result);
1391 /* Update stats and call continuation */
1393 no_more_local_results (struct GSF_PendingRequest *pr)
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1396 "No further local responses available.\n");
1397 #if INSANE_STATISTICS
1398 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == pr->public_data.type) ||
1399 (GNUNET_BLOCK_TYPE_FS_IBLOCK == pr->public_data.type))
1400 GNUNET_STATISTICS_update (GSF_stats,
1402 "# requested DBLOCK or IBLOCK not found"),
1406 call_continuation (pr);
1410 /* forward declaration */
1412 process_local_reply (void *cls,
1413 const struct GNUNET_HashCode *key,
1416 enum GNUNET_BLOCK_Type type,
1419 uint32_t replication,
1420 struct GNUNET_TIME_Absolute expiration,
1424 /* Start a local query */
1426 start_local_query (struct GSF_PendingRequest *pr,
1430 pr->qe_start = GNUNET_TIME_absolute_get ();
1431 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1434 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1437 &pr->public_data.query,
1438 pr->public_data.type ==
1439 GNUNET_BLOCK_TYPE_FS_DBLOCK
1440 ? GNUNET_BLOCK_TYPE_ANY
1441 : pr->public_data.type,
1442 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1443 & pr->public_data.options))
1446 /* queue priority */,
1447 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1448 & pr->public_data.options))
1450 : GSF_datastore_queue_size
1451 /* max queue size */,
1452 &process_local_reply,
1457 GNUNET_ERROR_TYPE_DEBUG,
1458 "ERROR Requesting `%s' of type %d with next_uid %llu from datastore.\n",
1459 GNUNET_h2s (&pr->public_data.query),
1460 pr->public_data.type,
1461 (unsigned long long) next_uid);
1462 GNUNET_STATISTICS_update (GSF_stats,
1464 "# Datastore lookups concluded (error queueing)"),
1467 call_continuation (pr);
1472 * We're processing (local) results for a search request
1473 * from another peer. Pass applicable results to the
1474 * peer and if we are done either clean up (operation
1475 * complete) or forward to other peers (more results possible).
1477 * @param cls our closure (`struct GSF_PendingRequest *`)
1478 * @param key key for the content
1479 * @param size number of bytes in @a data
1480 * @param data content stored
1481 * @param type type of the content
1482 * @param priority priority of the content
1483 * @param anonymity anonymity-level for the content
1484 * @param replication replication-level for the content
1485 * @param expiration expiration time for the content
1486 * @param uid unique identifier for the datum;
1487 * maybe 0 if no unique identifier is available
1490 process_local_reply (void *cls,
1491 const struct GNUNET_HashCode *key,
1494 enum GNUNET_BLOCK_Type type,
1497 uint32_t replication,
1498 struct GNUNET_TIME_Absolute expiration,
1501 struct GSF_PendingRequest *pr = cls;
1502 struct ProcessReplyClosure prq;
1503 struct GNUNET_HashCode query;
1504 unsigned int old_rf;
1506 GNUNET_SCHEDULER_cancel (pr->warn_task);
1507 pr->warn_task = NULL;
1509 goto called_from_on_demand;
1512 (NULL == key) && pr->seen_null &&
1513 ! pr->have_first_uid) /* We have hit the end for the 2nd time with no results */
1516 #if INSANE_STATISTICS
1517 GNUNET_STATISTICS_update (GSF_stats,
1519 "# Datastore lookups concluded (no results)"),
1523 no_more_local_results (pr);
1526 if (((NULL == key) &&
1527 pr->seen_null) || /* We have hit the end for the 2nd time OR */
1528 (pr->seen_null && pr->have_first_uid &&
1529 (uid >= pr->first_uid))) /* We have hit the end and past first UID */
1531 /* Seen all results */
1532 GNUNET_STATISTICS_update (GSF_stats,
1534 "# Datastore lookups concluded (seen all)"),
1537 no_more_local_results (pr);
1542 GNUNET_assert (! pr->seen_null);
1543 pr->seen_null = true;
1544 start_local_query (pr, 0 /* next_uid */, false /* random */);
1547 if (! pr->have_first_uid)
1549 pr->first_uid = uid;
1550 pr->have_first_uid = true;
1553 if (pr->result_count > MAX_RESULTS)
1555 GNUNET_STATISTICS_update (
1557 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1560 no_more_local_results (pr);
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1567 (unsigned long long) uid);
1568 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 "Found ONDEMAND block, performing on-demand encoding\n");
1572 GNUNET_STATISTICS_update (GSF_stats,
1574 "# on-demand blocks matched requests"),
1577 pr->qe_start = GNUNET_TIME_absolute_get ();
1578 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1579 &odc_warn_delay_task,
1581 if (GNUNET_OK == GNUNET_FS_handle_on_demand_block (key,
1590 &process_local_reply,
1593 GNUNET_STATISTICS_update (GSF_stats,
1595 "# on-demand lookups performed successfully"),
1598 return; /* we're done */
1600 GNUNET_STATISTICS_update (GSF_stats,
1601 gettext_noop ("# on-demand lookups failed"),
1604 GNUNET_SCHEDULER_cancel (pr->warn_task);
1605 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1608 called_from_on_demand:
1609 old_rf = pr->public_data.results_found;
1610 memset (&prq, 0, sizeof(prq));
1612 prq.expiration = expiration;
1615 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1618 GNUNET_DATASTORE_remove (GSF_dsh,
1626 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1630 prq.priority = priority;
1631 prq.request_found = GNUNET_NO;
1632 prq.anonymity_level = anonymity;
1633 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1634 GSF_update_datastore_delay_ (pr->public_data.start_time);
1635 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1636 process_reply (&prq, key, pr);
1637 pr->local_result = prq.eval;
1638 if (GNUNET_BLOCK_EVALUATION_OK_LAST == prq.eval)
1640 GNUNET_STATISTICS_update (
1642 gettext_noop ("# Datastore lookups concluded (found last result)"),
1645 call_continuation (pr);
1648 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1649 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1650 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1653 GNUNET_STATISTICS_update (GSF_stats,
1655 "# Datastore lookups concluded (load too high)"),
1658 call_continuation (pr);
1661 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1666 * Is the given target a legitimate peer for forwarding the given request?
1670 * @return #GNUNET_YES if this request could be forwarded to the given peer
1673 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1674 const struct GNUNET_PeerIdentity *target)
1676 struct GNUNET_PeerIdentity pi;
1678 if (0 == pr->origin_pid)
1680 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1681 return (0 == memcmp (&pi, target, sizeof(struct GNUNET_PeerIdentity)))
1688 * Look up the request in the local datastore.
1690 * @param pr the pending request to process
1691 * @param cont function to call at the end
1692 * @param cont_cls closure for @a cont
1695 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1696 GSF_LocalLookupContinuation cont,
1699 GNUNET_assert (NULL == pr->gh);
1700 GNUNET_assert (NULL == pr->cadet_request);
1701 GNUNET_assert (NULL == pr->llc_cont);
1702 pr->llc_cont = cont;
1703 pr->llc_cont_cls = cont_cls;
1704 #if INSANE_STATISTICS
1705 GNUNET_STATISTICS_update (GSF_stats,
1706 gettext_noop ("# Datastore lookups initiated"),
1710 start_local_query (pr, 0 /* next_uid */, true /* random */);
1715 * Handle P2P "CONTENT" message. Checks that the message is
1716 * well-formed and then checks if there are any pending requests for
1717 * this content and possibly passes it on (to local clients or other
1718 * peers). Does NOT perform migration (content caching at this peer).
1720 * @param cls the other peer involved
1721 * @param put the actual message
1724 handle_p2p_put (void *cls, const struct PutMessage *put)
1726 struct GSF_ConnectedPeer *cp = cls;
1729 enum GNUNET_BLOCK_Type type;
1730 struct GNUNET_TIME_Absolute expiration;
1731 struct GNUNET_HashCode query;
1732 struct ProcessReplyClosure prq;
1733 struct GNUNET_TIME_Relative block_time;
1735 struct PutMigrationContext *pmc;
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Received P2P PUT from %s\n",
1739 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1740 GSF_cover_content_count++;
1741 msize = ntohs (put->header.size);
1742 dsize = msize - sizeof(struct PutMessage);
1743 type = ntohl (put->type);
1744 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1745 /* do not allow migrated content to live longer than 1 year */
1746 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1747 GNUNET_TIME_UNIT_YEARS),
1750 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1752 GNUNET_break_op (0);
1755 GNUNET_STATISTICS_update (GSF_stats,
1756 gettext_noop ("# GAP PUT messages received"),
1759 /* now, lookup 'query' */
1760 prq.data = (const void *) &put[1];
1764 prq.expiration = expiration;
1766 prq.anonymity_level = UINT32_MAX;
1767 prq.request_found = GNUNET_NO;
1768 prq.eo = GNUNET_BLOCK_EO_NONE;
1769 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1775 GSF_connected_peer_change_preference_ (cp,
1776 CONTENT_BANDWIDTH_VALUE
1777 + 1000 * prq.priority);
1778 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1780 if ((GNUNET_YES == active_to_migration) && (NULL != cp) &&
1781 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1784 "Replicating result for query `%s' with priority %u\n",
1785 GNUNET_h2s (&query),
1787 pmc = GNUNET_new (struct PutMigrationContext);
1788 pmc->start = GNUNET_TIME_absolute_get ();
1789 pmc->requested = prq.request_found;
1790 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1791 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1793 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1801 0 /* replication */,
1804 MAX_DATASTORE_QUEUE,
1805 &put_migration_continuation,
1808 put_migration_continuation (pmc,
1810 GNUNET_TIME_UNIT_ZERO_ABS,
1814 else if (NULL != cp)
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Choosing not to keep content `%s' (%d/%d)\n",
1818 GNUNET_h2s (&query),
1819 active_to_migration,
1820 test_put_load_too_high (prq.priority));
1822 putl = GNUNET_LOAD_get_load (datastore_put_load);
1823 if ((NULL != cp) && (GNUNET_NO == prq.request_found) &&
1824 ((GNUNET_YES != active_to_migration) ||
1825 (putl > 2.5 * (1 + prq.priority))))
1827 if (GNUNET_YES != active_to_migration)
1828 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1829 block_time = GNUNET_TIME_relative_multiply (
1830 GNUNET_TIME_UNIT_MILLISECONDS,
1831 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1832 (unsigned int) (60000 * putl * putl)));
1834 GNUNET_ERROR_TYPE_DEBUG,
1835 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1836 GNUNET_STRINGS_relative_time_to_string (block_time, GNUNET_YES),
1838 active_to_migration,
1839 (GNUNET_NO == prq.request_found));
1840 GSF_block_peer_migration_ (cp,
1841 GNUNET_TIME_relative_to_absolute (block_time));
1847 * Check if the given request is still active.
1849 * @param pr pending request
1850 * @return #GNUNET_YES if the request is still active
1853 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1855 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1860 * Setup the subsystem.
1863 GSF_pending_request_init_ ()
1866 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1868 "MAX_PENDING_REQUESTS",
1869 &max_pending_requests))
1871 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1873 "MAX_PENDING_REQUESTS");
1875 active_to_migration =
1876 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1877 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1878 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1879 requests_by_expiration_heap =
1880 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1885 * Shutdown the subsystem.
1888 GSF_pending_request_done_ ()
1890 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1891 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1893 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1894 requests_by_expiration_heap = NULL;
1895 GNUNET_LOAD_value_free (datastore_put_load);
1896 datastore_put_load = NULL;
1900 /* end of gnunet-service-fs_pr.c */