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 * Current offset for querying our local datastore for results.
104 * Starts at a random value, incremented until we get the same
105 * UID again (detected using 'first_uid'), which is then used
106 * to termiante the iteration.
108 uint64_t local_result_offset;
111 * Unique ID of the first result from the local datastore;
112 * used to detect wrap-around of the offset.
117 * Number of valid entries in the 'replies_seen' array.
119 unsigned int replies_seen_count;
122 * Length of the 'replies_seen' array.
124 unsigned int replies_seen_size;
127 * Mingle value we currently use for the bf.
135 * All pending requests, ordered by the query. Entries
136 * are of type 'struct GSF_PendingRequest*'.
138 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
142 * Datastore 'PUT' load tracking.
144 static struct GNUNET_LOAD_Value *datastore_put_load;
148 * Are we allowed to migrate content to this peer.
150 static int active_to_migration;
154 * Heap with the request that will expire next at the top. Contains
155 * pointers of type "struct PendingRequest*"; these will *also* be
156 * aliased from the "requests_by_peer" data structures and the
157 * "requests_by_query" table. Note that requests from our clients
158 * don't expire and are thus NOT in the "requests_by_expiration"
159 * (or the "requests_by_peer" tables).
161 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
165 * Maximum number of requests (from other peers, overall) that we're
166 * willing to have pending at any given point in time. Can be changed
167 * via the configuration file (32k is just the default).
169 static unsigned long long max_pending_requests = (32 * 1024);
173 * How many bytes should a bloomfilter be if we have already seen
174 * entry_count responses? Note that BLOOMFILTER_K gives us the number
175 * of bits set per entry. Furthermore, we should not re-size the
176 * filter too often (to keep it cheap).
178 * Since other peers will also add entries but not resize the filter,
179 * we should generally pick a slightly larger size than what the
180 * strict math would suggest.
182 * @return must be a power of two and smaller or equal to 2^15.
185 compute_bloomfilter_size (unsigned int entry_count)
188 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
189 uint16_t max = 1 << 15;
191 if (entry_count > max)
194 while ((size < max) && (size < ideal))
203 * Recalculate our bloom filter for filtering replies. This function
204 * will create a new bloom filter from scratch, so it should only be
205 * called if we have no bloomfilter at all (and hence can create a
206 * fresh one of minimal size without problems) OR if our peer is the
207 * initiator (in which case we may resize to larger than mimimum size).
209 * @param pr request for which the BF is to be recomputed
210 * @return GNUNET_YES if a refresh actually happened
213 refresh_bloomfilter (struct GSF_PendingRequest *pr)
217 GNUNET_HashCode mhash;
219 nsize = compute_bloomfilter_size (pr->replies_seen_count);
220 if ( (pr->bf != NULL) &&
221 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
222 return GNUNET_NO; /* size not changed */
224 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
225 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
227 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
230 for (i=0;i<pr->replies_seen_count;i++)
232 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
235 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
242 * Create a new pending request.
244 * @param options request options
245 * @param type type of the block that is being requested
246 * @param query key for the lookup
247 * @param namespace namespace to lookup, NULL for no namespace
248 * @param target preferred target for the request, NULL for none
249 * @param bf_data raw data for bloom filter for known replies, can be NULL
250 * @param bf_size number of bytes in bf_data
251 * @param mingle mingle value for bf
252 * @param anonymity_level desired anonymity level
253 * @param priority maximum outgoing cummulative request priority to use
254 * @param ttl current time-to-live for the request
255 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
256 * @param replies_seen hash codes of known local replies
257 * @param replies_seen_count size of the 'replies_seen' array
258 * @param rh handle to call when we get a reply
259 * @param rh_cls closure for rh
260 * @return handle for the new pending request
262 struct GSF_PendingRequest *
263 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
264 enum GNUNET_BLOCK_Type type,
265 const GNUNET_HashCode *query,
266 const GNUNET_HashCode *namespace,
267 const struct GNUNET_PeerIdentity *target,
271 uint32_t anonymity_level,
274 GNUNET_PEER_Id sender_pid,
275 const GNUNET_HashCode *replies_seen,
276 unsigned int replies_seen_count,
277 GSF_PendingRequestReplyHandler rh,
280 struct GSF_PendingRequest *pr;
281 struct GSF_PendingRequest *dpr;
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Creating request handle for `%s' of type %d\n",
289 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
290 pr->local_result_offset = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
292 pr->public_data.query = *query;
293 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
295 GNUNET_assert (NULL != namespace);
296 pr->public_data.namespace = *namespace;
300 pr->public_data.target = *target;
301 pr->public_data.has_target = GNUNET_YES;
303 pr->public_data.anonymity_level = anonymity_level;
304 pr->public_data.priority = priority;
305 pr->public_data.original_priority = priority;
306 pr->public_data.options = options;
307 pr->public_data.type = type;
308 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
309 pr->sender_pid = sender_pid;
313 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
316 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
317 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
318 (uint32_t) (- ttl)));
319 if (replies_seen_count > 0)
321 pr->replies_seen_size = replies_seen_count;
322 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
323 memcpy (pr->replies_seen,
325 replies_seen_count * sizeof (GNUNET_HashCode));
326 pr->replies_seen_count = replies_seen_count;
330 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
335 else if ( (replies_seen_count > 0) &&
336 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
338 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
340 GNUNET_CONTAINER_multihashmap_put (pr_map,
343 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
344 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
346 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
348 pr->public_data.ttl.abs_value);
349 /* make sure we don't track too many requests */
350 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
352 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
353 GNUNET_assert (dpr != NULL);
355 break; /* let the request live briefly... */
356 dpr->rh (dpr->rh_cls,
357 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
359 GNUNET_TIME_UNIT_FOREVER_ABS,
360 GNUNET_BLOCK_TYPE_ANY,
362 GSF_pending_request_cancel_ (dpr);
370 * Obtain the public data associated with a pending request
372 * @param pr pending request
373 * @return associated public data
375 struct GSF_PendingRequestData *
376 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
378 return &pr->public_data;
383 * Update a given pending request with additional replies
384 * that have been seen.
386 * @param pr request to update
387 * @param replies_seen hash codes of replies that we've seen
388 * @param replies_seen_count size of the replies_seen array
391 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
392 const GNUNET_HashCode *replies_seen,
393 unsigned int replies_seen_count)
396 GNUNET_HashCode mhash;
398 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
399 return; /* integer overflow */
400 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
402 /* we're responsible for the BF, full refresh */
403 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
404 GNUNET_array_grow (pr->replies_seen,
405 pr->replies_seen_size,
406 replies_seen_count + pr->replies_seen_count);
407 memcpy (&pr->replies_seen[pr->replies_seen_count],
409 sizeof (GNUNET_HashCode) * replies_seen_count);
410 pr->replies_seen_count += replies_seen_count;
411 if (GNUNET_NO == refresh_bloomfilter (pr))
413 /* bf not recalculated, simply extend it with new bits */
414 for (i=0;i<pr->replies_seen_count;i++)
416 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
419 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
427 /* we're not the initiator, but the initiator did not give us
428 any bloom-filter, so we need to create one on-the-fly */
429 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
431 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
432 compute_bloomfilter_size (replies_seen_count),
435 for (i=0;i<pr->replies_seen_count;i++)
437 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
440 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
447 * Generate the message corresponding to the given pending request for
448 * transmission to other peers (or at least determine its size).
450 * @param pr request to generate the message for
451 * @param buf_size number of bytes available in buf
452 * @param buf where to copy the message (can be NULL)
453 * @return number of bytes needed (if > buf_size) or used
456 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
460 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
461 struct GetMessage *gm;
462 GNUNET_HashCode *ext;
468 struct GNUNET_TIME_Absolute now;
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Building request message for `%s' of type %d\n",
476 GNUNET_h2s (&pr->public_data.query),
477 pr->public_data.type);
481 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
484 bm |= GET_MESSAGE_BIT_RETURN_TO;
487 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
489 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
492 if (GNUNET_YES == pr->public_data.has_target)
494 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
497 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
498 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
499 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
500 if (buf_size < msize)
502 gm = (struct GetMessage*) lbuf;
503 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
504 gm->header.size = htons (msize);
505 gm->type = htonl (pr->public_data.type);
507 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
508 pr->public_data.priority + 1);
511 pr->public_data.priority -= prio;
512 gm->priority = htonl (prio);
513 now = GNUNET_TIME_absolute_get ();
514 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
515 gm->ttl = htonl (ttl / 1000);
516 gm->filter_mutator = htonl(pr->mingle);
517 gm->hash_bitmap = htonl (bm);
518 gm->query = pr->public_data.query;
519 ext = (GNUNET_HashCode*) &gm[1];
522 GNUNET_PEER_resolve (pr->sender_pid,
523 (struct GNUNET_PeerIdentity*) &ext[k++]);
524 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
526 &pr->public_data.namespace,
527 sizeof (GNUNET_HashCode));
528 if (GNUNET_YES == pr->public_data.has_target)
529 GNUNET_PEER_resolve (pr->sender_pid,
530 (struct GNUNET_PeerIdentity*) &ext[k++]);
532 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
535 memcpy (buf, gm, msize);
541 * Iterator to free pending requests.
543 * @param cls closure, unused
544 * @param key current key code
545 * @param value value in the hash map (pending request)
546 * @return GNUNET_YES (we should continue to iterate)
549 clean_request (void *cls,
550 const GNUNET_HashCode * key,
553 struct GSF_PendingRequest *pr = value;
554 GSF_LocalLookupContinuation cont;
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "Cleaning up pending request for `%s'.\n",
561 if (NULL != (cont = pr->llc_cont))
564 cont (pr->llc_cont_cls,
568 GSF_plan_notify_request_done_ (pr);
569 GNUNET_free_non_null (pr->replies_seen);
571 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
572 GNUNET_PEER_change_rc (pr->sender_pid, -1);
573 if (NULL != pr->hnode)
574 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
576 GNUNET_DATASTORE_cancel (pr->qe);
578 GNUNET_DHT_get_stop (pr->gh);
585 * Explicitly cancel a pending request.
587 * @param pr request to cancel
590 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
592 if (NULL == pr_map) return; /* already cleaned up! */
593 GNUNET_assert (GNUNET_OK ==
594 GNUNET_CONTAINER_multihashmap_remove (pr_map,
595 &pr->public_data.query,
597 GNUNET_assert (GNUNET_YES ==
598 clean_request (NULL, &pr->public_data.query, pr));
603 * Iterate over all pending requests.
605 * @param it function to call for each request
606 * @param cls closure for it
609 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
612 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
613 (GNUNET_CONTAINER_HashMapIterator) it,
621 * Closure for "process_reply" function.
623 struct ProcessReplyClosure
626 * The data for the reply.
631 * Who gave us this reply? NULL for local host (or DHT)
633 struct GSF_ConnectedPeer *sender;
636 * When the reply expires.
638 struct GNUNET_TIME_Absolute expiration;
648 enum GNUNET_BLOCK_Type type;
651 * How much was this reply worth to us?
656 * Anonymity requirements for this reply.
658 uint32_t anonymity_level;
661 * Evaluation result (returned).
663 enum GNUNET_BLOCK_EvaluationResult eval;
666 * Did we find a matching request?
673 * Update the performance data for the sender (if any) since
674 * the sender successfully answered one of our queries.
676 * @param prq information about the sender
677 * @param pr request that was satisfied
680 update_request_performance_data (struct ProcessReplyClosure *prq,
681 struct GSF_PendingRequest *pr)
683 if (prq->sender == NULL)
685 GSF_peer_update_performance_ (prq->sender,
686 pr->public_data.start_time,
692 * We have received a reply; handle it!
694 * @param cls response (struct ProcessReplyClosure)
695 * @param key our query
696 * @param value value in the hash map (info about the query)
697 * @return GNUNET_YES (we should continue to iterate)
700 process_reply (void *cls,
701 const GNUNET_HashCode * key,
704 struct ProcessReplyClosure *prq = cls;
705 struct GSF_PendingRequest *pr = value;
706 GNUNET_HashCode chash;
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Matched result (type %u) for query `%s' with pending request\n",
711 (unsigned int) prq->type,
714 GNUNET_STATISTICS_update (GSF_stats,
715 gettext_noop ("# replies received and matched"),
718 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
723 &pr->public_data.namespace,
724 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
729 case GNUNET_BLOCK_EVALUATION_OK_MORE:
730 update_request_performance_data (prq, pr);
732 case GNUNET_BLOCK_EVALUATION_OK_LAST:
733 /* short cut: stop processing early, no BF-update, etc. */
734 update_request_performance_data (prq, pr);
735 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
736 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
737 /* pass on to other peers / local clients */
743 prq->data, prq->size);
745 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
746 GNUNET_STATISTICS_update (GSF_stats,
747 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Duplicate response `%s', discarding.\n",
753 GNUNET_h2s (&mhash));
755 return GNUNET_YES; /* duplicate */
756 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
757 return GNUNET_YES; /* wrong namespace */
758 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
761 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
764 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 _("Unsupported block type %u\n"),
770 /* update bloomfilter */
771 GNUNET_CRYPTO_hash (prq->data,
774 GSF_pending_request_update_ (pr, &chash, 1);
775 if (NULL == prq->sender)
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Found result for query `%s' in local datastore\n",
782 GNUNET_STATISTICS_update (GSF_stats,
783 gettext_noop ("# results found locally"),
789 GSF_dht_lookup_ (pr);
791 prq->priority += pr->public_data.original_priority;
792 pr->public_data.priority = 0;
793 pr->public_data.original_priority = 0;
794 pr->public_data.results_found++;
795 prq->request_found = GNUNET_YES;
796 /* finally, pass on to other peer / local client */
802 prq->data, prq->size);
808 * Context for the 'put_migration_continuation'.
810 struct PutMigrationContext
814 * Start time for the operation.
816 struct GNUNET_TIME_Absolute start;
821 struct GNUNET_PeerIdentity origin;
824 * GNUNET_YES if we had a matching request for this block,
832 * Continuation called to notify client about result of the
836 * @param success GNUNET_SYSERR on failure
837 * @param msg NULL on success, otherwise an error message
840 put_migration_continuation (void *cls,
844 struct PutMigrationContext *pmc = cls;
845 struct GNUNET_TIME_Relative delay;
846 struct GNUNET_TIME_Relative block_time;
847 struct GSF_ConnectedPeer *cp;
848 struct GSF_PeerPerformanceData *ppd;
850 delay = GNUNET_TIME_absolute_get_duration (pmc->start);
851 cp = GSF_peer_get_ (&pmc->origin);
852 if ( (GNUNET_OK != success) &&
853 (GNUNET_NO == pmc->requested) )
855 /* block migration for a bit... */
858 ppd = GSF_get_peer_performance_data_ (cp);
859 ppd->migration_duplication++;
860 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
861 5 * ppd->migration_duplication +
862 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5));
863 GSF_block_peer_migration_ (cp, block_time);
870 ppd = GSF_get_peer_performance_data_ (cp);
871 ppd->migration_duplication = 0; /* reset counter */
875 /* FIXME: should we really update the load value on failure? */
876 GNUNET_LOAD_update (datastore_put_load,
878 if (GNUNET_OK == success)
880 GNUNET_STATISTICS_update (GSF_stats,
881 gettext_noop ("# datastore 'put' failures"),
888 * Test if the DATABASE (PUT) load on this peer is too high
889 * to even consider processing the query at
892 * @return GNUNET_YES if the load is too high to do anything (load high)
893 * GNUNET_NO to process normally (load normal or low)
896 test_put_load_too_high (uint32_t priority)
900 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
901 return GNUNET_NO; /* very fast */
902 ld = GNUNET_LOAD_get_load (datastore_put_load);
903 if (ld < 2.0 * (1 + priority))
905 GNUNET_STATISTICS_update (GSF_stats,
906 gettext_noop ("# storage requests dropped due to high load"),
914 * Iterator called on each result obtained for a DHT
915 * operation that expects a reply
918 * @param exp when will this value expire
919 * @param key key of the result
920 * @param get_path NULL-terminated array of pointers
921 * to the peers on reverse GET path (or NULL if not recorded)
922 * @param put_path NULL-terminated array of pointers
923 * to the peers on the PUT path (or NULL if not recorded)
924 * @param type type of the result
925 * @param size number of bytes in data
926 * @param data pointer to the result data
929 handle_dht_reply (void *cls,
930 struct GNUNET_TIME_Absolute exp,
931 const GNUNET_HashCode *key,
932 const struct GNUNET_PeerIdentity * const *get_path,
933 const struct GNUNET_PeerIdentity * const *put_path,
934 enum GNUNET_BLOCK_Type type,
938 struct GSF_PendingRequest *pr = cls;
939 struct ProcessReplyClosure prq;
940 struct PutMigrationContext *pmc;
942 memset (&prq, 0, sizeof (prq));
944 prq.expiration = exp;
947 process_reply (&prq, key, pr);
948 if ( (GNUNET_YES == active_to_migration) &&
949 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Replicating result for query `%s' with priority %u\n",
957 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
958 pmc->start = GNUNET_TIME_absolute_get ();
959 pmc->requested = GNUNET_YES;
960 GNUNET_DATASTORE_put (GSF_dsh,
962 type, prq.priority, 1 /* anonymity */,
965 1 + prq.priority, MAX_DATASTORE_QUEUE,
966 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
967 &put_migration_continuation,
974 * Consider looking up the data in the DHT (anonymity-level permitting).
976 * @param pr the pending request to process
979 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
983 struct GNUNET_PeerIdentity pi;
984 char buf[sizeof (GNUNET_HashCode) * 2];
986 if (0 != pr->public_data.anonymity_level)
990 GNUNET_DHT_get_stop (pr->gh);
995 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
998 memcpy (buf, &pr->public_data.namespace, sizeof (GNUNET_HashCode));
999 xquery_size = sizeof (GNUNET_HashCode);
1001 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1003 GNUNET_PEER_resolve (pr->sender_pid,
1005 memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1006 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1008 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1009 GNUNET_TIME_UNIT_FOREVER_REL,
1010 pr->public_data.type,
1011 &pr->public_data.query,
1012 DEFAULT_GET_REPLICATION,
1013 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1023 * We're processing (local) results for a search request
1024 * from another peer. Pass applicable results to the
1025 * peer and if we are done either clean up (operation
1026 * complete) or forward to other peers (more results possible).
1028 * @param cls our closure (struct PendingRequest)
1029 * @param key key for the content
1030 * @param size number of bytes in data
1031 * @param data content stored
1032 * @param type type of the content
1033 * @param priority priority of the content
1034 * @param anonymity anonymity-level for the content
1035 * @param expiration expiration time for the content
1036 * @param uid unique identifier for the datum;
1037 * maybe 0 if no unique identifier is available
1040 process_local_reply (void *cls,
1041 const GNUNET_HashCode *key,
1044 enum GNUNET_BLOCK_Type type,
1047 struct GNUNET_TIME_Absolute expiration,
1050 struct GSF_PendingRequest *pr = cls;
1051 GSF_LocalLookupContinuation cont;
1052 struct ProcessReplyClosure prq;
1053 GNUNET_HashCode query;
1054 unsigned int old_rf;
1057 if (0 == pr->replies_seen_count)
1059 pr->first_uid = uid;
1063 if (uid == pr->first_uid)
1064 key = NULL; /* all replies seen! */
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "No further local responses available.\n");
1072 if (NULL != (cont = pr->llc_cont))
1074 pr->llc_cont = NULL;
1075 cont (pr->llc_cont_cls,
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1086 (unsigned long long) uid);
1088 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Found ONDEMAND block, performing on-demand encoding\n");
1094 GNUNET_STATISTICS_update (GSF_stats,
1095 gettext_noop ("# on-demand blocks matched requests"),
1099 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1100 anonymity, expiration, uid,
1101 &process_local_reply,
1104 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1105 pr->local_result_offset - 1,
1106 &pr->public_data.query,
1107 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1108 ? GNUNET_BLOCK_TYPE_ANY
1109 : pr->public_data.type,
1110 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1112 : 1 /* queue priority */,
1113 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1115 : 1 /* max queue size */,
1116 GNUNET_TIME_UNIT_FOREVER_REL,
1117 &process_local_reply,
1119 GNUNET_assert (NULL != pr->qe);
1123 old_rf = pr->public_data.results_found;
1124 memset (&prq, 0, sizeof (prq));
1126 prq.expiration = expiration;
1129 GNUNET_BLOCK_get_key (GSF_block_ctx,
1136 GNUNET_DATASTORE_remove (GSF_dsh,
1140 GNUNET_TIME_UNIT_FOREVER_REL,
1142 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1143 pr->local_result_offset - 1,
1144 &pr->public_data.query,
1145 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1146 ? GNUNET_BLOCK_TYPE_ANY
1147 : pr->public_data.type,
1148 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1150 : 1 /* queue priority */,
1151 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1153 : 1 /* max queue size */,
1154 GNUNET_TIME_UNIT_FOREVER_REL,
1155 &process_local_reply,
1157 GNUNET_assert (NULL != pr->qe);
1161 prq.priority = priority;
1162 prq.request_found = GNUNET_NO;
1163 prq.anonymity_level = anonymity;
1164 if ( (old_rf == 0) &&
1165 (pr->public_data.results_found == 0) )
1166 GSF_update_datastore_delay_ (pr->public_data.start_time);
1167 process_reply (&prq, key, pr);
1168 pr->local_result = prq.eval;
1169 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1171 if (NULL != (cont = pr->llc_cont))
1173 pr->llc_cont = NULL;
1174 cont (pr->llc_cont_cls,
1180 if ( (0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1181 ( (GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1182 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority) ) )
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Load too high, done with request\n");
1188 GNUNET_STATISTICS_update (GSF_stats,
1189 gettext_noop ("# processing result set cut short due to load"),
1192 if (NULL != (cont = pr->llc_cont))
1194 pr->llc_cont = NULL;
1195 cont (pr->llc_cont_cls,
1201 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1202 pr->local_result_offset++,
1203 &pr->public_data.query,
1204 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1205 ? GNUNET_BLOCK_TYPE_ANY
1206 : pr->public_data.type,
1207 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1209 : 1 /* queue priority */,
1210 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1212 : 1 /* max queue size */,
1213 GNUNET_TIME_UNIT_FOREVER_REL,
1214 &process_local_reply,
1216 GNUNET_assert (NULL != pr->qe);
1221 * Look up the request in the local datastore.
1223 * @param pr the pending request to process
1224 * @param cont function to call at the end
1225 * @param cont_cls closure for cont
1228 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1229 GSF_LocalLookupContinuation cont,
1232 GNUNET_assert (NULL == pr->gh);
1233 GNUNET_assert (NULL == pr->llc_cont);
1234 pr->llc_cont = cont;
1235 pr->llc_cont_cls = cont_cls;
1236 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1237 pr->local_result_offset++,
1238 &pr->public_data.query,
1239 pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK
1240 ? GNUNET_BLOCK_TYPE_ANY
1241 : pr->public_data.type,
1242 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1244 : 1 /* queue priority */,
1245 (0 != (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options))
1247 : 1 /* max queue size */,
1248 GNUNET_TIME_UNIT_FOREVER_REL,
1249 &process_local_reply,
1256 * Handle P2P "CONTENT" message. Checks that the message is
1257 * well-formed and then checks if there are any pending requests for
1258 * this content and possibly passes it on (to local clients or other
1259 * peers). Does NOT perform migration (content caching at this peer).
1261 * @param cp the other peer involved (sender or receiver, NULL
1262 * for loopback messages where we are both sender and receiver)
1263 * @param message the actual message
1264 * @return GNUNET_OK if the message was well-formed,
1265 * GNUNET_SYSERR if the message was malformed (close connection,
1266 * do not cache under any circumstances)
1269 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
1270 const struct GNUNET_MessageHeader *message)
1272 const struct PutMessage *put;
1275 enum GNUNET_BLOCK_Type type;
1276 struct GNUNET_TIME_Absolute expiration;
1277 GNUNET_HashCode query;
1278 struct ProcessReplyClosure prq;
1279 struct GNUNET_TIME_Relative block_time;
1281 struct PutMigrationContext *pmc;
1283 msize = ntohs (message->size);
1284 if (msize < sizeof (struct PutMessage))
1287 return GNUNET_SYSERR;
1289 put = (const struct PutMessage*) message;
1290 dsize = msize - sizeof (struct PutMessage);
1291 type = ntohl (put->type);
1292 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1293 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1294 return GNUNET_SYSERR;
1296 GNUNET_BLOCK_get_key (GSF_block_ctx,
1302 GNUNET_break_op (0);
1303 return GNUNET_SYSERR;
1305 /* now, lookup 'query' */
1306 prq.data = (const void*) &put[1];
1313 prq.expiration = expiration;
1315 prq.anonymity_level = 1;
1316 prq.request_found = GNUNET_NO;
1317 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1323 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
1324 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
1326 if ( (GNUNET_YES == active_to_migration) &&
1327 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Replicating result for query `%s' with priority %u\n",
1332 GNUNET_h2s (&query),
1335 pmc = GNUNET_malloc (sizeof (struct PutMigrationContext));
1336 pmc->start = GNUNET_TIME_absolute_get ();
1337 pmc->requested = prq.request_found;
1338 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1340 GNUNET_DATASTORE_put (GSF_dsh,
1341 0, &query, dsize, &put[1],
1342 type, prq.priority, 1 /* anonymity */,
1343 0 /* replication */,
1345 1 + prq.priority, MAX_DATASTORE_QUEUE,
1346 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
1347 &put_migration_continuation,
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Choosing not to keep content `%s' (%d/%d)\n",
1355 GNUNET_h2s (&query),
1356 active_to_migration,
1357 test_put_load_too_high (prq.priority));
1360 putl = GNUNET_LOAD_get_load (datastore_put_load);
1361 if ( (NULL != (cp = prq.sender)) &&
1362 (GNUNET_NO == prq.request_found) &&
1363 ( (GNUNET_YES != active_to_migration) ||
1364 (putl > 2.5 * (1 + prq.priority)) ) )
1366 if (GNUNET_YES != active_to_migration)
1367 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1368 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1369 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1370 (unsigned int) (60000 * putl * putl)));
1371 GSF_block_peer_migration_ (cp, block_time);
1378 * Setup the subsystem.
1381 GSF_pending_request_init_ ()
1384 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1386 "MAX_PENDING_REQUESTS",
1387 &max_pending_requests))
1389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1390 _("Configuration fails to specify `%s', assuming default value."),
1391 "MAX_PENDING_REQUESTS");
1393 active_to_migration = GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
1396 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1397 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
1398 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1403 * Shutdown the subsystem.
1406 GSF_pending_request_done_ ()
1408 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1411 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1413 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1414 requests_by_expiration_heap = NULL;
1415 GNUNET_LOAD_value_free (datastore_put_load);
1416 datastore_put_load = NULL;
1420 /* end of gnunet-service-fs_pr.c */