2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/gnunet-service-fs_pr.c
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
27 #include "gnunet_load_lib.h"
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_indexing.h"
31 #include "gnunet-service-fs_pe.h"
32 #include "gnunet-service-fs_pr.h"
35 * Hard limit on the number of results we may get from the datastore per query.
37 #define MAX_RESULTS (100 * 1024)
42 struct GSF_PendingRequest
45 * Public data for the request.
47 struct GSF_PendingRequestData public_data;
50 * Function to call if we encounter a reply.
52 GSF_PendingRequestReplyHandler rh;
60 * Array of hash codes of replies we've already seen.
62 GNUNET_HashCode *replies_seen;
65 * Bloomfilter masking replies we've already seen.
67 struct GNUNET_CONTAINER_BloomFilter *bf;
70 * Entry for this pending request in the expiration heap, or NULL.
72 struct GNUNET_CONTAINER_HeapNode *hnode;
75 * Datastore queue entry for this request (or NULL for none).
77 struct GNUNET_DATASTORE_QueueEntry *qe;
80 * DHT request handle for this request (or NULL for none).
82 struct GNUNET_DHT_GetHandle *gh;
85 * Function to call upon completion of the local get
86 * request, or NULL for none.
88 GSF_LocalLookupContinuation llc_cont;
91 * Closure for llc_cont.
96 * Last result from the local datastore lookup evaluation.
98 enum GNUNET_BLOCK_EvaluationResult local_result;
101 * Identity of the peer that we should use for the 'sender'
102 * (recipient of the response) when forwarding (0 for none).
104 GNUNET_PEER_Id sender_pid;
107 * Time we started the last datastore lookup.
109 struct GNUNET_TIME_Absolute qe_start;
112 * Task that warns us if the local datastore lookup takes too long.
114 GNUNET_SCHEDULER_TaskIdentifier warn_task;
117 * Current offset for querying our local datastore for results.
118 * Starts at a random value, incremented until we get the same
119 * UID again (detected using 'first_uid'), which is then used
120 * to termiante the iteration.
122 uint64_t local_result_offset;
125 * Unique ID of the first result from the local datastore;
126 * used to detect wrap-around of the offset.
131 * Number of valid entries in the 'replies_seen' array.
133 unsigned int replies_seen_count;
136 * Length of the 'replies_seen' array.
138 unsigned int replies_seen_size;
141 * Mingle value we currently use for the bf.
146 * Do we have a first UID yet?
148 unsigned int have_first_uid;
154 * All pending requests, ordered by the query. Entries
155 * are of type 'struct GSF_PendingRequest*'.
157 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
161 * Datastore 'PUT' load tracking.
163 static struct GNUNET_LOAD_Value *datastore_put_load;
167 * Are we allowed to migrate content to this peer.
169 static int active_to_migration;
173 * Heap with the request that will expire next at the top. Contains
174 * pointers of type "struct PendingRequest*"; these will *also* be
175 * aliased from the "requests_by_peer" data structures and the
176 * "requests_by_query" table. Note that requests from our clients
177 * don't expire and are thus NOT in the "requests_by_expiration"
178 * (or the "requests_by_peer" tables).
180 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
184 * Maximum number of requests (from other peers, overall) that we're
185 * willing to have pending at any given point in time. Can be changed
186 * via the configuration file (32k is just the default).
188 static unsigned long long max_pending_requests = (32 * 1024);
192 * How many bytes should a bloomfilter be if we have already seen
193 * entry_count responses? Note that BLOOMFILTER_K gives us the number
194 * of bits set per entry. Furthermore, we should not re-size the
195 * filter too often (to keep it cheap).
197 * Since other peers will also add entries but not resize the filter,
198 * we should generally pick a slightly larger size than what the
199 * strict math would suggest.
201 * @return must be a power of two and smaller or equal to 2^15.
204 compute_bloomfilter_size (unsigned int entry_count)
207 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
208 uint16_t max = 1 << 15;
210 if (entry_count > max)
213 while ((size < max) && (size < ideal))
222 * Recalculate our bloom filter for filtering replies. This function
223 * will create a new bloom filter from scratch, so it should only be
224 * called if we have no bloomfilter at all (and hence can create a
225 * fresh one of minimal size without problems) OR if our peer is the
226 * initiator (in which case we may resize to larger than mimimum size).
228 * @param pr request for which the BF is to be recomputed
229 * @return GNUNET_YES if a refresh actually happened
232 refresh_bloomfilter (struct GSF_PendingRequest *pr)
236 GNUNET_HashCode mhash;
238 nsize = compute_bloomfilter_size (pr->replies_seen_count);
239 if ( (pr->bf != NULL) &&
240 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
241 return GNUNET_NO; /* size not changed */
243 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
244 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
246 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
249 for (i=0;i<pr->replies_seen_count;i++)
251 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
254 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
261 * Create a new pending request.
263 * @param options request options
264 * @param type type of the block that is being requested
265 * @param query key for the lookup
266 * @param namespace namespace to lookup, NULL for no namespace
267 * @param target preferred target for the request, NULL for none
268 * @param bf_data raw data for bloom filter for known replies, can be NULL
269 * @param bf_size number of bytes in bf_data
270 * @param mingle mingle value for bf
271 * @param anonymity_level desired anonymity level
272 * @param priority maximum outgoing cummulative request priority to use
273 * @param ttl current time-to-live for the request
274 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
275 * @param replies_seen hash codes of known local replies
276 * @param replies_seen_count size of the 'replies_seen' array
277 * @param rh handle to call when we get a reply
278 * @param rh_cls closure for rh
279 * @return handle for the new pending request
281 struct GSF_PendingRequest *
282 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
283 enum GNUNET_BLOCK_Type type,
284 const GNUNET_HashCode *query,
285 const GNUNET_HashCode *namespace,
286 const struct GNUNET_PeerIdentity *target,
290 uint32_t anonymity_level,
293 GNUNET_PEER_Id sender_pid,
294 const GNUNET_HashCode *replies_seen,
295 unsigned int replies_seen_count,
296 GSF_PendingRequestReplyHandler rh,
299 struct GSF_PendingRequest *pr;
300 struct GSF_PendingRequest *dpr;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Creating request handle for `%s' of type %d\n",
308 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
309 pr->local_result_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
311 pr->public_data.query = *query;
312 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
314 GNUNET_assert (NULL != namespace);
315 pr->public_data.namespace = *namespace;
319 pr->public_data.target = *target;
320 pr->public_data.has_target = GNUNET_YES;
322 pr->public_data.anonymity_level = anonymity_level;
323 pr->public_data.priority = priority;
324 pr->public_data.original_priority = priority;
325 pr->public_data.options = options;
326 pr->public_data.type = type;
327 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
328 pr->sender_pid = sender_pid;
332 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
335 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
336 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
337 (uint32_t) (- ttl)));
338 if (replies_seen_count > 0)
340 pr->replies_seen_size = replies_seen_count;
341 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
342 memcpy (pr->replies_seen,
344 replies_seen_count * sizeof (GNUNET_HashCode));
345 pr->replies_seen_count = replies_seen_count;
349 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
354 else if ( (replies_seen_count > 0) &&
355 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
357 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
359 GNUNET_CONTAINER_multihashmap_put (pr_map,
362 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
363 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
365 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
367 pr->public_data.ttl.abs_value);
368 /* make sure we don't track too many requests */
369 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
371 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
372 GNUNET_assert (dpr != NULL);
374 break; /* let the request live briefly... */
375 dpr->rh (dpr->rh_cls,
376 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
379 GNUNET_TIME_UNIT_FOREVER_ABS,
380 GNUNET_BLOCK_TYPE_ANY,
382 GSF_pending_request_cancel_ (dpr);
390 * Obtain the public data associated with a pending request
392 * @param pr pending request
393 * @return associated public data
395 struct GSF_PendingRequestData *
396 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
398 return &pr->public_data;
403 * Update a given pending request with additional replies
404 * that have been seen.
406 * @param pr request to update
407 * @param replies_seen hash codes of replies that we've seen
408 * @param replies_seen_count size of the replies_seen array
411 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
412 const GNUNET_HashCode *replies_seen,
413 unsigned int replies_seen_count)
416 GNUNET_HashCode mhash;
418 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
419 return; /* integer overflow */
420 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
422 /* we're responsible for the BF, full refresh */
423 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
424 GNUNET_array_grow (pr->replies_seen,
425 pr->replies_seen_size,
426 replies_seen_count + pr->replies_seen_count);
427 memcpy (&pr->replies_seen[pr->replies_seen_count],
429 sizeof (GNUNET_HashCode) * replies_seen_count);
430 pr->replies_seen_count += replies_seen_count;
431 if (GNUNET_NO == refresh_bloomfilter (pr))
433 /* bf not recalculated, simply extend it with new bits */
434 for (i=0;i<replies_seen_count;i++)
436 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
439 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
447 /* we're not the initiator, but the initiator did not give us
448 any bloom-filter, so we need to create one on-the-fly */
449 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
451 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
452 compute_bloomfilter_size (replies_seen_count),
455 for (i=0;i<pr->replies_seen_count;i++)
457 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
460 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
467 * Generate the message corresponding to the given pending request for
468 * transmission to other peers (or at least determine its size).
470 * @param pr request to generate the message for
471 * @param buf_size number of bytes available in buf
472 * @param buf where to copy the message (can be NULL)
473 * @return number of bytes needed (if > buf_size) or used
476 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
480 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
481 struct GetMessage *gm;
482 GNUNET_HashCode *ext;
488 struct GNUNET_TIME_Absolute now;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Building request message for `%s' of type %d\n",
496 GNUNET_h2s (&pr->public_data.query),
497 pr->public_data.type);
501 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
504 bm |= GET_MESSAGE_BIT_RETURN_TO;
507 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
509 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
512 if (GNUNET_YES == pr->public_data.has_target)
514 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
517 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
518 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
519 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
520 if (buf_size < msize)
522 gm = (struct GetMessage*) lbuf;
523 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
524 gm->header.size = htons (msize);
525 gm->type = htonl (pr->public_data.type);
527 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
528 pr->public_data.priority + 1);
531 pr->public_data.priority -= prio;
532 gm->priority = htonl (prio);
533 now = GNUNET_TIME_absolute_get ();
534 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
535 gm->ttl = htonl (ttl / 1000);
536 gm->filter_mutator = htonl(pr->mingle);
537 gm->hash_bitmap = htonl (bm);
538 gm->query = pr->public_data.query;
539 ext = (GNUNET_HashCode*) &gm[1];
542 GNUNET_PEER_resolve (pr->sender_pid,
543 (struct GNUNET_PeerIdentity*) &ext[k++]);
544 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
546 &pr->public_data.namespace,
547 sizeof (GNUNET_HashCode));
548 if (GNUNET_YES == pr->public_data.has_target)
549 ext[k++] = pr->public_data.target.hashPubKey;
551 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
554 memcpy (buf, gm, msize);
560 * Iterator to free pending requests.
562 * @param cls closure, unused
563 * @param key current key code
564 * @param value value in the hash map (pending request)
565 * @return GNUNET_YES (we should continue to iterate)
568 clean_request (void *cls,
569 const GNUNET_HashCode * key,
572 struct GSF_PendingRequest *pr = value;
573 GSF_LocalLookupContinuation cont;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Cleaning up pending request for `%s'.\n",
580 if (NULL != (cont = pr->llc_cont))
583 cont (pr->llc_cont_cls,
587 GSF_plan_notify_request_done_ (pr);
588 GNUNET_free_non_null (pr->replies_seen);
591 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
594 GNUNET_PEER_change_rc (pr->sender_pid, -1);
595 if (NULL != pr->hnode)
597 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
602 GNUNET_DATASTORE_cancel (pr->qe);
607 GNUNET_DHT_get_stop (pr->gh);
610 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
612 GNUNET_SCHEDULER_cancel (pr->warn_task);
613 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
621 * Explicitly cancel a pending request.
623 * @param pr request to cancel
626 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
629 return; /* already cleaned up! */
630 GNUNET_assert (GNUNET_OK ==
631 GNUNET_CONTAINER_multihashmap_remove (pr_map,
632 &pr->public_data.query,
634 GNUNET_assert (GNUNET_YES ==
635 clean_request (NULL, &pr->public_data.query, pr));
640 * Iterate over all pending requests.
642 * @param it function to call for each request
643 * @param cls closure for it
646 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
649 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
650 (GNUNET_CONTAINER_HashMapIterator) it,
658 * Closure for "process_reply" function.
660 struct ProcessReplyClosure
663 * The data for the reply.
668 * Who gave us this reply? NULL for local host (or DHT)
670 struct GSF_ConnectedPeer *sender;
673 * When the reply expires.
675 struct GNUNET_TIME_Absolute expiration;
685 enum GNUNET_BLOCK_Type type;
688 * How much was this reply worth to us?
693 * Anonymity requirements for this reply.
695 uint32_t anonymity_level;
698 * Evaluation result (returned).
700 enum GNUNET_BLOCK_EvaluationResult eval;
703 * Did we find a matching request?
710 * Update the performance data for the sender (if any) since
711 * the sender successfully answered one of our queries.
713 * @param prq information about the sender
714 * @param pr request that was satisfied
717 update_request_performance_data (struct ProcessReplyClosure *prq,
718 struct GSF_PendingRequest *pr)
720 if (prq->sender == NULL)
722 GSF_peer_update_performance_ (prq->sender,
723 pr->public_data.start_time,
729 * We have received a reply; handle it!
731 * @param cls response (struct ProcessReplyClosure)
732 * @param key our query
733 * @param value value in the hash map (info about the query)
734 * @return GNUNET_YES (we should continue to iterate)
737 process_reply (void *cls,
738 const GNUNET_HashCode *key,
741 struct ProcessReplyClosure *prq = cls;
742 struct GSF_PendingRequest *pr = value;
743 GNUNET_HashCode chash;
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Matched result (type %u) for query `%s' with pending request\n",
748 (unsigned int) prq->type,
751 GNUNET_STATISTICS_update (GSF_stats,
752 gettext_noop ("# replies received and matched"),
755 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
760 &pr->public_data.namespace,
761 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
766 case GNUNET_BLOCK_EVALUATION_OK_MORE:
767 update_request_performance_data (prq, pr);
769 case GNUNET_BLOCK_EVALUATION_OK_LAST:
770 /* short cut: stop processing early, no BF-update, etc. */
771 update_request_performance_data (prq, pr);
772 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
773 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
774 /* pass on to other peers / local clients */
778 prq->anonymity_level,
781 prq->data, prq->size);
783 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
784 GNUNET_STATISTICS_update (GSF_stats,
785 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Duplicate response `%s', discarding.\n",
791 GNUNET_h2s (&mhash));
793 return GNUNET_YES; /* duplicate */
794 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
795 return GNUNET_YES; /* wrong namespace */
796 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
799 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
802 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804 _("Unsupported block type %u\n"),
808 /* update bloomfilter */
809 GNUNET_CRYPTO_hash (prq->data,
812 GSF_pending_request_update_ (pr, &chash, 1);
813 if (NULL == prq->sender)
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Found result for query `%s' in local datastore\n",
820 GNUNET_STATISTICS_update (GSF_stats,
821 gettext_noop ("# results found locally"),
827 GSF_dht_lookup_ (pr);
829 prq->priority += pr->public_data.original_priority;
830 pr->public_data.priority = 0;
831 pr->public_data.original_priority = 0;
832 pr->public_data.results_found++;
833 prq->request_found = GNUNET_YES;
834 /* finally, pass on to other peer / local client */
838 prq->anonymity_level,
841 prq->data, prq->size);
847 * Context for the 'put_migration_continuation'.
849 struct PutMigrationContext
853 * Start time for the operation.
855 struct GNUNET_TIME_Absolute start;
860 struct GNUNET_PeerIdentity origin;
863 * GNUNET_YES if we had a matching request for this block,
871 * Continuation called to notify client about result of the
875 * @param success GNUNET_SYSERR on failure
876 * @param msg NULL on success, otherwise an error message
879 put_migration_continuation (void *cls,
883 struct PutMigrationContext *pmc = cls;
884 struct GNUNET_TIME_Relative delay;
885 struct GNUNET_TIME_Relative block_time;
886 struct GSF_ConnectedPeer *cp;
887 struct GSF_PeerPerformanceData *ppd;
889 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
890 cp = GSF_peer_get_ (&pmc->origin);
891 if ( (GNUNET_OK != success) &&
892 (GNUNET_NO == pmc->requested) )
894 /* block migration for a bit... */
897 ppd = GSF_get_peer_performance_data_ (cp);
898 ppd->migration_duplication++;
899 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
900 5 * ppd->migration_duplication +
901 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
902 GSF_block_peer_migration_ (cp, block_time);
909 ppd = GSF_get_peer_performance_data_ (cp);
910 ppd->migration_duplication = 0; /* reset counter */
914 /* FIXME: should we really update the load value on failure? */
915 GNUNET_LOAD_update (datastore_put_load,
917 if (GNUNET_OK == success)
919 GNUNET_STATISTICS_update (GSF_stats,
920 gettext_noop ("# datastore 'put' failures"),
927 * Test if the DATABASE (PUT) load on this peer is too high
928 * to even consider processing the query at
931 * @return GNUNET_YES if the load is too high to do anything (load high)
932 * GNUNET_NO to process normally (load normal or low)
935 test_put_load_too_high (uint32_t priority)
939 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
940 return GNUNET_NO; /* very fast */
941 ld = GNUNET_LOAD_get_load (datastore_put_load);
942 if (ld < 2.0 * (1 + priority))
944 GNUNET_STATISTICS_update (GSF_stats,
945 gettext_noop ("# storage requests dropped due to high load"),
953 * Iterator called on each result obtained for a DHT
954 * operation that expects a reply
957 * @param exp when will this value expire
958 * @param key key of the result
959 * @param get_path NULL-terminated array of pointers
960 * to the peers on reverse GET path (or NULL if not recorded)
961 * @param put_path NULL-terminated array of pointers
962 * to the peers on the PUT path (or NULL if not recorded)
963 * @param type type of the result
964 * @param size number of bytes in data
965 * @param data pointer to the result data
968 handle_dht_reply (void *cls,
969 struct GNUNET_TIME_Absolute exp,
970 const GNUNET_HashCode *key,
971 const struct GNUNET_PeerIdentity * const *get_path,
972 const struct GNUNET_PeerIdentity * const *put_path,
973 enum GNUNET_BLOCK_Type type,
977 struct GSF_PendingRequest *pr = cls;
978 struct ProcessReplyClosure prq;
979 struct PutMigrationContext *pmc;
981 GNUNET_STATISTICS_update (GSF_stats,
982 gettext_noop ("# Replies received from DHT"),
985 memset (&prq, 0, sizeof (prq));
987 prq.expiration = exp;
990 process_reply (&prq, key, pr);
991 if ( (GNUNET_YES == active_to_migration) &&
992 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "Replicating result for query `%s' with priority %u\n",
1000 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1001 pmc->start = GNUNET_TIME_absolute_get ();
1002 pmc->requested = GNUNET_YES;
1004 GNUNET_DATASTORE_put (GSF_dsh,
1006 type, prq.priority, 1 /* anonymity */,
1007 0 /* replication */,
1009 1 + prq.priority, MAX_DATASTORE_QUEUE,
1010 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1011 &put_migration_continuation,
1014 put_migration_continuation (pmc, GNUNET_NO, NULL);
1021 * Consider looking up the data in the DHT (anonymity-level permitting).
1023 * @param pr the pending request to process
1026 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1030 struct GNUNET_PeerIdentity pi;
1031 char buf[sizeof (GNUNET_HashCode) * 2];
1033 if (0 != pr->public_data.anonymity_level)
1037 GNUNET_DHT_get_stop (pr->gh);
1042 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1045 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
1046 xquery_size = sizeof (GNUNET_HashCode);
1048 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1050 GNUNET_PEER_resolve (pr->sender_pid,
1052 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1053 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1055 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1056 GNUNET_TIME_UNIT_FOREVER_REL,
1057 pr->public_data.type,
1058 &pr->public_data.query,
1059 DEFAULT_GET_REPLICATION,
1060 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1071 * Task that issues a warning if the datastore lookup takes too long.
1073 * @param cls the 'struct GSF_PendingRequest'
1074 * @param tc task context
1077 warn_delay_task (void *cls,
1078 const struct GNUNET_SCHEDULER_TaskContext *tc)
1080 struct GSF_PendingRequest *pr = cls;
1082 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1083 _("Datastore lookup already took %llu ms!\n"),
1084 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1085 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1092 * Task that issues a warning if the datastore lookup takes too long.
1094 * @param cls the 'struct GSF_PendingRequest'
1095 * @param tc task context
1098 odc_warn_delay_task (void *cls,
1099 const struct GNUNET_SCHEDULER_TaskContext *tc)
1101 struct GSF_PendingRequest *pr = cls;
1103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1104 _("On-demand lookup already took %llu ms!\n"),
1105 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1106 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1107 &odc_warn_delay_task,
1113 * We're processing (local) results for a search request
1114 * from another peer. Pass applicable results to the
1115 * peer and if we are done either clean up (operation
1116 * complete) or forward to other peers (more results possible).
1118 * @param cls our closure (struct PendingRequest)
1119 * @param key key for the content
1120 * @param size number of bytes in data
1121 * @param data content stored
1122 * @param type type of the content
1123 * @param priority priority of the content
1124 * @param anonymity anonymity-level for the content
1125 * @param expiration expiration time for the content
1126 * @param uid unique identifier for the datum;
1127 * maybe 0 if no unique identifier is available
1130 process_local_reply (void *cls,
1131 const GNUNET_HashCode *key,
1134 enum GNUNET_BLOCK_Type type,
1137 struct GNUNET_TIME_Absolute expiration,
1140 struct GSF_PendingRequest *pr = cls;
1141 GSF_LocalLookupContinuation cont;
1142 struct ProcessReplyClosure prq;
1143 GNUNET_HashCode query;
1144 unsigned int old_rf;
1147 GNUNET_SCHEDULER_cancel (pr->warn_task);
1148 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1149 if (GNUNET_NO == pr->have_first_uid)
1151 pr->first_uid = uid;
1152 pr->have_first_uid = 1;
1156 if (uid == pr->first_uid)
1158 GNUNET_STATISTICS_update (GSF_stats,
1159 gettext_noop ("# Datastore lookups concluded"),
1162 key = NULL; /* all replies seen! */
1164 pr->have_first_uid++;
1165 if (pr->have_first_uid > MAX_RESULTS)
1167 GNUNET_STATISTICS_update (GSF_stats,
1168 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1171 key = NULL; /* all replies seen! */
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178 "No further local responses available.\n");
1180 if ( (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1181 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK) )
1182 GNUNET_STATISTICS_update (GSF_stats,
1183 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1186 goto check_error_and_continue;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1193 (unsigned long long) uid);
1195 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Found ONDEMAND block, performing on-demand encoding\n");
1201 GNUNET_STATISTICS_update (GSF_stats,
1202 gettext_noop ("# on-demand blocks matched requests"),
1205 pr->qe_start = GNUNET_TIME_absolute_get ();
1206 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1207 &odc_warn_delay_task,
1210 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1211 anonymity, expiration, uid,
1212 &process_local_reply,
1215 GNUNET_STATISTICS_update (GSF_stats,
1216 gettext_noop ("# on-demand lookups performed successfully"),
1219 return; /* we're done */
1221 GNUNET_STATISTICS_update (GSF_stats,
1222 gettext_noop ("# on-demand lookups failed"),
1225 GNUNET_SCHEDULER_cancel (pr->warn_task);
1226 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1229 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1230 pr->local_result_offset - 1,
1231 &pr->public_data.query,
1232 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1233 ? GNUNET_BLOCK_TYPE_ANY
1234 : pr->public_data.type,
1235 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1237 : 1 /* queue priority */,
1238 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1240 : 1 /* max queue size */,
1241 GNUNET_TIME_UNIT_FOREVER_REL,
1242 &process_local_reply,
1245 return; /* we're done */
1246 goto check_error_and_continue;
1248 old_rf = pr->public_data.results_found;
1249 memset (&prq, 0, sizeof (prq));
1251 prq.expiration = expiration;
1254 GNUNET_BLOCK_get_key (GSF_block_ctx,
1261 GNUNET_DATASTORE_remove (GSF_dsh,
1265 GNUNET_TIME_UNIT_FOREVER_REL,
1267 pr->qe_start = GNUNET_TIME_absolute_get ();
1268 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1271 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1272 pr->local_result_offset - 1,
1273 &pr->public_data.query,
1274 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1275 ? GNUNET_BLOCK_TYPE_ANY
1276 : pr->public_data.type,
1277 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1279 : 1 /* queue priority */,
1280 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1282 : 1 /* max queue size */,
1283 GNUNET_TIME_UNIT_FOREVER_REL,
1284 &process_local_reply,
1287 goto check_error_and_continue;
1291 prq.priority = priority;
1292 prq.request_found = GNUNET_NO;
1293 prq.anonymity_level = anonymity;
1294 if ( (old_rf == 0) &&
1295 (pr->public_data.results_found == 0) )
1296 GSF_update_datastore_delay_ (pr->public_data.start_time);
1297 process_reply (&prq, key, pr);
1298 pr->local_result = prq.eval;
1299 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1300 goto check_error_and_continue;
1301 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1302 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1303 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Load too high, done with request\n");
1309 GNUNET_STATISTICS_update (GSF_stats,
1310 gettext_noop ("# processing result set cut short due to load"),
1313 goto check_error_and_continue;
1315 pr->qe_start = GNUNET_TIME_absolute_get ();
1316 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1319 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1320 pr->local_result_offset++,
1321 &pr->public_data.query,
1322 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1323 ? GNUNET_BLOCK_TYPE_ANY
1324 : pr->public_data.type,
1325 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1327 : 1 /* queue priority */,
1328 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1330 : 1 /* max queue size */,
1331 GNUNET_TIME_UNIT_FOREVER_REL,
1332 &process_local_reply,
1334 /* check if we successfully queued another datastore request;
1335 if so, return, otherwise call our continuation (if we have
1337 check_error_and_continue:
1340 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1342 GNUNET_SCHEDULER_cancel (pr->warn_task);
1343 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1345 if (NULL == (cont = pr->llc_cont))
1346 return; /* no continuation */
1347 pr->llc_cont = NULL;
1348 cont (pr->llc_cont_cls,
1355 * Look up the request in the local datastore.
1357 * @param pr the pending request to process
1358 * @param cont function to call at the end
1359 * @param cont_cls closure for cont
1362 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1363 GSF_LocalLookupContinuation cont,
1366 GNUNET_assert (NULL == pr->gh);
1367 GNUNET_assert (NULL == pr->llc_cont);
1368 pr->llc_cont = cont;
1369 pr->llc_cont_cls = cont_cls;
1370 pr->qe_start = GNUNET_TIME_absolute_get ();
1371 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1374 GNUNET_STATISTICS_update (GSF_stats,
1375 gettext_noop ("# Datastore lookups initiated"),
1378 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1379 pr->local_result_offset++,
1380 &pr->public_data.query,
1381 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1382 ? GNUNET_BLOCK_TYPE_ANY
1383 : pr->public_data.type,
1384 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1386 : 1 /* queue priority */,
1387 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1389 : 1 /* max queue size */,
1390 GNUNET_TIME_UNIT_FOREVER_REL,
1391 &process_local_reply,
1395 GNUNET_SCHEDULER_cancel (pr->warn_task);
1396 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1397 pr->llc_cont = NULL;
1399 cont (cont_cls, pr, pr->local_result);
1405 * Handle P2P "CONTENT" message. Checks that the message is
1406 * well-formed and then checks if there are any pending requests for
1407 * this content and possibly passes it on (to local clients or other
1408 * peers). Does NOT perform migration (content caching at this peer).
1410 * @param cp the other peer involved (sender or receiver, NULL
1411 * for loopback messages where we are both sender and receiver)
1412 * @param message the actual message
1413 * @return GNUNET_OK if the message was well-formed,
1414 * GNUNET_SYSERR if the message was malformed (close connection,
1415 * do not cache under any circumstances)
1418 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1419 const struct GNUNET_MessageHeader *message)
1421 const struct PutMessage *put;
1424 enum GNUNET_BLOCK_Type type;
1425 struct GNUNET_TIME_Absolute expiration;
1426 GNUNET_HashCode query;
1427 struct ProcessReplyClosure prq;
1428 struct GNUNET_TIME_Relative block_time;
1430 struct PutMigrationContext *pmc;
1432 msize = ntohs (message->size);
1433 if (msize < sizeof (struct PutMessage))
1436 return GNUNET_SYSERR;
1438 put = (const struct PutMessage*) message;
1439 dsize = msize - sizeof (struct PutMessage);
1440 type = ntohl (put->type);
1441 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1442 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1443 return GNUNET_SYSERR;
1445 GNUNET_BLOCK_get_key (GSF_block_ctx,
1451 GNUNET_break_op (0);
1452 return GNUNET_SYSERR;
1454 GNUNET_STATISTICS_update (GSF_stats,
1455 gettext_noop ("# GAP PUT messages received"),
1458 /* now, lookup 'query' */
1459 prq.data = (const void*) &put[1];
1466 prq.expiration = expiration;
1468 prq.anonymity_level = UINT32_MAX;
1469 prq.request_found = GNUNET_NO;
1470 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1476 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1477 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1479 if ( (GNUNET_YES == active_to_migration) &&
1480 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Replicating result for query `%s' with priority %u\n",
1485 GNUNET_h2s (&query),
1488 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1489 pmc->start = GNUNET_TIME_absolute_get ();
1490 pmc->requested = prq.request_found;
1491 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1494 GNUNET_DATASTORE_put (GSF_dsh,
1495 0, &query, dsize, &put[1],
1496 type, prq.priority, 1 /* anonymity */,
1497 0 /* replication */,
1499 1 + prq.priority, MAX_DATASTORE_QUEUE,
1500 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1501 &put_migration_continuation,
1504 put_migration_continuation (pmc, GNUNET_NO, NULL);
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Choosing not to keep content `%s' (%d/%d)\n",
1512 GNUNET_h2s (&query),
1513 active_to_migration,
1514 test_put_load_too_high (prq.priority));
1517 putl = GNUNET_LOAD_get_load (datastore_put_load);
1518 if ( (NULL != (cp = prq.sender)) &&
1519 (GNUNET_NO == prq.request_found) &&
1520 ( (GNUNET_YES != active_to_migration) ||
1521 (putl > 2.5 * (1 + prq.priority)) ) )
1523 if (GNUNET_YES != active_to_migration)
1524 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1525 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1526 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1527 (unsigned int) (60000 * putl * putl)));
1528 GSF_block_peer_migration_ (cp, block_time);
1535 * Setup the subsystem.
1538 GSF_pending_request_init_ ()
1541 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1543 "MAX_PENDING_REQUESTS",
1544 &max_pending_requests))
1546 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1547 _("Configuration fails to specify `%s', assuming default value."),
1548 "MAX_PENDING_REQUESTS");
1550 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1553 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1554 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1555 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1560 * Shutdown the subsystem.
1563 GSF_pending_request_done_ ()
1565 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1568 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1570 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1571 requests_by_expiration_heap = NULL;
1572 GNUNET_LOAD_value_free (datastore_put_load);
1573 datastore_put_load = NULL;
1577 /* end of gnunet-service-fs_pr.c */