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;
203 * All pending requests, ordered by the query. Entries
204 * are of type 'struct GSF_PendingRequest*'.
206 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
210 * Datastore 'PUT' load tracking.
212 static struct GNUNET_LOAD_Value *datastore_put_load;
216 * Are we allowed to migrate content to this peer.
218 static int active_to_migration;
222 * Heap with the request that will expire next at the top. Contains
223 * pointers of type "struct PendingRequest*"; these will *also* be
224 * aliased from the "requests_by_peer" data structures and the
225 * "requests_by_query" table. Note that requests from our clients
226 * don't expire and are thus NOT in the "requests_by_expiration"
227 * (or the "requests_by_peer" tables).
229 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
233 * Maximum number of requests (from other peers, overall) that we're
234 * willing to have pending at any given point in time. Can be changed
235 * via the configuration file (32k is just the default).
237 static unsigned long long max_pending_requests = (32 * 1024);
242 * Recalculate our bloom filter for filtering replies. This function
243 * will create a new bloom filter from scratch, so it should only be
244 * called if we have no bloomfilter at all (and hence can create a
245 * fresh one of minimal size without problems) OR if our peer is the
246 * initiator (in which case we may resize to larger than mimimum size).
248 * @param type type of the request
249 * @param pr request for which the BF is to be recomputed
252 refresh_bloomfilter (enum GNUNET_BLOCK_Type type,
253 struct GSF_PendingRequest *pr)
257 GNUNET_BLOCK_group_destroy (pr->bg);
260 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
261 return; /* no need */
263 = GNUNET_BLOCK_group_create (GSF_block_ctx,
265 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
270 pr->replies_seen_count,
274 GNUNET_break (GNUNET_OK ==
275 GNUNET_BLOCK_group_set_seen (pr->bg,
277 pr->replies_seen_count));
282 * Create a new pending request.
284 * @param options request options
285 * @param type type of the block that is being requested
286 * @param query key for the lookup
287 * @param target preferred target for the request, NULL for none
288 * @param bf_data raw data for bloom filter for known replies, can be NULL
289 * @param bf_size number of bytes in @a bf_data
290 * @param mingle mingle value for bf
291 * @param anonymity_level desired anonymity level
292 * @param priority maximum outgoing cummulative request priority to use
293 * @param ttl current time-to-live for the request
294 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
295 * @param origin_pid peer ID of origin of query (do not loop back)
296 * @param replies_seen hash codes of known local replies
297 * @param replies_seen_count size of the @a replies_seen array
298 * @param rh handle to call when we get a reply
299 * @param rh_cls closure for @a rh
300 * @return handle for the new pending request
302 struct GSF_PendingRequest *
303 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
304 enum GNUNET_BLOCK_Type type,
305 const struct GNUNET_HashCode *query,
306 const struct GNUNET_PeerIdentity *target,
310 uint32_t anonymity_level,
313 GNUNET_PEER_Id sender_pid,
314 GNUNET_PEER_Id origin_pid,
315 const struct GNUNET_HashCode *replies_seen,
316 unsigned int replies_seen_count,
317 GSF_PendingRequestReplyHandler rh,
320 struct GSF_PendingRequest *pr;
321 struct GSF_PendingRequest *dpr;
323 struct GNUNET_HashCode *eptr;
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Creating request handle for `%s' of type %d\n",
327 GNUNET_h2s (query), type);
328 #if INSANE_STATISTICS
329 GNUNET_STATISTICS_update (GSF_stats,
330 gettext_noop ("# Pending requests created"), 1,
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;
344 sizeof (struct GNUNET_PeerIdentity));
346 pr->public_data.anonymity_level = anonymity_level;
347 pr->public_data.priority = priority;
348 pr->public_data.original_priority = priority;
349 pr->public_data.options = options;
350 pr->public_data.type = type;
351 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
352 pr->sender_pid = sender_pid;
353 pr->origin_pid = origin_pid;
356 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
358 pr->public_data.ttl =
359 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
360 (GNUNET_TIME_UNIT_SECONDS,
363 pr->public_data.ttl =
364 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
365 GNUNET_TIME_relative_multiply
366 (GNUNET_TIME_UNIT_SECONDS,
368 if (replies_seen_count > 0)
370 pr->replies_seen_size = replies_seen_count;
371 pr->replies_seen = GNUNET_new_array (pr->replies_seen_size,
372 struct GNUNET_HashCode);
373 GNUNET_memcpy (pr->replies_seen,
375 replies_seen_count * sizeof (struct GNUNET_HashCode));
376 pr->replies_seen_count = replies_seen_count;
378 if ( (NULL != bf_data) &&
379 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type) )
382 = GNUNET_BLOCK_group_create (GSF_block_ctx,
383 pr->public_data.type,
391 else if ((replies_seen_count > 0) &&
392 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
394 refresh_bloomfilter (pr->public_data.type,
397 GNUNET_CONTAINER_multihashmap_put (pr_map,
398 &pr->public_data.query,
400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
401 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
404 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
406 pr->public_data.ttl.abs_value_us);
407 /* make sure we don't track too many requests */
408 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
409 max_pending_requests)
411 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
412 GNUNET_assert (NULL != dpr);
414 break; /* let the request live briefly... */
416 dpr->rh (dpr->rh_cls,
417 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
420 GNUNET_TIME_UNIT_FOREVER_ABS,
421 GNUNET_TIME_UNIT_FOREVER_ABS,
422 GNUNET_BLOCK_TYPE_ANY,
425 GSF_pending_request_cancel_ (dpr,
429 GNUNET_STATISTICS_update (GSF_stats,
430 gettext_noop ("# Pending requests active"), 1,
436 * Obtain the public data associated with a pending request
438 * @param pr pending request
439 * @return associated public data
441 struct GSF_PendingRequestData *
442 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
444 return &pr->public_data;
449 * Test if two pending requests are compatible (would generate
450 * the same query modulo filters and should thus be processed
453 * @param pra a pending request
454 * @param prb another pending request
455 * @return #GNUNET_OK if the requests are compatible
458 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
459 struct GSF_PendingRequest *prb)
461 if ( (pra->public_data.type != prb->public_data.type) ||
462 (0 != memcmp (&pra->public_data.query,
463 &prb->public_data.query,
464 sizeof (struct GNUNET_HashCode))))
471 * Update a given pending request with additional replies
472 * that have been seen.
474 * @param pr request to update
475 * @param replies_seen hash codes of replies that we've seen
476 * @param replies_seen_count size of the replies_seen array
479 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
480 const struct GNUNET_HashCode * replies_seen,
481 unsigned int replies_seen_count)
483 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
484 return; /* integer overflow */
485 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
487 /* we're responsible for the BF, full refresh */
488 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
489 GNUNET_array_grow (pr->replies_seen,
490 pr->replies_seen_size,
491 replies_seen_count + pr->replies_seen_count);
492 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
494 sizeof (struct GNUNET_HashCode) * replies_seen_count);
495 pr->replies_seen_count += replies_seen_count;
496 refresh_bloomfilter (pr->public_data.type,
503 /* we're not the initiator, but the initiator did not give us
504 * any bloom-filter, so we need to create one on-the-fly */
505 refresh_bloomfilter (pr->public_data.type,
510 GNUNET_break (GNUNET_OK ==
511 GNUNET_BLOCK_group_set_seen (pr->bg,
513 pr->replies_seen_count));
517 GNUNET_DHT_get_filter_known_results (pr->gh,
524 * Generate the message corresponding to the given pending request for
525 * transmission to other peers.
527 * @param pr request to generate the message for
528 * @return envelope with the request message
530 struct GNUNET_MQ_Envelope *
531 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
533 struct GNUNET_MQ_Envelope *env;
534 struct GetMessage *gm;
535 struct GNUNET_PeerIdentity *ext;
540 struct GNUNET_TIME_Absolute now;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Building request message for `%s' of type %d\n",
548 GNUNET_h2s (&pr->public_data.query),
549 pr->public_data.type);
552 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
553 if ((!do_route) && (pr->sender_pid == 0))
556 do_route = GNUNET_YES;
560 bm |= GET_MESSAGE_BIT_RETURN_TO;
563 if (NULL != pr->public_data.target)
565 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
569 GNUNET_BLOCK_group_serialize (pr->bg,
577 env = GNUNET_MQ_msg_extra (gm,
578 bf_size + k * sizeof (struct GNUNET_PeerIdentity),
579 GNUNET_MESSAGE_TYPE_FS_GET);
580 gm->type = htonl (pr->public_data.type);
583 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
584 pr->public_data.priority + 1);
587 pr->public_data.priority -= prio;
588 pr->public_data.num_transmissions++;
589 pr->public_data.respect_offered += prio;
590 gm->priority = htonl (prio);
591 now = GNUNET_TIME_absolute_get ();
592 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
593 gm->ttl = htonl (ttl / 1000LL / 1000LL);
594 gm->filter_mutator = htonl (bf_nonce);
595 gm->hash_bitmap = htonl (bm);
596 gm->query = pr->public_data.query;
597 ext = (struct GNUNET_PeerIdentity *) &gm[1];
600 GNUNET_PEER_resolve (pr->sender_pid,
602 if (NULL != pr->public_data.target)
603 ext[k++] = *pr->public_data.target;
604 GNUNET_memcpy (&ext[k],
607 GNUNET_free_non_null (bf_data);
613 * Iterator to free pending requests.
615 * @param cls closure, unused
616 * @param key current key code
617 * @param value value in the hash map (pending request)
618 * @return #GNUNET_YES (we should continue to iterate)
621 clean_request (void *cls,
622 const struct GNUNET_HashCode *key,
625 struct GSF_PendingRequest *pr = value;
626 GSF_LocalLookupContinuation cont;
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Cleaning up pending request for `%s'.\n",
631 if (NULL != pr->cadet_request)
633 pr->cadet_retry_count = CADET_RETRY_MAX;
634 GSF_cadet_query_cancel (pr->cadet_request);
635 pr->cadet_request = NULL;
637 if (NULL != (cont = pr->llc_cont))
640 cont (pr->llc_cont_cls,
644 GSF_plan_notify_request_done_ (pr);
645 GNUNET_free_non_null (pr->replies_seen);
646 GNUNET_BLOCK_group_destroy (pr->bg);
648 GNUNET_PEER_change_rc (pr->sender_pid, -1);
650 GNUNET_PEER_change_rc (pr->origin_pid, -1);
652 if (NULL != pr->hnode)
654 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
659 GNUNET_DATASTORE_cancel (pr->qe);
664 GNUNET_DHT_get_stop (pr->gh);
667 if (NULL != pr->warn_task)
669 GNUNET_SCHEDULER_cancel (pr->warn_task);
670 pr->warn_task = NULL;
672 GNUNET_assert (GNUNET_OK ==
673 GNUNET_CONTAINER_multihashmap_remove (pr_map,
674 &pr->public_data.query,
676 GNUNET_STATISTICS_update (GSF_stats,
677 gettext_noop ("# Pending requests active"),
686 * Explicitly cancel a pending request.
688 * @param pr request to cancel
689 * @param full_cleanup fully purge the request
692 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
695 GSF_LocalLookupContinuation cont;
698 return; /* already cleaned up! */
699 if (GNUNET_NO == full_cleanup)
701 /* make request inactive (we're no longer interested in more results),
702 * but do NOT remove from our data-structures, we still need it there
703 * to prevent the request from looping */
705 if (NULL != pr->cadet_request)
707 pr->cadet_retry_count = CADET_RETRY_MAX;
708 GSF_cadet_query_cancel (pr->cadet_request);
709 pr->cadet_request = NULL;
711 if (NULL != (cont = pr->llc_cont))
714 cont (pr->llc_cont_cls,
718 GSF_plan_notify_request_done_ (pr);
721 GNUNET_DATASTORE_cancel (pr->qe);
726 GNUNET_DHT_get_stop (pr->gh);
729 if (NULL != pr->warn_task)
731 GNUNET_SCHEDULER_cancel (pr->warn_task);
732 pr->warn_task = NULL;
736 GNUNET_assert (GNUNET_YES ==
738 &pr->public_data.query,
744 * Iterate over all pending requests.
746 * @param it function to call for each request
747 * @param cls closure for @a it
750 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
752 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
753 (GNUNET_CONTAINER_HashMapIterator) it,
759 * Closure for process_reply() function.
761 struct ProcessReplyClosure
764 * The data for the reply.
769 * Who gave us this reply? NULL for local host (or DHT)
771 struct GSF_ConnectedPeer *sender;
774 * When the reply expires.
776 struct GNUNET_TIME_Absolute expiration;
786 enum GNUNET_BLOCK_Type type;
789 * Control flags for evaluation.
791 enum GNUNET_BLOCK_EvaluationOptions eo;
794 * How much was this reply worth to us?
799 * Anonymity requirements for this reply.
801 uint32_t anonymity_level;
804 * Evaluation result (returned).
806 enum GNUNET_BLOCK_EvaluationResult eval;
809 * Did we find a matching request?
816 * Update the performance data for the sender (if any) since
817 * the sender successfully answered one of our queries.
819 * @param prq information about the sender
820 * @param pr request that was satisfied
823 update_request_performance_data (struct ProcessReplyClosure *prq,
824 struct GSF_PendingRequest *pr)
826 if (prq->sender == NULL)
828 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
834 * We have received a reply; handle it!
836 * @param cls response (a `struct ProcessReplyClosure`)
837 * @param key our query
838 * @param value value in the hash map (info about the query)
839 * @return #GNUNET_YES (we should continue to iterate)
842 process_reply (void *cls,
843 const struct GNUNET_HashCode *key,
846 struct ProcessReplyClosure *prq = cls;
847 struct GSF_PendingRequest *pr = value;
848 struct GNUNET_HashCode chash;
849 struct GNUNET_TIME_Absolute last_transmission;
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Matched result (type %u) for query `%s' with pending request\n",
855 (unsigned int) prq->type,
857 GNUNET_STATISTICS_update (GSF_stats,
858 gettext_noop ("# replies received and matched"), 1,
861 GNUNET_BLOCK_evaluate (GSF_block_ctx,
872 case GNUNET_BLOCK_EVALUATION_OK_MORE:
873 update_request_performance_data (prq, pr);
875 case GNUNET_BLOCK_EVALUATION_OK_LAST:
876 /* short cut: stop processing early, no BF-update, etc. */
877 update_request_performance_data (prq, pr);
878 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
879 GNUNET_TIME_absolute_get_duration (pr->
880 public_data.start_time).rel_value_us);
882 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
885 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
886 /* pass on to other peers / local clients */
887 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
888 last_transmission, prq->type, prq->data, prq->size);
890 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
891 #if INSANE_STATISTICS
892 GNUNET_STATISTICS_update (GSF_stats,
894 ("# duplicate replies discarded (bloomfilter)"),
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Duplicate response, discarding.\n");
899 return GNUNET_YES; /* duplicate */
900 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
901 GNUNET_STATISTICS_update (GSF_stats,
903 ("# irrelevant replies discarded"),
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Irrelevant response, ignoring.\n");
908 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
909 return GNUNET_YES; /* wrong namespace */
910 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
913 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
916 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918 _("Unsupported block type %u\n"),
922 /* update bloomfilter */
923 GNUNET_CRYPTO_hash (prq->data,
926 GSF_pending_request_update_ (pr,
929 if (NULL == prq->sender)
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Found result for query `%s' in local datastore\n",
934 GNUNET_STATISTICS_update (GSF_stats,
935 gettext_noop ("# results found locally"),
941 GSF_dht_lookup_ (pr);
943 prq->priority += pr->public_data.original_priority;
944 pr->public_data.priority = 0;
945 pr->public_data.original_priority = 0;
946 pr->public_data.results_found++;
947 prq->request_found = GNUNET_YES;
948 /* finally, pass on to other peer / local client */
949 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
952 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
956 prq->anonymity_level,
967 * Context for put_migration_continuation().
969 struct PutMigrationContext
973 * Start time for the operation.
975 struct GNUNET_TIME_Absolute start;
980 struct GNUNET_PeerIdentity origin;
983 * #GNUNET_YES if we had a matching request for this block,
991 * Continuation called to notify client about result of the
995 * @param success #GNUNET_SYSERR on failure
996 * @param min_expiration minimum expiration time required for content to be stored
997 * @param msg NULL on success, otherwise an error message
1000 put_migration_continuation (void *cls, int success,
1001 struct GNUNET_TIME_Absolute min_expiration,
1004 struct PutMigrationContext *pmc = cls;
1005 struct GSF_ConnectedPeer *cp;
1006 struct GNUNET_TIME_Relative mig_pause;
1007 struct GSF_PeerPerformanceData *ppd;
1009 if (NULL != datastore_put_load)
1011 if (GNUNET_SYSERR != success)
1013 GNUNET_LOAD_update (datastore_put_load,
1014 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
1018 /* on queue failure / timeout, increase the put load dramatically */
1019 GNUNET_LOAD_update (datastore_put_load,
1020 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
1023 cp = GSF_peer_get_ (&pmc->origin);
1024 if (GNUNET_OK == success)
1028 ppd = GSF_get_peer_performance_data_ (cp);
1029 ppd->migration_delay.rel_value_us /= 2;
1034 if ( (GNUNET_NO == success) &&
1035 (GNUNET_NO == pmc->requested) &&
1038 ppd = GSF_get_peer_performance_data_ (cp);
1039 if (min_expiration.abs_value_us > 0)
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Asking to stop migration for %s because datastore is full\n",
1043 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1044 GSF_block_peer_migration_ (cp, min_expiration);
1048 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1049 ppd->migration_delay);
1050 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1051 ppd->migration_delay);
1052 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1053 ppd->migration_delay.rel_value_us);
1054 ppd->migration_delay = GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Replicated content already exists locally, asking to stop migration for %s\n",
1057 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1059 GSF_block_peer_migration_ (cp,
1060 GNUNET_TIME_relative_to_absolute (mig_pause));
1064 GNUNET_STATISTICS_update (GSF_stats,
1065 gettext_noop ("# Datastore `PUT' failures"), 1,
1071 * Test if the DATABASE (PUT) load on this peer is too high
1072 * to even consider processing the query at
1075 * @param priority the priority of the item
1076 * @return #GNUNET_YES if the load is too high to do anything (load high)
1077 * #GNUNET_NO to process normally (load normal or low)
1080 test_put_load_too_high (uint32_t priority)
1084 if (NULL == datastore_put_load)
1086 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1087 return GNUNET_NO; /* very fast */
1088 ld = GNUNET_LOAD_get_load (datastore_put_load);
1089 if (ld < 2.0 * (1 + priority))
1091 GNUNET_STATISTICS_update (GSF_stats,
1093 ("# storage requests dropped due to high load"), 1,
1100 * Iterator called on each result obtained for a DHT
1101 * operation that expects a reply
1103 * @param cls closure
1104 * @param exp when will this value expire
1105 * @param key key of the result
1106 * @param get_path peers on reply path (or NULL if not recorded)
1107 * @param get_path_length number of entries in @a get_path
1108 * @param put_path peers on the PUT path (or NULL if not recorded)
1109 * @param put_path_length number of entries in @a get_path
1110 * @param type type of the result
1111 * @param size number of bytes in @a data
1112 * @param data pointer to the result data
1115 handle_dht_reply (void *cls,
1116 struct GNUNET_TIME_Absolute exp,
1117 const struct GNUNET_HashCode *key,
1118 const struct GNUNET_PeerIdentity *get_path,
1119 unsigned int get_path_length,
1120 const struct GNUNET_PeerIdentity *put_path,
1121 unsigned int put_path_length,
1122 enum GNUNET_BLOCK_Type type,
1126 struct GSF_PendingRequest *pr = cls;
1127 struct ProcessReplyClosure prq;
1128 struct PutMigrationContext *pmc;
1130 GNUNET_STATISTICS_update (GSF_stats,
1131 gettext_noop ("# Replies received from DHT"), 1,
1133 memset (&prq, 0, sizeof (prq));
1135 prq.expiration = exp;
1136 /* do not allow migrated content to live longer than 1 year */
1137 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1141 prq.eo = GNUNET_BLOCK_EO_NONE;
1142 process_reply (&prq, key, pr);
1143 if ((GNUNET_YES == active_to_migration) &&
1144 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Replicating result for query `%s' with priority %u\n",
1148 GNUNET_h2s (key), prq.priority);
1149 pmc = GNUNET_new (struct PutMigrationContext);
1150 pmc->start = GNUNET_TIME_absolute_get ();
1151 pmc->requested = GNUNET_YES;
1153 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1155 0 /* replication */ ,
1156 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1157 &put_migration_continuation, pmc))
1159 put_migration_continuation (pmc,
1161 GNUNET_TIME_UNIT_ZERO_ABS,
1169 * Consider looking up the data in the DHT (anonymity-level permitting).
1171 * @param pr the pending request to process
1174 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1178 struct GNUNET_PeerIdentity pi;
1179 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1181 if (0 != pr->public_data.anonymity_level)
1185 GNUNET_DHT_get_stop (pr->gh);
1190 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1192 GNUNET_assert (0 != pr->sender_pid);
1193 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1194 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1195 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1198 GNUNET_DHT_get_start (GSF_dht,
1199 pr->public_data.type, &pr->public_data.query,
1200 DHT_GET_REPLICATION,
1201 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1202 xquery, xquery_size, &handle_dht_reply, pr);
1203 if ( (NULL != pr->gh) &&
1204 (0 != pr->replies_seen_count) )
1205 GNUNET_DHT_get_filter_known_results (pr->gh,
1206 pr->replies_seen_count,
1212 * Function called with a reply from the cadet.
1214 * @param cls the pending request struct
1215 * @param type type of the block, ANY on error
1216 * @param expiration expiration time for the block
1217 * @param data_size number of bytes in @a data, 0 on error
1218 * @param data reply block data, NULL on error
1221 cadet_reply_proc (void *cls,
1222 enum GNUNET_BLOCK_Type type,
1223 struct GNUNET_TIME_Absolute expiration,
1227 struct GSF_PendingRequest *pr = cls;
1228 struct ProcessReplyClosure prq;
1229 struct GNUNET_HashCode query;
1231 pr->cadet_request = NULL;
1232 if (GNUNET_BLOCK_TYPE_ANY == type)
1234 GNUNET_break (NULL == data);
1235 GNUNET_break (0 == data_size);
1236 pr->cadet_retry_count++;
1237 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1238 return; /* give up on cadet */
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "Error retrieiving block via cadet\n");
1241 /* retry -- without delay, as this is non-anonymous
1242 and cadet/cadet connect will take some time anyway */
1243 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1244 &pr->public_data.query,
1245 pr->public_data.type,
1251 GNUNET_BLOCK_get_key (GSF_block_ctx,
1253 data, data_size, &query))
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Failed to derive key for block of type %d\n",
1258 GNUNET_break_op (0);
1261 GNUNET_STATISTICS_update (GSF_stats,
1262 gettext_noop ("# Replies received from CADET"), 1,
1264 memset (&prq, 0, sizeof (prq));
1266 prq.expiration = expiration;
1267 /* do not allow migrated content to live longer than 1 year */
1268 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1270 prq.size = data_size;
1272 prq.eo = GNUNET_BLOCK_EO_NONE;
1273 process_reply (&prq, &query, pr);
1278 * Consider downloading via cadet (if possible)
1280 * @param pr the pending request to process
1283 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1285 if (0 != pr->public_data.anonymity_level)
1287 if (0 == pr->public_data.target)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Cannot do cadet-based download, target peer not known\n");
1293 if (NULL != pr->cadet_request)
1295 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1296 &pr->public_data.query,
1297 pr->public_data.type,
1304 * Task that issues a warning if the datastore lookup takes too long.
1306 * @param cls the `struct GSF_PendingRequest`
1309 warn_delay_task (void *cls)
1311 struct GSF_PendingRequest *pr = cls;
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1314 _("Datastore lookup already took %s!\n"),
1315 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1318 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1325 * Task that issues a warning if the datastore lookup takes too long.
1327 * @param cls the `struct GSF_PendingRequest`
1330 odc_warn_delay_task (void *cls)
1332 struct GSF_PendingRequest *pr = cls;
1334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1335 _("On-demand lookup already took %s!\n"),
1336 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1338 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1339 &odc_warn_delay_task, pr);
1343 /* Call our continuation (if we have any) */
1345 call_continuation (struct GSF_PendingRequest *pr)
1347 GSF_LocalLookupContinuation cont = pr->llc_cont;
1349 GNUNET_assert (NULL == pr->qe);
1350 if (NULL != pr->warn_task)
1352 GNUNET_SCHEDULER_cancel (pr->warn_task);
1353 pr->warn_task = NULL;
1356 return; /* no continuation */
1357 pr->llc_cont = NULL;
1358 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1360 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1362 /* Signal that we are done and that there won't be any
1363 additional results to allow client to clean up state. */
1365 GNUNET_BLOCK_EVALUATION_OK_LAST,
1368 GNUNET_TIME_UNIT_ZERO_ABS,
1369 GNUNET_TIME_UNIT_FOREVER_ABS,
1370 GNUNET_BLOCK_TYPE_ANY,
1374 /* Finally, call our continuation to signal that we are
1375 done with local processing of this request; i.e. to
1376 start reading again from the client. */
1377 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1381 cont (pr->llc_cont_cls, pr, pr->local_result);
1385 /* Update stats and call continuation */
1387 no_more_local_results (struct GSF_PendingRequest *pr)
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1390 "No further local responses available.\n");
1391 #if INSANE_STATISTICS
1392 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == pr->public_data.type) ||
1393 (GNUNET_BLOCK_TYPE_FS_IBLOCK == pr->public_data.type) )
1394 GNUNET_STATISTICS_update (GSF_stats,
1395 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1399 call_continuation (pr);
1403 /* forward declaration */
1405 process_local_reply (void *cls,
1406 const struct GNUNET_HashCode *key,
1409 enum GNUNET_BLOCK_Type type,
1412 uint32_t replication,
1413 struct GNUNET_TIME_Absolute expiration,
1417 /* Start a local query */
1419 start_local_query (struct GSF_PendingRequest *pr,
1423 pr->qe_start = GNUNET_TIME_absolute_get ();
1425 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1429 GNUNET_DATASTORE_get_key (GSF_dsh,
1432 &pr->public_data.query,
1433 pr->public_data.type ==
1434 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1435 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1437 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1438 public_data.options)) ? UINT_MAX : 1
1439 /* queue priority */ ,
1441 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1442 public_data.options)) ? UINT_MAX :
1443 GSF_datastore_queue_size
1444 /* max queue size */ ,
1445 &process_local_reply, pr);
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "ERROR Requesting `%s' of type %d with next_uid %llu from datastore.\n",
1450 GNUNET_h2s (&pr->public_data.query),
1451 pr->public_data.type,
1452 (unsigned long long) next_uid);
1453 GNUNET_STATISTICS_update (GSF_stats,
1454 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1457 call_continuation (pr);
1462 * We're processing (local) results for a search request
1463 * from another peer. Pass applicable results to the
1464 * peer and if we are done either clean up (operation
1465 * complete) or forward to other peers (more results possible).
1467 * @param cls our closure (`struct GSF_PendingRequest *`)
1468 * @param key key for the content
1469 * @param size number of bytes in @a data
1470 * @param data content stored
1471 * @param type type of the content
1472 * @param priority priority of the content
1473 * @param anonymity anonymity-level for the content
1474 * @param replication replication-level for the content
1475 * @param expiration expiration time for the content
1476 * @param uid unique identifier for the datum;
1477 * maybe 0 if no unique identifier is available
1480 process_local_reply (void *cls,
1481 const struct GNUNET_HashCode *key,
1484 enum GNUNET_BLOCK_Type type,
1487 uint32_t replication,
1488 struct GNUNET_TIME_Absolute expiration,
1491 struct GSF_PendingRequest *pr = cls;
1492 struct ProcessReplyClosure prq;
1493 struct GNUNET_HashCode query;
1494 unsigned int old_rf;
1496 GNUNET_SCHEDULER_cancel (pr->warn_task);
1497 pr->warn_task = NULL;
1499 goto called_from_on_demand;
1501 if ( (NULL == key) &&
1503 !pr->have_first_uid) /* We have hit the end for the 2nd time with no results */
1506 #if INSANE_STATISTICS
1507 GNUNET_STATISTICS_update (GSF_stats,
1509 ("# Datastore lookups concluded (no results)"),
1512 no_more_local_results (pr);
1515 if ( ( (NULL == key) &&
1516 pr->seen_null ) || /* We have hit the end for the 2nd time OR */
1518 pr->have_first_uid &&
1519 (uid >= pr->first_uid) ) ) /* We have hit the end and past first UID */
1521 /* Seen all results */
1522 GNUNET_STATISTICS_update (GSF_stats,
1524 ("# Datastore lookups concluded (seen all)"),
1526 no_more_local_results (pr);
1531 GNUNET_assert (!pr->seen_null);
1532 pr->seen_null = true;
1533 start_local_query (pr,
1535 false /* random */);
1538 if (!pr->have_first_uid)
1540 pr->first_uid = uid;
1541 pr->have_first_uid = true;
1544 if (pr->result_count > MAX_RESULTS)
1546 GNUNET_STATISTICS_update (GSF_stats,
1548 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1550 no_more_local_results (pr);
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1555 GNUNET_h2s (key), type, (unsigned long long) uid);
1556 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "Found ONDEMAND block, performing on-demand encoding\n");
1560 GNUNET_STATISTICS_update (GSF_stats,
1562 ("# on-demand blocks matched requests"), 1,
1564 pr->qe_start = GNUNET_TIME_absolute_get ();
1566 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1567 &odc_warn_delay_task, pr);
1569 GNUNET_FS_handle_on_demand_block (key,
1578 &process_local_reply,
1581 GNUNET_STATISTICS_update (GSF_stats,
1583 ("# on-demand lookups performed successfully"),
1585 return; /* we're done */
1587 GNUNET_STATISTICS_update (GSF_stats,
1588 gettext_noop ("# on-demand lookups failed"), 1,
1590 GNUNET_SCHEDULER_cancel (pr->warn_task);
1591 start_local_query (pr,
1592 uid + 1 /* next_uid */,
1593 false /* random */);
1596 called_from_on_demand:
1597 old_rf = pr->public_data.results_found;
1598 memset (&prq, 0, sizeof (prq));
1600 prq.expiration = expiration;
1603 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1606 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, UINT_MAX, UINT_MAX,
1608 start_local_query (pr,
1609 uid + 1 /* next_uid */,
1610 false /* random */);
1614 prq.priority = priority;
1615 prq.request_found = GNUNET_NO;
1616 prq.anonymity_level = anonymity;
1617 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1618 GSF_update_datastore_delay_ (pr->public_data.start_time);
1619 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1620 process_reply (&prq, key, pr);
1621 pr->local_result = prq.eval;
1622 if (GNUNET_BLOCK_EVALUATION_OK_LAST == prq.eval)
1624 GNUNET_STATISTICS_update (GSF_stats,
1626 ("# Datastore lookups concluded (found last result)"),
1629 call_continuation (pr);
1632 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1633 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1634 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1637 "Load too high, done with request\n");
1638 GNUNET_STATISTICS_update (GSF_stats,
1639 gettext_noop ("# Datastore lookups concluded (load too high)"),
1642 call_continuation (pr);
1645 start_local_query (pr,
1646 uid + 1 /* next_uid */,
1647 false /* random */);
1652 * Is the given target a legitimate peer for forwarding the given request?
1656 * @return #GNUNET_YES if this request could be forwarded to the given peer
1659 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1660 const struct GNUNET_PeerIdentity *target)
1662 struct GNUNET_PeerIdentity pi;
1664 if (0 == pr->origin_pid)
1666 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1668 memcmp (&pi, target,
1669 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1675 * Look up the request in the local datastore.
1677 * @param pr the pending request to process
1678 * @param cont function to call at the end
1679 * @param cont_cls closure for @a cont
1682 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1683 GSF_LocalLookupContinuation cont,
1686 GNUNET_assert (NULL == pr->gh);
1687 GNUNET_assert (NULL == pr->cadet_request);
1688 GNUNET_assert (NULL == pr->llc_cont);
1689 pr->llc_cont = cont;
1690 pr->llc_cont_cls = cont_cls;
1691 #if INSANE_STATISTICS
1692 GNUNET_STATISTICS_update (GSF_stats,
1693 gettext_noop ("# Datastore lookups initiated"), 1,
1696 start_local_query(pr,
1704 * Handle P2P "CONTENT" message. Checks that the message is
1705 * well-formed and then checks if there are any pending requests for
1706 * this content and possibly passes it on (to local clients or other
1707 * peers). Does NOT perform migration (content caching at this peer).
1709 * @param cls the other peer involved
1710 * @param put the actual message
1713 handle_p2p_put (void *cls,
1714 const struct PutMessage *put)
1716 struct GSF_ConnectedPeer *cp = cls;
1719 enum GNUNET_BLOCK_Type type;
1720 struct GNUNET_TIME_Absolute expiration;
1721 struct GNUNET_HashCode query;
1722 struct ProcessReplyClosure prq;
1723 struct GNUNET_TIME_Relative block_time;
1725 struct PutMigrationContext *pmc;
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Received P2P PUT from %s\n",
1729 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1730 GSF_cover_content_count++;
1731 msize = ntohs (put->header.size);
1732 dsize = msize - sizeof (struct PutMessage);
1733 type = ntohl (put->type);
1734 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1735 /* do not allow migrated content to live longer than 1 year */
1736 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1739 GNUNET_BLOCK_get_key (GSF_block_ctx,
1745 GNUNET_break_op (0);
1748 GNUNET_STATISTICS_update (GSF_stats,
1749 gettext_noop ("# GAP PUT messages received"),
1752 /* now, lookup 'query' */
1753 prq.data = (const void *) &put[1];
1757 prq.expiration = expiration;
1759 prq.anonymity_level = UINT32_MAX;
1760 prq.request_found = GNUNET_NO;
1761 prq.eo = GNUNET_BLOCK_EO_NONE;
1762 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1768 GSF_connected_peer_change_preference_ (cp,
1769 CONTENT_BANDWIDTH_VALUE +
1770 1000 * prq.priority);
1771 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1773 if ((GNUNET_YES == active_to_migration) &&
1775 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "Replicating result for query `%s' with priority %u\n",
1779 GNUNET_h2s (&query),
1781 pmc = GNUNET_new (struct PutMigrationContext);
1782 pmc->start = GNUNET_TIME_absolute_get ();
1783 pmc->requested = prq.request_found;
1784 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1785 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1788 GNUNET_DATASTORE_put (GSF_dsh,
1796 0 /* replication */ ,
1797 expiration, 1 + prq.priority,
1798 MAX_DATASTORE_QUEUE,
1799 &put_migration_continuation,
1802 put_migration_continuation (pmc,
1804 GNUNET_TIME_UNIT_ZERO_ABS,
1808 else if (NULL != cp)
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1811 "Choosing not to keep content `%s' (%d/%d)\n",
1812 GNUNET_h2s (&query),
1813 active_to_migration,
1814 test_put_load_too_high (prq.priority));
1816 putl = GNUNET_LOAD_get_load (datastore_put_load);
1817 if ( (NULL != cp) &&
1818 (GNUNET_NO == prq.request_found) &&
1819 ( (GNUNET_YES != active_to_migration) ||
1820 (putl > 2.5 * (1 + prq.priority)) ) )
1822 if (GNUNET_YES != active_to_migration)
1823 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1825 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1827 GNUNET_CRYPTO_random_u32
1828 (GNUNET_CRYPTO_QUALITY_WEAK,
1829 (unsigned int) (60000 * putl * putl)));
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1832 GNUNET_STRINGS_relative_time_to_string (block_time,
1835 active_to_migration,
1836 (GNUNET_NO == prq.request_found));
1837 GSF_block_peer_migration_ (cp,
1838 GNUNET_TIME_relative_to_absolute (block_time));
1844 * Check if the given request is still active.
1846 * @param pr pending request
1847 * @return #GNUNET_YES if the request is still active
1850 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1852 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1857 * Setup the subsystem.
1860 GSF_pending_request_init_ ()
1863 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1864 "MAX_PENDING_REQUESTS",
1865 &max_pending_requests))
1867 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1868 "fs", "MAX_PENDING_REQUESTS");
1870 active_to_migration =
1871 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1872 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1873 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1874 requests_by_expiration_heap =
1875 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1880 * Shutdown the subsystem.
1883 GSF_pending_request_done_ ()
1885 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1888 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1890 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1891 requests_by_expiration_heap = NULL;
1892 GNUNET_LOAD_value_free (datastore_put_load);
1893 datastore_put_load = NULL;
1897 /* end of gnunet-service-fs_pr.c */