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);
822 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
825 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
826 /* pass on to other peers / local clients */
827 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
828 last_transmission, prq->type, prq->data, prq->size);
830 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
831 #if INSANE_STATISTICS
832 GNUNET_STATISTICS_update (GSF_stats,
834 ("# duplicate replies discarded (bloomfilter)"),
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Duplicate response, discarding.\n");
839 return GNUNET_YES; /* duplicate */
840 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
841 return GNUNET_YES; /* wrong namespace */
842 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
845 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
848 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
849 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
853 /* update bloomfilter */
854 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
855 GSF_pending_request_update_ (pr, &chash, 1);
856 if (NULL == prq->sender)
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Found result for query `%s' in local datastore\n",
861 GNUNET_STATISTICS_update (GSF_stats,
862 gettext_noop ("# results found locally"), 1,
867 GSF_dht_lookup_ (pr);
869 prq->priority += pr->public_data.original_priority;
870 pr->public_data.priority = 0;
871 pr->public_data.original_priority = 0;
872 pr->public_data.results_found++;
873 prq->request_found = GNUNET_YES;
874 /* finally, pass on to other peer / local client */
875 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
878 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
879 pr->rh (pr->rh_cls, prq->eval, pr,
880 prq->anonymity_level, prq->expiration,
881 last_transmission, prq->type, prq->data, prq->size);
887 * Context for the 'put_migration_continuation'.
889 struct PutMigrationContext
893 * Start time for the operation.
895 struct GNUNET_TIME_Absolute start;
900 struct GNUNET_PeerIdentity origin;
903 * GNUNET_YES if we had a matching request for this block,
911 * Continuation called to notify client about result of the
915 * @param success GNUNET_SYSERR on failure
916 * @param min_expiration minimum expiration time required for content to be stored
917 * @param msg NULL on success, otherwise an error message
920 put_migration_continuation (void *cls, int success,
921 struct GNUNET_TIME_Absolute min_expiration,
924 struct PutMigrationContext *pmc = cls;
925 struct GSF_ConnectedPeer *cp;
926 struct GNUNET_TIME_Relative mig_pause;
927 struct GSF_PeerPerformanceData *ppd;
929 if (NULL != datastore_put_load)
931 if (GNUNET_SYSERR != success)
933 GNUNET_LOAD_update (datastore_put_load,
934 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
938 /* on queue failure / timeout, increase the put load dramatically */
939 GNUNET_LOAD_update (datastore_put_load,
940 GNUNET_TIME_UNIT_MINUTES.rel_value);
943 cp = GSF_peer_get_ (&pmc->origin);
944 if (GNUNET_OK == success)
948 ppd = GSF_get_peer_performance_data_ (cp);
949 ppd->migration_delay.rel_value /= 2;
954 if ( (GNUNET_NO == success) &&
955 (GNUNET_NO == pmc->requested) &&
958 ppd = GSF_get_peer_performance_data_ (cp);
959 if (min_expiration.abs_value > 0)
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Asking to stop migration for %s because datastore is full\n",
963 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
964 GSF_block_peer_migration_ (cp, min_expiration);
968 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
969 ppd->migration_delay);
970 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
971 ppd->migration_delay);
972 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
973 ppd->migration_delay.rel_value);
974 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "Replicated content already exists locally, asking to stop migration for %s\n",
977 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
978 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
982 GNUNET_STATISTICS_update (GSF_stats,
983 gettext_noop ("# Datastore `PUT' failures"), 1,
989 * Test if the DATABASE (PUT) load on this peer is too high
990 * to even consider processing the query at
993 * @return GNUNET_YES if the load is too high to do anything (load high)
994 * GNUNET_NO to process normally (load normal or low)
997 test_put_load_too_high (uint32_t priority)
1001 if (NULL == datastore_put_load)
1003 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1004 return GNUNET_NO; /* very fast */
1005 ld = GNUNET_LOAD_get_load (datastore_put_load);
1006 if (ld < 2.0 * (1 + priority))
1008 GNUNET_STATISTICS_update (GSF_stats,
1010 ("# storage requests dropped due to high load"), 1,
1017 * Iterator called on each result obtained for a DHT
1018 * operation that expects a reply
1020 * @param cls closure
1021 * @param exp when will this value expire
1022 * @param key key of the result
1023 * @param get_path peers on reply path (or NULL if not recorded)
1024 * @param get_path_length number of entries in get_path
1025 * @param put_path peers on the PUT path (or NULL if not recorded)
1026 * @param put_path_length number of entries in get_path
1027 * @param type type of the result
1028 * @param size number of bytes in data
1029 * @param data pointer to the result data
1032 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1033 const struct GNUNET_HashCode * key,
1034 const struct GNUNET_PeerIdentity *get_path,
1035 unsigned int get_path_length,
1036 const struct GNUNET_PeerIdentity *put_path,
1037 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1038 size_t size, const void *data)
1040 struct GSF_PendingRequest *pr = cls;
1041 struct ProcessReplyClosure prq;
1042 struct PutMigrationContext *pmc;
1044 GNUNET_STATISTICS_update (GSF_stats,
1045 gettext_noop ("# Replies received from DHT"), 1,
1047 memset (&prq, 0, sizeof (prq));
1049 prq.expiration = exp;
1050 /* do not allow migrated content to live longer than 1 year */
1051 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1055 process_reply (&prq, key, pr);
1056 if ((GNUNET_YES == active_to_migration) &&
1057 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Replicating result for query `%s' with priority %u\n",
1061 GNUNET_h2s (key), prq.priority);
1062 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1063 pmc->start = GNUNET_TIME_absolute_get ();
1064 pmc->requested = GNUNET_YES;
1066 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1068 0 /* replication */ ,
1069 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1070 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1071 &put_migration_continuation, pmc))
1073 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1080 * Consider looking up the data in the DHT (anonymity-level permitting).
1082 * @param pr the pending request to process
1085 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1089 struct GNUNET_PeerIdentity pi;
1090 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1092 if (0 != pr->public_data.anonymity_level)
1096 GNUNET_DHT_get_stop (pr->gh);
1101 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1104 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1105 xquery_size = sizeof (struct GNUNET_HashCode);
1107 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1109 GNUNET_assert (0 != pr->sender_pid);
1110 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1111 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1112 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1115 GNUNET_DHT_get_start (GSF_dht,
1116 pr->public_data.type, &pr->public_data.query,
1117 5 /* DEFAULT_GET_REPLICATION */ ,
1118 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1119 /* FIXME: can no longer pass pr->bf/pr->mingle... */
1120 xquery, xquery_size, &handle_dht_reply, pr);
1125 * Task that issues a warning if the datastore lookup takes too long.
1127 * @param cls the 'struct GSF_PendingRequest'
1128 * @param tc task context
1131 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1133 struct GSF_PendingRequest *pr = cls;
1135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1136 _("Datastore lookup already took %s!\n"),
1137 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1139 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1145 * Task that issues a warning if the datastore lookup takes too long.
1147 * @param cls the 'struct GSF_PendingRequest'
1148 * @param tc task context
1151 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1153 struct GSF_PendingRequest *pr = cls;
1155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1156 _("On-demand lookup already took %s!\n"),
1157 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1159 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1160 &odc_warn_delay_task, pr);
1165 * We're processing (local) results for a search request
1166 * from another peer. Pass applicable results to the
1167 * peer and if we are done either clean up (operation
1168 * complete) or forward to other peers (more results possible).
1170 * @param cls our closure (struct PendingRequest)
1171 * @param key key for the content
1172 * @param size number of bytes in data
1173 * @param data content stored
1174 * @param type type of the content
1175 * @param priority priority of the content
1176 * @param anonymity anonymity-level for the content
1177 * @param expiration expiration time for the content
1178 * @param uid unique identifier for the datum;
1179 * maybe 0 if no unique identifier is available
1182 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1183 const void *data, enum GNUNET_BLOCK_Type type,
1184 uint32_t priority, uint32_t anonymity,
1185 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1187 struct GSF_PendingRequest *pr = cls;
1188 GSF_LocalLookupContinuation cont;
1189 struct ProcessReplyClosure prq;
1190 struct GNUNET_HashCode query;
1191 unsigned int old_rf;
1193 GNUNET_SCHEDULER_cancel (pr->warn_task);
1194 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1200 #if INSANE_STATISTICS
1201 GNUNET_STATISTICS_update (GSF_stats,
1203 ("# Datastore lookups concluded (no results)"),
1207 if (GNUNET_NO == pr->have_first_uid)
1209 pr->first_uid = uid;
1210 pr->have_first_uid = 1;
1214 if ((uid == pr->first_uid) && (key != NULL))
1216 GNUNET_STATISTICS_update (GSF_stats,
1218 ("# Datastore lookups concluded (seen all)"),
1220 key = NULL; /* all replies seen! */
1222 pr->have_first_uid++;
1223 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1225 GNUNET_STATISTICS_update (GSF_stats,
1227 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1229 key = NULL; /* all replies seen! */
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1236 "No further local responses available.\n");
1237 #if INSANE_STATISTICS
1238 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1239 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1240 GNUNET_STATISTICS_update (GSF_stats,
1242 ("# requested DBLOCK or IBLOCK not found"), 1,
1245 goto check_error_and_continue;
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1249 GNUNET_h2s (key), type, (unsigned long long) uid);
1250 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Found ONDEMAND block, performing on-demand encoding\n");
1254 GNUNET_STATISTICS_update (GSF_stats,
1256 ("# on-demand blocks matched requests"), 1,
1258 pr->qe_start = GNUNET_TIME_absolute_get ();
1260 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1261 &odc_warn_delay_task, pr);
1263 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1264 anonymity, expiration, uid,
1265 &process_local_reply, pr))
1267 GNUNET_STATISTICS_update (GSF_stats,
1269 ("# on-demand lookups performed successfully"),
1271 return; /* we're done */
1273 GNUNET_STATISTICS_update (GSF_stats,
1274 gettext_noop ("# on-demand lookups failed"), 1,
1276 GNUNET_SCHEDULER_cancel (pr->warn_task);
1278 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1279 &warn_delay_task, pr);
1281 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1282 &pr->public_data.query,
1283 pr->public_data.type ==
1284 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1285 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1287 (GSF_PRO_PRIORITY_UNLIMITED &
1288 pr->public_data.options)) ? UINT_MAX : 1
1289 /* queue priority */ ,
1291 (GSF_PRO_PRIORITY_UNLIMITED &
1292 pr->public_data.options)) ? UINT_MAX :
1293 datastore_queue_size
1294 /* max queue size */ ,
1295 GNUNET_TIME_UNIT_FOREVER_REL,
1296 &process_local_reply, pr);
1298 return; /* we're done */
1299 GNUNET_STATISTICS_update (GSF_stats,
1301 ("# Datastore lookups concluded (error queueing)"),
1303 goto check_error_and_continue;
1305 old_rf = pr->public_data.results_found;
1306 memset (&prq, 0, sizeof (prq));
1308 prq.expiration = expiration;
1311 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1314 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1315 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1316 pr->qe_start = GNUNET_TIME_absolute_get ();
1318 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1319 &warn_delay_task, pr);
1321 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1322 &pr->public_data.query,
1323 pr->public_data.type ==
1324 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1325 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1327 (GSF_PRO_PRIORITY_UNLIMITED &
1328 pr->public_data.options)) ? UINT_MAX : 1
1329 /* queue priority */ ,
1331 (GSF_PRO_PRIORITY_UNLIMITED &
1332 pr->public_data.options)) ? UINT_MAX :
1333 datastore_queue_size
1334 /* max queue size */ ,
1335 GNUNET_TIME_UNIT_FOREVER_REL,
1336 &process_local_reply, pr);
1339 GNUNET_STATISTICS_update (GSF_stats,
1341 ("# Datastore lookups concluded (error queueing)"),
1343 goto check_error_and_continue;
1348 prq.priority = priority;
1349 prq.request_found = GNUNET_NO;
1350 prq.anonymity_level = anonymity;
1351 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1352 GSF_update_datastore_delay_ (pr->public_data.start_time);
1353 process_reply (&prq, key, pr);
1354 pr->local_result = prq.eval;
1355 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1357 GNUNET_STATISTICS_update (GSF_stats,
1359 ("# Datastore lookups concluded (found last result)"),
1361 goto check_error_and_continue;
1363 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1364 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1365 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1368 GNUNET_STATISTICS_update (GSF_stats,
1370 ("# Datastore lookups concluded (load too high)"),
1372 goto check_error_and_continue;
1374 pr->qe_start = GNUNET_TIME_absolute_get ();
1376 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1379 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1380 &pr->public_data.query,
1381 pr->public_data.type ==
1382 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1383 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1385 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1386 public_data.options)) ? UINT_MAX : 1
1387 /* queue priority */ ,
1389 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1390 public_data.options)) ? UINT_MAX :
1391 datastore_queue_size
1392 /* max queue size */ ,
1393 GNUNET_TIME_UNIT_FOREVER_REL,
1394 &process_local_reply, pr);
1395 /* check if we successfully queued another datastore request;
1396 * if so, return, otherwise call our continuation (if we have
1398 check_error_and_continue:
1401 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1403 GNUNET_SCHEDULER_cancel (pr->warn_task);
1404 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1406 if (NULL == (cont = pr->llc_cont))
1407 return; /* no continuation */
1408 pr->llc_cont = NULL;
1409 cont (pr->llc_cont_cls, pr, pr->local_result);
1414 * Is the given target a legitimate peer for forwarding the given request?
1418 * @return GNUNET_YES if this request could be forwarded to the given peer
1421 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1422 const struct GNUNET_PeerIdentity *target)
1424 struct GNUNET_PeerIdentity pi;
1426 if (0 == pr->origin_pid)
1428 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1430 memcmp (&pi, target,
1431 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1437 * Look up the request in the local datastore.
1439 * @param pr the pending request to process
1440 * @param cont function to call at the end
1441 * @param cont_cls closure for cont
1444 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1445 GSF_LocalLookupContinuation cont, void *cont_cls)
1447 GNUNET_assert (NULL == pr->gh);
1448 GNUNET_assert (NULL == pr->llc_cont);
1449 pr->llc_cont = cont;
1450 pr->llc_cont_cls = cont_cls;
1451 pr->qe_start = GNUNET_TIME_absolute_get ();
1453 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1455 #if INSANE_STATISTICS
1456 GNUNET_STATISTICS_update (GSF_stats,
1457 gettext_noop ("# Datastore lookups initiated"), 1,
1461 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1462 &pr->public_data.query,
1463 pr->public_data.type ==
1464 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1465 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1467 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1468 public_data.options)) ? UINT_MAX : 1
1469 /* queue priority */ ,
1471 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1472 public_data.options)) ? UINT_MAX :
1473 datastore_queue_size
1474 /* max queue size */ ,
1475 GNUNET_TIME_UNIT_FOREVER_REL,
1476 &process_local_reply, pr);
1479 GNUNET_STATISTICS_update (GSF_stats,
1481 ("# Datastore lookups concluded (error queueing)"),
1483 GNUNET_SCHEDULER_cancel (pr->warn_task);
1484 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1485 pr->llc_cont = NULL;
1487 cont (cont_cls, pr, pr->local_result);
1493 * Handle P2P "CONTENT" message. Checks that the message is
1494 * well-formed and then checks if there are any pending requests for
1495 * this content and possibly passes it on (to local clients or other
1496 * peers). Does NOT perform migration (content caching at this peer).
1498 * @param cp the other peer involved (sender or receiver, NULL
1499 * for loopback messages where we are both sender and receiver)
1500 * @param message the actual message
1501 * @return GNUNET_OK if the message was well-formed,
1502 * GNUNET_SYSERR if the message was malformed (close connection,
1503 * do not cache under any circumstances)
1506 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1507 const struct GNUNET_MessageHeader *message)
1509 const struct PutMessage *put;
1512 enum GNUNET_BLOCK_Type type;
1513 struct GNUNET_TIME_Absolute expiration;
1514 struct GNUNET_HashCode query;
1515 struct ProcessReplyClosure prq;
1516 struct GNUNET_TIME_Relative block_time;
1518 struct PutMigrationContext *pmc;
1520 msize = ntohs (message->size);
1521 if (msize < sizeof (struct PutMessage))
1523 GNUNET_break_op (0);
1524 return GNUNET_SYSERR;
1526 put = (const struct PutMessage *) message;
1527 dsize = msize - sizeof (struct PutMessage);
1528 type = ntohl (put->type);
1529 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1530 /* do not allow migrated content to live longer than 1 year */
1531 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1533 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1534 return GNUNET_SYSERR;
1536 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1538 GNUNET_break_op (0);
1539 return GNUNET_SYSERR;
1541 GNUNET_STATISTICS_update (GSF_stats,
1542 gettext_noop ("# GAP PUT messages received"), 1,
1544 /* now, lookup 'query' */
1545 prq.data = (const void *) &put[1];
1549 prq.expiration = expiration;
1551 prq.anonymity_level = UINT32_MAX;
1552 prq.request_found = GNUNET_NO;
1553 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1557 GSF_connected_peer_change_preference_ (cp,
1558 CONTENT_BANDWIDTH_VALUE +
1559 1000 * prq.priority);
1560 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1562 if ((GNUNET_YES == active_to_migration) &&
1564 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Replicating result for query `%s' with priority %u\n",
1568 GNUNET_h2s (&query), prq.priority);
1569 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1570 pmc->start = GNUNET_TIME_absolute_get ();
1571 pmc->requested = prq.request_found;
1572 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1573 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1576 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1577 prq.priority, 1 /* anonymity */ ,
1578 0 /* replication */ ,
1579 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1580 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1581 &put_migration_continuation, pmc))
1583 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1586 else if (NULL != cp)
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Choosing not to keep content `%s' (%d/%d)\n",
1590 GNUNET_h2s (&query), active_to_migration,
1591 test_put_load_too_high (prq.priority));
1593 putl = GNUNET_LOAD_get_load (datastore_put_load);
1594 if ( (NULL != cp) &&
1595 (GNUNET_NO == prq.request_found) &&
1596 ( (GNUNET_YES != active_to_migration) ||
1597 (putl > 2.5 * (1 + prq.priority)) ) )
1599 if (GNUNET_YES != active_to_migration)
1600 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1602 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1604 GNUNET_CRYPTO_random_u32
1605 (GNUNET_CRYPTO_QUALITY_WEAK,
1606 (unsigned int) (60000 * putl * putl)));
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1609 (unsigned long long) block_time.rel_value,
1611 active_to_migration,
1612 (GNUNET_NO == prq.request_found));
1613 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1620 * Setup the subsystem.
1623 GSF_pending_request_init_ ()
1625 unsigned long long dqs;
1628 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1629 "MAX_PENDING_REQUESTS",
1630 &max_pending_requests))
1632 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1633 "fs", "MAX_PENDING_REQUESTS");
1636 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
1639 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1640 "fs", "DATASTORE_QUEUE_SIZE");
1643 datastore_queue_size = (unsigned int) dqs;
1645 active_to_migration =
1646 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1647 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1648 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1649 requests_by_expiration_heap =
1650 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1655 * Shutdown the subsystem.
1658 GSF_pending_request_done_ ()
1660 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1661 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1663 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1664 requests_by_expiration_heap = NULL;
1665 GNUNET_LOAD_value_free (datastore_put_load);
1666 datastore_put_load = NULL;
1670 /* end of gnunet-service-fs_pr.c */