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 * Number of valid entries in the 'replies_seen' array.
105 unsigned int replies_seen_count;
108 * Length of the 'replies_seen' array.
110 unsigned int replies_seen_size;
113 * Mingle value we currently use for the bf.
121 * All pending requests, ordered by the query. Entries
122 * are of type 'struct GSF_PendingRequest*'.
124 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
128 * Datastore 'PUT' load tracking.
130 static struct GNUNET_LOAD_Value *datastore_put_load;
134 * Are we allowed to migrate content to this peer.
136 static int active_to_migration;
140 * Heap with the request that will expire next at the top. Contains
141 * pointers of type "struct PendingRequest*"; these will *also* be
142 * aliased from the "requests_by_peer" data structures and the
143 * "requests_by_query" table. Note that requests from our clients
144 * don't expire and are thus NOT in the "requests_by_expiration"
145 * (or the "requests_by_peer" tables).
147 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
151 * Maximum number of requests (from other peers, overall) that we're
152 * willing to have pending at any given point in time. Can be changed
153 * via the configuration file (32k is just the default).
155 static unsigned long long max_pending_requests = (32 * 1024);
159 * How many bytes should a bloomfilter be if we have already seen
160 * entry_count responses? Note that BLOOMFILTER_K gives us the number
161 * of bits set per entry. Furthermore, we should not re-size the
162 * filter too often (to keep it cheap).
164 * Since other peers will also add entries but not resize the filter,
165 * we should generally pick a slightly larger size than what the
166 * strict math would suggest.
168 * @return must be a power of two and smaller or equal to 2^15.
171 compute_bloomfilter_size (unsigned int entry_count)
174 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
175 uint16_t max = 1 << 15;
177 if (entry_count > max)
180 while ((size < max) && (size < ideal))
189 * Recalculate our bloom filter for filtering replies. This function
190 * will create a new bloom filter from scratch, so it should only be
191 * called if we have no bloomfilter at all (and hence can create a
192 * fresh one of minimal size without problems) OR if our peer is the
193 * initiator (in which case we may resize to larger than mimimum size).
195 * @param pr request for which the BF is to be recomputed
196 * @return GNUNET_YES if a refresh actually happened
199 refresh_bloomfilter (struct GSF_PendingRequest *pr)
203 GNUNET_HashCode mhash;
205 nsize = compute_bloomfilter_size (pr->replies_seen_count);
206 if ( (pr->bf != NULL) &&
207 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
208 return GNUNET_NO; /* size not changed */
210 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
211 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
213 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
216 for (i=0;i<pr->replies_seen_count;i++)
218 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
221 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
228 * Create a new pending request.
230 * @param options request options
231 * @param type type of the block that is being requested
232 * @param query key for the lookup
233 * @param namespace namespace to lookup, NULL for no namespace
234 * @param target preferred target for the request, NULL for none
235 * @param bf_data raw data for bloom filter for known replies, can be NULL
236 * @param bf_size number of bytes in bf_data
237 * @param mingle mingle value for bf
238 * @param anonymity_level desired anonymity level
239 * @param priority maximum outgoing cummulative request priority to use
240 * @param ttl current time-to-live for the request
241 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
242 * @param replies_seen hash codes of known local replies
243 * @param replies_seen_count size of the 'replies_seen' array
244 * @param rh handle to call when we get a reply
245 * @param rh_cls closure for rh
246 * @return handle for the new pending request
248 struct GSF_PendingRequest *
249 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
250 enum GNUNET_BLOCK_Type type,
251 const GNUNET_HashCode *query,
252 const GNUNET_HashCode *namespace,
253 const struct GNUNET_PeerIdentity *target,
257 uint32_t anonymity_level,
260 GNUNET_PEER_Id sender_pid,
261 const GNUNET_HashCode *replies_seen,
262 unsigned int replies_seen_count,
263 GSF_PendingRequestReplyHandler rh,
266 struct GSF_PendingRequest *pr;
267 struct GSF_PendingRequest *dpr;
269 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
270 pr->public_data.query = *query;
271 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
273 GNUNET_assert (NULL != namespace);
274 pr->public_data.namespace = *namespace;
278 pr->public_data.target = *target;
279 pr->public_data.has_target = GNUNET_YES;
281 pr->public_data.anonymity_level = anonymity_level;
282 pr->public_data.priority = priority;
283 pr->public_data.original_priority = priority;
284 pr->public_data.options = options;
285 pr->public_data.type = type;
286 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
287 pr->sender_pid = sender_pid;
291 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
294 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
295 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
296 (uint32_t) (- ttl)));
297 if (replies_seen_count > 0)
299 pr->replies_seen_size = replies_seen_count;
300 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
301 memcpy (pr->replies_seen,
303 replies_seen_count * sizeof (GNUNET_HashCode));
304 pr->replies_seen_count = replies_seen_count;
308 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
313 else if ( (replies_seen_count > 0) &&
314 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
316 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
318 GNUNET_CONTAINER_multihashmap_put (pr_map,
321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
322 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
324 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
326 pr->public_data.ttl.abs_value);
327 /* make sure we don't track too many requests */
328 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
330 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
331 GNUNET_assert (dpr != NULL);
333 break; /* let the request live briefly... */
334 dpr->rh (dpr->rh_cls,
336 GNUNET_TIME_UNIT_FOREVER_ABS,
337 GNUNET_BLOCK_TYPE_ANY,
339 GSF_pending_request_cancel_ (dpr);
347 * Obtain the public data associated with a pending request
349 * @param pr pending request
350 * @return associated public data
352 struct GSF_PendingRequestData *
353 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
355 return &pr->public_data;
360 * Update a given pending request with additional replies
361 * that have been seen.
363 * @param pr request to update
364 * @param replies_seen hash codes of replies that we've seen
365 * @param replies_seen_count size of the replies_seen array
368 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
369 const GNUNET_HashCode *replies_seen,
370 unsigned int replies_seen_count)
373 GNUNET_HashCode mhash;
375 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
376 return; /* integer overflow */
377 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
379 /* we're responsible for the BF, full refresh */
380 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
381 GNUNET_array_grow (pr->replies_seen,
382 pr->replies_seen_size,
383 replies_seen_count + pr->replies_seen_count);
384 memcpy (&pr->replies_seen[pr->replies_seen_count],
386 sizeof (GNUNET_HashCode) * replies_seen_count);
387 pr->replies_seen_count += replies_seen_count;
388 if (GNUNET_NO == refresh_bloomfilter (pr))
390 /* bf not recalculated, simply extend it with new bits */
391 for (i=0;i<pr->replies_seen_count;i++)
393 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
396 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
404 /* we're not the initiator, but the initiator did not give us
405 any bloom-filter, so we need to create one on-the-fly */
406 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
408 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
409 compute_bloomfilter_size (replies_seen_count),
412 for (i=0;i<pr->replies_seen_count;i++)
414 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
417 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
424 * Generate the message corresponding to the given pending request for
425 * transmission to other peers (or at least determine its size).
427 * @param pr request to generate the message for
428 * @param buf_size number of bytes available in buf
429 * @param buf where to copy the message (can be NULL)
430 * @return number of bytes needed (if > buf_size) or used
433 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
437 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
438 struct GetMessage *gm;
439 GNUNET_HashCode *ext;
445 struct GNUNET_TIME_Absolute now;
452 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
455 bm |= GET_MESSAGE_BIT_RETURN_TO;
458 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
460 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
463 if (GNUNET_YES == pr->public_data.has_target)
465 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
468 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
469 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
470 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
471 if (buf_size < msize)
473 gm = (struct GetMessage*) lbuf;
474 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
475 gm->header.size = htons (msize);
476 gm->type = htonl (pr->public_data.type);
478 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
479 pr->public_data.priority + 1);
482 pr->public_data.priority -= prio;
483 gm->priority = htonl (prio);
484 now = GNUNET_TIME_absolute_get ();
485 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
486 gm->ttl = htonl (ttl / 1000);
487 gm->filter_mutator = htonl(pr->mingle);
488 gm->hash_bitmap = htonl (bm);
489 gm->query = pr->public_data.query;
490 ext = (GNUNET_HashCode*) &gm[1];
493 GNUNET_PEER_resolve (pr->sender_pid,
494 (struct GNUNET_PeerIdentity*) &ext[k++]);
495 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
497 &pr->public_data.namespace,
498 sizeof (GNUNET_HashCode));
499 if (GNUNET_YES == pr->public_data.has_target)
500 GNUNET_PEER_resolve (pr->sender_pid,
501 (struct GNUNET_PeerIdentity*) &ext[k++]);
503 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
506 memcpy (buf, gm, msize);
512 * Iterator to free pending requests.
514 * @param cls closure, unused
515 * @param key current key code
516 * @param value value in the hash map (pending request)
517 * @return GNUNET_YES (we should continue to iterate)
520 clean_request (void *cls,
521 const GNUNET_HashCode * key,
524 struct GSF_PendingRequest *pr = value;
526 GSF_plan_notify_request_done_ (pr);
527 GNUNET_free_non_null (pr->replies_seen);
529 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
530 GNUNET_PEER_change_rc (pr->sender_pid, -1);
531 if (NULL != pr->hnode)
532 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
534 GNUNET_DATASTORE_cancel (pr->qe);
536 GNUNET_DHT_get_stop (pr->gh);
543 * Explicitly cancel a pending request.
545 * @param pr request to cancel
548 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
550 GNUNET_assert (GNUNET_OK ==
551 GNUNET_CONTAINER_multihashmap_remove (pr_map,
552 &pr->public_data.query,
554 GNUNET_assert (GNUNET_YES ==
555 clean_request (NULL, &pr->public_data.query, pr));
560 * Iterate over all pending requests.
562 * @param it function to call for each request
563 * @param cls closure for it
566 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
569 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
570 (GNUNET_CONTAINER_HashMapIterator) it,
578 * Closure for "process_reply" function.
580 struct ProcessReplyClosure
583 * The data for the reply.
588 * Who gave us this reply? NULL for local host (or DHT)
590 struct GSF_ConnectedPeer *sender;
593 * When the reply expires.
595 struct GNUNET_TIME_Absolute expiration;
605 enum GNUNET_BLOCK_Type type;
608 * How much was this reply worth to us?
613 * Anonymity requirements for this reply.
615 uint32_t anonymity_level;
618 * Evaluation result (returned).
620 enum GNUNET_BLOCK_EvaluationResult eval;
623 * Did we find a matching request?
630 * Update the performance data for the sender (if any) since
631 * the sender successfully answered one of our queries.
633 * @param prq information about the sender
634 * @param pr request that was satisfied
637 update_request_performance_data (struct ProcessReplyClosure *prq,
638 struct GSF_PendingRequest *pr)
640 if (prq->sender == NULL)
642 GSF_peer_update_performance_ (prq->sender,
643 pr->public_data.start_time,
649 * We have received a reply; handle it!
651 * @param cls response (struct ProcessReplyClosure)
652 * @param key our query
653 * @param value value in the hash map (info about the query)
654 * @return GNUNET_YES (we should continue to iterate)
657 process_reply (void *cls,
658 const GNUNET_HashCode * key,
661 struct ProcessReplyClosure *prq = cls;
662 struct GSF_PendingRequest *pr = value;
663 GNUNET_HashCode chash;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Matched result (type %u) for query `%s' with pending request\n",
668 (unsigned int) prq->type,
671 GNUNET_STATISTICS_update (GSF_stats,
672 gettext_noop ("# replies received and matched"),
675 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
680 &pr->public_data.namespace,
681 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
686 case GNUNET_BLOCK_EVALUATION_OK_MORE:
687 update_request_performance_data (prq, pr);
689 case GNUNET_BLOCK_EVALUATION_OK_LAST:
690 /* short cut: stop processing early, no BF-update, etc. */
691 update_request_performance_data (prq, pr);
692 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
693 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
694 /* pass on to other peers / local clients */
699 prq->data, prq->size);
701 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
702 GNUNET_STATISTICS_update (GSF_stats,
703 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Duplicate response `%s', discarding.\n",
709 GNUNET_h2s (&mhash));
711 return GNUNET_YES; /* duplicate */
712 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
713 return GNUNET_YES; /* wrong namespace */
714 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
717 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
720 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
722 _("Unsupported block type %u\n"),
726 /* update bloomfilter */
727 GNUNET_CRYPTO_hash (prq->data,
730 GSF_pending_request_update_ (pr, &chash, 1);
731 if (NULL == prq->sender)
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Found result for query `%s' in local datastore\n",
738 GNUNET_STATISTICS_update (GSF_stats,
739 gettext_noop ("# results found locally"),
745 GSF_dht_lookup_ (pr);
747 prq->priority += pr->public_data.original_priority;
748 pr->public_data.priority = 0;
749 pr->public_data.original_priority = 0;
750 pr->public_data.results_found++;
751 prq->request_found = GNUNET_YES;
752 /* finally, pass on to other peer / local client */
757 prq->data, prq->size);
763 * Context for the 'put_migration_continuation'.
765 struct PutMigrationContext
769 * Start time for the operation.
771 struct GNUNET_TIME_Absolute start;
776 struct GNUNET_PeerIdentity origin;
779 * GNUNET_YES if we had a matching request for this block,
787 * Continuation called to notify client about result of the
791 * @param success GNUNET_SYSERR on failure
792 * @param msg NULL on success, otherwise an error message
795 put_migration_continuation (void *cls,
799 struct PutMigrationContext *pmc = cls;
800 struct GNUNET_TIME_Relative delay;
801 struct GNUNET_TIME_Relative block_time;
802 struct GSF_ConnectedPeer *cp;
803 struct GSF_PeerPerformanceData *ppd;
805 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
806 cp = GSF_peer_get_ (&pmc->origin);
807 if ( (GNUNET_OK != success) &&
808 (GNUNET_NO == pmc->requested) )
810 /* block migration for a bit... */
813 ppd = GSF_get_peer_performance_data_ (cp);
814 ppd->migration_duplication++;
815 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
816 5 * ppd->migration_duplication +
817 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
818 GSF_block_peer_migration_ (cp, block_time);
825 ppd = GSF_get_peer_performance_data_ (cp);
826 ppd->migration_duplication = 0; /* reset counter */
830 /* FIXME: should we really update the load value on failure? */
831 GNUNET_LOAD_update (datastore_put_load,
833 if (GNUNET_OK == success)
835 GNUNET_STATISTICS_update (GSF_stats,
836 gettext_noop ("# datastore 'put' failures"),
843 * Test if the DATABASE (PUT) load on this peer is too high
844 * to even consider processing the query at
847 * @return GNUNET_YES if the load is too high to do anything (load high)
848 * GNUNET_NO to process normally (load normal or low)
851 test_put_load_too_high (uint32_t priority)
855 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
856 return GNUNET_NO; /* very fast */
857 ld = GNUNET_LOAD_get_load (datastore_put_load);
858 if (ld < 2.0 * (1 + priority))
860 GNUNET_STATISTICS_update (GSF_stats,
861 gettext_noop ("# storage requests dropped due to high load"),
869 * Iterator called on each result obtained for a DHT
870 * operation that expects a reply
873 * @param exp when will this value expire
874 * @param key key of the result
875 * @param get_path NULL-terminated array of pointers
876 * to the peers on reverse GET path (or NULL if not recorded)
877 * @param put_path NULL-terminated array of pointers
878 * to the peers on the PUT path (or NULL if not recorded)
879 * @param type type of the result
880 * @param size number of bytes in data
881 * @param data pointer to the result data
884 handle_dht_reply (void *cls,
885 struct GNUNET_TIME_Absolute exp,
886 const GNUNET_HashCode *key,
887 const struct GNUNET_PeerIdentity * const *get_path,
888 const struct GNUNET_PeerIdentity * const *put_path,
889 enum GNUNET_BLOCK_Type type,
893 struct GSF_PendingRequest *pr = cls;
894 struct ProcessReplyClosure prq;
895 struct PutMigrationContext *pmc;
897 memset (&prq, 0, sizeof (prq));
899 prq.expiration = exp;
902 process_reply (&prq, key, pr);
903 if ( (GNUNET_YES == active_to_migration) &&
904 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Replicating result for query `%s' with priority %u\n",
912 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
913 pmc->start = GNUNET_TIME_absolute_get ();
914 pmc->requested = GNUNET_YES;
915 GNUNET_DATASTORE_put (GSF_dsh,
917 type, prq.priority, 1 /* anonymity */,
919 1 + prq.priority, MAX_DATASTORE_QUEUE,
920 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
921 &put_migration_continuation,
928 * Consider looking up the data in the DHT (anonymity-level permitting).
930 * @param pr the pending request to process
933 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
937 struct GNUNET_PeerIdentity pi;
938 char buf[sizeof (GNUNET_HashCode) * 2];
940 if (0 != pr->public_data.anonymity_level)
944 GNUNET_DHT_get_stop (pr->gh);
949 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
952 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
953 xquery_size = sizeof (GNUNET_HashCode);
955 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
957 GNUNET_PEER_resolve (pr->sender_pid,
959 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
960 xquery_size += sizeof (struct GNUNET_PeerIdentity);
962 pr->gh = GNUNET_DHT_get_start (GSF_dht,
963 GNUNET_TIME_UNIT_FOREVER_REL,
964 pr->public_data.type,
965 &pr->public_data.query,
966 DEFAULT_GET_REPLICATION,
967 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
977 * We're processing (local) results for a search request
978 * from another peer. Pass applicable results to the
979 * peer and if we are done either clean up (operation
980 * complete) or forward to other peers (more results possible).
982 * @param cls our closure (struct PendingRequest)
983 * @param key key for the content
984 * @param size number of bytes in data
985 * @param data content stored
986 * @param type type of the content
987 * @param priority priority of the content
988 * @param anonymity anonymity-level for the content
989 * @param expiration expiration time for the content
990 * @param uid unique identifier for the datum;
991 * maybe 0 if no unique identifier is available
994 process_local_reply (void *cls,
995 const GNUNET_HashCode * key,
998 enum GNUNET_BLOCK_Type type,
1001 struct GNUNET_TIME_Absolute expiration,
1004 struct GSF_PendingRequest *pr = cls;
1005 GSF_LocalLookupContinuation cont;
1006 struct ProcessReplyClosure prq;
1007 GNUNET_HashCode query;
1008 unsigned int old_rf;
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "No further local responses available.\n");
1017 if (NULL != (cont = pr->llc_cont))
1019 pr->llc_cont = NULL;
1020 cont (pr->llc_cont_cls,
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "New local response to `%s' of type %u.\n",
1032 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Found ONDEMAND block, performing on-demand encoding\n");
1038 GNUNET_STATISTICS_update (GSF_stats,
1039 gettext_noop ("# on-demand blocks matched requests"),
1043 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1044 anonymity, expiration, uid,
1045 &process_local_reply,
1049 GNUNET_DATASTORE_get_next (GSF_dsh);
1053 old_rf = pr->public_data.results_found;
1054 memset (&prq, 0, sizeof (prq));
1056 prq.expiration = expiration;
1059 GNUNET_BLOCK_get_key (GSF_block_ctx,
1066 GNUNET_DATASTORE_remove (GSF_dsh,
1070 GNUNET_TIME_UNIT_FOREVER_REL,
1072 GNUNET_DATASTORE_get_next (GSF_dsh);
1076 prq.priority = priority;
1077 prq.request_found = GNUNET_NO;
1078 prq.anonymity_level = anonymity;
1079 if ( (old_rf == 0) &&
1080 (pr->public_data.results_found == 0) )
1081 GSF_update_datastore_delay_ (pr->public_data.start_time);
1082 process_reply (&prq, key, pr);
1083 pr->local_result = prq.eval;
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Request cancelled, not asking datastore for more\n");
1091 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1092 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1093 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Load too high, done with request\n");
1099 GNUNET_STATISTICS_update (GSF_stats,
1100 gettext_noop ("# processing result set cut short due to load"),
1103 GNUNET_DATASTORE_cancel (pr->qe);
1105 if (NULL != (cont = pr->llc_cont))
1107 pr->llc_cont = NULL;
1108 cont (pr->llc_cont_cls,
1114 GNUNET_DATASTORE_get_next (GSF_dsh);
1119 * Look up the request in the local datastore.
1121 * @param pr the pending request to process
1122 * @param cont function to call at the end
1123 * @param cont_cls closure for cont
1126 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1127 GSF_LocalLookupContinuation cont,
1130 GNUNET_assert (NULL == pr->gh);
1131 GNUNET_assert (NULL == pr->llc_cont);
1132 pr->llc_cont = cont;
1133 pr->llc_cont_cls = cont_cls;
1134 pr->qe = GNUNET_DATASTORE_get (GSF_dsh,
1135 &pr->public_data.query,
1136 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1137 ? GNUNET_BLOCK_TYPE_ANY
1138 : pr->public_data.type,
1139 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1141 : 1 /* queue priority */,
1142 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1144 : 1 /* max queue size */,
1145 GNUNET_TIME_UNIT_FOREVER_REL,
1146 &process_local_reply,
1153 * Handle P2P "CONTENT" message. Checks that the message is
1154 * well-formed and then checks if there are any pending requests for
1155 * this content and possibly passes it on (to local clients or other
1156 * peers). Does NOT perform migration (content caching at this peer).
1158 * @param cp the other peer involved (sender or receiver, NULL
1159 * for loopback messages where we are both sender and receiver)
1160 * @param message the actual message
1161 * @return GNUNET_OK if the message was well-formed,
1162 * GNUNET_SYSERR if the message was malformed (close connection,
1163 * do not cache under any circumstances)
1166 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1167 const struct GNUNET_MessageHeader *message)
1169 const struct PutMessage *put;
1172 enum GNUNET_BLOCK_Type type;
1173 struct GNUNET_TIME_Absolute expiration;
1174 GNUNET_HashCode query;
1175 struct ProcessReplyClosure prq;
1176 struct GNUNET_TIME_Relative block_time;
1178 struct PutMigrationContext *pmc;
1180 msize = ntohs (message->size);
1181 if (msize < sizeof (struct PutMessage))
1184 return GNUNET_SYSERR;
1186 put = (const struct PutMessage*) message;
1187 dsize = msize - sizeof (struct PutMessage);
1188 type = ntohl (put->type);
1189 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1190 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1191 return GNUNET_SYSERR;
1193 GNUNET_BLOCK_get_key (GSF_block_ctx,
1199 GNUNET_break_op (0);
1200 return GNUNET_SYSERR;
1202 /* now, lookup 'query' */
1203 prq.data = (const void*) &put[1];
1210 prq.expiration = expiration;
1212 prq.anonymity_level = 1;
1213 prq.request_found = GNUNET_NO;
1214 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1220 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1221 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1223 if ( (GNUNET_YES == active_to_migration) &&
1224 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Replicating result for query `%s' with priority %u\n",
1229 GNUNET_h2s (&query),
1232 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1233 pmc->start = GNUNET_TIME_absolute_get ();
1234 pmc->requested = prq.request_found;
1235 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1237 GNUNET_DATASTORE_put (GSF_dsh,
1238 0, &query, dsize, &put[1],
1239 type, prq.priority, 1 /* anonymity */,
1241 1 + prq.priority, MAX_DATASTORE_QUEUE,
1242 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1243 &put_migration_continuation,
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Choosing not to keep content `%s' (%d/%d)\n",
1251 GNUNET_h2s (&query),
1252 active_to_migration,
1253 test_put_load_too_high (prq.priority));
1256 putl = GNUNET_LOAD_get_load (datastore_put_load);
1257 if ( (NULL != (cp = prq.sender)) &&
1258 (GNUNET_NO == prq.request_found) &&
1259 ( (GNUNET_YES != active_to_migration) ||
1260 (putl > 2.5 * (1 + prq.priority)) ) )
1262 if (GNUNET_YES != active_to_migration)
1263 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1264 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1265 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1266 (unsigned int) (60000 * putl * putl)));
1267 GSF_block_peer_migration_ (cp, block_time);
1274 * Setup the subsystem.
1277 GSF_pending_request_init_ ()
1280 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1282 "MAX_PENDING_REQUESTS",
1283 &max_pending_requests))
1285 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1286 _("Configuration fails to specify `%s', assuming default value."),
1287 "MAX_PENDING_REQUESTS");
1289 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1292 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1293 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1294 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1299 * Shutdown the subsystem.
1302 GSF_pending_request_done_ ()
1304 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1307 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1309 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1310 requests_by_expiration_heap = NULL;
1311 GNUNET_LOAD_value_free (datastore_put_load);
1312 datastore_put_load = NULL;
1316 /* end of gnunet-service-fs_pr.c */