2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 * Bloomfilter masking replies we've already seen.
102 struct GNUNET_CONTAINER_BloomFilter *bf;
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 * Current offset for querying our local datastore for results.
164 * Starts at a random value, incremented until we get the same
165 * UID again (detected using 'first_uid'), which is then used
166 * to termiante the iteration.
168 uint64_t local_result_offset;
171 * Unique ID of the first result from the local datastore;
172 * used to detect wrap-around of the offset.
177 * How often have we retried this request via 'cadet'?
178 * (used to bound overall retries).
180 unsigned int cadet_retry_count;
183 * Number of valid entries in the 'replies_seen' array.
185 unsigned int replies_seen_count;
188 * Length of the 'replies_seen' array.
190 unsigned int replies_seen_size;
193 * Mingle value we currently use for the bf.
198 * Do we have a first UID yet?
200 unsigned int have_first_uid;
206 * All pending requests, ordered by the query. Entries
207 * are of type 'struct GSF_PendingRequest*'.
209 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
213 * Datastore 'PUT' load tracking.
215 static struct GNUNET_LOAD_Value *datastore_put_load;
219 * Are we allowed to migrate content to this peer.
221 static int active_to_migration;
225 * Heap with the request that will expire next at the top. Contains
226 * pointers of type "struct PendingRequest*"; these will *also* be
227 * aliased from the "requests_by_peer" data structures and the
228 * "requests_by_query" table. Note that requests from our clients
229 * don't expire and are thus NOT in the "requests_by_expiration"
230 * (or the "requests_by_peer" tables).
232 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
236 * Maximum number of requests (from other peers, overall) that we're
237 * willing to have pending at any given point in time. Can be changed
238 * via the configuration file (32k is just the default).
240 static unsigned long long max_pending_requests = (32 * 1024);
245 * Recalculate our bloom filter for filtering replies. This function
246 * will create a new bloom filter from scratch, so it should only be
247 * called if we have no bloomfilter at all (and hence can create a
248 * fresh one of minimal size without problems) OR if our peer is the
249 * initiator (in which case we may resize to larger than mimimum size).
251 * @param pr request for which the BF is to be recomputed
254 refresh_bloomfilter (struct GSF_PendingRequest *pr)
257 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
259 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
261 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
262 pr->replies_seen_count);
267 * Create a new pending request.
269 * @param options request options
270 * @param type type of the block that is being requested
271 * @param query key for the lookup
272 * @param target preferred target for the request, NULL for none
273 * @param bf_data raw data for bloom filter for known replies, can be NULL
274 * @param bf_size number of bytes in @a bf_data
275 * @param mingle mingle value for bf
276 * @param anonymity_level desired anonymity level
277 * @param priority maximum outgoing cummulative request priority to use
278 * @param ttl current time-to-live for the request
279 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
280 * @param origin_pid peer ID of origin of query (do not loop back)
281 * @param replies_seen hash codes of known local replies
282 * @param replies_seen_count size of the @a replies_seen array
283 * @param rh handle to call when we get a reply
284 * @param rh_cls closure for @a rh
285 * @return handle for the new pending request
287 struct GSF_PendingRequest *
288 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
289 enum GNUNET_BLOCK_Type type,
290 const struct GNUNET_HashCode *query,
291 const struct GNUNET_PeerIdentity *target,
292 const char *bf_data, size_t bf_size,
293 uint32_t mingle, uint32_t anonymity_level,
294 uint32_t priority, int32_t ttl,
295 GNUNET_PEER_Id sender_pid,
296 GNUNET_PEER_Id origin_pid,
297 const struct GNUNET_HashCode *replies_seen,
298 unsigned int replies_seen_count,
299 GSF_PendingRequestReplyHandler rh, void *rh_cls)
301 struct GSF_PendingRequest *pr;
302 struct GSF_PendingRequest *dpr;
304 struct GNUNET_HashCode *eptr;
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Creating request handle for `%s' of type %d\n",
308 GNUNET_h2s (query), type);
309 #if INSANE_STATISTICS
310 GNUNET_STATISTICS_update (GSF_stats,
311 gettext_noop ("# Pending requests created"), 1,
316 extra += sizeof (struct GNUNET_PeerIdentity);
317 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
318 pr->local_result_offset =
319 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
320 pr->public_data.query = *query;
321 eptr = (struct GNUNET_HashCode *) &pr[1];
324 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
325 memcpy (eptr, target, sizeof (struct GNUNET_PeerIdentity));
327 pr->public_data.anonymity_level = anonymity_level;
328 pr->public_data.priority = priority;
329 pr->public_data.original_priority = priority;
330 pr->public_data.options = options;
331 pr->public_data.type = type;
332 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
333 pr->sender_pid = sender_pid;
334 pr->origin_pid = origin_pid;
337 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
339 pr->public_data.ttl =
340 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
341 (GNUNET_TIME_UNIT_SECONDS,
344 pr->public_data.ttl =
345 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
346 GNUNET_TIME_relative_multiply
347 (GNUNET_TIME_UNIT_SECONDS,
349 if (replies_seen_count > 0)
351 pr->replies_seen_size = replies_seen_count;
353 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
354 memcpy (pr->replies_seen, replies_seen,
355 replies_seen_count * sizeof (struct GNUNET_HashCode));
356 pr->replies_seen_count = replies_seen_count;
361 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size,
362 GNUNET_CONSTANTS_BLOOMFILTER_K);
365 else if ((replies_seen_count > 0) &&
366 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
368 refresh_bloomfilter (pr);
370 GNUNET_CONTAINER_multihashmap_put (pr_map,
371 &pr->public_data.query, pr,
372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
373 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
376 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
377 pr->public_data.ttl.abs_value_us);
378 /* make sure we don't track too many requests */
379 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
380 max_pending_requests)
382 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
383 GNUNET_assert (dpr != NULL);
385 break; /* let the request live briefly... */
387 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
388 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_ABS,
389 GNUNET_BLOCK_TYPE_ANY, NULL, 0);
390 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
393 GNUNET_STATISTICS_update (GSF_stats,
394 gettext_noop ("# Pending requests active"), 1,
400 * Obtain the public data associated with a pending request
402 * @param pr pending request
403 * @return associated public data
405 struct GSF_PendingRequestData *
406 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
408 return &pr->public_data;
413 * Test if two pending requests are compatible (would generate
414 * the same query modulo filters and should thus be processed
417 * @param pra a pending request
418 * @param prb another pending request
419 * @return #GNUNET_OK if the requests are compatible
422 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
423 struct GSF_PendingRequest *prb)
425 if ((pra->public_data.type != prb->public_data.type) ||
427 memcmp (&pra->public_data.query, &prb->public_data.query,
428 sizeof (struct GNUNET_HashCode))))
436 * Update a given pending request with additional replies
437 * that have been seen.
439 * @param pr request to update
440 * @param replies_seen hash codes of replies that we've seen
441 * @param replies_seen_count size of the replies_seen array
444 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
445 const struct GNUNET_HashCode * replies_seen,
446 unsigned int replies_seen_count)
449 struct GNUNET_HashCode mhash;
451 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
452 return; /* integer overflow */
453 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
455 /* we're responsible for the BF, full refresh */
456 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
457 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
458 replies_seen_count + pr->replies_seen_count);
459 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
460 sizeof (struct GNUNET_HashCode) * replies_seen_count);
461 pr->replies_seen_count += replies_seen_count;
462 refresh_bloomfilter (pr);
468 /* we're not the initiator, but the initiator did not give us
469 * any bloom-filter, so we need to create one on-the-fly */
471 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
473 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, replies_seen,
478 for (i = 0; i < pr->replies_seen_count; i++)
480 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
481 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
486 GNUNET_DHT_get_filter_known_results (pr->gh,
493 * Generate the message corresponding to the given pending request for
494 * transmission to other peers (or at least determine its size).
496 * @param pr request to generate the message for
497 * @param buf_size number of bytes available in @a buf
498 * @param buf where to copy the message (can be NULL)
499 * @return number of bytes needed (if `>` @a buf_size) or used
502 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
503 size_t buf_size, void *buf)
505 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
506 struct GetMessage *gm;
507 struct GNUNET_PeerIdentity *ext;
513 struct GNUNET_TIME_Absolute now;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Building request message for `%s' of type %d\n",
520 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
523 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
524 if ((!do_route) && (pr->sender_pid == 0))
527 do_route = GNUNET_YES;
531 bm |= GET_MESSAGE_BIT_RETURN_TO;
534 if (NULL != pr->public_data.target)
536 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
539 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
540 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_PeerIdentity);
541 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
542 if (buf_size < msize)
544 gm = (struct GetMessage *) lbuf;
545 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
546 gm->header.size = htons (msize);
547 gm->type = htonl (pr->public_data.type);
550 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
551 pr->public_data.priority + 1);
554 pr->public_data.priority -= prio;
555 pr->public_data.num_transmissions++;
556 pr->public_data.respect_offered += prio;
557 gm->priority = htonl (prio);
558 now = GNUNET_TIME_absolute_get ();
559 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
560 gm->ttl = htonl (ttl / 1000LL / 1000LL);
561 gm->filter_mutator = htonl (pr->mingle);
562 gm->hash_bitmap = htonl (bm);
563 gm->query = pr->public_data.query;
564 ext = (struct GNUNET_PeerIdentity *) &gm[1];
567 GNUNET_PEER_resolve (pr->sender_pid,
569 if (NULL != pr->public_data.target)
570 ext[k++] = *pr->public_data.target;
572 GNUNET_assert (GNUNET_SYSERR !=
573 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
576 memcpy (buf, gm, msize);
582 * Iterator to free pending requests.
584 * @param cls closure, unused
585 * @param key current key code
586 * @param value value in the hash map (pending request)
587 * @return #GNUNET_YES (we should continue to iterate)
590 clean_request (void *cls, const struct GNUNET_HashCode *key, void *value)
592 struct GSF_PendingRequest *pr = value;
593 GSF_LocalLookupContinuation cont;
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Cleaning up pending request for `%s'.\n",
598 if (NULL != pr->cadet_request)
600 pr->cadet_retry_count = CADET_RETRY_MAX;
601 GSF_cadet_query_cancel (pr->cadet_request);
602 pr->cadet_request = NULL;
604 if (NULL != (cont = pr->llc_cont))
607 cont (pr->llc_cont_cls, pr, pr->local_result);
609 GSF_plan_notify_request_done_ (pr);
610 GNUNET_free_non_null (pr->replies_seen);
613 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
616 GNUNET_PEER_change_rc (pr->sender_pid, -1);
618 GNUNET_PEER_change_rc (pr->origin_pid, -1);
620 if (NULL != pr->hnode)
622 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
627 GNUNET_DATASTORE_cancel (pr->qe);
632 GNUNET_DHT_get_stop (pr->gh);
635 if (NULL != pr->warn_task)
637 GNUNET_SCHEDULER_cancel (pr->warn_task);
638 pr->warn_task = NULL;
640 GNUNET_assert (GNUNET_OK ==
641 GNUNET_CONTAINER_multihashmap_remove (pr_map,
642 &pr->public_data.query,
644 GNUNET_STATISTICS_update (GSF_stats,
645 gettext_noop ("# Pending requests active"), -1,
653 * Explicitly cancel a pending request.
655 * @param pr request to cancel
656 * @param full_cleanup fully purge the request
659 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
661 GSF_LocalLookupContinuation cont;
664 return; /* already cleaned up! */
665 if (GNUNET_YES != full_cleanup)
667 /* make request inactive (we're no longer interested in more results),
668 * but do NOT remove from our data-structures, we still need it there
669 * to prevent the request from looping */
671 if (NULL != pr->cadet_request)
673 pr->cadet_retry_count = CADET_RETRY_MAX;
674 GSF_cadet_query_cancel (pr->cadet_request);
675 pr->cadet_request = NULL;
677 if (NULL != (cont = pr->llc_cont))
680 cont (pr->llc_cont_cls, pr, pr->local_result);
682 GSF_plan_notify_request_done_ (pr);
685 GNUNET_DATASTORE_cancel (pr->qe);
690 GNUNET_DHT_get_stop (pr->gh);
693 if (NULL != pr->warn_task)
695 GNUNET_SCHEDULER_cancel (pr->warn_task);
696 pr->warn_task = NULL;
700 GNUNET_assert (GNUNET_YES ==
701 clean_request (NULL, &pr->public_data.query, pr));
706 * Iterate over all pending requests.
708 * @param it function to call for each request
709 * @param cls closure for it
712 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
714 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
715 (GNUNET_CONTAINER_HashMapIterator) it,
721 * Closure for process_reply() function.
723 struct ProcessReplyClosure
726 * The data for the reply.
731 * Who gave us this reply? NULL for local host (or DHT)
733 struct GSF_ConnectedPeer *sender;
736 * When the reply expires.
738 struct GNUNET_TIME_Absolute expiration;
748 enum GNUNET_BLOCK_Type type;
751 * Control flags for evaluation.
753 enum GNUNET_BLOCK_EvaluationOptions eo;
756 * How much was this reply worth to us?
761 * Anonymity requirements for this reply.
763 uint32_t anonymity_level;
766 * Evaluation result (returned).
768 enum GNUNET_BLOCK_EvaluationResult eval;
771 * Did we find a matching request?
778 * Update the performance data for the sender (if any) since
779 * the sender successfully answered one of our queries.
781 * @param prq information about the sender
782 * @param pr request that was satisfied
785 update_request_performance_data (struct ProcessReplyClosure *prq,
786 struct GSF_PendingRequest *pr)
788 if (prq->sender == NULL)
790 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
796 * We have received a reply; handle it!
798 * @param cls response (a `struct ProcessReplyClosure`)
799 * @param key our query
800 * @param value value in the hash map (info about the query)
801 * @return #GNUNET_YES (we should continue to iterate)
804 process_reply (void *cls,
805 const struct GNUNET_HashCode *key,
808 struct ProcessReplyClosure *prq = cls;
809 struct GSF_PendingRequest *pr = value;
810 struct GNUNET_HashCode chash;
811 struct GNUNET_TIME_Absolute last_transmission;
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Matched result (type %u) for query `%s' with pending request\n",
817 (unsigned int) prq->type,
819 GNUNET_STATISTICS_update (GSF_stats,
820 gettext_noop ("# replies received and matched"), 1,
823 GNUNET_BLOCK_evaluate (GSF_block_ctx,
835 case GNUNET_BLOCK_EVALUATION_OK_MORE:
836 update_request_performance_data (prq, pr);
838 case GNUNET_BLOCK_EVALUATION_OK_LAST:
839 /* short cut: stop processing early, no BF-update, etc. */
840 update_request_performance_data (prq, pr);
841 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
842 GNUNET_TIME_absolute_get_duration (pr->
843 public_data.start_time).rel_value_us);
845 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
848 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
849 /* pass on to other peers / local clients */
850 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
851 last_transmission, prq->type, prq->data, prq->size);
853 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
854 #if INSANE_STATISTICS
855 GNUNET_STATISTICS_update (GSF_stats,
857 ("# duplicate replies discarded (bloomfilter)"),
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Duplicate response, discarding.\n");
862 return GNUNET_YES; /* duplicate */
863 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
864 GNUNET_STATISTICS_update (GSF_stats,
866 ("# irrelevant replies discarded"),
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "Irrelevant response, ignoring.\n");
871 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
872 return GNUNET_YES; /* wrong namespace */
873 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
876 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
879 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
880 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
881 _("Unsupported block type %u\n"),
885 /* update bloomfilter */
886 GNUNET_CRYPTO_hash (prq->data,
889 GSF_pending_request_update_ (pr,
892 if (NULL == prq->sender)
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Found result for query `%s' in local datastore\n",
897 GNUNET_STATISTICS_update (GSF_stats,
898 gettext_noop ("# results found locally"),
904 GSF_dht_lookup_ (pr);
906 prq->priority += pr->public_data.original_priority;
907 pr->public_data.priority = 0;
908 pr->public_data.original_priority = 0;
909 pr->public_data.results_found++;
910 prq->request_found = GNUNET_YES;
911 /* finally, pass on to other peer / local client */
912 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
915 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
919 prq->anonymity_level,
930 * Context for put_migration_continuation().
932 struct PutMigrationContext
936 * Start time for the operation.
938 struct GNUNET_TIME_Absolute start;
943 struct GNUNET_PeerIdentity origin;
946 * #GNUNET_YES if we had a matching request for this block,
954 * Continuation called to notify client about result of the
958 * @param success #GNUNET_SYSERR on failure
959 * @param min_expiration minimum expiration time required for content to be stored
960 * @param msg NULL on success, otherwise an error message
963 put_migration_continuation (void *cls, int success,
964 struct GNUNET_TIME_Absolute min_expiration,
967 struct PutMigrationContext *pmc = cls;
968 struct GSF_ConnectedPeer *cp;
969 struct GNUNET_TIME_Relative mig_pause;
970 struct GSF_PeerPerformanceData *ppd;
972 if (NULL != datastore_put_load)
974 if (GNUNET_SYSERR != success)
976 GNUNET_LOAD_update (datastore_put_load,
977 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
981 /* on queue failure / timeout, increase the put load dramatically */
982 GNUNET_LOAD_update (datastore_put_load,
983 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
986 cp = GSF_peer_get_ (&pmc->origin);
987 if (GNUNET_OK == success)
991 ppd = GSF_get_peer_performance_data_ (cp);
992 ppd->migration_delay.rel_value_us /= 2;
997 if ( (GNUNET_NO == success) &&
998 (GNUNET_NO == pmc->requested) &&
1001 ppd = GSF_get_peer_performance_data_ (cp);
1002 if (min_expiration.abs_value_us > 0)
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Asking to stop migration for %s because datastore is full\n",
1006 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1007 GSF_block_peer_migration_ (cp, min_expiration);
1011 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1012 ppd->migration_delay);
1013 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1014 ppd->migration_delay);
1015 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1016 ppd->migration_delay.rel_value_us);
1017 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Replicated content already exists locally, asking to stop migration for %s\n",
1020 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1022 GSF_block_peer_migration_ (cp,
1023 GNUNET_TIME_relative_to_absolute (mig_pause));
1027 GNUNET_STATISTICS_update (GSF_stats,
1028 gettext_noop ("# Datastore `PUT' failures"), 1,
1034 * Test if the DATABASE (PUT) load on this peer is too high
1035 * to even consider processing the query at
1038 * @param priority the priority of the item
1039 * @return #GNUNET_YES if the load is too high to do anything (load high)
1040 * #GNUNET_NO to process normally (load normal or low)
1043 test_put_load_too_high (uint32_t priority)
1047 if (NULL == datastore_put_load)
1049 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1050 return GNUNET_NO; /* very fast */
1051 ld = GNUNET_LOAD_get_load (datastore_put_load);
1052 if (ld < 2.0 * (1 + priority))
1054 GNUNET_STATISTICS_update (GSF_stats,
1056 ("# storage requests dropped due to high load"), 1,
1063 * Iterator called on each result obtained for a DHT
1064 * operation that expects a reply
1066 * @param cls closure
1067 * @param exp when will this value expire
1068 * @param key key of the result
1069 * @param get_path peers on reply path (or NULL if not recorded)
1070 * @param get_path_length number of entries in @a get_path
1071 * @param put_path peers on the PUT path (or NULL if not recorded)
1072 * @param put_path_length number of entries in @a get_path
1073 * @param type type of the result
1074 * @param size number of bytes in @a data
1075 * @param data pointer to the result data
1078 handle_dht_reply (void *cls,
1079 struct GNUNET_TIME_Absolute exp,
1080 const struct GNUNET_HashCode *key,
1081 const struct GNUNET_PeerIdentity *get_path,
1082 unsigned int get_path_length,
1083 const struct GNUNET_PeerIdentity *put_path,
1084 unsigned int put_path_length,
1085 enum GNUNET_BLOCK_Type type,
1089 struct GSF_PendingRequest *pr = cls;
1090 struct ProcessReplyClosure prq;
1091 struct PutMigrationContext *pmc;
1093 GNUNET_STATISTICS_update (GSF_stats,
1094 gettext_noop ("# Replies received from DHT"), 1,
1096 memset (&prq, 0, sizeof (prq));
1098 prq.expiration = exp;
1099 /* do not allow migrated content to live longer than 1 year */
1100 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1104 prq.eo = GNUNET_BLOCK_EO_NONE;
1105 process_reply (&prq, key, pr);
1106 if ((GNUNET_YES == active_to_migration) &&
1107 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Replicating result for query `%s' with priority %u\n",
1111 GNUNET_h2s (key), prq.priority);
1112 pmc = GNUNET_new (struct PutMigrationContext);
1113 pmc->start = GNUNET_TIME_absolute_get ();
1114 pmc->requested = GNUNET_YES;
1116 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1118 0 /* replication */ ,
1119 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1120 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1121 &put_migration_continuation, pmc))
1123 put_migration_continuation (pmc,
1125 GNUNET_TIME_UNIT_ZERO_ABS,
1133 * Consider looking up the data in the DHT (anonymity-level permitting).
1135 * @param pr the pending request to process
1138 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1142 struct GNUNET_PeerIdentity pi;
1143 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1145 if (0 != pr->public_data.anonymity_level)
1149 GNUNET_DHT_get_stop (pr->gh);
1154 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1156 GNUNET_assert (0 != pr->sender_pid);
1157 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1158 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1159 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1162 GNUNET_DHT_get_start (GSF_dht,
1163 pr->public_data.type, &pr->public_data.query,
1164 DHT_GET_REPLICATION,
1165 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1166 xquery, xquery_size, &handle_dht_reply, pr);
1167 if ( (NULL != pr->gh) &&
1168 (0 != pr->replies_seen_count) )
1169 GNUNET_DHT_get_filter_known_results (pr->gh,
1170 pr->replies_seen_count,
1176 * Function called with a reply from the cadet.
1178 * @param cls the pending request struct
1179 * @param type type of the block, ANY on error
1180 * @param expiration expiration time for the block
1181 * @param data_size number of bytes in @a data, 0 on error
1182 * @param data reply block data, NULL on error
1185 cadet_reply_proc (void *cls,
1186 enum GNUNET_BLOCK_Type type,
1187 struct GNUNET_TIME_Absolute expiration,
1191 struct GSF_PendingRequest *pr = cls;
1192 struct ProcessReplyClosure prq;
1193 struct GNUNET_HashCode query;
1195 pr->cadet_request = NULL;
1196 if (GNUNET_BLOCK_TYPE_ANY == type)
1198 GNUNET_break (NULL == data);
1199 GNUNET_break (0 == data_size);
1200 pr->cadet_retry_count++;
1201 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1202 return; /* give up on cadet */
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Error retrieiving block via cadet\n");
1205 /* retry -- without delay, as this is non-anonymous
1206 and cadet/cadet connect will take some time anyway */
1207 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1208 &pr->public_data.query,
1209 pr->public_data.type,
1215 GNUNET_BLOCK_get_key (GSF_block_ctx,
1217 data, data_size, &query))
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Failed to derive key for block of type %d\n",
1222 GNUNET_break_op (0);
1225 GNUNET_STATISTICS_update (GSF_stats,
1226 gettext_noop ("# Replies received from CADET"), 1,
1228 memset (&prq, 0, sizeof (prq));
1230 prq.expiration = expiration;
1231 /* do not allow migrated content to live longer than 1 year */
1232 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1234 prq.size = data_size;
1236 prq.eo = GNUNET_BLOCK_EO_NONE;
1237 process_reply (&prq, &query, pr);
1242 * Consider downloading via cadet (if possible)
1244 * @param pr the pending request to process
1247 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1249 if (0 != pr->public_data.anonymity_level)
1251 if (0 == pr->public_data.target)
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Cannot do cadet-based download, target peer not known\n");
1257 if (NULL != pr->cadet_request)
1259 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1260 &pr->public_data.query,
1261 pr->public_data.type,
1268 * Task that issues a warning if the datastore lookup takes too long.
1270 * @param cls the 'struct GSF_PendingRequest'
1271 * @param tc task context
1274 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1276 struct GSF_PendingRequest *pr = cls;
1278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1279 _("Datastore lookup already took %s!\n"),
1280 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1283 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1290 * Task that issues a warning if the datastore lookup takes too long.
1292 * @param cls the 'struct GSF_PendingRequest'
1293 * @param tc task context
1296 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1298 struct GSF_PendingRequest *pr = cls;
1300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1301 _("On-demand lookup already took %s!\n"),
1302 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1304 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1305 &odc_warn_delay_task, pr);
1310 * We're processing (local) results for a search request
1311 * from another peer. Pass applicable results to the
1312 * peer and if we are done either clean up (operation
1313 * complete) or forward to other peers (more results possible).
1315 * @param cls our closure (`struct GSF_PendingRequest *`)
1316 * @param key key for the content
1317 * @param size number of bytes in @a data
1318 * @param data content stored
1319 * @param type type of the content
1320 * @param priority priority of the content
1321 * @param anonymity anonymity-level for the content
1322 * @param expiration expiration time for the content
1323 * @param uid unique identifier for the datum;
1324 * maybe 0 if no unique identifier is available
1327 process_local_reply (void *cls,
1328 const struct GNUNET_HashCode *key,
1331 enum GNUNET_BLOCK_Type type,
1334 struct GNUNET_TIME_Absolute expiration,
1337 struct GSF_PendingRequest *pr = cls;
1338 GSF_LocalLookupContinuation cont;
1339 struct ProcessReplyClosure prq;
1340 struct GNUNET_HashCode query;
1341 unsigned int old_rf;
1343 GNUNET_SCHEDULER_cancel (pr->warn_task);
1344 pr->warn_task = NULL;
1350 #if INSANE_STATISTICS
1351 GNUNET_STATISTICS_update (GSF_stats,
1353 ("# Datastore lookups concluded (no results)"),
1357 if (GNUNET_NO == pr->have_first_uid)
1359 pr->first_uid = uid;
1360 pr->have_first_uid = 1;
1364 if ((uid == pr->first_uid) && (key != NULL))
1366 GNUNET_STATISTICS_update (GSF_stats,
1368 ("# Datastore lookups concluded (seen all)"),
1370 key = NULL; /* all replies seen! */
1372 pr->have_first_uid++;
1373 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1375 GNUNET_STATISTICS_update (GSF_stats,
1377 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1379 key = NULL; /* all replies seen! */
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1386 "No further local responses available.\n");
1387 #if INSANE_STATISTICS
1388 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1389 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1390 GNUNET_STATISTICS_update (GSF_stats,
1392 ("# requested DBLOCK or IBLOCK not found"), 1,
1395 goto check_error_and_continue;
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1399 GNUNET_h2s (key), type, (unsigned long long) uid);
1400 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Found ONDEMAND block, performing on-demand encoding\n");
1404 GNUNET_STATISTICS_update (GSF_stats,
1406 ("# on-demand blocks matched requests"), 1,
1408 pr->qe_start = GNUNET_TIME_absolute_get ();
1410 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1411 &odc_warn_delay_task, pr);
1413 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1414 anonymity, expiration, uid,
1415 &process_local_reply, pr))
1417 GNUNET_STATISTICS_update (GSF_stats,
1419 ("# on-demand lookups performed successfully"),
1421 return; /* we're done */
1423 GNUNET_STATISTICS_update (GSF_stats,
1424 gettext_noop ("# on-demand lookups failed"), 1,
1426 GNUNET_SCHEDULER_cancel (pr->warn_task);
1428 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1429 &warn_delay_task, pr);
1431 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
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 &
1438 pr->public_data.options)) ? UINT_MAX : 1
1439 /* queue priority */ ,
1441 (GSF_PRO_PRIORITY_UNLIMITED &
1442 pr->public_data.options)) ? UINT_MAX :
1443 GSF_datastore_queue_size
1444 /* max queue size */ ,
1445 GNUNET_TIME_UNIT_FOREVER_REL,
1446 &process_local_reply, pr);
1448 return; /* we're done */
1449 GNUNET_STATISTICS_update (GSF_stats,
1451 ("# Datastore lookups concluded (error queueing)"),
1453 goto check_error_and_continue;
1455 old_rf = pr->public_data.results_found;
1456 memset (&prq, 0, sizeof (prq));
1458 prq.expiration = expiration;
1461 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1464 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1465 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1466 pr->qe_start = GNUNET_TIME_absolute_get ();
1468 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1469 &warn_delay_task, pr);
1471 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1472 &pr->public_data.query,
1473 pr->public_data.type ==
1474 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1475 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1477 (GSF_PRO_PRIORITY_UNLIMITED &
1478 pr->public_data.options)) ? UINT_MAX : 1
1479 /* queue priority */ ,
1481 (GSF_PRO_PRIORITY_UNLIMITED &
1482 pr->public_data.options)) ? UINT_MAX :
1483 GSF_datastore_queue_size
1484 /* max queue size */ ,
1485 GNUNET_TIME_UNIT_FOREVER_REL,
1486 &process_local_reply, pr);
1489 GNUNET_STATISTICS_update (GSF_stats,
1491 ("# Datastore lookups concluded (error queueing)"),
1493 goto check_error_and_continue;
1498 prq.priority = priority;
1499 prq.request_found = GNUNET_NO;
1500 prq.anonymity_level = anonymity;
1501 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1502 GSF_update_datastore_delay_ (pr->public_data.start_time);
1503 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1504 process_reply (&prq, key, pr);
1505 pr->local_result = prq.eval;
1506 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1508 GNUNET_STATISTICS_update (GSF_stats,
1510 ("# Datastore lookups concluded (found last result)"),
1513 goto check_error_and_continue;
1515 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1516 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1517 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Load too high, done with request\n");
1521 GNUNET_STATISTICS_update (GSF_stats,
1522 gettext_noop ("# Datastore lookups concluded (load too high)"),
1525 goto check_error_and_continue;
1527 pr->qe_start = GNUNET_TIME_absolute_get ();
1529 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1533 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1534 &pr->public_data.query,
1535 pr->public_data.type ==
1536 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1537 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1539 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1540 public_data.options)) ? UINT_MAX : 1
1541 /* queue priority */ ,
1543 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1544 public_data.options)) ? UINT_MAX :
1545 GSF_datastore_queue_size
1546 /* max queue size */ ,
1547 GNUNET_TIME_UNIT_FOREVER_REL,
1548 &process_local_reply, pr);
1549 /* check if we successfully queued another datastore request;
1550 * if so, return, otherwise call our continuation (if we have
1552 check_error_and_continue:
1555 if (NULL != pr->warn_task)
1557 GNUNET_SCHEDULER_cancel (pr->warn_task);
1558 pr->warn_task = NULL;
1560 if (NULL == (cont = pr->llc_cont))
1561 return; /* no continuation */
1562 pr->llc_cont = NULL;
1563 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1565 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1567 /* Signal that we are done and that there won't be any
1568 additional results to allow client to clean up state. */
1570 GNUNET_BLOCK_EVALUATION_OK_LAST,
1573 GNUNET_TIME_UNIT_ZERO_ABS,
1574 GNUNET_TIME_UNIT_FOREVER_ABS,
1575 GNUNET_BLOCK_TYPE_ANY,
1578 /* Finally, call our continuation to signal that we are
1579 done with local processing of this request; i.e. to
1580 start reading again from the client. */
1581 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1585 cont (pr->llc_cont_cls, pr, pr->local_result);
1590 * Is the given target a legitimate peer for forwarding the given request?
1594 * @return #GNUNET_YES if this request could be forwarded to the given peer
1597 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1598 const struct GNUNET_PeerIdentity *target)
1600 struct GNUNET_PeerIdentity pi;
1602 if (0 == pr->origin_pid)
1604 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1606 memcmp (&pi, target,
1607 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1613 * Look up the request in the local datastore.
1615 * @param pr the pending request to process
1616 * @param cont function to call at the end
1617 * @param cont_cls closure for @a cont
1620 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1621 GSF_LocalLookupContinuation cont,
1624 GNUNET_assert (NULL == pr->gh);
1625 GNUNET_assert (NULL == pr->cadet_request);
1626 GNUNET_assert (NULL == pr->llc_cont);
1627 pr->llc_cont = cont;
1628 pr->llc_cont_cls = cont_cls;
1629 pr->qe_start = GNUNET_TIME_absolute_get ();
1631 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1634 #if INSANE_STATISTICS
1635 GNUNET_STATISTICS_update (GSF_stats,
1636 gettext_noop ("# Datastore lookups initiated"), 1,
1640 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1641 &pr->public_data.query,
1642 pr->public_data.type ==
1643 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1644 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1646 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1647 public_data.options)) ? UINT_MAX : 1
1648 /* queue priority */ ,
1650 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1651 public_data.options)) ? UINT_MAX :
1652 GSF_datastore_queue_size
1653 /* max queue size */ ,
1654 GNUNET_TIME_UNIT_FOREVER_REL,
1655 &process_local_reply, pr);
1658 GNUNET_STATISTICS_update (GSF_stats,
1660 ("# Datastore lookups concluded (error queueing)"),
1662 GNUNET_SCHEDULER_cancel (pr->warn_task);
1663 pr->warn_task = NULL;
1664 pr->llc_cont = NULL;
1666 cont (cont_cls, pr, pr->local_result);
1672 * Handle P2P "CONTENT" message. Checks that the message is
1673 * well-formed and then checks if there are any pending requests for
1674 * this content and possibly passes it on (to local clients or other
1675 * peers). Does NOT perform migration (content caching at this peer).
1677 * @param cp the other peer involved (sender or receiver, NULL
1678 * for loopback messages where we are both sender and receiver)
1679 * @param message the actual message
1680 * @return #GNUNET_OK if the message was well-formed,
1681 * #GNUNET_SYSERR if the message was malformed (close connection,
1682 * do not cache under any circumstances)
1685 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1686 const struct GNUNET_MessageHeader *message)
1688 const struct PutMessage *put;
1691 enum GNUNET_BLOCK_Type type;
1692 struct GNUNET_TIME_Absolute expiration;
1693 struct GNUNET_HashCode query;
1694 struct ProcessReplyClosure prq;
1695 struct GNUNET_TIME_Relative block_time;
1697 struct PutMigrationContext *pmc;
1699 msize = ntohs (message->size);
1700 if (msize < sizeof (struct PutMessage))
1702 GNUNET_break_op (0);
1703 return GNUNET_SYSERR;
1705 put = (const struct PutMessage *) message;
1706 dsize = msize - sizeof (struct PutMessage);
1707 type = ntohl (put->type);
1708 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1709 /* do not allow migrated content to live longer than 1 year */
1710 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1712 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1713 return GNUNET_SYSERR;
1715 GNUNET_BLOCK_get_key (GSF_block_ctx,
1721 GNUNET_break_op (0);
1722 return GNUNET_SYSERR;
1724 GNUNET_STATISTICS_update (GSF_stats,
1725 gettext_noop ("# GAP PUT messages received"),
1728 /* now, lookup 'query' */
1729 prq.data = (const void *) &put[1];
1733 prq.expiration = expiration;
1735 prq.anonymity_level = UINT32_MAX;
1736 prq.request_found = GNUNET_NO;
1737 prq.eo = GNUNET_BLOCK_EO_NONE;
1738 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1744 GSF_connected_peer_change_preference_ (cp,
1745 CONTENT_BANDWIDTH_VALUE +
1746 1000 * prq.priority);
1747 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1749 if ((GNUNET_YES == active_to_migration) &&
1751 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Replicating result for query `%s' with priority %u\n",
1755 GNUNET_h2s (&query),
1757 pmc = GNUNET_new (struct PutMigrationContext);
1758 pmc->start = GNUNET_TIME_absolute_get ();
1759 pmc->requested = prq.request_found;
1760 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1761 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1764 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1765 prq.priority, 1 /* anonymity */ ,
1766 0 /* replication */ ,
1767 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1768 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1769 &put_migration_continuation, pmc))
1771 put_migration_continuation (pmc,
1773 GNUNET_TIME_UNIT_ZERO_ABS,
1777 else if (NULL != cp)
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Choosing not to keep content `%s' (%d/%d)\n",
1781 GNUNET_h2s (&query), active_to_migration,
1782 test_put_load_too_high (prq.priority));
1784 putl = GNUNET_LOAD_get_load (datastore_put_load);
1785 if ( (NULL != cp) &&
1786 (GNUNET_NO == prq.request_found) &&
1787 ( (GNUNET_YES != active_to_migration) ||
1788 (putl > 2.5 * (1 + prq.priority)) ) )
1790 if (GNUNET_YES != active_to_migration)
1791 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1793 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1795 GNUNET_CRYPTO_random_u32
1796 (GNUNET_CRYPTO_QUALITY_WEAK,
1797 (unsigned int) (60000 * putl * putl)));
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1800 GNUNET_STRINGS_relative_time_to_string (block_time,
1803 active_to_migration,
1804 (GNUNET_NO == prq.request_found));
1805 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1812 * Setup the subsystem.
1815 GSF_pending_request_init_ ()
1818 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1819 "MAX_PENDING_REQUESTS",
1820 &max_pending_requests))
1822 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1823 "fs", "MAX_PENDING_REQUESTS");
1825 active_to_migration =
1826 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1827 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1828 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1829 requests_by_expiration_heap =
1830 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1835 * Shutdown the subsystem.
1838 GSF_pending_request_done_ ()
1840 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1841 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1843 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1844 requests_by_expiration_heap = NULL;
1845 GNUNET_LOAD_value_free (datastore_put_load);
1846 datastore_put_load = NULL;
1850 /* end of gnunet-service-fs_pr.c */