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);
193 * Recalculate our bloom filter for filtering replies. This function
194 * will create a new bloom filter from scratch, so it should only be
195 * called if we have no bloomfilter at all (and hence can create a
196 * fresh one of minimal size without problems) OR if our peer is the
197 * initiator (in which case we may resize to larger than mimimum size).
199 * @param pr request for which the BF is to be recomputed
202 refresh_bloomfilter (struct GSF_PendingRequest *pr)
205 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
207 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
208 pr->bf = GNUNET_BLOCK_construct_bloomfilter (pr->mingle,
210 pr->replies_seen_count);
215 * Create a new pending request.
217 * @param options request options
218 * @param type type of the block that is being requested
219 * @param query key for the lookup
220 * @param namespace namespace to lookup, NULL for no namespace
221 * @param target preferred target for the request, NULL for none
222 * @param bf_data raw data for bloom filter for known replies, can be NULL
223 * @param bf_size number of bytes in bf_data
224 * @param mingle mingle value for bf
225 * @param anonymity_level desired anonymity level
226 * @param priority maximum outgoing cummulative request priority to use
227 * @param ttl current time-to-live for the request
228 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
229 * @param replies_seen hash codes of known local replies
230 * @param replies_seen_count size of the 'replies_seen' array
231 * @param rh handle to call when we get a reply
232 * @param rh_cls closure for rh
233 * @return handle for the new pending request
235 struct GSF_PendingRequest *
236 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
237 enum GNUNET_BLOCK_Type type,
238 const GNUNET_HashCode * query,
239 const GNUNET_HashCode * namespace,
240 const struct GNUNET_PeerIdentity *target,
241 const char *bf_data, size_t bf_size,
242 uint32_t mingle, uint32_t anonymity_level,
243 uint32_t priority, int32_t ttl,
244 GNUNET_PEER_Id sender_pid,
245 const GNUNET_HashCode * replies_seen,
246 unsigned int replies_seen_count,
247 GSF_PendingRequestReplyHandler rh, void *rh_cls)
249 struct GSF_PendingRequest *pr;
250 struct GSF_PendingRequest *dpr;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Creating request handle for `%s' of type %d\n",
255 GNUNET_h2s (query), type);
257 GNUNET_STATISTICS_update (GSF_stats,
258 gettext_noop ("# Pending requests created"), 1,
260 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
261 pr->local_result_offset =
262 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
263 pr->public_data.query = *query;
264 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
266 GNUNET_assert (NULL != namespace);
267 pr->public_data.namespace = *namespace;
271 pr->public_data.target = *target;
272 pr->public_data.has_target = GNUNET_YES;
274 pr->public_data.anonymity_level = anonymity_level;
275 pr->public_data.priority = priority;
276 pr->public_data.original_priority = priority;
277 pr->public_data.options = options;
278 pr->public_data.type = type;
279 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
280 pr->sender_pid = sender_pid;
283 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
285 pr->public_data.ttl =
286 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
287 (GNUNET_TIME_UNIT_SECONDS,
290 pr->public_data.ttl =
291 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
292 GNUNET_TIME_relative_multiply
293 (GNUNET_TIME_UNIT_SECONDS,
295 if (replies_seen_count > 0)
297 pr->replies_seen_size = replies_seen_count;
299 GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
300 memcpy (pr->replies_seen, replies_seen,
301 replies_seen_count * sizeof (GNUNET_HashCode));
302 pr->replies_seen_count = replies_seen_count;
307 GNUNET_CONTAINER_bloomfilter_init (bf_data, bf_size, GNUNET_CONSTANTS_BLOOMFILTER_K);
310 else if ((replies_seen_count > 0) &&
311 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
313 refresh_bloomfilter (pr);
315 GNUNET_CONTAINER_multihashmap_put (pr_map, query, pr,
316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
317 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
320 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap, pr,
321 pr->public_data.ttl.abs_value);
322 /* make sure we don't track too many requests */
323 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
324 max_pending_requests)
326 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
327 GNUNET_assert (dpr != NULL);
329 break; /* let the request live briefly... */
330 dpr->rh (dpr->rh_cls, GNUNET_BLOCK_EVALUATION_REQUEST_VALID, dpr,
331 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_BLOCK_TYPE_ANY,
333 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
336 GNUNET_STATISTICS_update (GSF_stats,
337 gettext_noop ("# Pending requests active"), 1,
344 * Obtain the public data associated with a pending request
346 * @param pr pending request
347 * @return associated public data
349 struct GSF_PendingRequestData *
350 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
352 return &pr->public_data;
357 * Test if two pending requests are compatible (would generate
358 * the same query modulo filters and should thus be processed
361 * @param pra a pending request
362 * @param prb another pending request
363 * @return GNUNET_OK if the requests are compatible
366 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
367 struct GSF_PendingRequest *prb)
369 if ((pra->public_data.type != prb->public_data.type) ||
371 memcmp (&pra->public_data.query, &prb->public_data.query,
372 sizeof (GNUNET_HashCode))) ||
373 ((pra->public_data.type == GNUNET_BLOCK_TYPE_FS_SBLOCK) &&
375 memcmp (&pra->public_data.namespace, &prb->public_data.namespace,
376 sizeof (GNUNET_HashCode)))))
384 * Update a given pending request with additional replies
385 * that have been seen.
387 * @param pr request to update
388 * @param replies_seen hash codes of replies that we've seen
389 * @param replies_seen_count size of the replies_seen array
392 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
393 const GNUNET_HashCode * replies_seen,
394 unsigned int replies_seen_count)
397 GNUNET_HashCode mhash;
399 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
400 return; /* integer overflow */
401 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
403 /* we're responsible for the BF, full refresh */
404 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
405 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
406 replies_seen_count + pr->replies_seen_count);
407 memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
408 sizeof (GNUNET_HashCode) * replies_seen_count);
409 pr->replies_seen_count += replies_seen_count;
410 refresh_bloomfilter (pr);
416 /* we're not the initiator, but the initiator did not give us
417 * any bloom-filter, so we need to create one on-the-fly */
419 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
421 GNUNET_BLOCK_construct_bloomfilter (pr->mingle,
427 for (i = 0; i < pr->replies_seen_count; i++)
429 GNUNET_BLOCK_mingle_hash (&replies_seen[i], pr->mingle, &mhash);
430 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
438 * Generate the message corresponding to the given pending request for
439 * transmission to other peers (or at least determine its size).
441 * @param pr request to generate the message for
442 * @param buf_size number of bytes available in buf
443 * @param buf where to copy the message (can be NULL)
444 * @return number of bytes needed (if > buf_size) or used
447 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
448 size_t buf_size, void *buf)
450 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
451 struct GetMessage *gm;
452 GNUNET_HashCode *ext;
458 struct GNUNET_TIME_Absolute now;
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Building request message for `%s' of type %d\n",
466 GNUNET_h2s (&pr->public_data.query), pr->public_data.type);
470 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
471 if ((!do_route) && (pr->sender_pid == 0))
474 do_route = GNUNET_YES;
478 bm |= GET_MESSAGE_BIT_RETURN_TO;
481 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
483 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
486 if (GNUNET_YES == pr->public_data.has_target)
488 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
491 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
492 msize = sizeof (struct GetMessage) + bf_size + k * sizeof (GNUNET_HashCode);
493 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
494 if (buf_size < msize)
496 gm = (struct GetMessage *) lbuf;
497 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
498 gm->header.size = htons (msize);
499 gm->type = htonl (pr->public_data.type);
502 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
503 pr->public_data.priority + 1);
506 pr->public_data.priority -= prio;
507 gm->priority = htonl (prio);
508 now = GNUNET_TIME_absolute_get ();
509 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
510 gm->ttl = htonl (ttl / 1000);
511 gm->filter_mutator = htonl (pr->mingle);
512 gm->hash_bitmap = htonl (bm);
513 gm->query = pr->public_data.query;
514 ext = (GNUNET_HashCode *) & gm[1];
517 GNUNET_PEER_resolve (pr->sender_pid,
518 (struct GNUNET_PeerIdentity *) &ext[k++]);
519 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
520 memcpy (&ext[k++], &pr->public_data.namespace, sizeof (GNUNET_HashCode));
521 if (GNUNET_YES == pr->public_data.has_target)
522 ext[k++] = pr->public_data.target.hashPubKey;
524 GNUNET_assert (GNUNET_SYSERR !=
525 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
528 memcpy (buf, gm, msize);
534 * Iterator to free pending requests.
536 * @param cls closure, unused
537 * @param key current key code
538 * @param value value in the hash map (pending request)
539 * @return GNUNET_YES (we should continue to iterate)
542 clean_request (void *cls, const GNUNET_HashCode * key, void *value)
544 struct GSF_PendingRequest *pr = value;
545 GSF_LocalLookupContinuation cont;
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Cleaning up pending request for `%s'.\n", GNUNET_h2s (key));
551 if (NULL != (cont = pr->llc_cont))
554 cont (pr->llc_cont_cls, pr, pr->local_result);
556 GSF_plan_notify_request_done_ (pr);
557 GNUNET_free_non_null (pr->replies_seen);
560 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
563 GNUNET_PEER_change_rc (pr->sender_pid, -1);
565 if (NULL != pr->hnode)
567 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
572 GNUNET_DATASTORE_cancel (pr->qe);
577 GNUNET_DHT_get_stop (pr->gh);
580 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
582 GNUNET_SCHEDULER_cancel (pr->warn_task);
583 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
585 GNUNET_assert (GNUNET_OK ==
586 GNUNET_CONTAINER_multihashmap_remove (pr_map,
587 &pr->public_data.query,
589 GNUNET_STATISTICS_update (GSF_stats,
590 gettext_noop ("# Pending requests active"), -1,
598 * Explicitly cancel a pending request.
600 * @param pr request to cancel
601 * @param full_cleanup fully purge the request
604 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
606 GSF_LocalLookupContinuation cont;
609 return; /* already cleaned up! */
610 if (GNUNET_YES != full_cleanup)
612 /* make request inactive (we're no longer interested in more results),
613 * but do NOT remove from our data-structures, we still need it there
614 * to prevent the request from looping */
616 if (NULL != (cont = pr->llc_cont))
619 cont (pr->llc_cont_cls, pr, pr->local_result);
621 GSF_plan_notify_request_done_ (pr);
624 GNUNET_DATASTORE_cancel (pr->qe);
629 GNUNET_DHT_get_stop (pr->gh);
632 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
634 GNUNET_SCHEDULER_cancel (pr->warn_task);
635 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
639 GNUNET_assert (GNUNET_YES ==
640 clean_request (NULL, &pr->public_data.query, pr));
645 * Iterate over all pending requests.
647 * @param it function to call for each request
648 * @param cls closure for it
651 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
653 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
654 (GNUNET_CONTAINER_HashMapIterator) it,
662 * Closure for "process_reply" function.
664 struct ProcessReplyClosure
667 * The data for the reply.
672 * Who gave us this reply? NULL for local host (or DHT)
674 struct GSF_ConnectedPeer *sender;
677 * When the reply expires.
679 struct GNUNET_TIME_Absolute expiration;
689 enum GNUNET_BLOCK_Type type;
692 * How much was this reply worth to us?
697 * Anonymity requirements for this reply.
699 uint32_t anonymity_level;
702 * Evaluation result (returned).
704 enum GNUNET_BLOCK_EvaluationResult eval;
707 * Did we find a matching request?
714 * Update the performance data for the sender (if any) since
715 * the sender successfully answered one of our queries.
717 * @param prq information about the sender
718 * @param pr request that was satisfied
721 update_request_performance_data (struct ProcessReplyClosure *prq,
722 struct GSF_PendingRequest *pr)
724 if (prq->sender == NULL)
726 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
732 * We have received a reply; handle it!
734 * @param cls response (struct ProcessReplyClosure)
735 * @param key our query
736 * @param value value in the hash map (info about the query)
737 * @return GNUNET_YES (we should continue to iterate)
740 process_reply (void *cls, const GNUNET_HashCode * key, void *value)
742 struct ProcessReplyClosure *prq = cls;
743 struct GSF_PendingRequest *pr = value;
744 GNUNET_HashCode chash;
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
750 "Matched result (type %u) for query `%s' with pending request\n",
751 (unsigned int) prq->type, GNUNET_h2s (key));
753 GNUNET_STATISTICS_update (GSF_stats,
754 gettext_noop ("# replies received and matched"), 1,
757 GNUNET_BLOCK_evaluate (GSF_block_ctx, prq->type, key, &pr->bf, pr->mingle,
758 &pr->public_data.namespace,
760 GNUNET_BLOCK_TYPE_FS_SBLOCK) ?
761 sizeof (GNUNET_HashCode) : 0, prq->data,
765 case GNUNET_BLOCK_EVALUATION_OK_MORE:
766 update_request_performance_data (prq, pr);
768 case GNUNET_BLOCK_EVALUATION_OK_LAST:
769 /* short cut: stop processing early, no BF-update, etc. */
770 update_request_performance_data (prq, pr);
771 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
772 GNUNET_TIME_absolute_get_duration (pr->
773 public_data.start_time).rel_value);
774 /* pass on to other peers / local clients */
775 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
776 prq->type, prq->data, prq->size);
778 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
779 GNUNET_STATISTICS_update (GSF_stats,
781 ("# duplicate replies discarded (bloomfilter)"),
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Duplicate response `%s', discarding.\n", GNUNET_h2s (&mhash));
787 return GNUNET_YES; /* duplicate */
788 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
789 return GNUNET_YES; /* wrong namespace */
790 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
793 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
796 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported block type %u\n"),
801 /* update bloomfilter */
802 GNUNET_CRYPTO_hash (prq->data, prq->size, &chash);
803 GSF_pending_request_update_ (pr, &chash, 1);
804 if (NULL == prq->sender)
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Found result for query `%s' in local datastore\n",
811 GNUNET_STATISTICS_update (GSF_stats,
812 gettext_noop ("# results found locally"), 1,
817 GSF_dht_lookup_ (pr);
819 prq->priority += pr->public_data.original_priority;
820 pr->public_data.priority = 0;
821 pr->public_data.original_priority = 0;
822 pr->public_data.results_found++;
823 prq->request_found = GNUNET_YES;
824 /* finally, pass on to other peer / local client */
825 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
826 prq->type, prq->data, prq->size);
832 * Context for the 'put_migration_continuation'.
834 struct PutMigrationContext
838 * Start time for the operation.
840 struct GNUNET_TIME_Absolute start;
845 struct GNUNET_PeerIdentity origin;
848 * GNUNET_YES if we had a matching request for this block,
856 * Continuation called to notify client about result of the
860 * @param success GNUNET_SYSERR on failure
861 * @param msg NULL on success, otherwise an error message
864 put_migration_continuation (void *cls, int success, const char *msg)
866 struct PutMigrationContext *pmc = cls;
867 struct GNUNET_TIME_Relative delay;
868 struct GNUNET_TIME_Relative block_time;
869 struct GSF_ConnectedPeer *cp;
870 struct GSF_PeerPerformanceData *ppd;
872 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
873 cp = GSF_peer_get_ (&pmc->origin);
874 if ((GNUNET_OK != success) && (GNUNET_NO == pmc->requested))
876 /* block migration for a bit... */
879 ppd = GSF_get_peer_performance_data_ (cp);
880 ppd->migration_duplication++;
882 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
883 5 * ppd->migration_duplication +
884 GNUNET_CRYPTO_random_u32
885 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
886 GSF_block_peer_migration_ (cp, block_time);
893 ppd = GSF_get_peer_performance_data_ (cp);
894 ppd->migration_duplication = 0; /* reset counter */
898 /* FIXME: should we really update the load value on failure? */
899 if (NULL != datastore_put_load)
900 GNUNET_LOAD_update (datastore_put_load, delay.rel_value);
901 if (GNUNET_OK == success)
903 GNUNET_STATISTICS_update (GSF_stats,
904 gettext_noop ("# Datastore `PUT' failures"), 1,
910 * Test if the DATABASE (PUT) load on this peer is too high
911 * to even consider processing the query at
914 * @return GNUNET_YES if the load is too high to do anything (load high)
915 * GNUNET_NO to process normally (load normal or low)
918 test_put_load_too_high (uint32_t priority)
922 if (NULL == datastore_put_load)
924 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
925 return GNUNET_NO; /* very fast */
926 ld = GNUNET_LOAD_get_load (datastore_put_load);
927 if (ld < 2.0 * (1 + priority))
929 GNUNET_STATISTICS_update (GSF_stats,
931 ("# storage requests dropped due to high load"), 1,
938 * Iterator called on each result obtained for a DHT
939 * operation that expects a reply
942 * @param exp when will this value expire
943 * @param key key of the result
944 * @param get_path peers on reply path (or NULL if not recorded)
945 * @param get_path_length number of entries in get_path
946 * @param put_path peers on the PUT path (or NULL if not recorded)
947 * @param put_path_length number of entries in get_path
948 * @param type type of the result
949 * @param size number of bytes in data
950 * @param data pointer to the result data
953 handle_dht_reply (void *cls, struct GNUNET_TIME_Absolute exp,
954 const GNUNET_HashCode * key,
955 const struct GNUNET_PeerIdentity *get_path,
956 unsigned int get_path_length,
957 const struct GNUNET_PeerIdentity *put_path,
958 unsigned int put_path_length,
959 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
961 struct GSF_PendingRequest *pr = cls;
962 struct ProcessReplyClosure prq;
963 struct PutMigrationContext *pmc;
965 GNUNET_STATISTICS_update (GSF_stats,
966 gettext_noop ("# Replies received from DHT"), 1,
968 memset (&prq, 0, sizeof (prq));
970 prq.expiration = exp;
973 process_reply (&prq, key, pr);
974 if ((GNUNET_YES == active_to_migration) &&
975 (GNUNET_NO == test_put_load_too_high (prq.priority)))
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Replicating result for query `%s' with priority %u\n",
980 GNUNET_h2s (key), prq.priority);
982 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
983 pmc->start = GNUNET_TIME_absolute_get ();
984 pmc->requested = GNUNET_YES;
986 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
988 0 /* replication */ ,
989 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
990 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
991 &put_migration_continuation, pmc))
993 put_migration_continuation (pmc, GNUNET_NO, NULL);
1000 * Consider looking up the data in the DHT (anonymity-level permitting).
1002 * @param pr the pending request to process
1005 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1009 struct GNUNET_PeerIdentity pi;
1010 char buf[sizeof (GNUNET_HashCode) * 2];
1012 if (0 != pr->public_data.anonymity_level)
1016 GNUNET_DHT_get_stop (pr->gh);
1021 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1024 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
1025 xquery_size = sizeof (GNUNET_HashCode);
1027 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1029 GNUNET_assert (0 != pr->sender_pid);
1030 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1031 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1032 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1035 GNUNET_DHT_get_start (GSF_dht, GNUNET_TIME_UNIT_FOREVER_REL,
1036 pr->public_data.type, &pr->public_data.query,
1037 5 /* DEFAULT_GET_REPLICATION */,
1038 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1039 /* FIXME: can no longer pass pr->bf/pr->mingle... */
1040 xquery, xquery_size, &handle_dht_reply,
1046 * Task that issues a warning if the datastore lookup takes too long.
1048 * @param cls the 'struct GSF_PendingRequest'
1049 * @param tc task context
1052 warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1054 struct GSF_PendingRequest *pr = cls;
1056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1057 _("Datastore lookup already took %llu ms!\n"),
1058 (unsigned long long)
1059 GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1061 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1067 * Task that issues a warning if the datastore lookup takes too long.
1069 * @param cls the 'struct GSF_PendingRequest'
1070 * @param tc task context
1073 odc_warn_delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1075 struct GSF_PendingRequest *pr = cls;
1077 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1078 _("On-demand lookup already took %llu ms!\n"),
1079 (unsigned long long)
1080 GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1082 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1083 &odc_warn_delay_task, pr);
1088 * We're processing (local) results for a search request
1089 * from another peer. Pass applicable results to the
1090 * peer and if we are done either clean up (operation
1091 * complete) or forward to other peers (more results possible).
1093 * @param cls our closure (struct PendingRequest)
1094 * @param key key for the content
1095 * @param size number of bytes in data
1096 * @param data content stored
1097 * @param type type of the content
1098 * @param priority priority of the content
1099 * @param anonymity anonymity-level for the content
1100 * @param expiration expiration time for the content
1101 * @param uid unique identifier for the datum;
1102 * maybe 0 if no unique identifier is available
1105 process_local_reply (void *cls, const GNUNET_HashCode * key, size_t size,
1106 const void *data, enum GNUNET_BLOCK_Type type,
1107 uint32_t priority, uint32_t anonymity,
1108 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1110 struct GSF_PendingRequest *pr = cls;
1111 GSF_LocalLookupContinuation cont;
1112 struct ProcessReplyClosure prq;
1113 GNUNET_HashCode query;
1114 unsigned int old_rf;
1116 GNUNET_SCHEDULER_cancel (pr->warn_task);
1117 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1123 GNUNET_STATISTICS_update (GSF_stats,
1125 ("# Datastore lookups concluded (no results)"),
1128 if (GNUNET_NO == pr->have_first_uid)
1130 pr->first_uid = uid;
1131 pr->have_first_uid = 1;
1135 if ((uid == pr->first_uid) && (key != NULL))
1137 GNUNET_STATISTICS_update (GSF_stats,
1139 ("# Datastore lookups concluded (seen all)"),
1141 key = NULL; /* all replies seen! */
1143 pr->have_first_uid++;
1144 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1146 GNUNET_STATISTICS_update (GSF_stats,
1148 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1150 key = NULL; /* all replies seen! */
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "No further local responses available.\n");
1160 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1161 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1162 GNUNET_STATISTICS_update (GSF_stats,
1164 ("# requested DBLOCK or IBLOCK not found"), 1,
1166 goto check_error_and_continue;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1171 GNUNET_h2s (key), type, (unsigned long long) uid);
1173 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Found ONDEMAND block, performing on-demand encoding\n");
1179 GNUNET_STATISTICS_update (GSF_stats,
1181 ("# on-demand blocks matched requests"), 1,
1183 pr->qe_start = GNUNET_TIME_absolute_get ();
1185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1186 &odc_warn_delay_task, pr);
1188 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1189 anonymity, expiration, uid,
1190 &process_local_reply, pr))
1192 GNUNET_STATISTICS_update (GSF_stats,
1194 ("# on-demand lookups performed successfully"),
1196 return; /* we're done */
1198 GNUNET_STATISTICS_update (GSF_stats,
1199 gettext_noop ("# on-demand lookups failed"), 1,
1201 GNUNET_SCHEDULER_cancel (pr->warn_task);
1203 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1204 &warn_delay_task, pr);
1206 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1207 &pr->public_data.query,
1208 pr->public_data.type ==
1209 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1210 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1212 (GSF_PRO_PRIORITY_UNLIMITED &
1213 pr->public_data.options)) ? UINT_MAX : 1
1214 /* queue priority */ ,
1216 (GSF_PRO_PRIORITY_UNLIMITED &
1217 pr->public_data.options)) ? UINT_MAX : 1
1218 /* max queue size */ ,
1219 GNUNET_TIME_UNIT_FOREVER_REL,
1220 &process_local_reply, pr);
1223 GNUNET_STATISTICS_update (GSF_stats,
1225 ("# Datastore lookups concluded (error queueing)"),
1227 return; /* we're done */
1229 goto check_error_and_continue;
1231 old_rf = pr->public_data.results_found;
1232 memset (&prq, 0, sizeof (prq));
1234 prq.expiration = expiration;
1237 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1240 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1241 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
1242 pr->qe_start = GNUNET_TIME_absolute_get ();
1244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1245 &warn_delay_task, pr);
1247 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1248 &pr->public_data.query,
1249 pr->public_data.type ==
1250 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1251 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1253 (GSF_PRO_PRIORITY_UNLIMITED &
1254 pr->public_data.options)) ? UINT_MAX : 1
1255 /* queue priority */ ,
1257 (GSF_PRO_PRIORITY_UNLIMITED &
1258 pr->public_data.options)) ? UINT_MAX : 1
1259 /* max queue size */ ,
1260 GNUNET_TIME_UNIT_FOREVER_REL,
1261 &process_local_reply, pr);
1264 GNUNET_STATISTICS_update (GSF_stats,
1266 ("# Datastore lookups concluded (error queueing)"),
1268 goto check_error_and_continue;
1273 prq.priority = priority;
1274 prq.request_found = GNUNET_NO;
1275 prq.anonymity_level = anonymity;
1276 if ((old_rf == 0) && (pr->public_data.results_found == 0))
1277 GSF_update_datastore_delay_ (pr->public_data.start_time);
1278 process_reply (&prq, key, pr);
1279 pr->local_result = prq.eval;
1280 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1282 GNUNET_STATISTICS_update (GSF_stats,
1284 ("# Datastore lookups concluded (found ultimate result)"),
1286 goto check_error_and_continue;
1288 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1289 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1290 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1295 GNUNET_STATISTICS_update (GSF_stats,
1297 ("# Datastore lookups concluded (load too high)"),
1299 goto check_error_and_continue;
1301 pr->qe_start = GNUNET_TIME_absolute_get ();
1303 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1306 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1307 &pr->public_data.query,
1308 pr->public_data.type ==
1309 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1310 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1312 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1313 public_data.options)) ? UINT_MAX : 1
1314 /* queue priority */ ,
1316 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1317 public_data.options)) ? UINT_MAX : 1
1318 /* max queue size */ ,
1319 GNUNET_TIME_UNIT_FOREVER_REL,
1320 &process_local_reply, pr);
1321 /* check if we successfully queued another datastore request;
1322 * if so, return, otherwise call our continuation (if we have
1324 check_error_and_continue:
1327 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1329 GNUNET_SCHEDULER_cancel (pr->warn_task);
1330 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1332 if (NULL == (cont = pr->llc_cont))
1333 return; /* no continuation */
1334 pr->llc_cont = NULL;
1335 cont (pr->llc_cont_cls, pr, pr->local_result);
1340 * Look up the request in the local datastore.
1342 * @param pr the pending request to process
1343 * @param cont function to call at the end
1344 * @param cont_cls closure for cont
1347 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1348 GSF_LocalLookupContinuation cont, void *cont_cls)
1350 GNUNET_assert (NULL == pr->gh);
1351 GNUNET_assert (NULL == pr->llc_cont);
1352 pr->llc_cont = cont;
1353 pr->llc_cont_cls = cont_cls;
1354 pr->qe_start = GNUNET_TIME_absolute_get ();
1356 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &warn_delay_task,
1358 GNUNET_STATISTICS_update (GSF_stats,
1359 gettext_noop ("# Datastore lookups initiated"), 1,
1362 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1363 &pr->public_data.query,
1364 pr->public_data.type ==
1365 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1366 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1368 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1369 public_data.options)) ? UINT_MAX : 1
1370 /* queue priority */ ,
1372 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1373 public_data.options)) ? UINT_MAX : 1
1374 /* max queue size */ ,
1375 GNUNET_TIME_UNIT_FOREVER_REL,
1376 &process_local_reply, pr);
1379 GNUNET_STATISTICS_update (GSF_stats,
1381 ("# Datastore lookups concluded (error queueing)"),
1386 GNUNET_SCHEDULER_cancel (pr->warn_task);
1387 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1388 pr->llc_cont = NULL;
1390 cont (cont_cls, pr, pr->local_result);
1396 * Handle P2P "CONTENT" message. Checks that the message is
1397 * well-formed and then checks if there are any pending requests for
1398 * this content and possibly passes it on (to local clients or other
1399 * peers). Does NOT perform migration (content caching at this peer).
1401 * @param cp the other peer involved (sender or receiver, NULL
1402 * for loopback messages where we are both sender and receiver)
1403 * @param message the actual message
1404 * @return GNUNET_OK if the message was well-formed,
1405 * GNUNET_SYSERR if the message was malformed (close connection,
1406 * do not cache under any circumstances)
1409 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1410 const struct GNUNET_MessageHeader *message)
1412 const struct PutMessage *put;
1415 enum GNUNET_BLOCK_Type type;
1416 struct GNUNET_TIME_Absolute expiration;
1417 GNUNET_HashCode query;
1418 struct ProcessReplyClosure prq;
1419 struct GNUNET_TIME_Relative block_time;
1421 struct PutMigrationContext *pmc;
1423 msize = ntohs (message->size);
1424 if (msize < sizeof (struct PutMessage))
1426 GNUNET_break_op (0);
1427 return GNUNET_SYSERR;
1429 put = (const struct PutMessage *) message;
1430 dsize = msize - sizeof (struct PutMessage);
1431 type = ntohl (put->type);
1432 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1433 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1434 return GNUNET_SYSERR;
1436 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &put[1], dsize, &query))
1438 GNUNET_break_op (0);
1439 return GNUNET_SYSERR;
1441 GNUNET_STATISTICS_update (GSF_stats,
1442 gettext_noop ("# GAP PUT messages received"), 1,
1444 /* now, lookup 'query' */
1445 prq.data = (const void *) &put[1];
1452 prq.expiration = expiration;
1454 prq.anonymity_level = UINT32_MAX;
1455 prq.request_found = GNUNET_NO;
1456 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map, &query, &process_reply,
1460 GSF_connected_peer_change_preference_ (cp,
1461 CONTENT_BANDWIDTH_VALUE +
1462 1000 * prq.priority);
1463 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1465 if ((GNUNET_YES == active_to_migration) &&
1466 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Replicating result for query `%s' with priority %u\n",
1471 GNUNET_h2s (&query), prq.priority);
1473 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1474 pmc->start = GNUNET_TIME_absolute_get ();
1475 pmc->requested = prq.request_found;
1476 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1477 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1480 GNUNET_DATASTORE_put (GSF_dsh, 0, &query, dsize, &put[1], type,
1481 prq.priority, 1 /* anonymity */ ,
1482 0 /* replication */ ,
1483 expiration, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1484 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1485 &put_migration_continuation, pmc))
1487 put_migration_continuation (pmc, GNUNET_NO, NULL);
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "Choosing not to keep content `%s' (%d/%d)\n",
1495 GNUNET_h2s (&query), active_to_migration,
1496 test_put_load_too_high (prq.priority));
1499 putl = GNUNET_LOAD_get_load (datastore_put_load);
1500 if ((NULL != (cp = prq.sender)) && (GNUNET_NO == prq.request_found) &&
1501 ((GNUNET_YES != active_to_migration) ||
1502 (putl > 2.5 * (1 + prq.priority))))
1504 if (GNUNET_YES != active_to_migration)
1505 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1507 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1509 GNUNET_CRYPTO_random_u32
1510 (GNUNET_CRYPTO_QUALITY_WEAK,
1511 (unsigned int) (60000 * putl * putl)));
1512 GSF_block_peer_migration_ (cp, block_time);
1519 * Setup the subsystem.
1522 GSF_pending_request_init_ ()
1525 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1526 "MAX_PENDING_REQUESTS",
1527 &max_pending_requests))
1529 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1531 ("Configuration fails to specify `%s', assuming default value."),
1532 "MAX_PENDING_REQUESTS");
1534 active_to_migration =
1535 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1536 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1537 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1538 requests_by_expiration_heap =
1539 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1544 * Shutdown the subsystem.
1547 GSF_pending_request_done_ ()
1549 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1550 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1552 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1553 requests_by_expiration_heap = NULL;
1554 GNUNET_LOAD_value_free (datastore_put_load);
1555 datastore_put_load = NULL;
1559 /* end of gnunet-service-fs_pr.c */