2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011, 2012 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_load_lib.h"
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_indexing.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
35 * Maximum size of the datastore queue for P2P operations. Needs to
36 * be large enough to queue MAX_QUEUE_PER_PEER operations for roughly
37 * the number of active (connected) peers.
39 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
42 * Bandwidth value of a 0-priority content (must be fairly high
43 * compared to query since content is typically significantly larger
44 * -- and more valueable since it can take many queries to get one
47 #define CONTENT_BANDWIDTH_VALUE 800
50 * Hard limit on the number of results we may get from the datastore per query.
52 #define MAX_RESULTS (100 * 1024)
55 * Collect an instane number of statistics? May cause excessive IPC.
57 #define INSANE_STATISTICS GNUNET_NO
62 struct GSF_PendingRequest
65 * Public data for the request.
67 struct GSF_PendingRequestData public_data;
70 * Function to call if we encounter a reply.
72 GSF_PendingRequestReplyHandler rh;
80 * Array of hash codes of replies we've already seen.
82 struct GNUNET_HashCode *replies_seen;
85 * Bloomfilter masking replies we've already seen.
87 struct GNUNET_CONTAINER_BloomFilter *bf;
90 * Entry for this pending request in the expiration heap, or NULL.
92 struct GNUNET_CONTAINER_HeapNode *hnode;
95 * Datastore queue entry for this request (or NULL for none).
97 struct GNUNET_DATASTORE_QueueEntry *qe;
100 * DHT request handle for this request (or NULL for none).
102 struct GNUNET_DHT_GetHandle *gh;
105 * Function to call upon completion of the local get
106 * request, or NULL for none.
108 GSF_LocalLookupContinuation llc_cont;
111 * Closure for llc_cont.
116 * Last result from the local datastore lookup evaluation.
118 enum GNUNET_BLOCK_EvaluationResult local_result;
121 * Identity of the peer that we should use for the 'sender'
122 * (recipient of the response) when forwarding (0 for none).
124 GNUNET_PEER_Id sender_pid;
127 * Identity of the peer that we should never forward this query
128 * to since it originated this query (0 for none).
130 GNUNET_PEER_Id origin_pid;
133 * Time we started the last datastore lookup.
135 struct GNUNET_TIME_Absolute qe_start;
138 * Task that warns us if the local datastore lookup takes too long.
140 GNUNET_SCHEDULER_TaskIdentifier warn_task;
143 * Current offset for querying our local datastore for results.
144 * Starts at a random value, incremented until we get the same
145 * UID again (detected using 'first_uid'), which is then used
146 * to termiante the iteration.
148 uint64_t local_result_offset;
151 * Unique ID of the first result from the local datastore;
152 * used to detect wrap-around of the offset.
157 * Number of valid entries in the 'replies_seen' array.
159 unsigned int replies_seen_count;
162 * Length of the 'replies_seen' array.
164 unsigned int replies_seen_size;
167 * Mingle value we currently use for the bf.
172 * Do we have a first UID yet?
174 unsigned int have_first_uid;
180 * All pending requests, ordered by the query. Entries
181 * are of type 'struct GSF_PendingRequest*'.
183 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
187 * Datastore 'PUT' load tracking.
189 static struct GNUNET_LOAD_Value *datastore_put_load;
193 * Are we allowed to migrate content to this peer.
195 static int active_to_migration;
199 * Size of the datastore queue we assume for common requests.
200 * Determined based on the network quota.
202 static unsigned int datastore_queue_size;
205 * Heap with the request that will expire next at the top. Contains
206 * pointers of type "struct PendingRequest*"; these will *also* be
207 * aliased from the "requests_by_peer" data structures and the
208 * "requests_by_query" table. Note that requests from our clients
209 * don't expire and are thus NOT in the "requests_by_expiration"
210 * (or the "requests_by_peer" tables).
212 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
216 * Maximum number of requests (from other peers, overall) that we're
217 * willing to have pending at any given point in time. Can be changed
218 * via the configuration file (32k is just the default).
220 static unsigned long long max_pending_requests = (32 * 1024);
225 * Recalculate our bloom filter for filtering replies. This function
226 * will create a new bloom filter from scratch, so it should only be
227 * called if we have no bloomfilter at all (and hence can create a
228 * fresh one of minimal size without problems) OR if our peer is the
229 * initiator (in which case we may resize to larger than mimimum size).
231 * @param pr request for which the BF is to be recomputed
234 refresh_bloomfilter (struct GSF_PendingRequest *pr)
237 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
239 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
241 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
242 pr->replies_seen_count);
247 * Create a new pending request.
249 * @param options request options
250 * @param type type of the block that is being requested
251 * @param query key for the lookup
252 * @param namespace namespace to lookup, NULL for no namespace
253 * @param target preferred target for the request, NULL for none
254 * @param bf_data raw data for bloom filter for known replies, can be NULL
255 * @param bf_size number of bytes in bf_data
256 * @param mingle mingle value for bf
257 * @param anonymity_level desired anonymity level
258 * @param priority maximum outgoing cummulative request priority to use
259 * @param ttl current time-to-live for the request
260 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
261 * @param origin_pid peer ID of origin of query (do not loop back)
262 * @param replies_seen hash codes of known local replies
263 * @param replies_seen_count size of the 'replies_seen' array
264 * @param rh handle to call when we get a reply
265 * @param rh_cls closure for rh
266 * @return handle for the new pending request
268 struct GSF_PendingRequest *
269 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
270 enum GNUNET_BLOCK_Type type,
271 const struct GNUNET_HashCode * query,
272 const struct GNUNET_HashCode * namespace,
273 const struct GNUNET_PeerIdentity *target,
274 const char *bf_data, size_t bf_size,
275 uint32_t mingle, uint32_t anonymity_level,
276 uint32_t priority, int32_t ttl,
277 GNUNET_PEER_Id sender_pid,
278 GNUNET_PEER_Id origin_pid,
279 const struct GNUNET_HashCode * replies_seen,
280 unsigned int replies_seen_count,
281 GSF_PendingRequestReplyHandler rh, void *rh_cls)
283 struct GSF_PendingRequest *pr;
284 struct GSF_PendingRequest *dpr;
286 struct GNUNET_HashCode *eptr;
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Creating request handle for `%s' of type %d\n",
290 GNUNET_h2s (query), type);
291 #if INSANE_STATISTICS
292 GNUNET_STATISTICS_update (GSF_stats,
293 gettext_noop ("# Pending requests created"), 1,
297 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
298 extra += sizeof (struct GNUNET_HashCode);
300 extra += sizeof (struct GNUNET_PeerIdentity);
301 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
302 pr->local_result_offset =
303 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
304 pr->public_data.query = *query;
305 eptr = (struct GNUNET_HashCode *) &pr[1];
306 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
308 GNUNET_assert (NULL != namespace);
309 pr->public_data.namespace = eptr;
310 memcpy (eptr, namespace, sizeof (struct GNUNET_HashCode));
315 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
316 memcpy (eptr, target, sizeof (struct GNUNET_PeerIdentity));
318 pr->public_data.anonymity_level = anonymity_level;
319 pr->public_data.priority = priority;
320 pr->public_data.original_priority = priority;
321 pr->public_data.options = options;
322 pr->public_data.type = type;
323 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
324 pr->sender_pid = sender_pid;
325 pr->origin_pid = origin_pid;
328 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
330 pr->public_data.ttl =
331 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
332 (GNUNET_TIME_UNIT_SECONDS,
335 pr->public_data.ttl =
336 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
337 GNUNET_TIME_relative_multiply
338 (GNUNET_TIME_UNIT_SECONDS,
340 if (replies_seen_count > 0)
342 pr->replies_seen_size = replies_seen_count;
344 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
345 memcpy (pr->replies_seen, replies_seen,
346 replies_seen_count * sizeof (struct GNUNET_HashCode));
347 pr->replies_seen_count = replies_seen_count;
352 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size,
353 GNUNET_CONSTANTS_BLOOMFILTER_K);
356 else if ((replies_seen_count > 0) &&
357 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
359 refresh_bloomfilter (pr);
361 GNUNET_CONTAINER_multihashmap_put (pr_map,
362 &pr->public_data.query, pr,
363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
364 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
367 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
368 pr->public_data.ttl.abs_value);
369 /* make sure we don't track too many requests */
370 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
371 max_pending_requests)
373 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
374 GNUNET_assert (dpr != NULL);
376 break; /* let the request live briefly... */
378 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
379 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_ABS,
380 GNUNET_BLOCK_TYPE_ANY, NULL, 0);
381 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
384 GNUNET_STATISTICS_update (GSF_stats,
385 gettext_noop ("# Pending requests active"), 1,
391 * Obtain the public data associated with a pending request
393 * @param pr pending request
394 * @return associated public data
396 struct GSF_PendingRequestData *
397 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
399 return &pr->public_data;
404 * Test if two pending requests are compatible (would generate
405 * the same query modulo filters and should thus be processed
408 * @param pra a pending request
409 * @param prb another pending request
410 * @return GNUNET_OK if the requests are compatible
413 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
414 struct GSF_PendingRequest *prb)
416 if ((pra->public_data.type != prb->public_data.type) ||
418 memcmp (&pra->public_data.query, &prb->public_data.query,
419 sizeof (struct GNUNET_HashCode))) ||
420 ((pra->public_data.type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
422 memcmp (pra->public_data.namespace,
423 prb->public_data.namespace,
424 sizeof (struct GNUNET_HashCode)))))
432 * Update a given pending request with additional replies
433 * that have been seen.
435 * @param pr request to update
436 * @param replies_seen hash codes of replies that we've seen
437 * @param replies_seen_count size of the replies_seen array
440 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
441 const struct GNUNET_HashCode * replies_seen,
442 unsigned int replies_seen_count)
445 struct GNUNET_HashCode mhash;
447 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
448 return; /* integer overflow */
449 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
451 /* we're responsible for the BF, full refresh */
452 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
453 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
454 replies_seen_count + pr->replies_seen_count);
455 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
456 sizeof (struct GNUNET_HashCode) * replies_seen_count);
457 pr->replies_seen_count += replies_seen_count;
458 refresh_bloomfilter (pr);
464 /* we're not the initiator, but the initiator did not give us
465 * any bloom-filter, so we need to create one on-the-fly */
467 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
469 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, replies_seen,
474 for (i = 0; i < pr->replies_seen_count; i++)
476 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
477 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
482 GNUNET_DHT_get_filter_known_results (pr->gh,
489 * Generate the message corresponding to the given pending request for
490 * transmission to other peers (or at least determine its size).
492 * @param pr request to generate the message for
493 * @param buf_size number of bytes available in buf
494 * @param buf where to copy the message (can be NULL)
495 * @return number of bytes needed (if > buf_size) or used
498 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
499 size_t buf_size, void *buf)
501 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
502 struct GetMessage *gm;
503 struct GNUNET_HashCode *ext;
509 struct GNUNET_TIME_Absolute now;
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Building request message for `%s' of type %d\n",
516 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
519 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
520 if ((!do_route) && (pr->sender_pid == 0))
523 do_route = GNUNET_YES;
527 bm |= GET_MESSAGE_BIT_RETURN_TO;
530 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
532 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
535 if (NULL != pr->public_data.target)
537 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
540 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
541 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_HashCode);
542 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
543 if (buf_size < msize)
545 gm = (struct GetMessage *) lbuf;
546 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
547 gm->header.size = htons (msize);
548 gm->type = htonl (pr->public_data.type);
551 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
552 pr->public_data.priority + 1);
555 pr->public_data.priority -= prio;
556 pr->public_data.num_transmissions++;
557 pr->public_data.respect_offered += prio;
558 gm->priority = htonl (prio);
559 now = GNUNET_TIME_absolute_get ();
560 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
561 gm->ttl = htonl (ttl / 1000);
562 gm->filter_mutator = htonl (pr->mingle);
563 gm->hash_bitmap = htonl (bm);
564 gm->query = pr->public_data.query;
565 ext = (struct GNUNET_HashCode *) & gm[1];
568 GNUNET_PEER_resolve (pr->sender_pid,
569 (struct GNUNET_PeerIdentity *) &ext[k++]);
570 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
571 memcpy (&ext[k++], pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
572 if (NULL != pr->public_data.target)
574 pr->public_data.target,
575 sizeof (struct GNUNET_PeerIdentity));
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, const struct GNUNET_HashCode * key, void *value)
597 struct GSF_PendingRequest *pr = value;
598 GSF_LocalLookupContinuation cont;
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
602 if (NULL != (cont = pr->llc_cont))
605 cont (pr->llc_cont_cls, pr, pr->local_result);
607 GSF_plan_notify_request_done_ (pr);
608 GNUNET_free_non_null (pr->replies_seen);
611 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
614 GNUNET_PEER_change_rc (pr->sender_pid, -1);
616 GNUNET_PEER_change_rc (pr->origin_pid, -1);
618 if (NULL != pr->hnode)
620 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
625 GNUNET_DATASTORE_cancel (pr->qe);
630 GNUNET_DHT_get_stop (pr->gh);
633 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
635 GNUNET_SCHEDULER_cancel (pr->warn_task);
636 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
638 GNUNET_assert (GNUNET_OK ==
639 GNUNET_CONTAINER_multihashmap_remove (pr_map,
640 &pr->public_data.query,
642 GNUNET_STATISTICS_update (GSF_stats,
643 gettext_noop ("# Pending requests active"), -1,
651 * Explicitly cancel a pending request.
653 * @param pr request to cancel
654 * @param full_cleanup fully purge the request
657 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
659 GSF_LocalLookupContinuation cont;
662 return; /* already cleaned up! */
663 if (GNUNET_YES != full_cleanup)
665 /* make request inactive (we're no longer interested in more results),
666 * but do NOT remove from our data-structures, we still need it there
667 * to prevent the request from looping */
669 if (NULL != (cont = pr->llc_cont))
672 cont (pr->llc_cont_cls, pr, pr->local_result);
674 GSF_plan_notify_request_done_ (pr);
677 GNUNET_DATASTORE_cancel (pr->qe);
682 GNUNET_DHT_get_stop (pr->gh);
685 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
687 GNUNET_SCHEDULER_cancel (pr->warn_task);
688 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
692 GNUNET_assert (GNUNET_YES ==
693 clean_request (NULL, &pr->public_data.query, pr));
698 * Iterate over all pending requests.
700 * @param it function to call for each request
701 * @param cls closure for it
704 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
706 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
707 (GNUNET_CONTAINER_HashMapIterator) it,
715 * Closure for "process_reply" function.
717 struct ProcessReplyClosure
720 * The data for the reply.
725 * Who gave us this reply? NULL for local host (or DHT)
727 struct GSF_ConnectedPeer *sender;
730 * When the reply expires.
732 struct GNUNET_TIME_Absolute expiration;
742 enum GNUNET_BLOCK_Type type;
745 * How much was this reply worth to us?
750 * Anonymity requirements for this reply.
752 uint32_t anonymity_level;
755 * Evaluation result (returned).
757 enum GNUNET_BLOCK_EvaluationResult eval;
760 * Did we find a matching request?
767 * Update the performance data for the sender (if any) since
768 * the sender successfully answered one of our queries.
770 * @param prq information about the sender
771 * @param pr request that was satisfied
774 update_request_performance_data (struct ProcessReplyClosure *prq,
775 struct GSF_PendingRequest *pr)
777 if (prq->sender == NULL)
779 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
785 * We have received a reply; handle it!
787 * @param cls response (struct ProcessReplyClosure)
788 * @param key our query
789 * @param value value in the hash map (info about the query)
790 * @return GNUNET_YES (we should continue to iterate)
793 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
795 struct ProcessReplyClosure *prq = cls;
796 struct GSF_PendingRequest *pr = value;
797 struct GNUNET_HashCode chash;
798 struct GNUNET_TIME_Absolute last_transmission;
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Matched result (type %u) for query `%s' with pending request\n",
804 (unsigned int) prq->type, GNUNET_h2s (key));
805 GNUNET_STATISTICS_update (GSF_stats,
806 gettext_noop ("# replies received and matched"), 1,
809 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
810 pr->public_data.namespace,
812 GNUNET_BLOCK_TYPE_FS_SBLOCK) ?
813 sizeof (struct GNUNET_HashCode) : 0, prq->data,
817 case GNUNET_BLOCK_EVALUATION_OK_MORE:
818 update_request_performance_data (prq, pr);
820 case GNUNET_BLOCK_EVALUATION_OK_LAST:
821 /* short cut: stop processing early, no BF-update, etc. */
822 update_request_performance_data (prq, pr);
823 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
824 GNUNET_TIME_absolute_get_duration (pr->
825 public_data.start_time).rel_value);
827 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
830 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
831 /* pass on to other peers / local clients */
832 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
833 last_transmission, prq->type, prq->data, prq->size);
835 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
836 #if INSANE_STATISTICS
837 GNUNET_STATISTICS_update (GSF_stats,
839 ("# duplicate replies discarded (bloomfilter)"),
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Duplicate response, discarding.\n");
844 return GNUNET_YES; /* duplicate */
845 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
846 return GNUNET_YES; /* wrong namespace */
847 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
850 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
853 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
858 /* update bloomfilter */
859 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
860 GSF_pending_request_update_ (pr, &chash, 1);
861 if (NULL == prq->sender)
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Found result for query `%s' in local datastore\n",
866 GNUNET_STATISTICS_update (GSF_stats,
867 gettext_noop ("# results found locally"), 1,
872 GSF_dht_lookup_ (pr);
874 prq->priority += pr->public_data.original_priority;
875 pr->public_data.priority = 0;
876 pr->public_data.original_priority = 0;
877 pr->public_data.results_found++;
878 prq->request_found = GNUNET_YES;
879 /* finally, pass on to other peer / local client */
880 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
883 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
884 pr->rh (pr->rh_cls, prq->eval, pr,
885 prq->anonymity_level, prq->expiration,
886 last_transmission, prq->type, prq->data, prq->size);
892 * Context for the 'put_migration_continuation'.
894 struct PutMigrationContext
898 * Start time for the operation.
900 struct GNUNET_TIME_Absolute start;
905 struct GNUNET_PeerIdentity origin;
908 * GNUNET_YES if we had a matching request for this block,
916 * Continuation called to notify client about result of the
920 * @param success GNUNET_SYSERR on failure
921 * @param min_expiration minimum expiration time required for content to be stored
922 * @param msg NULL on success, otherwise an error message
925 put_migration_continuation (void *cls, int success,
926 struct GNUNET_TIME_Absolute min_expiration,
929 struct PutMigrationContext *pmc = cls;
930 struct GSF_ConnectedPeer *cp;
931 struct GNUNET_TIME_Relative mig_pause;
932 struct GSF_PeerPerformanceData *ppd;
934 if (NULL != datastore_put_load)
936 if (GNUNET_SYSERR != success)
938 GNUNET_LOAD_update (datastore_put_load,
939 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
943 /* on queue failure / timeout, increase the put load dramatically */
944 GNUNET_LOAD_update (datastore_put_load,
945 GNUNET_TIME_UNIT_MINUTES.rel_value);
948 cp = GSF_peer_get_ (&pmc->origin);
949 if (GNUNET_OK == success)
953 ppd = GSF_get_peer_performance_data_ (cp);
954 ppd->migration_delay.rel_value /= 2;
959 if ( (GNUNET_NO == success) &&
960 (GNUNET_NO == pmc->requested) &&
963 ppd = GSF_get_peer_performance_data_ (cp);
964 if (min_expiration.abs_value > 0)
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967 "Asking to stop migration for %s because datastore is full\n",
968 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
969 GSF_block_peer_migration_ (cp, min_expiration);
973 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
974 ppd->migration_delay);
975 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
976 ppd->migration_delay);
977 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
978 ppd->migration_delay.rel_value);
979 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Replicated content already exists locally, asking to stop migration for %s\n",
982 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
983 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
987 GNUNET_STATISTICS_update (GSF_stats,
988 gettext_noop ("# Datastore `PUT' failures"), 1,
994 * Test if the DATABASE (PUT) load on this peer is too high
995 * to even consider processing the query at
998 * @return GNUNET_YES if the load is too high to do anything (load high)
999 * GNUNET_NO to process normally (load normal or low)
1002 test_put_load_too_high (uint32_t priority)
1006 if (NULL == datastore_put_load)
1008 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1009 return GNUNET_NO; /* very fast */
1010 ld = GNUNET_LOAD_get_load (datastore_put_load);
1011 if (ld < 2.0 * (1 + priority))
1013 GNUNET_STATISTICS_update (GSF_stats,
1015 ("# storage requests dropped due to high load"), 1,
1022 * Iterator called on each result obtained for a DHT
1023 * operation that expects a reply
1025 * @param cls closure
1026 * @param exp when will this value expire
1027 * @param key key of the result
1028 * @param get_path peers on reply path (or NULL if not recorded)
1029 * @param get_path_length number of entries in get_path
1030 * @param put_path peers on the PUT path (or NULL if not recorded)
1031 * @param put_path_length number of entries in get_path
1032 * @param type type of the result
1033 * @param size number of bytes in data
1034 * @param data pointer to the result data
1037 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1038 const struct GNUNET_HashCode * key,
1039 const struct GNUNET_PeerIdentity *get_path,
1040 unsigned int get_path_length,
1041 const struct GNUNET_PeerIdentity *put_path,
1042 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1043 size_t size, const void *data)
1045 struct GSF_PendingRequest *pr = cls;
1046 struct ProcessReplyClosure prq;
1047 struct PutMigrationContext *pmc;
1049 GNUNET_STATISTICS_update (GSF_stats,
1050 gettext_noop ("# Replies received from DHT"), 1,
1052 memset (&prq, 0, sizeof (prq));
1054 prq.expiration = exp;
1055 /* do not allow migrated content to live longer than 1 year */
1056 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1060 process_reply (&prq, key, pr);
1061 if ((GNUNET_YES == active_to_migration) &&
1062 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "Replicating result for query `%s' with priority %u\n",
1066 GNUNET_h2s (key), prq.priority);
1067 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1068 pmc->start = GNUNET_TIME_absolute_get ();
1069 pmc->requested = GNUNET_YES;
1071 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1073 0 /* replication */ ,
1074 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1075 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1076 &put_migration_continuation, pmc))
1078 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1085 * Consider looking up the data in the DHT (anonymity-level permitting).
1087 * @param pr the pending request to process
1090 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1094 struct GNUNET_PeerIdentity pi;
1095 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1097 if (0 != pr->public_data.anonymity_level)
1101 GNUNET_DHT_get_stop (pr->gh);
1106 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1109 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1110 xquery_size = sizeof (struct GNUNET_HashCode);
1112 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1114 GNUNET_assert (0 != pr->sender_pid);
1115 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1116 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1117 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1120 GNUNET_DHT_get_start (GSF_dht,
1121 pr->public_data.type, &pr->public_data.query,
1122 5 /* DEFAULT_GET_REPLICATION */ ,
1123 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1124 xquery, xquery_size, &handle_dht_reply, pr);
1125 if ( (NULL != pr->gh) &&
1126 (0 != pr->replies_seen_count) )
1127 GNUNET_DHT_get_filter_known_results (pr->gh,
1128 pr->replies_seen_count,
1134 * Task that issues a warning if the datastore lookup takes too long.
1136 * @param cls the 'struct GSF_PendingRequest'
1137 * @param tc task context
1140 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1142 struct GSF_PendingRequest *pr = cls;
1144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1145 _("Datastore lookup already took %s!\n"),
1146 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1148 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1154 * Task that issues a warning if the datastore lookup takes too long.
1156 * @param cls the 'struct GSF_PendingRequest'
1157 * @param tc task context
1160 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1162 struct GSF_PendingRequest *pr = cls;
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1165 _("On-demand lookup already took %s!\n"),
1166 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1168 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1169 &odc_warn_delay_task, pr);
1174 * We're processing (local) results for a search request
1175 * from another peer. Pass applicable results to the
1176 * peer and if we are done either clean up (operation
1177 * complete) or forward to other peers (more results possible).
1179 * @param cls our closure (struct PendingRequest)
1180 * @param key key for the content
1181 * @param size number of bytes in data
1182 * @param data content stored
1183 * @param type type of the content
1184 * @param priority priority of the content
1185 * @param anonymity anonymity-level for the content
1186 * @param expiration expiration time for the content
1187 * @param uid unique identifier for the datum;
1188 * maybe 0 if no unique identifier is available
1191 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1192 const void *data, enum GNUNET_BLOCK_Type type,
1193 uint32_t priority, uint32_t anonymity,
1194 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1196 struct GSF_PendingRequest *pr = cls;
1197 GSF_LocalLookupContinuation cont;
1198 struct ProcessReplyClosure prq;
1199 struct GNUNET_HashCode query;
1200 unsigned int old_rf;
1202 GNUNET_SCHEDULER_cancel (pr->warn_task);
1203 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1209 #if INSANE_STATISTICS
1210 GNUNET_STATISTICS_update (GSF_stats,
1212 ("# Datastore lookups concluded (no results)"),
1216 if (GNUNET_NO == pr->have_first_uid)
1218 pr->first_uid = uid;
1219 pr->have_first_uid = 1;
1223 if ((uid == pr->first_uid) && (key != NULL))
1225 GNUNET_STATISTICS_update (GSF_stats,
1227 ("# Datastore lookups concluded (seen all)"),
1229 key = NULL; /* all replies seen! */
1231 pr->have_first_uid++;
1232 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1234 GNUNET_STATISTICS_update (GSF_stats,
1236 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1238 key = NULL; /* all replies seen! */
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1245 "No further local responses available.\n");
1246 #if INSANE_STATISTICS
1247 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1248 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1249 GNUNET_STATISTICS_update (GSF_stats,
1251 ("# requested DBLOCK or IBLOCK not found"), 1,
1254 goto check_error_and_continue;
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1258 GNUNET_h2s (key), type, (unsigned long long) uid);
1259 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Found ONDEMAND block, performing on-demand encoding\n");
1263 GNUNET_STATISTICS_update (GSF_stats,
1265 ("# on-demand blocks matched requests"), 1,
1267 pr->qe_start = GNUNET_TIME_absolute_get ();
1269 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1270 &odc_warn_delay_task, pr);
1272 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1273 anonymity, expiration, uid,
1274 &process_local_reply, pr))
1276 GNUNET_STATISTICS_update (GSF_stats,
1278 ("# on-demand lookups performed successfully"),
1280 return; /* we're done */
1282 GNUNET_STATISTICS_update (GSF_stats,
1283 gettext_noop ("# on-demand lookups failed"), 1,
1285 GNUNET_SCHEDULER_cancel (pr->warn_task);
1287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1288 &warn_delay_task, pr);
1290 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1291 &pr->public_data.query,
1292 pr->public_data.type ==
1293 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1294 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1296 (GSF_PRO_PRIORITY_UNLIMITED &
1297 pr->public_data.options)) ? UINT_MAX : 1
1298 /* queue priority */ ,
1300 (GSF_PRO_PRIORITY_UNLIMITED &
1301 pr->public_data.options)) ? UINT_MAX :
1302 datastore_queue_size
1303 /* max queue size */ ,
1304 GNUNET_TIME_UNIT_FOREVER_REL,
1305 &process_local_reply, pr);
1307 return; /* we're done */
1308 GNUNET_STATISTICS_update (GSF_stats,
1310 ("# Datastore lookups concluded (error queueing)"),
1312 goto check_error_and_continue;
1314 old_rf = pr->public_data.results_found;
1315 memset (&prq, 0, sizeof (prq));
1317 prq.expiration = expiration;
1320 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1323 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1324 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1325 pr->qe_start = GNUNET_TIME_absolute_get ();
1327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1328 &warn_delay_task, pr);
1330 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1331 &pr->public_data.query,
1332 pr->public_data.type ==
1333 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1334 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1336 (GSF_PRO_PRIORITY_UNLIMITED &
1337 pr->public_data.options)) ? UINT_MAX : 1
1338 /* queue priority */ ,
1340 (GSF_PRO_PRIORITY_UNLIMITED &
1341 pr->public_data.options)) ? UINT_MAX :
1342 datastore_queue_size
1343 /* max queue size */ ,
1344 GNUNET_TIME_UNIT_FOREVER_REL,
1345 &process_local_reply, pr);
1348 GNUNET_STATISTICS_update (GSF_stats,
1350 ("# Datastore lookups concluded (error queueing)"),
1352 goto check_error_and_continue;
1357 prq.priority = priority;
1358 prq.request_found = GNUNET_NO;
1359 prq.anonymity_level = anonymity;
1360 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1361 GSF_update_datastore_delay_ (pr->public_data.start_time);
1362 process_reply (&prq, key, pr);
1363 pr->local_result = prq.eval;
1364 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1366 GNUNET_STATISTICS_update (GSF_stats,
1368 ("# Datastore lookups concluded (found last result)"),
1370 goto check_error_and_continue;
1372 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1373 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1374 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1377 GNUNET_STATISTICS_update (GSF_stats,
1379 ("# Datastore lookups concluded (load too high)"),
1381 goto check_error_and_continue;
1383 pr->qe_start = GNUNET_TIME_absolute_get ();
1385 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1388 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1389 &pr->public_data.query,
1390 pr->public_data.type ==
1391 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1392 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1394 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1395 public_data.options)) ? UINT_MAX : 1
1396 /* queue priority */ ,
1398 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1399 public_data.options)) ? UINT_MAX :
1400 datastore_queue_size
1401 /* max queue size */ ,
1402 GNUNET_TIME_UNIT_FOREVER_REL,
1403 &process_local_reply, pr);
1404 /* check if we successfully queued another datastore request;
1405 * if so, return, otherwise call our continuation (if we have
1407 check_error_and_continue:
1410 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1412 GNUNET_SCHEDULER_cancel (pr->warn_task);
1413 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1415 if (NULL == (cont = pr->llc_cont))
1416 return; /* no continuation */
1417 pr->llc_cont = NULL;
1418 cont (pr->llc_cont_cls, pr, pr->local_result);
1423 * Is the given target a legitimate peer for forwarding the given request?
1427 * @return GNUNET_YES if this request could be forwarded to the given peer
1430 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1431 const struct GNUNET_PeerIdentity *target)
1433 struct GNUNET_PeerIdentity pi;
1435 if (0 == pr->origin_pid)
1437 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1439 memcmp (&pi, target,
1440 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1446 * Look up the request in the local datastore.
1448 * @param pr the pending request to process
1449 * @param cont function to call at the end
1450 * @param cont_cls closure for cont
1453 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1454 GSF_LocalLookupContinuation cont, void *cont_cls)
1456 GNUNET_assert (NULL == pr->gh);
1457 GNUNET_assert (NULL == pr->llc_cont);
1458 pr->llc_cont = cont;
1459 pr->llc_cont_cls = cont_cls;
1460 pr->qe_start = GNUNET_TIME_absolute_get ();
1462 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1464 #if INSANE_STATISTICS
1465 GNUNET_STATISTICS_update (GSF_stats,
1466 gettext_noop ("# Datastore lookups initiated"), 1,
1470 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1471 &pr->public_data.query,
1472 pr->public_data.type ==
1473 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1474 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1476 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1477 public_data.options)) ? UINT_MAX : 1
1478 /* queue priority */ ,
1480 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1481 public_data.options)) ? UINT_MAX :
1482 datastore_queue_size
1483 /* max queue size */ ,
1484 GNUNET_TIME_UNIT_FOREVER_REL,
1485 &process_local_reply, pr);
1488 GNUNET_STATISTICS_update (GSF_stats,
1490 ("# Datastore lookups concluded (error queueing)"),
1492 GNUNET_SCHEDULER_cancel (pr->warn_task);
1493 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1494 pr->llc_cont = NULL;
1496 cont (cont_cls, pr, pr->local_result);
1502 * Handle P2P "CONTENT" message. Checks that the message is
1503 * well-formed and then checks if there are any pending requests for
1504 * this content and possibly passes it on (to local clients or other
1505 * peers). Does NOT perform migration (content caching at this peer).
1507 * @param cp the other peer involved (sender or receiver, NULL
1508 * for loopback messages where we are both sender and receiver)
1509 * @param message the actual message
1510 * @return GNUNET_OK if the message was well-formed,
1511 * GNUNET_SYSERR if the message was malformed (close connection,
1512 * do not cache under any circumstances)
1515 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1516 const struct GNUNET_MessageHeader *message)
1518 const struct PutMessage *put;
1521 enum GNUNET_BLOCK_Type type;
1522 struct GNUNET_TIME_Absolute expiration;
1523 struct GNUNET_HashCode query;
1524 struct ProcessReplyClosure prq;
1525 struct GNUNET_TIME_Relative block_time;
1527 struct PutMigrationContext *pmc;
1529 msize = ntohs (message->size);
1530 if (msize < sizeof (struct PutMessage))
1532 GNUNET_break_op (0);
1533 return GNUNET_SYSERR;
1535 put = (const struct PutMessage *) message;
1536 dsize = msize - sizeof (struct PutMessage);
1537 type = ntohl (put->type);
1538 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1539 /* do not allow migrated content to live longer than 1 year */
1540 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1542 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1543 return GNUNET_SYSERR;
1545 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1547 GNUNET_break_op (0);
1548 return GNUNET_SYSERR;
1550 GNUNET_STATISTICS_update (GSF_stats,
1551 gettext_noop ("# GAP PUT messages received"), 1,
1553 /* now, lookup 'query' */
1554 prq.data = (const void *) &put[1];
1558 prq.expiration = expiration;
1560 prq.anonymity_level = UINT32_MAX;
1561 prq.request_found = GNUNET_NO;
1562 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1566 GSF_connected_peer_change_preference_ (cp,
1567 CONTENT_BANDWIDTH_VALUE +
1568 1000 * prq.priority);
1569 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1571 if ((GNUNET_YES == active_to_migration) &&
1573 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Replicating result for query `%s' with priority %u\n",
1577 GNUNET_h2s (&query), prq.priority);
1578 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1579 pmc->start = GNUNET_TIME_absolute_get ();
1580 pmc->requested = prq.request_found;
1581 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1582 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1585 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1586 prq.priority, 1 /* anonymity */ ,
1587 0 /* replication */ ,
1588 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1589 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1590 &put_migration_continuation, pmc))
1592 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1595 else if (NULL != cp)
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1598 "Choosing not to keep content `%s' (%d/%d)\n",
1599 GNUNET_h2s (&query), active_to_migration,
1600 test_put_load_too_high (prq.priority));
1602 putl = GNUNET_LOAD_get_load (datastore_put_load);
1603 if ( (NULL != cp) &&
1604 (GNUNET_NO == prq.request_found) &&
1605 ( (GNUNET_YES != active_to_migration) ||
1606 (putl > 2.5 * (1 + prq.priority)) ) )
1608 if (GNUNET_YES != active_to_migration)
1609 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1611 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1613 GNUNET_CRYPTO_random_u32
1614 (GNUNET_CRYPTO_QUALITY_WEAK,
1615 (unsigned int) (60000 * putl * putl)));
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1618 (unsigned long long) block_time.rel_value,
1620 active_to_migration,
1621 (GNUNET_NO == prq.request_found));
1622 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1629 * Setup the subsystem.
1632 GSF_pending_request_init_ ()
1634 unsigned long long dqs;
1637 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1638 "MAX_PENDING_REQUESTS",
1639 &max_pending_requests))
1641 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1642 "fs", "MAX_PENDING_REQUESTS");
1645 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
1648 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1649 "fs", "DATASTORE_QUEUE_SIZE");
1652 datastore_queue_size = (unsigned int) dqs;
1654 active_to_migration =
1655 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1656 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1657 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1658 requests_by_expiration_heap =
1659 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1664 * Shutdown the subsystem.
1667 GSF_pending_request_done_ ()
1669 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1670 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1672 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1673 requests_by_expiration_heap = NULL;
1674 GNUNET_LOAD_value_free (datastore_put_load);
1675 datastore_put_load = NULL;
1679 /* end of gnunet-service-fs_pr.c */