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_cp.h"
29 #include "gnunet-service-fs_indexing.h"
30 #include "gnunet-service-fs_pr.h"
36 struct GSF_PendingRequest
39 * Public data for the request.
41 struct GSF_PendingRequestData public_data;
44 * Function to call if we encounter a reply.
46 GSF_PendingRequestReplyHandler rh;
54 * Array of hash codes of replies we've already seen.
56 GNUNET_HashCode *replies_seen;
59 * Bloomfilter masking replies we've already seen.
61 struct GNUNET_CONTAINER_BloomFilter *bf;
64 * Entry for this pending request in the expiration heap, or NULL.
66 struct GNUNET_CONTAINER_HeapNode *hnode;
69 * Datastore queue entry for this request (or NULL for none).
71 struct GNUNET_DATASTORE_QueueEntry *qe;
74 * DHT request handle for this request (or NULL for none).
76 struct GNUNET_DHT_GetHandle *gh;
79 * Function to call upon completion of the local get
80 * request, or NULL for none.
82 GSF_LocalLookupContinuation llc_cont;
85 * Closure for llc_cont.
90 * Last result from the local datastore lookup evaluation.
92 enum GNUNET_BLOCK_EvaluationResult local_result;
95 * Identity of the peer that we should use for the 'sender'
96 * (recipient of the response) when forwarding (0 for none).
98 GNUNET_PEER_Id sender_pid;
101 * Number of valid entries in the 'replies_seen' array.
103 unsigned int replies_seen_count;
106 * Length of the 'replies_seen' array.
108 unsigned int replies_seen_size;
111 * Mingle value we currently use for the bf.
119 * All pending requests, ordered by the query. Entries
120 * are of type 'struct GSF_PendingRequest*'.
122 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
126 * Datastore 'PUT' load tracking.
128 static struct GNUNET_LOAD_Value *datastore_put_load;
132 * Are we allowed to migrate content to this peer.
134 static int active_to_migration;
138 * Heap with the request that will expire next at the top. Contains
139 * pointers of type "struct PendingRequest*"; these will *also* be
140 * aliased from the "requests_by_peer" data structures and the
141 * "requests_by_query" table. Note that requests from our clients
142 * don't expire and are thus NOT in the "requests_by_expiration"
143 * (or the "requests_by_peer" tables).
145 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
149 * Maximum number of requests (from other peers, overall) that we're
150 * willing to have pending at any given point in time. Can be changed
151 * via the configuration file (32k is just the default).
153 static unsigned long long max_pending_requests = (32 * 1024);
157 * How many bytes should a bloomfilter be if we have already seen
158 * entry_count responses? Note that BLOOMFILTER_K gives us the number
159 * of bits set per entry. Furthermore, we should not re-size the
160 * filter too often (to keep it cheap).
162 * Since other peers will also add entries but not resize the filter,
163 * we should generally pick a slightly larger size than what the
164 * strict math would suggest.
166 * @return must be a power of two and smaller or equal to 2^15.
169 compute_bloomfilter_size (unsigned int entry_count)
172 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
173 uint16_t max = 1 << 15;
175 if (entry_count > max)
178 while ((size < max) && (size < ideal))
187 * Recalculate our bloom filter for filtering replies. This function
188 * will create a new bloom filter from scratch, so it should only be
189 * called if we have no bloomfilter at all (and hence can create a
190 * fresh one of minimal size without problems) OR if our peer is the
191 * initiator (in which case we may resize to larger than mimimum size).
193 * @param pr request for which the BF is to be recomputed
194 * @return GNUNET_YES if a refresh actually happened
197 refresh_bloomfilter (struct GSF_PendingRequest *pr)
201 GNUNET_HashCode mhash;
203 nsize = compute_bloomfilter_size (pr->replies_seen_count);
204 if ( (pr->bf != NULL) &&
205 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
206 return GNUNET_NO; /* size not changed */
208 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
209 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
211 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
214 for (i=0;i<pr->replies_seen_count;i++)
216 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
219 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
226 * Create a new pending request.
228 * @param options request options
229 * @param type type of the block that is being requested
230 * @param query key for the lookup
231 * @param namespace namespace to lookup, NULL for no namespace
232 * @param target preferred target for the request, NULL for none
233 * @param bf_data raw data for bloom filter for known replies, can be NULL
234 * @param bf_size number of bytes in bf_data
235 * @param mingle mingle value for bf
236 * @param anonymity_level desired anonymity level
237 * @param priority maximum outgoing cummulative request priority to use
238 * @param ttl current time-to-live for the request
239 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
240 * @param replies_seen hash codes of known local replies
241 * @param replies_seen_count size of the 'replies_seen' array
242 * @param rh handle to call when we get a reply
243 * @param rh_cls closure for rh
244 * @return handle for the new pending request
246 struct GSF_PendingRequest *
247 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
248 enum GNUNET_BLOCK_Type type,
249 const GNUNET_HashCode *query,
250 const GNUNET_HashCode *namespace,
251 const struct GNUNET_PeerIdentity *target,
255 uint32_t anonymity_level,
258 GNUNET_PEER_Id sender_pid,
259 const GNUNET_HashCode *replies_seen,
260 unsigned int replies_seen_count,
261 GSF_PendingRequestReplyHandler rh,
264 struct GSF_PendingRequest *pr;
265 struct GSF_PendingRequest *dpr;
267 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
268 pr->public_data.query = *query;
269 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
271 GNUNET_assert (NULL != namespace);
272 pr->public_data.namespace = *namespace;
276 pr->public_data.target = *target;
277 pr->public_data.has_target = GNUNET_YES;
279 pr->public_data.anonymity_level = anonymity_level;
280 pr->public_data.priority = priority;
281 pr->public_data.original_priority = priority;
282 pr->public_data.options = options;
283 pr->public_data.type = type;
284 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
285 pr->sender_pid = sender_pid;
289 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
292 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
293 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
294 (uint32_t) (- ttl)));
295 if (replies_seen_count > 0)
297 pr->replies_seen_size = replies_seen_count;
298 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
299 memcpy (pr->replies_seen,
301 replies_seen_count * sizeof (GNUNET_HashCode));
302 pr->replies_seen_count = replies_seen_count;
306 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
311 else if ( (replies_seen_count > 0) &&
312 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
314 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
316 GNUNET_CONTAINER_multihashmap_put (pr_map,
319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
320 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
322 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
324 pr->public_data.ttl.abs_value);
325 /* make sure we don't track too many requests */
326 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
328 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
329 GNUNET_assert (dpr != NULL);
331 break; /* let the request live briefly... */
332 dpr->rh (dpr->rh_cls,
334 GNUNET_TIME_UNIT_FOREVER_ABS,
337 GSF_pending_request_cancel_ (dpr);
345 * Obtain the public data associated with a pending request
347 * @param pr pending request
348 * @return associated public data
350 struct GSF_PendingRequestData *
351 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
353 return &pr->public_data;
358 * Update a given pending request with additional replies
359 * that have been seen.
361 * @param pr request to update
362 * @param replies_seen hash codes of replies that we've seen
363 * @param replies_seen_count size of the replies_seen array
366 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
367 const GNUNET_HashCode *replies_seen,
368 unsigned int replies_seen_count)
371 GNUNET_HashCode mhash;
373 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
374 return; /* integer overflow */
375 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
377 /* we're responsible for the BF, full refresh */
378 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
379 GNUNET_array_grow (pr->replies_seen,
380 pr->replies_seen_size,
381 replies_seen_count + pr->replies_seen_count);
382 memcpy (&pr->replies_seen[pr->replies_seen_count],
384 sizeof (GNUNET_HashCode) * replies_seen_count);
385 pr->replies_seen_count += replies_seen_count;
386 if (GNUNET_NO == refresh_bloomfilter (pr))
388 /* bf not recalculated, simply extend it with new bits */
389 for (i=0;i<pr->replies_seen_count;i++)
391 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
394 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
402 /* we're not the initiator, but the initiator did not give us
403 any bloom-filter, so we need to create one on-the-fly */
404 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
406 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
407 compute_bloomfilter_size (replies_seen_count),
410 for (i=0;i<pr->replies_seen_count;i++)
412 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
415 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
422 * Generate the message corresponding to the given pending request for
423 * transmission to other peers (or at least determine its size).
425 * @param pr request to generate the message for
426 * @param buf_size number of bytes available in buf
427 * @param buf where to copy the message (can be NULL)
428 * @return number of bytes needed (if > buf_size) or used
431 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
435 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
436 struct GetMessage *gm;
437 GNUNET_HashCode *ext;
443 struct GNUNET_TIME_Absolute now;
450 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
453 bm |= GET_MESSAGE_BIT_RETURN_TO;
456 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
458 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
461 if (GNUNET_YES == pr->public_data.has_target)
463 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
466 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
467 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
468 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
469 if (buf_size < msize)
471 gm = (struct GetMessage*) lbuf;
472 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
473 gm->header.size = htons (msize);
474 gm->type = htonl (pr->public_data.type);
476 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
477 pr->public_data.priority + 1);
480 pr->public_data.priority -= prio;
481 gm->priority = htonl (prio);
482 now = GNUNET_TIME_absolute_get ();
483 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
484 gm->ttl = htonl (ttl / 1000);
485 gm->filter_mutator = htonl(pr->mingle);
486 gm->hash_bitmap = htonl (bm);
487 gm->query = pr->public_data.query;
488 ext = (GNUNET_HashCode*) &gm[1];
491 GNUNET_PEER_resolve (pr->sender_pid,
492 (struct GNUNET_PeerIdentity*) &ext[k++]);
493 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
495 &pr->public_data.namespace,
496 sizeof (GNUNET_HashCode));
497 if (GNUNET_YES == pr->public_data.has_target)
498 GNUNET_PEER_resolve (pr->sender_pid,
499 (struct GNUNET_PeerIdentity*) &ext[k++]);
501 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
504 memcpy (buf, gm, msize);
510 * Iterator to free pending requests.
512 * @param cls closure, unused
513 * @param key current key code
514 * @param value value in the hash map (pending request)
515 * @return GNUNET_YES (we should continue to iterate)
518 clean_request (void *cls,
519 const GNUNET_HashCode * key,
522 struct GSF_PendingRequest *pr = value;
524 GNUNET_free_non_null (pr->replies_seen);
526 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
527 GNUNET_PEER_change_rc (pr->sender_pid, -1);
528 if (NULL != pr->hnode)
529 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
532 GNUNET_DATASTORE_cancel (pr->qe);
534 GNUNET_DHT_get_stop (pr->gh);
541 * Explicitly cancel a pending request.
543 * @param pr request to cancel
546 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
548 GNUNET_assert (GNUNET_OK ==
549 GNUNET_CONTAINER_multihashmap_remove (pr_map,
550 &pr->public_data.query,
552 GNUNET_assert (GNUNET_YES ==
553 clean_request (NULL, &pr->public_data.query, pr));
558 * Iterate over all pending requests.
560 * @param it function to call for each request
561 * @param cls closure for it
564 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
567 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
568 (GNUNET_CONTAINER_HashMapIterator) it,
576 * Closure for "process_reply" function.
578 struct ProcessReplyClosure
581 * The data for the reply.
586 * Who gave us this reply? NULL for local host (or DHT)
588 struct GSF_ConnectedPeer *sender;
591 * When the reply expires.
593 struct GNUNET_TIME_Absolute expiration;
603 enum GNUNET_BLOCK_Type type;
606 * How much was this reply worth to us?
611 * Anonymity requirements for this reply.
613 uint32_t anonymity_level;
616 * Evaluation result (returned).
618 enum GNUNET_BLOCK_EvaluationResult eval;
621 * Did we finish processing the associated request?
626 * Did we find a matching request?
633 * Update the performance data for the sender (if any) since
634 * the sender successfully answered one of our queries.
636 * @param prq information about the sender
637 * @param pr request that was satisfied
640 update_request_performance_data (struct ProcessReplyClosure *prq,
641 struct GSF_PendingRequest *pr)
643 if (prq->sender == NULL)
645 GSF_peer_update_performance_ (prq->sender,
646 pr->public_data.start_time,
652 * We have received a reply; handle it!
654 * @param cls response (struct ProcessReplyClosure)
655 * @param key our query
656 * @param value value in the hash map (info about the query)
657 * @return GNUNET_YES (we should continue to iterate)
660 process_reply (void *cls,
661 const GNUNET_HashCode * key,
664 struct ProcessReplyClosure *prq = cls;
665 struct GSF_PendingRequest *pr = value;
666 GNUNET_HashCode chash;
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Matched result (type %u) for query `%s' with pending request\n",
671 (unsigned int) prq->type,
674 GNUNET_STATISTICS_update (GSF_stats,
675 gettext_noop ("# replies received and matched"),
678 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
683 &pr->public_data.namespace,
684 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
689 case GNUNET_BLOCK_EVALUATION_OK_MORE:
690 update_request_performance_data (prq, pr);
692 case GNUNET_BLOCK_EVALUATION_OK_LAST:
693 /* short cut: stop processing early, no BF-update, etc. */
694 update_request_performance_data (prq, pr);
695 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
696 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
697 /* pass on to other peers / local clients */
701 prq->data, prq->size,
703 /* destroy request, we're done */
704 prq->finished = GNUNET_YES;
705 GSF_pending_request_cancel_ (pr);
707 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
708 GNUNET_STATISTICS_update (GSF_stats,
709 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Duplicate response `%s', discarding.\n",
715 GNUNET_h2s (&mhash));
717 return GNUNET_YES; /* duplicate */
718 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
719 return GNUNET_YES; /* wrong namespace */
720 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
723 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
726 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728 _("Unsupported block type %u\n"),
732 /* update bloomfilter */
733 GNUNET_CRYPTO_hash (prq->data,
736 GSF_pending_request_update_ (pr, &chash, 1);
737 if (NULL == prq->sender)
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Found result for query `%s' in local datastore\n",
744 GNUNET_STATISTICS_update (GSF_stats,
745 gettext_noop ("# results found locally"),
751 GSF_dht_lookup_ (pr);
753 prq->priority += pr->public_data.original_priority;
754 pr->public_data.priority = 0;
755 pr->public_data.original_priority = 0;
756 pr->public_data.results_found++;
757 prq->request_found = GNUNET_YES;
758 /* finally, pass on to other peer / local client */
762 prq->data, prq->size,
769 * Continuation called to notify client about result of the
773 * @param success GNUNET_SYSERR on failure
774 * @param msg NULL on success, otherwise an error message
777 put_migration_continuation (void *cls,
781 struct GNUNET_TIME_Absolute *start = cls;
782 struct GNUNET_TIME_Relative delay;
784 delay = GNUNET_TIME_absolute_get_duration (*start);
786 /* FIXME: should we really update the load value on failure? */
787 GNUNET_LOAD_update (datastore_put_load,
789 if (GNUNET_OK == success)
791 GNUNET_STATISTICS_update (GSF_stats,
792 gettext_noop ("# datastore 'put' failures"),
799 * Test if the DATABASE (PUT) load on this peer is too high
800 * to even consider processing the query at
803 * @return GNUNET_YES if the load is too high to do anything (load high)
804 * GNUNET_NO to process normally (load normal or low)
807 test_put_load_too_high (uint32_t priority)
811 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
812 return GNUNET_NO; /* very fast */
813 ld = GNUNET_LOAD_get_load (datastore_put_load);
814 if (ld < 2.0 * (1 + priority))
816 GNUNET_STATISTICS_update (GSF_stats,
817 gettext_noop ("# storage requests dropped due to high load"),
825 * Iterator called on each result obtained for a DHT
826 * operation that expects a reply
829 * @param exp when will this value expire
830 * @param key key of the result
831 * @param get_path NULL-terminated array of pointers
832 * to the peers on reverse GET path (or NULL if not recorded)
833 * @param put_path NULL-terminated array of pointers
834 * to the peers on the PUT path (or NULL if not recorded)
835 * @param type type of the result
836 * @param size number of bytes in data
837 * @param data pointer to the result data
840 handle_dht_reply (void *cls,
841 struct GNUNET_TIME_Absolute exp,
842 const GNUNET_HashCode *key,
843 const struct GNUNET_PeerIdentity * const *get_path,
844 const struct GNUNET_PeerIdentity * const *put_path,
845 enum GNUNET_BLOCK_Type type,
849 struct GSF_PendingRequest *pr = cls;
850 struct ProcessReplyClosure prq;
851 struct GNUNET_TIME_Absolute *start;
853 memset (&prq, 0, sizeof (prq));
855 prq.expiration = exp;
858 process_reply (&prq, key, pr);
859 if ( (GNUNET_YES == active_to_migration) &&
860 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Replicating result for query `%s' with priority %u\n",
868 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
869 *start = GNUNET_TIME_absolute_get ();
870 GNUNET_DATASTORE_put (GSF_dsh,
872 type, prq.priority, 1 /* anonymity */,
874 1 + prq.priority, MAX_DATASTORE_QUEUE,
875 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
876 &put_migration_continuation,
883 * Consider looking up the data in the DHT (anonymity-level permitting).
885 * @param pr the pending request to process
888 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
892 struct GNUNET_PeerIdentity pi;
893 char buf[sizeof (GNUNET_HashCode) * 2];
895 if (0 != pr->public_data.anonymity_level)
899 GNUNET_DHT_get_stop (pr->gh);
904 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
907 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
908 xquery_size = sizeof (GNUNET_HashCode);
910 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
912 GNUNET_PEER_resolve (pr->sender_pid,
914 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
915 xquery_size += sizeof (struct GNUNET_PeerIdentity);
917 pr->gh = GNUNET_DHT_get_start (GSF_dht,
918 GNUNET_TIME_UNIT_FOREVER_REL,
919 pr->public_data.type,
920 &pr->public_data.query,
921 DEFAULT_GET_REPLICATION,
922 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
932 * We're processing (local) results for a search request
933 * from another peer. Pass applicable results to the
934 * peer and if we are done either clean up (operation
935 * complete) or forward to other peers (more results possible).
937 * @param cls our closure (struct PendingRequest)
938 * @param key key for the content
939 * @param size number of bytes in data
940 * @param data content stored
941 * @param type type of the content
942 * @param priority priority of the content
943 * @param anonymity anonymity-level for the content
944 * @param expiration expiration time for the content
945 * @param uid unique identifier for the datum;
946 * maybe 0 if no unique identifier is available
949 process_local_reply (void *cls,
950 const GNUNET_HashCode * key,
953 enum GNUNET_BLOCK_Type type,
956 struct GNUNET_TIME_Absolute expiration,
959 struct GSF_PendingRequest *pr = cls;
960 GSF_LocalLookupContinuation cont;
962 struct ProcessReplyClosure prq;
963 GNUNET_HashCode query;
969 if (NULL != (cont = pr->llc_cont))
972 cont (pr->llc_cont_cls,
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "New local response to `%s' of type %u.\n",
984 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "Found ONDEMAND block, performing on-demand encoding\n");
990 GNUNET_STATISTICS_update (GSF_stats,
991 gettext_noop ("# on-demand blocks matched requests"),
995 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
996 anonymity, expiration, uid,
997 &process_local_reply,
1001 GNUNET_DATASTORE_get_next (GSF_dsh, GNUNET_YES);
1005 old_rf = pr->public_data.results_found;
1006 memset (&prq, 0, sizeof (prq));
1008 prq.expiration = expiration;
1011 GNUNET_BLOCK_get_key (GSF_block_ctx,
1018 GNUNET_DATASTORE_remove (GSF_dsh,
1022 GNUNET_TIME_UNIT_FOREVER_REL,
1024 GNUNET_DATASTORE_get_next (GSF_dsh, GNUNET_YES);
1028 prq.priority = priority;
1029 prq.finished = GNUNET_NO;
1030 prq.request_found = GNUNET_NO;
1031 prq.anonymity_level = anonymity;
1032 if ( (old_rf == 0) &&
1033 (pr->public_data.results_found == 0) )
1034 GSF_update_datastore_delay_ (pr->public_data.start_time);
1035 process_reply (&prq, key, pr);
1036 if (prq.finished == GNUNET_YES)
1038 pr->local_result = prq.eval;
1040 return; /* done here */
1041 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1043 GNUNET_DATASTORE_get_next (GSF_dsh, GNUNET_NO);
1046 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1047 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1048 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Load too high, done with request\n");
1054 GNUNET_STATISTICS_update (GSF_stats,
1055 gettext_noop ("# processing result set cut short due to load"),
1058 GNUNET_DATASTORE_get_next (GSF_dsh, GNUNET_NO);
1061 GNUNET_DATASTORE_get_next (GSF_dsh, GNUNET_YES);
1066 * Look up the request in the local datastore.
1068 * @param pr the pending request to process
1069 * @param cont function to call at the end
1070 * @param cont_cls closure for cont
1073 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1074 GSF_LocalLookupContinuation cont,
1077 GNUNET_assert (NULL == pr->gh);
1078 GNUNET_assert (NULL == pr->llc_cont);
1079 pr->llc_cont = cont;
1080 pr->llc_cont_cls = cont_cls;
1081 pr->qe = GNUNET_DATASTORE_get (GSF_dsh,
1082 &pr->public_data.query,
1083 pr->public_data.type,
1084 1 /* queue priority */,
1085 1 /* max queue size */,
1086 GNUNET_TIME_UNIT_FOREVER_REL,
1087 &process_local_reply,
1094 * Handle P2P "CONTENT" message. Checks that the message is
1095 * well-formed and then checks if there are any pending requests for
1096 * this content and possibly passes it on (to local clients or other
1097 * peers). Does NOT perform migration (content caching at this peer).
1099 * @param cp the other peer involved (sender or receiver, NULL
1100 * for loopback messages where we are both sender and receiver)
1101 * @param message the actual message
1102 * @return GNUNET_OK if the message was well-formed,
1103 * GNUNET_SYSERR if the message was malformed (close connection,
1104 * do not cache under any circumstances)
1107 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1108 const struct GNUNET_MessageHeader *message)
1110 const struct PutMessage *put;
1113 enum GNUNET_BLOCK_Type type;
1114 struct GNUNET_TIME_Absolute expiration;
1115 GNUNET_HashCode query;
1116 struct ProcessReplyClosure prq;
1117 struct GNUNET_TIME_Relative block_time;
1119 struct GNUNET_TIME_Absolute *start;
1121 msize = ntohs (message->size);
1122 if (msize < sizeof (struct PutMessage))
1125 return GNUNET_SYSERR;
1127 put = (const struct PutMessage*) message;
1128 dsize = msize - sizeof (struct PutMessage);
1129 type = ntohl (put->type);
1130 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1131 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1132 return GNUNET_SYSERR;
1134 GNUNET_BLOCK_get_key (GSF_block_ctx,
1140 GNUNET_break_op (0);
1141 return GNUNET_SYSERR;
1143 /* now, lookup 'query' */
1144 prq.data = (const void*) &put[1];
1151 prq.expiration = expiration;
1153 prq.anonymity_level = 1;
1154 prq.finished = GNUNET_NO;
1155 prq.request_found = GNUNET_NO;
1156 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1162 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1163 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1165 if ( (GNUNET_YES == active_to_migration) &&
1166 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Replicating result for query `%s' with priority %u\n",
1171 GNUNET_h2s (&query),
1174 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
1175 *start = GNUNET_TIME_absolute_get ();
1176 GNUNET_DATASTORE_put (GSF_dsh,
1177 0, &query, dsize, &put[1],
1178 type, prq.priority, 1 /* anonymity */,
1180 1 + prq.priority, MAX_DATASTORE_QUEUE,
1181 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1182 &put_migration_continuation,
1185 putl = GNUNET_LOAD_get_load (datastore_put_load);
1186 if ( (NULL != (cp = prq.sender)) &&
1187 (GNUNET_NO == prq.request_found) &&
1188 ( (GNUNET_YES != active_to_migration) ||
1189 (putl > 2.5 * (1 + prq.priority)) ) )
1191 if (GNUNET_YES != active_to_migration)
1192 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1193 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1194 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1195 (unsigned int) (60000 * putl * putl)));
1196 GSF_block_peer_migration_ (cp, block_time);
1203 * Setup the subsystem.
1206 GSF_pending_request_init_ ()
1209 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1211 "MAX_PENDING_REQUESTS",
1212 &max_pending_requests))
1214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1215 _("Configuration fails to specify `%s', assuming default value."),
1216 "MAX_PENDING_REQUESTS");
1218 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1219 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1224 * Shutdown the subsystem.
1227 GSF_pending_request_done_ ()
1229 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1232 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1234 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1235 requests_by_expiration_heap = NULL;
1239 /* end of gnunet-service-fs_pr.c */