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;
331 GNUNET_assert ( (sender_pid != 0) ||
332 (0 == (options & GSF_PRO_FORWARD_ONLY)) );
334 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
337 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
338 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
339 (uint32_t) (- ttl)));
340 if (replies_seen_count > 0)
342 pr->replies_seen_size = replies_seen_count;
343 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
344 memcpy (pr->replies_seen,
346 replies_seen_count * sizeof (GNUNET_HashCode));
347 pr->replies_seen_count = replies_seen_count;
351 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
356 else if ( (replies_seen_count > 0) &&
357 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
359 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
361 GNUNET_CONTAINER_multihashmap_put (pr_map,
364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
365 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
367 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
369 pr->public_data.ttl.abs_value);
370 /* make sure we don't track too many requests */
371 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
373 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
374 GNUNET_assert (dpr != NULL);
376 break; /* let the request live briefly... */
377 dpr->rh (dpr->rh_cls,
378 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
381 GNUNET_TIME_UNIT_FOREVER_ABS,
382 GNUNET_BLOCK_TYPE_ANY,
384 GSF_pending_request_cancel_ (dpr);
387 GNUNET_STATISTICS_update (GSF_stats,
388 gettext_noop ("# Pending requests active"),
396 * Obtain the public data associated with a pending request
398 * @param pr pending request
399 * @return associated public data
401 struct GSF_PendingRequestData *
402 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
404 return &pr->public_data;
409 * Update a given pending request with additional replies
410 * that have been seen.
412 * @param pr request to update
413 * @param replies_seen hash codes of replies that we've seen
414 * @param replies_seen_count size of the replies_seen array
417 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
418 const GNUNET_HashCode *replies_seen,
419 unsigned int replies_seen_count)
422 GNUNET_HashCode mhash;
424 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
425 return; /* integer overflow */
426 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
428 /* we're responsible for the BF, full refresh */
429 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
430 GNUNET_array_grow (pr->replies_seen,
431 pr->replies_seen_size,
432 replies_seen_count + pr->replies_seen_count);
433 memcpy (&pr->replies_seen[pr->replies_seen_count],
435 sizeof (GNUNET_HashCode) * replies_seen_count);
436 pr->replies_seen_count += replies_seen_count;
437 if (GNUNET_NO == refresh_bloomfilter (pr))
439 /* bf not recalculated, simply extend it with new bits */
440 for (i=0;i<replies_seen_count;i++)
442 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
445 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
453 /* we're not the initiator, but the initiator did not give us
454 any bloom-filter, so we need to create one on-the-fly */
455 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
457 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
458 compute_bloomfilter_size (replies_seen_count),
461 for (i=0;i<pr->replies_seen_count;i++)
463 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
466 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
473 * Generate the message corresponding to the given pending request for
474 * transmission to other peers (or at least determine its size).
476 * @param pr request to generate the message for
477 * @param buf_size number of bytes available in buf
478 * @param buf where to copy the message (can be NULL)
479 * @return number of bytes needed (if > buf_size) or used
482 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
486 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
487 struct GetMessage *gm;
488 GNUNET_HashCode *ext;
494 struct GNUNET_TIME_Absolute now;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Building request message for `%s' of type %d\n",
502 GNUNET_h2s (&pr->public_data.query),
503 pr->public_data.type);
507 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
508 if ( (! do_route) && (pr->sender_pid == 0))
511 do_route = GNUNET_YES;
515 bm |= GET_MESSAGE_BIT_RETURN_TO;
518 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
520 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
523 if (GNUNET_YES == pr->public_data.has_target)
525 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
528 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
529 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
530 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
531 if (buf_size < msize)
533 gm = (struct GetMessage*) lbuf;
534 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
535 gm->header.size = htons (msize);
536 gm->type = htonl (pr->public_data.type);
538 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
539 pr->public_data.priority + 1);
542 pr->public_data.priority -= prio;
543 gm->priority = htonl (prio);
544 now = GNUNET_TIME_absolute_get ();
545 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
546 gm->ttl = htonl (ttl / 1000);
547 gm->filter_mutator = htonl(pr->mingle);
548 gm->hash_bitmap = htonl (bm);
549 gm->query = pr->public_data.query;
550 ext = (GNUNET_HashCode*) &gm[1];
553 GNUNET_PEER_resolve (pr->sender_pid,
554 (struct GNUNET_PeerIdentity*) &ext[k++]);
555 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
557 &pr->public_data.namespace,
558 sizeof (GNUNET_HashCode));
559 if (GNUNET_YES == pr->public_data.has_target)
560 ext[k++] = pr->public_data.target.hashPubKey;
562 GNUNET_assert (GNUNET_SYSERR !=
563 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
566 memcpy (buf, gm, msize);
572 * Iterator to free pending requests.
574 * @param cls closure, unused
575 * @param key current key code
576 * @param value value in the hash map (pending request)
577 * @return GNUNET_YES (we should continue to iterate)
580 clean_request (void *cls,
581 const GNUNET_HashCode * key,
584 struct GSF_PendingRequest *pr = value;
585 GSF_LocalLookupContinuation cont;
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Cleaning up pending request for `%s'.\n",
592 if (NULL != (cont = pr->llc_cont))
595 cont (pr->llc_cont_cls,
599 GSF_plan_notify_request_done_ (pr);
600 GNUNET_free_non_null (pr->replies_seen);
603 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
606 GNUNET_PEER_change_rc (pr->sender_pid, -1);
607 if (NULL != pr->hnode)
609 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
614 GNUNET_DATASTORE_cancel (pr->qe);
619 GNUNET_DHT_get_stop (pr->gh);
622 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
624 GNUNET_SCHEDULER_cancel (pr->warn_task);
625 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
627 GNUNET_assert (GNUNET_OK ==
628 GNUNET_CONTAINER_multihashmap_remove (pr_map,
629 &pr->public_data.query,
631 GNUNET_STATISTICS_update (GSF_stats,
632 gettext_noop ("# Pending requests active"),
641 * Explicitly cancel a pending request.
643 * @param pr request to cancel
646 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
649 return; /* already cleaned up! */
650 GNUNET_assert (GNUNET_YES ==
651 clean_request (NULL, &pr->public_data.query, pr));
656 * Iterate over all pending requests.
658 * @param it function to call for each request
659 * @param cls closure for it
662 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
665 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
666 (GNUNET_CONTAINER_HashMapIterator) it,
674 * Closure for "process_reply" function.
676 struct ProcessReplyClosure
679 * The data for the reply.
684 * Who gave us this reply? NULL for local host (or DHT)
686 struct GSF_ConnectedPeer *sender;
689 * When the reply expires.
691 struct GNUNET_TIME_Absolute expiration;
701 enum GNUNET_BLOCK_Type type;
704 * How much was this reply worth to us?
709 * Anonymity requirements for this reply.
711 uint32_t anonymity_level;
714 * Evaluation result (returned).
716 enum GNUNET_BLOCK_EvaluationResult eval;
719 * Did we find a matching request?
726 * Update the performance data for the sender (if any) since
727 * the sender successfully answered one of our queries.
729 * @param prq information about the sender
730 * @param pr request that was satisfied
733 update_request_performance_data (struct ProcessReplyClosure *prq,
734 struct GSF_PendingRequest *pr)
736 if (prq->sender == NULL)
738 GSF_peer_update_performance_ (prq->sender,
739 pr->public_data.start_time,
745 * We have received a reply; handle it!
747 * @param cls response (struct ProcessReplyClosure)
748 * @param key our query
749 * @param value value in the hash map (info about the query)
750 * @return GNUNET_YES (we should continue to iterate)
753 process_reply (void *cls,
754 const GNUNET_HashCode *key,
757 struct ProcessReplyClosure *prq = cls;
758 struct GSF_PendingRequest *pr = value;
759 GNUNET_HashCode chash;
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "Matched result (type %u) for query `%s' with pending request\n",
764 (unsigned int) prq->type,
767 GNUNET_STATISTICS_update (GSF_stats,
768 gettext_noop ("# replies received and matched"),
771 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
776 &pr->public_data.namespace,
777 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
782 case GNUNET_BLOCK_EVALUATION_OK_MORE:
783 update_request_performance_data (prq, pr);
785 case GNUNET_BLOCK_EVALUATION_OK_LAST:
786 /* short cut: stop processing early, no BF-update, etc. */
787 update_request_performance_data (prq, pr);
788 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
789 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
790 /* pass on to other peers / local clients */
794 prq->anonymity_level,
797 prq->data, prq->size);
799 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
800 GNUNET_STATISTICS_update (GSF_stats,
801 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Duplicate response `%s', discarding.\n",
807 GNUNET_h2s (&mhash));
809 return GNUNET_YES; /* duplicate */
810 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
811 return GNUNET_YES; /* wrong namespace */
812 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
815 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
818 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
820 _("Unsupported block type %u\n"),
824 /* update bloomfilter */
825 GNUNET_CRYPTO_hash (prq->data,
828 GSF_pending_request_update_ (pr, &chash, 1);
829 if (NULL == prq->sender)
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Found result for query `%s' in local datastore\n",
836 GNUNET_STATISTICS_update (GSF_stats,
837 gettext_noop ("# results found locally"),
843 GSF_dht_lookup_ (pr);
845 prq->priority += pr->public_data.original_priority;
846 pr->public_data.priority = 0;
847 pr->public_data.original_priority = 0;
848 pr->public_data.results_found++;
849 prq->request_found = GNUNET_YES;
850 /* finally, pass on to other peer / local client */
854 prq->anonymity_level,
857 prq->data, prq->size);
863 * Context for the 'put_migration_continuation'.
865 struct PutMigrationContext
869 * Start time for the operation.
871 struct GNUNET_TIME_Absolute start;
876 struct GNUNET_PeerIdentity origin;
879 * GNUNET_YES if we had a matching request for this block,
887 * Continuation called to notify client about result of the
891 * @param success GNUNET_SYSERR on failure
892 * @param msg NULL on success, otherwise an error message
895 put_migration_continuation (void *cls,
899 struct PutMigrationContext *pmc = cls;
900 struct GNUNET_TIME_Relative delay;
901 struct GNUNET_TIME_Relative block_time;
902 struct GSF_ConnectedPeer *cp;
903 struct GSF_PeerPerformanceData *ppd;
905 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
906 cp = GSF_peer_get_ (&pmc->origin);
907 if ( (GNUNET_OK != success) &&
908 (GNUNET_NO == pmc->requested) )
910 /* block migration for a bit... */
913 ppd = GSF_get_peer_performance_data_ (cp);
914 ppd->migration_duplication++;
915 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
916 5 * ppd->migration_duplication +
917 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
918 GSF_block_peer_migration_ (cp, block_time);
925 ppd = GSF_get_peer_performance_data_ (cp);
926 ppd->migration_duplication = 0; /* reset counter */
930 /* FIXME: should we really update the load value on failure? */
931 GNUNET_LOAD_update (datastore_put_load,
933 if (GNUNET_OK == success)
935 GNUNET_STATISTICS_update (GSF_stats,
936 gettext_noop ("# Datastore `PUT' failures"),
943 * Test if the DATABASE (PUT) load on this peer is too high
944 * to even consider processing the query at
947 * @return GNUNET_YES if the load is too high to do anything (load high)
948 * GNUNET_NO to process normally (load normal or low)
951 test_put_load_too_high (uint32_t priority)
955 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
956 return GNUNET_NO; /* very fast */
957 ld = GNUNET_LOAD_get_load (datastore_put_load);
958 if (ld < 2.0 * (1 + priority))
960 GNUNET_STATISTICS_update (GSF_stats,
961 gettext_noop ("# storage requests dropped due to high load"),
969 * Iterator called on each result obtained for a DHT
970 * operation that expects a reply
973 * @param exp when will this value expire
974 * @param key key of the result
975 * @param get_path NULL-terminated array of pointers
976 * to the peers on reverse GET path (or NULL if not recorded)
977 * @param put_path NULL-terminated array of pointers
978 * to the peers on the PUT path (or NULL if not recorded)
979 * @param type type of the result
980 * @param size number of bytes in data
981 * @param data pointer to the result data
984 handle_dht_reply (void *cls,
985 struct GNUNET_TIME_Absolute exp,
986 const GNUNET_HashCode *key,
987 const struct GNUNET_PeerIdentity * const *get_path,
988 const struct GNUNET_PeerIdentity * const *put_path,
989 enum GNUNET_BLOCK_Type type,
993 struct GSF_PendingRequest *pr = cls;
994 struct ProcessReplyClosure prq;
995 struct PutMigrationContext *pmc;
997 GNUNET_STATISTICS_update (GSF_stats,
998 gettext_noop ("# Replies received from DHT"),
1001 memset (&prq, 0, sizeof (prq));
1003 prq.expiration = exp;
1006 process_reply (&prq, key, pr);
1007 if ( (GNUNET_YES == active_to_migration) &&
1008 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Replicating result for query `%s' with priority %u\n",
1016 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1017 pmc->start = GNUNET_TIME_absolute_get ();
1018 pmc->requested = GNUNET_YES;
1020 GNUNET_DATASTORE_put (GSF_dsh,
1022 type, prq.priority, 1 /* anonymity */,
1023 0 /* replication */,
1025 1 + prq.priority, MAX_DATASTORE_QUEUE,
1026 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1027 &put_migration_continuation,
1030 put_migration_continuation (pmc, GNUNET_NO, NULL);
1037 * Consider looking up the data in the DHT (anonymity-level permitting).
1039 * @param pr the pending request to process
1042 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1046 struct GNUNET_PeerIdentity pi;
1047 char buf[sizeof (GNUNET_HashCode) * 2];
1049 if (0 != pr->public_data.anonymity_level)
1053 GNUNET_DHT_get_stop (pr->gh);
1058 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1061 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
1062 xquery_size = sizeof (GNUNET_HashCode);
1064 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1066 GNUNET_assert (0 != pr->sender_pid);
1067 GNUNET_PEER_resolve (pr->sender_pid,
1069 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1070 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1072 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1073 GNUNET_TIME_UNIT_FOREVER_REL,
1074 pr->public_data.type,
1075 &pr->public_data.query,
1076 DEFAULT_GET_REPLICATION,
1077 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1088 * Task that issues a warning if the datastore lookup takes too long.
1090 * @param cls the 'struct GSF_PendingRequest'
1091 * @param tc task context
1094 warn_delay_task (void *cls,
1095 const struct GNUNET_SCHEDULER_TaskContext *tc)
1097 struct GSF_PendingRequest *pr = cls;
1099 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1100 _("Datastore lookup already took %llu ms!\n"),
1101 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1102 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1109 * Task that issues a warning if the datastore lookup takes too long.
1111 * @param cls the 'struct GSF_PendingRequest'
1112 * @param tc task context
1115 odc_warn_delay_task (void *cls,
1116 const struct GNUNET_SCHEDULER_TaskContext *tc)
1118 struct GSF_PendingRequest *pr = cls;
1120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1121 _("On-demand lookup already took %llu ms!\n"),
1122 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1123 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1124 &odc_warn_delay_task,
1130 * We're processing (local) results for a search request
1131 * from another peer. Pass applicable results to the
1132 * peer and if we are done either clean up (operation
1133 * complete) or forward to other peers (more results possible).
1135 * @param cls our closure (struct PendingRequest)
1136 * @param key key for the content
1137 * @param size number of bytes in data
1138 * @param data content stored
1139 * @param type type of the content
1140 * @param priority priority of the content
1141 * @param anonymity anonymity-level for the content
1142 * @param expiration expiration time for the content
1143 * @param uid unique identifier for the datum;
1144 * maybe 0 if no unique identifier is available
1147 process_local_reply (void *cls,
1148 const GNUNET_HashCode *key,
1151 enum GNUNET_BLOCK_Type type,
1154 struct GNUNET_TIME_Absolute expiration,
1157 struct GSF_PendingRequest *pr = cls;
1158 GSF_LocalLookupContinuation cont;
1159 struct ProcessReplyClosure prq;
1160 GNUNET_HashCode query;
1161 unsigned int old_rf;
1163 GNUNET_SCHEDULER_cancel (pr->warn_task);
1164 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1170 GNUNET_STATISTICS_update (GSF_stats,
1171 gettext_noop ("# Datastore lookups concluded (no results)"),
1175 if (GNUNET_NO == pr->have_first_uid)
1177 pr->first_uid = uid;
1178 pr->have_first_uid = 1;
1182 if ( (uid == pr->first_uid) && (key != NULL) )
1184 GNUNET_STATISTICS_update (GSF_stats,
1185 gettext_noop ("# Datastore lookups concluded (seen all)"),
1188 key = NULL; /* all replies seen! */
1190 pr->have_first_uid++;
1191 if ( (pr->have_first_uid > MAX_RESULTS) && (key != NULL) )
1193 GNUNET_STATISTICS_update (GSF_stats,
1194 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1197 key = NULL; /* all replies seen! */
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1205 "No further local responses available.\n");
1207 if ( (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1208 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK) )
1209 GNUNET_STATISTICS_update (GSF_stats,
1210 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1213 goto check_error_and_continue;
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1220 (unsigned long long) uid);
1222 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Found ONDEMAND block, performing on-demand encoding\n");
1228 GNUNET_STATISTICS_update (GSF_stats,
1229 gettext_noop ("# on-demand blocks matched requests"),
1232 pr->qe_start = GNUNET_TIME_absolute_get ();
1233 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1234 &odc_warn_delay_task,
1237 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1238 anonymity, expiration, uid,
1239 &process_local_reply,
1242 GNUNET_STATISTICS_update (GSF_stats,
1243 gettext_noop ("# on-demand lookups performed successfully"),
1246 return; /* we're done */
1248 GNUNET_STATISTICS_update (GSF_stats,
1249 gettext_noop ("# on-demand lookups failed"),
1252 GNUNET_SCHEDULER_cancel (pr->warn_task);
1253 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1256 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1257 pr->local_result_offset - 1,
1258 &pr->public_data.query,
1259 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1260 ? GNUNET_BLOCK_TYPE_ANY
1261 : pr->public_data.type,
1262 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1264 : 1 /* queue priority */,
1265 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1267 : 1 /* max queue size */,
1268 GNUNET_TIME_UNIT_FOREVER_REL,
1269 &process_local_reply,
1273 GNUNET_STATISTICS_update (GSF_stats,
1274 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1277 return; /* we're done */
1279 goto check_error_and_continue;
1281 old_rf = pr->public_data.results_found;
1282 memset (&prq, 0, sizeof (prq));
1284 prq.expiration = expiration;
1287 GNUNET_BLOCK_get_key (GSF_block_ctx,
1294 GNUNET_DATASTORE_remove (GSF_dsh,
1298 GNUNET_TIME_UNIT_FOREVER_REL,
1300 pr->qe_start = GNUNET_TIME_absolute_get ();
1301 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1304 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1305 pr->local_result_offset - 1,
1306 &pr->public_data.query,
1307 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1308 ? GNUNET_BLOCK_TYPE_ANY
1309 : pr->public_data.type,
1310 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1312 : 1 /* queue priority */,
1313 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1315 : 1 /* max queue size */,
1316 GNUNET_TIME_UNIT_FOREVER_REL,
1317 &process_local_reply,
1321 GNUNET_STATISTICS_update (GSF_stats,
1322 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1325 goto check_error_and_continue;
1330 prq.priority = priority;
1331 prq.request_found = GNUNET_NO;
1332 prq.anonymity_level = anonymity;
1333 if ( (old_rf == 0) &&
1334 (pr->public_data.results_found == 0) )
1335 GSF_update_datastore_delay_ (pr->public_data.start_time);
1336 process_reply (&prq, key, pr);
1337 pr->local_result = prq.eval;
1338 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1340 GNUNET_STATISTICS_update (GSF_stats,
1341 gettext_noop ("# Datastore lookups concluded (found ultimate result)"),
1344 goto check_error_and_continue;
1346 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1347 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1348 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "Load too high, done with request\n");
1354 GNUNET_STATISTICS_update (GSF_stats,
1355 gettext_noop ("# Datastore lookups concluded (load too high)"),
1358 goto check_error_and_continue;
1360 pr->qe_start = GNUNET_TIME_absolute_get ();
1361 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1364 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1365 pr->local_result_offset++,
1366 &pr->public_data.query,
1367 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1368 ? GNUNET_BLOCK_TYPE_ANY
1369 : pr->public_data.type,
1370 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1372 : 1 /* queue priority */,
1373 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1375 : 1 /* max queue size */,
1376 GNUNET_TIME_UNIT_FOREVER_REL,
1377 &process_local_reply,
1379 /* check if we successfully queued another datastore request;
1380 if so, return, otherwise call our continuation (if we have
1382 check_error_and_continue:
1385 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1387 GNUNET_SCHEDULER_cancel (pr->warn_task);
1388 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1390 if (NULL == (cont = pr->llc_cont))
1391 return; /* no continuation */
1392 pr->llc_cont = NULL;
1393 cont (pr->llc_cont_cls,
1400 * Look up the request in the local datastore.
1402 * @param pr the pending request to process
1403 * @param cont function to call at the end
1404 * @param cont_cls closure for cont
1407 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1408 GSF_LocalLookupContinuation cont,
1411 GNUNET_assert (NULL == pr->gh);
1412 GNUNET_assert (NULL == pr->llc_cont);
1413 pr->llc_cont = cont;
1414 pr->llc_cont_cls = cont_cls;
1415 pr->qe_start = GNUNET_TIME_absolute_get ();
1416 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1419 GNUNET_STATISTICS_update (GSF_stats,
1420 gettext_noop ("# Datastore lookups initiated"),
1423 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1424 pr->local_result_offset++,
1425 &pr->public_data.query,
1426 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1427 ? GNUNET_BLOCK_TYPE_ANY
1428 : pr->public_data.type,
1429 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1431 : 1 /* queue priority */,
1432 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1434 : 1 /* max queue size */,
1435 GNUNET_TIME_UNIT_FOREVER_REL,
1436 &process_local_reply,
1440 GNUNET_STATISTICS_update (GSF_stats,
1441 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1447 GNUNET_SCHEDULER_cancel (pr->warn_task);
1448 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1449 pr->llc_cont = NULL;
1451 cont (cont_cls, pr, pr->local_result);
1457 * Handle P2P "CONTENT" message. Checks that the message is
1458 * well-formed and then checks if there are any pending requests for
1459 * this content and possibly passes it on (to local clients or other
1460 * peers). Does NOT perform migration (content caching at this peer).
1462 * @param cp the other peer involved (sender or receiver, NULL
1463 * for loopback messages where we are both sender and receiver)
1464 * @param message the actual message
1465 * @return GNUNET_OK if the message was well-formed,
1466 * GNUNET_SYSERR if the message was malformed (close connection,
1467 * do not cache under any circumstances)
1470 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1471 const struct GNUNET_MessageHeader *message)
1473 const struct PutMessage *put;
1476 enum GNUNET_BLOCK_Type type;
1477 struct GNUNET_TIME_Absolute expiration;
1478 GNUNET_HashCode query;
1479 struct ProcessReplyClosure prq;
1480 struct GNUNET_TIME_Relative block_time;
1482 struct PutMigrationContext *pmc;
1484 msize = ntohs (message->size);
1485 if (msize < sizeof (struct PutMessage))
1488 return GNUNET_SYSERR;
1490 put = (const struct PutMessage*) message;
1491 dsize = msize - sizeof (struct PutMessage);
1492 type = ntohl (put->type);
1493 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1494 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1495 return GNUNET_SYSERR;
1497 GNUNET_BLOCK_get_key (GSF_block_ctx,
1503 GNUNET_break_op (0);
1504 return GNUNET_SYSERR;
1506 GNUNET_STATISTICS_update (GSF_stats,
1507 gettext_noop ("# GAP PUT messages received"),
1510 /* now, lookup 'query' */
1511 prq.data = (const void*) &put[1];
1518 prq.expiration = expiration;
1520 prq.anonymity_level = UINT32_MAX;
1521 prq.request_found = GNUNET_NO;
1522 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1528 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1529 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1531 if ( (GNUNET_YES == active_to_migration) &&
1532 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1536 "Replicating result for query `%s' with priority %u\n",
1537 GNUNET_h2s (&query),
1540 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1541 pmc->start = GNUNET_TIME_absolute_get ();
1542 pmc->requested = prq.request_found;
1543 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1544 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1547 GNUNET_DATASTORE_put (GSF_dsh,
1548 0, &query, dsize, &put[1],
1549 type, prq.priority, 1 /* anonymity */,
1550 0 /* replication */,
1552 1 + prq.priority, MAX_DATASTORE_QUEUE,
1553 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1554 &put_migration_continuation,
1557 put_migration_continuation (pmc, GNUNET_NO, NULL);
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Choosing not to keep content `%s' (%d/%d)\n",
1565 GNUNET_h2s (&query),
1566 active_to_migration,
1567 test_put_load_too_high (prq.priority));
1570 putl = GNUNET_LOAD_get_load (datastore_put_load);
1571 if ( (NULL != (cp = prq.sender)) &&
1572 (GNUNET_NO == prq.request_found) &&
1573 ( (GNUNET_YES != active_to_migration) ||
1574 (putl > 2.5 * (1 + prq.priority)) ) )
1576 if (GNUNET_YES != active_to_migration)
1577 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1578 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1579 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1580 (unsigned int) (60000 * putl * putl)));
1581 GSF_block_peer_migration_ (cp, block_time);
1588 * Setup the subsystem.
1591 GSF_pending_request_init_ ()
1594 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1596 "MAX_PENDING_REQUESTS",
1597 &max_pending_requests))
1599 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1600 _("Configuration fails to specify `%s', assuming default value."),
1601 "MAX_PENDING_REQUESTS");
1603 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1606 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1607 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1608 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1613 * Shutdown the subsystem.
1616 GSF_pending_request_done_ ()
1618 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1621 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1623 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1624 requests_by_expiration_heap = NULL;
1625 GNUNET_LOAD_value_free (datastore_put_load);
1626 datastore_put_load = NULL;
1630 /* end of gnunet-service-fs_pr.c */