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 GNUNET_STATISTICS_update (GSF_stats,
309 gettext_noop ("# Pending requests created"),
312 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
313 pr->local_result_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
315 pr->public_data.query = *query;
316 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
318 GNUNET_assert (NULL != namespace);
319 pr->public_data.namespace = *namespace;
323 pr->public_data.target = *target;
324 pr->public_data.has_target = GNUNET_YES;
326 pr->public_data.anonymity_level = anonymity_level;
327 pr->public_data.priority = priority;
328 pr->public_data.original_priority = priority;
329 pr->public_data.options = options;
330 pr->public_data.type = type;
331 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
332 pr->sender_pid = sender_pid;
335 GNUNET_assert ( (sender_pid != 0) ||
336 (0 == (options & GSF_PRO_FORWARD_ONLY)) );
338 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
341 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
342 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
343 (uint32_t) (- ttl)));
344 if (replies_seen_count > 0)
346 pr->replies_seen_size = replies_seen_count;
347 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
348 memcpy (pr->replies_seen,
350 replies_seen_count * sizeof (GNUNET_HashCode));
351 pr->replies_seen_count = replies_seen_count;
355 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
360 else if ( (replies_seen_count > 0) &&
361 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
363 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
365 GNUNET_CONTAINER_multihashmap_put (pr_map,
368 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
369 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
371 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
373 pr->public_data.ttl.abs_value);
374 /* make sure we don't track too many requests */
375 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
377 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
378 GNUNET_assert (dpr != NULL);
380 break; /* let the request live briefly... */
381 dpr->rh (dpr->rh_cls,
382 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
385 GNUNET_TIME_UNIT_FOREVER_ABS,
386 GNUNET_BLOCK_TYPE_ANY,
388 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
391 GNUNET_STATISTICS_update (GSF_stats,
392 gettext_noop ("# Pending requests active"),
400 * Obtain the public data associated with a pending request
402 * @param pr pending request
403 * @return associated public data
405 struct GSF_PendingRequestData *
406 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
408 return &pr->public_data;
413 * Test if two pending requests are compatible (would generate
414 * the same query modulo filters and should thus be processed
417 * @param pra a pending request
418 * @param pra another pending request
419 * @return GNUNET_OK if the requests are compatible
422 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
423 struct GSF_PendingRequest *prb)
425 if ( (pra->public_data.type != prb->public_data.type) ||
426 (0 != memcmp (&pra->public_data.query,
427 &prb->public_data.query,
428 sizeof (GNUNET_HashCode))) ||
429 ( (pra->public_data.type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
430 (0 != memcmp (&pra->public_data.namespace,
431 &prb->public_data.namespace,
432 sizeof (GNUNET_HashCode))) ) )
440 * Update a given pending request with additional replies
441 * that have been seen.
443 * @param pr request to update
444 * @param replies_seen hash codes of replies that we've seen
445 * @param replies_seen_count size of the replies_seen array
448 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
449 const GNUNET_HashCode *replies_seen,
450 unsigned int replies_seen_count)
453 GNUNET_HashCode mhash;
455 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
456 return; /* integer overflow */
457 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
459 /* we're responsible for the BF, full refresh */
460 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
461 GNUNET_array_grow (pr->replies_seen,
462 pr->replies_seen_size,
463 replies_seen_count + pr->replies_seen_count);
464 memcpy (&pr->replies_seen[pr->replies_seen_count],
466 sizeof (GNUNET_HashCode) * replies_seen_count);
467 pr->replies_seen_count += replies_seen_count;
468 if (GNUNET_NO == refresh_bloomfilter (pr))
470 /* bf not recalculated, simply extend it with new bits */
471 for (i=0;i<replies_seen_count;i++)
473 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
476 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
484 /* we're not the initiator, but the initiator did not give us
485 any bloom-filter, so we need to create one on-the-fly */
486 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
488 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
489 compute_bloomfilter_size (replies_seen_count),
492 for (i=0;i<pr->replies_seen_count;i++)
494 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
497 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
504 * Generate the message corresponding to the given pending request for
505 * transmission to other peers (or at least determine its size).
507 * @param pr request to generate the message for
508 * @param buf_size number of bytes available in buf
509 * @param buf where to copy the message (can be NULL)
510 * @return number of bytes needed (if > buf_size) or used
513 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
517 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
518 struct GetMessage *gm;
519 GNUNET_HashCode *ext;
525 struct GNUNET_TIME_Absolute now;
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Building request message for `%s' of type %d\n",
533 GNUNET_h2s (&pr->public_data.query),
534 pr->public_data.type);
538 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
539 if ( (! do_route) && (pr->sender_pid == 0))
542 do_route = GNUNET_YES;
546 bm |= GET_MESSAGE_BIT_RETURN_TO;
549 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
551 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
554 if (GNUNET_YES == pr->public_data.has_target)
556 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
559 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
560 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
561 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
562 if (buf_size < msize)
564 gm = (struct GetMessage*) lbuf;
565 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
566 gm->header.size = htons (msize);
567 gm->type = htonl (pr->public_data.type);
569 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
570 pr->public_data.priority + 1);
573 pr->public_data.priority -= prio;
574 gm->priority = htonl (prio);
575 now = GNUNET_TIME_absolute_get ();
576 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
577 gm->ttl = htonl (ttl / 1000);
578 gm->filter_mutator = htonl(pr->mingle);
579 gm->hash_bitmap = htonl (bm);
580 gm->query = pr->public_data.query;
581 ext = (GNUNET_HashCode*) &gm[1];
584 GNUNET_PEER_resolve (pr->sender_pid,
585 (struct GNUNET_PeerIdentity*) &ext[k++]);
586 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
588 &pr->public_data.namespace,
589 sizeof (GNUNET_HashCode));
590 if (GNUNET_YES == pr->public_data.has_target)
591 ext[k++] = pr->public_data.target.hashPubKey;
593 GNUNET_assert (GNUNET_SYSERR !=
594 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
597 memcpy (buf, gm, msize);
603 * Iterator to free pending requests.
605 * @param cls closure, unused
606 * @param key current key code
607 * @param value value in the hash map (pending request)
608 * @return GNUNET_YES (we should continue to iterate)
611 clean_request (void *cls,
612 const GNUNET_HashCode * key,
615 struct GSF_PendingRequest *pr = value;
616 GSF_LocalLookupContinuation cont;
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Cleaning up pending request for `%s'.\n",
623 if (NULL != (cont = pr->llc_cont))
626 cont (pr->llc_cont_cls,
630 GSF_plan_notify_request_done_ (pr);
631 GNUNET_free_non_null (pr->replies_seen);
634 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
637 GNUNET_PEER_change_rc (pr->sender_pid, -1);
639 if (NULL != pr->hnode)
641 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
646 GNUNET_DATASTORE_cancel (pr->qe);
651 GNUNET_DHT_get_stop (pr->gh);
654 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
656 GNUNET_SCHEDULER_cancel (pr->warn_task);
657 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
659 GNUNET_assert (GNUNET_OK ==
660 GNUNET_CONTAINER_multihashmap_remove (pr_map,
661 &pr->public_data.query,
663 GNUNET_STATISTICS_update (GSF_stats,
664 gettext_noop ("# Pending requests active"),
673 * Explicitly cancel a pending request.
675 * @param pr request to cancel
676 * @param full_cleanup fully purge the request
679 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
682 GSF_LocalLookupContinuation cont;
685 return; /* already cleaned up! */
686 if (GNUNET_YES != full_cleanup)
688 /* make request inactive (we're no longer interested in more results),
689 but do NOT remove from our data-structures, we still need it there
690 to prevent the request from looping */
692 if (NULL != (cont = pr->llc_cont))
695 cont (pr->llc_cont_cls,
699 GSF_plan_notify_request_done_ (pr);
702 GNUNET_DATASTORE_cancel (pr->qe);
707 GNUNET_DHT_get_stop (pr->gh);
710 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
712 GNUNET_SCHEDULER_cancel (pr->warn_task);
713 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
717 GNUNET_assert (GNUNET_YES ==
718 clean_request (NULL, &pr->public_data.query, pr));
723 * Iterate over all pending requests.
725 * @param it function to call for each request
726 * @param cls closure for it
729 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
732 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
733 (GNUNET_CONTAINER_HashMapIterator) it,
741 * Closure for "process_reply" function.
743 struct ProcessReplyClosure
746 * The data for the reply.
751 * Who gave us this reply? NULL for local host (or DHT)
753 struct GSF_ConnectedPeer *sender;
756 * When the reply expires.
758 struct GNUNET_TIME_Absolute expiration;
768 enum GNUNET_BLOCK_Type type;
771 * How much was this reply worth to us?
776 * Anonymity requirements for this reply.
778 uint32_t anonymity_level;
781 * Evaluation result (returned).
783 enum GNUNET_BLOCK_EvaluationResult eval;
786 * Did we find a matching request?
793 * Update the performance data for the sender (if any) since
794 * the sender successfully answered one of our queries.
796 * @param prq information about the sender
797 * @param pr request that was satisfied
800 update_request_performance_data (struct ProcessReplyClosure *prq,
801 struct GSF_PendingRequest *pr)
803 if (prq->sender == NULL)
805 GSF_peer_update_performance_ (prq->sender,
806 pr->public_data.start_time,
812 * We have received a reply; handle it!
814 * @param cls response (struct ProcessReplyClosure)
815 * @param key our query
816 * @param value value in the hash map (info about the query)
817 * @return GNUNET_YES (we should continue to iterate)
820 process_reply (void *cls,
821 const GNUNET_HashCode *key,
824 struct ProcessReplyClosure *prq = cls;
825 struct GSF_PendingRequest *pr = value;
826 GNUNET_HashCode chash;
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Matched result (type %u) for query `%s' with pending request\n",
833 (unsigned int) prq->type,
836 GNUNET_STATISTICS_update (GSF_stats,
837 gettext_noop ("# replies received and matched"),
840 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
845 &pr->public_data.namespace,
846 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
851 case GNUNET_BLOCK_EVALUATION_OK_MORE:
852 update_request_performance_data (prq, pr);
854 case GNUNET_BLOCK_EVALUATION_OK_LAST:
855 /* short cut: stop processing early, no BF-update, etc. */
856 update_request_performance_data (prq, pr);
857 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
858 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
859 /* pass on to other peers / local clients */
863 prq->anonymity_level,
866 prq->data, prq->size);
868 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
869 GNUNET_STATISTICS_update (GSF_stats,
870 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Duplicate response `%s', discarding.\n",
876 GNUNET_h2s (&mhash));
878 return GNUNET_YES; /* duplicate */
879 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
880 return GNUNET_YES; /* wrong namespace */
881 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
884 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
887 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
889 _("Unsupported block type %u\n"),
893 /* update bloomfilter */
894 GNUNET_CRYPTO_hash (prq->data,
897 GSF_pending_request_update_ (pr, &chash, 1);
898 if (NULL == prq->sender)
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Found result for query `%s' in local datastore\n",
905 GNUNET_STATISTICS_update (GSF_stats,
906 gettext_noop ("# results found locally"),
912 GSF_dht_lookup_ (pr);
914 prq->priority += pr->public_data.original_priority;
915 pr->public_data.priority = 0;
916 pr->public_data.original_priority = 0;
917 pr->public_data.results_found++;
918 prq->request_found = GNUNET_YES;
919 /* finally, pass on to other peer / local client */
923 prq->anonymity_level,
926 prq->data, prq->size);
932 * Context for the 'put_migration_continuation'.
934 struct PutMigrationContext
938 * Start time for the operation.
940 struct GNUNET_TIME_Absolute start;
945 struct GNUNET_PeerIdentity origin;
948 * GNUNET_YES if we had a matching request for this block,
956 * Continuation called to notify client about result of the
960 * @param success GNUNET_SYSERR on failure
961 * @param msg NULL on success, otherwise an error message
964 put_migration_continuation (void *cls,
968 struct PutMigrationContext *pmc = cls;
969 struct GNUNET_TIME_Relative delay;
970 struct GNUNET_TIME_Relative block_time;
971 struct GSF_ConnectedPeer *cp;
972 struct GSF_PeerPerformanceData *ppd;
974 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
975 cp = GSF_peer_get_ (&pmc->origin);
976 if ( (GNUNET_OK != success) &&
977 (GNUNET_NO == pmc->requested) )
979 /* block migration for a bit... */
982 ppd = GSF_get_peer_performance_data_ (cp);
983 ppd->migration_duplication++;
984 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
985 5 * ppd->migration_duplication +
986 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
987 GSF_block_peer_migration_ (cp, block_time);
994 ppd = GSF_get_peer_performance_data_ (cp);
995 ppd->migration_duplication = 0; /* reset counter */
999 /* FIXME: should we really update the load value on failure? */
1000 if (NULL != datastore_put_load)
1001 GNUNET_LOAD_update (datastore_put_load,
1003 if (GNUNET_OK == success)
1005 GNUNET_STATISTICS_update (GSF_stats,
1006 gettext_noop ("# Datastore `PUT' failures"),
1013 * Test if the DATABASE (PUT) load on this peer is too high
1014 * to even consider processing the query at
1017 * @return GNUNET_YES if the load is too high to do anything (load high)
1018 * GNUNET_NO to process normally (load normal or low)
1021 test_put_load_too_high (uint32_t priority)
1025 if (NULL == datastore_put_load)
1027 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1028 return GNUNET_NO; /* very fast */
1029 ld = GNUNET_LOAD_get_load (datastore_put_load);
1030 if (ld < 2.0 * (1 + priority))
1032 GNUNET_STATISTICS_update (GSF_stats,
1033 gettext_noop ("# storage requests dropped due to high load"),
1041 * Iterator called on each result obtained for a DHT
1042 * operation that expects a reply
1044 * @param cls closure
1045 * @param exp when will this value expire
1046 * @param key key of the result
1047 * @param get_path NULL-terminated array of pointers
1048 * to the peers on reverse GET path (or NULL if not recorded)
1049 * @param put_path NULL-terminated array of pointers
1050 * to the peers on the PUT path (or NULL if not recorded)
1051 * @param type type of the result
1052 * @param size number of bytes in data
1053 * @param data pointer to the result data
1056 handle_dht_reply (void *cls,
1057 struct GNUNET_TIME_Absolute exp,
1058 const GNUNET_HashCode *key,
1059 const struct GNUNET_PeerIdentity * const *get_path,
1060 const struct GNUNET_PeerIdentity * const *put_path,
1061 enum GNUNET_BLOCK_Type type,
1065 struct GSF_PendingRequest *pr = cls;
1066 struct ProcessReplyClosure prq;
1067 struct PutMigrationContext *pmc;
1069 GNUNET_STATISTICS_update (GSF_stats,
1070 gettext_noop ("# Replies received from DHT"),
1073 memset (&prq, 0, sizeof (prq));
1075 prq.expiration = exp;
1078 process_reply (&prq, key, pr);
1079 if ( (GNUNET_YES == active_to_migration) &&
1080 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Replicating result for query `%s' with priority %u\n",
1088 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1089 pmc->start = GNUNET_TIME_absolute_get ();
1090 pmc->requested = GNUNET_YES;
1092 GNUNET_DATASTORE_put (GSF_dsh,
1094 type, prq.priority, 1 /* anonymity */,
1095 0 /* replication */,
1097 1 + prq.priority, MAX_DATASTORE_QUEUE,
1098 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1099 &put_migration_continuation,
1102 put_migration_continuation (pmc, GNUNET_NO, 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 (GNUNET_HashCode) * 2];
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 (GNUNET_HashCode));
1134 xquery_size = sizeof (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,
1141 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1142 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1144 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1145 GNUNET_TIME_UNIT_FOREVER_REL,
1146 pr->public_data.type,
1147 &pr->public_data.query,
1148 DEFAULT_GET_REPLICATION,
1149 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1160 * Task that issues a warning if the datastore lookup takes too long.
1162 * @param cls the 'struct GSF_PendingRequest'
1163 * @param tc task context
1166 warn_delay_task (void *cls,
1167 const struct GNUNET_SCHEDULER_TaskContext *tc)
1169 struct GSF_PendingRequest *pr = cls;
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1172 _("Datastore lookup already took %llu ms!\n"),
1173 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1174 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1181 * Task that issues a warning if the datastore lookup takes too long.
1183 * @param cls the 'struct GSF_PendingRequest'
1184 * @param tc task context
1187 odc_warn_delay_task (void *cls,
1188 const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 struct GSF_PendingRequest *pr = cls;
1192 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1193 _("On-demand lookup already took %llu ms!\n"),
1194 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1195 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1196 &odc_warn_delay_task,
1202 * We're processing (local) results for a search request
1203 * from another peer. Pass applicable results to the
1204 * peer and if we are done either clean up (operation
1205 * complete) or forward to other peers (more results possible).
1207 * @param cls our closure (struct PendingRequest)
1208 * @param key key for the content
1209 * @param size number of bytes in data
1210 * @param data content stored
1211 * @param type type of the content
1212 * @param priority priority of the content
1213 * @param anonymity anonymity-level for the content
1214 * @param expiration expiration time for the content
1215 * @param uid unique identifier for the datum;
1216 * maybe 0 if no unique identifier is available
1219 process_local_reply (void *cls,
1220 const GNUNET_HashCode *key,
1223 enum GNUNET_BLOCK_Type type,
1226 struct GNUNET_TIME_Absolute expiration,
1229 struct GSF_PendingRequest *pr = cls;
1230 GSF_LocalLookupContinuation cont;
1231 struct ProcessReplyClosure prq;
1232 GNUNET_HashCode query;
1233 unsigned int old_rf;
1235 GNUNET_SCHEDULER_cancel (pr->warn_task);
1236 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1242 GNUNET_STATISTICS_update (GSF_stats,
1243 gettext_noop ("# Datastore lookups concluded (no results)"),
1247 if (GNUNET_NO == pr->have_first_uid)
1249 pr->first_uid = uid;
1250 pr->have_first_uid = 1;
1254 if ( (uid == pr->first_uid) && (key != NULL) )
1256 GNUNET_STATISTICS_update (GSF_stats,
1257 gettext_noop ("# Datastore lookups concluded (seen all)"),
1260 key = NULL; /* all replies seen! */
1262 pr->have_first_uid++;
1263 if ( (pr->have_first_uid > MAX_RESULTS) && (key != NULL) )
1265 GNUNET_STATISTICS_update (GSF_stats,
1266 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1269 key = NULL; /* all replies seen! */
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "No further local responses available.\n");
1279 if ( (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1280 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK) )
1281 GNUNET_STATISTICS_update (GSF_stats,
1282 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1285 goto check_error_and_continue;
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1292 (unsigned long long) uid);
1294 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Found ONDEMAND block, performing on-demand encoding\n");
1300 GNUNET_STATISTICS_update (GSF_stats,
1301 gettext_noop ("# on-demand blocks matched requests"),
1304 pr->qe_start = GNUNET_TIME_absolute_get ();
1305 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1306 &odc_warn_delay_task,
1309 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1310 anonymity, expiration, uid,
1311 &process_local_reply,
1314 GNUNET_STATISTICS_update (GSF_stats,
1315 gettext_noop ("# on-demand lookups performed successfully"),
1318 return; /* we're done */
1320 GNUNET_STATISTICS_update (GSF_stats,
1321 gettext_noop ("# on-demand lookups failed"),
1324 GNUNET_SCHEDULER_cancel (pr->warn_task);
1325 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1328 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1329 pr->local_result_offset - 1,
1330 &pr->public_data.query,
1331 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1332 ? GNUNET_BLOCK_TYPE_ANY
1333 : pr->public_data.type,
1334 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1336 : 1 /* queue priority */,
1337 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1339 : 1 /* max queue size */,
1340 GNUNET_TIME_UNIT_FOREVER_REL,
1341 &process_local_reply,
1345 GNUNET_STATISTICS_update (GSF_stats,
1346 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1349 return; /* we're done */
1351 goto check_error_and_continue;
1353 old_rf = pr->public_data.results_found;
1354 memset (&prq, 0, sizeof (prq));
1356 prq.expiration = expiration;
1359 GNUNET_BLOCK_get_key (GSF_block_ctx,
1366 GNUNET_DATASTORE_remove (GSF_dsh,
1370 GNUNET_TIME_UNIT_FOREVER_REL,
1372 pr->qe_start = GNUNET_TIME_absolute_get ();
1373 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1376 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1377 pr->local_result_offset - 1,
1378 &pr->public_data.query,
1379 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1380 ? GNUNET_BLOCK_TYPE_ANY
1381 : pr->public_data.type,
1382 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1384 : 1 /* queue priority */,
1385 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1387 : 1 /* max queue size */,
1388 GNUNET_TIME_UNIT_FOREVER_REL,
1389 &process_local_reply,
1393 GNUNET_STATISTICS_update (GSF_stats,
1394 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1397 goto check_error_and_continue;
1402 prq.priority = priority;
1403 prq.request_found = GNUNET_NO;
1404 prq.anonymity_level = anonymity;
1405 if ( (old_rf == 0) &&
1406 (pr->public_data.results_found == 0) )
1407 GSF_update_datastore_delay_ (pr->public_data.start_time);
1408 process_reply (&prq, key, pr);
1409 pr->local_result = prq.eval;
1410 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1412 GNUNET_STATISTICS_update (GSF_stats,
1413 gettext_noop ("# Datastore lookups concluded (found ultimate result)"),
1416 goto check_error_and_continue;
1418 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1419 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1420 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Load too high, done with request\n");
1426 GNUNET_STATISTICS_update (GSF_stats,
1427 gettext_noop ("# Datastore lookups concluded (load too high)"),
1430 goto check_error_and_continue;
1432 pr->qe_start = GNUNET_TIME_absolute_get ();
1433 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1436 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1437 pr->local_result_offset++,
1438 &pr->public_data.query,
1439 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1440 ? GNUNET_BLOCK_TYPE_ANY
1441 : pr->public_data.type,
1442 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1444 : 1 /* queue priority */,
1445 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1447 : 1 /* max queue size */,
1448 GNUNET_TIME_UNIT_FOREVER_REL,
1449 &process_local_reply,
1451 /* check if we successfully queued another datastore request;
1452 if so, return, otherwise call our continuation (if we have
1454 check_error_and_continue:
1457 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1459 GNUNET_SCHEDULER_cancel (pr->warn_task);
1460 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1462 if (NULL == (cont = pr->llc_cont))
1463 return; /* no continuation */
1464 pr->llc_cont = NULL;
1465 cont (pr->llc_cont_cls,
1472 * Look up the request in the local datastore.
1474 * @param pr the pending request to process
1475 * @param cont function to call at the end
1476 * @param cont_cls closure for cont
1479 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1480 GSF_LocalLookupContinuation cont,
1483 GNUNET_assert (NULL == pr->gh);
1484 GNUNET_assert (NULL == pr->llc_cont);
1485 pr->llc_cont = cont;
1486 pr->llc_cont_cls = cont_cls;
1487 pr->qe_start = GNUNET_TIME_absolute_get ();
1488 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1491 GNUNET_STATISTICS_update (GSF_stats,
1492 gettext_noop ("# Datastore lookups initiated"),
1495 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1496 pr->local_result_offset++,
1497 &pr->public_data.query,
1498 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1499 ? GNUNET_BLOCK_TYPE_ANY
1500 : pr->public_data.type,
1501 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1503 : 1 /* queue priority */,
1504 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1506 : 1 /* max queue size */,
1507 GNUNET_TIME_UNIT_FOREVER_REL,
1508 &process_local_reply,
1512 GNUNET_STATISTICS_update (GSF_stats,
1513 gettext_noop ("# Datastore lookups concluded (error queueing)"),
1519 GNUNET_SCHEDULER_cancel (pr->warn_task);
1520 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1521 pr->llc_cont = NULL;
1523 cont (cont_cls, pr, pr->local_result);
1529 * Handle P2P "CONTENT" message. Checks that the message is
1530 * well-formed and then checks if there are any pending requests for
1531 * this content and possibly passes it on (to local clients or other
1532 * peers). Does NOT perform migration (content caching at this peer).
1534 * @param cp the other peer involved (sender or receiver, NULL
1535 * for loopback messages where we are both sender and receiver)
1536 * @param message the actual message
1537 * @return GNUNET_OK if the message was well-formed,
1538 * GNUNET_SYSERR if the message was malformed (close connection,
1539 * do not cache under any circumstances)
1542 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1543 const struct GNUNET_MessageHeader *message)
1545 const struct PutMessage *put;
1548 enum GNUNET_BLOCK_Type type;
1549 struct GNUNET_TIME_Absolute expiration;
1550 GNUNET_HashCode query;
1551 struct ProcessReplyClosure prq;
1552 struct GNUNET_TIME_Relative block_time;
1554 struct PutMigrationContext *pmc;
1556 msize = ntohs (message->size);
1557 if (msize < sizeof (struct PutMessage))
1560 return GNUNET_SYSERR;
1562 put = (const struct PutMessage*) message;
1563 dsize = msize - sizeof (struct PutMessage);
1564 type = ntohl (put->type);
1565 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1566 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1567 return GNUNET_SYSERR;
1569 GNUNET_BLOCK_get_key (GSF_block_ctx,
1575 GNUNET_break_op (0);
1576 return GNUNET_SYSERR;
1578 GNUNET_STATISTICS_update (GSF_stats,
1579 gettext_noop ("# GAP PUT messages received"),
1582 /* now, lookup 'query' */
1583 prq.data = (const void*) &put[1];
1590 prq.expiration = expiration;
1592 prq.anonymity_level = UINT32_MAX;
1593 prq.request_found = GNUNET_NO;
1594 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1600 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1601 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1603 if ( (GNUNET_YES == active_to_migration) &&
1604 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Replicating result for query `%s' with priority %u\n",
1609 GNUNET_h2s (&query),
1612 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1613 pmc->start = GNUNET_TIME_absolute_get ();
1614 pmc->requested = prq.request_found;
1615 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1616 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1619 GNUNET_DATASTORE_put (GSF_dsh,
1620 0, &query, dsize, &put[1],
1621 type, prq.priority, 1 /* anonymity */,
1622 0 /* replication */,
1624 1 + prq.priority, MAX_DATASTORE_QUEUE,
1625 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1626 &put_migration_continuation,
1629 put_migration_continuation (pmc, GNUNET_NO, NULL);
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Choosing not to keep content `%s' (%d/%d)\n",
1637 GNUNET_h2s (&query),
1638 active_to_migration,
1639 test_put_load_too_high (prq.priority));
1642 putl = GNUNET_LOAD_get_load (datastore_put_load);
1643 if ( (NULL != (cp = prq.sender)) &&
1644 (GNUNET_NO == prq.request_found) &&
1645 ( (GNUNET_YES != active_to_migration) ||
1646 (putl > 2.5 * (1 + prq.priority)) ) )
1648 if (GNUNET_YES != active_to_migration)
1649 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1650 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1651 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1652 (unsigned int) (60000 * putl * putl)));
1653 GSF_block_peer_migration_ (cp, block_time);
1660 * Setup the subsystem.
1663 GSF_pending_request_init_ ()
1666 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1668 "MAX_PENDING_REQUESTS",
1669 &max_pending_requests))
1671 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1672 _("Configuration fails to specify `%s', assuming default value."),
1673 "MAX_PENDING_REQUESTS");
1675 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1678 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1679 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1680 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1685 * Shutdown the subsystem.
1688 GSF_pending_request_done_ ()
1690 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1693 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1695 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1696 requests_by_expiration_heap = NULL;
1697 GNUNET_LOAD_value_free (datastore_put_load);
1698 datastore_put_load = NULL;
1702 /* end of gnunet-service-fs_pr.c */