2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/gnunet-service-fs_pr.c
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_load_lib.h"
29 #include "gnunet-service-fs.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_indexing.h"
32 #include "gnunet-service-fs_pe.h"
33 #include "gnunet-service-fs_pr.h"
34 #include "gnunet-service-fs_cadet.h"
38 * Desired replication level for GETs.
40 #define DHT_GET_REPLICATION 5
43 * Maximum size of the datastore queue for P2P operations. Needs to
44 * be large enough to queue #MAX_QUEUE_PER_PEER operations for roughly
45 * the number of active (connected) peers.
47 #define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
50 * Bandwidth value of a 0-priority content (must be fairly high
51 * compared to query since content is typically significantly larger
52 * -- and more valueable since it can take many queries to get one
55 #define CONTENT_BANDWIDTH_VALUE 800
58 * Hard limit on the number of results we may get from the datastore per query.
60 #define MAX_RESULTS (100 * 1024)
63 * Collect an instane number of statistics? May cause excessive IPC.
65 #define INSANE_STATISTICS GNUNET_NO
68 * If obtaining a block via cadet fails, how often do we retry it before
69 * giving up for good (and sticking to non-anonymous transfer)?
71 #define CADET_RETRY_MAX 3
77 struct GSF_PendingRequest
80 * Public data for the request.
82 struct GSF_PendingRequestData public_data;
85 * Function to call if we encounter a reply.
87 GSF_PendingRequestReplyHandler rh;
95 * Array of hash codes of replies we've already seen.
97 struct GNUNET_HashCode *replies_seen;
100 * Bloomfilter masking replies we've already seen.
102 struct GNUNET_CONTAINER_BloomFilter *bf;
105 * Entry for this pending request in the expiration heap, or NULL.
107 struct GNUNET_CONTAINER_HeapNode *hnode;
110 * Datastore queue entry for this request (or NULL for none).
112 struct GNUNET_DATASTORE_QueueEntry *qe;
115 * DHT request handle for this request (or NULL for none).
117 struct GNUNET_DHT_GetHandle *gh;
120 * Cadet request handle for this request (or NULL for none).
122 struct GSF_CadetRequest *cadet_request;
125 * Function to call upon completion of the local get
126 * request, or NULL for none.
128 GSF_LocalLookupContinuation llc_cont;
131 * Closure for @e llc_cont.
136 * Last result from the local datastore lookup evaluation.
138 enum GNUNET_BLOCK_EvaluationResult local_result;
141 * Identity of the peer that we should use for the 'sender'
142 * (recipient of the response) when forwarding (0 for none).
144 GNUNET_PEER_Id sender_pid;
147 * Identity of the peer that we should never forward this query
148 * to since it originated this query (0 for none).
150 GNUNET_PEER_Id origin_pid;
153 * Time we started the last datastore lookup.
155 struct GNUNET_TIME_Absolute qe_start;
158 * Task that warns us if the local datastore lookup takes too long.
160 struct GNUNET_SCHEDULER_Task * warn_task;
163 * Current offset for querying our local datastore for results.
164 * Starts at a random value, incremented until we get the same
165 * UID again (detected using 'first_uid'), which is then used
166 * to termiante the iteration.
168 uint64_t local_result_offset;
171 * Unique ID of the first result from the local datastore;
172 * used to detect wrap-around of the offset.
177 * How often have we retried this request via 'cadet'?
178 * (used to bound overall retries).
180 unsigned int cadet_retry_count;
183 * Number of valid entries in the 'replies_seen' array.
185 unsigned int replies_seen_count;
188 * Length of the 'replies_seen' array.
190 unsigned int replies_seen_size;
193 * Mingle value we currently use for the bf.
198 * Do we have a first UID yet?
200 unsigned int have_first_uid;
206 * All pending requests, ordered by the query. Entries
207 * are of type 'struct GSF_PendingRequest*'.
209 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
213 * Datastore 'PUT' load tracking.
215 static struct GNUNET_LOAD_Value *datastore_put_load;
219 * Are we allowed to migrate content to this peer.
221 static int active_to_migration;
225 * Heap with the request that will expire next at the top. Contains
226 * pointers of type "struct PendingRequest*"; these will *also* be
227 * aliased from the "requests_by_peer" data structures and the
228 * "requests_by_query" table. Note that requests from our clients
229 * don't expire and are thus NOT in the "requests_by_expiration"
230 * (or the "requests_by_peer" tables).
232 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
236 * Maximum number of requests (from other peers, overall) that we're
237 * willing to have pending at any given point in time. Can be changed
238 * via the configuration file (32k is just the default).
240 static unsigned long long max_pending_requests = (32 * 1024);
245 * Recalculate our bloom filter for filtering replies. This function
246 * will create a new bloom filter from scratch, so it should only be
247 * called if we have no bloomfilter at all (and hence can create a
248 * fresh one of minimal size without problems) OR if our peer is the
249 * initiator (in which case we may resize to larger than mimimum size).
251 * @param pr request for which the BF is to be recomputed
254 refresh_bloomfilter (struct GSF_PendingRequest *pr)
257 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
259 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX);
261 GNUNET_BLOCK_construct_bloomfilter (pr->mingle, pr->replies_seen,
262 pr->replies_seen_count);
267 * Create a new pending request.
269 * @param options request options
270 * @param type type of the block that is being requested
271 * @param query key for the lookup
272 * @param target preferred target for the request, NULL for none
273 * @param bf_data raw data for bloom filter for known replies, can be NULL
274 * @param bf_size number of bytes in @a bf_data
275 * @param mingle mingle value for bf
276 * @param anonymity_level desired anonymity level
277 * @param priority maximum outgoing cummulative request priority to use
278 * @param ttl current time-to-live for the request
279 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
280 * @param origin_pid peer ID of origin of query (do not loop back)
281 * @param replies_seen hash codes of known local replies
282 * @param replies_seen_count size of the @a replies_seen array
283 * @param rh handle to call when we get a reply
284 * @param rh_cls closure for @a rh
285 * @return handle for the new pending request
287 struct GSF_PendingRequest *
288 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
289 enum GNUNET_BLOCK_Type type,
290 const struct GNUNET_HashCode *query,
291 const struct GNUNET_PeerIdentity *target,
295 uint32_t anonymity_level,
298 GNUNET_PEER_Id sender_pid,
299 GNUNET_PEER_Id origin_pid,
300 const struct GNUNET_HashCode *replies_seen,
301 unsigned int replies_seen_count,
302 GSF_PendingRequestReplyHandler rh,
305 struct GSF_PendingRequest *pr;
306 struct GSF_PendingRequest *dpr;
308 struct GNUNET_HashCode *eptr;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Creating request handle for `%s' of type %d\n",
312 GNUNET_h2s (query), type);
313 #if INSANE_STATISTICS
314 GNUNET_STATISTICS_update (GSF_stats,
315 gettext_noop ("# Pending requests created"), 1,
320 extra += sizeof (struct GNUNET_PeerIdentity);
321 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
322 pr->local_result_offset =
323 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
324 pr->public_data.query = *query;
325 eptr = (struct GNUNET_HashCode *) &pr[1];
328 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
331 sizeof (struct GNUNET_PeerIdentity));
333 pr->public_data.anonymity_level = anonymity_level;
334 pr->public_data.priority = priority;
335 pr->public_data.original_priority = priority;
336 pr->public_data.options = options;
337 pr->public_data.type = type;
338 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
339 pr->sender_pid = sender_pid;
340 pr->origin_pid = origin_pid;
343 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
345 pr->public_data.ttl =
346 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
347 (GNUNET_TIME_UNIT_SECONDS,
350 pr->public_data.ttl =
351 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
352 GNUNET_TIME_relative_multiply
353 (GNUNET_TIME_UNIT_SECONDS,
355 if (replies_seen_count > 0)
357 pr->replies_seen_size = replies_seen_count;
359 GNUNET_malloc (sizeof (struct GNUNET_HashCode) * pr->replies_seen_size);
360 GNUNET_memcpy (pr->replies_seen,
362 replies_seen_count * sizeof (struct GNUNET_HashCode));
363 pr->replies_seen_count = replies_seen_count;
368 GNUNET_CONTAINER_bloomfilter_init (bf_data,
370 GNUNET_CONSTANTS_BLOOMFILTER_K);
373 else if ((replies_seen_count > 0) &&
374 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
376 refresh_bloomfilter (pr);
378 GNUNET_CONTAINER_multihashmap_put (pr_map,
379 &pr->public_data.query,
381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
382 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
385 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
387 pr->public_data.ttl.abs_value_us);
388 /* make sure we don't track too many requests */
389 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
390 max_pending_requests)
392 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
393 GNUNET_assert (NULL != dpr);
395 break; /* let the request live briefly... */
397 dpr->rh (dpr->rh_cls,
398 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
401 GNUNET_TIME_UNIT_FOREVER_ABS,
402 GNUNET_TIME_UNIT_FOREVER_ABS,
403 GNUNET_BLOCK_TYPE_ANY,
406 GSF_pending_request_cancel_ (dpr,
410 GNUNET_STATISTICS_update (GSF_stats,
411 gettext_noop ("# Pending requests active"), 1,
417 * Obtain the public data associated with a pending request
419 * @param pr pending request
420 * @return associated public data
422 struct GSF_PendingRequestData *
423 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
425 return &pr->public_data;
430 * Test if two pending requests are compatible (would generate
431 * the same query modulo filters and should thus be processed
434 * @param pra a pending request
435 * @param prb another pending request
436 * @return #GNUNET_OK if the requests are compatible
439 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
440 struct GSF_PendingRequest *prb)
442 if ( (pra->public_data.type != prb->public_data.type) ||
443 (0 != memcmp (&pra->public_data.query,
444 &prb->public_data.query,
445 sizeof (struct GNUNET_HashCode))))
452 * Update a given pending request with additional replies
453 * that have been seen.
455 * @param pr request to update
456 * @param replies_seen hash codes of replies that we've seen
457 * @param replies_seen_count size of the replies_seen array
460 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
461 const struct GNUNET_HashCode * replies_seen,
462 unsigned int replies_seen_count)
465 struct GNUNET_HashCode mhash;
467 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
468 return; /* integer overflow */
469 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
471 /* we're responsible for the BF, full refresh */
472 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
473 GNUNET_array_grow (pr->replies_seen, pr->replies_seen_size,
474 replies_seen_count + pr->replies_seen_count);
475 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count], replies_seen,
476 sizeof (struct GNUNET_HashCode) * replies_seen_count);
477 pr->replies_seen_count += replies_seen_count;
478 refresh_bloomfilter (pr);
484 /* we're not the initiator, but the initiator did not give us
485 * any bloom-filter, so we need to create one on-the-fly */
487 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
490 GNUNET_BLOCK_construct_bloomfilter (pr->mingle,
496 for (i = 0; i < pr->replies_seen_count; i++)
498 GNUNET_BLOCK_mingle_hash (&replies_seen[i],
501 GNUNET_CONTAINER_bloomfilter_add (pr->bf,
507 GNUNET_DHT_get_filter_known_results (pr->gh,
514 * Generate the message corresponding to the given pending request for
515 * transmission to other peers.
517 * @param pr request to generate the message for
518 * @return envelope with the request message
520 struct GNUNET_MQ_Envelope *
521 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
523 struct GNUNET_MQ_Envelope *env;
524 struct GetMessage *gm;
525 struct GNUNET_PeerIdentity *ext;
530 struct GNUNET_TIME_Absolute now;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Building request message for `%s' of type %d\n",
536 GNUNET_h2s (&pr->public_data.query),
537 pr->public_data.type);
540 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
541 if ((!do_route) && (pr->sender_pid == 0))
544 do_route = GNUNET_YES;
548 bm |= GET_MESSAGE_BIT_RETURN_TO;
551 if (NULL != pr->public_data.target)
553 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
556 bf_size = GNUNET_CONTAINER_bloomfilter_get_size (pr->bf);
557 env = GNUNET_MQ_msg_extra (gm,
558 bf_size + k * sizeof (struct GNUNET_PeerIdentity),
559 GNUNET_MESSAGE_TYPE_FS_GET);
560 gm->type = htonl (pr->public_data.type);
563 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
564 pr->public_data.priority + 1);
567 pr->public_data.priority -= prio;
568 pr->public_data.num_transmissions++;
569 pr->public_data.respect_offered += prio;
570 gm->priority = htonl (prio);
571 now = GNUNET_TIME_absolute_get ();
572 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
573 gm->ttl = htonl (ttl / 1000LL / 1000LL);
574 gm->filter_mutator = htonl (pr->mingle);
575 gm->hash_bitmap = htonl (bm);
576 gm->query = pr->public_data.query;
577 ext = (struct GNUNET_PeerIdentity *) &gm[1];
580 GNUNET_PEER_resolve (pr->sender_pid,
582 if (NULL != pr->public_data.target)
583 ext[k++] = *pr->public_data.target;
585 GNUNET_assert (GNUNET_SYSERR !=
586 GNUNET_CONTAINER_bloomfilter_get_raw_data (pr->bf,
594 * Iterator to free pending requests.
596 * @param cls closure, unused
597 * @param key current key code
598 * @param value value in the hash map (pending request)
599 * @return #GNUNET_YES (we should continue to iterate)
602 clean_request (void *cls,
603 const struct GNUNET_HashCode *key,
606 struct GSF_PendingRequest *pr = value;
607 GSF_LocalLookupContinuation cont;
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Cleaning up pending request for `%s'.\n",
612 if (NULL != pr->cadet_request)
614 pr->cadet_retry_count = CADET_RETRY_MAX;
615 GSF_cadet_query_cancel (pr->cadet_request);
616 pr->cadet_request = NULL;
618 if (NULL != (cont = pr->llc_cont))
621 cont (pr->llc_cont_cls,
625 GSF_plan_notify_request_done_ (pr);
626 GNUNET_free_non_null (pr->replies_seen);
629 GNUNET_CONTAINER_bloomfilter_free (pr->bf);
632 GNUNET_PEER_change_rc (pr->sender_pid, -1);
634 GNUNET_PEER_change_rc (pr->origin_pid, -1);
636 if (NULL != pr->hnode)
638 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
643 GNUNET_DATASTORE_cancel (pr->qe);
648 GNUNET_DHT_get_stop (pr->gh);
651 if (NULL != pr->warn_task)
653 GNUNET_SCHEDULER_cancel (pr->warn_task);
654 pr->warn_task = NULL;
656 GNUNET_assert (GNUNET_OK ==
657 GNUNET_CONTAINER_multihashmap_remove (pr_map,
658 &pr->public_data.query,
660 GNUNET_STATISTICS_update (GSF_stats,
661 gettext_noop ("# Pending requests active"),
670 * Explicitly cancel a pending request.
672 * @param pr request to cancel
673 * @param full_cleanup fully purge the request
676 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
679 GSF_LocalLookupContinuation cont;
682 return; /* already cleaned up! */
683 if (GNUNET_NO == full_cleanup)
685 /* make request inactive (we're no longer interested in more results),
686 * but do NOT remove from our data-structures, we still need it there
687 * to prevent the request from looping */
689 if (NULL != pr->cadet_request)
691 pr->cadet_retry_count = CADET_RETRY_MAX;
692 GSF_cadet_query_cancel (pr->cadet_request);
693 pr->cadet_request = NULL;
695 if (NULL != (cont = pr->llc_cont))
698 cont (pr->llc_cont_cls,
702 GSF_plan_notify_request_done_ (pr);
705 GNUNET_DATASTORE_cancel (pr->qe);
710 GNUNET_DHT_get_stop (pr->gh);
713 if (NULL != pr->warn_task)
715 GNUNET_SCHEDULER_cancel (pr->warn_task);
716 pr->warn_task = NULL;
720 GNUNET_assert (GNUNET_YES ==
722 &pr->public_data.query,
728 * Iterate over all pending requests.
730 * @param it function to call for each request
731 * @param cls closure for @a it
734 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
736 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
737 (GNUNET_CONTAINER_HashMapIterator) it,
743 * Closure for process_reply() function.
745 struct ProcessReplyClosure
748 * The data for the reply.
753 * Who gave us this reply? NULL for local host (or DHT)
755 struct GSF_ConnectedPeer *sender;
758 * When the reply expires.
760 struct GNUNET_TIME_Absolute expiration;
770 enum GNUNET_BLOCK_Type type;
773 * Control flags for evaluation.
775 enum GNUNET_BLOCK_EvaluationOptions eo;
778 * How much was this reply worth to us?
783 * Anonymity requirements for this reply.
785 uint32_t anonymity_level;
788 * Evaluation result (returned).
790 enum GNUNET_BLOCK_EvaluationResult eval;
793 * Did we find a matching request?
800 * Update the performance data for the sender (if any) since
801 * the sender successfully answered one of our queries.
803 * @param prq information about the sender
804 * @param pr request that was satisfied
807 update_request_performance_data (struct ProcessReplyClosure *prq,
808 struct GSF_PendingRequest *pr)
810 if (prq->sender == NULL)
812 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
818 * We have received a reply; handle it!
820 * @param cls response (a `struct ProcessReplyClosure`)
821 * @param key our query
822 * @param value value in the hash map (info about the query)
823 * @return #GNUNET_YES (we should continue to iterate)
826 process_reply (void *cls,
827 const struct GNUNET_HashCode *key,
830 struct ProcessReplyClosure *prq = cls;
831 struct GSF_PendingRequest *pr = value;
832 struct GNUNET_HashCode chash;
833 struct GNUNET_TIME_Absolute last_transmission;
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Matched result (type %u) for query `%s' with pending request\n",
839 (unsigned int) prq->type,
841 GNUNET_STATISTICS_update (GSF_stats,
842 gettext_noop ("# replies received and matched"), 1,
845 GNUNET_BLOCK_evaluate (GSF_block_ctx,
857 case GNUNET_BLOCK_EVALUATION_OK_MORE:
858 update_request_performance_data (prq, pr);
860 case GNUNET_BLOCK_EVALUATION_OK_LAST:
861 /* short cut: stop processing early, no BF-update, etc. */
862 update_request_performance_data (prq, pr);
863 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
864 GNUNET_TIME_absolute_get_duration (pr->
865 public_data.start_time).rel_value_us);
867 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
870 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
871 /* pass on to other peers / local clients */
872 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
873 last_transmission, prq->type, prq->data, prq->size);
875 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
876 #if INSANE_STATISTICS
877 GNUNET_STATISTICS_update (GSF_stats,
879 ("# duplicate replies discarded (bloomfilter)"),
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Duplicate response, discarding.\n");
884 return GNUNET_YES; /* duplicate */
885 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
886 GNUNET_STATISTICS_update (GSF_stats,
888 ("# irrelevant replies discarded"),
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Irrelevant response, ignoring.\n");
893 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
894 return GNUNET_YES; /* wrong namespace */
895 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
898 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
901 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
903 _("Unsupported block type %u\n"),
907 /* update bloomfilter */
908 GNUNET_CRYPTO_hash (prq->data,
911 GSF_pending_request_update_ (pr,
914 if (NULL == prq->sender)
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Found result for query `%s' in local datastore\n",
919 GNUNET_STATISTICS_update (GSF_stats,
920 gettext_noop ("# results found locally"),
926 GSF_dht_lookup_ (pr);
928 prq->priority += pr->public_data.original_priority;
929 pr->public_data.priority = 0;
930 pr->public_data.original_priority = 0;
931 pr->public_data.results_found++;
932 prq->request_found = GNUNET_YES;
933 /* finally, pass on to other peer / local client */
934 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
937 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
941 prq->anonymity_level,
952 * Context for put_migration_continuation().
954 struct PutMigrationContext
958 * Start time for the operation.
960 struct GNUNET_TIME_Absolute start;
965 struct GNUNET_PeerIdentity origin;
968 * #GNUNET_YES if we had a matching request for this block,
976 * Continuation called to notify client about result of the
980 * @param success #GNUNET_SYSERR on failure
981 * @param min_expiration minimum expiration time required for content to be stored
982 * @param msg NULL on success, otherwise an error message
985 put_migration_continuation (void *cls, int success,
986 struct GNUNET_TIME_Absolute min_expiration,
989 struct PutMigrationContext *pmc = cls;
990 struct GSF_ConnectedPeer *cp;
991 struct GNUNET_TIME_Relative mig_pause;
992 struct GSF_PeerPerformanceData *ppd;
994 if (NULL != datastore_put_load)
996 if (GNUNET_SYSERR != success)
998 GNUNET_LOAD_update (datastore_put_load,
999 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
1003 /* on queue failure / timeout, increase the put load dramatically */
1004 GNUNET_LOAD_update (datastore_put_load,
1005 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
1008 cp = GSF_peer_get_ (&pmc->origin);
1009 if (GNUNET_OK == success)
1013 ppd = GSF_get_peer_performance_data_ (cp);
1014 ppd->migration_delay.rel_value_us /= 2;
1019 if ( (GNUNET_NO == success) &&
1020 (GNUNET_NO == pmc->requested) &&
1023 ppd = GSF_get_peer_performance_data_ (cp);
1024 if (min_expiration.abs_value_us > 0)
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1027 "Asking to stop migration for %s because datastore is full\n",
1028 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1029 GSF_block_peer_migration_ (cp, min_expiration);
1033 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1034 ppd->migration_delay);
1035 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1036 ppd->migration_delay);
1037 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1038 ppd->migration_delay.rel_value_us);
1039 ppd->migration_delay = GNUNET_TIME_relative_multiply (ppd->migration_delay, 2);
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1041 "Replicated content already exists locally, asking to stop migration for %s\n",
1042 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1044 GSF_block_peer_migration_ (cp,
1045 GNUNET_TIME_relative_to_absolute (mig_pause));
1049 GNUNET_STATISTICS_update (GSF_stats,
1050 gettext_noop ("# Datastore `PUT' failures"), 1,
1056 * Test if the DATABASE (PUT) load on this peer is too high
1057 * to even consider processing the query at
1060 * @param priority the priority of the item
1061 * @return #GNUNET_YES if the load is too high to do anything (load high)
1062 * #GNUNET_NO to process normally (load normal or low)
1065 test_put_load_too_high (uint32_t priority)
1069 if (NULL == datastore_put_load)
1071 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1072 return GNUNET_NO; /* very fast */
1073 ld = GNUNET_LOAD_get_load (datastore_put_load);
1074 if (ld < 2.0 * (1 + priority))
1076 GNUNET_STATISTICS_update (GSF_stats,
1078 ("# storage requests dropped due to high load"), 1,
1085 * Iterator called on each result obtained for a DHT
1086 * operation that expects a reply
1088 * @param cls closure
1089 * @param exp when will this value expire
1090 * @param key key of the result
1091 * @param get_path peers on reply path (or NULL if not recorded)
1092 * @param get_path_length number of entries in @a get_path
1093 * @param put_path peers on the PUT path (or NULL if not recorded)
1094 * @param put_path_length number of entries in @a get_path
1095 * @param type type of the result
1096 * @param size number of bytes in @a data
1097 * @param data pointer to the result data
1100 handle_dht_reply (void *cls,
1101 struct GNUNET_TIME_Absolute exp,
1102 const struct GNUNET_HashCode *key,
1103 const struct GNUNET_PeerIdentity *get_path,
1104 unsigned int get_path_length,
1105 const struct GNUNET_PeerIdentity *put_path,
1106 unsigned int put_path_length,
1107 enum GNUNET_BLOCK_Type type,
1111 struct GSF_PendingRequest *pr = cls;
1112 struct ProcessReplyClosure prq;
1113 struct PutMigrationContext *pmc;
1115 GNUNET_STATISTICS_update (GSF_stats,
1116 gettext_noop ("# Replies received from DHT"), 1,
1118 memset (&prq, 0, sizeof (prq));
1120 prq.expiration = exp;
1121 /* do not allow migrated content to live longer than 1 year */
1122 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1126 prq.eo = GNUNET_BLOCK_EO_NONE;
1127 process_reply (&prq, key, pr);
1128 if ((GNUNET_YES == active_to_migration) &&
1129 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Replicating result for query `%s' with priority %u\n",
1133 GNUNET_h2s (key), prq.priority);
1134 pmc = GNUNET_new (struct PutMigrationContext);
1135 pmc->start = GNUNET_TIME_absolute_get ();
1136 pmc->requested = GNUNET_YES;
1138 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1140 0 /* replication */ ,
1141 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1142 &put_migration_continuation, pmc))
1144 put_migration_continuation (pmc,
1146 GNUNET_TIME_UNIT_ZERO_ABS,
1154 * Consider looking up the data in the DHT (anonymity-level permitting).
1156 * @param pr the pending request to process
1159 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1163 struct GNUNET_PeerIdentity pi;
1164 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1166 if (0 != pr->public_data.anonymity_level)
1170 GNUNET_DHT_get_stop (pr->gh);
1175 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1177 GNUNET_assert (0 != pr->sender_pid);
1178 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1179 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1180 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1183 GNUNET_DHT_get_start (GSF_dht,
1184 pr->public_data.type, &pr->public_data.query,
1185 DHT_GET_REPLICATION,
1186 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1187 xquery, xquery_size, &handle_dht_reply, pr);
1188 if ( (NULL != pr->gh) &&
1189 (0 != pr->replies_seen_count) )
1190 GNUNET_DHT_get_filter_known_results (pr->gh,
1191 pr->replies_seen_count,
1197 * Function called with a reply from the cadet.
1199 * @param cls the pending request struct
1200 * @param type type of the block, ANY on error
1201 * @param expiration expiration time for the block
1202 * @param data_size number of bytes in @a data, 0 on error
1203 * @param data reply block data, NULL on error
1206 cadet_reply_proc (void *cls,
1207 enum GNUNET_BLOCK_Type type,
1208 struct GNUNET_TIME_Absolute expiration,
1212 struct GSF_PendingRequest *pr = cls;
1213 struct ProcessReplyClosure prq;
1214 struct GNUNET_HashCode query;
1216 pr->cadet_request = NULL;
1217 if (GNUNET_BLOCK_TYPE_ANY == type)
1219 GNUNET_break (NULL == data);
1220 GNUNET_break (0 == data_size);
1221 pr->cadet_retry_count++;
1222 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1223 return; /* give up on cadet */
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Error retrieiving block via cadet\n");
1226 /* retry -- without delay, as this is non-anonymous
1227 and cadet/cadet connect will take some time anyway */
1228 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1229 &pr->public_data.query,
1230 pr->public_data.type,
1236 GNUNET_BLOCK_get_key (GSF_block_ctx,
1238 data, data_size, &query))
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Failed to derive key for block of type %d\n",
1243 GNUNET_break_op (0);
1246 GNUNET_STATISTICS_update (GSF_stats,
1247 gettext_noop ("# Replies received from CADET"), 1,
1249 memset (&prq, 0, sizeof (prq));
1251 prq.expiration = expiration;
1252 /* do not allow migrated content to live longer than 1 year */
1253 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1255 prq.size = data_size;
1257 prq.eo = GNUNET_BLOCK_EO_NONE;
1258 process_reply (&prq, &query, pr);
1263 * Consider downloading via cadet (if possible)
1265 * @param pr the pending request to process
1268 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1270 if (0 != pr->public_data.anonymity_level)
1272 if (0 == pr->public_data.target)
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275 "Cannot do cadet-based download, target peer not known\n");
1278 if (NULL != pr->cadet_request)
1280 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1281 &pr->public_data.query,
1282 pr->public_data.type,
1289 * Task that issues a warning if the datastore lookup takes too long.
1291 * @param cls the `struct GSF_PendingRequest`
1294 warn_delay_task (void *cls)
1296 struct GSF_PendingRequest *pr = cls;
1298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1299 _("Datastore lookup already took %s!\n"),
1300 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1303 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1310 * Task that issues a warning if the datastore lookup takes too long.
1312 * @param cls the `struct GSF_PendingRequest`
1315 odc_warn_delay_task (void *cls)
1317 struct GSF_PendingRequest *pr = cls;
1319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1320 _("On-demand lookup already took %s!\n"),
1321 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1323 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1324 &odc_warn_delay_task, pr);
1329 * We're processing (local) results for a search request
1330 * from another peer. Pass applicable results to the
1331 * peer and if we are done either clean up (operation
1332 * complete) or forward to other peers (more results possible).
1334 * @param cls our closure (`struct GSF_PendingRequest *`)
1335 * @param key key for the content
1336 * @param size number of bytes in @a data
1337 * @param data content stored
1338 * @param type type of the content
1339 * @param priority priority of the content
1340 * @param anonymity anonymity-level for the content
1341 * @param expiration expiration time for the content
1342 * @param uid unique identifier for the datum;
1343 * maybe 0 if no unique identifier is available
1346 process_local_reply (void *cls,
1347 const struct GNUNET_HashCode *key,
1350 enum GNUNET_BLOCK_Type type,
1353 struct GNUNET_TIME_Absolute expiration,
1356 struct GSF_PendingRequest *pr = cls;
1357 GSF_LocalLookupContinuation cont;
1358 struct ProcessReplyClosure prq;
1359 struct GNUNET_HashCode query;
1360 unsigned int old_rf;
1362 GNUNET_SCHEDULER_cancel (pr->warn_task);
1363 pr->warn_task = NULL;
1369 #if INSANE_STATISTICS
1370 GNUNET_STATISTICS_update (GSF_stats,
1372 ("# Datastore lookups concluded (no results)"),
1376 if (GNUNET_NO == pr->have_first_uid)
1378 pr->first_uid = uid;
1379 pr->have_first_uid = 1;
1383 if ((uid == pr->first_uid) && (key != NULL))
1385 GNUNET_STATISTICS_update (GSF_stats,
1387 ("# Datastore lookups concluded (seen all)"),
1389 key = NULL; /* all replies seen! */
1391 pr->have_first_uid++;
1392 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1394 GNUNET_STATISTICS_update (GSF_stats,
1396 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1398 key = NULL; /* all replies seen! */
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1405 "No further local responses available.\n");
1406 #if INSANE_STATISTICS
1407 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1408 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1409 GNUNET_STATISTICS_update (GSF_stats,
1411 ("# requested DBLOCK or IBLOCK not found"), 1,
1414 goto check_error_and_continue;
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1418 GNUNET_h2s (key), type, (unsigned long long) uid);
1419 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Found ONDEMAND block, performing on-demand encoding\n");
1423 GNUNET_STATISTICS_update (GSF_stats,
1425 ("# on-demand blocks matched requests"), 1,
1427 pr->qe_start = GNUNET_TIME_absolute_get ();
1429 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1430 &odc_warn_delay_task, pr);
1432 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1433 anonymity, expiration, uid,
1434 &process_local_reply, pr))
1436 GNUNET_STATISTICS_update (GSF_stats,
1438 ("# on-demand lookups performed successfully"),
1440 return; /* we're done */
1442 GNUNET_STATISTICS_update (GSF_stats,
1443 gettext_noop ("# on-demand lookups failed"), 1,
1445 GNUNET_SCHEDULER_cancel (pr->warn_task);
1447 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1448 &warn_delay_task, pr);
1450 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1451 &pr->public_data.query,
1452 pr->public_data.type ==
1453 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1454 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1456 (GSF_PRO_PRIORITY_UNLIMITED &
1457 pr->public_data.options)) ? UINT_MAX : 1
1458 /* queue priority */ ,
1460 (GSF_PRO_PRIORITY_UNLIMITED &
1461 pr->public_data.options)) ? UINT_MAX :
1462 GSF_datastore_queue_size
1463 /* max queue size */ ,
1464 &process_local_reply, pr);
1466 return; /* we're done */
1467 GNUNET_STATISTICS_update (GSF_stats,
1469 ("# Datastore lookups concluded (error queueing)"),
1471 goto check_error_and_continue;
1473 old_rf = pr->public_data.results_found;
1474 memset (&prq, 0, sizeof (prq));
1476 prq.expiration = expiration;
1479 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1482 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1484 pr->qe_start = GNUNET_TIME_absolute_get ();
1486 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1487 &warn_delay_task, pr);
1489 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1490 &pr->public_data.query,
1491 pr->public_data.type ==
1492 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1493 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1495 (GSF_PRO_PRIORITY_UNLIMITED &
1496 pr->public_data.options)) ? UINT_MAX : 1
1497 /* queue priority */ ,
1499 (GSF_PRO_PRIORITY_UNLIMITED &
1500 pr->public_data.options)) ? UINT_MAX :
1501 GSF_datastore_queue_size
1502 /* max queue size */ ,
1503 &process_local_reply, pr);
1506 GNUNET_STATISTICS_update (GSF_stats,
1508 ("# Datastore lookups concluded (error queueing)"),
1510 goto check_error_and_continue;
1515 prq.priority = priority;
1516 prq.request_found = GNUNET_NO;
1517 prq.anonymity_level = anonymity;
1518 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1519 GSF_update_datastore_delay_ (pr->public_data.start_time);
1520 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1521 process_reply (&prq, key, pr);
1522 pr->local_result = prq.eval;
1523 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1525 GNUNET_STATISTICS_update (GSF_stats,
1527 ("# Datastore lookups concluded (found last result)"),
1530 goto check_error_and_continue;
1532 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1533 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1534 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "Load too high, done with request\n");
1538 GNUNET_STATISTICS_update (GSF_stats,
1539 gettext_noop ("# Datastore lookups concluded (load too high)"),
1542 goto check_error_and_continue;
1544 pr->qe_start = GNUNET_TIME_absolute_get ();
1546 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1550 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1551 &pr->public_data.query,
1552 pr->public_data.type ==
1553 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1554 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1556 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1557 public_data.options)) ? UINT_MAX : 1
1558 /* queue priority */ ,
1560 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1561 public_data.options)) ? UINT_MAX :
1562 GSF_datastore_queue_size
1563 /* max queue size */ ,
1564 &process_local_reply, pr);
1565 /* check if we successfully queued another datastore request;
1566 * if so, return, otherwise call our continuation (if we have
1568 check_error_and_continue:
1571 if (NULL != pr->warn_task)
1573 GNUNET_SCHEDULER_cancel (pr->warn_task);
1574 pr->warn_task = NULL;
1576 if (NULL == (cont = pr->llc_cont))
1577 return; /* no continuation */
1578 pr->llc_cont = NULL;
1579 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1581 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1583 /* Signal that we are done and that there won't be any
1584 additional results to allow client to clean up state. */
1586 GNUNET_BLOCK_EVALUATION_OK_LAST,
1589 GNUNET_TIME_UNIT_ZERO_ABS,
1590 GNUNET_TIME_UNIT_FOREVER_ABS,
1591 GNUNET_BLOCK_TYPE_ANY,
1594 /* Finally, call our continuation to signal that we are
1595 done with local processing of this request; i.e. to
1596 start reading again from the client. */
1597 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1601 cont (pr->llc_cont_cls, pr, pr->local_result);
1606 * Is the given target a legitimate peer for forwarding the given request?
1610 * @return #GNUNET_YES if this request could be forwarded to the given peer
1613 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1614 const struct GNUNET_PeerIdentity *target)
1616 struct GNUNET_PeerIdentity pi;
1618 if (0 == pr->origin_pid)
1620 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1622 memcmp (&pi, target,
1623 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1629 * Look up the request in the local datastore.
1631 * @param pr the pending request to process
1632 * @param cont function to call at the end
1633 * @param cont_cls closure for @a cont
1636 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1637 GSF_LocalLookupContinuation cont,
1640 GNUNET_assert (NULL == pr->gh);
1641 GNUNET_assert (NULL == pr->cadet_request);
1642 GNUNET_assert (NULL == pr->llc_cont);
1643 pr->llc_cont = cont;
1644 pr->llc_cont_cls = cont_cls;
1645 pr->qe_start = GNUNET_TIME_absolute_get ();
1647 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1650 #if INSANE_STATISTICS
1651 GNUNET_STATISTICS_update (GSF_stats,
1652 gettext_noop ("# Datastore lookups initiated"), 1,
1656 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1657 &pr->public_data.query,
1658 pr->public_data.type ==
1659 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1660 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1662 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1663 public_data.options)) ? UINT_MAX : 1
1664 /* queue priority */ ,
1666 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1667 public_data.options)) ? UINT_MAX :
1668 GSF_datastore_queue_size
1669 /* max queue size */ ,
1670 &process_local_reply, pr);
1673 GNUNET_STATISTICS_update (GSF_stats,
1675 ("# Datastore lookups concluded (error queueing)"),
1677 GNUNET_SCHEDULER_cancel (pr->warn_task);
1678 pr->warn_task = NULL;
1679 pr->llc_cont = NULL;
1681 cont (cont_cls, pr, pr->local_result);
1687 * Handle P2P "CONTENT" message. Checks that the message is
1688 * well-formed and then checks if there are any pending requests for
1689 * this content and possibly passes it on (to local clients or other
1690 * peers). Does NOT perform migration (content caching at this peer).
1692 * @param cls the other peer involved
1693 * @param put the actual message
1696 handle_p2p_put (void *cls,
1697 const struct PutMessage *put)
1699 struct GSF_ConnectedPeer *cp = cls;
1702 enum GNUNET_BLOCK_Type type;
1703 struct GNUNET_TIME_Absolute expiration;
1704 struct GNUNET_HashCode query;
1705 struct ProcessReplyClosure prq;
1706 struct GNUNET_TIME_Relative block_time;
1708 struct PutMigrationContext *pmc;
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711 "Received P2P PUT from %s\n",
1712 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1713 GSF_cover_content_count++;
1714 msize = ntohs (put->header.size);
1715 dsize = msize - sizeof (struct PutMessage);
1716 type = ntohl (put->type);
1717 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1718 /* do not allow migrated content to live longer than 1 year */
1719 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1722 GNUNET_BLOCK_get_key (GSF_block_ctx,
1728 GNUNET_break_op (0);
1731 GNUNET_STATISTICS_update (GSF_stats,
1732 gettext_noop ("# GAP PUT messages received"),
1735 /* now, lookup 'query' */
1736 prq.data = (const void *) &put[1];
1740 prq.expiration = expiration;
1742 prq.anonymity_level = UINT32_MAX;
1743 prq.request_found = GNUNET_NO;
1744 prq.eo = GNUNET_BLOCK_EO_NONE;
1745 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1751 GSF_connected_peer_change_preference_ (cp,
1752 CONTENT_BANDWIDTH_VALUE +
1753 1000 * prq.priority);
1754 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1756 if ((GNUNET_YES == active_to_migration) &&
1758 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1761 "Replicating result for query `%s' with priority %u\n",
1762 GNUNET_h2s (&query),
1764 pmc = GNUNET_new (struct PutMigrationContext);
1765 pmc->start = GNUNET_TIME_absolute_get ();
1766 pmc->requested = prq.request_found;
1767 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1768 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1771 GNUNET_DATASTORE_put (GSF_dsh,
1779 0 /* replication */ ,
1780 expiration, 1 + prq.priority,
1781 MAX_DATASTORE_QUEUE,
1782 &put_migration_continuation,
1785 put_migration_continuation (pmc,
1787 GNUNET_TIME_UNIT_ZERO_ABS,
1791 else if (NULL != cp)
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Choosing not to keep content `%s' (%d/%d)\n",
1795 GNUNET_h2s (&query),
1796 active_to_migration,
1797 test_put_load_too_high (prq.priority));
1799 putl = GNUNET_LOAD_get_load (datastore_put_load);
1800 if ( (NULL != cp) &&
1801 (GNUNET_NO == prq.request_found) &&
1802 ( (GNUNET_YES != active_to_migration) ||
1803 (putl > 2.5 * (1 + prq.priority)) ) )
1805 if (GNUNET_YES != active_to_migration)
1806 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1808 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1810 GNUNET_CRYPTO_random_u32
1811 (GNUNET_CRYPTO_QUALITY_WEAK,
1812 (unsigned int) (60000 * putl * putl)));
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1814 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1815 GNUNET_STRINGS_relative_time_to_string (block_time,
1818 active_to_migration,
1819 (GNUNET_NO == prq.request_found));
1820 GSF_block_peer_migration_ (cp,
1821 GNUNET_TIME_relative_to_absolute (block_time));
1827 * Check if the given request is still active.
1829 * @param pr pending request
1830 * @return #GNUNET_YES if the request is still active
1833 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1835 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1840 * Setup the subsystem.
1843 GSF_pending_request_init_ ()
1846 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1847 "MAX_PENDING_REQUESTS",
1848 &max_pending_requests))
1850 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1851 "fs", "MAX_PENDING_REQUESTS");
1853 active_to_migration =
1854 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1855 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1856 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1857 requests_by_expiration_heap =
1858 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1863 * Shutdown the subsystem.
1866 GSF_pending_request_done_ ()
1868 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1871 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1873 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1874 requests_by_expiration_heap = NULL;
1875 GNUNET_LOAD_value_free (datastore_put_load);
1876 datastore_put_load = NULL;
1880 /* end of gnunet-service-fs_pr.c */