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 * Block group for filtering replies we've already seen.
102 struct GNUNET_BLOCK_Group *bg;
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 * Do we have a first UID yet?
195 unsigned int have_first_uid;
201 * All pending requests, ordered by the query. Entries
202 * are of type 'struct GSF_PendingRequest*'.
204 static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
208 * Datastore 'PUT' load tracking.
210 static struct GNUNET_LOAD_Value *datastore_put_load;
214 * Are we allowed to migrate content to this peer.
216 static int active_to_migration;
220 * Heap with the request that will expire next at the top. Contains
221 * pointers of type "struct PendingRequest*"; these will *also* be
222 * aliased from the "requests_by_peer" data structures and the
223 * "requests_by_query" table. Note that requests from our clients
224 * don't expire and are thus NOT in the "requests_by_expiration"
225 * (or the "requests_by_peer" tables).
227 static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
231 * Maximum number of requests (from other peers, overall) that we're
232 * willing to have pending at any given point in time. Can be changed
233 * via the configuration file (32k is just the default).
235 static unsigned long long max_pending_requests = (32 * 1024);
240 * Recalculate our bloom filter for filtering replies. This function
241 * will create a new bloom filter from scratch, so it should only be
242 * called if we have no bloomfilter at all (and hence can create a
243 * fresh one of minimal size without problems) OR if our peer is the
244 * initiator (in which case we may resize to larger than mimimum size).
246 * @param type type of the request
247 * @param pr request for which the BF is to be recomputed
250 refresh_bloomfilter (enum GNUNET_BLOCK_Type type,
251 struct GSF_PendingRequest *pr)
255 GNUNET_BLOCK_group_destroy (pr->bg);
258 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
259 return; /* no need */
261 = GNUNET_BLOCK_group_create (GSF_block_ctx,
263 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
268 pr->replies_seen_count,
272 GNUNET_break (GNUNET_OK ==
273 GNUNET_BLOCK_group_set_seen (pr->bg,
275 pr->replies_seen_count));
280 * Create a new pending request.
282 * @param options request options
283 * @param type type of the block that is being requested
284 * @param query key for the lookup
285 * @param target preferred target for the request, NULL for none
286 * @param bf_data raw data for bloom filter for known replies, can be NULL
287 * @param bf_size number of bytes in @a bf_data
288 * @param mingle mingle value for bf
289 * @param anonymity_level desired anonymity level
290 * @param priority maximum outgoing cummulative request priority to use
291 * @param ttl current time-to-live for the request
292 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
293 * @param origin_pid peer ID of origin of query (do not loop back)
294 * @param replies_seen hash codes of known local replies
295 * @param replies_seen_count size of the @a replies_seen array
296 * @param rh handle to call when we get a reply
297 * @param rh_cls closure for @a rh
298 * @return handle for the new pending request
300 struct GSF_PendingRequest *
301 GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
302 enum GNUNET_BLOCK_Type type,
303 const struct GNUNET_HashCode *query,
304 const struct GNUNET_PeerIdentity *target,
308 uint32_t anonymity_level,
311 GNUNET_PEER_Id sender_pid,
312 GNUNET_PEER_Id origin_pid,
313 const struct GNUNET_HashCode *replies_seen,
314 unsigned int replies_seen_count,
315 GSF_PendingRequestReplyHandler rh,
318 struct GSF_PendingRequest *pr;
319 struct GSF_PendingRequest *dpr;
321 struct GNUNET_HashCode *eptr;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Creating request handle for `%s' of type %d\n",
325 GNUNET_h2s (query), type);
326 #if INSANE_STATISTICS
327 GNUNET_STATISTICS_update (GSF_stats,
328 gettext_noop ("# Pending requests created"), 1,
333 extra += sizeof (struct GNUNET_PeerIdentity);
334 pr = GNUNET_malloc (sizeof (struct GSF_PendingRequest) + extra);
335 pr->local_result_offset =
336 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
337 pr->public_data.query = *query;
338 eptr = (struct GNUNET_HashCode *) &pr[1];
341 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
344 sizeof (struct GNUNET_PeerIdentity));
346 pr->public_data.anonymity_level = anonymity_level;
347 pr->public_data.priority = priority;
348 pr->public_data.original_priority = priority;
349 pr->public_data.options = options;
350 pr->public_data.type = type;
351 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
352 pr->sender_pid = sender_pid;
353 pr->origin_pid = origin_pid;
356 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
358 pr->public_data.ttl =
359 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_relative_multiply
360 (GNUNET_TIME_UNIT_SECONDS,
363 pr->public_data.ttl =
364 GNUNET_TIME_absolute_subtract (pr->public_data.start_time,
365 GNUNET_TIME_relative_multiply
366 (GNUNET_TIME_UNIT_SECONDS,
368 if (replies_seen_count > 0)
370 pr->replies_seen_size = replies_seen_count;
371 pr->replies_seen = GNUNET_new_array (pr->replies_seen_size,
372 struct GNUNET_HashCode);
373 GNUNET_memcpy (pr->replies_seen,
375 replies_seen_count * sizeof (struct GNUNET_HashCode));
376 pr->replies_seen_count = replies_seen_count;
378 if ( (NULL != bf_data) &&
379 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type) )
382 = GNUNET_BLOCK_group_create (GSF_block_ctx,
383 pr->public_data.type,
391 else if ((replies_seen_count > 0) &&
392 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
394 refresh_bloomfilter (pr->public_data.type,
397 GNUNET_CONTAINER_multihashmap_put (pr_map,
398 &pr->public_data.query,
400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
401 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
404 GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
406 pr->public_data.ttl.abs_value_us);
407 /* make sure we don't track too many requests */
408 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
409 max_pending_requests)
411 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
412 GNUNET_assert (NULL != dpr);
414 break; /* let the request live briefly... */
416 dpr->rh (dpr->rh_cls,
417 GNUNET_BLOCK_EVALUATION_REQUEST_VALID,
420 GNUNET_TIME_UNIT_FOREVER_ABS,
421 GNUNET_TIME_UNIT_FOREVER_ABS,
422 GNUNET_BLOCK_TYPE_ANY,
425 GSF_pending_request_cancel_ (dpr,
429 GNUNET_STATISTICS_update (GSF_stats,
430 gettext_noop ("# Pending requests active"), 1,
436 * Obtain the public data associated with a pending request
438 * @param pr pending request
439 * @return associated public data
441 struct GSF_PendingRequestData *
442 GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
444 return &pr->public_data;
449 * Test if two pending requests are compatible (would generate
450 * the same query modulo filters and should thus be processed
453 * @param pra a pending request
454 * @param prb another pending request
455 * @return #GNUNET_OK if the requests are compatible
458 GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
459 struct GSF_PendingRequest *prb)
461 if ( (pra->public_data.type != prb->public_data.type) ||
462 (0 != memcmp (&pra->public_data.query,
463 &prb->public_data.query,
464 sizeof (struct GNUNET_HashCode))))
471 * Update a given pending request with additional replies
472 * that have been seen.
474 * @param pr request to update
475 * @param replies_seen hash codes of replies that we've seen
476 * @param replies_seen_count size of the replies_seen array
479 GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
480 const struct GNUNET_HashCode * replies_seen,
481 unsigned int replies_seen_count)
483 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
484 return; /* integer overflow */
485 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
487 /* we're responsible for the BF, full refresh */
488 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
489 GNUNET_array_grow (pr->replies_seen,
490 pr->replies_seen_size,
491 replies_seen_count + pr->replies_seen_count);
492 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
494 sizeof (struct GNUNET_HashCode) * replies_seen_count);
495 pr->replies_seen_count += replies_seen_count;
496 refresh_bloomfilter (pr->public_data.type,
503 /* we're not the initiator, but the initiator did not give us
504 * any bloom-filter, so we need to create one on-the-fly */
505 refresh_bloomfilter (pr->public_data.type,
510 GNUNET_break (GNUNET_OK ==
511 GNUNET_BLOCK_group_set_seen (pr->bg,
513 pr->replies_seen_count));
517 GNUNET_DHT_get_filter_known_results (pr->gh,
524 * Generate the message corresponding to the given pending request for
525 * transmission to other peers.
527 * @param pr request to generate the message for
528 * @return envelope with the request message
530 struct GNUNET_MQ_Envelope *
531 GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
533 struct GNUNET_MQ_Envelope *env;
534 struct GetMessage *gm;
535 struct GNUNET_PeerIdentity *ext;
540 struct GNUNET_TIME_Absolute now;
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Building request message for `%s' of type %d\n",
548 GNUNET_h2s (&pr->public_data.query),
549 pr->public_data.type);
552 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
553 if ((!do_route) && (pr->sender_pid == 0))
556 do_route = GNUNET_YES;
560 bm |= GET_MESSAGE_BIT_RETURN_TO;
563 if (NULL != pr->public_data.target)
565 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
569 GNUNET_BLOCK_group_serialize (pr->bg,
577 env = GNUNET_MQ_msg_extra (gm,
578 bf_size + k * sizeof (struct GNUNET_PeerIdentity),
579 GNUNET_MESSAGE_TYPE_FS_GET);
580 gm->type = htonl (pr->public_data.type);
583 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
584 pr->public_data.priority + 1);
587 pr->public_data.priority -= prio;
588 pr->public_data.num_transmissions++;
589 pr->public_data.respect_offered += prio;
590 gm->priority = htonl (prio);
591 now = GNUNET_TIME_absolute_get ();
592 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
593 gm->ttl = htonl (ttl / 1000LL / 1000LL);
594 gm->filter_mutator = htonl (bf_nonce);
595 gm->hash_bitmap = htonl (bm);
596 gm->query = pr->public_data.query;
597 ext = (struct GNUNET_PeerIdentity *) &gm[1];
600 GNUNET_PEER_resolve (pr->sender_pid,
602 if (NULL != pr->public_data.target)
603 ext[k++] = *pr->public_data.target;
604 GNUNET_memcpy (&ext[k],
607 GNUNET_free_non_null (bf_data);
613 * Iterator to free pending requests.
615 * @param cls closure, unused
616 * @param key current key code
617 * @param value value in the hash map (pending request)
618 * @return #GNUNET_YES (we should continue to iterate)
621 clean_request (void *cls,
622 const struct GNUNET_HashCode *key,
625 struct GSF_PendingRequest *pr = value;
626 GSF_LocalLookupContinuation cont;
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Cleaning up pending request for `%s'.\n",
631 if (NULL != pr->cadet_request)
633 pr->cadet_retry_count = CADET_RETRY_MAX;
634 GSF_cadet_query_cancel (pr->cadet_request);
635 pr->cadet_request = NULL;
637 if (NULL != (cont = pr->llc_cont))
640 cont (pr->llc_cont_cls,
644 GSF_plan_notify_request_done_ (pr);
645 GNUNET_free_non_null (pr->replies_seen);
646 GNUNET_BLOCK_group_destroy (pr->bg);
648 GNUNET_PEER_change_rc (pr->sender_pid, -1);
650 GNUNET_PEER_change_rc (pr->origin_pid, -1);
652 if (NULL != pr->hnode)
654 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
659 GNUNET_DATASTORE_cancel (pr->qe);
664 GNUNET_DHT_get_stop (pr->gh);
667 if (NULL != pr->warn_task)
669 GNUNET_SCHEDULER_cancel (pr->warn_task);
670 pr->warn_task = NULL;
672 GNUNET_assert (GNUNET_OK ==
673 GNUNET_CONTAINER_multihashmap_remove (pr_map,
674 &pr->public_data.query,
676 GNUNET_STATISTICS_update (GSF_stats,
677 gettext_noop ("# Pending requests active"),
686 * Explicitly cancel a pending request.
688 * @param pr request to cancel
689 * @param full_cleanup fully purge the request
692 GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr,
695 GSF_LocalLookupContinuation cont;
698 return; /* already cleaned up! */
699 if (GNUNET_NO == full_cleanup)
701 /* make request inactive (we're no longer interested in more results),
702 * but do NOT remove from our data-structures, we still need it there
703 * to prevent the request from looping */
705 if (NULL != pr->cadet_request)
707 pr->cadet_retry_count = CADET_RETRY_MAX;
708 GSF_cadet_query_cancel (pr->cadet_request);
709 pr->cadet_request = NULL;
711 if (NULL != (cont = pr->llc_cont))
714 cont (pr->llc_cont_cls,
718 GSF_plan_notify_request_done_ (pr);
721 GNUNET_DATASTORE_cancel (pr->qe);
726 GNUNET_DHT_get_stop (pr->gh);
729 if (NULL != pr->warn_task)
731 GNUNET_SCHEDULER_cancel (pr->warn_task);
732 pr->warn_task = NULL;
736 GNUNET_assert (GNUNET_YES ==
738 &pr->public_data.query,
744 * Iterate over all pending requests.
746 * @param it function to call for each request
747 * @param cls closure for @a it
750 GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
752 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
753 (GNUNET_CONTAINER_HashMapIterator) it,
759 * Closure for process_reply() function.
761 struct ProcessReplyClosure
764 * The data for the reply.
769 * Who gave us this reply? NULL for local host (or DHT)
771 struct GSF_ConnectedPeer *sender;
774 * When the reply expires.
776 struct GNUNET_TIME_Absolute expiration;
786 enum GNUNET_BLOCK_Type type;
789 * Control flags for evaluation.
791 enum GNUNET_BLOCK_EvaluationOptions eo;
794 * How much was this reply worth to us?
799 * Anonymity requirements for this reply.
801 uint32_t anonymity_level;
804 * Evaluation result (returned).
806 enum GNUNET_BLOCK_EvaluationResult eval;
809 * Did we find a matching request?
816 * Update the performance data for the sender (if any) since
817 * the sender successfully answered one of our queries.
819 * @param prq information about the sender
820 * @param pr request that was satisfied
823 update_request_performance_data (struct ProcessReplyClosure *prq,
824 struct GSF_PendingRequest *pr)
826 if (prq->sender == NULL)
828 GSF_peer_update_performance_ (prq->sender, pr->public_data.start_time,
834 * We have received a reply; handle it!
836 * @param cls response (a `struct ProcessReplyClosure`)
837 * @param key our query
838 * @param value value in the hash map (info about the query)
839 * @return #GNUNET_YES (we should continue to iterate)
842 process_reply (void *cls,
843 const struct GNUNET_HashCode *key,
846 struct ProcessReplyClosure *prq = cls;
847 struct GSF_PendingRequest *pr = value;
848 struct GNUNET_HashCode chash;
849 struct GNUNET_TIME_Absolute last_transmission;
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Matched result (type %u) for query `%s' with pending request\n",
855 (unsigned int) prq->type,
857 GNUNET_STATISTICS_update (GSF_stats,
858 gettext_noop ("# replies received and matched"), 1,
861 GNUNET_BLOCK_evaluate (GSF_block_ctx,
872 case GNUNET_BLOCK_EVALUATION_OK_MORE:
873 update_request_performance_data (prq, pr);
875 case GNUNET_BLOCK_EVALUATION_OK_LAST:
876 /* short cut: stop processing early, no BF-update, etc. */
877 update_request_performance_data (prq, pr);
878 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
879 GNUNET_TIME_absolute_get_duration (pr->
880 public_data.start_time).rel_value_us);
882 GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
885 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
886 /* pass on to other peers / local clients */
887 pr->rh (pr->rh_cls, prq->eval, pr, prq->anonymity_level, prq->expiration,
888 last_transmission, prq->type, prq->data, prq->size);
890 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
891 #if INSANE_STATISTICS
892 GNUNET_STATISTICS_update (GSF_stats,
894 ("# duplicate replies discarded (bloomfilter)"),
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Duplicate response, discarding.\n");
899 return GNUNET_YES; /* duplicate */
900 case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT:
901 GNUNET_STATISTICS_update (GSF_stats,
903 ("# irrelevant replies discarded"),
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Irrelevant response, ignoring.\n");
908 case GNUNET_BLOCK_EVALUATION_RESULT_INVALID:
909 return GNUNET_YES; /* wrong namespace */
910 case GNUNET_BLOCK_EVALUATION_REQUEST_VALID:
913 case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID:
916 case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED:
917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918 _("Unsupported block type %u\n"),
922 /* update bloomfilter */
923 GNUNET_CRYPTO_hash (prq->data,
926 GSF_pending_request_update_ (pr,
929 if (NULL == prq->sender)
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Found result for query `%s' in local datastore\n",
934 GNUNET_STATISTICS_update (GSF_stats,
935 gettext_noop ("# results found locally"),
941 GSF_dht_lookup_ (pr);
943 prq->priority += pr->public_data.original_priority;
944 pr->public_data.priority = 0;
945 pr->public_data.original_priority = 0;
946 pr->public_data.results_found++;
947 prq->request_found = GNUNET_YES;
948 /* finally, pass on to other peer / local client */
949 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data.pr_head,
952 last_transmission.abs_value_us = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
956 prq->anonymity_level,
967 * Context for put_migration_continuation().
969 struct PutMigrationContext
973 * Start time for the operation.
975 struct GNUNET_TIME_Absolute start;
980 struct GNUNET_PeerIdentity origin;
983 * #GNUNET_YES if we had a matching request for this block,
991 * Continuation called to notify client about result of the
995 * @param success #GNUNET_SYSERR on failure
996 * @param min_expiration minimum expiration time required for content to be stored
997 * @param msg NULL on success, otherwise an error message
1000 put_migration_continuation (void *cls, int success,
1001 struct GNUNET_TIME_Absolute min_expiration,
1004 struct PutMigrationContext *pmc = cls;
1005 struct GSF_ConnectedPeer *cp;
1006 struct GNUNET_TIME_Relative mig_pause;
1007 struct GSF_PeerPerformanceData *ppd;
1009 if (NULL != datastore_put_load)
1011 if (GNUNET_SYSERR != success)
1013 GNUNET_LOAD_update (datastore_put_load,
1014 GNUNET_TIME_absolute_get_duration (pmc->start).rel_value_us);
1018 /* on queue failure / timeout, increase the put load dramatically */
1019 GNUNET_LOAD_update (datastore_put_load,
1020 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
1023 cp = GSF_peer_get_ (&pmc->origin);
1024 if (GNUNET_OK == success)
1028 ppd = GSF_get_peer_performance_data_ (cp);
1029 ppd->migration_delay.rel_value_us /= 2;
1034 if ( (GNUNET_NO == success) &&
1035 (GNUNET_NO == pmc->requested) &&
1038 ppd = GSF_get_peer_performance_data_ (cp);
1039 if (min_expiration.abs_value_us > 0)
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Asking to stop migration for %s because datastore is full\n",
1043 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (min_expiration), GNUNET_YES));
1044 GSF_block_peer_migration_ (cp, min_expiration);
1048 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1049 ppd->migration_delay);
1050 ppd->migration_delay = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS,
1051 ppd->migration_delay);
1052 mig_pause.rel_value_us = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1053 ppd->migration_delay.rel_value_us);
1054 ppd->migration_delay = GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Replicated content already exists locally, asking to stop migration for %s\n",
1057 GNUNET_STRINGS_relative_time_to_string (mig_pause,
1059 GSF_block_peer_migration_ (cp,
1060 GNUNET_TIME_relative_to_absolute (mig_pause));
1064 GNUNET_STATISTICS_update (GSF_stats,
1065 gettext_noop ("# Datastore `PUT' failures"), 1,
1071 * Test if the DATABASE (PUT) load on this peer is too high
1072 * to even consider processing the query at
1075 * @param priority the priority of the item
1076 * @return #GNUNET_YES if the load is too high to do anything (load high)
1077 * #GNUNET_NO to process normally (load normal or low)
1080 test_put_load_too_high (uint32_t priority)
1084 if (NULL == datastore_put_load)
1086 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1087 return GNUNET_NO; /* very fast */
1088 ld = GNUNET_LOAD_get_load (datastore_put_load);
1089 if (ld < 2.0 * (1 + priority))
1091 GNUNET_STATISTICS_update (GSF_stats,
1093 ("# storage requests dropped due to high load"), 1,
1100 * Iterator called on each result obtained for a DHT
1101 * operation that expects a reply
1103 * @param cls closure
1104 * @param exp when will this value expire
1105 * @param key key of the result
1106 * @param get_path peers on reply path (or NULL if not recorded)
1107 * @param get_path_length number of entries in @a get_path
1108 * @param put_path peers on the PUT path (or NULL if not recorded)
1109 * @param put_path_length number of entries in @a get_path
1110 * @param type type of the result
1111 * @param size number of bytes in @a data
1112 * @param data pointer to the result data
1115 handle_dht_reply (void *cls,
1116 struct GNUNET_TIME_Absolute exp,
1117 const struct GNUNET_HashCode *key,
1118 const struct GNUNET_PeerIdentity *get_path,
1119 unsigned int get_path_length,
1120 const struct GNUNET_PeerIdentity *put_path,
1121 unsigned int put_path_length,
1122 enum GNUNET_BLOCK_Type type,
1126 struct GSF_PendingRequest *pr = cls;
1127 struct ProcessReplyClosure prq;
1128 struct PutMigrationContext *pmc;
1130 GNUNET_STATISTICS_update (GSF_stats,
1131 gettext_noop ("# Replies received from DHT"), 1,
1133 memset (&prq, 0, sizeof (prq));
1135 prq.expiration = exp;
1136 /* do not allow migrated content to live longer than 1 year */
1137 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1141 prq.eo = GNUNET_BLOCK_EO_NONE;
1142 process_reply (&prq, key, pr);
1143 if ((GNUNET_YES == active_to_migration) &&
1144 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Replicating result for query `%s' with priority %u\n",
1148 GNUNET_h2s (key), prq.priority);
1149 pmc = GNUNET_new (struct PutMigrationContext);
1150 pmc->start = GNUNET_TIME_absolute_get ();
1151 pmc->requested = GNUNET_YES;
1153 GNUNET_DATASTORE_put (GSF_dsh, 0, key, size, data, type, prq.priority,
1155 0 /* replication */ ,
1156 exp, 1 + prq.priority, MAX_DATASTORE_QUEUE,
1157 &put_migration_continuation, pmc))
1159 put_migration_continuation (pmc,
1161 GNUNET_TIME_UNIT_ZERO_ABS,
1169 * Consider looking up the data in the DHT (anonymity-level permitting).
1171 * @param pr the pending request to process
1174 GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1178 struct GNUNET_PeerIdentity pi;
1179 char buf[sizeof (struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1181 if (0 != pr->public_data.anonymity_level)
1185 GNUNET_DHT_get_stop (pr->gh);
1190 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1192 GNUNET_assert (0 != pr->sender_pid);
1193 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1194 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof (struct GNUNET_PeerIdentity));
1195 xquery_size += sizeof (struct GNUNET_PeerIdentity);
1198 GNUNET_DHT_get_start (GSF_dht,
1199 pr->public_data.type, &pr->public_data.query,
1200 DHT_GET_REPLICATION,
1201 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1202 xquery, xquery_size, &handle_dht_reply, pr);
1203 if ( (NULL != pr->gh) &&
1204 (0 != pr->replies_seen_count) )
1205 GNUNET_DHT_get_filter_known_results (pr->gh,
1206 pr->replies_seen_count,
1212 * Function called with a reply from the cadet.
1214 * @param cls the pending request struct
1215 * @param type type of the block, ANY on error
1216 * @param expiration expiration time for the block
1217 * @param data_size number of bytes in @a data, 0 on error
1218 * @param data reply block data, NULL on error
1221 cadet_reply_proc (void *cls,
1222 enum GNUNET_BLOCK_Type type,
1223 struct GNUNET_TIME_Absolute expiration,
1227 struct GSF_PendingRequest *pr = cls;
1228 struct ProcessReplyClosure prq;
1229 struct GNUNET_HashCode query;
1231 pr->cadet_request = NULL;
1232 if (GNUNET_BLOCK_TYPE_ANY == type)
1234 GNUNET_break (NULL == data);
1235 GNUNET_break (0 == data_size);
1236 pr->cadet_retry_count++;
1237 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1238 return; /* give up on cadet */
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1240 "Error retrieiving block via cadet\n");
1241 /* retry -- without delay, as this is non-anonymous
1242 and cadet/cadet connect will take some time anyway */
1243 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1244 &pr->public_data.query,
1245 pr->public_data.type,
1251 GNUNET_BLOCK_get_key (GSF_block_ctx,
1253 data, data_size, &query))
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Failed to derive key for block of type %d\n",
1258 GNUNET_break_op (0);
1261 GNUNET_STATISTICS_update (GSF_stats,
1262 gettext_noop ("# Replies received from CADET"), 1,
1264 memset (&prq, 0, sizeof (prq));
1266 prq.expiration = expiration;
1267 /* do not allow migrated content to live longer than 1 year */
1268 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1270 prq.size = data_size;
1272 prq.eo = GNUNET_BLOCK_EO_NONE;
1273 process_reply (&prq, &query, pr);
1278 * Consider downloading via cadet (if possible)
1280 * @param pr the pending request to process
1283 GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1285 if (0 != pr->public_data.anonymity_level)
1287 if (0 == pr->public_data.target)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Cannot do cadet-based download, target peer not known\n");
1293 if (NULL != pr->cadet_request)
1295 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1296 &pr->public_data.query,
1297 pr->public_data.type,
1304 * Task that issues a warning if the datastore lookup takes too long.
1306 * @param cls the `struct GSF_PendingRequest`
1309 warn_delay_task (void *cls)
1311 struct GSF_PendingRequest *pr = cls;
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1314 _("Datastore lookup already took %s!\n"),
1315 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start),
1318 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1325 * Task that issues a warning if the datastore lookup takes too long.
1327 * @param cls the `struct GSF_PendingRequest`
1330 odc_warn_delay_task (void *cls)
1332 struct GSF_PendingRequest *pr = cls;
1334 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1335 _("On-demand lookup already took %s!\n"),
1336 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pr->qe_start), GNUNET_YES));
1338 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1339 &odc_warn_delay_task, pr);
1344 * We're processing (local) results for a search request
1345 * from another peer. Pass applicable results to the
1346 * peer and if we are done either clean up (operation
1347 * complete) or forward to other peers (more results possible).
1349 * @param cls our closure (`struct GSF_PendingRequest *`)
1350 * @param key key for the content
1351 * @param size number of bytes in @a data
1352 * @param data content stored
1353 * @param type type of the content
1354 * @param priority priority of the content
1355 * @param anonymity anonymity-level for the content
1356 * @param expiration expiration time for the content
1357 * @param uid unique identifier for the datum;
1358 * maybe 0 if no unique identifier is available
1361 process_local_reply (void *cls,
1362 const struct GNUNET_HashCode *key,
1365 enum GNUNET_BLOCK_Type type,
1368 struct GNUNET_TIME_Absolute expiration,
1371 struct GSF_PendingRequest *pr = cls;
1372 GSF_LocalLookupContinuation cont;
1373 struct ProcessReplyClosure prq;
1374 struct GNUNET_HashCode query;
1375 unsigned int old_rf;
1377 GNUNET_SCHEDULER_cancel (pr->warn_task);
1378 pr->warn_task = NULL;
1384 #if INSANE_STATISTICS
1385 GNUNET_STATISTICS_update (GSF_stats,
1387 ("# Datastore lookups concluded (no results)"),
1391 if (GNUNET_NO == pr->have_first_uid)
1393 pr->first_uid = uid;
1394 pr->have_first_uid = 1;
1398 if ((uid == pr->first_uid) && (key != NULL))
1400 GNUNET_STATISTICS_update (GSF_stats,
1402 ("# Datastore lookups concluded (seen all)"),
1404 key = NULL; /* all replies seen! */
1406 pr->have_first_uid++;
1407 if ((pr->have_first_uid > MAX_RESULTS) && (key != NULL))
1409 GNUNET_STATISTICS_update (GSF_stats,
1411 ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1413 key = NULL; /* all replies seen! */
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1420 "No further local responses available.\n");
1421 #if INSANE_STATISTICS
1422 if ((pr->public_data.type == GNUNET_BLOCK_TYPE_FS_DBLOCK) ||
1423 (pr->public_data.type == GNUNET_BLOCK_TYPE_FS_IBLOCK))
1424 GNUNET_STATISTICS_update (GSF_stats,
1426 ("# requested DBLOCK or IBLOCK not found"), 1,
1429 goto check_error_and_continue;
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1433 GNUNET_h2s (key), type, (unsigned long long) uid);
1434 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1437 "Found ONDEMAND block, performing on-demand encoding\n");
1438 GNUNET_STATISTICS_update (GSF_stats,
1440 ("# on-demand blocks matched requests"), 1,
1442 pr->qe_start = GNUNET_TIME_absolute_get ();
1444 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1445 &odc_warn_delay_task, pr);
1447 GNUNET_FS_handle_on_demand_block (key, size, data, type, priority,
1448 anonymity, expiration, uid,
1449 &process_local_reply, pr))
1451 GNUNET_STATISTICS_update (GSF_stats,
1453 ("# on-demand lookups performed successfully"),
1455 return; /* we're done */
1457 GNUNET_STATISTICS_update (GSF_stats,
1458 gettext_noop ("# on-demand lookups failed"), 1,
1460 GNUNET_SCHEDULER_cancel (pr->warn_task);
1462 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1463 &warn_delay_task, pr);
1465 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1466 &pr->public_data.query,
1467 pr->public_data.type ==
1468 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1469 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1471 (GSF_PRO_PRIORITY_UNLIMITED &
1472 pr->public_data.options)) ? UINT_MAX : 1
1473 /* queue priority */ ,
1475 (GSF_PRO_PRIORITY_UNLIMITED &
1476 pr->public_data.options)) ? UINT_MAX :
1477 GSF_datastore_queue_size
1478 /* max queue size */ ,
1479 &process_local_reply, pr);
1481 return; /* we're done */
1482 GNUNET_STATISTICS_update (GSF_stats,
1484 ("# Datastore lookups concluded (error queueing)"),
1486 goto check_error_and_continue;
1488 old_rf = pr->public_data.results_found;
1489 memset (&prq, 0, sizeof (prq));
1491 prq.expiration = expiration;
1494 GNUNET_BLOCK_get_key (GSF_block_ctx, type, data, size, &query))
1497 GNUNET_DATASTORE_remove (GSF_dsh, key, size, data, -1, -1,
1499 pr->qe_start = GNUNET_TIME_absolute_get ();
1501 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1502 &warn_delay_task, pr);
1504 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset - 1,
1505 &pr->public_data.query,
1506 pr->public_data.type ==
1507 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1508 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1510 (GSF_PRO_PRIORITY_UNLIMITED &
1511 pr->public_data.options)) ? UINT_MAX : 1
1512 /* queue priority */ ,
1514 (GSF_PRO_PRIORITY_UNLIMITED &
1515 pr->public_data.options)) ? UINT_MAX :
1516 GSF_datastore_queue_size
1517 /* max queue size */ ,
1518 &process_local_reply, pr);
1521 GNUNET_STATISTICS_update (GSF_stats,
1523 ("# Datastore lookups concluded (error queueing)"),
1525 goto check_error_and_continue;
1530 prq.priority = priority;
1531 prq.request_found = GNUNET_NO;
1532 prq.anonymity_level = anonymity;
1533 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1534 GSF_update_datastore_delay_ (pr->public_data.start_time);
1535 prq.eo = GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO;
1536 process_reply (&prq, key, pr);
1537 pr->local_result = prq.eval;
1538 if (prq.eval == GNUNET_BLOCK_EVALUATION_OK_LAST)
1540 GNUNET_STATISTICS_update (GSF_stats,
1542 ("# Datastore lookups concluded (found last result)"),
1545 goto check_error_and_continue;
1547 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1548 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1549 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Load too high, done with request\n");
1553 GNUNET_STATISTICS_update (GSF_stats,
1554 gettext_noop ("# Datastore lookups concluded (load too high)"),
1557 goto check_error_and_continue;
1559 pr->qe_start = GNUNET_TIME_absolute_get ();
1561 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1565 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1566 &pr->public_data.query,
1567 pr->public_data.type ==
1568 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1569 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1571 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1572 public_data.options)) ? UINT_MAX : 1
1573 /* queue priority */ ,
1575 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1576 public_data.options)) ? UINT_MAX :
1577 GSF_datastore_queue_size
1578 /* max queue size */ ,
1579 &process_local_reply, pr);
1580 /* check if we successfully queued another datastore request;
1581 * if so, return, otherwise call our continuation (if we have
1583 check_error_and_continue:
1586 if (NULL != pr->warn_task)
1588 GNUNET_SCHEDULER_cancel (pr->warn_task);
1589 pr->warn_task = NULL;
1591 if (NULL == (cont = pr->llc_cont))
1592 return; /* no continuation */
1593 pr->llc_cont = NULL;
1594 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1596 if (GNUNET_BLOCK_EVALUATION_OK_LAST != pr->local_result)
1598 /* Signal that we are done and that there won't be any
1599 additional results to allow client to clean up state. */
1601 GNUNET_BLOCK_EVALUATION_OK_LAST,
1604 GNUNET_TIME_UNIT_ZERO_ABS,
1605 GNUNET_TIME_UNIT_FOREVER_ABS,
1606 GNUNET_BLOCK_TYPE_ANY,
1609 /* Finally, call our continuation to signal that we are
1610 done with local processing of this request; i.e. to
1611 start reading again from the client. */
1612 cont (pr->llc_cont_cls, NULL, GNUNET_BLOCK_EVALUATION_OK_LAST);
1616 cont (pr->llc_cont_cls, pr, pr->local_result);
1621 * Is the given target a legitimate peer for forwarding the given request?
1625 * @return #GNUNET_YES if this request could be forwarded to the given peer
1628 GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1629 const struct GNUNET_PeerIdentity *target)
1631 struct GNUNET_PeerIdentity pi;
1633 if (0 == pr->origin_pid)
1635 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1637 memcmp (&pi, target,
1638 sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_NO :
1644 * Look up the request in the local datastore.
1646 * @param pr the pending request to process
1647 * @param cont function to call at the end
1648 * @param cont_cls closure for @a cont
1651 GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1652 GSF_LocalLookupContinuation cont,
1655 GNUNET_assert (NULL == pr->gh);
1656 GNUNET_assert (NULL == pr->cadet_request);
1657 GNUNET_assert (NULL == pr->llc_cont);
1658 pr->llc_cont = cont;
1659 pr->llc_cont_cls = cont_cls;
1660 pr->qe_start = GNUNET_TIME_absolute_get ();
1662 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1665 #if INSANE_STATISTICS
1666 GNUNET_STATISTICS_update (GSF_stats,
1667 gettext_noop ("# Datastore lookups initiated"), 1,
1671 GNUNET_DATASTORE_get_key (GSF_dsh, pr->local_result_offset++,
1672 &pr->public_data.query,
1673 pr->public_data.type ==
1674 GNUNET_BLOCK_TYPE_FS_DBLOCK ?
1675 GNUNET_BLOCK_TYPE_ANY : pr->public_data.type,
1677 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1678 public_data.options)) ? UINT_MAX : 1
1679 /* queue priority */ ,
1681 (GSF_PRO_PRIORITY_UNLIMITED & pr->
1682 public_data.options)) ? UINT_MAX :
1683 GSF_datastore_queue_size
1684 /* max queue size */ ,
1685 &process_local_reply, pr);
1688 GNUNET_STATISTICS_update (GSF_stats,
1690 ("# Datastore lookups concluded (error queueing)"),
1692 GNUNET_SCHEDULER_cancel (pr->warn_task);
1693 pr->warn_task = NULL;
1694 pr->llc_cont = NULL;
1696 cont (cont_cls, pr, pr->local_result);
1702 * Handle P2P "CONTENT" message. Checks that the message is
1703 * well-formed and then checks if there are any pending requests for
1704 * this content and possibly passes it on (to local clients or other
1705 * peers). Does NOT perform migration (content caching at this peer).
1707 * @param cls the other peer involved
1708 * @param put the actual message
1711 handle_p2p_put (void *cls,
1712 const struct PutMessage *put)
1714 struct GSF_ConnectedPeer *cp = cls;
1717 enum GNUNET_BLOCK_Type type;
1718 struct GNUNET_TIME_Absolute expiration;
1719 struct GNUNET_HashCode query;
1720 struct ProcessReplyClosure prq;
1721 struct GNUNET_TIME_Relative block_time;
1723 struct PutMigrationContext *pmc;
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1726 "Received P2P PUT from %s\n",
1727 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1728 GSF_cover_content_count++;
1729 msize = ntohs (put->header.size);
1730 dsize = msize - sizeof (struct PutMessage);
1731 type = ntohl (put->type);
1732 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1733 /* do not allow migrated content to live longer than 1 year */
1734 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS),
1737 GNUNET_BLOCK_get_key (GSF_block_ctx,
1743 GNUNET_break_op (0);
1746 GNUNET_STATISTICS_update (GSF_stats,
1747 gettext_noop ("# GAP PUT messages received"),
1750 /* now, lookup 'query' */
1751 prq.data = (const void *) &put[1];
1755 prq.expiration = expiration;
1757 prq.anonymity_level = UINT32_MAX;
1758 prq.request_found = GNUNET_NO;
1759 prq.eo = GNUNET_BLOCK_EO_NONE;
1760 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1766 GSF_connected_peer_change_preference_ (cp,
1767 CONTENT_BANDWIDTH_VALUE +
1768 1000 * prq.priority);
1769 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1771 if ((GNUNET_YES == active_to_migration) &&
1773 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Replicating result for query `%s' with priority %u\n",
1777 GNUNET_h2s (&query),
1779 pmc = GNUNET_new (struct PutMigrationContext);
1780 pmc->start = GNUNET_TIME_absolute_get ();
1781 pmc->requested = prq.request_found;
1782 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1783 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1786 GNUNET_DATASTORE_put (GSF_dsh,
1794 0 /* replication */ ,
1795 expiration, 1 + prq.priority,
1796 MAX_DATASTORE_QUEUE,
1797 &put_migration_continuation,
1800 put_migration_continuation (pmc,
1802 GNUNET_TIME_UNIT_ZERO_ABS,
1806 else if (NULL != cp)
1808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809 "Choosing not to keep content `%s' (%d/%d)\n",
1810 GNUNET_h2s (&query),
1811 active_to_migration,
1812 test_put_load_too_high (prq.priority));
1814 putl = GNUNET_LOAD_get_load (datastore_put_load);
1815 if ( (NULL != cp) &&
1816 (GNUNET_NO == prq.request_found) &&
1817 ( (GNUNET_YES != active_to_migration) ||
1818 (putl > 2.5 * (1 + prq.priority)) ) )
1820 if (GNUNET_YES != active_to_migration)
1821 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1823 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1825 GNUNET_CRYPTO_random_u32
1826 (GNUNET_CRYPTO_QUALITY_WEAK,
1827 (unsigned int) (60000 * putl * putl)));
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1830 GNUNET_STRINGS_relative_time_to_string (block_time,
1833 active_to_migration,
1834 (GNUNET_NO == prq.request_found));
1835 GSF_block_peer_migration_ (cp,
1836 GNUNET_TIME_relative_to_absolute (block_time));
1842 * Check if the given request is still active.
1844 * @param pr pending request
1845 * @return #GNUNET_YES if the request is still active
1848 GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1850 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1855 * Setup the subsystem.
1858 GSF_pending_request_init_ ()
1861 GNUNET_CONFIGURATION_get_value_number (GSF_cfg, "fs",
1862 "MAX_PENDING_REQUESTS",
1863 &max_pending_requests))
1865 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1866 "fs", "MAX_PENDING_REQUESTS");
1868 active_to_migration =
1869 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1870 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1871 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1872 requests_by_expiration_heap =
1873 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1878 * Shutdown the subsystem.
1881 GSF_pending_request_done_ ()
1883 GNUNET_CONTAINER_multihashmap_iterate (pr_map,
1886 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1888 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1889 requests_by_expiration_heap = NULL;
1890 GNUNET_LOAD_value_free (datastore_put_load);
1891 datastore_put_load = NULL;
1895 /* end of gnunet-service-fs_pr.c */