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) ||
426 (0 != memcmp (&pra->public_data.query,
427 &prb->public_data.query,
428 sizeof (struct GNUNET_HashCode))))
435 * Update a given pending request with additional replies
436 * that have been seen.
438 * @param pr request to update
439 * @param replies_seen hash codes of replies that we've seen
440 * @param replies_seen_count size of the replies_seen array
443 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
444 const struct GNUNET_HashCode * replies_seen,
445 unsigned int replies_seen_count)
448 struct GNUNET_HashCode mhash;
450 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
451 return; /* integer overflow */
452 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
454 /* we're responsible for the BF, full refresh */
455 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
456 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
457 replies_seen_count + pr->replies_seen_count);
458 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
459 sizeof (struct GNUNET_HashCode) * replies_seen_count);
460 pr->replies_seen_count += replies_seen_count;
461 refresh_bloomfilter (pr);
467 /* we're not the initiator, but the initiator did not give us
468 * any bloom-filter, so we need to create one on-the-fly */
470 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
473 GNUNET_BLOCK_construct_bloomfilter (pr->mingle,
479 for (i = 0; i < pr->replies_seen_count; i++)
481 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
484 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
490 GNUNET_DHT_get_filter_known_results (pr->gh,
497 * Generate the message corresponding to the given pending request for
498 * transmission to other peers (or at least determine its size).
500 * @param pr request to generate the message for
501 * @param buf_size number of bytes available in @a buf
502 * @param buf where to copy the message (can be NULL)
503 * @return number of bytes needed (if `>` @a buf_size) or used
506 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
507 size_t buf_size, void *buf)
509 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
510 struct GetMessage *gm;
511 struct GNUNET_PeerIdentity *ext;
517 struct GNUNET_TIME_Absolute now;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Building request message for `%s' of type %d\n",
524 GNUNET_h2s (&pr->public_data.query),
525 pr->public_data.type);
528 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
529 if ((!do_route) && (pr->sender_pid == 0))
532 do_route = GNUNET_YES;
536 bm |= GET_MESSAGE_BIT_RETURN_TO;
539 if (NULL != pr->public_data.target)
541 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
544 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
545 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_PeerIdentity);
546 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
547 if (buf_size < msize)
549 gm = (struct GetMessage *) lbuf;
550 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
551 gm->header.size = htons (msize);
552 gm->type = htonl (pr->public_data.type);
555 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
556 pr->public_data.priority + 1);
559 pr->public_data.priority -= prio;
560 pr->public_data.num_transmissions++;
561 pr->public_data.respect_offered += prio;
562 gm->priority = htonl (prio);
563 now = GNUNET_TIME_absolute_get ();
564 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
565 gm->ttl = htonl (ttl / 1000LL / 1000LL);
566 gm->filter_mutator = htonl (pr->mingle);
567 gm->hash_bitmap = htonl (bm);
568 gm->query = pr->public_data.query;
569 ext = (struct GNUNET_PeerIdentity *) &gm[1];
572 GNUNET_PEER_resolve (pr->sender_pid,
574 if (NULL != pr->public_data.target)
575 ext[k++] = *pr->public_data.target;
577 GNUNET_assert (GNUNET_SYSERR !=
578 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
581 memcpy (buf, gm, msize);
587 * Iterator to free pending requests.
589 * @param cls closure, unused
590 * @param key current key code
591 * @param value value in the hash map (pending request)
592 * @return #GNUNET_YES (we should continue to iterate)
595 clean_request (void *cls,
596 const struct GNUNET_HashCode *key,
599 struct GSF_PendingRequest *pr = value;
600 GSF_LocalLookupContinuation cont;
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Cleaning up pending request for `%s'.\n",
605 if (NULL != pr->cadet_request)
607 pr->cadet_retry_count = CADET_RETRY_MAX;
608 GSF_cadet_query_cancel (pr->cadet_request);
609 pr->cadet_request = NULL;
611 if (NULL != (cont = pr->llc_cont))
614 cont (pr->llc_cont_cls, pr, pr->local_result);
616 GSF_plan_notify_request_done_ (pr);
617 GNUNET_free_non_null (pr->replies_seen);
620 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
623 GNUNET_PEER_change_rc (pr->sender_pid, -1);
625 GNUNET_PEER_change_rc (pr->origin_pid, -1);
627 if (NULL != pr->hnode)
629 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
634 GNUNET_DATASTORE_cancel (pr->qe);
639 GNUNET_DHT_get_stop (pr->gh);
642 if (NULL != pr->warn_task)
644 GNUNET_SCHEDULER_cancel (pr->warn_task);
645 pr->warn_task = NULL;
647 GNUNET_assert (GNUNET_OK ==
648 GNUNET_CONTAINER_multihashmap_remove (pr_map,
649 &pr->public_data.query,
651 GNUNET_STATISTICS_update (GSF_stats,
652 gettext_noop ("# Pending requests active"), -1,
660 * Explicitly cancel a pending request.
662 * @param pr request to cancel
663 * @param full_cleanup fully purge the request
666 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
669 GSF_LocalLookupContinuation cont;
672 return; /* already cleaned up! */
673 if (GNUNET_YES != full_cleanup)
675 /* make request inactive (we're no longer interested in more results),
676 * but do NOT remove from our data-structures, we still need it there
677 * to prevent the request from looping */
679 if (NULL != pr->cadet_request)
681 pr->cadet_retry_count = CADET_RETRY_MAX;
682 GSF_cadet_query_cancel (pr->cadet_request);
683 pr->cadet_request = NULL;
685 if (NULL != (cont = pr->llc_cont))
688 cont (pr->llc_cont_cls,
692 GSF_plan_notify_request_done_ (pr);
695 GNUNET_DATASTORE_cancel (pr->qe);
700 GNUNET_DHT_get_stop (pr->gh);
703 if (NULL != pr->warn_task)
705 GNUNET_SCHEDULER_cancel (pr->warn_task);
706 pr->warn_task = NULL;
710 GNUNET_assert (GNUNET_YES ==
712 &pr->public_data.query,
718 * Iterate over all pending requests.
720 * @param it function to call for each request
721 * @param cls closure for @a it
724 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
726 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
727 (GNUNET_CONTAINER_HashMapIterator) it,
733 * Closure for process_reply() function.
735 struct ProcessReplyClosure
738 * The data for the reply.
743 * Who gave us this reply? NULL for local host (or DHT)
745 struct GSF_ConnectedPeer *sender;
748 * When the reply expires.
750 struct GNUNET_TIME_Absolute expiration;
760 enum GNUNET_BLOCK_Type type;
763 * Control flags for evaluation.
765 enum GNUNET_BLOCK_EvaluationOptions eo;
768 * How much was this reply worth to us?
773 * Anonymity requirements for this reply.
775 uint32_t anonymity_level;
778 * Evaluation result (returned).
780 enum GNUNET_BLOCK_EvaluationResult eval;
783 * Did we find a matching request?
790 * Update the performance data for the sender (if any) since
791 * the sender successfully answered one of our queries.
793 * @param prq information about the sender
794 * @param pr request that was satisfied
797 update_request_performance_data (struct ProcessReplyClosure *prq,
798 struct GSF_PendingRequest *pr)
800 if (prq->sender == NULL)
802 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
808 * We have received a reply; handle it!
810 * @param cls response (a `struct ProcessReplyClosure`)
811 * @param key our query
812 * @param value value in the hash map (info about the query)
813 * @return #GNUNET_YES (we should continue to iterate)
816 process_reply (void *cls,
817 const struct GNUNET_HashCode *key,
820 struct ProcessReplyClosure *prq = cls;
821 struct GSF_PendingRequest *pr = value;
822 struct GNUNET_HashCode chash;
823 struct GNUNET_TIME_Absolute last_transmission;
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Matched result (type %u) for query `%s' with pending request\n",
829 (unsigned int) prq->type,
831 GNUNET_STATISTICS_update (GSF_stats,
832 gettext_noop ("# replies received and matched"), 1,
835 GNUNET_BLOCK_evaluate (GSF_block_ctx,
847 case GNUNET_BLOCK_EVALUATION_OK_MORE:
848 update_request_performance_data (prq, pr);
850 case GNUNET_BLOCK_EVALUATION_OK_LAST:
851 /* short cut: stop processing early, no BF-update, etc. */
852 update_request_performance_data (prq, pr);
853 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
854 GNUNET_TIME_absolute_get_duration (pr->
855 public_data.start_time).rel_value_us);
857 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
860 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
861 /* pass on to other peers / local clients */
862 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
863 last_transmission, prq->type, prq->data, prq->size);
865 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
866 #if INSANE_STATISTICS
867 GNUNET_STATISTICS_update (GSF_stats,
869 ("# duplicate replies discarded (bloomfilter)"),
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Duplicate response, discarding.\n");
874 return GNUNET_YES; /* duplicate */
875 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
876 GNUNET_STATISTICS_update (GSF_stats,
878 ("# irrelevant replies discarded"),
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Irrelevant response, ignoring.\n");
883 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
884 return GNUNET_YES; /* wrong namespace */
885 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
888 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
891 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
893 _("Unsupported block type %u\n"),
897 /* update bloomfilter */
898 GNUNET_CRYPTO_hash (prq->data,
901 GSF_pending_request_update_ (pr,
904 if (NULL == prq->sender)
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Found result for query `%s' in local datastore\n",
909 GNUNET_STATISTICS_update (GSF_stats,
910 gettext_noop ("# results found locally"),
916 GSF_dht_lookup_ (pr);
918 prq->priority += pr->public_data.original_priority;
919 pr->public_data.priority = 0;
920 pr->public_data.original_priority = 0;
921 pr->public_data.results_found++;
922 prq->request_found = GNUNET_YES;
923 /* finally, pass on to other peer / local client */
924 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
927 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
931 prq->anonymity_level,
942 * Context for put_migration_continuation().
944 struct PutMigrationContext
948 * Start time for the operation.
950 struct GNUNET_TIME_Absolute start;
955 struct GNUNET_PeerIdentity origin;
958 * #GNUNET_YES if we had a matching request for this block,
966 * Continuation called to notify client about result of the
970 * @param success #GNUNET_SYSERR on failure
971 * @param min_expiration minimum expiration time required for content to be stored
972 * @param msg NULL on success, otherwise an error message
975 put_migration_continuation (void *cls, int success,
976 struct GNUNET_TIME_Absolute min_expiration,
979 struct PutMigrationContext *pmc = cls;
980 struct GSF_ConnectedPeer *cp;
981 struct GNUNET_TIME_Relative mig_pause;
982 struct GSF_PeerPerformanceData *ppd;
984 if (NULL != datastore_put_load)
986 if (GNUNET_SYSERR != success)
988 GNUNET_LOAD_update (datastore_put_load,
989 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
993 /* on queue failure / timeout, increase the put load dramatically */
994 GNUNET_LOAD_update (datastore_put_load,
995 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
998 cp = GSF_peer_get_ (&pmc->origin);
999 if (GNUNET_OK == success)
1003 ppd = GSF_get_peer_performance_data_ (cp);
1004 ppd->migration_delay.rel_value_us /= 2;
1009 if ( (GNUNET_NO == success) &&
1010 (GNUNET_NO == pmc->requested) &&
1013 ppd = GSF_get_peer_performance_data_ (cp);
1014 if (min_expiration.abs_value_us > 0)
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "Asking to stop migration for %s because datastore is full\n",
1018 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1019 GSF_block_peer_migration_ (cp, min_expiration);
1023 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1024 ppd->migration_delay);
1025 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1026 ppd->migration_delay);
1027 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1028 ppd->migration_delay.rel_value_us);
1029 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Replicated content already exists locally, asking to stop migration for %s\n",
1032 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1034 GSF_block_peer_migration_ (cp,
1035 GNUNET_TIME_relative_to_absolute (mig_pause));
1039 GNUNET_STATISTICS_update (GSF_stats,
1040 gettext_noop ("# Datastore `PUT' failures"), 1,
1046 * Test if the DATABASE (PUT) load on this peer is too high
1047 * to even consider processing the query at
1050 * @param priority the priority of the item
1051 * @return #GNUNET_YES if the load is too high to do anything (load high)
1052 * #GNUNET_NO to process normally (load normal or low)
1055 test_put_load_too_high (uint32_t priority)
1059 if (NULL == datastore_put_load)
1061 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1062 return GNUNET_NO; /* very fast */
1063 ld = GNUNET_LOAD_get_load (datastore_put_load);
1064 if (ld < 2.0 * (1 + priority))
1066 GNUNET_STATISTICS_update (GSF_stats,
1068 ("# storage requests dropped due to high load"), 1,
1075 * Iterator called on each result obtained for a DHT
1076 * operation that expects a reply
1078 * @param cls closure
1079 * @param exp when will this value expire
1080 * @param key key of the result
1081 * @param get_path peers on reply path (or NULL if not recorded)
1082 * @param get_path_length number of entries in @a get_path
1083 * @param put_path peers on the PUT path (or NULL if not recorded)
1084 * @param put_path_length number of entries in @a get_path
1085 * @param type type of the result
1086 * @param size number of bytes in @a data
1087 * @param data pointer to the result data
1090 handle_dht_reply (void *cls,
1091 struct GNUNET_TIME_Absolute exp,
1092 const struct GNUNET_HashCode *key,
1093 const struct GNUNET_PeerIdentity *get_path,
1094 unsigned int get_path_length,
1095 const struct GNUNET_PeerIdentity *put_path,
1096 unsigned int put_path_length,
1097 enum GNUNET_BLOCK_Type type,
1101 struct GSF_PendingRequest *pr = cls;
1102 struct ProcessReplyClosure prq;
1103 struct PutMigrationContext *pmc;
1105 GNUNET_STATISTICS_update (GSF_stats,
1106 gettext_noop ("# Replies received from DHT"), 1,
1108 memset (&prq, 0, sizeof (prq));
1110 prq.expiration = exp;
1111 /* do not allow migrated content to live longer than 1 year */
1112 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1116 prq.eo = GNUNET_BLOCK_EO_NONE;
1117 process_reply (&prq, key, pr);
1118 if ((GNUNET_YES == active_to_migration) &&
1119 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Replicating result for query `%s' with priority %u\n",
1123 GNUNET_h2s (key), prq.priority);
1124 pmc = GNUNET_new (struct PutMigrationContext);
1125 pmc->start = GNUNET_TIME_absolute_get ();
1126 pmc->requested = GNUNET_YES;
1128 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1130 0 /* replication */ ,
1131 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1132 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1133 &put_migration_continuation, pmc))
1135 put_migration_continuation (pmc,
1137 GNUNET_TIME_UNIT_ZERO_ABS,
1145 * Consider looking up the data in the DHT (anonymity-level permitting).
1147 * @param pr the pending request to process
1150 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1154 struct GNUNET_PeerIdentity pi;
1155 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1157 if (0 != pr->public_data.anonymity_level)
1161 GNUNET_DHT_get_stop (pr->gh);
1166 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1168 GNUNET_assert (0 != pr->sender_pid);
1169 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1170 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1171 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1174 GNUNET_DHT_get_start (GSF_dht,
1175 pr->public_data.type, &pr->public_data.query,
1176 DHT_GET_REPLICATION,
1177 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1178 xquery, xquery_size, &handle_dht_reply, pr);
1179 if ( (NULL != pr->gh) &&
1180 (0 != pr->replies_seen_count) )
1181 GNUNET_DHT_get_filter_known_results (pr->gh,
1182 pr->replies_seen_count,
1188 * Function called with a reply from the cadet.
1190 * @param cls the pending request struct
1191 * @param type type of the block, ANY on error
1192 * @param expiration expiration time for the block
1193 * @param data_size number of bytes in @a data, 0 on error
1194 * @param data reply block data, NULL on error
1197 cadet_reply_proc (void *cls,
1198 enum GNUNET_BLOCK_Type type,
1199 struct GNUNET_TIME_Absolute expiration,
1203 struct GSF_PendingRequest *pr = cls;
1204 struct ProcessReplyClosure prq;
1205 struct GNUNET_HashCode query;
1207 pr->cadet_request = NULL;
1208 if (GNUNET_BLOCK_TYPE_ANY == type)
1210 GNUNET_break (NULL == data);
1211 GNUNET_break (0 == data_size);
1212 pr->cadet_retry_count++;
1213 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1214 return; /* give up on cadet */
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Error retrieiving block via cadet\n");
1217 /* retry -- without delay, as this is non-anonymous
1218 and cadet/cadet connect will take some time anyway */
1219 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1220 &pr->public_data.query,
1221 pr->public_data.type,
1227 GNUNET_BLOCK_get_key (GSF_block_ctx,
1229 data, data_size, &query))
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Failed to derive key for block of type %d\n",
1234 GNUNET_break_op (0);
1237 GNUNET_STATISTICS_update (GSF_stats,
1238 gettext_noop ("# Replies received from CADET"), 1,
1240 memset (&prq, 0, sizeof (prq));
1242 prq.expiration = expiration;
1243 /* do not allow migrated content to live longer than 1 year */
1244 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1246 prq.size = data_size;
1248 prq.eo = GNUNET_BLOCK_EO_NONE;
1249 process_reply (&prq, &query, pr);
1254 * Consider downloading via cadet (if possible)
1256 * @param pr the pending request to process
1259 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1261 if (0 != pr->public_data.anonymity_level)
1263 if (0 == pr->public_data.target)
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Cannot do cadet-based download, target peer not known\n");
1269 if (NULL != pr->cadet_request)
1271 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1272 &pr->public_data.query,
1273 pr->public_data.type,
1280 * Task that issues a warning if the datastore lookup takes too long.
1282 * @param cls the `struct GSF_PendingRequest`
1283 * @param tc task context
1286 warn_delay_task (void *cls,
1287 const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 struct GSF_PendingRequest *pr = cls;
1291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1292 _("Datastore lookup already took %s!\n"),
1293 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1296 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1303 * Task that issues a warning if the datastore lookup takes too long.
1305 * @param cls the `struct GSF_PendingRequest`
1306 * @param tc task context
1309 odc_warn_delay_task (void *cls,
1310 const struct GNUNET_SCHEDULER_TaskContext *tc)
1312 struct GSF_PendingRequest *pr = cls;
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1315 _("On-demand lookup already took %s!\n"),
1316 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1318 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1319 &odc_warn_delay_task, pr);
1324 * We're processing (local) results for a search request
1325 * from another peer. Pass applicable results to the
1326 * peer and if we are done either clean up (operation
1327 * complete) or forward to other peers (more results possible).
1329 * @param cls our closure (`struct GSF_PendingRequest *`)
1330 * @param key key for the content
1331 * @param size number of bytes in @a data
1332 * @param data content stored
1333 * @param type type of the content
1334 * @param priority priority of the content
1335 * @param anonymity anonymity-level for the content
1336 * @param expiration expiration time for the content
1337 * @param uid unique identifier for the datum;
1338 * maybe 0 if no unique identifier is available
1341 process_local_reply (void *cls,
1342 const struct GNUNET_HashCode *key,
1345 enum GNUNET_BLOCK_Type type,
1348 struct GNUNET_TIME_Absolute expiration,
1351 struct GSF_PendingRequest *pr = cls;
1352 GSF_LocalLookupContinuation cont;
1353 struct ProcessReplyClosure prq;
1354 struct GNUNET_HashCode query;
1355 unsigned int old_rf;
1357 GNUNET_SCHEDULER_cancel (pr->warn_task);
1358 pr->warn_task = NULL;
1364 #if INSANE_STATISTICS
1365 GNUNET_STATISTICS_update (GSF_stats,
1367 ("# Datastore lookups concluded (no results)"),
1371 if (GNUNET_NO == pr->have_first_uid)
1373 pr->first_uid = uid;
1374 pr->have_first_uid = 1;
1378 if ((uid == pr->first_uid) && (key != NULL))
1380 GNUNET_STATISTICS_update (GSF_stats,
1382 ("# Datastore lookups concluded (seen all)"),
1384 key = NULL; /* all replies seen! */
1386 pr->have_first_uid++;
1387 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1389 GNUNET_STATISTICS_update (GSF_stats,
1391 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1393 key = NULL; /* all replies seen! */
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1400 "No further local responses available.\n");
1401 #if INSANE_STATISTICS
1402 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1403 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1404 GNUNET_STATISTICS_update (GSF_stats,
1406 ("# requested DBLOCK or IBLOCK not found"), 1,
1409 goto check_error_and_continue;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1413 GNUNET_h2s (key), type, (unsigned long long) uid);
1414 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Found ONDEMAND block, performing on-demand encoding\n");
1418 GNUNET_STATISTICS_update (GSF_stats,
1420 ("# on-demand blocks matched requests"), 1,
1422 pr->qe_start = GNUNET_TIME_absolute_get ();
1424 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1425 &odc_warn_delay_task, pr);
1427 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1428 anonymity, expiration, uid,
1429 &process_local_reply, pr))
1431 GNUNET_STATISTICS_update (GSF_stats,
1433 ("# on-demand lookups performed successfully"),
1435 return; /* we're done */
1437 GNUNET_STATISTICS_update (GSF_stats,
1438 gettext_noop ("# on-demand lookups failed"), 1,
1440 GNUNET_SCHEDULER_cancel (pr->warn_task);
1442 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1443 &warn_delay_task, pr);
1445 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1446 &pr->public_data.query,
1447 pr->public_data.type ==
1448 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1449 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1451 (GSF_PRO_PRIORITY_UNLIMITED &
1452 pr->public_data.options)) ? UINT_MAX : 1
1453 /* queue priority */ ,
1455 (GSF_PRO_PRIORITY_UNLIMITED &
1456 pr->public_data.options)) ? UINT_MAX :
1457 GSF_datastore_queue_size
1458 /* max queue size */ ,
1459 GNUNET_TIME_UNIT_FOREVER_REL,
1460 &process_local_reply, pr);
1462 return; /* we're done */
1463 GNUNET_STATISTICS_update (GSF_stats,
1465 ("# Datastore lookups concluded (error queueing)"),
1467 goto check_error_and_continue;
1469 old_rf = pr->public_data.results_found;
1470 memset (&prq, 0, sizeof (prq));
1472 prq.expiration = expiration;
1475 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1478 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1479 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1480 pr->qe_start = GNUNET_TIME_absolute_get ();
1482 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1483 &warn_delay_task, pr);
1485 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1486 &pr->public_data.query,
1487 pr->public_data.type ==
1488 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1489 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1491 (GSF_PRO_PRIORITY_UNLIMITED &
1492 pr->public_data.options)) ? UINT_MAX : 1
1493 /* queue priority */ ,
1495 (GSF_PRO_PRIORITY_UNLIMITED &
1496 pr->public_data.options)) ? UINT_MAX :
1497 GSF_datastore_queue_size
1498 /* max queue size */ ,
1499 GNUNET_TIME_UNIT_FOREVER_REL,
1500 &process_local_reply, pr);
1503 GNUNET_STATISTICS_update (GSF_stats,
1505 ("# Datastore lookups concluded (error queueing)"),
1507 goto check_error_and_continue;
1512 prq.priority = priority;
1513 prq.request_found = GNUNET_NO;
1514 prq.anonymity_level = anonymity;
1515 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1516 GSF_update_datastore_delay_ (pr->public_data.start_time);
1517 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1518 process_reply (&prq, key, pr);
1519 pr->local_result = prq.eval;
1520 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1522 GNUNET_STATISTICS_update (GSF_stats,
1524 ("# Datastore lookups concluded (found last result)"),
1527 goto check_error_and_continue;
1529 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1530 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1531 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Load too high, done with request\n");
1535 GNUNET_STATISTICS_update (GSF_stats,
1536 gettext_noop ("# Datastore lookups concluded (load too high)"),
1539 goto check_error_and_continue;
1541 pr->qe_start = GNUNET_TIME_absolute_get ();
1543 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1547 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1548 &pr->public_data.query,
1549 pr->public_data.type ==
1550 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1551 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1553 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1554 public_data.options)) ? UINT_MAX : 1
1555 /* queue priority */ ,
1557 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1558 public_data.options)) ? UINT_MAX :
1559 GSF_datastore_queue_size
1560 /* max queue size */ ,
1561 GNUNET_TIME_UNIT_FOREVER_REL,
1562 &process_local_reply, pr);
1563 /* check if we successfully queued another datastore request;
1564 * if so, return, otherwise call our continuation (if we have
1566 check_error_and_continue:
1569 if (NULL != pr->warn_task)
1571 GNUNET_SCHEDULER_cancel (pr->warn_task);
1572 pr->warn_task = NULL;
1574 if (NULL == (cont = pr->llc_cont))
1575 return; /* no continuation */
1576 pr->llc_cont = NULL;
1577 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1579 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1581 /* Signal that we are done and that there won't be any
1582 additional results to allow client to clean up state. */
1584 GNUNET_BLOCK_EVALUATION_OK_LAST,
1587 GNUNET_TIME_UNIT_ZERO_ABS,
1588 GNUNET_TIME_UNIT_FOREVER_ABS,
1589 GNUNET_BLOCK_TYPE_ANY,
1592 /* Finally, call our continuation to signal that we are
1593 done with local processing of this request; i.e. to
1594 start reading again from the client. */
1595 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1599 cont (pr->llc_cont_cls, pr, pr->local_result);
1604 * Is the given target a legitimate peer for forwarding the given request?
1608 * @return #GNUNET_YES if this request could be forwarded to the given peer
1611 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1612 const struct GNUNET_PeerIdentity *target)
1614 struct GNUNET_PeerIdentity pi;
1616 if (0 == pr->origin_pid)
1618 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1620 memcmp (&pi, target,
1621 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1627 * Look up the request in the local datastore.
1629 * @param pr the pending request to process
1630 * @param cont function to call at the end
1631 * @param cont_cls closure for @a cont
1634 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1635 GSF_LocalLookupContinuation cont,
1638 GNUNET_assert (NULL == pr->gh);
1639 GNUNET_assert (NULL == pr->cadet_request);
1640 GNUNET_assert (NULL == pr->llc_cont);
1641 pr->llc_cont = cont;
1642 pr->llc_cont_cls = cont_cls;
1643 pr->qe_start = GNUNET_TIME_absolute_get ();
1645 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1648 #if INSANE_STATISTICS
1649 GNUNET_STATISTICS_update (GSF_stats,
1650 gettext_noop ("# Datastore lookups initiated"), 1,
1654 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1655 &pr->public_data.query,
1656 pr->public_data.type ==
1657 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1658 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1660 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1661 public_data.options)) ? UINT_MAX : 1
1662 /* queue priority */ ,
1664 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1665 public_data.options)) ? UINT_MAX :
1666 GSF_datastore_queue_size
1667 /* max queue size */ ,
1668 GNUNET_TIME_UNIT_FOREVER_REL,
1669 &process_local_reply, pr);
1672 GNUNET_STATISTICS_update (GSF_stats,
1674 ("# Datastore lookups concluded (error queueing)"),
1676 GNUNET_SCHEDULER_cancel (pr->warn_task);
1677 pr->warn_task = NULL;
1678 pr->llc_cont = NULL;
1680 cont (cont_cls, pr, pr->local_result);
1686 * Handle P2P "CONTENT" message. Checks that the message is
1687 * well-formed and then checks if there are any pending requests for
1688 * this content and possibly passes it on (to local clients or other
1689 * peers). Does NOT perform migration (content caching at this peer).
1691 * @param cp the other peer involved (sender or receiver, NULL
1692 * for loopback messages where we are both sender and receiver)
1693 * @param message the actual message
1694 * @return #GNUNET_OK if the message was well-formed,
1695 * #GNUNET_SYSERR if the message was malformed (close connection,
1696 * do not cache under any circumstances)
1699 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1700 const struct GNUNET_MessageHeader *message)
1702 const struct PutMessage *put;
1705 enum GNUNET_BLOCK_Type type;
1706 struct GNUNET_TIME_Absolute expiration;
1707 struct GNUNET_HashCode query;
1708 struct ProcessReplyClosure prq;
1709 struct GNUNET_TIME_Relative block_time;
1711 struct PutMigrationContext *pmc;
1713 msize = ntohs (message->size);
1714 if (msize < sizeof (struct PutMessage))
1716 GNUNET_break_op (0);
1717 return GNUNET_SYSERR;
1719 put = (const struct PutMessage *) message;
1720 dsize = msize - sizeof (struct PutMessage);
1721 type = ntohl (put->type);
1722 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1723 /* do not allow migrated content to live longer than 1 year */
1724 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1726 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1727 return GNUNET_SYSERR;
1729 GNUNET_BLOCK_get_key (GSF_block_ctx,
1735 GNUNET_break_op (0);
1736 return GNUNET_SYSERR;
1738 GNUNET_STATISTICS_update (GSF_stats,
1739 gettext_noop ("# GAP PUT messages received"),
1742 /* now, lookup 'query' */
1743 prq.data = (const void *) &put[1];
1747 prq.expiration = expiration;
1749 prq.anonymity_level = UINT32_MAX;
1750 prq.request_found = GNUNET_NO;
1751 prq.eo = GNUNET_BLOCK_EO_NONE;
1752 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1758 GSF_connected_peer_change_preference_ (cp,
1759 CONTENT_BANDWIDTH_VALUE +
1760 1000 * prq.priority);
1761 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1763 if ((GNUNET_YES == active_to_migration) &&
1765 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Replicating result for query `%s' with priority %u\n",
1769 GNUNET_h2s (&query),
1771 pmc = GNUNET_new (struct PutMigrationContext);
1772 pmc->start = GNUNET_TIME_absolute_get ();
1773 pmc->requested = prq.request_found;
1774 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1775 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1778 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1779 prq.priority, 1 /* anonymity */ ,
1780 0 /* replication */ ,
1781 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1782 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1783 &put_migration_continuation, pmc))
1785 put_migration_continuation (pmc,
1787 GNUNET_TIME_UNIT_ZERO_ABS,
1791 else if (NULL != cp)
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Choosing not to keep content `%s' (%d/%d)\n",
1795 GNUNET_h2s (&query), active_to_migration,
1796 test_put_load_too_high (prq.priority));
1798 putl = GNUNET_LOAD_get_load (datastore_put_load);
1799 if ( (NULL != cp) &&
1800 (GNUNET_NO == prq.request_found) &&
1801 ( (GNUNET_YES != active_to_migration) ||
1802 (putl > 2.5 * (1 + prq.priority)) ) )
1804 if (GNUNET_YES != active_to_migration)
1805 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1807 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1809 GNUNET_CRYPTO_random_u32
1810 (GNUNET_CRYPTO_QUALITY_WEAK,
1811 (unsigned int) (60000 * putl * putl)));
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1814 GNUNET_STRINGS_relative_time_to_string (block_time,
1817 active_to_migration,
1818 (GNUNET_NO == prq.request_found));
1819 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1826 * Setup the subsystem.
1829 GSF_pending_request_init_ ()
1832 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1833 "MAX_PENDING_REQUESTS",
1834 &max_pending_requests))
1836 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1837 "fs", "MAX_PENDING_REQUESTS");
1839 active_to_migration =
1840 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1841 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1842 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1843 requests_by_expiration_heap =
1844 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1849 * Shutdown the subsystem.
1852 GSF_pending_request_done_ ()
1854 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1855 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1857 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1858 requests_by_expiration_heap = NULL;
1859 GNUNET_LOAD_value_free (datastore_put_load);
1860 datastore_put_load = NULL;
1864 /* end of gnunet-service-fs_pr.c */