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"
33 #include "gnunet-service-fs_stream.h"
37 * Desired replication level for GETs.
39 #define DHT_GET_REPLICATION 5
42 * Maximum size of the datastore queue for P2P operations. Needs to
43 * be large enough to queue MAX_QUEUE_PER_PEER operations for roughly
44 * the number of active (connected) peers.
46 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
49 * Bandwidth value of a 0-priority content (must be fairly high
50 * compared to query since content is typically significantly larger
51 * -- and more valueable since it can take many queries to get one
54 #define CONTENT_BANDWIDTH_VALUE 800
57 * Hard limit on the number of results we may get from the datastore per query.
59 #define MAX_RESULTS (100 * 1024)
62 * Collect an instane number of statistics? May cause excessive IPC.
64 #define INSANE_STATISTICS GNUNET_NO
69 struct GSF_PendingRequest
72 * Public data for the request.
74 struct GSF_PendingRequestData public_data;
77 * Function to call if we encounter a reply.
79 GSF_PendingRequestReplyHandler rh;
87 * Array of hash codes of replies we've already seen.
89 struct GNUNET_HashCode *replies_seen;
92 * Bloomfilter masking replies we've already seen.
94 struct GNUNET_CONTAINER_BloomFilter *bf;
97 * Entry for this pending request in the expiration heap, or NULL.
99 struct GNUNET_CONTAINER_HeapNode *hnode;
102 * Datastore queue entry for this request (or NULL for none).
104 struct GNUNET_DATASTORE_QueueEntry *qe;
107 * DHT request handle for this request (or NULL for none).
109 struct GNUNET_DHT_GetHandle *gh;
112 * Stream request handle for this request (or NULL for none).
114 struct GSF_StreamRequest *stream_request;
117 * Function to call upon completion of the local get
118 * request, or NULL for none.
120 GSF_LocalLookupContinuation llc_cont;
123 * Closure for llc_cont.
128 * Last result from the local datastore lookup evaluation.
130 enum GNUNET_BLOCK_EvaluationResult local_result;
133 * Identity of the peer that we should use for the 'sender'
134 * (recipient of the response) when forwarding (0 for none).
136 GNUNET_PEER_Id sender_pid;
139 * Identity of the peer that we should never forward this query
140 * to since it originated this query (0 for none).
142 GNUNET_PEER_Id origin_pid;
145 * Time we started the last datastore lookup.
147 struct GNUNET_TIME_Absolute qe_start;
150 * Task that warns us if the local datastore lookup takes too long.
152 GNUNET_SCHEDULER_TaskIdentifier warn_task;
155 * Current offset for querying our local datastore for results.
156 * Starts at a random value, incremented until we get the same
157 * UID again (detected using 'first_uid'), which is then used
158 * to termiante the iteration.
160 uint64_t local_result_offset;
163 * Unique ID of the first result from the local datastore;
164 * used to detect wrap-around of the offset.
169 * Number of valid entries in the 'replies_seen' array.
171 unsigned int replies_seen_count;
174 * Length of the 'replies_seen' array.
176 unsigned int replies_seen_size;
179 * Mingle value we currently use for the bf.
184 * Do we have a first UID yet?
186 unsigned int have_first_uid;
192 * All pending requests, ordered by the query. Entries
193 * are of type 'struct GSF_PendingRequest*'.
195 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
199 * Datastore 'PUT' load tracking.
201 static struct GNUNET_LOAD_Value *datastore_put_load;
205 * Are we allowed to migrate content to this peer.
207 static int active_to_migration;
211 * Heap with the request that will expire next at the top. Contains
212 * pointers of type "struct PendingRequest*"; these will *also* be
213 * aliased from the "requests_by_peer" data structures and the
214 * "requests_by_query" table. Note that requests from our clients
215 * don't expire and are thus NOT in the "requests_by_expiration"
216 * (or the "requests_by_peer" tables).
218 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
222 * Maximum number of requests (from other peers, overall) that we're
223 * willing to have pending at any given point in time. Can be changed
224 * via the configuration file (32k is just the default).
226 static unsigned long long max_pending_requests = (32 * 1024);
231 * Recalculate our bloom filter for filtering replies. This function
232 * will create a new bloom filter from scratch, so it should only be
233 * called if we have no bloomfilter at all (and hence can create a
234 * fresh one of minimal size without problems) OR if our peer is the
235 * initiator (in which case we may resize to larger than mimimum size).
237 * @param pr request for which the BF is to be recomputed
240 refresh_bloomfilter (struct GSF_PendingRequest *pr)
243 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
245 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
247 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
248 pr->replies_seen_count);
253 * Create a new pending request.
255 * @param options request options
256 * @param type type of the block that is being requested
257 * @param query key for the lookup
258 * @param namespace namespace to lookup, NULL for no namespace
259 * @param target preferred target for the request, NULL for none
260 * @param bf_data raw data for bloom filter for known replies, can be NULL
261 * @param bf_size number of bytes in bf_data
262 * @param mingle mingle value for bf
263 * @param anonymity_level desired anonymity level
264 * @param priority maximum outgoing cummulative request priority to use
265 * @param ttl current time-to-live for the request
266 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
267 * @param origin_pid peer ID of origin of query (do not loop back)
268 * @param replies_seen hash codes of known local replies
269 * @param replies_seen_count size of the 'replies_seen' array
270 * @param rh handle to call when we get a reply
271 * @param rh_cls closure for rh
272 * @return handle for the new pending request
274 struct GSF_PendingRequest *
275 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
276 enum GNUNET_BLOCK_Type type,
277 const struct GNUNET_HashCode *query,
278 const struct GNUNET_HashCode *namespace,
279 const struct GNUNET_PeerIdentity *target,
280 const char *bf_data, size_t bf_size,
281 uint32_t mingle, uint32_t anonymity_level,
282 uint32_t priority, int32_t ttl,
283 GNUNET_PEER_Id sender_pid,
284 GNUNET_PEER_Id origin_pid,
285 const struct GNUNET_HashCode *replies_seen,
286 unsigned int replies_seen_count,
287 GSF_PendingRequestReplyHandler rh, void *rh_cls)
289 struct GSF_PendingRequest *pr;
290 struct GSF_PendingRequest *dpr;
292 struct GNUNET_HashCode *eptr;
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Creating request handle for `%s' of type %d\n",
296 GNUNET_h2s (query), type);
297 #if INSANE_STATISTICS
298 GNUNET_STATISTICS_update (GSF_stats,
299 gettext_noop ("# Pending requests created"), 1,
303 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
304 extra += sizeof (struct GNUNET_HashCode);
306 extra += sizeof (struct GNUNET_PeerIdentity);
307 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
308 pr->local_result_offset =
309 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
310 pr->public_data.query = *query;
311 eptr = (struct GNUNET_HashCode *) &pr[1];
312 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
314 GNUNET_assert (NULL != namespace);
315 pr->public_data.namespace = eptr;
316 memcpy (eptr, namespace, sizeof (struct GNUNET_HashCode));
321 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
322 memcpy (eptr, target, sizeof (struct GNUNET_PeerIdentity));
324 pr->public_data.anonymity_level = anonymity_level;
325 pr->public_data.priority = priority;
326 pr->public_data.original_priority = priority;
327 pr->public_data.options = options;
328 pr->public_data.type = type;
329 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
330 pr->sender_pid = sender_pid;
331 pr->origin_pid = origin_pid;
334 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
336 pr->public_data.ttl =
337 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
338 (GNUNET_TIME_UNIT_SECONDS,
341 pr->public_data.ttl =
342 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
343 GNUNET_TIME_relative_multiply
344 (GNUNET_TIME_UNIT_SECONDS,
346 if (replies_seen_count > 0)
348 pr->replies_seen_size = replies_seen_count;
350 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
351 memcpy (pr->replies_seen, replies_seen,
352 replies_seen_count * sizeof (struct GNUNET_HashCode));
353 pr->replies_seen_count = replies_seen_count;
358 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size,
359 GNUNET_CONSTANTS_BLOOMFILTER_K);
362 else if ((replies_seen_count > 0) &&
363 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
365 refresh_bloomfilter (pr);
367 GNUNET_CONTAINER_multihashmap_put (pr_map,
368 &pr->public_data.query, pr,
369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
370 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
373 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
374 pr->public_data.ttl.abs_value);
375 /* make sure we don't track too many requests */
376 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
377 max_pending_requests)
379 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
380 GNUNET_assert (dpr != NULL);
382 break; /* let the request live briefly... */
384 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
385 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_ABS,
386 GNUNET_BLOCK_TYPE_ANY, NULL, 0);
387 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
390 GNUNET_STATISTICS_update (GSF_stats,
391 gettext_noop ("# Pending requests active"), 1,
397 * Obtain the public data associated with a pending request
399 * @param pr pending request
400 * @return associated public data
402 struct GSF_PendingRequestData *
403 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
405 return &pr->public_data;
410 * Test if two pending requests are compatible (would generate
411 * the same query modulo filters and should thus be processed
414 * @param pra a pending request
415 * @param prb another pending request
416 * @return GNUNET_OK if the requests are compatible
419 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
420 struct GSF_PendingRequest *prb)
422 if ((pra->public_data.type != prb->public_data.type) ||
424 memcmp (&pra->public_data.query, &prb->public_data.query,
425 sizeof (struct GNUNET_HashCode))) ||
426 ((pra->public_data.type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
428 memcmp (pra->public_data.namespace,
429 prb->public_data.namespace,
430 sizeof (struct GNUNET_HashCode)))))
438 * Update a given pending request with additional replies
439 * that have been seen.
441 * @param pr request to update
442 * @param replies_seen hash codes of replies that we've seen
443 * @param replies_seen_count size of the replies_seen array
446 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
447 const struct GNUNET_HashCode * replies_seen,
448 unsigned int replies_seen_count)
451 struct GNUNET_HashCode mhash;
453 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
454 return; /* integer overflow */
455 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
457 /* we're responsible for the BF, full refresh */
458 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
459 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
460 replies_seen_count + pr->replies_seen_count);
461 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
462 sizeof (struct GNUNET_HashCode) * replies_seen_count);
463 pr->replies_seen_count += replies_seen_count;
464 refresh_bloomfilter (pr);
470 /* we're not the initiator, but the initiator did not give us
471 * any bloom-filter, so we need to create one on-the-fly */
473 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
475 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, replies_seen,
480 for (i = 0; i < pr->replies_seen_count; i++)
482 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
483 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
488 GNUNET_DHT_get_filter_known_results (pr->gh,
495 * Generate the message corresponding to the given pending request for
496 * transmission to other peers (or at least determine its size).
498 * @param pr request to generate the message for
499 * @param buf_size number of bytes available in buf
500 * @param buf where to copy the message (can be NULL)
501 * @return number of bytes needed (if > buf_size) or used
504 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
505 size_t buf_size, void *buf)
507 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
508 struct GetMessage *gm;
509 struct GNUNET_HashCode *ext;
515 struct GNUNET_TIME_Absolute now;
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Building request message for `%s' of type %d\n",
522 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
525 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
526 if ((!do_route) && (pr->sender_pid == 0))
529 do_route = GNUNET_YES;
533 bm |= GET_MESSAGE_BIT_RETURN_TO;
536 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
538 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
541 if (NULL != pr->public_data.target)
543 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
546 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
547 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (struct GNUNET_HashCode);
548 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
549 if (buf_size < msize)
551 gm = (struct GetMessage *) lbuf;
552 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
553 gm->header.size = htons (msize);
554 gm->type = htonl (pr->public_data.type);
557 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
558 pr->public_data.priority + 1);
561 pr->public_data.priority -= prio;
562 pr->public_data.num_transmissions++;
563 pr->public_data.respect_offered += prio;
564 gm->priority = htonl (prio);
565 now = GNUNET_TIME_absolute_get ();
566 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
567 gm->ttl = htonl (ttl / 1000);
568 gm->filter_mutator = htonl (pr->mingle);
569 gm->hash_bitmap = htonl (bm);
570 gm->query = pr->public_data.query;
571 ext = (struct GNUNET_HashCode *) & gm[1];
574 GNUNET_PEER_resolve (pr->sender_pid,
575 (struct GNUNET_PeerIdentity *) &ext[k++]);
576 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
577 memcpy (&ext[k++], pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
578 if (NULL != pr->public_data.target)
580 pr->public_data.target,
581 sizeof (struct GNUNET_PeerIdentity));
583 GNUNET_assert (GNUNET_SYSERR !=
584 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
587 memcpy (buf, gm, msize);
593 * Iterator to free pending requests.
595 * @param cls closure, unused
596 * @param key current key code
597 * @param value value in the hash map (pending request)
598 * @return GNUNET_YES (we should continue to iterate)
601 clean_request (void *cls, const struct GNUNET_HashCode * key, void *value)
603 struct GSF_PendingRequest *pr = value;
604 GSF_LocalLookupContinuation cont;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
608 if (NULL != (cont = pr->llc_cont))
611 cont (pr->llc_cont_cls, pr, pr->local_result);
613 GSF_plan_notify_request_done_ (pr);
614 GNUNET_free_non_null (pr->replies_seen);
617 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
620 GNUNET_PEER_change_rc (pr->sender_pid, -1);
622 GNUNET_PEER_change_rc (pr->origin_pid, -1);
624 if (NULL != pr->hnode)
626 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
631 GNUNET_DATASTORE_cancel (pr->qe);
636 GNUNET_DHT_get_stop (pr->gh);
639 if (NULL != pr->stream_request)
641 GSF_stream_query_cancel (pr->stream_request);
642 pr->stream_request = NULL;
644 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
646 GNUNET_SCHEDULER_cancel (pr->warn_task);
647 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
649 GNUNET_assert (GNUNET_OK ==
650 GNUNET_CONTAINER_multihashmap_remove (pr_map,
651 &pr->public_data.query,
653 GNUNET_STATISTICS_update (GSF_stats,
654 gettext_noop ("# Pending requests active"), -1,
662 * Explicitly cancel a pending request.
664 * @param pr request to cancel
665 * @param full_cleanup fully purge the request
668 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
670 GSF_LocalLookupContinuation cont;
673 return; /* already cleaned up! */
674 if (GNUNET_YES != full_cleanup)
676 /* make request inactive (we're no longer interested in more results),
677 * but do NOT remove from our data-structures, we still need it there
678 * to prevent the request from looping */
680 if (NULL != (cont = pr->llc_cont))
683 cont (pr->llc_cont_cls, pr, pr->local_result);
685 GSF_plan_notify_request_done_ (pr);
688 GNUNET_DATASTORE_cancel (pr->qe);
693 GNUNET_DHT_get_stop (pr->gh);
696 if (NULL != pr->stream_request)
698 GSF_stream_query_cancel (pr->stream_request);
699 pr->stream_request = NULL;
701 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
703 GNUNET_SCHEDULER_cancel (pr->warn_task);
704 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
708 GNUNET_assert (GNUNET_YES ==
709 clean_request (NULL, &pr->public_data.query, pr));
714 * Iterate over all pending requests.
716 * @param it function to call for each request
717 * @param cls closure for it
720 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
722 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
723 (GNUNET_CONTAINER_HashMapIterator) it,
731 * Closure for "process_reply" function.
733 struct ProcessReplyClosure
736 * The data for the reply.
741 * Who gave us this reply? NULL for local host (or DHT)
743 struct GSF_ConnectedPeer *sender;
746 * When the reply expires.
748 struct GNUNET_TIME_Absolute expiration;
758 enum GNUNET_BLOCK_Type type;
761 * How much was this reply worth to us?
766 * Anonymity requirements for this reply.
768 uint32_t anonymity_level;
771 * Evaluation result (returned).
773 enum GNUNET_BLOCK_EvaluationResult eval;
776 * Did we find a matching request?
783 * Update the performance data for the sender (if any) since
784 * the sender successfully answered one of our queries.
786 * @param prq information about the sender
787 * @param pr request that was satisfied
790 update_request_performance_data (struct ProcessReplyClosure *prq,
791 struct GSF_PendingRequest *pr)
793 if (prq->sender == NULL)
795 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
801 * We have received a reply; handle it!
803 * @param cls response (struct ProcessReplyClosure)
804 * @param key our query
805 * @param value value in the hash map (info about the query)
806 * @return GNUNET_YES (we should continue to iterate)
809 process_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
811 struct ProcessReplyClosure *prq = cls;
812 struct GSF_PendingRequest *pr = value;
813 struct GNUNET_HashCode chash;
814 struct GNUNET_TIME_Absolute last_transmission;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Matched result (type %u) for query `%s' with pending request\n",
820 (unsigned int) prq->type, GNUNET_h2s (key));
821 GNUNET_STATISTICS_update (GSF_stats,
822 gettext_noop ("# replies received and matched"), 1,
825 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
826 pr->public_data.namespace,
828 GNUNET_BLOCK_TYPE_FS_SBLOCK) ?
829 sizeof (struct GNUNET_HashCode) : 0, prq->data,
833 case GNUNET_BLOCK_EVALUATION_OK_MORE:
834 update_request_performance_data (prq, pr);
836 case GNUNET_BLOCK_EVALUATION_OK_LAST:
837 /* short cut: stop processing early, no BF-update, etc. */
838 update_request_performance_data (prq, pr);
839 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
840 GNUNET_TIME_absolute_get_duration (pr->
841 public_data.start_time).rel_value);
843 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
846 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
847 /* pass on to other peers / local clients */
848 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
849 last_transmission, prq->type, prq->data, prq->size);
851 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
852 #if INSANE_STATISTICS
853 GNUNET_STATISTICS_update (GSF_stats,
855 ("# duplicate replies discarded (bloomfilter)"),
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Duplicate response, discarding.\n");
860 return GNUNET_YES; /* duplicate */
861 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
862 GNUNET_STATISTICS_update (GSF_stats,
864 ("# irrelevant replies discarded"),
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867 "Irrelevant response, ignoring.\n");
869 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
870 return GNUNET_YES; /* wrong namespace */
871 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
874 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
877 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
882 /* update bloomfilter */
883 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
884 GSF_pending_request_update_ (pr, &chash, 1);
885 if (NULL == prq->sender)
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Found result for query `%s' in local datastore\n",
890 GNUNET_STATISTICS_update (GSF_stats,
891 gettext_noop ("# results found locally"), 1,
896 GSF_dht_lookup_ (pr);
898 prq->priority += pr->public_data.original_priority;
899 pr->public_data.priority = 0;
900 pr->public_data.original_priority = 0;
901 pr->public_data.results_found++;
902 prq->request_found = GNUNET_YES;
903 /* finally, pass on to other peer / local client */
904 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
907 last_transmission.abs_value = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
908 pr->rh (pr->rh_cls, prq->eval, pr,
909 prq->anonymity_level, prq->expiration,
910 last_transmission, prq->type, prq->data, prq->size);
916 * Context for the 'put_migration_continuation'.
918 struct PutMigrationContext
922 * Start time for the operation.
924 struct GNUNET_TIME_Absolute start;
929 struct GNUNET_PeerIdentity origin;
932 * GNUNET_YES if we had a matching request for this block,
940 * Continuation called to notify client about result of the
944 * @param success GNUNET_SYSERR on failure
945 * @param min_expiration minimum expiration time required for content to be stored
946 * @param msg NULL on success, otherwise an error message
949 put_migration_continuation (void *cls, int success,
950 struct GNUNET_TIME_Absolute min_expiration,
953 struct PutMigrationContext *pmc = cls;
954 struct GSF_ConnectedPeer *cp;
955 struct GNUNET_TIME_Relative mig_pause;
956 struct GSF_PeerPerformanceData *ppd;
958 if (NULL != datastore_put_load)
960 if (GNUNET_SYSERR != success)
962 GNUNET_LOAD_update (datastore_put_load,
963 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value);
967 /* on queue failure / timeout, increase the put load dramatically */
968 GNUNET_LOAD_update (datastore_put_load,
969 GNUNET_TIME_UNIT_MINUTES.rel_value);
972 cp = GSF_peer_get_ (&pmc->origin);
973 if (GNUNET_OK == success)
977 ppd = GSF_get_peer_performance_data_ (cp);
978 ppd->migration_delay.rel_value /= 2;
983 if ( (GNUNET_NO == success) &&
984 (GNUNET_NO == pmc->requested) &&
987 ppd = GSF_get_peer_performance_data_ (cp);
988 if (min_expiration.abs_value > 0)
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Asking to stop migration for %s because datastore is full\n",
992 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
993 GSF_block_peer_migration_ (cp, min_expiration);
997 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
998 ppd->migration_delay);
999 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1000 ppd->migration_delay);
1001 mig_pause.rel_value = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1002 ppd->migration_delay.rel_value);
1003 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Replicated content already exists locally, asking to stop migration for %s\n",
1006 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
1007 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (mig_pause));
1011 GNUNET_STATISTICS_update (GSF_stats,
1012 gettext_noop ("# Datastore `PUT' failures"), 1,
1018 * Test if the DATABASE (PUT) load on this peer is too high
1019 * to even consider processing the query at
1022 * @return GNUNET_YES if the load is too high to do anything (load high)
1023 * GNUNET_NO to process normally (load normal or low)
1026 test_put_load_too_high (uint32_t priority)
1030 if (NULL == datastore_put_load)
1032 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1033 return GNUNET_NO; /* very fast */
1034 ld = GNUNET_LOAD_get_load (datastore_put_load);
1035 if (ld < 2.0 * (1 + priority))
1037 GNUNET_STATISTICS_update (GSF_stats,
1039 ("# storage requests dropped due to high load"), 1,
1046 * Iterator called on each result obtained for a DHT
1047 * operation that expects a reply
1049 * @param cls closure
1050 * @param exp when will this value expire
1051 * @param key key of the result
1052 * @param get_path peers on reply path (or NULL if not recorded)
1053 * @param get_path_length number of entries in get_path
1054 * @param put_path peers on the PUT path (or NULL if not recorded)
1055 * @param put_path_length number of entries in get_path
1056 * @param type type of the result
1057 * @param size number of bytes in data
1058 * @param data pointer to the result data
1061 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
1062 const struct GNUNET_HashCode * key,
1063 const struct GNUNET_PeerIdentity *get_path,
1064 unsigned int get_path_length,
1065 const struct GNUNET_PeerIdentity *put_path,
1066 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1067 size_t size, const void *data)
1069 struct GSF_PendingRequest *pr = cls;
1070 struct ProcessReplyClosure prq;
1071 struct PutMigrationContext *pmc;
1073 GNUNET_STATISTICS_update (GSF_stats,
1074 gettext_noop ("# Replies received from DHT"), 1,
1076 memset (&prq, 0, sizeof (prq));
1078 prq.expiration = exp;
1079 /* do not allow migrated content to live longer than 1 year */
1080 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1084 process_reply (&prq, key, pr);
1085 if ((GNUNET_YES == active_to_migration) &&
1086 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "Replicating result for query `%s' with priority %u\n",
1090 GNUNET_h2s (key), prq.priority);
1091 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1092 pmc->start = GNUNET_TIME_absolute_get ();
1093 pmc->requested = GNUNET_YES;
1095 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1097 0 /* replication */ ,
1098 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1099 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1100 &put_migration_continuation, pmc))
1102 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1109 * Consider looking up the data in the DHT (anonymity-level permitting).
1111 * @param pr the pending request to process
1114 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1118 struct GNUNET_PeerIdentity pi;
1119 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1121 if (0 != pr->public_data.anonymity_level)
1125 GNUNET_DHT_get_stop (pr->gh);
1130 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1133 memcpy (buf, pr->public_data.namespace, sizeof (struct GNUNET_HashCode));
1134 xquery_size = sizeof (struct GNUNET_HashCode);
1136 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1138 GNUNET_assert (0 != pr->sender_pid);
1139 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1140 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1141 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1144 GNUNET_DHT_get_start (GSF_dht,
1145 pr->public_data.type, &pr->public_data.query,
1146 DHT_GET_REPLICATION,
1147 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1148 xquery, xquery_size, &handle_dht_reply, pr);
1149 if ( (NULL != pr->gh) &&
1150 (0 != pr->replies_seen_count) )
1151 GNUNET_DHT_get_filter_known_results (pr->gh,
1152 pr->replies_seen_count,
1158 * Function called with a reply from the stream.
1160 * @param cls the pending request struct
1161 * @param type type of the block, ANY on error
1162 * @param expiration expiration time for the block
1163 * @param data_size number of bytes in 'data', 0 on error
1164 * @param data reply block data, NULL on error
1167 stream_reply_proc (void *cls,
1168 enum GNUNET_BLOCK_Type type,
1169 struct GNUNET_TIME_Absolute expiration,
1173 struct GSF_PendingRequest *pr = cls;
1174 struct ProcessReplyClosure prq;
1175 struct GNUNET_HashCode query;
1177 pr->stream_request = NULL;
1179 GNUNET_BLOCK_get_key (GSF_block_ctx,
1181 data, data_size, &query))
1183 GNUNET_break_op (0);
1186 GNUNET_STATISTICS_update (GSF_stats,
1187 gettext_noop ("# Replies received from STREAM"), 1,
1189 memset (&prq, 0, sizeof (prq));
1191 prq.expiration = expiration;
1192 /* do not allow migrated content to live longer than 1 year */
1193 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1195 prq.size = data_size;
1197 process_reply (&prq, &query, pr);
1202 * Consider downloading via stream (if possible)
1204 * @param pr the pending request to process
1207 GSF_stream_lookup_ (struct GSF_PendingRequest *pr)
1209 if (0 != pr->public_data.anonymity_level)
1211 if (0 == pr->public_data.target)
1213 if (NULL != pr->stream_request)
1215 pr->stream_request = GSF_stream_query (pr->public_data.target,
1216 &pr->public_data.query,
1217 pr->public_data.type,
1224 * Task that issues a warning if the datastore lookup takes too long.
1226 * @param cls the 'struct GSF_PendingRequest'
1227 * @param tc task context
1230 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1232 struct GSF_PendingRequest *pr = cls;
1234 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1235 _("Datastore lookup already took %s!\n"),
1236 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1238 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1244 * Task that issues a warning if the datastore lookup takes too long.
1246 * @param cls the 'struct GSF_PendingRequest'
1247 * @param tc task context
1250 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1252 struct GSF_PendingRequest *pr = cls;
1254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1255 _("On-demand lookup already took %s!\n"),
1256 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1258 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1259 &odc_warn_delay_task, pr);
1264 * We're processing (local) results for a search request
1265 * from another peer. Pass applicable results to the
1266 * peer and if we are done either clean up (operation
1267 * complete) or forward to other peers (more results possible).
1269 * @param cls our closure (struct PendingRequest)
1270 * @param key key for the content
1271 * @param size number of bytes in data
1272 * @param data content stored
1273 * @param type type of the content
1274 * @param priority priority of the content
1275 * @param anonymity anonymity-level for the content
1276 * @param expiration expiration time for the content
1277 * @param uid unique identifier for the datum;
1278 * maybe 0 if no unique identifier is available
1281 process_local_reply (void *cls, const struct GNUNET_HashCode * key, size_t size,
1282 const void *data, enum GNUNET_BLOCK_Type type,
1283 uint32_t priority, uint32_t anonymity,
1284 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1286 struct GSF_PendingRequest *pr = cls;
1287 GSF_LocalLookupContinuation cont;
1288 struct ProcessReplyClosure prq;
1289 struct GNUNET_HashCode query;
1290 unsigned int old_rf;
1292 GNUNET_SCHEDULER_cancel (pr->warn_task);
1293 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1299 #if INSANE_STATISTICS
1300 GNUNET_STATISTICS_update (GSF_stats,
1302 ("# Datastore lookups concluded (no results)"),
1306 if (GNUNET_NO == pr->have_first_uid)
1308 pr->first_uid = uid;
1309 pr->have_first_uid = 1;
1313 if ((uid == pr->first_uid) && (key != NULL))
1315 GNUNET_STATISTICS_update (GSF_stats,
1317 ("# Datastore lookups concluded (seen all)"),
1319 key = NULL; /* all replies seen! */
1321 pr->have_first_uid++;
1322 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1324 GNUNET_STATISTICS_update (GSF_stats,
1326 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1328 key = NULL; /* all replies seen! */
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1335 "No further local responses available.\n");
1336 #if INSANE_STATISTICS
1337 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1338 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1339 GNUNET_STATISTICS_update (GSF_stats,
1341 ("# requested DBLOCK or IBLOCK not found"), 1,
1344 goto check_error_and_continue;
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1348 GNUNET_h2s (key), type, (unsigned long long) uid);
1349 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "Found ONDEMAND block, performing on-demand encoding\n");
1353 GNUNET_STATISTICS_update (GSF_stats,
1355 ("# on-demand blocks matched requests"), 1,
1357 pr->qe_start = GNUNET_TIME_absolute_get ();
1359 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1360 &odc_warn_delay_task, pr);
1362 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1363 anonymity, expiration, uid,
1364 &process_local_reply, pr))
1366 GNUNET_STATISTICS_update (GSF_stats,
1368 ("# on-demand lookups performed successfully"),
1370 return; /* we're done */
1372 GNUNET_STATISTICS_update (GSF_stats,
1373 gettext_noop ("# on-demand lookups failed"), 1,
1375 GNUNET_SCHEDULER_cancel (pr->warn_task);
1377 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1378 &warn_delay_task, pr);
1380 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1381 &pr->public_data.query,
1382 pr->public_data.type ==
1383 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1384 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1386 (GSF_PRO_PRIORITY_UNLIMITED &
1387 pr->public_data.options)) ? UINT_MAX : 1
1388 /* queue priority */ ,
1390 (GSF_PRO_PRIORITY_UNLIMITED &
1391 pr->public_data.options)) ? UINT_MAX :
1392 GSF_datastore_queue_size
1393 /* max queue size */ ,
1394 GNUNET_TIME_UNIT_FOREVER_REL,
1395 &process_local_reply, pr);
1397 return; /* we're done */
1398 GNUNET_STATISTICS_update (GSF_stats,
1400 ("# Datastore lookups concluded (error queueing)"),
1402 goto check_error_and_continue;
1404 old_rf = pr->public_data.results_found;
1405 memset (&prq, 0, sizeof (prq));
1407 prq.expiration = expiration;
1410 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1413 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1414 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1415 pr->qe_start = GNUNET_TIME_absolute_get ();
1417 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1418 &warn_delay_task, pr);
1420 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1421 &pr->public_data.query,
1422 pr->public_data.type ==
1423 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1424 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1426 (GSF_PRO_PRIORITY_UNLIMITED &
1427 pr->public_data.options)) ? UINT_MAX : 1
1428 /* queue priority */ ,
1430 (GSF_PRO_PRIORITY_UNLIMITED &
1431 pr->public_data.options)) ? UINT_MAX :
1432 GSF_datastore_queue_size
1433 /* max queue size */ ,
1434 GNUNET_TIME_UNIT_FOREVER_REL,
1435 &process_local_reply, pr);
1438 GNUNET_STATISTICS_update (GSF_stats,
1440 ("# Datastore lookups concluded (error queueing)"),
1442 goto check_error_and_continue;
1447 prq.priority = priority;
1448 prq.request_found = GNUNET_NO;
1449 prq.anonymity_level = anonymity;
1450 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1451 GSF_update_datastore_delay_ (pr->public_data.start_time);
1452 process_reply (&prq, key, pr);
1453 pr->local_result = prq.eval;
1454 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1456 GNUNET_STATISTICS_update (GSF_stats,
1458 ("# Datastore lookups concluded (found last result)"),
1460 goto check_error_and_continue;
1462 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1463 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1464 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1467 GNUNET_STATISTICS_update (GSF_stats,
1469 ("# Datastore lookups concluded (load too high)"),
1471 goto check_error_and_continue;
1473 pr->qe_start = GNUNET_TIME_absolute_get ();
1475 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1478 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1479 &pr->public_data.query,
1480 pr->public_data.type ==
1481 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1482 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1484 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1485 public_data.options)) ? UINT_MAX : 1
1486 /* queue priority */ ,
1488 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1489 public_data.options)) ? UINT_MAX :
1490 GSF_datastore_queue_size
1491 /* max queue size */ ,
1492 GNUNET_TIME_UNIT_FOREVER_REL,
1493 &process_local_reply, pr);
1494 /* check if we successfully queued another datastore request;
1495 * if so, return, otherwise call our continuation (if we have
1497 check_error_and_continue:
1500 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1502 GNUNET_SCHEDULER_cancel (pr->warn_task);
1503 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1505 if (NULL == (cont = pr->llc_cont))
1506 return; /* no continuation */
1507 pr->llc_cont = NULL;
1508 cont (pr->llc_cont_cls, pr, pr->local_result);
1513 * Is the given target a legitimate peer for forwarding the given request?
1517 * @return GNUNET_YES if this request could be forwarded to the given peer
1520 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1521 const struct GNUNET_PeerIdentity *target)
1523 struct GNUNET_PeerIdentity pi;
1525 if (0 == pr->origin_pid)
1527 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1529 memcmp (&pi, target,
1530 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1536 * Look up the request in the local datastore.
1538 * @param pr the pending request to process
1539 * @param cont function to call at the end
1540 * @param cont_cls closure for cont
1543 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1544 GSF_LocalLookupContinuation cont, void *cont_cls)
1546 GNUNET_assert (NULL == pr->gh);
1547 GNUNET_assert (NULL == pr->stream_request);
1548 GNUNET_assert (NULL == pr->llc_cont);
1549 pr->llc_cont = cont;
1550 pr->llc_cont_cls = cont_cls;
1551 pr->qe_start = GNUNET_TIME_absolute_get ();
1553 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1555 #if INSANE_STATISTICS
1556 GNUNET_STATISTICS_update (GSF_stats,
1557 gettext_noop ("# Datastore lookups initiated"), 1,
1561 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1562 &pr->public_data.query,
1563 pr->public_data.type ==
1564 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1565 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1567 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1568 public_data.options)) ? UINT_MAX : 1
1569 /* queue priority */ ,
1571 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1572 public_data.options)) ? UINT_MAX :
1573 GSF_datastore_queue_size
1574 /* max queue size */ ,
1575 GNUNET_TIME_UNIT_FOREVER_REL,
1576 &process_local_reply, pr);
1579 GNUNET_STATISTICS_update (GSF_stats,
1581 ("# Datastore lookups concluded (error queueing)"),
1583 GNUNET_SCHEDULER_cancel (pr->warn_task);
1584 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1585 pr->llc_cont = NULL;
1587 cont (cont_cls, pr, pr->local_result);
1593 * Handle P2P "CONTENT" message. Checks that the message is
1594 * well-formed and then checks if there are any pending requests for
1595 * this content and possibly passes it on (to local clients or other
1596 * peers). Does NOT perform migration (content caching at this peer).
1598 * @param cp the other peer involved (sender or receiver, NULL
1599 * for loopback messages where we are both sender and receiver)
1600 * @param message the actual message
1601 * @return GNUNET_OK if the message was well-formed,
1602 * GNUNET_SYSERR if the message was malformed (close connection,
1603 * do not cache under any circumstances)
1606 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1607 const struct GNUNET_MessageHeader *message)
1609 const struct PutMessage *put;
1612 enum GNUNET_BLOCK_Type type;
1613 struct GNUNET_TIME_Absolute expiration;
1614 struct GNUNET_HashCode query;
1615 struct ProcessReplyClosure prq;
1616 struct GNUNET_TIME_Relative block_time;
1618 struct PutMigrationContext *pmc;
1620 msize = ntohs (message->size);
1621 if (msize < sizeof (struct PutMessage))
1623 GNUNET_break_op (0);
1624 return GNUNET_SYSERR;
1626 put = (const struct PutMessage *) message;
1627 dsize = msize - sizeof (struct PutMessage);
1628 type = ntohl (put->type);
1629 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1630 /* do not allow migrated content to live longer than 1 year */
1631 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1633 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1634 return GNUNET_SYSERR;
1636 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1638 GNUNET_break_op (0);
1639 return GNUNET_SYSERR;
1641 GNUNET_STATISTICS_update (GSF_stats,
1642 gettext_noop ("# GAP PUT messages received"), 1,
1644 /* now, lookup 'query' */
1645 prq.data = (const void *) &put[1];
1649 prq.expiration = expiration;
1651 prq.anonymity_level = UINT32_MAX;
1652 prq.request_found = GNUNET_NO;
1653 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1657 GSF_connected_peer_change_preference_ (cp,
1658 CONTENT_BANDWIDTH_VALUE +
1659 1000 * prq.priority);
1660 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1662 if ((GNUNET_YES == active_to_migration) &&
1664 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "Replicating result for query `%s' with priority %u\n",
1668 GNUNET_h2s (&query), prq.priority);
1669 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1670 pmc->start = GNUNET_TIME_absolute_get ();
1671 pmc->requested = prq.request_found;
1672 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1673 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1676 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1677 prq.priority, 1 /* anonymity */ ,
1678 0 /* replication */ ,
1679 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1680 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1681 &put_migration_continuation, pmc))
1683 put_migration_continuation (pmc, GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, NULL);
1686 else if (NULL != cp)
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Choosing not to keep content `%s' (%d/%d)\n",
1690 GNUNET_h2s (&query), active_to_migration,
1691 test_put_load_too_high (prq.priority));
1693 putl = GNUNET_LOAD_get_load (datastore_put_load);
1694 if ( (NULL != cp) &&
1695 (GNUNET_NO == prq.request_found) &&
1696 ( (GNUNET_YES != active_to_migration) ||
1697 (putl > 2.5 * (1 + prq.priority)) ) )
1699 if (GNUNET_YES != active_to_migration)
1700 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1702 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1704 GNUNET_CRYPTO_random_u32
1705 (GNUNET_CRYPTO_QUALITY_WEAK,
1706 (unsigned int) (60000 * putl * putl)));
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "Asking to stop migration for %llu ms because of load %f and events %d/%d\n",
1709 (unsigned long long) block_time.rel_value,
1711 active_to_migration,
1712 (GNUNET_NO == prq.request_found));
1713 GSF_block_peer_migration_ (cp, GNUNET_TIME_relative_to_absolute (block_time));
1720 * Setup the subsystem.
1723 GSF_pending_request_init_ ()
1726 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1727 "MAX_PENDING_REQUESTS",
1728 &max_pending_requests))
1730 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1731 "fs", "MAX_PENDING_REQUESTS");
1733 active_to_migration =
1734 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1735 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1736 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1737 requests_by_expiration_heap =
1738 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1743 * Shutdown the subsystem.
1746 GSF_pending_request_done_ ()
1748 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1749 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1751 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1752 requests_by_expiration_heap = NULL;
1753 GNUNET_LOAD_value_free (datastore_put_load);
1754 datastore_put_load = NULL;
1758 /* end of gnunet-service-fs_pr.c */