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"
38 struct GSF_PendingRequest
41 * Public data for the request.
43 struct GSF_PendingRequestData public_data;
46 * Function to call if we encounter a reply.
48 GSF_PendingRequestReplyHandler rh;
56 * Array of hash codes of replies we've already seen.
58 GNUNET_HashCode *replies_seen;
61 * Bloomfilter masking replies we've already seen.
63 struct GNUNET_CONTAINER_BloomFilter *bf;
66 * Entry for this pending request in the expiration heap, or NULL.
68 struct GNUNET_CONTAINER_HeapNode *hnode;
71 * Datastore queue entry for this request (or NULL for none).
73 struct GNUNET_DATASTORE_QueueEntry *qe;
76 * DHT request handle for this request (or NULL for none).
78 struct GNUNET_DHT_GetHandle *gh;
81 * Function to call upon completion of the local get
82 * request, or NULL for none.
84 GSF_LocalLookupContinuation llc_cont;
87 * Closure for llc_cont.
92 * Last result from the local datastore lookup evaluation.
94 enum GNUNET_BLOCK_EvaluationResult local_result;
97 * Identity of the peer that we should use for the 'sender'
98 * (recipient of the response) when forwarding (0 for none).
100 GNUNET_PEER_Id sender_pid;
103 * Time we started the last datastore lookup.
105 struct GNUNET_TIME_Absolute qe_start;
108 * Task that warns us if the local datastore lookup takes too long.
110 GNUNET_SCHEDULER_TaskIdentifier warn_task;
113 * Current offset for querying our local datastore for results.
114 * Starts at a random value, incremented until we get the same
115 * UID again (detected using 'first_uid'), which is then used
116 * to termiante the iteration.
118 uint64_t local_result_offset;
121 * Unique ID of the first result from the local datastore;
122 * used to detect wrap-around of the offset.
127 * Number of valid entries in the 'replies_seen' array.
129 unsigned int replies_seen_count;
132 * Length of the 'replies_seen' array.
134 unsigned int replies_seen_size;
137 * Mingle value we currently use for the bf.
145 * All pending requests, ordered by the query. Entries
146 * are of type 'struct GSF_PendingRequest*'.
148 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
152 * Datastore 'PUT' load tracking.
154 static struct GNUNET_LOAD_Value *datastore_put_load;
158 * Are we allowed to migrate content to this peer.
160 static int active_to_migration;
164 * Heap with the request that will expire next at the top. Contains
165 * pointers of type "struct PendingRequest*"; these will *also* be
166 * aliased from the "requests_by_peer" data structures and the
167 * "requests_by_query" table. Note that requests from our clients
168 * don't expire and are thus NOT in the "requests_by_expiration"
169 * (or the "requests_by_peer" tables).
171 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
175 * Maximum number of requests (from other peers, overall) that we're
176 * willing to have pending at any given point in time. Can be changed
177 * via the configuration file (32k is just the default).
179 static unsigned long long max_pending_requests = (32 * 1024);
183 * How many bytes should a bloomfilter be if we have already seen
184 * entry_count responses? Note that BLOOMFILTER_K gives us the number
185 * of bits set per entry. Furthermore, we should not re-size the
186 * filter too often (to keep it cheap).
188 * Since other peers will also add entries but not resize the filter,
189 * we should generally pick a slightly larger size than what the
190 * strict math would suggest.
192 * @return must be a power of two and smaller or equal to 2^15.
195 compute_bloomfilter_size (unsigned int entry_count)
198 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
199 uint16_t max = 1 << 15;
201 if (entry_count > max)
204 while ((size < max) && (size < ideal))
213 * Recalculate our bloom filter for filtering replies. This function
214 * will create a new bloom filter from scratch, so it should only be
215 * called if we have no bloomfilter at all (and hence can create a
216 * fresh one of minimal size without problems) OR if our peer is the
217 * initiator (in which case we may resize to larger than mimimum size).
219 * @param pr request for which the BF is to be recomputed
220 * @return GNUNET_YES if a refresh actually happened
223 refresh_bloomfilter (struct GSF_PendingRequest *pr)
227 GNUNET_HashCode mhash;
229 nsize = compute_bloomfilter_size (pr->replies_seen_count);
230 if ( (pr->bf != NULL) &&
231 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
232 return GNUNET_NO; /* size not changed */
234 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
235 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
237 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
240 for (i=0;i<pr->replies_seen_count;i++)
242 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
245 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
252 * Create a new pending request.
254 * @param options request options
255 * @param type type of the block that is being requested
256 * @param query key for the lookup
257 * @param namespace namespace to lookup, NULL for no namespace
258 * @param target preferred target for the request, NULL for none
259 * @param bf_data raw data for bloom filter for known replies, can be NULL
260 * @param bf_size number of bytes in bf_data
261 * @param mingle mingle value for bf
262 * @param anonymity_level desired anonymity level
263 * @param priority maximum outgoing cummulative request priority to use
264 * @param ttl current time-to-live for the request
265 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
266 * @param replies_seen hash codes of known local replies
267 * @param replies_seen_count size of the 'replies_seen' array
268 * @param rh handle to call when we get a reply
269 * @param rh_cls closure for rh
270 * @return handle for the new pending request
272 struct GSF_PendingRequest *
273 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
274 enum GNUNET_BLOCK_Type type,
275 const GNUNET_HashCode *query,
276 const GNUNET_HashCode *namespace,
277 const struct GNUNET_PeerIdentity *target,
281 uint32_t anonymity_level,
284 GNUNET_PEER_Id sender_pid,
285 const GNUNET_HashCode *replies_seen,
286 unsigned int replies_seen_count,
287 GSF_PendingRequestReplyHandler rh,
290 struct GSF_PendingRequest *pr;
291 struct GSF_PendingRequest *dpr;
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Creating request handle for `%s' of type %d\n",
299 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
300 pr->local_result_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
302 pr->public_data.query = *query;
303 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
305 GNUNET_assert (NULL != namespace);
306 pr->public_data.namespace = *namespace;
310 pr->public_data.target = *target;
311 pr->public_data.has_target = GNUNET_YES;
313 pr->public_data.anonymity_level = anonymity_level;
314 pr->public_data.priority = priority;
315 pr->public_data.original_priority = priority;
316 pr->public_data.options = options;
317 pr->public_data.type = type;
318 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
319 pr->sender_pid = sender_pid;
323 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
326 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
327 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
328 (uint32_t) (- ttl)));
329 if (replies_seen_count > 0)
331 pr->replies_seen_size = replies_seen_count;
332 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
333 memcpy (pr->replies_seen,
335 replies_seen_count * sizeof (GNUNET_HashCode));
336 pr->replies_seen_count = replies_seen_count;
340 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
345 else if ( (replies_seen_count > 0) &&
346 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
348 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
350 GNUNET_CONTAINER_multihashmap_put (pr_map,
353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
354 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
356 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
358 pr->public_data.ttl.abs_value);
359 /* make sure we don't track too many requests */
360 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
362 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
363 GNUNET_assert (dpr != NULL);
365 break; /* let the request live briefly... */
366 dpr->rh (dpr->rh_cls,
367 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
369 GNUNET_TIME_UNIT_FOREVER_ABS,
370 GNUNET_BLOCK_TYPE_ANY,
372 GSF_pending_request_cancel_ (dpr);
380 * Obtain the public data associated with a pending request
382 * @param pr pending request
383 * @return associated public data
385 struct GSF_PendingRequestData *
386 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
388 return &pr->public_data;
393 * Update a given pending request with additional replies
394 * that have been seen.
396 * @param pr request to update
397 * @param replies_seen hash codes of replies that we've seen
398 * @param replies_seen_count size of the replies_seen array
401 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
402 const GNUNET_HashCode *replies_seen,
403 unsigned int replies_seen_count)
406 GNUNET_HashCode mhash;
408 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
409 return; /* integer overflow */
410 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
412 /* we're responsible for the BF, full refresh */
413 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
414 GNUNET_array_grow (pr->replies_seen,
415 pr->replies_seen_size,
416 replies_seen_count + pr->replies_seen_count);
417 memcpy (&pr->replies_seen[pr->replies_seen_count],
419 sizeof (GNUNET_HashCode) * replies_seen_count);
420 pr->replies_seen_count += replies_seen_count;
421 if (GNUNET_NO == refresh_bloomfilter (pr))
423 /* bf not recalculated, simply extend it with new bits */
424 for (i=0;i<pr->replies_seen_count;i++)
426 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
429 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
437 /* we're not the initiator, but the initiator did not give us
438 any bloom-filter, so we need to create one on-the-fly */
439 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
441 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
442 compute_bloomfilter_size (replies_seen_count),
445 for (i=0;i<pr->replies_seen_count;i++)
447 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
450 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
457 * Generate the message corresponding to the given pending request for
458 * transmission to other peers (or at least determine its size).
460 * @param pr request to generate the message for
461 * @param buf_size number of bytes available in buf
462 * @param buf where to copy the message (can be NULL)
463 * @return number of bytes needed (if > buf_size) or used
466 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
470 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
471 struct GetMessage *gm;
472 GNUNET_HashCode *ext;
478 struct GNUNET_TIME_Absolute now;
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Building request message for `%s' of type %d\n",
486 GNUNET_h2s (&pr->public_data.query),
487 pr->public_data.type);
491 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
494 bm |= GET_MESSAGE_BIT_RETURN_TO;
497 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
499 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
502 if (GNUNET_YES == pr->public_data.has_target)
504 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
507 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
508 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
509 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
510 if (buf_size < msize)
512 gm = (struct GetMessage*) lbuf;
513 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
514 gm->header.size = htons (msize);
515 gm->type = htonl (pr->public_data.type);
517 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
518 pr->public_data.priority + 1);
521 pr->public_data.priority -= prio;
522 gm->priority = htonl (prio);
523 now = GNUNET_TIME_absolute_get ();
524 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
525 gm->ttl = htonl (ttl / 1000);
526 gm->filter_mutator = htonl(pr->mingle);
527 gm->hash_bitmap = htonl (bm);
528 gm->query = pr->public_data.query;
529 ext = (GNUNET_HashCode*) &gm[1];
532 GNUNET_PEER_resolve (pr->sender_pid,
533 (struct GNUNET_PeerIdentity*) &ext[k++]);
534 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
536 &pr->public_data.namespace,
537 sizeof (GNUNET_HashCode));
538 if (GNUNET_YES == pr->public_data.has_target)
539 GNUNET_PEER_resolve (pr->sender_pid,
540 (struct GNUNET_PeerIdentity*) &ext[k++]);
542 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
545 memcpy (buf, gm, msize);
551 * Iterator to free pending requests.
553 * @param cls closure, unused
554 * @param key current key code
555 * @param value value in the hash map (pending request)
556 * @return GNUNET_YES (we should continue to iterate)
559 clean_request (void *cls,
560 const GNUNET_HashCode * key,
563 struct GSF_PendingRequest *pr = value;
564 GSF_LocalLookupContinuation cont;
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Cleaning up pending request for `%s'.\n",
571 if (NULL != (cont = pr->llc_cont))
574 cont (pr->llc_cont_cls,
578 GSF_plan_notify_request_done_ (pr);
579 GNUNET_free_non_null (pr->replies_seen);
582 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
585 GNUNET_PEER_change_rc (pr->sender_pid, -1);
586 if (NULL != pr->hnode)
588 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
593 GNUNET_DATASTORE_cancel (pr->qe);
598 GNUNET_DHT_get_stop (pr->gh);
601 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
603 GNUNET_SCHEDULER_cancel (pr->warn_task);
604 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
612 * Explicitly cancel a pending request.
614 * @param pr request to cancel
617 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
620 return; /* already cleaned up! */
621 GNUNET_assert (GNUNET_OK ==
622 GNUNET_CONTAINER_multihashmap_remove (pr_map,
623 &pr->public_data.query,
625 GNUNET_assert (GNUNET_YES ==
626 clean_request (NULL, &pr->public_data.query, pr));
631 * Iterate over all pending requests.
633 * @param it function to call for each request
634 * @param cls closure for it
637 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
640 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
641 (GNUNET_CONTAINER_HashMapIterator) it,
649 * Closure for "process_reply" function.
651 struct ProcessReplyClosure
654 * The data for the reply.
659 * Who gave us this reply? NULL for local host (or DHT)
661 struct GSF_ConnectedPeer *sender;
664 * When the reply expires.
666 struct GNUNET_TIME_Absolute expiration;
676 enum GNUNET_BLOCK_Type type;
679 * How much was this reply worth to us?
684 * Anonymity requirements for this reply.
686 uint32_t anonymity_level;
689 * Evaluation result (returned).
691 enum GNUNET_BLOCK_EvaluationResult eval;
694 * Did we find a matching request?
701 * Update the performance data for the sender (if any) since
702 * the sender successfully answered one of our queries.
704 * @param prq information about the sender
705 * @param pr request that was satisfied
708 update_request_performance_data (struct ProcessReplyClosure *prq,
709 struct GSF_PendingRequest *pr)
711 if (prq->sender == NULL)
713 GSF_peer_update_performance_ (prq->sender,
714 pr->public_data.start_time,
720 * We have received a reply; handle it!
722 * @param cls response (struct ProcessReplyClosure)
723 * @param key our query
724 * @param value value in the hash map (info about the query)
725 * @return GNUNET_YES (we should continue to iterate)
728 process_reply (void *cls,
729 const GNUNET_HashCode * key,
732 struct ProcessReplyClosure *prq = cls;
733 struct GSF_PendingRequest *pr = value;
734 GNUNET_HashCode chash;
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Matched result (type %u) for query `%s' with pending request\n",
739 (unsigned int) prq->type,
742 GNUNET_STATISTICS_update (GSF_stats,
743 gettext_noop ("# replies received and matched"),
746 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
751 &pr->public_data.namespace,
752 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
757 case GNUNET_BLOCK_EVALUATION_OK_MORE:
758 update_request_performance_data (prq, pr);
760 case GNUNET_BLOCK_EVALUATION_OK_LAST:
761 /* short cut: stop processing early, no BF-update, etc. */
762 update_request_performance_data (prq, pr);
763 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
764 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
765 /* pass on to other peers / local clients */
771 prq->data, prq->size);
773 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
774 GNUNET_STATISTICS_update (GSF_stats,
775 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Duplicate response `%s', discarding.\n",
781 GNUNET_h2s (&mhash));
783 return GNUNET_YES; /* duplicate */
784 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
785 return GNUNET_YES; /* wrong namespace */
786 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
789 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
792 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
794 _("Unsupported block type %u\n"),
798 /* update bloomfilter */
799 GNUNET_CRYPTO_hash (prq->data,
802 GSF_pending_request_update_ (pr, &chash, 1);
803 if (NULL == prq->sender)
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Found result for query `%s' in local datastore\n",
810 GNUNET_STATISTICS_update (GSF_stats,
811 gettext_noop ("# results found locally"),
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 */
830 prq->data, prq->size);
836 * Context for the 'put_migration_continuation'.
838 struct PutMigrationContext
842 * Start time for the operation.
844 struct GNUNET_TIME_Absolute start;
849 struct GNUNET_PeerIdentity origin;
852 * GNUNET_YES if we had a matching request for this block,
860 * Continuation called to notify client about result of the
864 * @param success GNUNET_SYSERR on failure
865 * @param msg NULL on success, otherwise an error message
868 put_migration_continuation (void *cls,
872 struct PutMigrationContext *pmc = cls;
873 struct GNUNET_TIME_Relative delay;
874 struct GNUNET_TIME_Relative block_time;
875 struct GSF_ConnectedPeer *cp;
876 struct GSF_PeerPerformanceData *ppd;
878 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
879 cp = GSF_peer_get_ (&pmc->origin);
880 if ( (GNUNET_OK != success) &&
881 (GNUNET_NO == pmc->requested) )
883 /* block migration for a bit... */
886 ppd = GSF_get_peer_performance_data_ (cp);
887 ppd->migration_duplication++;
888 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
889 5 * ppd->migration_duplication +
890 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
891 GSF_block_peer_migration_ (cp, block_time);
898 ppd = GSF_get_peer_performance_data_ (cp);
899 ppd->migration_duplication = 0; /* reset counter */
903 /* FIXME: should we really update the load value on failure? */
904 GNUNET_LOAD_update (datastore_put_load,
906 if (GNUNET_OK == success)
908 GNUNET_STATISTICS_update (GSF_stats,
909 gettext_noop ("# datastore 'put' failures"),
916 * Test if the DATABASE (PUT) load on this peer is too high
917 * to even consider processing the query at
920 * @return GNUNET_YES if the load is too high to do anything (load high)
921 * GNUNET_NO to process normally (load normal or low)
924 test_put_load_too_high (uint32_t priority)
928 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
929 return GNUNET_NO; /* very fast */
930 ld = GNUNET_LOAD_get_load (datastore_put_load);
931 if (ld < 2.0 * (1 + priority))
933 GNUNET_STATISTICS_update (GSF_stats,
934 gettext_noop ("# storage requests dropped due to high load"),
942 * Iterator called on each result obtained for a DHT
943 * operation that expects a reply
946 * @param exp when will this value expire
947 * @param key key of the result
948 * @param get_path NULL-terminated array of pointers
949 * to the peers on reverse GET path (or NULL if not recorded)
950 * @param put_path NULL-terminated array of pointers
951 * to the peers on the PUT path (or NULL if not recorded)
952 * @param type type of the result
953 * @param size number of bytes in data
954 * @param data pointer to the result data
957 handle_dht_reply (void *cls,
958 struct GNUNET_TIME_Absolute exp,
959 const GNUNET_HashCode *key,
960 const struct GNUNET_PeerIdentity * const *get_path,
961 const struct GNUNET_PeerIdentity * const *put_path,
962 enum GNUNET_BLOCK_Type type,
966 struct GSF_PendingRequest *pr = cls;
967 struct ProcessReplyClosure prq;
968 struct PutMigrationContext *pmc;
970 memset (&prq, 0, sizeof (prq));
972 prq.expiration = exp;
975 process_reply (&prq, key, pr);
976 if ( (GNUNET_YES == active_to_migration) &&
977 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Replicating result for query `%s' with priority %u\n",
985 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
986 pmc->start = GNUNET_TIME_absolute_get ();
987 pmc->requested = GNUNET_YES;
989 GNUNET_DATASTORE_put (GSF_dsh,
991 type, prq.priority, 1 /* anonymity */,
994 1 + prq.priority, MAX_DATASTORE_QUEUE,
995 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
996 &put_migration_continuation,
999 put_migration_continuation (pmc, GNUNET_NO, NULL);
1006 * Consider looking up the data in the DHT (anonymity-level permitting).
1008 * @param pr the pending request to process
1011 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1015 struct GNUNET_PeerIdentity pi;
1016 char buf[sizeof (GNUNET_HashCode) * 2];
1018 if (0 != pr->public_data.anonymity_level)
1022 GNUNET_DHT_get_stop (pr->gh);
1027 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
1030 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
1031 xquery_size = sizeof (GNUNET_HashCode);
1033 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1035 GNUNET_PEER_resolve (pr->sender_pid,
1037 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1038 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1040 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1041 GNUNET_TIME_UNIT_FOREVER_REL,
1042 pr->public_data.type,
1043 &pr->public_data.query,
1044 DEFAULT_GET_REPLICATION,
1045 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1056 * Task that issues a warning if the datastore lookup takes too long.
1058 * @param cls the 'struct GSF_PendingRequest'
1059 * @param tc task context
1062 warn_delay_task (void *cls,
1063 const struct GNUNET_SCHEDULER_TaskContext *tc)
1065 struct GSF_PendingRequest *pr = cls;
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1068 _("Datastore lookup already took %llu ms!\n"),
1069 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1070 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1077 * Task that issues a warning if the datastore lookup takes too long.
1079 * @param cls the 'struct GSF_PendingRequest'
1080 * @param tc task context
1083 odc_warn_delay_task (void *cls,
1084 const struct GNUNET_SCHEDULER_TaskContext *tc)
1086 struct GSF_PendingRequest *pr = cls;
1088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1089 _("On-demand lookup already took %llu ms!\n"),
1090 (unsigned long long) GNUNET_TIME_absolute_get_duration (pr->qe_start).rel_value);
1091 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1092 &odc_warn_delay_task,
1098 * We're processing (local) results for a search request
1099 * from another peer. Pass applicable results to the
1100 * peer and if we are done either clean up (operation
1101 * complete) or forward to other peers (more results possible).
1103 * @param cls our closure (struct PendingRequest)
1104 * @param key key for the content
1105 * @param size number of bytes in data
1106 * @param data content stored
1107 * @param type type of the content
1108 * @param priority priority of the content
1109 * @param anonymity anonymity-level for the content
1110 * @param expiration expiration time for the content
1111 * @param uid unique identifier for the datum;
1112 * maybe 0 if no unique identifier is available
1115 process_local_reply (void *cls,
1116 const GNUNET_HashCode *key,
1119 enum GNUNET_BLOCK_Type type,
1122 struct GNUNET_TIME_Absolute expiration,
1125 struct GSF_PendingRequest *pr = cls;
1126 GSF_LocalLookupContinuation cont;
1127 struct ProcessReplyClosure prq;
1128 GNUNET_HashCode query;
1129 unsigned int old_rf;
1132 GNUNET_SCHEDULER_cancel (pr->warn_task);
1133 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1134 if (0 == pr->replies_seen_count)
1136 pr->first_uid = uid;
1140 if (uid == pr->first_uid)
1141 key = NULL; /* all replies seen! */
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "No further local responses available.\n");
1149 if ( (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1150 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK) )
1151 GNUNET_STATISTICS_update (GSF_stats,
1152 gettext_noop ("# requested DBLOCK or IBLOCK not found"),
1155 goto check_error_and_continue;
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1162 (unsigned long long) uid);
1164 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1168 "Found ONDEMAND block, performing on-demand encoding\n");
1170 GNUNET_STATISTICS_update (GSF_stats,
1171 gettext_noop ("# on-demand blocks matched requests"),
1174 pr->qe_start = GNUNET_TIME_absolute_get ();
1175 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1176 &odc_warn_delay_task,
1179 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1180 anonymity, expiration, uid,
1181 &process_local_reply,
1184 GNUNET_STATISTICS_update (GSF_stats,
1185 gettext_noop ("# on-demand lookups performed successfully"),
1188 return; /* we're done */
1190 GNUNET_STATISTICS_update (GSF_stats,
1191 gettext_noop ("# on-demand lookups failed"),
1194 GNUNET_SCHEDULER_cancel (pr->warn_task);
1195 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1198 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1199 pr->local_result_offset - 1,
1200 &pr->public_data.query,
1201 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1202 ? GNUNET_BLOCK_TYPE_ANY
1203 : pr->public_data.type,
1204 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1206 : 1 /* queue priority */,
1207 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1209 : 1 /* max queue size */,
1210 GNUNET_TIME_UNIT_FOREVER_REL,
1211 &process_local_reply,
1214 return; /* we're done */
1215 goto check_error_and_continue;
1217 old_rf = pr->public_data.results_found;
1218 memset (&prq, 0, sizeof (prq));
1220 prq.expiration = expiration;
1223 GNUNET_BLOCK_get_key (GSF_block_ctx,
1230 GNUNET_DATASTORE_remove (GSF_dsh,
1234 GNUNET_TIME_UNIT_FOREVER_REL,
1236 pr->qe_start = GNUNET_TIME_absolute_get ();
1237 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1240 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1241 pr->local_result_offset - 1,
1242 &pr->public_data.query,
1243 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1244 ? GNUNET_BLOCK_TYPE_ANY
1245 : pr->public_data.type,
1246 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1248 : 1 /* queue priority */,
1249 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1251 : 1 /* max queue size */,
1252 GNUNET_TIME_UNIT_FOREVER_REL,
1253 &process_local_reply,
1256 goto check_error_and_continue;
1260 prq.priority = priority;
1261 prq.request_found = GNUNET_NO;
1262 prq.anonymity_level = anonymity;
1263 if ( (old_rf == 0) &&
1264 (pr->public_data.results_found == 0) )
1265 GSF_update_datastore_delay_ (pr->public_data.start_time);
1266 process_reply (&prq, key, pr);
1267 pr->local_result = prq.eval;
1268 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1269 goto check_error_and_continue;
1270 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1271 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1272 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Load too high, done with request\n");
1278 GNUNET_STATISTICS_update (GSF_stats,
1279 gettext_noop ("# processing result set cut short due to load"),
1282 goto check_error_and_continue;
1284 pr->qe_start = GNUNET_TIME_absolute_get ();
1285 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1288 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1289 pr->local_result_offset++,
1290 &pr->public_data.query,
1291 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1292 ? GNUNET_BLOCK_TYPE_ANY
1293 : pr->public_data.type,
1294 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1296 : 1 /* queue priority */,
1297 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1299 : 1 /* max queue size */,
1300 GNUNET_TIME_UNIT_FOREVER_REL,
1301 &process_local_reply,
1303 /* check if we successfully queued another datastore request;
1304 if so, return, otherwise call our continuation (if we have
1306 check_error_and_continue:
1309 if (GNUNET_SCHEDULER_NO_TASK != pr->warn_task)
1311 GNUNET_SCHEDULER_cancel (pr->warn_task);
1312 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1314 if (NULL == (cont = pr->llc_cont))
1315 return; /* no continuation */
1316 pr->llc_cont = NULL;
1317 cont (pr->llc_cont_cls,
1324 * Look up the request in the local datastore.
1326 * @param pr the pending request to process
1327 * @param cont function to call at the end
1328 * @param cont_cls closure for cont
1331 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1332 GSF_LocalLookupContinuation cont,
1335 GNUNET_assert (NULL == pr->gh);
1336 GNUNET_assert (NULL == pr->llc_cont);
1337 pr->llc_cont = cont;
1338 pr->llc_cont_cls = cont_cls;
1339 pr->qe_start = GNUNET_TIME_absolute_get ();
1340 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1343 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1344 pr->local_result_offset++,
1345 &pr->public_data.query,
1346 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1347 ? GNUNET_BLOCK_TYPE_ANY
1348 : pr->public_data.type,
1349 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1351 : 1 /* queue priority */,
1352 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1354 : 1 /* max queue size */,
1355 GNUNET_TIME_UNIT_FOREVER_REL,
1356 &process_local_reply,
1360 GNUNET_SCHEDULER_cancel (pr->warn_task);
1361 pr->warn_task = GNUNET_SCHEDULER_NO_TASK;
1362 pr->llc_cont = NULL;
1364 cont (cont_cls, pr, pr->local_result);
1370 * Handle P2P "CONTENT" message. Checks that the message is
1371 * well-formed and then checks if there are any pending requests for
1372 * this content and possibly passes it on (to local clients or other
1373 * peers). Does NOT perform migration (content caching at this peer).
1375 * @param cp the other peer involved (sender or receiver, NULL
1376 * for loopback messages where we are both sender and receiver)
1377 * @param message the actual message
1378 * @return GNUNET_OK if the message was well-formed,
1379 * GNUNET_SYSERR if the message was malformed (close connection,
1380 * do not cache under any circumstances)
1383 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1384 const struct GNUNET_MessageHeader *message)
1386 const struct PutMessage *put;
1389 enum GNUNET_BLOCK_Type type;
1390 struct GNUNET_TIME_Absolute expiration;
1391 GNUNET_HashCode query;
1392 struct ProcessReplyClosure prq;
1393 struct GNUNET_TIME_Relative block_time;
1395 struct PutMigrationContext *pmc;
1397 msize = ntohs (message->size);
1398 if (msize < sizeof (struct PutMessage))
1401 return GNUNET_SYSERR;
1403 put = (const struct PutMessage*) message;
1404 dsize = msize - sizeof (struct PutMessage);
1405 type = ntohl (put->type);
1406 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1407 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1408 return GNUNET_SYSERR;
1410 GNUNET_BLOCK_get_key (GSF_block_ctx,
1416 GNUNET_break_op (0);
1417 return GNUNET_SYSERR;
1419 /* now, lookup 'query' */
1420 prq.data = (const void*) &put[1];
1427 prq.expiration = expiration;
1429 prq.anonymity_level = 1;
1430 prq.request_found = GNUNET_NO;
1431 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1437 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1438 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1440 if ( (GNUNET_YES == active_to_migration) &&
1441 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "Replicating result for query `%s' with priority %u\n",
1446 GNUNET_h2s (&query),
1449 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1450 pmc->start = GNUNET_TIME_absolute_get ();
1451 pmc->requested = prq.request_found;
1452 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1455 GNUNET_DATASTORE_put (GSF_dsh,
1456 0, &query, dsize, &put[1],
1457 type, prq.priority, 1 /* anonymity */,
1458 0 /* replication */,
1460 1 + prq.priority, MAX_DATASTORE_QUEUE,
1461 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1462 &put_migration_continuation,
1465 put_migration_continuation (pmc, GNUNET_NO, NULL);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Choosing not to keep content `%s' (%d/%d)\n",
1473 GNUNET_h2s (&query),
1474 active_to_migration,
1475 test_put_load_too_high (prq.priority));
1478 putl = GNUNET_LOAD_get_load (datastore_put_load);
1479 if ( (NULL != (cp = prq.sender)) &&
1480 (GNUNET_NO == prq.request_found) &&
1481 ( (GNUNET_YES != active_to_migration) ||
1482 (putl > 2.5 * (1 + prq.priority)) ) )
1484 if (GNUNET_YES != active_to_migration)
1485 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1486 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1487 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1488 (unsigned int) (60000 * putl * putl)));
1489 GSF_block_peer_migration_ (cp, block_time);
1496 * Setup the subsystem.
1499 GSF_pending_request_init_ ()
1502 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1504 "MAX_PENDING_REQUESTS",
1505 &max_pending_requests))
1507 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1508 _("Configuration fails to specify `%s', assuming default value."),
1509 "MAX_PENDING_REQUESTS");
1511 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1514 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1515 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1516 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1521 * Shutdown the subsystem.
1524 GSF_pending_request_done_ ()
1526 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1529 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1531 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1532 requests_by_expiration_heap = NULL;
1533 GNUNET_LOAD_value_free (datastore_put_load);
1534 datastore_put_load = NULL;
1538 /* end of gnunet-service-fs_pr.c */