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);
485 * Generate the message corresponding to the given pending request for
486 * transmission to other peers (or at least determine its size).
488 * @param pr request to generate the message for
489 * @param buf_size number of bytes available in buf
490 * @param buf where to copy the message (can be NULL)
491 * @return number of bytes needed (if > buf_size) or used
494 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
495 size_t buf_size, void *buf)
497 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
498 struct GetMessage *gm;
499 struct GNUNET_HashCode *ext;
505 struct GNUNET_TIME_Absolute now;
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Building request message for `%s' of type %d\n",
512 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
515 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
516 if ((!do_route) && (pr->sender_pid == 0))
519 do_route = GNUNET_YES;
523 bm |= GET_MESSAGE_BIT_RETURN_TO;
526 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
528 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
531 if (NULL != pr->public_data.target)
533 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
536 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
537 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_HashCode);
538 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
539 if (buf_size < msize)
541 gm = (struct GetMessage *) lbuf;
542 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
543 gm->header.size = htons (msize);
544 gm->type = htonl (pr->public_data.type);
547 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
548 pr->public_data.priority + 1);
551 pr->public_data.priority -= prio;
552 pr->public_data.num_transmissions++;
553 pr->public_data.respect_offered += prio;
554 gm->priority = htonl (prio);
555 now = GNUNET_TIME_absolute_get ();
556 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
557 gm->ttl = htonl (ttl / 1000);
558 gm->filter_mutator = htonl (pr->mingle);
559 gm->hash_bitmap = htonl (bm);
560 gm->query = pr->public_data.query;
561 ext = (struct GNUNET_HashCode *) & gm[1];
564 GNUNET_PEER_resolve (pr->sender_pid,
565 (struct GNUNET_PeerIdentity *) &ext[k++]);
566 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
567 memcpy (&ext[k++], pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
568 if (NULL != pr->public_data.target)
570 pr->public_data.target,
571 sizeof (struct GNUNET_PeerIdentity));
573 GNUNET_assert (GNUNET_SYSERR !=
574 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
577 memcpy (buf, gm, msize);
583 * Iterator to free pending requests.
585 * @param cls closure, unused
586 * @param key current key code
587 * @param value value in the hash map (pending request)
588 * @return GNUNET_YES (we should continue to iterate)
591 clean_request (void *cls, const struct GNUNET_HashCode * key, void *value)
593 struct GSF_PendingRequest *pr = value;
594 GSF_LocalLookupContinuation cont;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
598 if (NULL != (cont = pr->llc_cont))
601 cont (pr->llc_cont_cls, pr, pr->local_result);
603 GSF_plan_notify_request_done_ (pr);
604 GNUNET_free_non_null (pr->replies_seen);
607 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
610 GNUNET_PEER_change_rc (pr->sender_pid, -1);
612 GNUNET_PEER_change_rc (pr->origin_pid, -1);
614 if (NULL != pr->hnode)
616 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
621 GNUNET_DATASTORE_cancel (pr->qe);
626 GNUNET_DHT_get_stop (pr->gh);
629 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
631 GNUNET_SCHEDULER_cancel (pr->warn_task);
632 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
634 GNUNET_assert (GNUNET_OK ==
635 GNUNET_CONTAINER_multihashmap_remove (pr_map,
636 &pr->public_data.query,
638 GNUNET_STATISTICS_update (GSF_stats,
639 gettext_noop ("# Pending requests active"), -1,
647 * Explicitly cancel a pending request.
649 * @param pr request to cancel
650 * @param full_cleanup fully purge the request
653 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
655 GSF_LocalLookupContinuation cont;
658 return; /* already cleaned up! */
659 if (GNUNET_YES != full_cleanup)
661 /* make request inactive (we're no longer interested in more results),
662 * but do NOT remove from our data-structures, we still need it there
663 * to prevent the request from looping */
665 if (NULL != (cont = pr->llc_cont))
668 cont (pr->llc_cont_cls, pr, pr->local_result);
670 GSF_plan_notify_request_done_ (pr);
673 GNUNET_DATASTORE_cancel (pr->qe);
678 GNUNET_DHT_get_stop (pr->gh);
681 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
683 GNUNET_SCHEDULER_cancel (pr->warn_task);
684 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
688 GNUNET_assert (GNUNET_YES ==
689 clean_request (NULL, &pr->public_data.query, pr));
694 * Iterate over all pending requests.
696 * @param it function to call for each request
697 * @param cls closure for it
700 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
702 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
703 (GNUNET_CONTAINER_HashMapIterator) it,
711 * Closure for "process_reply" function.
713 struct ProcessReplyClosure
716 * The data for the reply.
721 * Who gave us this reply? NULL for local host (or DHT)
723 struct GSF_ConnectedPeer *sender;
726 * When the reply expires.
728 struct GNUNET_TIME_Absolute expiration;
738 enum GNUNET_BLOCK_Type type;
741 * How much was this reply worth to us?
746 * Anonymity requirements for this reply.
748 uint32_t anonymity_level;
751 * Evaluation result (returned).
753 enum GNUNET_BLOCK_EvaluationResult eval;
756 * Did we find a matching request?
763 * Update the performance data for the sender (if any) since
764 * the sender successfully answered one of our queries.
766 * @param prq information about the sender
767 * @param pr request that was satisfied
770 update_request_performance_data (struct ProcessReplyClosure *prq,
771 struct GSF_PendingRequest *pr)
773 if (prq->sender == NULL)
775 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
781 * We have received a reply; handle it!
783 * @param cls response (struct ProcessReplyClosure)
784 * @param key our query
785 * @param value value in the hash map (info about the query)
786 * @return GNUNET_YES (we should continue to iterate)
789 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
791 struct ProcessReplyClosure *prq = cls;
792 struct GSF_PendingRequest *pr = value;
793 struct GNUNET_HashCode chash;
794 struct GNUNET_TIME_Absolute last_transmission;
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Matched result (type %u) for query `%s' with pending request\n",
800 (unsigned int) prq->type, GNUNET_h2s (key));
801 GNUNET_STATISTICS_update (GSF_stats,
802 gettext_noop ("# replies received and matched"), 1,
805 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
806 pr->public_data.namespace,
808 GNUNET_BLOCK_TYPE_FS_SBLOCK) ?
809 sizeof (struct GNUNET_HashCode) : 0, prq->data,
813 case GNUNET_BLOCK_EVALUATION_OK_MORE:
814 update_request_performance_data (prq, pr);
816 case GNUNET_BLOCK_EVALUATION_OK_LAST:
817 /* short cut: stop processing early, no BF-update, etc. */
818 update_request_performance_data (prq, pr);
819 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
820 GNUNET_TIME_absolute_get_duration (pr->
821 public_data.start_time).rel_value);
823 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
826 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
827 /* pass on to other peers / local clients */
828 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
829 last_transmission, prq->type, prq->data, prq->size);
831 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
832 #if INSANE_STATISTICS
833 GNUNET_STATISTICS_update (GSF_stats,
835 ("# duplicate replies discarded (bloomfilter)"),
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Duplicate response, discarding.\n");
840 return GNUNET_YES; /* duplicate */
841 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
842 return GNUNET_YES; /* wrong namespace */
843 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
846 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
849 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
854 /* update bloomfilter */
855 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
856 GSF_pending_request_update_ (pr, &chash, 1);
857 if (NULL == prq->sender)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Found result for query `%s' in local datastore\n",
862 GNUNET_STATISTICS_update (GSF_stats,
863 gettext_noop ("# results found locally"), 1,
868 GSF_dht_lookup_ (pr);
870 prq->priority += pr->public_data.original_priority;
871 pr->public_data.priority = 0;
872 pr->public_data.original_priority = 0;
873 pr->public_data.results_found++;
874 prq->request_found = GNUNET_YES;
875 /* finally, pass on to other peer / local client */
876 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
879 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
880 pr->rh (pr->rh_cls, prq->eval, pr,
881 prq->anonymity_level, prq->expiration,
882 last_transmission, prq->type, prq->data, prq->size);
888 * Context for the 'put_migration_continuation'.
890 struct PutMigrationContext
894 * Start time for the operation.
896 struct GNUNET_TIME_Absolute start;
901 struct GNUNET_PeerIdentity origin;
904 * GNUNET_YES if we had a matching request for this block,
912 * Continuation called to notify client about result of the
916 * @param success GNUNET_SYSERR on failure
917 * @param min_expiration minimum expiration time required for content to be stored
918 * @param msg NULL on success, otherwise an error message
921 put_migration_continuation (void *cls, int success,
922 struct GNUNET_TIME_Absolute min_expiration,
925 struct PutMigrationContext *pmc = cls;
926 struct GSF_ConnectedPeer *cp;
927 struct GNUNET_TIME_Relative mig_pause;
928 struct GSF_PeerPerformanceData *ppd;
930 if (NULL != datastore_put_load)
932 if (GNUNET_SYSERR != success)
934 GNUNET_LOAD_update (datastore_put_load,
935 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
939 /* on queue failure / timeout, increase the put load dramatically */
940 GNUNET_LOAD_update (datastore_put_load,
941 GNUNET_TIME_UNIT_MINUTES.rel_value);
944 cp = GSF_peer_get_ (&pmc->origin);
945 if (GNUNET_OK == success)
949 ppd = GSF_get_peer_performance_data_ (cp);
950 ppd->migration_delay.rel_value /= 2;
955 if ( (GNUNET_NO == success) &&
956 (GNUNET_NO == pmc->requested) &&
959 ppd = GSF_get_peer_performance_data_ (cp);
960 if (min_expiration.abs_value > 0)
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "Asking to stop migration for %s because datastore is full\n",
964 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
965 GSF_block_peer_migration_ (cp, min_expiration);
969 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
970 ppd->migration_delay);
971 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
972 ppd->migration_delay);
973 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
974 ppd->migration_delay.rel_value);
975 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Replicated content already exists locally, asking to stop migration for %s\n",
978 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
979 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
983 GNUNET_STATISTICS_update (GSF_stats,
984 gettext_noop ("# Datastore `PUT' failures"), 1,
990 * Test if the DATABASE (PUT) load on this peer is too high
991 * to even consider processing the query at
994 * @return GNUNET_YES if the load is too high to do anything (load high)
995 * GNUNET_NO to process normally (load normal or low)
998 test_put_load_too_high (uint32_t priority)
1002 if (NULL == datastore_put_load)
1004 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1005 return GNUNET_NO; /* very fast */
1006 ld = GNUNET_LOAD_get_load (datastore_put_load);
1007 if (ld < 2.0 * (1 + priority))
1009 GNUNET_STATISTICS_update (GSF_stats,
1011 ("# storage requests dropped due to high load"), 1,
1018 * Iterator called on each result obtained for a DHT
1019 * operation that expects a reply
1021 * @param cls closure
1022 * @param exp when will this value expire
1023 * @param key key of the result
1024 * @param get_path peers on reply path (or NULL if not recorded)
1025 * @param get_path_length number of entries in get_path
1026 * @param put_path peers on the PUT path (or NULL if not recorded)
1027 * @param put_path_length number of entries in get_path
1028 * @param type type of the result
1029 * @param size number of bytes in data
1030 * @param data pointer to the result data
1033 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1034 const struct GNUNET_HashCode * key,
1035 const struct GNUNET_PeerIdentity *get_path,
1036 unsigned int get_path_length,
1037 const struct GNUNET_PeerIdentity *put_path,
1038 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1039 size_t size, const void *data)
1041 struct GSF_PendingRequest *pr = cls;
1042 struct ProcessReplyClosure prq;
1043 struct PutMigrationContext *pmc;
1045 GNUNET_STATISTICS_update (GSF_stats,
1046 gettext_noop ("# Replies received from DHT"), 1,
1048 memset (&prq, 0, sizeof (prq));
1050 prq.expiration = exp;
1051 /* do not allow migrated content to live longer than 1 year */
1052 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1056 process_reply (&prq, key, pr);
1057 if ((GNUNET_YES == active_to_migration) &&
1058 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Replicating result for query `%s' with priority %u\n",
1062 GNUNET_h2s (key), prq.priority);
1063 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1064 pmc->start = GNUNET_TIME_absolute_get ();
1065 pmc->requested = GNUNET_YES;
1067 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1069 0 /* replication */ ,
1070 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1071 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1072 &put_migration_continuation, pmc))
1074 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1081 * Consider looking up the data in the DHT (anonymity-level permitting).
1083 * @param pr the pending request to process
1086 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1090 struct GNUNET_PeerIdentity pi;
1091 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1093 if (0 != pr->public_data.anonymity_level)
1097 GNUNET_DHT_get_stop (pr->gh);
1102 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1105 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1106 xquery_size = sizeof (struct GNUNET_HashCode);
1108 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1110 GNUNET_assert (0 != pr->sender_pid);
1111 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1112 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1113 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1116 GNUNET_DHT_get_start (GSF_dht,
1117 pr->public_data.type, &pr->public_data.query,
1118 5 /* DEFAULT_GET_REPLICATION */ ,
1119 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1120 /* FIXME: can no longer pass pr->bf/pr->mingle... */
1121 xquery, xquery_size, &handle_dht_reply, pr);
1126 * Task that issues a warning if the datastore lookup takes too long.
1128 * @param cls the 'struct GSF_PendingRequest'
1129 * @param tc task context
1132 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1134 struct GSF_PendingRequest *pr = cls;
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137 _("Datastore lookup already took %s!\n"),
1138 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1140 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1146 * Task that issues a warning if the datastore lookup takes too long.
1148 * @param cls the 'struct GSF_PendingRequest'
1149 * @param tc task context
1152 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1154 struct GSF_PendingRequest *pr = cls;
1156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1157 _("On-demand lookup already took %s!\n"),
1158 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1160 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1161 &odc_warn_delay_task, pr);
1166 * We're processing (local) results for a search request
1167 * from another peer. Pass applicable results to the
1168 * peer and if we are done either clean up (operation
1169 * complete) or forward to other peers (more results possible).
1171 * @param cls our closure (struct PendingRequest)
1172 * @param key key for the content
1173 * @param size number of bytes in data
1174 * @param data content stored
1175 * @param type type of the content
1176 * @param priority priority of the content
1177 * @param anonymity anonymity-level for the content
1178 * @param expiration expiration time for the content
1179 * @param uid unique identifier for the datum;
1180 * maybe 0 if no unique identifier is available
1183 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1184 const void *data, enum GNUNET_BLOCK_Type type,
1185 uint32_t priority, uint32_t anonymity,
1186 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1188 struct GSF_PendingRequest *pr = cls;
1189 GSF_LocalLookupContinuation cont;
1190 struct ProcessReplyClosure prq;
1191 struct GNUNET_HashCode query;
1192 unsigned int old_rf;
1194 GNUNET_SCHEDULER_cancel (pr->warn_task);
1195 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1201 #if INSANE_STATISTICS
1202 GNUNET_STATISTICS_update (GSF_stats,
1204 ("# Datastore lookups concluded (no results)"),
1208 if (GNUNET_NO == pr->have_first_uid)
1210 pr->first_uid = uid;
1211 pr->have_first_uid = 1;
1215 if ((uid == pr->first_uid) && (key != NULL))
1217 GNUNET_STATISTICS_update (GSF_stats,
1219 ("# Datastore lookups concluded (seen all)"),
1221 key = NULL; /* all replies seen! */
1223 pr->have_first_uid++;
1224 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1226 GNUNET_STATISTICS_update (GSF_stats,
1228 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1230 key = NULL; /* all replies seen! */
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1237 "No further local responses available.\n");
1238 #if INSANE_STATISTICS
1239 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1240 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1241 GNUNET_STATISTICS_update (GSF_stats,
1243 ("# requested DBLOCK or IBLOCK not found"), 1,
1246 goto check_error_and_continue;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1250 GNUNET_h2s (key), type, (unsigned long long) uid);
1251 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Found ONDEMAND block, performing on-demand encoding\n");
1255 GNUNET_STATISTICS_update (GSF_stats,
1257 ("# on-demand blocks matched requests"), 1,
1259 pr->qe_start = GNUNET_TIME_absolute_get ();
1261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1262 &odc_warn_delay_task, pr);
1264 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1265 anonymity, expiration, uid,
1266 &process_local_reply, pr))
1268 GNUNET_STATISTICS_update (GSF_stats,
1270 ("# on-demand lookups performed successfully"),
1272 return; /* we're done */
1274 GNUNET_STATISTICS_update (GSF_stats,
1275 gettext_noop ("# on-demand lookups failed"), 1,
1277 GNUNET_SCHEDULER_cancel (pr->warn_task);
1279 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1280 &warn_delay_task, pr);
1282 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1283 &pr->public_data.query,
1284 pr->public_data.type ==
1285 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1286 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1288 (GSF_PRO_PRIORITY_UNLIMITED &
1289 pr->public_data.options)) ? UINT_MAX : 1
1290 /* queue priority */ ,
1292 (GSF_PRO_PRIORITY_UNLIMITED &
1293 pr->public_data.options)) ? UINT_MAX :
1294 datastore_queue_size
1295 /* max queue size */ ,
1296 GNUNET_TIME_UNIT_FOREVER_REL,
1297 &process_local_reply, pr);
1299 return; /* we're done */
1300 GNUNET_STATISTICS_update (GSF_stats,
1302 ("# Datastore lookups concluded (error queueing)"),
1304 goto check_error_and_continue;
1306 old_rf = pr->public_data.results_found;
1307 memset (&prq, 0, sizeof (prq));
1309 prq.expiration = expiration;
1312 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1315 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1316 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1317 pr->qe_start = GNUNET_TIME_absolute_get ();
1319 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1320 &warn_delay_task, pr);
1322 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1323 &pr->public_data.query,
1324 pr->public_data.type ==
1325 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1326 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1328 (GSF_PRO_PRIORITY_UNLIMITED &
1329 pr->public_data.options)) ? UINT_MAX : 1
1330 /* queue priority */ ,
1332 (GSF_PRO_PRIORITY_UNLIMITED &
1333 pr->public_data.options)) ? UINT_MAX :
1334 datastore_queue_size
1335 /* max queue size */ ,
1336 GNUNET_TIME_UNIT_FOREVER_REL,
1337 &process_local_reply, pr);
1340 GNUNET_STATISTICS_update (GSF_stats,
1342 ("# Datastore lookups concluded (error queueing)"),
1344 goto check_error_and_continue;
1349 prq.priority = priority;
1350 prq.request_found = GNUNET_NO;
1351 prq.anonymity_level = anonymity;
1352 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1353 GSF_update_datastore_delay_ (pr->public_data.start_time);
1354 process_reply (&prq, key, pr);
1355 pr->local_result = prq.eval;
1356 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1358 GNUNET_STATISTICS_update (GSF_stats,
1360 ("# Datastore lookups concluded (found last result)"),
1362 goto check_error_and_continue;
1364 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1365 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1366 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1369 GNUNET_STATISTICS_update (GSF_stats,
1371 ("# Datastore lookups concluded (load too high)"),
1373 goto check_error_and_continue;
1375 pr->qe_start = GNUNET_TIME_absolute_get ();
1377 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1380 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1381 &pr->public_data.query,
1382 pr->public_data.type ==
1383 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1384 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1386 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1387 public_data.options)) ? UINT_MAX : 1
1388 /* queue priority */ ,
1390 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1391 public_data.options)) ? UINT_MAX :
1392 datastore_queue_size
1393 /* max queue size */ ,
1394 GNUNET_TIME_UNIT_FOREVER_REL,
1395 &process_local_reply, pr);
1396 /* check if we successfully queued another datastore request;
1397 * if so, return, otherwise call our continuation (if we have
1399 check_error_and_continue:
1402 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1404 GNUNET_SCHEDULER_cancel (pr->warn_task);
1405 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1407 if (NULL == (cont = pr->llc_cont))
1408 return; /* no continuation */
1409 pr->llc_cont = NULL;
1410 cont (pr->llc_cont_cls, pr, pr->local_result);
1415 * Is the given target a legitimate peer for forwarding the given request?
1419 * @return GNUNET_YES if this request could be forwarded to the given peer
1422 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1423 const struct GNUNET_PeerIdentity *target)
1425 struct GNUNET_PeerIdentity pi;
1427 if (0 == pr->origin_pid)
1429 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1431 memcmp (&pi, target,
1432 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1438 * Look up the request in the local datastore.
1440 * @param pr the pending request to process
1441 * @param cont function to call at the end
1442 * @param cont_cls closure for cont
1445 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1446 GSF_LocalLookupContinuation cont, void *cont_cls)
1448 GNUNET_assert (NULL == pr->gh);
1449 GNUNET_assert (NULL == pr->llc_cont);
1450 pr->llc_cont = cont;
1451 pr->llc_cont_cls = cont_cls;
1452 pr->qe_start = GNUNET_TIME_absolute_get ();
1454 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1456 #if INSANE_STATISTICS
1457 GNUNET_STATISTICS_update (GSF_stats,
1458 gettext_noop ("# Datastore lookups initiated"), 1,
1462 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1463 &pr->public_data.query,
1464 pr->public_data.type ==
1465 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1466 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1468 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1469 public_data.options)) ? UINT_MAX : 1
1470 /* queue priority */ ,
1472 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1473 public_data.options)) ? UINT_MAX :
1474 datastore_queue_size
1475 /* max queue size */ ,
1476 GNUNET_TIME_UNIT_FOREVER_REL,
1477 &process_local_reply, pr);
1480 GNUNET_STATISTICS_update (GSF_stats,
1482 ("# Datastore lookups concluded (error queueing)"),
1484 GNUNET_SCHEDULER_cancel (pr->warn_task);
1485 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1486 pr->llc_cont = NULL;
1488 cont (cont_cls, pr, pr->local_result);
1494 * Handle P2P "CONTENT" message. Checks that the message is
1495 * well-formed and then checks if there are any pending requests for
1496 * this content and possibly passes it on (to local clients or other
1497 * peers). Does NOT perform migration (content caching at this peer).
1499 * @param cp the other peer involved (sender or receiver, NULL
1500 * for loopback messages where we are both sender and receiver)
1501 * @param message the actual message
1502 * @return GNUNET_OK if the message was well-formed,
1503 * GNUNET_SYSERR if the message was malformed (close connection,
1504 * do not cache under any circumstances)
1507 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1508 const struct GNUNET_MessageHeader *message)
1510 const struct PutMessage *put;
1513 enum GNUNET_BLOCK_Type type;
1514 struct GNUNET_TIME_Absolute expiration;
1515 struct GNUNET_HashCode query;
1516 struct ProcessReplyClosure prq;
1517 struct GNUNET_TIME_Relative block_time;
1519 struct PutMigrationContext *pmc;
1521 msize = ntohs (message->size);
1522 if (msize < sizeof (struct PutMessage))
1524 GNUNET_break_op (0);
1525 return GNUNET_SYSERR;
1527 put = (const struct PutMessage *) message;
1528 dsize = msize - sizeof (struct PutMessage);
1529 type = ntohl (put->type);
1530 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1531 /* do not allow migrated content to live longer than 1 year */
1532 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1534 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1535 return GNUNET_SYSERR;
1537 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1539 GNUNET_break_op (0);
1540 return GNUNET_SYSERR;
1542 GNUNET_STATISTICS_update (GSF_stats,
1543 gettext_noop ("# GAP PUT messages received"), 1,
1545 /* now, lookup 'query' */
1546 prq.data = (const void *) &put[1];
1550 prq.expiration = expiration;
1552 prq.anonymity_level = UINT32_MAX;
1553 prq.request_found = GNUNET_NO;
1554 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1558 GSF_connected_peer_change_preference_ (cp,
1559 CONTENT_BANDWIDTH_VALUE +
1560 1000 * prq.priority);
1561 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1563 if ((GNUNET_YES == active_to_migration) &&
1565 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1568 "Replicating result for query `%s' with priority %u\n",
1569 GNUNET_h2s (&query), prq.priority);
1570 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1571 pmc->start = GNUNET_TIME_absolute_get ();
1572 pmc->requested = prq.request_found;
1573 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1574 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1577 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1578 prq.priority, 1 /* anonymity */ ,
1579 0 /* replication */ ,
1580 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1581 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1582 &put_migration_continuation, pmc))
1584 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1587 else if (NULL != cp)
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Choosing not to keep content `%s' (%d/%d)\n",
1591 GNUNET_h2s (&query), active_to_migration,
1592 test_put_load_too_high (prq.priority));
1594 putl = GNUNET_LOAD_get_load (datastore_put_load);
1595 if ( (NULL != cp) &&
1596 (GNUNET_NO == prq.request_found) &&
1597 ( (GNUNET_YES != active_to_migration) ||
1598 (putl > 2.5 * (1 + prq.priority)) ) )
1600 if (GNUNET_YES != active_to_migration)
1601 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1603 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1605 GNUNET_CRYPTO_random_u32
1606 (GNUNET_CRYPTO_QUALITY_WEAK,
1607 (unsigned int) (60000 * putl * putl)));
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1609 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1610 (unsigned long long) block_time.rel_value,
1612 active_to_migration,
1613 (GNUNET_NO == prq.request_found));
1614 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1621 * Setup the subsystem.
1624 GSF_pending_request_init_ ()
1626 unsigned long long dqs;
1629 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1630 "MAX_PENDING_REQUESTS",
1631 &max_pending_requests))
1633 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1634 "fs", "MAX_PENDING_REQUESTS");
1637 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
1640 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1641 "fs", "DATASTORE_QUEUE_SIZE");
1644 datastore_queue_size = (unsigned int) dqs;
1646 active_to_migration =
1647 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1648 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1649 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1650 requests_by_expiration_heap =
1651 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1656 * Shutdown the subsystem.
1659 GSF_pending_request_done_ ()
1661 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1662 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1664 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1665 requests_by_expiration_heap = NULL;
1666 GNUNET_LOAD_value_free (datastore_put_load);
1667 datastore_put_load = NULL;
1671 /* end of gnunet-service-fs_pr.c */