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_pr.h"
35 struct GSF_PendingRequest
38 * Public data for the request.
40 struct GSF_PendingRequestData public_data;
43 * Function to call if we encounter a reply.
45 GSF_PendingRequestReplyHandler rh;
53 * Array of hash codes of replies we've already seen.
55 GNUNET_HashCode *replies_seen;
58 * Bloomfilter masking replies we've already seen.
60 struct GNUNET_CONTAINER_BloomFilter *bf;
63 * Entry for this pending request in the expiration heap, or NULL.
65 struct GNUNET_CONTAINER_HeapNode *hnode;
68 * Identity of the peer that we should use for the 'sender'
69 * (recipient of the response) when forwarding (0 for none).
71 GNUNET_PEER_Id sender_pid;
74 * Number of valid entries in the 'replies_seen' array.
76 unsigned int replies_seen_count;
79 * Length of the 'replies_seen' array.
81 unsigned int replies_seen_size;
84 * Mingle value we currently use for the bf.
92 * All pending requests, ordered by the query. Entries
93 * are of type 'struct GSF_PendingRequest*'.
95 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
99 * Datastore 'PUT' load tracking.
101 static struct GNUNET_LOAD_Value *datastore_put_load;
105 * Are we allowed to migrate content to this peer.
107 static int active_to_migration;
111 * Heap with the request that will expire next at the top. Contains
112 * pointers of type "struct PendingRequest*"; these will *also* be
113 * aliased from the "requests_by_peer" data structures and the
114 * "requests_by_query" table. Note that requests from our clients
115 * don't expire and are thus NOT in the "requests_by_expiration"
116 * (or the "requests_by_peer" tables).
118 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
122 * Maximum number of requests (from other peers, overall) that we're
123 * willing to have pending at any given point in time. Can be changed
124 * via the configuration file (32k is just the default).
126 static unsigned long long max_pending_requests = (32 * 1024);
130 * How many bytes should a bloomfilter be if we have already seen
131 * entry_count responses? Note that BLOOMFILTER_K gives us the number
132 * of bits set per entry. Furthermore, we should not re-size the
133 * filter too often (to keep it cheap).
135 * Since other peers will also add entries but not resize the filter,
136 * we should generally pick a slightly larger size than what the
137 * strict math would suggest.
139 * @return must be a power of two and smaller or equal to 2^15.
142 compute_bloomfilter_size (unsigned int entry_count)
145 unsigned int ideal = (entry_count * BLOOMFILTER_K) / 4;
146 uint16_t max = 1 << 15;
148 if (entry_count > max)
151 while ((size < max) && (size < ideal))
160 * Recalculate our bloom filter for filtering replies. This function
161 * will create a new bloom filter from scratch, so it should only be
162 * called if we have no bloomfilter at all (and hence can create a
163 * fresh one of minimal size without problems) OR if our peer is the
164 * initiator (in which case we may resize to larger than mimimum size).
166 * @param pr request for which the BF is to be recomputed
167 * @return GNUNET_YES if a refresh actually happened
170 refresh_bloomfilter (struct GSF_PendingRequest *pr)
174 GNUNET_HashCode mhash;
176 nsize = compute_bloomfilter_size (pr->replies_seen_count);
177 if ( (pr->bf != NULL) &&
178 (nsize == GNUNET_CONTAINER_bloomfilter_get_size (pr->bf)) )
179 return GNUNET_NO; /* size not changed */
181 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
182 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
184 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
187 for (i=0;i<pr->replies_seen_count;i++)
189 GNUNET_BLOCK_mingle_hash (&pr->replies_seen[i],
192 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
199 * Create a new pending request.
201 * @param options request options
202 * @param type type of the block that is being requested
203 * @param query key for the lookup
204 * @param namespace namespace to lookup, NULL for no namespace
205 * @param target preferred target for the request, NULL for none
206 * @param bf_data raw data for bloom filter for known replies, can be NULL
207 * @param bf_size number of bytes in bf_data
208 * @param mingle mingle value for bf
209 * @param anonymity_level desired anonymity level
210 * @param priority maximum outgoing cummulative request priority to use
211 * @param ttl current time-to-live for the request
212 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
213 * @param replies_seen hash codes of known local replies
214 * @param replies_seen_count size of the 'replies_seen' array
215 * @param rh handle to call when we get a reply
216 * @param rh_cls closure for rh
217 * @return handle for the new pending request
219 struct GSF_PendingRequest *
220 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
221 enum GNUNET_BLOCK_Type type,
222 const GNUNET_HashCode *query,
223 const GNUNET_HashCode *namespace,
224 const struct GNUNET_PeerIdentity *target,
228 uint32_t anonymity_level,
231 GNUNET_PEER_Id sender_pid,
232 const GNUNET_HashCode *replies_seen,
233 unsigned int replies_seen_count,
234 GSF_PendingRequestReplyHandler rh,
237 struct GSF_PendingRequest *pr;
238 struct GSF_PendingRequest *dpr;
240 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest));
241 pr->public_data.query = *query;
242 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == type)
244 GNUNET_assert (NULL != namespace);
245 pr->public_data.namespace = *namespace;
249 pr->public_data.target = *target;
250 pr->public_data.has_target = GNUNET_YES;
252 pr->public_data.anonymity_level = anonymity_level;
253 pr->public_data.priority = priority;
254 pr->public_data.original_priority = priority;
255 pr->public_data.options = options;
256 pr->public_data.type = type;
257 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
258 pr->sender_pid = sender_pid;
262 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
265 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
266 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
267 (uint32_t) (- ttl)));
268 if (replies_seen_count > 0)
270 pr->replies_seen_size = replies_seen_count;
271 pr->replies_seen = GNUNET_malloc (sizeof (GNUNET_HashCode) * pr->replies_seen_size);
272 memcpy (pr->replies_seen,
274 replies_seen_count * sizeof (GNUNET_HashCode));
275 pr->replies_seen_count = replies_seen_count;
279 pr->bf = GNUNET_CONTAINER_bloomfilter_init (bf_data,
284 else if ( (replies_seen_count > 0) &&
285 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)) )
287 GNUNET_assert (GNUNET_YES == refresh_bloomfilter (pr));
289 GNUNET_CONTAINER_multihashmap_put (pr_map,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
293 if (0 != (options & GSF_PRO_REQUEST_EXPIRES))
295 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
297 pr->public_data.ttl.abs_value);
298 /* make sure we don't track too many requests */
299 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) > max_pending_requests)
301 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
302 GNUNET_assert (dpr != NULL);
304 break; /* let the request live briefly... */
305 dpr->rh (dpr->rh_cls,
307 GNUNET_TIME_UNIT_FOREVER_ABS,
310 GSF_pending_request_cancel_ (dpr);
318 * Obtain the public data associated with a pending request
320 * @param pr pending request
321 * @return associated public data
323 struct GSF_PendingRequestData *
324 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
326 return &pr->public_data;
331 * Update a given pending request with additional replies
332 * that have been seen.
334 * @param pr request to update
335 * @param replies_seen hash codes of replies that we've seen
336 * @param replies_seen_count size of the replies_seen array
339 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
340 const GNUNET_HashCode *replies_seen,
341 unsigned int replies_seen_count)
344 GNUNET_HashCode mhash;
346 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
347 return; /* integer overflow */
348 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
350 /* we're responsible for the BF, full refresh */
351 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
352 GNUNET_array_grow (pr->replies_seen,
353 pr->replies_seen_size,
354 replies_seen_count + pr->replies_seen_count);
355 memcpy (&pr->replies_seen[pr->replies_seen_count],
357 sizeof (GNUNET_HashCode) * replies_seen_count);
358 pr->replies_seen_count += replies_seen_count;
359 if (GNUNET_NO == refresh_bloomfilter (pr))
361 /* bf not recalculated, simply extend it with new bits */
362 for (i=0;i<pr->replies_seen_count;i++)
364 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
367 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
375 /* we're not the initiator, but the initiator did not give us
376 any bloom-filter, so we need to create one on-the-fly */
377 pr->mingle = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
379 pr->bf = GNUNET_CONTAINER_bloomfilter_init (NULL,
380 compute_bloomfilter_size (replies_seen_count),
383 for (i=0;i<pr->replies_seen_count;i++)
385 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
388 GNUNET_CONTAINER_bloomfilter_add (pr->bf, &mhash);
395 * Generate the message corresponding to the given pending request for
396 * transmission to other peers (or at least determine its size).
398 * @param pr request to generate the message for
399 * @param do_route are we routing the reply
400 * @param buf_size number of bytes available in buf
401 * @param buf where to copy the message (can be NULL)
402 * @return number of bytes needed (if > buf_size) or used
405 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr,
410 char lbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE];
411 struct GetMessage *gm;
412 GNUNET_HashCode *ext;
418 struct GNUNET_TIME_Absolute now;
423 if (GNUNET_YES != do_route)
425 bm |= GET_MESSAGE_BIT_RETURN_TO;
428 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
430 bm |= GET_MESSAGE_BIT_SKS_NAMESPACE;
433 if (GNUNET_YES == pr->public_data.has_target)
435 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
438 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
439 msize = sizeof (struct GetMessage) + bf_size + k * sizeof(GNUNET_HashCode);
440 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
441 if (buf_size < msize)
443 gm = (struct GetMessage*) lbuf;
444 gm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_GET);
445 gm->header.size = htons (msize);
446 gm->type = htonl (pr->public_data.type);
447 if (GNUNET_YES == do_route)
448 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
449 pr->public_data.priority + 1);
452 pr->public_data.priority -= prio;
453 gm->priority = htonl (prio);
454 now = GNUNET_TIME_absolute_get ();
455 ttl = (int64_t) (pr->public_data.ttl.abs_value - now.abs_value);
456 gm->ttl = htonl (ttl / 1000);
457 gm->filter_mutator = htonl(pr->mingle);
458 gm->hash_bitmap = htonl (bm);
459 gm->query = pr->public_data.query;
460 ext = (GNUNET_HashCode*) &gm[1];
462 if (GNUNET_YES != do_route)
463 GNUNET_PEER_resolve (pr->sender_pid,
464 (struct GNUNET_PeerIdentity*) &ext[k++]);
465 if (GNUNET_BLOCK_TYPE_FS_SBLOCK == pr->public_data.type)
467 &pr->public_data.namespace,
468 sizeof (GNUNET_HashCode));
469 if (GNUNET_YES == pr->public_data.has_target)
470 GNUNET_PEER_resolve (pr->sender_pid,
471 (struct GNUNET_PeerIdentity*) &ext[k++]);
473 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
476 memcpy (buf, gm, msize);
482 * Iterator to free pending requests.
484 * @param cls closure, unused
485 * @param key current key code
486 * @param value value in the hash map (pending request)
487 * @return GNUNET_YES (we should continue to iterate)
490 clean_request (void *cls,
491 const GNUNET_HashCode * key,
494 struct GSF_PendingRequest *pr = value;
496 GNUNET_free_non_null (pr->replies_seen);
498 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
499 GNUNET_PEER_change_rc (pr->sender_pid, -1);
500 if (NULL != pr->hnode)
501 GNUNET_CONTAINER_heap_remove_node (requests_by_expiration_heap,
509 * Explicitly cancel a pending request.
511 * @param pr request to cancel
514 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr)
516 GNUNET_assert (GNUNET_OK ==
517 GNUNET_CONTAINER_multihashmap_remove (pr_map,
518 &pr->public_data.query,
520 GNUNET_assert (GNUNET_YES ==
521 clean_request (NULL, &pr->public_data.query, pr));
526 * Iterate over all pending requests.
528 * @param it function to call for each request
529 * @param cls closure for it
532 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it,
535 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
536 (GNUNET_CONTAINER_HashMapIterator) it,
544 * Closure for "process_reply" function.
546 struct ProcessReplyClosure
549 * The data for the reply.
554 * Who gave us this reply? NULL for local host (or DHT)
556 struct GSF_ConnectedPeer *sender;
559 * When the reply expires.
561 struct GNUNET_TIME_Absolute expiration;
571 enum GNUNET_BLOCK_Type type;
574 * How much was this reply worth to us?
579 * Anonymity requirements for this reply.
581 uint32_t anonymity_level;
584 * Evaluation result (returned).
586 enum GNUNET_BLOCK_EvaluationResult eval;
589 * Did we finish processing the associated request?
594 * Did we find a matching request?
601 * Update the performance data for the sender (if any) since
602 * the sender successfully answered one of our queries.
604 * @param prq information about the sender
605 * @param pr request that was satisfied
608 update_request_performance_data (struct ProcessReplyClosure *prq,
609 struct GSF_PendingRequest *pr)
611 if (prq->sender == NULL)
613 GSF_peer_update_performance_ (prq->sender,
614 pr->public_data.start_time,
620 * We have received a reply; handle it!
622 * @param cls response (struct ProcessReplyClosure)
623 * @param key our query
624 * @param value value in the hash map (info about the query)
625 * @return GNUNET_YES (we should continue to iterate)
628 process_reply (void *cls,
629 const GNUNET_HashCode * key,
632 struct ProcessReplyClosure *prq = cls;
633 struct GSF_PendingRequest *pr = value;
634 GNUNET_HashCode chash;
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Matched result (type %u) for query `%s' with pending request\n",
639 (unsigned int) prq->type,
642 GNUNET_STATISTICS_update (GSF_stats,
643 gettext_noop ("# replies received and matched"),
646 prq->eval = GNUNET_BLOCK_evaluate (GSF_block_ctx,
651 &pr->public_data.namespace,
652 (prq->type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? sizeof (GNUNET_HashCode) : 0,
657 case GNUNET_BLOCK_EVALUATION_OK_MORE:
658 update_request_performance_data (prq, pr);
660 case GNUNET_BLOCK_EVALUATION_OK_LAST:
661 /* short cut: stop processing early, no BF-update, etc. */
662 update_request_performance_data (prq, pr);
663 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
664 GNUNET_TIME_absolute_get_duration (pr->public_data.start_time).rel_value);
665 /* pass on to other peers / local clients */
669 prq->data, prq->size,
671 /* destroy request, we're done */
672 GSF_pending_request_cancel_ (pr);
674 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
675 GNUNET_STATISTICS_update (GSF_stats,
676 gettext_noop ("# duplicate replies discarded (bloomfilter)"),
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Duplicate response `%s', discarding.\n",
682 GNUNET_h2s (&mhash));
684 return GNUNET_YES; /* duplicate */
685 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
686 return GNUNET_YES; /* wrong namespace */
687 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
690 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
693 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 _("Unsupported block type %u\n"),
699 /* update bloomfilter */
700 GNUNET_CRYPTO_hash (prq->data,
703 GSF_pending_request_update_ (pr, &chash, 1);
704 if (NULL == prq->sender)
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Found result for query `%s' in local datastore\n",
711 GNUNET_STATISTICS_update (GSF_stats,
712 gettext_noop ("# results found locally"),
716 prq->priority += pr->public_data.original_priority;
717 pr->public_data.priority = 0;
718 pr->public_data.original_priority = 0;
719 pr->public_data.results_found++;
720 prq->request_found = GNUNET_YES;
721 /* finally, pass on to other peer / local client */
725 prq->data, prq->size,
732 * Continuation called to notify client about result of the
736 * @param success GNUNET_SYSERR on failure
737 * @param msg NULL on success, otherwise an error message
740 put_migration_continuation (void *cls,
744 struct GNUNET_TIME_Absolute *start = cls;
745 struct GNUNET_TIME_Relative delay;
747 delay = GNUNET_TIME_absolute_get_duration (*start);
749 /* FIXME: should we really update the load value on failure? */
750 GNUNET_LOAD_update (datastore_put_load,
752 if (GNUNET_OK == success)
754 GNUNET_STATISTICS_update (GSF_stats,
755 gettext_noop ("# datastore 'put' failures"),
762 * Test if the DATABASE (PUT) load on this peer is too high
763 * to even consider processing the query at
766 * @return GNUNET_YES if the load is too high to do anything (load high)
767 * GNUNET_NO to process normally (load normal or low)
770 test_put_load_too_high (uint32_t priority)
774 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
775 return GNUNET_NO; /* very fast */
776 ld = GNUNET_LOAD_get_load (datastore_put_load);
777 if (ld < 2.0 * (1 + priority))
779 GNUNET_STATISTICS_update (GSF_stats,
780 gettext_noop ("# storage requests dropped due to high load"),
788 * Iterator called on each result obtained for a DHT
789 * operation that expects a reply
792 * @param exp when will this value expire
793 * @param key key of the result
794 * @param get_path NULL-terminated array of pointers
795 * to the peers on reverse GET path (or NULL if not recorded)
796 * @param put_path NULL-terminated array of pointers
797 * to the peers on the PUT path (or NULL if not recorded)
798 * @param type type of the result
799 * @param size number of bytes in data
800 * @param data pointer to the result data
803 GSF_handle_dht_reply_ (void *cls,
804 struct GNUNET_TIME_Absolute exp,
805 const GNUNET_HashCode *key,
806 const struct GNUNET_PeerIdentity * const *get_path,
807 const struct GNUNET_PeerIdentity * const *put_path,
808 enum GNUNET_BLOCK_Type type,
812 struct GSF_PendingRequest *pr = cls;
813 struct ProcessReplyClosure prq;
814 struct GNUNET_TIME_Absolute *start;
816 memset (&prq, 0, sizeof (prq));
818 prq.expiration = exp;
821 process_reply (&prq, key, pr);
822 if ( (GNUNET_YES == active_to_migration) &&
823 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 "Replicating result for query `%s' with priority %u\n",
831 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
832 *start = GNUNET_TIME_absolute_get ();
833 GNUNET_DATASTORE_put (GSF_dsh,
835 type, prq.priority, 1 /* anonymity */,
837 1 + prq.priority, MAX_DATASTORE_QUEUE,
838 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
839 &put_migration_continuation,
846 * Handle P2P "CONTENT" message. Checks that the message is
847 * well-formed and then checks if there are any pending requests for
848 * this content and possibly passes it on (to local clients or other
849 * peers). Does NOT perform migration (content caching at this peer).
851 * @param cp the other peer involved (sender or receiver, NULL
852 * for loopback messages where we are both sender and receiver)
853 * @param message the actual message
854 * @return GNUNET_OK if the message was well-formed,
855 * GNUNET_SYSERR if the message was malformed (close connection,
856 * do not cache under any circumstances)
859 GSF_handle_p2p_content_ (struct GSF_ConnectedPeer *cp,
860 const struct GNUNET_MessageHeader *message)
862 const struct PutMessage *put;
865 enum GNUNET_BLOCK_Type type;
866 struct GNUNET_TIME_Absolute expiration;
867 GNUNET_HashCode query;
868 struct ProcessReplyClosure prq;
869 struct GNUNET_TIME_Relative block_time;
871 struct GNUNET_TIME_Absolute *start;
873 msize = ntohs (message->size);
874 if (msize < sizeof (struct PutMessage))
877 return GNUNET_SYSERR;
879 put = (const struct PutMessage*) message;
880 dsize = msize - sizeof (struct PutMessage);
881 type = ntohl (put->type);
882 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
883 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
884 return GNUNET_SYSERR;
886 GNUNET_BLOCK_get_key (GSF_block_ctx,
893 return GNUNET_SYSERR;
895 /* now, lookup 'query' */
896 prq.data = (const void*) &put[1];
903 prq.expiration = expiration;
905 prq.anonymity_level = 1;
906 prq.finished = GNUNET_NO;
907 prq.request_found = GNUNET_NO;
908 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
914 GSF_connected_peer_change_preference_ (cp, CONTENT_BANDWIDTH_VALUE + 1000 * prq.priority);
915 GSF_get_peer_performance_data_ (cp)->trust += prq.priority;
917 if ( (GNUNET_YES == active_to_migration) &&
918 (GNUNET_NO == test_put_load_too_high (prq.priority)) )
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Replicating result for query `%s' with priority %u\n",
926 start = GNUNET_malloc (sizeof (struct GNUNET_TIME_Absolute));
927 *start = GNUNET_TIME_absolute_get ();
928 GNUNET_DATASTORE_put (GSF_dsh,
929 0, &query, dsize, &put[1],
930 type, prq.priority, 1 /* anonymity */,
932 1 + prq.priority, MAX_DATASTORE_QUEUE,
933 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
934 &put_migration_continuation,
937 putl = GNUNET_LOAD_get_load (datastore_put_load);
938 if ( (NULL != (cp = prq.sender)) &&
939 (GNUNET_NO == prq.request_found) &&
940 ( (GNUNET_YES != active_to_migration) ||
941 (putl > 2.5 * (1 + prq.priority)) ) )
943 if (GNUNET_YES != active_to_migration)
944 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
945 block_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
946 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
947 (unsigned int) (60000 * putl * putl)));
948 GSF_block_peer_migration_ (cp, block_time);
955 * Setup the subsystem.
958 GSF_pending_request_init_ ()
961 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
963 "MAX_PENDING_REQUESTS",
964 &max_pending_requests))
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
967 _("Configuration fails to specify `%s', assuming default value."),
968 "MAX_PENDING_REQUESTS");
970 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024);
971 requests_by_expiration_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
976 * Shutdown the subsystem.
979 GSF_pending_request_done_ ()
981 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
984 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
986 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
987 requests_by_expiration_heap = NULL;
991 /* end of gnunet-service-fs_pr.c */