2 This file is part of GNUnet.
3 (C) 2009-2013 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_util_lib.h"
28 #include "gnunet_load_lib.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_indexing.h"
32 #include "gnunet-service-fs_pe.h"
33 #include "gnunet-service-fs_pr.h"
34 #include "gnunet-service-fs_mesh.h"
38 * Desired replication level for GETs.
40 #define DHT_GET_REPLICATION 5
43 * Maximum size of the datastore queue for P2P operations. Needs to
44 * be large enough to queue MAX_QUEUE_PER_PEER operations for roughly
45 * the number of active (connected) peers.
47 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
50 * Bandwidth value of a 0-priority content (must be fairly high
51 * compared to query since content is typically significantly larger
52 * -- and more valueable since it can take many queries to get one
55 #define CONTENT_BANDWIDTH_VALUE 800
58 * Hard limit on the number of results we may get from the datastore per query.
60 #define MAX_RESULTS (100 * 1024)
63 * Collect an instane number of statistics? May cause excessive IPC.
65 #define INSANE_STATISTICS GNUNET_NO
68 * If obtaining a block via mesh fails, how often do we retry it before
69 * giving up for good (and sticking to non-anonymous transfer)?
71 #define MESH_RETRY_MAX 3
77 struct GSF_PendingRequest
80 * Public data for the request.
82 struct GSF_PendingRequestData public_data;
85 * Function to call if we encounter a reply.
87 GSF_PendingRequestReplyHandler rh;
95 * Array of hash codes of replies we've already seen.
97 struct GNUNET_HashCode *replies_seen;
100 * Bloomfilter masking replies we've already seen.
102 struct GNUNET_CONTAINER_BloomFilter *bf;
105 * Entry for this pending request in the expiration heap, or NULL.
107 struct GNUNET_CONTAINER_HeapNode *hnode;
110 * Datastore queue entry for this request (or NULL for none).
112 struct GNUNET_DATASTORE_QueueEntry *qe;
115 * DHT request handle for this request (or NULL for none).
117 struct GNUNET_DHT_GetHandle *gh;
120 * Mesh request handle for this request (or NULL for none).
122 struct GSF_MeshRequest *mesh_request;
125 * Function to call upon completion of the local get
126 * request, or NULL for none.
128 GSF_LocalLookupContinuation llc_cont;
131 * Closure for llc_cont.
136 * Last result from the local datastore lookup evaluation.
138 enum GNUNET_BLOCK_EvaluationResult local_result;
141 * Identity of the peer that we should use for the 'sender'
142 * (recipient of the response) when forwarding (0 for none).
144 GNUNET_PEER_Id sender_pid;
147 * Identity of the peer that we should never forward this query
148 * to since it originated this query (0 for none).
150 GNUNET_PEER_Id origin_pid;
153 * Time we started the last datastore lookup.
155 struct GNUNET_TIME_Absolute qe_start;
158 * Task that warns us if the local datastore lookup takes too long.
160 GNUNET_SCHEDULER_TaskIdentifier warn_task;
163 * Current offset for querying our local datastore for results.
164 * Starts at a random value, incremented until we get the same
165 * UID again (detected using 'first_uid'), which is then used
166 * to termiante the iteration.
168 uint64_t local_result_offset;
171 * Unique ID of the first result from the local datastore;
172 * used to detect wrap-around of the offset.
177 * How often have we retried this request via 'mesh'?
178 * (used to bound overall retries).
180 unsigned int mesh_retry_count;
183 * Number of valid entries in the 'replies_seen' array.
185 unsigned int replies_seen_count;
188 * Length of the 'replies_seen' array.
190 unsigned int replies_seen_size;
193 * Mingle value we currently use for the bf.
198 * Do we have a first UID yet?
200 unsigned int have_first_uid;
206 * All pending requests, ordered by the query. Entries
207 * are of type 'struct GSF_PendingRequest*'.
209 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
213 * Datastore 'PUT' load tracking.
215 static struct GNUNET_LOAD_Value *datastore_put_load;
219 * Are we allowed to migrate content to this peer.
221 static int active_to_migration;
225 * Heap with the request that will expire next at the top. Contains
226 * pointers of type "struct PendingRequest*"; these will *also* be
227 * aliased from the "requests_by_peer" data structures and the
228 * "requests_by_query" table. Note that requests from our clients
229 * don't expire and are thus NOT in the "requests_by_expiration"
230 * (or the "requests_by_peer" tables).
232 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
236 * Maximum number of requests (from other peers, overall) that we're
237 * willing to have pending at any given point in time. Can be changed
238 * via the configuration file (32k is just the default).
240 static unsigned long long max_pending_requests = (32 * 1024);
245 * Recalculate our bloom filter for filtering replies. This function
246 * will create a new bloom filter from scratch, so it should only be
247 * called if we have no bloomfilter at all (and hence can create a
248 * fresh one of minimal size without problems) OR if our peer is the
249 * initiator (in which case we may resize to larger than mimimum size).
251 * @param pr request for which the BF is to be recomputed
254 refresh_bloomfilter (struct GSF_PendingRequest *pr)
257 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
259 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
261 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
262 pr->replies_seen_count);
267 * Create a new pending request.
269 * @param options request options
270 * @param type type of the block that is being requested
271 * @param query key for the lookup
272 * @param target preferred target for the request, NULL for none
273 * @param bf_data raw data for bloom filter for known replies, can be NULL
274 * @param bf_size number of bytes in @a bf_data
275 * @param mingle mingle value for bf
276 * @param anonymity_level desired anonymity level
277 * @param priority maximum outgoing cummulative request priority to use
278 * @param ttl current time-to-live for the request
279 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
280 * @param origin_pid peer ID of origin of query (do not loop back)
281 * @param replies_seen hash codes of known local replies
282 * @param replies_seen_count size of the @a replies_seen array
283 * @param rh handle to call when we get a reply
284 * @param rh_cls closure for @a rh
285 * @return handle for the new pending request
287 struct GSF_PendingRequest *
288 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
289 enum GNUNET_BLOCK_Type type,
290 const struct GNUNET_HashCode *query,
291 const struct GNUNET_PeerIdentity *target,
292 const char *bf_data, size_t bf_size,
293 uint32_t mingle, uint32_t anonymity_level,
294 uint32_t priority, int32_t ttl,
295 GNUNET_PEER_Id sender_pid,
296 GNUNET_PEER_Id origin_pid,
297 const struct GNUNET_HashCode *replies_seen,
298 unsigned int replies_seen_count,
299 GSF_PendingRequestReplyHandler rh, void *rh_cls)
301 struct GSF_PendingRequest *pr;
302 struct GSF_PendingRequest *dpr;
304 struct GNUNET_HashCode *eptr;
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Creating request handle for `%s' of type %d\n",
308 GNUNET_h2s (query), type);
309 #if INSANE_STATISTICS
310 GNUNET_STATISTICS_update (GSF_stats,
311 gettext_noop ("# Pending requests created"), 1,
316 extra += sizeof (struct GNUNET_PeerIdentity);
317 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
318 pr->local_result_offset =
319 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
320 pr->public_data.query = *query;
321 eptr = (struct GNUNET_HashCode *) &pr[1];
324 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
325 memcpy (eptr, target, sizeof (struct GNUNET_PeerIdentity));
327 pr->public_data.anonymity_level = anonymity_level;
328 pr->public_data.priority = priority;
329 pr->public_data.original_priority = priority;
330 pr->public_data.options = options;
331 pr->public_data.type = type;
332 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
333 pr->sender_pid = sender_pid;
334 pr->origin_pid = origin_pid;
337 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
339 pr->public_data.ttl =
340 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
341 (GNUNET_TIME_UNIT_SECONDS,
344 pr->public_data.ttl =
345 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
346 GNUNET_TIME_relative_multiply
347 (GNUNET_TIME_UNIT_SECONDS,
349 if (replies_seen_count > 0)
351 pr->replies_seen_size = replies_seen_count;
353 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
354 memcpy (pr->replies_seen, replies_seen,
355 replies_seen_count * sizeof (struct GNUNET_HashCode));
356 pr->replies_seen_count = replies_seen_count;
361 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size,
362 GNUNET_CONSTANTS_BLOOMFILTER_K);
365 else if ((replies_seen_count > 0) &&
366 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
368 refresh_bloomfilter (pr);
370 GNUNET_CONTAINER_multihashmap_put (pr_map,
371 &pr->public_data.query, pr,
372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
373 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
376 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
377 pr->public_data.ttl.abs_value_us);
378 /* make sure we don't track too many requests */
379 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
380 max_pending_requests)
382 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
383 GNUNET_assert (dpr != NULL);
385 break; /* let the request live briefly... */
387 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
388 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_ABS,
389 GNUNET_BLOCK_TYPE_ANY, NULL, 0);
390 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
393 GNUNET_STATISTICS_update (GSF_stats,
394 gettext_noop ("# Pending requests active"), 1,
400 * Obtain the public data associated with a pending request
402 * @param pr pending request
403 * @return associated public data
405 struct GSF_PendingRequestData *
406 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
408 return &pr->public_data;
413 * Test if two pending requests are compatible (would generate
414 * the same query modulo filters and should thus be processed
417 * @param pra a pending request
418 * @param prb another pending request
419 * @return #GNUNET_OK if the requests are compatible
422 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
423 struct GSF_PendingRequest *prb)
425 if ((pra->public_data.type != prb->public_data.type) ||
427 memcmp (&pra->public_data.query, &prb->public_data.query,
428 sizeof (struct GNUNET_HashCode))))
436 * Update a given pending request with additional replies
437 * that have been seen.
439 * @param pr request to update
440 * @param replies_seen hash codes of replies that we've seen
441 * @param replies_seen_count size of the replies_seen array
444 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
445 const struct GNUNET_HashCode * replies_seen,
446 unsigned int replies_seen_count)
449 struct GNUNET_HashCode mhash;
451 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
452 return; /* integer overflow */
453 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
455 /* we're responsible for the BF, full refresh */
456 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
457 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
458 replies_seen_count + pr->replies_seen_count);
459 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
460 sizeof (struct GNUNET_HashCode) * replies_seen_count);
461 pr->replies_seen_count += replies_seen_count;
462 refresh_bloomfilter (pr);
468 /* we're not the initiator, but the initiator did not give us
469 * any bloom-filter, so we need to create one on-the-fly */
471 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
473 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, replies_seen,
478 for (i = 0; i < pr->replies_seen_count; i++)
480 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
481 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
486 GNUNET_DHT_get_filter_known_results (pr->gh,
493 * Generate the message corresponding to the given pending request for
494 * transmission to other peers (or at least determine its size).
496 * @param pr request to generate the message for
497 * @param buf_size number of bytes available in @a buf
498 * @param buf where to copy the message (can be NULL)
499 * @return number of bytes needed (if `>` @a buf_size) or used
502 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
503 size_t buf_size, void *buf)
505 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
506 struct GetMessage *gm;
507 struct GNUNET_HashCode *ext;
513 struct GNUNET_TIME_Absolute now;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Building request message for `%s' of type %d\n",
520 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
523 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
524 if ((!do_route) && (pr->sender_pid == 0))
527 do_route = GNUNET_YES;
531 bm |= GET_MESSAGE_BIT_RETURN_TO;
534 if (NULL != pr->public_data.target)
536 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
539 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
540 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_HashCode);
541 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
542 if (buf_size < msize)
544 gm = (struct GetMessage *) lbuf;
545 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
546 gm->header.size = htons (msize);
547 gm->type = htonl (pr->public_data.type);
550 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
551 pr->public_data.priority + 1);
554 pr->public_data.priority -= prio;
555 pr->public_data.num_transmissions++;
556 pr->public_data.respect_offered += prio;
557 gm->priority = htonl (prio);
558 now = GNUNET_TIME_absolute_get ();
559 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
560 gm->ttl = htonl (ttl / 1000LL / 1000LL);
561 gm->filter_mutator = htonl (pr->mingle);
562 gm->hash_bitmap = htonl (bm);
563 gm->query = pr->public_data.query;
564 ext = (struct GNUNET_HashCode *) & gm[1];
567 GNUNET_PEER_resolve (pr->sender_pid,
568 (struct GNUNET_PeerIdentity *) &ext[k++]);
569 if (NULL != pr->public_data.target)
571 pr->public_data.target,
572 sizeof (struct GNUNET_PeerIdentity));
574 GNUNET_assert (GNUNET_SYSERR !=
575 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
578 memcpy (buf, gm, msize);
584 * Iterator to free pending requests.
586 * @param cls closure, unused
587 * @param key current key code
588 * @param value value in the hash map (pending request)
589 * @return #GNUNET_YES (we should continue to iterate)
592 clean_request (void *cls, const struct GNUNET_HashCode * key, void *value)
594 struct GSF_PendingRequest *pr = value;
595 GSF_LocalLookupContinuation cont;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
599 if (NULL != (cont = pr->llc_cont))
602 cont (pr->llc_cont_cls, pr, pr->local_result);
604 GSF_plan_notify_request_done_ (pr);
605 GNUNET_free_non_null (pr->replies_seen);
608 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
611 GNUNET_PEER_change_rc (pr->sender_pid, -1);
613 GNUNET_PEER_change_rc (pr->origin_pid, -1);
615 if (NULL != pr->hnode)
617 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
622 GNUNET_DATASTORE_cancel (pr->qe);
627 GNUNET_DHT_get_stop (pr->gh);
630 if (NULL != pr->mesh_request)
632 GSF_mesh_query_cancel (pr->mesh_request);
633 pr->mesh_request = NULL;
635 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
637 GNUNET_SCHEDULER_cancel (pr->warn_task);
638 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
640 GNUNET_assert (GNUNET_OK ==
641 GNUNET_CONTAINER_multihashmap_remove (pr_map,
642 &pr->public_data.query,
644 GNUNET_STATISTICS_update (GSF_stats,
645 gettext_noop ("# Pending requests active"), -1,
653 * Explicitly cancel a pending request.
655 * @param pr request to cancel
656 * @param full_cleanup fully purge the request
659 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
661 GSF_LocalLookupContinuation cont;
664 return; /* already cleaned up! */
665 if (GNUNET_YES != full_cleanup)
667 /* make request inactive (we're no longer interested in more results),
668 * but do NOT remove from our data-structures, we still need it there
669 * to prevent the request from looping */
671 if (NULL != (cont = pr->llc_cont))
674 cont (pr->llc_cont_cls, pr, pr->local_result);
676 GSF_plan_notify_request_done_ (pr);
679 GNUNET_DATASTORE_cancel (pr->qe);
684 GNUNET_DHT_get_stop (pr->gh);
687 if (NULL != pr->mesh_request)
689 GSF_mesh_query_cancel (pr->mesh_request);
690 pr->mesh_request = NULL;
692 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
694 GNUNET_SCHEDULER_cancel (pr->warn_task);
695 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
699 GNUNET_assert (GNUNET_YES ==
700 clean_request (NULL, &pr->public_data.query, pr));
705 * Iterate over all pending requests.
707 * @param it function to call for each request
708 * @param cls closure for it
711 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
713 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
714 (GNUNET_CONTAINER_HashMapIterator) it,
722 * Closure for "process_reply" function.
724 struct ProcessReplyClosure
727 * The data for the reply.
732 * Who gave us this reply? NULL for local host (or DHT)
734 struct GSF_ConnectedPeer *sender;
737 * When the reply expires.
739 struct GNUNET_TIME_Absolute expiration;
749 enum GNUNET_BLOCK_Type type;
752 * How much was this reply worth to us?
757 * Anonymity requirements for this reply.
759 uint32_t anonymity_level;
762 * Evaluation result (returned).
764 enum GNUNET_BLOCK_EvaluationResult eval;
767 * Did we find a matching request?
774 * Update the performance data for the sender (if any) since
775 * the sender successfully answered one of our queries.
777 * @param prq information about the sender
778 * @param pr request that was satisfied
781 update_request_performance_data (struct ProcessReplyClosure *prq,
782 struct GSF_PendingRequest *pr)
784 if (prq->sender == NULL)
786 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
792 * We have received a reply; handle it!
794 * @param cls response (struct ProcessReplyClosure)
795 * @param key our query
796 * @param value value in the hash map (info about the query)
797 * @return #GNUNET_YES (we should continue to iterate)
800 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
802 struct ProcessReplyClosure *prq = cls;
803 struct GSF_PendingRequest *pr = value;
804 struct GNUNET_HashCode chash;
805 struct GNUNET_TIME_Absolute last_transmission;
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Matched result (type %u) for query `%s' with pending request\n",
811 (unsigned int) prq->type, GNUNET_h2s (key));
812 GNUNET_STATISTICS_update (GSF_stats,
813 gettext_noop ("# replies received and matched"), 1,
816 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
821 case GNUNET_BLOCK_EVALUATION_OK_MORE:
822 update_request_performance_data (prq, pr);
824 case GNUNET_BLOCK_EVALUATION_OK_LAST:
825 /* short cut: stop processing early, no BF-update, etc. */
826 update_request_performance_data (prq, pr);
827 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
828 GNUNET_TIME_absolute_get_duration (pr->
829 public_data.start_time).rel_value_us);
831 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
834 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
835 /* pass on to other peers / local clients */
836 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
837 last_transmission, prq->type, prq->data, prq->size);
839 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
840 #if INSANE_STATISTICS
841 GNUNET_STATISTICS_update (GSF_stats,
843 ("# duplicate replies discarded (bloomfilter)"),
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Duplicate response, discarding.\n");
848 return GNUNET_YES; /* duplicate */
849 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
850 GNUNET_STATISTICS_update (GSF_stats,
852 ("# irrelevant replies discarded"),
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Irrelevant response, ignoring.\n");
857 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
858 return GNUNET_YES; /* wrong namespace */
859 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
862 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
865 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
866 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
870 /* update bloomfilter */
871 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
872 GSF_pending_request_update_ (pr, &chash, 1);
873 if (NULL == prq->sender)
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Found result for query `%s' in local datastore\n",
878 GNUNET_STATISTICS_update (GSF_stats,
879 gettext_noop ("# results found locally"), 1,
884 GSF_dht_lookup_ (pr);
886 prq->priority += pr->public_data.original_priority;
887 pr->public_data.priority = 0;
888 pr->public_data.original_priority = 0;
889 pr->public_data.results_found++;
890 prq->request_found = GNUNET_YES;
891 /* finally, pass on to other peer / local client */
892 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
895 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
896 pr->rh (pr->rh_cls, prq->eval, pr,
897 prq->anonymity_level, prq->expiration,
898 last_transmission, prq->type, prq->data, prq->size);
904 * Context for put_migration_continuation().
906 struct PutMigrationContext
910 * Start time for the operation.
912 struct GNUNET_TIME_Absolute start;
917 struct GNUNET_PeerIdentity origin;
920 * GNUNET_YES if we had a matching request for this block,
928 * Continuation called to notify client about result of the
932 * @param success #GNUNET_SYSERR on failure
933 * @param min_expiration minimum expiration time required for content to be stored
934 * @param msg NULL on success, otherwise an error message
937 put_migration_continuation (void *cls, int success,
938 struct GNUNET_TIME_Absolute min_expiration,
941 struct PutMigrationContext *pmc = cls;
942 struct GSF_ConnectedPeer *cp;
943 struct GNUNET_TIME_Relative mig_pause;
944 struct GSF_PeerPerformanceData *ppd;
946 if (NULL != datastore_put_load)
948 if (GNUNET_SYSERR != success)
950 GNUNET_LOAD_update (datastore_put_load,
951 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
955 /* on queue failure / timeout, increase the put load dramatically */
956 GNUNET_LOAD_update (datastore_put_load,
957 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
960 cp = GSF_peer_get_ (&pmc->origin);
961 if (GNUNET_OK == success)
965 ppd = GSF_get_peer_performance_data_ (cp);
966 ppd->migration_delay.rel_value_us /= 2;
971 if ( (GNUNET_NO == success) &&
972 (GNUNET_NO == pmc->requested) &&
975 ppd = GSF_get_peer_performance_data_ (cp);
976 if (min_expiration.abs_value_us > 0)
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Asking to stop migration for %s because datastore is full\n",
980 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
981 GSF_block_peer_migration_ (cp, min_expiration);
985 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
986 ppd->migration_delay);
987 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
988 ppd->migration_delay);
989 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
990 ppd->migration_delay.rel_value_us);
991 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Replicated content already exists locally, asking to stop migration for %s\n",
994 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
995 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
999 GNUNET_STATISTICS_update (GSF_stats,
1000 gettext_noop ("# Datastore `PUT' failures"), 1,
1006 * Test if the DATABASE (PUT) load on this peer is too high
1007 * to even consider processing the query at
1010 * @param priority the priority of the item
1011 * @return #GNUNET_YES if the load is too high to do anything (load high)
1012 * #GNUNET_NO to process normally (load normal or low)
1015 test_put_load_too_high (uint32_t priority)
1019 if (NULL == datastore_put_load)
1021 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1022 return GNUNET_NO; /* very fast */
1023 ld = GNUNET_LOAD_get_load (datastore_put_load);
1024 if (ld < 2.0 * (1 + priority))
1026 GNUNET_STATISTICS_update (GSF_stats,
1028 ("# storage requests dropped due to high load"), 1,
1035 * Iterator called on each result obtained for a DHT
1036 * operation that expects a reply
1038 * @param cls closure
1039 * @param exp when will this value expire
1040 * @param key key of the result
1041 * @param get_path peers on reply path (or NULL if not recorded)
1042 * @param get_path_length number of entries in @a get_path
1043 * @param put_path peers on the PUT path (or NULL if not recorded)
1044 * @param put_path_length number of entries in @a get_path
1045 * @param type type of the result
1046 * @param size number of bytes in @a data
1047 * @param data pointer to the result data
1050 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1051 const struct GNUNET_HashCode * key,
1052 const struct GNUNET_PeerIdentity *get_path,
1053 unsigned int get_path_length,
1054 const struct GNUNET_PeerIdentity *put_path,
1055 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1056 size_t size, const void *data)
1058 struct GSF_PendingRequest *pr = cls;
1059 struct ProcessReplyClosure prq;
1060 struct PutMigrationContext *pmc;
1062 GNUNET_STATISTICS_update (GSF_stats,
1063 gettext_noop ("# Replies received from DHT"), 1,
1065 memset (&prq, 0, sizeof (prq));
1067 prq.expiration = exp;
1068 /* do not allow migrated content to live longer than 1 year */
1069 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1073 process_reply (&prq, key, pr);
1074 if ((GNUNET_YES == active_to_migration) &&
1075 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Replicating result for query `%s' with priority %u\n",
1079 GNUNET_h2s (key), prq.priority);
1080 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1081 pmc->start = GNUNET_TIME_absolute_get ();
1082 pmc->requested = GNUNET_YES;
1084 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1086 0 /* replication */ ,
1087 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1088 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1089 &put_migration_continuation, pmc))
1091 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1098 * Consider looking up the data in the DHT (anonymity-level permitting).
1100 * @param pr the pending request to process
1103 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1107 struct GNUNET_PeerIdentity pi;
1108 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1110 if (0 != pr->public_data.anonymity_level)
1114 GNUNET_DHT_get_stop (pr->gh);
1119 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1121 GNUNET_assert (0 != pr->sender_pid);
1122 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1123 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1124 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1127 GNUNET_DHT_get_start (GSF_dht,
1128 pr->public_data.type, &pr->public_data.query,
1129 DHT_GET_REPLICATION,
1130 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1131 xquery, xquery_size, &handle_dht_reply, pr);
1132 if ( (NULL != pr->gh) &&
1133 (0 != pr->replies_seen_count) )
1134 GNUNET_DHT_get_filter_known_results (pr->gh,
1135 pr->replies_seen_count,
1141 * Function called with a reply from the mesh.
1143 * @param cls the pending request struct
1144 * @param type type of the block, ANY on error
1145 * @param expiration expiration time for the block
1146 * @param data_size number of bytes in 'data', 0 on error
1147 * @param data reply block data, NULL on error
1150 mesh_reply_proc (void *cls,
1151 enum GNUNET_BLOCK_Type type,
1152 struct GNUNET_TIME_Absolute expiration,
1156 struct GSF_PendingRequest *pr = cls;
1157 struct ProcessReplyClosure prq;
1158 struct GNUNET_HashCode query;
1160 pr->mesh_request = NULL;
1161 if (GNUNET_BLOCK_TYPE_ANY == type)
1163 GNUNET_break (NULL == data);
1164 GNUNET_break (0 == data_size);
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "Error retrieiving block via mesh\n");
1167 pr->mesh_retry_count++;
1168 if (pr->mesh_retry_count >= MESH_RETRY_MAX)
1169 return; /* give up on mesh */
1170 /* retry -- without delay, as this is non-anonymous
1171 and mesh/mesh connect will take some time anyway */
1172 pr->mesh_request = GSF_mesh_query (pr->public_data.target,
1173 &pr->public_data.query,
1174 pr->public_data.type,
1180 GNUNET_BLOCK_get_key (GSF_block_ctx,
1182 data, data_size, &query))
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Failed to derive key for block of type %d\n",
1187 GNUNET_break_op (0);
1190 GNUNET_STATISTICS_update (GSF_stats,
1191 gettext_noop ("# Replies received from MESH"), 1,
1193 memset (&prq, 0, sizeof (prq));
1195 prq.expiration = expiration;
1196 /* do not allow migrated content to live longer than 1 year */
1197 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1199 prq.size = data_size;
1201 process_reply (&prq, &query, pr);
1206 * Consider downloading via mesh (if possible)
1208 * @param pr the pending request to process
1211 GSF_mesh_lookup_ (struct GSF_PendingRequest *pr)
1213 if (0 != pr->public_data.anonymity_level)
1215 if (0 == pr->public_data.target)
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "Cannot do mesh-based download, target peer not known\n");
1221 if (NULL != pr->mesh_request)
1223 pr->mesh_request = GSF_mesh_query (pr->public_data.target,
1224 &pr->public_data.query,
1225 pr->public_data.type,
1232 * Task that issues a warning if the datastore lookup takes too long.
1234 * @param cls the 'struct GSF_PendingRequest'
1235 * @param tc task context
1238 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1240 struct GSF_PendingRequest *pr = cls;
1242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1243 _("Datastore lookup already took %s!\n"),
1244 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1246 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1252 * Task that issues a warning if the datastore lookup takes too long.
1254 * @param cls the 'struct GSF_PendingRequest'
1255 * @param tc task context
1258 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1260 struct GSF_PendingRequest *pr = cls;
1262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1263 _("On-demand lookup already took %s!\n"),
1264 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1266 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1267 &odc_warn_delay_task, pr);
1272 * We're processing (local) results for a search request
1273 * from another peer. Pass applicable results to the
1274 * peer and if we are done either clean up (operation
1275 * complete) or forward to other peers (more results possible).
1277 * @param cls our closure (struct PendingRequest)
1278 * @param key key for the content
1279 * @param size number of bytes in data
1280 * @param data content stored
1281 * @param type type of the content
1282 * @param priority priority of the content
1283 * @param anonymity anonymity-level for the content
1284 * @param expiration expiration time for the content
1285 * @param uid unique identifier for the datum;
1286 * maybe 0 if no unique identifier is available
1289 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1290 const void *data, enum GNUNET_BLOCK_Type type,
1291 uint32_t priority, uint32_t anonymity,
1292 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1294 struct GSF_PendingRequest *pr = cls;
1295 GSF_LocalLookupContinuation cont;
1296 struct ProcessReplyClosure prq;
1297 struct GNUNET_HashCode query;
1298 unsigned int old_rf;
1300 GNUNET_SCHEDULER_cancel (pr->warn_task);
1301 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1307 #if INSANE_STATISTICS
1308 GNUNET_STATISTICS_update (GSF_stats,
1310 ("# Datastore lookups concluded (no results)"),
1314 if (GNUNET_NO == pr->have_first_uid)
1316 pr->first_uid = uid;
1317 pr->have_first_uid = 1;
1321 if ((uid == pr->first_uid) && (key != NULL))
1323 GNUNET_STATISTICS_update (GSF_stats,
1325 ("# Datastore lookups concluded (seen all)"),
1327 key = NULL; /* all replies seen! */
1329 pr->have_first_uid++;
1330 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1332 GNUNET_STATISTICS_update (GSF_stats,
1334 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1336 key = NULL; /* all replies seen! */
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1343 "No further local responses available.\n");
1344 #if INSANE_STATISTICS
1345 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1346 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1347 GNUNET_STATISTICS_update (GSF_stats,
1349 ("# requested DBLOCK or IBLOCK not found"), 1,
1352 goto check_error_and_continue;
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1356 GNUNET_h2s (key), type, (unsigned long long) uid);
1357 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Found ONDEMAND block, performing on-demand encoding\n");
1361 GNUNET_STATISTICS_update (GSF_stats,
1363 ("# on-demand blocks matched requests"), 1,
1365 pr->qe_start = GNUNET_TIME_absolute_get ();
1367 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1368 &odc_warn_delay_task, pr);
1370 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1371 anonymity, expiration, uid,
1372 &process_local_reply, pr))
1374 GNUNET_STATISTICS_update (GSF_stats,
1376 ("# on-demand lookups performed successfully"),
1378 return; /* we're done */
1380 GNUNET_STATISTICS_update (GSF_stats,
1381 gettext_noop ("# on-demand lookups failed"), 1,
1383 GNUNET_SCHEDULER_cancel (pr->warn_task);
1385 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1386 &warn_delay_task, pr);
1388 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1389 &pr->public_data.query,
1390 pr->public_data.type ==
1391 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1392 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1394 (GSF_PRO_PRIORITY_UNLIMITED &
1395 pr->public_data.options)) ? UINT_MAX : 1
1396 /* queue priority */ ,
1398 (GSF_PRO_PRIORITY_UNLIMITED &
1399 pr->public_data.options)) ? UINT_MAX :
1400 GSF_datastore_queue_size
1401 /* max queue size */ ,
1402 GNUNET_TIME_UNIT_FOREVER_REL,
1403 &process_local_reply, pr);
1405 return; /* we're done */
1406 GNUNET_STATISTICS_update (GSF_stats,
1408 ("# Datastore lookups concluded (error queueing)"),
1410 goto check_error_and_continue;
1412 old_rf = pr->public_data.results_found;
1413 memset (&prq, 0, sizeof (prq));
1415 prq.expiration = expiration;
1418 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1421 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1422 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1423 pr->qe_start = GNUNET_TIME_absolute_get ();
1425 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1426 &warn_delay_task, pr);
1428 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1429 &pr->public_data.query,
1430 pr->public_data.type ==
1431 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1432 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1434 (GSF_PRO_PRIORITY_UNLIMITED &
1435 pr->public_data.options)) ? UINT_MAX : 1
1436 /* queue priority */ ,
1438 (GSF_PRO_PRIORITY_UNLIMITED &
1439 pr->public_data.options)) ? UINT_MAX :
1440 GSF_datastore_queue_size
1441 /* max queue size */ ,
1442 GNUNET_TIME_UNIT_FOREVER_REL,
1443 &process_local_reply, pr);
1446 GNUNET_STATISTICS_update (GSF_stats,
1448 ("# Datastore lookups concluded (error queueing)"),
1450 goto check_error_and_continue;
1455 prq.priority = priority;
1456 prq.request_found = GNUNET_NO;
1457 prq.anonymity_level = anonymity;
1458 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1459 GSF_update_datastore_delay_ (pr->public_data.start_time);
1460 process_reply (&prq, key, pr);
1461 pr->local_result = prq.eval;
1462 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1464 GNUNET_STATISTICS_update (GSF_stats,
1466 ("# Datastore lookups concluded (found last result)"),
1468 goto check_error_and_continue;
1470 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1471 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1472 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1475 GNUNET_STATISTICS_update (GSF_stats,
1477 ("# Datastore lookups concluded (load too high)"),
1479 goto check_error_and_continue;
1481 pr->qe_start = GNUNET_TIME_absolute_get ();
1483 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1486 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1487 &pr->public_data.query,
1488 pr->public_data.type ==
1489 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1490 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1492 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1493 public_data.options)) ? UINT_MAX : 1
1494 /* queue priority */ ,
1496 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1497 public_data.options)) ? UINT_MAX :
1498 GSF_datastore_queue_size
1499 /* max queue size */ ,
1500 GNUNET_TIME_UNIT_FOREVER_REL,
1501 &process_local_reply, pr);
1502 /* check if we successfully queued another datastore request;
1503 * if so, return, otherwise call our continuation (if we have
1505 check_error_and_continue:
1508 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1510 GNUNET_SCHEDULER_cancel (pr->warn_task);
1511 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1513 if (NULL == (cont = pr->llc_cont))
1514 return; /* no continuation */
1515 pr->llc_cont = NULL;
1516 cont (pr->llc_cont_cls, pr, pr->local_result);
1521 * Is the given target a legitimate peer for forwarding the given request?
1525 * @return GNUNET_YES if this request could be forwarded to the given peer
1528 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1529 const struct GNUNET_PeerIdentity *target)
1531 struct GNUNET_PeerIdentity pi;
1533 if (0 == pr->origin_pid)
1535 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1537 memcmp (&pi, target,
1538 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1544 * Look up the request in the local datastore.
1546 * @param pr the pending request to process
1547 * @param cont function to call at the end
1548 * @param cont_cls closure for cont
1551 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1552 GSF_LocalLookupContinuation cont, void *cont_cls)
1554 GNUNET_assert (NULL == pr->gh);
1555 GNUNET_assert (NULL == pr->mesh_request);
1556 GNUNET_assert (NULL == pr->llc_cont);
1557 pr->llc_cont = cont;
1558 pr->llc_cont_cls = cont_cls;
1559 pr->qe_start = GNUNET_TIME_absolute_get ();
1561 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1563 #if INSANE_STATISTICS
1564 GNUNET_STATISTICS_update (GSF_stats,
1565 gettext_noop ("# Datastore lookups initiated"), 1,
1569 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1570 &pr->public_data.query,
1571 pr->public_data.type ==
1572 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1573 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1575 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1576 public_data.options)) ? UINT_MAX : 1
1577 /* queue priority */ ,
1579 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1580 public_data.options)) ? UINT_MAX :
1581 GSF_datastore_queue_size
1582 /* max queue size */ ,
1583 GNUNET_TIME_UNIT_FOREVER_REL,
1584 &process_local_reply, pr);
1587 GNUNET_STATISTICS_update (GSF_stats,
1589 ("# Datastore lookups concluded (error queueing)"),
1591 GNUNET_SCHEDULER_cancel (pr->warn_task);
1592 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1593 pr->llc_cont = NULL;
1595 cont (cont_cls, pr, pr->local_result);
1601 * Handle P2P "CONTENT" message. Checks that the message is
1602 * well-formed and then checks if there are any pending requests for
1603 * this content and possibly passes it on (to local clients or other
1604 * peers). Does NOT perform migration (content caching at this peer).
1606 * @param cp the other peer involved (sender or receiver, NULL
1607 * for loopback messages where we are both sender and receiver)
1608 * @param message the actual message
1609 * @return GNUNET_OK if the message was well-formed,
1610 * GNUNET_SYSERR if the message was malformed (close connection,
1611 * do not cache under any circumstances)
1614 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1615 const struct GNUNET_MessageHeader *message)
1617 const struct PutMessage *put;
1620 enum GNUNET_BLOCK_Type type;
1621 struct GNUNET_TIME_Absolute expiration;
1622 struct GNUNET_HashCode query;
1623 struct ProcessReplyClosure prq;
1624 struct GNUNET_TIME_Relative block_time;
1626 struct PutMigrationContext *pmc;
1628 msize = ntohs (message->size);
1629 if (msize < sizeof (struct PutMessage))
1631 GNUNET_break_op (0);
1632 return GNUNET_SYSERR;
1634 put = (const struct PutMessage *) message;
1635 dsize = msize - sizeof (struct PutMessage);
1636 type = ntohl (put->type);
1637 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1638 /* do not allow migrated content to live longer than 1 year */
1639 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1641 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1642 return GNUNET_SYSERR;
1644 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1646 GNUNET_break_op (0);
1647 return GNUNET_SYSERR;
1649 GNUNET_STATISTICS_update (GSF_stats,
1650 gettext_noop ("# GAP PUT messages received"), 1,
1652 /* now, lookup 'query' */
1653 prq.data = (const void *) &put[1];
1657 prq.expiration = expiration;
1659 prq.anonymity_level = UINT32_MAX;
1660 prq.request_found = GNUNET_NO;
1661 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1665 GSF_connected_peer_change_preference_ (cp,
1666 CONTENT_BANDWIDTH_VALUE +
1667 1000 * prq.priority);
1668 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1670 if ((GNUNET_YES == active_to_migration) &&
1672 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675 "Replicating result for query `%s' with priority %u\n",
1676 GNUNET_h2s (&query), prq.priority);
1677 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1678 pmc->start = GNUNET_TIME_absolute_get ();
1679 pmc->requested = prq.request_found;
1680 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1681 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1684 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1685 prq.priority, 1 /* anonymity */ ,
1686 0 /* replication */ ,
1687 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1688 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1689 &put_migration_continuation, pmc))
1691 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1694 else if (NULL != cp)
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697 "Choosing not to keep content `%s' (%d/%d)\n",
1698 GNUNET_h2s (&query), active_to_migration,
1699 test_put_load_too_high (prq.priority));
1701 putl = GNUNET_LOAD_get_load (datastore_put_load);
1702 if ( (NULL != cp) &&
1703 (GNUNET_NO == prq.request_found) &&
1704 ( (GNUNET_YES != active_to_migration) ||
1705 (putl > 2.5 * (1 + prq.priority)) ) )
1707 if (GNUNET_YES != active_to_migration)
1708 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1710 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1712 GNUNET_CRYPTO_random_u32
1713 (GNUNET_CRYPTO_QUALITY_WEAK,
1714 (unsigned int) (60000 * putl * putl)));
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1717 GNUNET_STRINGS_relative_time_to_string (block_time,
1720 active_to_migration,
1721 (GNUNET_NO == prq.request_found));
1722 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1729 * Setup the subsystem.
1732 GSF_pending_request_init_ ()
1735 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1736 "MAX_PENDING_REQUESTS",
1737 &max_pending_requests))
1739 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1740 "fs", "MAX_PENDING_REQUESTS");
1742 active_to_migration =
1743 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1744 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1745 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1746 requests_by_expiration_heap =
1747 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1752 * Shutdown the subsystem.
1755 GSF_pending_request_done_ ()
1757 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1758 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1760 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1761 requests_by_expiration_heap = NULL;
1762 GNUNET_LOAD_value_free (datastore_put_load);
1763 datastore_put_load = NULL;
1767 /* end of gnunet-service-fs_pr.c */