2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * How much was this reply worth to us?
756 * Anonymity requirements for this reply.
758 uint32_t anonymity_level;
761 * Evaluation result (returned).
763 enum GNUNET_BLOCK_EvaluationResult eval;
766 * Did we find a matching request?
773 * Update the performance data for the sender (if any) since
774 * the sender successfully answered one of our queries.
776 * @param prq information about the sender
777 * @param pr request that was satisfied
780 update_request_performance_data (struct ProcessReplyClosure *prq,
781 struct GSF_PendingRequest *pr)
783 if (prq->sender == NULL)
785 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
791 * We have received a reply; handle it!
793 * @param cls response (struct ProcessReplyClosure)
794 * @param key our query
795 * @param value value in the hash map (info about the query)
796 * @return #GNUNET_YES (we should continue to iterate)
799 process_reply (void *cls,
800 const struct GNUNET_HashCode *key,
803 struct ProcessReplyClosure *prq = cls;
804 struct GSF_PendingRequest *pr = value;
805 struct GNUNET_HashCode chash;
806 struct GNUNET_TIME_Absolute last_transmission;
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Matched result (type %u) for query `%s' with pending request\n",
812 (unsigned int) prq->type, GNUNET_h2s (key));
813 GNUNET_STATISTICS_update (GSF_stats,
814 gettext_noop ("# replies received and matched"), 1,
817 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
822 case GNUNET_BLOCK_EVALUATION_OK_MORE:
823 update_request_performance_data (prq, pr);
825 case GNUNET_BLOCK_EVALUATION_OK_LAST:
826 /* short cut: stop processing early, no BF-update, etc. */
827 update_request_performance_data (prq, pr);
828 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
829 GNUNET_TIME_absolute_get_duration (pr->
830 public_data.start_time).rel_value_us);
832 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
835 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
836 /* pass on to other peers / local clients */
837 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
838 last_transmission, prq->type, prq->data, prq->size);
840 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
841 #if INSANE_STATISTICS
842 GNUNET_STATISTICS_update (GSF_stats,
844 ("# duplicate replies discarded (bloomfilter)"),
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Duplicate response, discarding.\n");
849 return GNUNET_YES; /* duplicate */
850 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
851 GNUNET_STATISTICS_update (GSF_stats,
853 ("# irrelevant replies discarded"),
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Irrelevant response, ignoring.\n");
858 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
859 return GNUNET_YES; /* wrong namespace */
860 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
863 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
866 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
871 /* update bloomfilter */
872 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
873 GSF_pending_request_update_ (pr, &chash, 1);
874 if (NULL == prq->sender)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Found result for query `%s' in local datastore\n",
879 GNUNET_STATISTICS_update (GSF_stats,
880 gettext_noop ("# results found locally"), 1,
885 GSF_dht_lookup_ (pr);
887 prq->priority += pr->public_data.original_priority;
888 pr->public_data.priority = 0;
889 pr->public_data.original_priority = 0;
890 pr->public_data.results_found++;
891 prq->request_found = GNUNET_YES;
892 /* finally, pass on to other peer / local client */
893 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
896 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
897 pr->rh (pr->rh_cls, prq->eval, pr,
898 prq->anonymity_level, prq->expiration,
899 last_transmission, prq->type, prq->data, prq->size);
905 * Context for put_migration_continuation().
907 struct PutMigrationContext
911 * Start time for the operation.
913 struct GNUNET_TIME_Absolute start;
918 struct GNUNET_PeerIdentity origin;
921 * #GNUNET_YES if we had a matching request for this block,
929 * Continuation called to notify client about result of the
933 * @param success #GNUNET_SYSERR on failure
934 * @param min_expiration minimum expiration time required for content to be stored
935 * @param msg NULL on success, otherwise an error message
938 put_migration_continuation (void *cls, int success,
939 struct GNUNET_TIME_Absolute min_expiration,
942 struct PutMigrationContext *pmc = cls;
943 struct GSF_ConnectedPeer *cp;
944 struct GNUNET_TIME_Relative mig_pause;
945 struct GSF_PeerPerformanceData *ppd;
947 if (NULL != datastore_put_load)
949 if (GNUNET_SYSERR != success)
951 GNUNET_LOAD_update (datastore_put_load,
952 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
956 /* on queue failure / timeout, increase the put load dramatically */
957 GNUNET_LOAD_update (datastore_put_load,
958 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
961 cp = GSF_peer_get_ (&pmc->origin);
962 if (GNUNET_OK == success)
966 ppd = GSF_get_peer_performance_data_ (cp);
967 ppd->migration_delay.rel_value_us /= 2;
972 if ( (GNUNET_NO == success) &&
973 (GNUNET_NO == pmc->requested) &&
976 ppd = GSF_get_peer_performance_data_ (cp);
977 if (min_expiration.abs_value_us > 0)
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Asking to stop migration for %s because datastore is full\n",
981 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
982 GSF_block_peer_migration_ (cp, min_expiration);
986 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
987 ppd->migration_delay);
988 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
989 ppd->migration_delay);
990 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
991 ppd->migration_delay.rel_value_us);
992 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Replicated content already exists locally, asking to stop migration for %s\n",
995 GNUNET_STRINGS_relative_time_to_string (mig_pause,
997 GSF_block_peer_migration_ (cp,
998 GNUNET_TIME_relative_to_absolute (mig_pause));
1002 GNUNET_STATISTICS_update (GSF_stats,
1003 gettext_noop ("# Datastore `PUT' failures"), 1,
1009 * Test if the DATABASE (PUT) load on this peer is too high
1010 * to even consider processing the query at
1013 * @param priority the priority of the item
1014 * @return #GNUNET_YES if the load is too high to do anything (load high)
1015 * #GNUNET_NO to process normally (load normal or low)
1018 test_put_load_too_high (uint32_t priority)
1022 if (NULL == datastore_put_load)
1024 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1025 return GNUNET_NO; /* very fast */
1026 ld = GNUNET_LOAD_get_load (datastore_put_load);
1027 if (ld < 2.0 * (1 + priority))
1029 GNUNET_STATISTICS_update (GSF_stats,
1031 ("# storage requests dropped due to high load"), 1,
1038 * Iterator called on each result obtained for a DHT
1039 * operation that expects a reply
1041 * @param cls closure
1042 * @param exp when will this value expire
1043 * @param key key of the result
1044 * @param get_path peers on reply path (or NULL if not recorded)
1045 * @param get_path_length number of entries in @a get_path
1046 * @param put_path peers on the PUT path (or NULL if not recorded)
1047 * @param put_path_length number of entries in @a get_path
1048 * @param type type of the result
1049 * @param size number of bytes in @a data
1050 * @param data pointer to the result data
1053 handle_dht_reply (void *cls,
1054 struct GNUNET_TIME_Absolute exp,
1055 const struct GNUNET_HashCode *key,
1056 const struct GNUNET_PeerIdentity *get_path,
1057 unsigned int get_path_length,
1058 const struct GNUNET_PeerIdentity *put_path,
1059 unsigned int put_path_length,
1060 enum GNUNET_BLOCK_Type type,
1064 struct GSF_PendingRequest *pr = cls;
1065 struct ProcessReplyClosure prq;
1066 struct PutMigrationContext *pmc;
1068 GNUNET_STATISTICS_update (GSF_stats,
1069 gettext_noop ("# Replies received from DHT"), 1,
1071 memset (&prq, 0, sizeof (prq));
1073 prq.expiration = exp;
1074 /* do not allow migrated content to live longer than 1 year */
1075 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1079 process_reply (&prq, key, pr);
1080 if ((GNUNET_YES == active_to_migration) &&
1081 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Replicating result for query `%s' with priority %u\n",
1085 GNUNET_h2s (key), prq.priority);
1086 pmc = GNUNET_new (struct PutMigrationContext);
1087 pmc->start = GNUNET_TIME_absolute_get ();
1088 pmc->requested = GNUNET_YES;
1090 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1092 0 /* replication */ ,
1093 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1094 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1095 &put_migration_continuation, pmc))
1097 put_migration_continuation (pmc,
1099 GNUNET_TIME_UNIT_ZERO_ABS,
1107 * Consider looking up the data in the DHT (anonymity-level permitting).
1109 * @param pr the pending request to process
1112 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1116 struct GNUNET_PeerIdentity pi;
1117 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1119 if (0 != pr->public_data.anonymity_level)
1123 GNUNET_DHT_get_stop (pr->gh);
1128 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1130 GNUNET_assert (0 != pr->sender_pid);
1131 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1132 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1133 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1136 GNUNET_DHT_get_start (GSF_dht,
1137 pr->public_data.type, &pr->public_data.query,
1138 DHT_GET_REPLICATION,
1139 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1140 xquery, xquery_size, &handle_dht_reply, pr);
1141 if ( (NULL != pr->gh) &&
1142 (0 != pr->replies_seen_count) )
1143 GNUNET_DHT_get_filter_known_results (pr->gh,
1144 pr->replies_seen_count,
1150 * Function called with a reply from the cadet.
1152 * @param cls the pending request struct
1153 * @param type type of the block, ANY on error
1154 * @param expiration expiration time for the block
1155 * @param data_size number of bytes in @a data, 0 on error
1156 * @param data reply block data, NULL on error
1159 cadet_reply_proc (void *cls,
1160 enum GNUNET_BLOCK_Type type,
1161 struct GNUNET_TIME_Absolute expiration,
1165 struct GSF_PendingRequest *pr = cls;
1166 struct ProcessReplyClosure prq;
1167 struct GNUNET_HashCode query;
1169 pr->cadet_request = NULL;
1170 if (GNUNET_BLOCK_TYPE_ANY == type)
1172 GNUNET_break (NULL == data);
1173 GNUNET_break (0 == data_size);
1174 pr->cadet_retry_count++;
1175 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1176 return; /* give up on cadet */
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "Error retrieiving block via cadet\n");
1179 /* retry -- without delay, as this is non-anonymous
1180 and cadet/cadet connect will take some time anyway */
1181 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1182 &pr->public_data.query,
1183 pr->public_data.type,
1189 GNUNET_BLOCK_get_key (GSF_block_ctx,
1191 data, data_size, &query))
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "Failed to derive key for block of type %d\n",
1196 GNUNET_break_op (0);
1199 GNUNET_STATISTICS_update (GSF_stats,
1200 gettext_noop ("# Replies received from CADET"), 1,
1202 memset (&prq, 0, sizeof (prq));
1204 prq.expiration = expiration;
1205 /* do not allow migrated content to live longer than 1 year */
1206 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1208 prq.size = data_size;
1210 process_reply (&prq, &query, pr);
1215 * Consider downloading via cadet (if possible)
1217 * @param pr the pending request to process
1220 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1222 if (0 != pr->public_data.anonymity_level)
1224 if (0 == pr->public_data.target)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Cannot do cadet-based download, target peer not known\n");
1230 if (NULL != pr->cadet_request)
1232 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1233 &pr->public_data.query,
1234 pr->public_data.type,
1241 * Task that issues a warning if the datastore lookup takes too long.
1243 * @param cls the 'struct GSF_PendingRequest'
1244 * @param tc task context
1247 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1249 struct GSF_PendingRequest *pr = cls;
1251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1252 _("Datastore lookup already took %s!\n"),
1253 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1255 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1261 * Task that issues a warning if the datastore lookup takes too long.
1263 * @param cls the 'struct GSF_PendingRequest'
1264 * @param tc task context
1267 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1269 struct GSF_PendingRequest *pr = cls;
1271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1272 _("On-demand lookup already took %s!\n"),
1273 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1275 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1276 &odc_warn_delay_task, pr);
1281 * We're processing (local) results for a search request
1282 * from another peer. Pass applicable results to the
1283 * peer and if we are done either clean up (operation
1284 * complete) or forward to other peers (more results possible).
1286 * @param cls our closure (struct PendingRequest)
1287 * @param key key for the content
1288 * @param size number of bytes in data
1289 * @param data content stored
1290 * @param type type of the content
1291 * @param priority priority of the content
1292 * @param anonymity anonymity-level for the content
1293 * @param expiration expiration time for the content
1294 * @param uid unique identifier for the datum;
1295 * maybe 0 if no unique identifier is available
1298 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1299 const void *data, enum GNUNET_BLOCK_Type type,
1300 uint32_t priority, uint32_t anonymity,
1301 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1303 struct GSF_PendingRequest *pr = cls;
1304 GSF_LocalLookupContinuation cont;
1305 struct ProcessReplyClosure prq;
1306 struct GNUNET_HashCode query;
1307 unsigned int old_rf;
1309 GNUNET_SCHEDULER_cancel (pr->warn_task);
1310 pr->warn_task = NULL;
1316 #if INSANE_STATISTICS
1317 GNUNET_STATISTICS_update (GSF_stats,
1319 ("# Datastore lookups concluded (no results)"),
1323 if (GNUNET_NO == pr->have_first_uid)
1325 pr->first_uid = uid;
1326 pr->have_first_uid = 1;
1330 if ((uid == pr->first_uid) && (key != NULL))
1332 GNUNET_STATISTICS_update (GSF_stats,
1334 ("# Datastore lookups concluded (seen all)"),
1336 key = NULL; /* all replies seen! */
1338 pr->have_first_uid++;
1339 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1341 GNUNET_STATISTICS_update (GSF_stats,
1343 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1345 key = NULL; /* all replies seen! */
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1352 "No further local responses available.\n");
1353 #if INSANE_STATISTICS
1354 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1355 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1356 GNUNET_STATISTICS_update (GSF_stats,
1358 ("# requested DBLOCK or IBLOCK not found"), 1,
1361 goto check_error_and_continue;
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1365 GNUNET_h2s (key), type, (unsigned long long) uid);
1366 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "Found ONDEMAND block, performing on-demand encoding\n");
1370 GNUNET_STATISTICS_update (GSF_stats,
1372 ("# on-demand blocks matched requests"), 1,
1374 pr->qe_start = GNUNET_TIME_absolute_get ();
1376 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1377 &odc_warn_delay_task, pr);
1379 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1380 anonymity, expiration, uid,
1381 &process_local_reply, pr))
1383 GNUNET_STATISTICS_update (GSF_stats,
1385 ("# on-demand lookups performed successfully"),
1387 return; /* we're done */
1389 GNUNET_STATISTICS_update (GSF_stats,
1390 gettext_noop ("# on-demand lookups failed"), 1,
1392 GNUNET_SCHEDULER_cancel (pr->warn_task);
1394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1395 &warn_delay_task, pr);
1397 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1398 &pr->public_data.query,
1399 pr->public_data.type ==
1400 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1401 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1403 (GSF_PRO_PRIORITY_UNLIMITED &
1404 pr->public_data.options)) ? UINT_MAX : 1
1405 /* queue priority */ ,
1407 (GSF_PRO_PRIORITY_UNLIMITED &
1408 pr->public_data.options)) ? UINT_MAX :
1409 GSF_datastore_queue_size
1410 /* max queue size */ ,
1411 GNUNET_TIME_UNIT_FOREVER_REL,
1412 &process_local_reply, pr);
1414 return; /* we're done */
1415 GNUNET_STATISTICS_update (GSF_stats,
1417 ("# Datastore lookups concluded (error queueing)"),
1419 goto check_error_and_continue;
1421 old_rf = pr->public_data.results_found;
1422 memset (&prq, 0, sizeof (prq));
1424 prq.expiration = expiration;
1427 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1430 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1431 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1432 pr->qe_start = GNUNET_TIME_absolute_get ();
1434 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1435 &warn_delay_task, pr);
1437 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1438 &pr->public_data.query,
1439 pr->public_data.type ==
1440 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1441 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1443 (GSF_PRO_PRIORITY_UNLIMITED &
1444 pr->public_data.options)) ? UINT_MAX : 1
1445 /* queue priority */ ,
1447 (GSF_PRO_PRIORITY_UNLIMITED &
1448 pr->public_data.options)) ? UINT_MAX :
1449 GSF_datastore_queue_size
1450 /* max queue size */ ,
1451 GNUNET_TIME_UNIT_FOREVER_REL,
1452 &process_local_reply, pr);
1455 GNUNET_STATISTICS_update (GSF_stats,
1457 ("# Datastore lookups concluded (error queueing)"),
1459 goto check_error_and_continue;
1464 prq.priority = priority;
1465 prq.request_found = GNUNET_NO;
1466 prq.anonymity_level = anonymity;
1467 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1468 GSF_update_datastore_delay_ (pr->public_data.start_time);
1469 process_reply (&prq, key, pr);
1470 pr->local_result = prq.eval;
1471 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1473 GNUNET_STATISTICS_update (GSF_stats,
1475 ("# Datastore lookups concluded (found last result)"),
1477 goto check_error_and_continue;
1479 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1480 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1481 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1484 GNUNET_STATISTICS_update (GSF_stats,
1486 ("# Datastore lookups concluded (load too high)"),
1488 goto check_error_and_continue;
1490 pr->qe_start = GNUNET_TIME_absolute_get ();
1492 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1495 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1496 &pr->public_data.query,
1497 pr->public_data.type ==
1498 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1499 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1501 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1502 public_data.options)) ? UINT_MAX : 1
1503 /* queue priority */ ,
1505 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1506 public_data.options)) ? UINT_MAX :
1507 GSF_datastore_queue_size
1508 /* max queue size */ ,
1509 GNUNET_TIME_UNIT_FOREVER_REL,
1510 &process_local_reply, pr);
1511 /* check if we successfully queued another datastore request;
1512 * if so, return, otherwise call our continuation (if we have
1514 check_error_and_continue:
1517 if (NULL != pr->warn_task)
1519 GNUNET_SCHEDULER_cancel (pr->warn_task);
1520 pr->warn_task = NULL;
1522 if (NULL == (cont = pr->llc_cont))
1523 return; /* no continuation */
1524 pr->llc_cont = NULL;
1525 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1527 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1529 /* Signal that we are done and that there won't be any
1530 additional results to allow client to clean up state. */
1532 GNUNET_BLOCK_EVALUATION_OK_LAST,
1535 GNUNET_TIME_UNIT_ZERO_ABS,
1536 GNUNET_TIME_UNIT_FOREVER_ABS,
1537 GNUNET_BLOCK_TYPE_ANY,
1540 /* Finally, call our continuation to signal that we are
1541 done with local processing of this request; i.e. to
1542 start reading again from the client. */
1543 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1547 cont (pr->llc_cont_cls, pr, pr->local_result);
1552 * Is the given target a legitimate peer for forwarding the given request?
1556 * @return GNUNET_YES if this request could be forwarded to the given peer
1559 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1560 const struct GNUNET_PeerIdentity *target)
1562 struct GNUNET_PeerIdentity pi;
1564 if (0 == pr->origin_pid)
1566 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1568 memcmp (&pi, target,
1569 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1575 * Look up the request in the local datastore.
1577 * @param pr the pending request to process
1578 * @param cont function to call at the end
1579 * @param cont_cls closure for cont
1582 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1583 GSF_LocalLookupContinuation cont, void *cont_cls)
1585 GNUNET_assert (NULL == pr->gh);
1586 GNUNET_assert (NULL == pr->cadet_request);
1587 GNUNET_assert (NULL == pr->llc_cont);
1588 pr->llc_cont = cont;
1589 pr->llc_cont_cls = cont_cls;
1590 pr->qe_start = GNUNET_TIME_absolute_get ();
1592 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1594 #if INSANE_STATISTICS
1595 GNUNET_STATISTICS_update (GSF_stats,
1596 gettext_noop ("# Datastore lookups initiated"), 1,
1600 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1601 &pr->public_data.query,
1602 pr->public_data.type ==
1603 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1604 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1606 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1607 public_data.options)) ? UINT_MAX : 1
1608 /* queue priority */ ,
1610 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1611 public_data.options)) ? UINT_MAX :
1612 GSF_datastore_queue_size
1613 /* max queue size */ ,
1614 GNUNET_TIME_UNIT_FOREVER_REL,
1615 &process_local_reply, pr);
1618 GNUNET_STATISTICS_update (GSF_stats,
1620 ("# Datastore lookups concluded (error queueing)"),
1622 GNUNET_SCHEDULER_cancel (pr->warn_task);
1623 pr->warn_task = NULL;
1624 pr->llc_cont = NULL;
1626 cont (cont_cls, pr, pr->local_result);
1632 * Handle P2P "CONTENT" message. Checks that the message is
1633 * well-formed and then checks if there are any pending requests for
1634 * this content and possibly passes it on (to local clients or other
1635 * peers). Does NOT perform migration (content caching at this peer).
1637 * @param cp the other peer involved (sender or receiver, NULL
1638 * for loopback messages where we are both sender and receiver)
1639 * @param message the actual message
1640 * @return #GNUNET_OK if the message was well-formed,
1641 * #GNUNET_SYSERR if the message was malformed (close connection,
1642 * do not cache under any circumstances)
1645 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1646 const struct GNUNET_MessageHeader *message)
1648 const struct PutMessage *put;
1651 enum GNUNET_BLOCK_Type type;
1652 struct GNUNET_TIME_Absolute expiration;
1653 struct GNUNET_HashCode query;
1654 struct ProcessReplyClosure prq;
1655 struct GNUNET_TIME_Relative block_time;
1657 struct PutMigrationContext *pmc;
1659 msize = ntohs (message->size);
1660 if (msize < sizeof (struct PutMessage))
1662 GNUNET_break_op (0);
1663 return GNUNET_SYSERR;
1665 put = (const struct PutMessage *) message;
1666 dsize = msize - sizeof (struct PutMessage);
1667 type = ntohl (put->type);
1668 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1669 /* do not allow migrated content to live longer than 1 year */
1670 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1672 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1673 return GNUNET_SYSERR;
1675 GNUNET_BLOCK_get_key (GSF_block_ctx,
1681 GNUNET_break_op (0);
1682 return GNUNET_SYSERR;
1684 GNUNET_STATISTICS_update (GSF_stats,
1685 gettext_noop ("# GAP PUT messages received"), 1,
1687 /* now, lookup 'query' */
1688 prq.data = (const void *) &put[1];
1692 prq.expiration = expiration;
1694 prq.anonymity_level = UINT32_MAX;
1695 prq.request_found = GNUNET_NO;
1696 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1702 GSF_connected_peer_change_preference_ (cp,
1703 CONTENT_BANDWIDTH_VALUE +
1704 1000 * prq.priority);
1705 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1707 if ((GNUNET_YES == active_to_migration) &&
1709 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Replicating result for query `%s' with priority %u\n",
1713 GNUNET_h2s (&query),
1715 pmc = GNUNET_new (struct PutMigrationContext);
1716 pmc->start = GNUNET_TIME_absolute_get ();
1717 pmc->requested = prq.request_found;
1718 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1719 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1722 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1723 prq.priority, 1 /* anonymity */ ,
1724 0 /* replication */ ,
1725 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1726 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1727 &put_migration_continuation, pmc))
1729 put_migration_continuation (pmc,
1731 GNUNET_TIME_UNIT_ZERO_ABS,
1735 else if (NULL != cp)
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Choosing not to keep content `%s' (%d/%d)\n",
1739 GNUNET_h2s (&query), active_to_migration,
1740 test_put_load_too_high (prq.priority));
1742 putl = GNUNET_LOAD_get_load (datastore_put_load);
1743 if ( (NULL != cp) &&
1744 (GNUNET_NO == prq.request_found) &&
1745 ( (GNUNET_YES != active_to_migration) ||
1746 (putl > 2.5 * (1 + prq.priority)) ) )
1748 if (GNUNET_YES != active_to_migration)
1749 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1751 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1753 GNUNET_CRYPTO_random_u32
1754 (GNUNET_CRYPTO_QUALITY_WEAK,
1755 (unsigned int) (60000 * putl * putl)));
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1758 GNUNET_STRINGS_relative_time_to_string (block_time,
1761 active_to_migration,
1762 (GNUNET_NO == prq.request_found));
1763 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1770 * Setup the subsystem.
1773 GSF_pending_request_init_ ()
1776 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1777 "MAX_PENDING_REQUESTS",
1778 &max_pending_requests))
1780 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1781 "fs", "MAX_PENDING_REQUESTS");
1783 active_to_migration =
1784 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1785 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1786 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1787 requests_by_expiration_heap =
1788 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1793 * Shutdown the subsystem.
1796 GSF_pending_request_done_ ()
1798 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1799 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1801 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1802 requests_by_expiration_heap = NULL;
1803 GNUNET_LOAD_value_free (datastore_put_load);
1804 datastore_put_load = NULL;
1808 /* end of gnunet-service-fs_pr.c */