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)
57 struct GSF_PendingRequest
60 * Public data for the request.
62 struct GSF_PendingRequestData public_data;
65 * Function to call if we encounter a reply.
67 GSF_PendingRequestReplyHandler rh;
75 * Array of hash codes of replies we've already seen.
77 struct GNUNET_HashCode *replies_seen;
80 * Bloomfilter masking replies we've already seen.
82 struct GNUNET_CONTAINER_BloomFilter *bf;
85 * Entry for this pending request in the expiration heap, or NULL.
87 struct GNUNET_CONTAINER_HeapNode *hnode;
90 * Datastore queue entry for this request (or NULL for none).
92 struct GNUNET_DATASTORE_QueueEntry *qe;
95 * DHT request handle for this request (or NULL for none).
97 struct GNUNET_DHT_GetHandle *gh;
100 * Function to call upon completion of the local get
101 * request, or NULL for none.
103 GSF_LocalLookupContinuation llc_cont;
106 * Closure for llc_cont.
111 * Last result from the local datastore lookup evaluation.
113 enum GNUNET_BLOCK_EvaluationResult local_result;
116 * Identity of the peer that we should use for the 'sender'
117 * (recipient of the response) when forwarding (0 for none).
119 GNUNET_PEER_Id sender_pid;
122 * Identity of the peer that we should never forward this query
123 * to since it originated this query (0 for none).
125 GNUNET_PEER_Id origin_pid;
128 * Time we started the last datastore lookup.
130 struct GNUNET_TIME_Absolute qe_start;
133 * Task that warns us if the local datastore lookup takes too long.
135 GNUNET_SCHEDULER_TaskIdentifier warn_task;
138 * Current offset for querying our local datastore for results.
139 * Starts at a random value, incremented until we get the same
140 * UID again (detected using 'first_uid'), which is then used
141 * to termiante the iteration.
143 uint64_t local_result_offset;
146 * Unique ID of the first result from the local datastore;
147 * used to detect wrap-around of the offset.
152 * Number of valid entries in the 'replies_seen' array.
154 unsigned int replies_seen_count;
157 * Length of the 'replies_seen' array.
159 unsigned int replies_seen_size;
162 * Mingle value we currently use for the bf.
167 * Do we have a first UID yet?
169 unsigned int have_first_uid;
175 * All pending requests, ordered by the query. Entries
176 * are of type 'struct GSF_PendingRequest*'.
178 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
182 * Datastore 'PUT' load tracking.
184 static struct GNUNET_LOAD_Value *datastore_put_load;
188 * Are we allowed to migrate content to this peer.
190 static int active_to_migration;
194 * Size of the datastore queue we assume for common requests.
195 * Determined based on the network quota.
197 static unsigned int datastore_queue_size;
200 * Heap with the request that will expire next at the top. Contains
201 * pointers of type "struct PendingRequest*"; these will *also* be
202 * aliased from the "requests_by_peer" data structures and the
203 * "requests_by_query" table. Note that requests from our clients
204 * don't expire and are thus NOT in the "requests_by_expiration"
205 * (or the "requests_by_peer" tables).
207 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
211 * Maximum number of requests (from other peers, overall) that we're
212 * willing to have pending at any given point in time. Can be changed
213 * via the configuration file (32k is just the default).
215 static unsigned long long max_pending_requests = (32 * 1024);
220 * Recalculate our bloom filter for filtering replies. This function
221 * will create a new bloom filter from scratch, so it should only be
222 * called if we have no bloomfilter at all (and hence can create a
223 * fresh one of minimal size without problems) OR if our peer is the
224 * initiator (in which case we may resize to larger than mimimum size).
226 * @param pr request for which the BF is to be recomputed
229 refresh_bloomfilter (struct GSF_PendingRequest *pr)
232 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
234 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
236 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
237 pr->replies_seen_count);
242 * Create a new pending request.
244 * @param options request options
245 * @param type type of the block that is being requested
246 * @param query key for the lookup
247 * @param namespace namespace to lookup, NULL for no namespace
248 * @param target preferred target for the request, NULL for none
249 * @param bf_data raw data for bloom filter for known replies, can be NULL
250 * @param bf_size number of bytes in bf_data
251 * @param mingle mingle value for bf
252 * @param anonymity_level desired anonymity level
253 * @param priority maximum outgoing cummulative request priority to use
254 * @param ttl current time-to-live for the request
255 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
256 * @param origin_pid peer ID of origin of query (do not loop back)
257 * @param replies_seen hash codes of known local replies
258 * @param replies_seen_count size of the 'replies_seen' array
259 * @param rh handle to call when we get a reply
260 * @param rh_cls closure for rh
261 * @return handle for the new pending request
263 struct GSF_PendingRequest *
264 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
265 enum GNUNET_BLOCK_Type type,
266 const struct GNUNET_HashCode * query,
267 const struct GNUNET_HashCode * namespace,
268 const struct GNUNET_PeerIdentity *target,
269 const char *bf_data, size_t bf_size,
270 uint32_t mingle, uint32_t anonymity_level,
271 uint32_t priority, int32_t ttl,
272 GNUNET_PEER_Id sender_pid,
273 GNUNET_PEER_Id origin_pid,
274 const struct GNUNET_HashCode * replies_seen,
275 unsigned int replies_seen_count,
276 GSF_PendingRequestReplyHandler rh, void *rh_cls)
278 struct GSF_PendingRequest *pr;
279 struct GSF_PendingRequest *dpr;
281 struct GNUNET_HashCode *eptr;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Creating request handle for `%s' of type %d\n",
285 GNUNET_h2s (query), type);
286 GNUNET_STATISTICS_update (GSF_stats,
287 gettext_noop ("# Pending requests created"), 1,
290 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
291 extra += sizeof (struct GNUNET_HashCode);
293 extra += sizeof (struct GNUNET_PeerIdentity);
294 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
295 pr->local_result_offset =
296 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
297 pr->public_data.query = *query;
298 eptr = (struct GNUNET_HashCode *) &pr[1];
299 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
301 GNUNET_assert (NULL != namespace);
302 pr->public_data.namespace = eptr;
303 memcpy (eptr, namespace, sizeof (struct GNUNET_HashCode));
308 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
309 memcpy (eptr, target, sizeof (struct GNUNET_PeerIdentity));
311 pr->public_data.anonymity_level = anonymity_level;
312 pr->public_data.priority = priority;
313 pr->public_data.original_priority = priority;
314 pr->public_data.options = options;
315 pr->public_data.type = type;
316 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
317 pr->sender_pid = sender_pid;
318 pr->origin_pid = origin_pid;
321 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
323 pr->public_data.ttl =
324 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
325 (GNUNET_TIME_UNIT_SECONDS,
328 pr->public_data.ttl =
329 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
330 GNUNET_TIME_relative_multiply
331 (GNUNET_TIME_UNIT_SECONDS,
333 if (replies_seen_count > 0)
335 pr->replies_seen_size = replies_seen_count;
337 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
338 memcpy (pr->replies_seen, replies_seen,
339 replies_seen_count * sizeof (struct GNUNET_HashCode));
340 pr->replies_seen_count = replies_seen_count;
345 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size,
346 GNUNET_CONSTANTS_BLOOMFILTER_K);
349 else if ((replies_seen_count > 0) &&
350 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
352 refresh_bloomfilter (pr);
354 GNUNET_CONTAINER_multihashmap_put (pr_map,
355 &pr->public_data.query, pr,
356 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
357 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
360 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
361 pr->public_data.ttl.abs_value);
362 /* make sure we don't track too many requests */
363 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
364 max_pending_requests)
366 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
367 GNUNET_assert (dpr != NULL);
369 break; /* let the request live briefly... */
371 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
372 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_ABS,
373 GNUNET_BLOCK_TYPE_ANY, NULL, 0);
374 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
377 GNUNET_STATISTICS_update (GSF_stats,
378 gettext_noop ("# Pending requests active"), 1,
384 * Obtain the public data associated with a pending request
386 * @param pr pending request
387 * @return associated public data
389 struct GSF_PendingRequestData *
390 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
392 return &pr->public_data;
397 * Test if two pending requests are compatible (would generate
398 * the same query modulo filters and should thus be processed
401 * @param pra a pending request
402 * @param prb another pending request
403 * @return GNUNET_OK if the requests are compatible
406 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
407 struct GSF_PendingRequest *prb)
409 if ((pra->public_data.type != prb->public_data.type) ||
411 memcmp (&pra->public_data.query, &prb->public_data.query,
412 sizeof (struct GNUNET_HashCode))) ||
413 ((pra->public_data.type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
415 memcmp (pra->public_data.namespace,
416 prb->public_data.namespace,
417 sizeof (struct GNUNET_HashCode)))))
425 * Update a given pending request with additional replies
426 * that have been seen.
428 * @param pr request to update
429 * @param replies_seen hash codes of replies that we've seen
430 * @param replies_seen_count size of the replies_seen array
433 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
434 const struct GNUNET_HashCode * replies_seen,
435 unsigned int replies_seen_count)
438 struct GNUNET_HashCode mhash;
440 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
441 return; /* integer overflow */
442 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
444 /* we're responsible for the BF, full refresh */
445 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
446 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
447 replies_seen_count + pr->replies_seen_count);
448 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
449 sizeof (struct GNUNET_HashCode) * replies_seen_count);
450 pr->replies_seen_count += replies_seen_count;
451 refresh_bloomfilter (pr);
457 /* we're not the initiator, but the initiator did not give us
458 * any bloom-filter, so we need to create one on-the-fly */
460 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
462 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, replies_seen,
467 for (i = 0; i < pr->replies_seen_count; i++)
469 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
470 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
478 * Generate the message corresponding to the given pending request for
479 * transmission to other peers (or at least determine its size).
481 * @param pr request to generate the message for
482 * @param buf_size number of bytes available in buf
483 * @param buf where to copy the message (can be NULL)
484 * @return number of bytes needed (if > buf_size) or used
487 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
488 size_t buf_size, void *buf)
490 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
491 struct GetMessage *gm;
492 struct GNUNET_HashCode *ext;
498 struct GNUNET_TIME_Absolute now;
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Building request message for `%s' of type %d\n",
505 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
508 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
509 if ((!do_route) && (pr->sender_pid == 0))
512 do_route = GNUNET_YES;
516 bm |= GET_MESSAGE_BIT_RETURN_TO;
519 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
521 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
524 if (NULL != pr->public_data.target)
526 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
529 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
530 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_HashCode);
531 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
532 if (buf_size < msize)
534 gm = (struct GetMessage *) lbuf;
535 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
536 gm->header.size = htons (msize);
537 gm->type = htonl (pr->public_data.type);
540 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
541 pr->public_data.priority + 1);
544 pr->public_data.priority -= prio;
545 pr->public_data.num_transmissions++;
546 pr->public_data.respect_offered += prio;
547 gm->priority = htonl (prio);
548 now = GNUNET_TIME_absolute_get ();
549 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
550 gm->ttl = htonl (ttl / 1000);
551 gm->filter_mutator = htonl (pr->mingle);
552 gm->hash_bitmap = htonl (bm);
553 gm->query = pr->public_data.query;
554 ext = (struct GNUNET_HashCode *) & gm[1];
557 GNUNET_PEER_resolve (pr->sender_pid,
558 (struct GNUNET_PeerIdentity *) &ext[k++]);
559 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
560 memcpy (&ext[k++], pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
561 if (NULL != pr->public_data.target)
563 pr->public_data.target,
564 sizeof (struct GNUNET_PeerIdentity));
566 GNUNET_assert (GNUNET_SYSERR !=
567 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
570 memcpy (buf, gm, msize);
576 * Iterator to free pending requests.
578 * @param cls closure, unused
579 * @param key current key code
580 * @param value value in the hash map (pending request)
581 * @return GNUNET_YES (we should continue to iterate)
584 clean_request (void *cls, const struct GNUNET_HashCode * key, void *value)
586 struct GSF_PendingRequest *pr = value;
587 GSF_LocalLookupContinuation cont;
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
591 if (NULL != (cont = pr->llc_cont))
594 cont (pr->llc_cont_cls, pr, pr->local_result);
596 GSF_plan_notify_request_done_ (pr);
597 GNUNET_free_non_null (pr->replies_seen);
600 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
603 GNUNET_PEER_change_rc (pr->sender_pid, -1);
605 GNUNET_PEER_change_rc (pr->origin_pid, -1);
607 if (NULL != pr->hnode)
609 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
614 GNUNET_DATASTORE_cancel (pr->qe);
619 GNUNET_DHT_get_stop (pr->gh);
622 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
624 GNUNET_SCHEDULER_cancel (pr->warn_task);
625 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
627 GNUNET_assert (GNUNET_OK ==
628 GNUNET_CONTAINER_multihashmap_remove (pr_map,
629 &pr->public_data.query,
631 GNUNET_STATISTICS_update (GSF_stats,
632 gettext_noop ("# Pending requests active"), -1,
640 * Explicitly cancel a pending request.
642 * @param pr request to cancel
643 * @param full_cleanup fully purge the request
646 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
648 GSF_LocalLookupContinuation cont;
651 return; /* already cleaned up! */
652 if (GNUNET_YES != full_cleanup)
654 /* make request inactive (we're no longer interested in more results),
655 * but do NOT remove from our data-structures, we still need it there
656 * to prevent the request from looping */
658 if (NULL != (cont = pr->llc_cont))
661 cont (pr->llc_cont_cls, pr, pr->local_result);
663 GSF_plan_notify_request_done_ (pr);
666 GNUNET_DATASTORE_cancel (pr->qe);
671 GNUNET_DHT_get_stop (pr->gh);
674 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
676 GNUNET_SCHEDULER_cancel (pr->warn_task);
677 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
681 GNUNET_assert (GNUNET_YES ==
682 clean_request (NULL, &pr->public_data.query, pr));
687 * Iterate over all pending requests.
689 * @param it function to call for each request
690 * @param cls closure for it
693 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
695 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
696 (GNUNET_CONTAINER_HashMapIterator) it,
704 * Closure for "process_reply" function.
706 struct ProcessReplyClosure
709 * The data for the reply.
714 * Who gave us this reply? NULL for local host (or DHT)
716 struct GSF_ConnectedPeer *sender;
719 * When the reply expires.
721 struct GNUNET_TIME_Absolute expiration;
731 enum GNUNET_BLOCK_Type type;
734 * How much was this reply worth to us?
739 * Anonymity requirements for this reply.
741 uint32_t anonymity_level;
744 * Evaluation result (returned).
746 enum GNUNET_BLOCK_EvaluationResult eval;
749 * Did we find a matching request?
756 * Update the performance data for the sender (if any) since
757 * the sender successfully answered one of our queries.
759 * @param prq information about the sender
760 * @param pr request that was satisfied
763 update_request_performance_data (struct ProcessReplyClosure *prq,
764 struct GSF_PendingRequest *pr)
766 if (prq->sender == NULL)
768 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
774 * We have received a reply; handle it!
776 * @param cls response (struct ProcessReplyClosure)
777 * @param key our query
778 * @param value value in the hash map (info about the query)
779 * @return GNUNET_YES (we should continue to iterate)
782 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
784 struct ProcessReplyClosure *prq = cls;
785 struct GSF_PendingRequest *pr = value;
786 struct GNUNET_HashCode chash;
787 struct GNUNET_TIME_Absolute last_transmission;
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Matched result (type %u) for query `%s' with pending request\n",
793 (unsigned int) prq->type, GNUNET_h2s (key));
794 GNUNET_STATISTICS_update (GSF_stats,
795 gettext_noop ("# replies received and matched"), 1,
798 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
799 pr->public_data.namespace,
801 GNUNET_BLOCK_TYPE_FS_SBLOCK) ?
802 sizeof (struct GNUNET_HashCode) : 0, prq->data,
806 case GNUNET_BLOCK_EVALUATION_OK_MORE:
807 update_request_performance_data (prq, pr);
809 case GNUNET_BLOCK_EVALUATION_OK_LAST:
810 /* short cut: stop processing early, no BF-update, etc. */
811 update_request_performance_data (prq, pr);
812 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
813 GNUNET_TIME_absolute_get_duration (pr->
814 public_data.start_time).rel_value);
815 if (!GSF_request_plan_reference_get_last_transmission_ (pr->public_data.rpr_head, prq->sender, &last_transmission))
816 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
817 /* pass on to other peers / local clients */
818 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
819 last_transmission, prq->type, prq->data, prq->size);
821 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
822 GNUNET_STATISTICS_update (GSF_stats,
824 ("# duplicate replies discarded (bloomfilter)"),
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Duplicate response, discarding.\n");
828 return GNUNET_YES; /* duplicate */
829 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
830 return GNUNET_YES; /* wrong namespace */
831 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
834 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
837 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
842 /* update bloomfilter */
843 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
844 GSF_pending_request_update_ (pr, &chash, 1);
845 if (NULL == prq->sender)
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Found result for query `%s' in local datastore\n",
850 GNUNET_STATISTICS_update (GSF_stats,
851 gettext_noop ("# results found locally"), 1,
856 GSF_dht_lookup_ (pr);
858 prq->priority += pr->public_data.original_priority;
859 pr->public_data.priority = 0;
860 pr->public_data.original_priority = 0;
861 pr->public_data.results_found++;
862 prq->request_found = GNUNET_YES;
863 /* finally, pass on to other peer / local client */
864 if (!GSF_request_plan_reference_get_last_transmission_ (pr->public_data.rpr_head, prq->sender, &last_transmission))
865 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
866 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
867 last_transmission, prq->type, prq->data, prq->size);
873 * Context for the 'put_migration_continuation'.
875 struct PutMigrationContext
879 * Start time for the operation.
881 struct GNUNET_TIME_Absolute start;
886 struct GNUNET_PeerIdentity origin;
889 * GNUNET_YES if we had a matching request for this block,
897 * Continuation called to notify client about result of the
901 * @param success GNUNET_SYSERR on failure
902 * @param min_expiration minimum expiration time required for content to be stored
903 * @param msg NULL on success, otherwise an error message
906 put_migration_continuation (void *cls, int success,
907 struct GNUNET_TIME_Absolute min_expiration,
910 struct PutMigrationContext *pmc = cls;
911 struct GSF_ConnectedPeer *cp;
912 struct GNUNET_TIME_Relative mig_pause;
913 struct GSF_PeerPerformanceData *ppd;
915 if (NULL != datastore_put_load)
917 if (GNUNET_SYSERR != success)
919 GNUNET_LOAD_update (datastore_put_load,
920 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
924 /* on queue failure / timeout, increase the put load dramatically */
925 GNUNET_LOAD_update (datastore_put_load,
926 GNUNET_TIME_UNIT_MINUTES.rel_value);
929 cp = GSF_peer_get_ (&pmc->origin);
930 if (GNUNET_OK == success)
934 ppd = GSF_get_peer_performance_data_ (cp);
935 ppd->migration_delay.rel_value /= 2;
940 if ( (GNUNET_NO == success) &&
941 (GNUNET_NO == pmc->requested) &&
944 ppd = GSF_get_peer_performance_data_ (cp);
945 if (min_expiration.abs_value > 0)
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Asking to stop migration for %s because datastore is full\n",
949 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
950 GSF_block_peer_migration_ (cp, min_expiration);
954 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
955 ppd->migration_delay);
956 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
957 ppd->migration_delay);
958 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
959 ppd->migration_delay.rel_value);
960 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Replicated content already exists locally, asking to stop migration for %s\n",
963 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
964 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
968 GNUNET_STATISTICS_update (GSF_stats,
969 gettext_noop ("# Datastore `PUT' failures"), 1,
975 * Test if the DATABASE (PUT) load on this peer is too high
976 * to even consider processing the query at
979 * @return GNUNET_YES if the load is too high to do anything (load high)
980 * GNUNET_NO to process normally (load normal or low)
983 test_put_load_too_high (uint32_t priority)
987 if (NULL == datastore_put_load)
989 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
990 return GNUNET_NO; /* very fast */
991 ld = GNUNET_LOAD_get_load (datastore_put_load);
992 if (ld < 2.0 * (1 + priority))
994 GNUNET_STATISTICS_update (GSF_stats,
996 ("# storage requests dropped due to high load"), 1,
1003 * Iterator called on each result obtained for a DHT
1004 * operation that expects a reply
1006 * @param cls closure
1007 * @param exp when will this value expire
1008 * @param key key of the result
1009 * @param get_path peers on reply path (or NULL if not recorded)
1010 * @param get_path_length number of entries in get_path
1011 * @param put_path peers on the PUT path (or NULL if not recorded)
1012 * @param put_path_length number of entries in get_path
1013 * @param type type of the result
1014 * @param size number of bytes in data
1015 * @param data pointer to the result data
1018 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1019 const struct GNUNET_HashCode * key,
1020 const struct GNUNET_PeerIdentity *get_path,
1021 unsigned int get_path_length,
1022 const struct GNUNET_PeerIdentity *put_path,
1023 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1024 size_t size, const void *data)
1026 struct GSF_PendingRequest *pr = cls;
1027 struct ProcessReplyClosure prq;
1028 struct PutMigrationContext *pmc;
1030 GNUNET_STATISTICS_update (GSF_stats,
1031 gettext_noop ("# Replies received from DHT"), 1,
1033 memset (&prq, 0, sizeof (prq));
1035 prq.expiration = exp;
1036 /* do not allow migrated content to live longer than 1 year */
1037 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1041 process_reply (&prq, key, pr);
1042 if ((GNUNET_YES == active_to_migration) &&
1043 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Replicating result for query `%s' with priority %u\n",
1047 GNUNET_h2s (key), prq.priority);
1048 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1049 pmc->start = GNUNET_TIME_absolute_get ();
1050 pmc->requested = GNUNET_YES;
1052 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1054 0 /* replication */ ,
1055 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1056 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1057 &put_migration_continuation, pmc))
1059 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1066 * Consider looking up the data in the DHT (anonymity-level permitting).
1068 * @param pr the pending request to process
1071 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1075 struct GNUNET_PeerIdentity pi;
1076 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1078 if (0 != pr->public_data.anonymity_level)
1082 GNUNET_DHT_get_stop (pr->gh);
1087 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1090 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1091 xquery_size = sizeof (struct GNUNET_HashCode);
1093 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1095 GNUNET_assert (0 != pr->sender_pid);
1096 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1097 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1098 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1101 GNUNET_DHT_get_start (GSF_dht,
1102 pr->public_data.type, &pr->public_data.query,
1103 5 /* DEFAULT_GET_REPLICATION */ ,
1104 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1105 /* FIXME: can no longer pass pr->bf/pr->mingle... */
1106 xquery, xquery_size, &handle_dht_reply, pr);
1111 * Task that issues a warning if the datastore lookup takes too long.
1113 * @param cls the 'struct GSF_PendingRequest'
1114 * @param tc task context
1117 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1119 struct GSF_PendingRequest *pr = cls;
1121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1122 _("Datastore lookup already took %s!\n"),
1123 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1125 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1131 * Task that issues a warning if the datastore lookup takes too long.
1133 * @param cls the 'struct GSF_PendingRequest'
1134 * @param tc task context
1137 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 struct GSF_PendingRequest *pr = cls;
1141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1142 _("On-demand lookup already took %s!\n"),
1143 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1145 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1146 &odc_warn_delay_task, pr);
1151 * We're processing (local) results for a search request
1152 * from another peer. Pass applicable results to the
1153 * peer and if we are done either clean up (operation
1154 * complete) or forward to other peers (more results possible).
1156 * @param cls our closure (struct PendingRequest)
1157 * @param key key for the content
1158 * @param size number of bytes in data
1159 * @param data content stored
1160 * @param type type of the content
1161 * @param priority priority of the content
1162 * @param anonymity anonymity-level for the content
1163 * @param expiration expiration time for the content
1164 * @param uid unique identifier for the datum;
1165 * maybe 0 if no unique identifier is available
1168 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1169 const void *data, enum GNUNET_BLOCK_Type type,
1170 uint32_t priority, uint32_t anonymity,
1171 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1173 struct GSF_PendingRequest *pr = cls;
1174 GSF_LocalLookupContinuation cont;
1175 struct ProcessReplyClosure prq;
1176 struct GNUNET_HashCode query;
1177 unsigned int old_rf;
1179 GNUNET_SCHEDULER_cancel (pr->warn_task);
1180 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1186 GNUNET_STATISTICS_update (GSF_stats,
1188 ("# Datastore lookups concluded (no results)"),
1191 if (GNUNET_NO == pr->have_first_uid)
1193 pr->first_uid = uid;
1194 pr->have_first_uid = 1;
1198 if ((uid == pr->first_uid) && (key != NULL))
1200 GNUNET_STATISTICS_update (GSF_stats,
1202 ("# Datastore lookups concluded (seen all)"),
1204 key = NULL; /* all replies seen! */
1206 pr->have_first_uid++;
1207 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1209 GNUNET_STATISTICS_update (GSF_stats,
1211 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1213 key = NULL; /* all replies seen! */
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1220 "No further local responses available.\n");
1221 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1222 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1223 GNUNET_STATISTICS_update (GSF_stats,
1225 ("# requested DBLOCK or IBLOCK not found"), 1,
1227 goto check_error_and_continue;
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1231 GNUNET_h2s (key), type, (unsigned long long) uid);
1232 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Found ONDEMAND block, performing on-demand encoding\n");
1236 GNUNET_STATISTICS_update (GSF_stats,
1238 ("# on-demand blocks matched requests"), 1,
1240 pr->qe_start = GNUNET_TIME_absolute_get ();
1242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1243 &odc_warn_delay_task, pr);
1245 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1246 anonymity, expiration, uid,
1247 &process_local_reply, pr))
1249 GNUNET_STATISTICS_update (GSF_stats,
1251 ("# on-demand lookups performed successfully"),
1253 return; /* we're done */
1255 GNUNET_STATISTICS_update (GSF_stats,
1256 gettext_noop ("# on-demand lookups failed"), 1,
1258 GNUNET_SCHEDULER_cancel (pr->warn_task);
1260 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1261 &warn_delay_task, pr);
1263 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1264 &pr->public_data.query,
1265 pr->public_data.type ==
1266 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1267 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1269 (GSF_PRO_PRIORITY_UNLIMITED &
1270 pr->public_data.options)) ? UINT_MAX : 1
1271 /* queue priority */ ,
1273 (GSF_PRO_PRIORITY_UNLIMITED &
1274 pr->public_data.options)) ? UINT_MAX :
1275 datastore_queue_size
1276 /* max queue size */ ,
1277 GNUNET_TIME_UNIT_FOREVER_REL,
1278 &process_local_reply, pr);
1280 return; /* we're done */
1281 GNUNET_STATISTICS_update (GSF_stats,
1283 ("# Datastore lookups concluded (error queueing)"),
1285 goto check_error_and_continue;
1287 old_rf = pr->public_data.results_found;
1288 memset (&prq, 0, sizeof (prq));
1290 prq.expiration = expiration;
1293 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1296 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1297 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1298 pr->qe_start = GNUNET_TIME_absolute_get ();
1300 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1301 &warn_delay_task, pr);
1303 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1304 &pr->public_data.query,
1305 pr->public_data.type ==
1306 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1307 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1309 (GSF_PRO_PRIORITY_UNLIMITED &
1310 pr->public_data.options)) ? UINT_MAX : 1
1311 /* queue priority */ ,
1313 (GSF_PRO_PRIORITY_UNLIMITED &
1314 pr->public_data.options)) ? UINT_MAX :
1315 datastore_queue_size
1316 /* max queue size */ ,
1317 GNUNET_TIME_UNIT_FOREVER_REL,
1318 &process_local_reply, pr);
1321 GNUNET_STATISTICS_update (GSF_stats,
1323 ("# Datastore lookups concluded (error queueing)"),
1325 goto check_error_and_continue;
1330 prq.priority = priority;
1331 prq.request_found = GNUNET_NO;
1332 prq.anonymity_level = anonymity;
1333 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1334 GSF_update_datastore_delay_ (pr->public_data.start_time);
1335 process_reply (&prq, key, pr);
1336 pr->local_result = prq.eval;
1337 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1339 GNUNET_STATISTICS_update (GSF_stats,
1341 ("# Datastore lookups concluded (found last result)"),
1343 goto check_error_and_continue;
1345 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1346 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1347 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1350 GNUNET_STATISTICS_update (GSF_stats,
1352 ("# Datastore lookups concluded (load too high)"),
1354 goto check_error_and_continue;
1356 pr->qe_start = GNUNET_TIME_absolute_get ();
1358 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1361 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1362 &pr->public_data.query,
1363 pr->public_data.type ==
1364 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1365 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1367 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1368 public_data.options)) ? UINT_MAX : 1
1369 /* queue priority */ ,
1371 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1372 public_data.options)) ? UINT_MAX :
1373 datastore_queue_size
1374 /* max queue size */ ,
1375 GNUNET_TIME_UNIT_FOREVER_REL,
1376 &process_local_reply, pr);
1377 /* check if we successfully queued another datastore request;
1378 * if so, return, otherwise call our continuation (if we have
1380 check_error_and_continue:
1383 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1385 GNUNET_SCHEDULER_cancel (pr->warn_task);
1386 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1388 if (NULL == (cont = pr->llc_cont))
1389 return; /* no continuation */
1390 pr->llc_cont = NULL;
1391 cont (pr->llc_cont_cls, pr, pr->local_result);
1396 * Is the given target a legitimate peer for forwarding the given request?
1400 * @return GNUNET_YES if this request could be forwarded to the given peer
1403 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1404 const struct GNUNET_PeerIdentity *target)
1406 struct GNUNET_PeerIdentity pi;
1408 if (0 == pr->origin_pid)
1410 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1412 memcmp (&pi, target,
1413 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1419 * Look up the request in the local datastore.
1421 * @param pr the pending request to process
1422 * @param cont function to call at the end
1423 * @param cont_cls closure for cont
1426 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1427 GSF_LocalLookupContinuation cont, void *cont_cls)
1429 GNUNET_assert (NULL == pr->gh);
1430 GNUNET_assert (NULL == pr->llc_cont);
1431 pr->llc_cont = cont;
1432 pr->llc_cont_cls = cont_cls;
1433 pr->qe_start = GNUNET_TIME_absolute_get ();
1435 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1437 GNUNET_STATISTICS_update (GSF_stats,
1438 gettext_noop ("# Datastore lookups initiated"), 1,
1441 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1442 &pr->public_data.query,
1443 pr->public_data.type ==
1444 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1445 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1447 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1448 public_data.options)) ? UINT_MAX : 1
1449 /* queue priority */ ,
1451 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1452 public_data.options)) ? UINT_MAX :
1453 datastore_queue_size
1454 /* max queue size */ ,
1455 GNUNET_TIME_UNIT_FOREVER_REL,
1456 &process_local_reply, pr);
1459 GNUNET_STATISTICS_update (GSF_stats,
1461 ("# Datastore lookups concluded (error queueing)"),
1463 GNUNET_SCHEDULER_cancel (pr->warn_task);
1464 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1465 pr->llc_cont = NULL;
1467 cont (cont_cls, pr, pr->local_result);
1473 * Handle P2P "CONTENT" message. Checks that the message is
1474 * well-formed and then checks if there are any pending requests for
1475 * this content and possibly passes it on (to local clients or other
1476 * peers). Does NOT perform migration (content caching at this peer).
1478 * @param cp the other peer involved (sender or receiver, NULL
1479 * for loopback messages where we are both sender and receiver)
1480 * @param message the actual message
1481 * @return GNUNET_OK if the message was well-formed,
1482 * GNUNET_SYSERR if the message was malformed (close connection,
1483 * do not cache under any circumstances)
1486 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1487 const struct GNUNET_MessageHeader *message)
1489 const struct PutMessage *put;
1492 enum GNUNET_BLOCK_Type type;
1493 struct GNUNET_TIME_Absolute expiration;
1494 struct GNUNET_HashCode query;
1495 struct ProcessReplyClosure prq;
1496 struct GNUNET_TIME_Relative block_time;
1498 struct PutMigrationContext *pmc;
1500 msize = ntohs (message->size);
1501 if (msize < sizeof (struct PutMessage))
1503 GNUNET_break_op (0);
1504 return GNUNET_SYSERR;
1506 put = (const struct PutMessage *) message;
1507 dsize = msize - sizeof (struct PutMessage);
1508 type = ntohl (put->type);
1509 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1510 /* do not allow migrated content to live longer than 1 year */
1511 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1513 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1514 return GNUNET_SYSERR;
1516 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1518 GNUNET_break_op (0);
1519 return GNUNET_SYSERR;
1521 GNUNET_STATISTICS_update (GSF_stats,
1522 gettext_noop ("# GAP PUT messages received"), 1,
1524 /* now, lookup 'query' */
1525 prq.data = (const void *) &put[1];
1529 prq.expiration = expiration;
1531 prq.anonymity_level = UINT32_MAX;
1532 prq.request_found = GNUNET_NO;
1533 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1537 GSF_connected_peer_change_preference_ (cp,
1538 CONTENT_BANDWIDTH_VALUE +
1539 1000 * prq.priority);
1540 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1542 if ((GNUNET_YES == active_to_migration) &&
1544 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "Replicating result for query `%s' with priority %u\n",
1548 GNUNET_h2s (&query), prq.priority);
1549 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1550 pmc->start = GNUNET_TIME_absolute_get ();
1551 pmc->requested = prq.request_found;
1552 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1553 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1556 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1557 prq.priority, 1 /* anonymity */ ,
1558 0 /* replication */ ,
1559 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1560 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1561 &put_migration_continuation, pmc))
1563 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1566 else if (NULL != cp)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Choosing not to keep content `%s' (%d/%d)\n",
1570 GNUNET_h2s (&query), active_to_migration,
1571 test_put_load_too_high (prq.priority));
1573 putl = GNUNET_LOAD_get_load (datastore_put_load);
1574 if ( (NULL != cp) &&
1575 (GNUNET_NO == prq.request_found) &&
1576 ( (GNUNET_YES != active_to_migration) ||
1577 (putl > 2.5 * (1 + prq.priority)) ) )
1579 if (GNUNET_YES != active_to_migration)
1580 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1582 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1584 GNUNET_CRYPTO_random_u32
1585 (GNUNET_CRYPTO_QUALITY_WEAK,
1586 (unsigned int) (60000 * putl * putl)));
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1589 (unsigned long long) block_time.rel_value,
1591 active_to_migration,
1592 (GNUNET_NO == prq.request_found));
1593 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1600 * Setup the subsystem.
1603 GSF_pending_request_init_ ()
1605 unsigned long long bps;
1608 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1609 "MAX_PENDING_REQUESTS",
1610 &max_pending_requests))
1612 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1613 "fs", "MAX_PENDING_REQUESTS");
1616 GNUNET_CONFIGURATION_get_value_size (GSF_cfg, "ats", "WAN_QUOTA_OUT",
1619 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1620 "ats", "WAN_QUOTA_OUT");
1623 /* queue size should be #queries we can have pending and satisfy within
1624 * a carry interval: */
1625 datastore_queue_size =
1626 bps * GNUNET_CONSTANTS_MAX_BANDWIDTH_CARRY_S / DBLOCK_SIZE;
1628 active_to_migration =
1629 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1630 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1631 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1632 requests_by_expiration_heap =
1633 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1638 * Shutdown the subsystem.
1641 GSF_pending_request_done_ ()
1643 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1644 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1646 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1647 requests_by_expiration_heap = NULL;
1648 GNUNET_LOAD_value_free (datastore_put_load);
1649 datastore_put_load = NULL;
1653 /* end of gnunet-service-fs_pr.c */