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.rpr_head, prq->sender, &last_transmission))
823 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
824 /* pass on to other peers / local clients */
825 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
826 last_transmission, prq->type, prq->data, prq->size);
828 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
829 #if INSANE_STATISTICS
830 GNUNET_STATISTICS_update (GSF_stats,
832 ("# duplicate replies discarded (bloomfilter)"),
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Duplicate response, discarding.\n");
837 return GNUNET_YES; /* duplicate */
838 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
839 return GNUNET_YES; /* wrong namespace */
840 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
843 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
846 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
851 /* update bloomfilter */
852 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
853 GSF_pending_request_update_ (pr, &chash, 1);
854 if (NULL == prq->sender)
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Found result for query `%s' in local datastore\n",
859 GNUNET_STATISTICS_update (GSF_stats,
860 gettext_noop ("# results found locally"), 1,
865 GSF_dht_lookup_ (pr);
867 prq->priority += pr->public_data.original_priority;
868 pr->public_data.priority = 0;
869 pr->public_data.original_priority = 0;
870 pr->public_data.results_found++;
871 prq->request_found = GNUNET_YES;
872 /* finally, pass on to other peer / local client */
873 if (!GSF_request_plan_reference_get_last_transmission_ (pr->public_data.rpr_head, prq->sender, &last_transmission))
874 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
875 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
876 last_transmission, prq->type, prq->data, prq->size);
882 * Context for the 'put_migration_continuation'.
884 struct PutMigrationContext
888 * Start time for the operation.
890 struct GNUNET_TIME_Absolute start;
895 struct GNUNET_PeerIdentity origin;
898 * GNUNET_YES if we had a matching request for this block,
906 * Continuation called to notify client about result of the
910 * @param success GNUNET_SYSERR on failure
911 * @param min_expiration minimum expiration time required for content to be stored
912 * @param msg NULL on success, otherwise an error message
915 put_migration_continuation (void *cls, int success,
916 struct GNUNET_TIME_Absolute min_expiration,
919 struct PutMigrationContext *pmc = cls;
920 struct GSF_ConnectedPeer *cp;
921 struct GNUNET_TIME_Relative mig_pause;
922 struct GSF_PeerPerformanceData *ppd;
924 if (NULL != datastore_put_load)
926 if (GNUNET_SYSERR != success)
928 GNUNET_LOAD_update (datastore_put_load,
929 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
933 /* on queue failure / timeout, increase the put load dramatically */
934 GNUNET_LOAD_update (datastore_put_load,
935 GNUNET_TIME_UNIT_MINUTES.rel_value);
938 cp = GSF_peer_get_ (&pmc->origin);
939 if (GNUNET_OK == success)
943 ppd = GSF_get_peer_performance_data_ (cp);
944 ppd->migration_delay.rel_value /= 2;
949 if ( (GNUNET_NO == success) &&
950 (GNUNET_NO == pmc->requested) &&
953 ppd = GSF_get_peer_performance_data_ (cp);
954 if (min_expiration.abs_value > 0)
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Asking to stop migration for %s because datastore is full\n",
958 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
959 GSF_block_peer_migration_ (cp, min_expiration);
963 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
964 ppd->migration_delay);
965 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
966 ppd->migration_delay);
967 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
968 ppd->migration_delay.rel_value);
969 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971 "Replicated content already exists locally, asking to stop migration for %s\n",
972 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
973 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
977 GNUNET_STATISTICS_update (GSF_stats,
978 gettext_noop ("# Datastore `PUT' failures"), 1,
984 * Test if the DATABASE (PUT) load on this peer is too high
985 * to even consider processing the query at
988 * @return GNUNET_YES if the load is too high to do anything (load high)
989 * GNUNET_NO to process normally (load normal or low)
992 test_put_load_too_high (uint32_t priority)
996 if (NULL == datastore_put_load)
998 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
999 return GNUNET_NO; /* very fast */
1000 ld = GNUNET_LOAD_get_load (datastore_put_load);
1001 if (ld < 2.0 * (1 + priority))
1003 GNUNET_STATISTICS_update (GSF_stats,
1005 ("# storage requests dropped due to high load"), 1,
1012 * Iterator called on each result obtained for a DHT
1013 * operation that expects a reply
1015 * @param cls closure
1016 * @param exp when will this value expire
1017 * @param key key of the result
1018 * @param get_path peers on reply path (or NULL if not recorded)
1019 * @param get_path_length number of entries in get_path
1020 * @param put_path peers on the PUT path (or NULL if not recorded)
1021 * @param put_path_length number of entries in get_path
1022 * @param type type of the result
1023 * @param size number of bytes in data
1024 * @param data pointer to the result data
1027 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1028 const struct GNUNET_HashCode * key,
1029 const struct GNUNET_PeerIdentity *get_path,
1030 unsigned int get_path_length,
1031 const struct GNUNET_PeerIdentity *put_path,
1032 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1033 size_t size, const void *data)
1035 struct GSF_PendingRequest *pr = cls;
1036 struct ProcessReplyClosure prq;
1037 struct PutMigrationContext *pmc;
1039 GNUNET_STATISTICS_update (GSF_stats,
1040 gettext_noop ("# Replies received from DHT"), 1,
1042 memset (&prq, 0, sizeof (prq));
1044 prq.expiration = exp;
1045 /* do not allow migrated content to live longer than 1 year */
1046 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1050 process_reply (&prq, key, pr);
1051 if ((GNUNET_YES == active_to_migration) &&
1052 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Replicating result for query `%s' with priority %u\n",
1056 GNUNET_h2s (key), prq.priority);
1057 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1058 pmc->start = GNUNET_TIME_absolute_get ();
1059 pmc->requested = GNUNET_YES;
1061 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1063 0 /* replication */ ,
1064 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1065 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1066 &put_migration_continuation, pmc))
1068 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1075 * Consider looking up the data in the DHT (anonymity-level permitting).
1077 * @param pr the pending request to process
1080 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1084 struct GNUNET_PeerIdentity pi;
1085 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1087 if (0 != pr->public_data.anonymity_level)
1091 GNUNET_DHT_get_stop (pr->gh);
1096 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1099 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1100 xquery_size = sizeof (struct GNUNET_HashCode);
1102 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1104 GNUNET_assert (0 != pr->sender_pid);
1105 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1106 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1107 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1110 GNUNET_DHT_get_start (GSF_dht,
1111 pr->public_data.type, &pr->public_data.query,
1112 5 /* DEFAULT_GET_REPLICATION */ ,
1113 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1114 /* FIXME: can no longer pass pr->bf/pr->mingle... */
1115 xquery, xquery_size, &handle_dht_reply, pr);
1120 * Task that issues a warning if the datastore lookup takes too long.
1122 * @param cls the 'struct GSF_PendingRequest'
1123 * @param tc task context
1126 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1128 struct GSF_PendingRequest *pr = cls;
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1131 _("Datastore lookup already took %s!\n"),
1132 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1134 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1140 * Task that issues a warning if the datastore lookup takes too long.
1142 * @param cls the 'struct GSF_PendingRequest'
1143 * @param tc task context
1146 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1148 struct GSF_PendingRequest *pr = cls;
1150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1151 _("On-demand lookup already took %s!\n"),
1152 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1154 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1155 &odc_warn_delay_task, pr);
1160 * We're processing (local) results for a search request
1161 * from another peer. Pass applicable results to the
1162 * peer and if we are done either clean up (operation
1163 * complete) or forward to other peers (more results possible).
1165 * @param cls our closure (struct PendingRequest)
1166 * @param key key for the content
1167 * @param size number of bytes in data
1168 * @param data content stored
1169 * @param type type of the content
1170 * @param priority priority of the content
1171 * @param anonymity anonymity-level for the content
1172 * @param expiration expiration time for the content
1173 * @param uid unique identifier for the datum;
1174 * maybe 0 if no unique identifier is available
1177 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1178 const void *data, enum GNUNET_BLOCK_Type type,
1179 uint32_t priority, uint32_t anonymity,
1180 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1182 struct GSF_PendingRequest *pr = cls;
1183 GSF_LocalLookupContinuation cont;
1184 struct ProcessReplyClosure prq;
1185 struct GNUNET_HashCode query;
1186 unsigned int old_rf;
1188 GNUNET_SCHEDULER_cancel (pr->warn_task);
1189 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1195 #if INSANE_STATISTICS
1196 GNUNET_STATISTICS_update (GSF_stats,
1198 ("# Datastore lookups concluded (no results)"),
1202 if (GNUNET_NO == pr->have_first_uid)
1204 pr->first_uid = uid;
1205 pr->have_first_uid = 1;
1209 if ((uid == pr->first_uid) && (key != NULL))
1211 GNUNET_STATISTICS_update (GSF_stats,
1213 ("# Datastore lookups concluded (seen all)"),
1215 key = NULL; /* all replies seen! */
1217 pr->have_first_uid++;
1218 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1220 GNUNET_STATISTICS_update (GSF_stats,
1222 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1224 key = NULL; /* all replies seen! */
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1231 "No further local responses available.\n");
1232 #if INSANE_STATISTICS
1233 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1234 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1235 GNUNET_STATISTICS_update (GSF_stats,
1237 ("# requested DBLOCK or IBLOCK not found"), 1,
1240 goto check_error_and_continue;
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1243 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1244 GNUNET_h2s (key), type, (unsigned long long) uid);
1245 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Found ONDEMAND block, performing on-demand encoding\n");
1249 GNUNET_STATISTICS_update (GSF_stats,
1251 ("# on-demand blocks matched requests"), 1,
1253 pr->qe_start = GNUNET_TIME_absolute_get ();
1255 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1256 &odc_warn_delay_task, pr);
1258 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1259 anonymity, expiration, uid,
1260 &process_local_reply, pr))
1262 GNUNET_STATISTICS_update (GSF_stats,
1264 ("# on-demand lookups performed successfully"),
1266 return; /* we're done */
1268 GNUNET_STATISTICS_update (GSF_stats,
1269 gettext_noop ("# on-demand lookups failed"), 1,
1271 GNUNET_SCHEDULER_cancel (pr->warn_task);
1273 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1274 &warn_delay_task, pr);
1276 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1277 &pr->public_data.query,
1278 pr->public_data.type ==
1279 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1280 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1282 (GSF_PRO_PRIORITY_UNLIMITED &
1283 pr->public_data.options)) ? UINT_MAX : 1
1284 /* queue priority */ ,
1286 (GSF_PRO_PRIORITY_UNLIMITED &
1287 pr->public_data.options)) ? UINT_MAX :
1288 datastore_queue_size
1289 /* max queue size */ ,
1290 GNUNET_TIME_UNIT_FOREVER_REL,
1291 &process_local_reply, pr);
1293 return; /* we're done */
1294 GNUNET_STATISTICS_update (GSF_stats,
1296 ("# Datastore lookups concluded (error queueing)"),
1298 goto check_error_and_continue;
1300 old_rf = pr->public_data.results_found;
1301 memset (&prq, 0, sizeof (prq));
1303 prq.expiration = expiration;
1306 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1309 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1310 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1311 pr->qe_start = GNUNET_TIME_absolute_get ();
1313 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1314 &warn_delay_task, pr);
1316 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1317 &pr->public_data.query,
1318 pr->public_data.type ==
1319 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1320 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1322 (GSF_PRO_PRIORITY_UNLIMITED &
1323 pr->public_data.options)) ? UINT_MAX : 1
1324 /* queue priority */ ,
1326 (GSF_PRO_PRIORITY_UNLIMITED &
1327 pr->public_data.options)) ? UINT_MAX :
1328 datastore_queue_size
1329 /* max queue size */ ,
1330 GNUNET_TIME_UNIT_FOREVER_REL,
1331 &process_local_reply, pr);
1334 GNUNET_STATISTICS_update (GSF_stats,
1336 ("# Datastore lookups concluded (error queueing)"),
1338 goto check_error_and_continue;
1343 prq.priority = priority;
1344 prq.request_found = GNUNET_NO;
1345 prq.anonymity_level = anonymity;
1346 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1347 GSF_update_datastore_delay_ (pr->public_data.start_time);
1348 process_reply (&prq, key, pr);
1349 pr->local_result = prq.eval;
1350 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1352 GNUNET_STATISTICS_update (GSF_stats,
1354 ("# Datastore lookups concluded (found last result)"),
1356 goto check_error_and_continue;
1358 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1359 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1360 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1363 GNUNET_STATISTICS_update (GSF_stats,
1365 ("# Datastore lookups concluded (load too high)"),
1367 goto check_error_and_continue;
1369 pr->qe_start = GNUNET_TIME_absolute_get ();
1371 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1374 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1375 &pr->public_data.query,
1376 pr->public_data.type ==
1377 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1378 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1380 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1381 public_data.options)) ? UINT_MAX : 1
1382 /* queue priority */ ,
1384 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1385 public_data.options)) ? UINT_MAX :
1386 datastore_queue_size
1387 /* max queue size */ ,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1389 &process_local_reply, pr);
1390 /* check if we successfully queued another datastore request;
1391 * if so, return, otherwise call our continuation (if we have
1393 check_error_and_continue:
1396 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1398 GNUNET_SCHEDULER_cancel (pr->warn_task);
1399 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1401 if (NULL == (cont = pr->llc_cont))
1402 return; /* no continuation */
1403 pr->llc_cont = NULL;
1404 cont (pr->llc_cont_cls, pr, pr->local_result);
1409 * Is the given target a legitimate peer for forwarding the given request?
1413 * @return GNUNET_YES if this request could be forwarded to the given peer
1416 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1417 const struct GNUNET_PeerIdentity *target)
1419 struct GNUNET_PeerIdentity pi;
1421 if (0 == pr->origin_pid)
1423 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1425 memcmp (&pi, target,
1426 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1432 * Look up the request in the local datastore.
1434 * @param pr the pending request to process
1435 * @param cont function to call at the end
1436 * @param cont_cls closure for cont
1439 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1440 GSF_LocalLookupContinuation cont, void *cont_cls)
1442 GNUNET_assert (NULL == pr->gh);
1443 GNUNET_assert (NULL == pr->llc_cont);
1444 pr->llc_cont = cont;
1445 pr->llc_cont_cls = cont_cls;
1446 pr->qe_start = GNUNET_TIME_absolute_get ();
1448 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1450 #if INSANE_STATISTICS
1451 GNUNET_STATISTICS_update (GSF_stats,
1452 gettext_noop ("# Datastore lookups initiated"), 1,
1456 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1457 &pr->public_data.query,
1458 pr->public_data.type ==
1459 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1460 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1462 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1463 public_data.options)) ? UINT_MAX : 1
1464 /* queue priority */ ,
1466 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1467 public_data.options)) ? UINT_MAX :
1468 datastore_queue_size
1469 /* max queue size */ ,
1470 GNUNET_TIME_UNIT_FOREVER_REL,
1471 &process_local_reply, pr);
1474 GNUNET_STATISTICS_update (GSF_stats,
1476 ("# Datastore lookups concluded (error queueing)"),
1478 GNUNET_SCHEDULER_cancel (pr->warn_task);
1479 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1480 pr->llc_cont = NULL;
1482 cont (cont_cls, pr, pr->local_result);
1488 * Handle P2P "CONTENT" message. Checks that the message is
1489 * well-formed and then checks if there are any pending requests for
1490 * this content and possibly passes it on (to local clients or other
1491 * peers). Does NOT perform migration (content caching at this peer).
1493 * @param cp the other peer involved (sender or receiver, NULL
1494 * for loopback messages where we are both sender and receiver)
1495 * @param message the actual message
1496 * @return GNUNET_OK if the message was well-formed,
1497 * GNUNET_SYSERR if the message was malformed (close connection,
1498 * do not cache under any circumstances)
1501 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1502 const struct GNUNET_MessageHeader *message)
1504 const struct PutMessage *put;
1507 enum GNUNET_BLOCK_Type type;
1508 struct GNUNET_TIME_Absolute expiration;
1509 struct GNUNET_HashCode query;
1510 struct ProcessReplyClosure prq;
1511 struct GNUNET_TIME_Relative block_time;
1513 struct PutMigrationContext *pmc;
1515 msize = ntohs (message->size);
1516 if (msize < sizeof (struct PutMessage))
1518 GNUNET_break_op (0);
1519 return GNUNET_SYSERR;
1521 put = (const struct PutMessage *) message;
1522 dsize = msize - sizeof (struct PutMessage);
1523 type = ntohl (put->type);
1524 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1525 /* do not allow migrated content to live longer than 1 year */
1526 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1528 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1529 return GNUNET_SYSERR;
1531 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1533 GNUNET_break_op (0);
1534 return GNUNET_SYSERR;
1536 GNUNET_STATISTICS_update (GSF_stats,
1537 gettext_noop ("# GAP PUT messages received"), 1,
1539 /* now, lookup 'query' */
1540 prq.data = (const void *) &put[1];
1544 prq.expiration = expiration;
1546 prq.anonymity_level = UINT32_MAX;
1547 prq.request_found = GNUNET_NO;
1548 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1552 GSF_connected_peer_change_preference_ (cp,
1553 CONTENT_BANDWIDTH_VALUE +
1554 1000 * prq.priority);
1555 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1557 if ((GNUNET_YES == active_to_migration) &&
1559 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Replicating result for query `%s' with priority %u\n",
1563 GNUNET_h2s (&query), prq.priority);
1564 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1565 pmc->start = GNUNET_TIME_absolute_get ();
1566 pmc->requested = prq.request_found;
1567 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1568 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1571 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1572 prq.priority, 1 /* anonymity */ ,
1573 0 /* replication */ ,
1574 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1575 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1576 &put_migration_continuation, pmc))
1578 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1581 else if (NULL != cp)
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Choosing not to keep content `%s' (%d/%d)\n",
1585 GNUNET_h2s (&query), active_to_migration,
1586 test_put_load_too_high (prq.priority));
1588 putl = GNUNET_LOAD_get_load (datastore_put_load);
1589 if ( (NULL != cp) &&
1590 (GNUNET_NO == prq.request_found) &&
1591 ( (GNUNET_YES != active_to_migration) ||
1592 (putl > 2.5 * (1 + prq.priority)) ) )
1594 if (GNUNET_YES != active_to_migration)
1595 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1597 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1599 GNUNET_CRYPTO_random_u32
1600 (GNUNET_CRYPTO_QUALITY_WEAK,
1601 (unsigned int) (60000 * putl * putl)));
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1604 (unsigned long long) block_time.rel_value,
1606 active_to_migration,
1607 (GNUNET_NO == prq.request_found));
1608 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1615 * Setup the subsystem.
1618 GSF_pending_request_init_ ()
1620 unsigned long long dqs;
1623 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1624 "MAX_PENDING_REQUESTS",
1625 &max_pending_requests))
1627 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1628 "fs", "MAX_PENDING_REQUESTS");
1631 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "fs", "DATASTORE_QUEUE_SIZE",
1634 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1635 "fs", "DATASTORE_QUEUE_SIZE");
1638 datastore_queue_size = (unsigned int) dqs;
1640 active_to_migration =
1641 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1642 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1643 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1644 requests_by_expiration_heap =
1645 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1650 * Shutdown the subsystem.
1653 GSF_pending_request_done_ ()
1655 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1656 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1658 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1659 requests_by_expiration_heap = NULL;
1660 GNUNET_LOAD_value_free (datastore_put_load);
1661 datastore_put_load = NULL;
1665 /* end of gnunet-service-fs_pr.c */