aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-service-fs_pr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-service-fs_pr.c')
-rw-r--r--src/fs/gnunet-service-fs_pr.c1919
1 files changed, 0 insertions, 1919 deletions
diff --git a/src/fs/gnunet-service-fs_pr.c b/src/fs/gnunet-service-fs_pr.c
deleted file mode 100644
index f05194907..000000000
--- a/src/fs/gnunet-service-fs_pr.c
+++ /dev/null
@@ -1,1919 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file fs/gnunet-service-fs_pr.c
23 * @brief API to handle pending requests
24 * @author Christian Grothoff
25 */
26#include "platform.h"
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"
35
36
37/**
38 * Desired replication level for GETs.
39 */
40#define DHT_GET_REPLICATION 5
41
42/**
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.
46 */
47#define MAX_DATASTORE_QUEUE (16 * MAX_QUEUE_PER_PEER)
48
49/**
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 valuable since it can take many queries to get one
53 * piece of content).
54 */
55#define CONTENT_BANDWIDTH_VALUE 800
56
57/**
58 * Hard limit on the number of results we may get from the datastore per query.
59 */
60#define MAX_RESULTS (100 * 1024)
61
62/**
63 * Collect an instance number of statistics? May cause excessive IPC.
64 */
65#define INSANE_STATISTICS GNUNET_NO
66
67/**
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)?
70 */
71#define CADET_RETRY_MAX 3
72
73
74/**
75 * An active request.
76 */
77struct GSF_PendingRequest
78{
79 /**
80 * Public data for the request.
81 */
82 struct GSF_PendingRequestData public_data;
83
84 /**
85 * Function to call if we encounter a reply.
86 */
87 GSF_PendingRequestReplyHandler rh;
88
89 /**
90 * Closure for @e rh
91 */
92 void *rh_cls;
93
94 /**
95 * Array of hash codes of replies we've already seen.
96 */
97 struct GNUNET_HashCode *replies_seen;
98
99 /**
100 * Block group for filtering replies we've already seen.
101 */
102 struct GNUNET_BLOCK_Group *bg;
103
104 /**
105 * Entry for this pending request in the expiration heap, or NULL.
106 */
107 struct GNUNET_CONTAINER_HeapNode *hnode;
108
109 /**
110 * Datastore queue entry for this request (or NULL for none).
111 */
112 struct GNUNET_DATASTORE_QueueEntry *qe;
113
114 /**
115 * DHT request handle for this request (or NULL for none).
116 */
117 struct GNUNET_DHT_GetHandle *gh;
118
119 /**
120 * Cadet request handle for this request (or NULL for none).
121 */
122 struct GSF_CadetRequest *cadet_request;
123
124 /**
125 * Function to call upon completion of the local get
126 * request, or NULL for none.
127 */
128 GSF_LocalLookupContinuation llc_cont;
129
130 /**
131 * Closure for @e llc_cont.
132 */
133 void *llc_cont_cls;
134
135 /**
136 * Last result from the local datastore lookup evaluation.
137 */
138 enum GNUNET_BLOCK_ReplyEvaluationResult local_result;
139
140 /**
141 * Identity of the peer that we should use for the 'sender'
142 * (recipient of the response) when forwarding (0 for none).
143 */
144 GNUNET_PEER_Id sender_pid;
145
146 /**
147 * Identity of the peer that we should never forward this query
148 * to since it originated this query (0 for none).
149 */
150 GNUNET_PEER_Id origin_pid;
151
152 /**
153 * Time we started the last datastore lookup.
154 */
155 struct GNUNET_TIME_Absolute qe_start;
156
157 /**
158 * Task that warns us if the local datastore lookup takes too long.
159 */
160 struct GNUNET_SCHEDULER_Task *warn_task;
161
162 /**
163 * Do we have a first UID yet?
164 */
165 bool have_first_uid;
166
167 /**
168 * Have we seen a NULL result yet?
169 */
170 bool seen_null;
171
172 /**
173 * Unique ID of the first result from the local datastore;
174 * used to terminate the loop.
175 */
176 uint64_t first_uid;
177
178 /**
179 * Result count.
180 */
181 size_t result_count;
182
183 /**
184 * How often have we retried this request via 'cadet'?
185 * (used to bound overall retries).
186 */
187 unsigned int cadet_retry_count;
188
189 /**
190 * Number of valid entries in the 'replies_seen' array.
191 */
192 unsigned int replies_seen_count;
193
194 /**
195 * Length of the 'replies_seen' array.
196 */
197 unsigned int replies_seen_size;
198};
199
200
201/**
202 * All pending requests, ordered by the query. Entries
203 * are of type 'struct GSF_PendingRequest*'.
204 */
205static struct GNUNET_CONTAINER_MultiHashMap *pr_map;
206
207
208/**
209 * Datastore 'PUT' load tracking.
210 */
211static struct GNUNET_LOAD_Value *datastore_put_load;
212
213
214/**
215 * Are we allowed to migrate content to this peer.
216 */
217static int active_to_migration;
218
219
220/**
221 * Heap with the request that will expire next at the top. Contains
222 * pointers of type "struct PendingRequest*"; these will *also* be
223 * aliased from the "requests_by_peer" data structures and the
224 * "requests_by_query" table. Note that requests from our clients
225 * don't expire and are thus NOT in the "requests_by_expiration"
226 * (or the "requests_by_peer" tables).
227 */
228static struct GNUNET_CONTAINER_Heap *requests_by_expiration_heap;
229
230
231/**
232 * Maximum number of requests (from other peers, overall) that we're
233 * willing to have pending at any given point in time. Can be changed
234 * via the configuration file (32k is just the default).
235 */
236static unsigned long long max_pending_requests = (32 * 1024);
237
238
239/**
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 minimum size).
245 *
246 * @param type type of the request
247 * @param pr request for which the BF is to be recomputed
248 */
249static void
250refresh_bloomfilter (enum GNUNET_BLOCK_Type type,
251 struct GSF_PendingRequest *pr)
252{
253 if (NULL != pr->bg)
254 {
255 GNUNET_BLOCK_group_destroy (pr->bg);
256 pr->bg = NULL;
257 }
258 if (GNUNET_BLOCK_TYPE_FS_UBLOCK != type)
259 return; /* no need */
260 pr->bg =
261 GNUNET_BLOCK_group_create (GSF_block_ctx,
262 type,
263 NULL,
264 0,
265 "seen-set-size",
266 pr->replies_seen_count,
267 NULL);
268 if (NULL == pr->bg)
269 return;
270 GNUNET_break (GNUNET_OK ==
271 GNUNET_BLOCK_group_set_seen (pr->bg,
272 pr->replies_seen,
273 pr->replies_seen_count));
274}
275
276
277/**
278 * Create a new pending request.
279 *
280 * @param options request options
281 * @param type type of the block that is being requested
282 * @param query key for the lookup
283 * @param target preferred target for the request, NULL for none
284 * @param bf_data raw data for bloom filter for known replies, can be NULL
285 * @param bf_size number of bytes in @a bf_data
286 * @param anonymity_level desired anonymity level
287 * @param priority maximum outgoing cumulative request priority to use
288 * @param ttl current time-to-live for the request
289 * @param sender_pid peer ID to use for the sender when forwarding, 0 for none
290 * @param origin_pid peer ID of origin of query (do not loop back)
291 * @param replies_seen hash codes of known local replies
292 * @param replies_seen_count size of the @a replies_seen array
293 * @param rh handle to call when we get a reply
294 * @param rh_cls closure for @a rh
295 * @return handle for the new pending request
296 */
297struct GSF_PendingRequest *
298GSF_pending_request_create_ (enum GSF_PendingRequestOptions options,
299 enum GNUNET_BLOCK_Type type,
300 const struct GNUNET_HashCode *query,
301 const struct GNUNET_PeerIdentity *target,
302 const char *bf_data,
303 size_t bf_size,
304 uint32_t anonymity_level,
305 uint32_t priority,
306 int32_t ttl,
307 GNUNET_PEER_Id sender_pid,
308 GNUNET_PEER_Id origin_pid,
309 const struct GNUNET_HashCode *replies_seen,
310 unsigned int replies_seen_count,
311 GSF_PendingRequestReplyHandler rh,
312 void *rh_cls)
313{
314 struct GSF_PendingRequest *pr;
315 struct GSF_PendingRequest *dpr;
316 size_t extra;
317 struct GNUNET_HashCode *eptr;
318
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Creating request handle for `%s' of type %d\n",
321 GNUNET_h2s (query),
322 type);
323#if INSANE_STATISTICS
324 GNUNET_STATISTICS_update (GSF_stats,
325 gettext_noop ("# Pending requests created"),
326 1,
327 GNUNET_NO);
328#endif
329 extra = 0;
330 if (NULL != target)
331 extra += sizeof(struct GNUNET_PeerIdentity);
332 pr = GNUNET_malloc (sizeof(struct GSF_PendingRequest) + extra);
333 pr->public_data.query = *query;
334 eptr = (struct GNUNET_HashCode *) &pr[1];
335 if (NULL != target)
336 {
337 pr->public_data.target = (struct GNUNET_PeerIdentity *) eptr;
338 GNUNET_memcpy (eptr, target, sizeof(struct GNUNET_PeerIdentity));
339 }
340 pr->public_data.anonymity_level = anonymity_level;
341 pr->public_data.priority = priority;
342 pr->public_data.original_priority = priority;
343 pr->public_data.options = options;
344 pr->public_data.type = type;
345 pr->public_data.start_time = GNUNET_TIME_absolute_get ();
346 pr->sender_pid = sender_pid;
347 pr->origin_pid = origin_pid;
348 pr->rh = rh;
349 pr->rh_cls = rh_cls;
350 GNUNET_assert ((sender_pid != 0) || (0 == (options & GSF_PRO_FORWARD_ONLY)));
351 if (ttl >= 0)
352 pr->public_data.ttl = GNUNET_TIME_relative_to_absolute (
353 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (uint32_t) ttl));
354 else
355 pr->public_data.ttl = GNUNET_TIME_absolute_subtract (
356 pr->public_data.start_time,
357 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
358 (uint32_t) (-ttl)));
359 if (replies_seen_count > 0)
360 {
361 pr->replies_seen_size = replies_seen_count;
362 pr->replies_seen =
363 GNUNET_new_array (pr->replies_seen_size, struct GNUNET_HashCode);
364 GNUNET_memcpy (pr->replies_seen,
365 replies_seen,
366 replies_seen_count * sizeof(struct GNUNET_HashCode));
367 pr->replies_seen_count = replies_seen_count;
368 }
369 if ((NULL != bf_data) &&
370 (GNUNET_BLOCK_TYPE_FS_UBLOCK == pr->public_data.type))
371 {
372 pr->bg = GNUNET_BLOCK_group_create (GSF_block_ctx,
373 pr->public_data.type,
374 bf_data,
375 bf_size,
376 "seen-set-size",
377 0,
378 NULL);
379 }
380 else if ((replies_seen_count > 0) &&
381 (0 != (options & GSF_PRO_BLOOMFILTER_FULL_REFRESH)))
382 {
383 refresh_bloomfilter (pr->public_data.type, pr);
384 }
385 GNUNET_CONTAINER_multihashmap_put (pr_map,
386 &pr->public_data.query,
387 pr,
388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
389 if (0 == (options & GSF_PRO_REQUEST_NEVER_EXPIRES))
390 {
391 pr->hnode = GNUNET_CONTAINER_heap_insert (requests_by_expiration_heap,
392 pr,
393 pr->public_data.ttl.abs_value_us);
394 /* make sure we don't track too many requests */
395 while (GNUNET_CONTAINER_heap_get_size (requests_by_expiration_heap) >
396 max_pending_requests)
397 {
398 dpr = GNUNET_CONTAINER_heap_peek (requests_by_expiration_heap);
399 GNUNET_assert (NULL != dpr);
400 if (pr == dpr)
401 break; /* let the request live briefly... */
402 if (NULL != dpr->rh)
403 dpr->rh (dpr->rh_cls,
404 GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED,
405 dpr,
406 UINT32_MAX,
407 GNUNET_TIME_UNIT_FOREVER_ABS,
408 GNUNET_TIME_UNIT_FOREVER_ABS,
409 GNUNET_BLOCK_TYPE_ANY,
410 NULL,
411 0);
412 GSF_pending_request_cancel_ (dpr, GNUNET_YES);
413 }
414 }
415 GNUNET_STATISTICS_update (GSF_stats,
416 gettext_noop ("# Pending requests active"),
417 1,
418 GNUNET_NO);
419 return pr;
420}
421
422
423/**
424 * Obtain the public data associated with a pending request
425 *
426 * @param pr pending request
427 * @return associated public data
428 */
429struct GSF_PendingRequestData *
430GSF_pending_request_get_data_ (struct GSF_PendingRequest *pr)
431{
432 return &pr->public_data;
433}
434
435
436/**
437 * Test if two pending requests are compatible (would generate
438 * the same query modulo filters and should thus be processed
439 * jointly).
440 *
441 * @param pra a pending request
442 * @param prb another pending request
443 * @return #GNUNET_OK if the requests are compatible
444 */
445int
446GSF_pending_request_is_compatible_ (struct GSF_PendingRequest *pra,
447 struct GSF_PendingRequest *prb)
448{
449 if ((pra->public_data.type != prb->public_data.type) ||
450 (0 != memcmp (&pra->public_data.query,
451 &prb->public_data.query,
452 sizeof(struct GNUNET_HashCode))))
453 return GNUNET_NO;
454 return GNUNET_OK;
455}
456
457
458/**
459 * Update a given pending request with additional replies
460 * that have been seen.
461 *
462 * @param pr request to update
463 * @param replies_seen hash codes of replies that we've seen
464 * @param replies_seen_count size of the replies_seen array
465 */
466void
467GSF_pending_request_update_ (struct GSF_PendingRequest *pr,
468 const struct GNUNET_HashCode *replies_seen,
469 unsigned int replies_seen_count)
470{
471 if (replies_seen_count + pr->replies_seen_count < pr->replies_seen_count)
472 return; /* integer overflow */
473 if (0 != (pr->public_data.options & GSF_PRO_BLOOMFILTER_FULL_REFRESH))
474 {
475 /* we're responsible for the BF, full refresh */
476 if (replies_seen_count + pr->replies_seen_count > pr->replies_seen_size)
477 GNUNET_array_grow (pr->replies_seen,
478 pr->replies_seen_size,
479 replies_seen_count + pr->replies_seen_count);
480 GNUNET_memcpy (&pr->replies_seen[pr->replies_seen_count],
481 replies_seen,
482 sizeof(struct GNUNET_HashCode) * replies_seen_count);
483 pr->replies_seen_count += replies_seen_count;
484 refresh_bloomfilter (pr->public_data.type, pr);
485 }
486 else
487 {
488 if (NULL == pr->bg)
489 {
490 /* we're not the initiator, but the initiator did not give us
491 * any bloom-filter, so we need to create one on-the-fly */
492 refresh_bloomfilter (pr->public_data.type, pr);
493 }
494 else
495 {
496 GNUNET_break (GNUNET_OK ==
497 GNUNET_BLOCK_group_set_seen (pr->bg,
498 replies_seen,
499 pr->replies_seen_count));
500 }
501 }
502 if (NULL != pr->gh)
503 GNUNET_DHT_get_filter_known_results (pr->gh,
504 replies_seen_count,
505 replies_seen);
506}
507
508
509/**
510 * Generate the message corresponding to the given pending request for
511 * transmission to other peers.
512 *
513 * @param pr request to generate the message for
514 * @return envelope with the request message
515 */
516struct GNUNET_MQ_Envelope *
517GSF_pending_request_get_message_ (struct GSF_PendingRequest *pr)
518{
519 struct GNUNET_MQ_Envelope *env;
520 struct GetMessage *gm;
521 struct GNUNET_PeerIdentity *ext;
522 unsigned int k;
523 uint32_t bm;
524 uint32_t prio;
525 size_t bf_size;
526 struct GNUNET_TIME_Absolute now;
527 int64_t ttl;
528 int do_route;
529 void *bf_data;
530
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Building request message for `%s' of type %d\n",
533 GNUNET_h2s (&pr->public_data.query),
534 pr->public_data.type);
535 k = 0;
536 bm = 0;
537 do_route = (0 == (pr->public_data.options & GSF_PRO_FORWARD_ONLY));
538 if ((! do_route) && (pr->sender_pid == 0))
539 {
540 GNUNET_break (0);
541 do_route = GNUNET_YES;
542 }
543 if (! do_route)
544 {
545 bm |= GET_MESSAGE_BIT_RETURN_TO;
546 k++;
547 }
548 if (NULL != pr->public_data.target)
549 {
550 bm |= GET_MESSAGE_BIT_TRANSMIT_TO;
551 k++;
552 }
553 if (GNUNET_OK !=
554 GNUNET_BLOCK_group_serialize (pr->bg,
555 &bf_data,
556 &bf_size))
557 {
558 bf_size = 0;
559 bf_data = NULL;
560 }
561 env = GNUNET_MQ_msg_extra (gm,
562 bf_size + k * sizeof(struct GNUNET_PeerIdentity),
563 GNUNET_MESSAGE_TYPE_FS_GET);
564 gm->type = htonl (pr->public_data.type);
565 if (do_route)
566 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
567 pr->public_data.priority + 1);
568 else
569 prio = 0;
570 pr->public_data.priority -= prio;
571 pr->public_data.num_transmissions++;
572 pr->public_data.respect_offered += prio;
573 gm->priority = htonl (prio);
574 now = GNUNET_TIME_absolute_get ();
575 ttl = (int64_t) (pr->public_data.ttl.abs_value_us - now.abs_value_us);
576 gm->ttl = htonl (ttl / 1000LL / 1000LL);
577 gm->reserved = htonl (0);
578 gm->hash_bitmap = htonl (bm);
579 gm->query = pr->public_data.query;
580 ext = (struct GNUNET_PeerIdentity *) &gm[1];
581 k = 0;
582 if (! do_route)
583 GNUNET_PEER_resolve (pr->sender_pid, &ext[k++]);
584 if (NULL != pr->public_data.target)
585 ext[k++] = *pr->public_data.target;
586 GNUNET_memcpy (&ext[k], bf_data, bf_size);
587 GNUNET_free (bf_data);
588 return env;
589}
590
591
592/**
593 * Iterator to free pending requests.
594 *
595 * @param cls closure, unused
596 * @param key current key code
597 * @param value value in the hash map (pending request)
598 * @return #GNUNET_YES (we should continue to iterate)
599 */
600static int
601clean_request (void *cls, const struct GNUNET_HashCode *key, void *value)
602{
603 struct GSF_PendingRequest *pr = value;
604 GSF_LocalLookupContinuation cont;
605
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Cleaning up pending request for `%s'.\n",
608 GNUNET_h2s (key));
609 if (NULL != pr->cadet_request)
610 {
611 pr->cadet_retry_count = CADET_RETRY_MAX;
612 GSF_cadet_query_cancel (pr->cadet_request);
613 pr->cadet_request = NULL;
614 }
615 if (NULL != (cont = pr->llc_cont))
616 {
617 pr->llc_cont = NULL;
618 cont (pr->llc_cont_cls,
619 pr,
620 pr->local_result);
621 }
622 GSF_plan_notify_request_done_ (pr);
623 GNUNET_free (pr->replies_seen);
624 GNUNET_BLOCK_group_destroy (pr->bg);
625 pr->bg = NULL;
626 GNUNET_PEER_change_rc (pr->sender_pid, -1);
627 pr->sender_pid = 0;
628 GNUNET_PEER_change_rc (pr->origin_pid, -1);
629 pr->origin_pid = 0;
630 if (NULL != pr->hnode)
631 {
632 GNUNET_CONTAINER_heap_remove_node (pr->hnode);
633 pr->hnode = NULL;
634 }
635 if (NULL != pr->qe)
636 {
637 GNUNET_DATASTORE_cancel (pr->qe);
638 pr->qe = NULL;
639 }
640 if (NULL != pr->gh)
641 {
642 GNUNET_DHT_get_stop (pr->gh);
643 pr->gh = NULL;
644 }
645 if (NULL != pr->warn_task)
646 {
647 GNUNET_SCHEDULER_cancel (pr->warn_task);
648 pr->warn_task = NULL;
649 }
650 GNUNET_assert (
651 GNUNET_OK ==
652 GNUNET_CONTAINER_multihashmap_remove (pr_map, &pr->public_data.query, pr));
653 GNUNET_STATISTICS_update (GSF_stats,
654 gettext_noop ("# Pending requests active"),
655 -1,
656 GNUNET_NO);
657 GNUNET_free (pr);
658 return GNUNET_YES;
659}
660
661
662/**
663 * Explicitly cancel a pending request.
664 *
665 * @param pr request to cancel
666 * @param full_cleanup fully purge the request
667 */
668void
669GSF_pending_request_cancel_ (struct GSF_PendingRequest *pr, int full_cleanup)
670{
671 GSF_LocalLookupContinuation cont;
672
673 if (NULL == pr_map)
674 return; /* already cleaned up! */
675 if (GNUNET_NO == full_cleanup)
676 {
677 /* make request inactive (we're no longer interested in more results),
678 * but do NOT remove from our data-structures, we still need it there
679 * to prevent the request from looping */
680 pr->rh = NULL;
681 if (NULL != pr->cadet_request)
682 {
683 pr->cadet_retry_count = CADET_RETRY_MAX;
684 GSF_cadet_query_cancel (pr->cadet_request);
685 pr->cadet_request = NULL;
686 }
687 if (NULL != (cont = pr->llc_cont))
688 {
689 pr->llc_cont = NULL;
690 cont (pr->llc_cont_cls,
691 pr,
692 pr->local_result);
693 }
694 GSF_plan_notify_request_done_ (pr);
695 if (NULL != pr->qe)
696 {
697 GNUNET_DATASTORE_cancel (pr->qe);
698 pr->qe = NULL;
699 }
700 if (NULL != pr->gh)
701 {
702 GNUNET_DHT_get_stop (pr->gh);
703 pr->gh = NULL;
704 }
705 if (NULL != pr->warn_task)
706 {
707 GNUNET_SCHEDULER_cancel (pr->warn_task);
708 pr->warn_task = NULL;
709 }
710 return;
711 }
712 GNUNET_assert (GNUNET_YES ==
713 clean_request (NULL, &pr->public_data.query, pr));
714}
715
716
717/**
718 * Iterate over all pending requests.
719 *
720 * @param it function to call for each request
721 * @param cls closure for @a it
722 */
723void
724GSF_iterate_pending_requests_ (GSF_PendingRequestIterator it, void *cls)
725{
726 GNUNET_CONTAINER_multihashmap_iterate (
727 pr_map,
728 (GNUNET_CONTAINER_MulitHashMapIteratorCallback) it,
729 cls);
730}
731
732
733/**
734 * Closure for process_reply() function.
735 */
736struct ProcessReplyClosure
737{
738 /**
739 * The data for the reply.
740 */
741 const void *data;
742
743 /**
744 * Who gave us this reply? NULL for local host (or DHT)
745 */
746 struct GSF_ConnectedPeer *sender;
747
748 /**
749 * When the reply expires.
750 */
751 struct GNUNET_TIME_Absolute expiration;
752
753 /**
754 * Size of data.
755 */
756 size_t size;
757
758 /**
759 * Type of the block.
760 */
761 enum GNUNET_BLOCK_Type type;
762
763 /**
764 * How much was this reply worth to us?
765 */
766 uint32_t priority;
767
768 /**
769 * Anonymity requirements for this reply.
770 */
771 uint32_t anonymity_level;
772
773 /**
774 * Evaluation result (returned).
775 */
776 enum GNUNET_BLOCK_ReplyEvaluationResult eval;
777
778 /**
779 * Did we find a matching request?
780 */
781 int request_found;
782};
783
784
785/**
786 * Update the performance data for the sender (if any) since
787 * the sender successfully answered one of our queries.
788 *
789 * @param prq information about the sender
790 * @param pr request that was satisfied
791 */
792static void
793update_request_performance_data (struct ProcessReplyClosure *prq,
794 struct GSF_PendingRequest *pr)
795{
796 if (prq->sender == NULL)
797 return;
798 GSF_peer_update_performance_ (prq->sender,
799 pr->public_data.start_time,
800 prq->priority);
801}
802
803
804/**
805 * We have received a reply; handle it!
806 *
807 * @param cls response (a `struct ProcessReplyClosure`)
808 * @param key our query
809 * @param value value in the hash map (info about the query)
810 * @return #GNUNET_YES (we should continue to iterate)
811 */
812static enum GNUNET_GenericReturnValue
813process_reply (void *cls,
814 const struct GNUNET_HashCode *key,
815 void *value)
816{
817 struct ProcessReplyClosure *prq = cls;
818 struct GSF_PendingRequest *pr = value;
819 struct GNUNET_HashCode chash;
820 struct GNUNET_TIME_Absolute last_transmission;
821
822 if (NULL == pr->rh)
823 return GNUNET_YES;
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Matched result (type %u) for query `%s' with pending request\n",
826 (unsigned int) prq->type,
827 GNUNET_h2s (key));
828 GNUNET_STATISTICS_update (GSF_stats,
829 gettext_noop ("# replies received and matched"),
830 1,
831 GNUNET_NO);
832 prq->eval = GNUNET_BLOCK_check_reply (GSF_block_ctx,
833 prq->type,
834 pr->bg,
835 key,
836 NULL, 0,
837 prq->data,
838 prq->size);
839 switch (prq->eval)
840 {
841 case GNUNET_BLOCK_REPLY_OK_MORE:
842 update_request_performance_data (prq, pr);
843 break;
844 case GNUNET_BLOCK_REPLY_OK_LAST:
845 /* short cut: stop processing early, no BF-update, etc. */
846 update_request_performance_data (prq, pr);
847 GNUNET_LOAD_update (GSF_rt_entry_lifetime,
848 GNUNET_TIME_absolute_get_duration (
849 pr->public_data.start_time)
850 .rel_value_us);
851 if (GNUNET_YES !=
852 GSF_request_plan_reference_get_last_transmission_ (pr->public_data
853 .pr_head,
854 prq->sender,
855 &last_transmission))
856 last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
857 /* pass on to other peers / local clients */
858 pr->rh (pr->rh_cls,
859 prq->eval,
860 pr,
861 prq->anonymity_level,
862 prq->expiration,
863 last_transmission,
864 prq->type,
865 prq->data,
866 prq->size);
867 return GNUNET_YES;
868 case GNUNET_BLOCK_REPLY_OK_DUPLICATE:
869#if INSANE_STATISTICS
870 GNUNET_STATISTICS_update (GSF_stats,
871 "# duplicate replies discarded (bloomfilter)",
872 1,
873 GNUNET_NO);
874#endif
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Duplicate response, discarding.\n");
877 return GNUNET_YES; /* duplicate */
878 case GNUNET_BLOCK_REPLY_IRRELEVANT:
879 GNUNET_STATISTICS_update (GSF_stats,
880 "# irrelevant replies discarded",
881 1,
882 GNUNET_NO);
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Irrelevant response, ignoring.\n");
885 return GNUNET_YES;
886 case GNUNET_BLOCK_REPLY_TYPE_NOT_SUPPORTED:
887 GNUNET_break (0); /* bad installation? */
888 return GNUNET_NO;
889 }
890 /* update bloomfilter */
891 GNUNET_CRYPTO_hash (prq->data,
892 prq->size,
893 &chash);
894 GSF_pending_request_update_ (pr,
895 &chash,
896 1);
897 if (NULL == prq->sender)
898 {
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Found result for query `%s' in local datastore\n",
901 GNUNET_h2s (key));
902 GNUNET_STATISTICS_update (GSF_stats,
903 gettext_noop ("# results found locally"),
904 1,
905 GNUNET_NO);
906 }
907 else
908 {
909 GSF_dht_lookup_ (pr);
910 }
911 prq->priority += pr->public_data.original_priority;
912 pr->public_data.priority = 0;
913 pr->public_data.original_priority = 0;
914 pr->public_data.results_found++;
915 prq->request_found = GNUNET_YES;
916 /* finally, pass on to other peer / local client */
917 if (! GSF_request_plan_reference_get_last_transmission_ (pr->public_data
918 .pr_head,
919 prq->sender,
920 &last_transmission))
921 last_transmission = GNUNET_TIME_UNIT_FOREVER_ABS;
922 pr->rh (pr->rh_cls,
923 prq->eval,
924 pr,
925 prq->anonymity_level,
926 prq->expiration,
927 last_transmission,
928 prq->type,
929 prq->data,
930 prq->size);
931 return GNUNET_YES;
932}
933
934
935/**
936 * Context for put_migration_continuation().
937 */
938struct PutMigrationContext
939{
940 /**
941 * Start time for the operation.
942 */
943 struct GNUNET_TIME_Absolute start;
944
945 /**
946 * Request origin.
947 */
948 struct GNUNET_PeerIdentity origin;
949
950 /**
951 * #GNUNET_YES if we had a matching request for this block,
952 * #GNUNET_NO if not.
953 */
954 int requested;
955};
956
957
958/**
959 * Continuation called to notify client about result of the
960 * operation.
961 *
962 * @param cls closure
963 * @param success #GNUNET_SYSERR on failure
964 * @param min_expiration minimum expiration time required for content to be stored
965 * @param msg NULL on success, otherwise an error message
966 */
967static void
968put_migration_continuation (void *cls,
969 int success,
970 struct GNUNET_TIME_Absolute min_expiration,
971 const char *msg)
972{
973 struct PutMigrationContext *pmc = cls;
974 struct GSF_ConnectedPeer *cp;
975 struct GNUNET_TIME_Relative mig_pause;
976 struct GSF_PeerPerformanceData *ppd;
977
978 if (NULL != datastore_put_load)
979 {
980 if (GNUNET_SYSERR != success)
981 {
982 GNUNET_LOAD_update (datastore_put_load,
983 GNUNET_TIME_absolute_get_duration (pmc->start)
984 .rel_value_us);
985 }
986 else
987 {
988 /* on queue failure / timeout, increase the put load dramatically */
989 GNUNET_LOAD_update (datastore_put_load,
990 GNUNET_TIME_UNIT_MINUTES.rel_value_us);
991 }
992 }
993 cp = GSF_peer_get_ (&pmc->origin);
994 if (GNUNET_OK == success)
995 {
996 if (NULL != cp)
997 {
998 ppd = GSF_get_peer_performance_data_ (cp);
999 ppd->migration_delay.rel_value_us /= 2;
1000 }
1001 GNUNET_free (pmc);
1002 return;
1003 }
1004 if ((GNUNET_NO == success) && (GNUNET_NO == pmc->requested) && (NULL != cp))
1005 {
1006 ppd = GSF_get_peer_performance_data_ (cp);
1007 if (min_expiration.abs_value_us > 0)
1008 {
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Asking to stop migration for %s because datastore is full\n",
1011 GNUNET_STRINGS_relative_time_to_string (
1012 GNUNET_TIME_absolute_get_remaining (min_expiration),
1013 GNUNET_YES));
1014 GSF_block_peer_migration_ (cp, min_expiration);
1015 }
1016 else
1017 {
1018 ppd->migration_delay = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_SECONDS,
1019 ppd->migration_delay);
1020 ppd->migration_delay =
1021 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_HOURS, ppd->migration_delay);
1022 mig_pause.rel_value_us =
1023 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1024 ppd->migration_delay.rel_value_us);
1025 ppd->migration_delay =
1026 GNUNET_TIME_relative_saturating_multiply (ppd->migration_delay, 2);
1027 GNUNET_log (
1028 GNUNET_ERROR_TYPE_DEBUG,
1029 "Replicated content already exists locally, asking to stop migration for %s\n",
1030 GNUNET_STRINGS_relative_time_to_string (mig_pause, GNUNET_YES));
1031 GSF_block_peer_migration_ (cp,
1032 GNUNET_TIME_relative_to_absolute (mig_pause));
1033 }
1034 }
1035 GNUNET_free (pmc);
1036 GNUNET_STATISTICS_update (GSF_stats,
1037 gettext_noop ("# Datastore `PUT' failures"),
1038 1,
1039 GNUNET_NO);
1040}
1041
1042
1043/**
1044 * Test if the DATABASE (PUT) load on this peer is too high
1045 * to even consider processing the query at
1046 * all.
1047 *
1048 * @param priority the priority of the item
1049 * @return #GNUNET_YES if the load is too high to do anything (load high)
1050 * #GNUNET_NO to process normally (load normal or low)
1051 */
1052static int
1053test_put_load_too_high (uint32_t priority)
1054{
1055 double ld;
1056
1057 if (NULL == datastore_put_load)
1058 return GNUNET_NO;
1059 if (GNUNET_LOAD_get_average (datastore_put_load) < 50)
1060 return GNUNET_NO; /* very fast */
1061 ld = GNUNET_LOAD_get_load (datastore_put_load);
1062 if (ld < 2.0 * (1 + priority))
1063 return GNUNET_NO;
1064 GNUNET_STATISTICS_update (GSF_stats,
1065 gettext_noop (
1066 "# storage requests dropped due to high load"),
1067 1,
1068 GNUNET_NO);
1069 return GNUNET_YES;
1070}
1071
1072
1073/**
1074 * Iterator called on each result obtained for a DHT
1075 * operation that expects a reply
1076 *
1077 * @param cls closure
1078 * @param exp when will this value expire
1079 * @param key key of the result
1080 * @param get_path peers on reply path (or NULL if not recorded)
1081 * @param get_path_length number of entries in @a get_path
1082 * @param put_path peers on the PUT path (or NULL if not recorded)
1083 * @param put_path_length number of entries in @a get_path
1084 * @param type type of the result
1085 * @param size number of bytes in @a data
1086 * @param data pointer to the result data
1087 */
1088static void
1089handle_dht_reply (void *cls,
1090 struct GNUNET_TIME_Absolute exp,
1091 const struct GNUNET_HashCode *key,
1092 const struct GNUNET_DHT_PathElement *get_path,
1093 unsigned int get_path_length,
1094 const struct GNUNET_DHT_PathElement *put_path,
1095 unsigned int put_path_length,
1096 enum GNUNET_BLOCK_Type type,
1097 size_t size,
1098 const void *data)
1099{
1100 struct GSF_PendingRequest *pr = cls;
1101 struct ProcessReplyClosure prq;
1102 struct PutMigrationContext *pmc;
1103
1104 GNUNET_STATISTICS_update (GSF_stats,
1105 gettext_noop ("# Replies received from DHT"),
1106 1,
1107 GNUNET_NO);
1108 memset (&prq, 0, sizeof(prq));
1109 prq.data = data;
1110 prq.expiration = exp;
1111 /* do not allow migrated content to live longer than 1 year */
1112 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1113 GNUNET_TIME_UNIT_YEARS),
1114 prq.expiration);
1115 prq.size = size;
1116 prq.type = type;
1117 process_reply (&prq,
1118 key,
1119 pr);
1120 if ((GNUNET_YES == active_to_migration) &&
1121 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1122 {
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Replicating result for query `%s' with priority %u\n",
1125 GNUNET_h2s (key),
1126 prq.priority);
1127 pmc = GNUNET_new (struct PutMigrationContext);
1128 pmc->start = GNUNET_TIME_absolute_get ();
1129 pmc->requested = GNUNET_YES;
1130 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1131 0,
1132 key,
1133 size,
1134 data,
1135 type,
1136 prq.priority,
1137 1 /* anonymity */,
1138 0 /* replication */,
1139 exp,
1140 1 + prq.priority,
1141 MAX_DATASTORE_QUEUE,
1142 &put_migration_continuation,
1143 pmc))
1144 {
1145 put_migration_continuation (pmc,
1146 GNUNET_SYSERR,
1147 GNUNET_TIME_UNIT_ZERO_ABS,
1148 NULL);
1149 }
1150 }
1151}
1152
1153
1154/**
1155 * Consider looking up the data in the DHT (anonymity-level permitting).
1156 *
1157 * @param pr the pending request to process
1158 */
1159void
1160GSF_dht_lookup_ (struct GSF_PendingRequest *pr)
1161{
1162 const void *xquery;
1163 size_t xquery_size;
1164 struct GNUNET_PeerIdentity pi;
1165 char buf[sizeof(struct GNUNET_HashCode) * 2] GNUNET_ALIGN;
1166
1167 if (0 != pr->public_data.anonymity_level)
1168 return;
1169 if (NULL != pr->gh)
1170 {
1171 GNUNET_DHT_get_stop (pr->gh);
1172 pr->gh = NULL;
1173 }
1174 xquery = NULL;
1175 xquery_size = 0;
1176 if (0 != (pr->public_data.options & GSF_PRO_FORWARD_ONLY))
1177 {
1178 GNUNET_assert (0 != pr->sender_pid);
1179 GNUNET_PEER_resolve (pr->sender_pid, &pi);
1180 GNUNET_memcpy (&buf[xquery_size], &pi, sizeof(struct GNUNET_PeerIdentity));
1181 xquery_size += sizeof(struct GNUNET_PeerIdentity);
1182 }
1183 pr->gh = GNUNET_DHT_get_start (GSF_dht,
1184 pr->public_data.type,
1185 &pr->public_data.query,
1186 DHT_GET_REPLICATION,
1187 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1188 xquery,
1189 xquery_size,
1190 &handle_dht_reply,
1191 pr);
1192 if ((NULL != pr->gh) && (0 != pr->replies_seen_count))
1193 GNUNET_DHT_get_filter_known_results (pr->gh,
1194 pr->replies_seen_count,
1195 pr->replies_seen);
1196}
1197
1198
1199/**
1200 * Function called with a reply from the cadet.
1201 *
1202 * @param cls the pending request struct
1203 * @param type type of the block, ANY on error
1204 * @param expiration expiration time for the block
1205 * @param data_size number of bytes in @a data, 0 on error
1206 * @param data reply block data, NULL on error
1207 */
1208static void
1209cadet_reply_proc (void *cls,
1210 enum GNUNET_BLOCK_Type type,
1211 struct GNUNET_TIME_Absolute expiration,
1212 size_t data_size,
1213 const void *data)
1214{
1215 struct GSF_PendingRequest *pr = cls;
1216 struct ProcessReplyClosure prq;
1217 struct GNUNET_HashCode query;
1218
1219 pr->cadet_request = NULL;
1220 if (GNUNET_OK !=
1221 GNUNET_BLOCK_check_block (GSF_block_ctx,
1222 type,
1223 data,
1224 data_size))
1225 {
1226 GNUNET_break_op (0);
1227 return;
1228 }
1229 if (GNUNET_BLOCK_TYPE_ANY == type)
1230 {
1231 GNUNET_break (NULL == data);
1232 GNUNET_break (0 == data_size);
1233 pr->cadet_retry_count++;
1234 if (pr->cadet_retry_count >= CADET_RETRY_MAX)
1235 return; /* give up on cadet */
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error retrieiving block via cadet\n");
1237 /* retry -- without delay, as this is non-anonymous
1238 and cadet/cadet connect will take some time anyway */
1239 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1240 &pr->public_data.query,
1241 pr->public_data.type,
1242 &cadet_reply_proc,
1243 pr);
1244 return;
1245 }
1246 if (GNUNET_YES !=
1247 GNUNET_BLOCK_get_key (GSF_block_ctx,
1248 type,
1249 data,
1250 data_size,
1251 &query))
1252 {
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Failed to derive key for block of type %d\n",
1255 (int) type);
1256 GNUNET_break_op (0);
1257 return;
1258 }
1259 GNUNET_STATISTICS_update (GSF_stats,
1260 gettext_noop ("# Replies received from CADET"),
1261 1,
1262 GNUNET_NO);
1263 memset (&prq, 0, sizeof(prq));
1264 prq.data = data;
1265 prq.expiration = expiration;
1266 /* do not allow migrated content to live longer than 1 year */
1267 prq.expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1268 GNUNET_TIME_UNIT_YEARS),
1269 prq.expiration);
1270 prq.size = data_size;
1271 prq.type = type;
1272 process_reply (&prq,
1273 &query,
1274 pr);
1275}
1276
1277
1278/**
1279 * Consider downloading via cadet (if possible)
1280 *
1281 * @param pr the pending request to process
1282 */
1283void
1284GSF_cadet_lookup_ (struct GSF_PendingRequest *pr)
1285{
1286 if (0 != pr->public_data.anonymity_level)
1287 return;
1288 if (0 == pr->public_data.target)
1289 {
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "Cannot do cadet-based download, target peer not known\n");
1292 return;
1293 }
1294 if (NULL != pr->cadet_request)
1295 return;
1296 pr->cadet_request = GSF_cadet_query (pr->public_data.target,
1297 &pr->public_data.query,
1298 pr->public_data.type,
1299 &cadet_reply_proc,
1300 pr);
1301}
1302
1303
1304/**
1305 * Task that issues a warning if the datastore lookup takes too long.
1306 *
1307 * @param cls the `struct GSF_PendingRequest`
1308 */
1309static void
1310warn_delay_task (void *cls)
1311{
1312 struct GSF_PendingRequest *pr = cls;
1313
1314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1315 _ ("Datastore lookup already took %s!\n"),
1316 GNUNET_STRINGS_relative_time_to_string (
1317 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1318 GNUNET_YES));
1319 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1320 &warn_delay_task,
1321 pr);
1322}
1323
1324
1325/**
1326 * Task that issues a warning if the datastore lookup takes too long.
1327 *
1328 * @param cls the `struct GSF_PendingRequest`
1329 */
1330static void
1331odc_warn_delay_task (void *cls)
1332{
1333 struct GSF_PendingRequest *pr = cls;
1334
1335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1336 _ ("On-demand lookup already took %s!\n"),
1337 GNUNET_STRINGS_relative_time_to_string (
1338 GNUNET_TIME_absolute_get_duration (pr->qe_start),
1339 GNUNET_YES));
1340 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1341 &odc_warn_delay_task,
1342 pr);
1343}
1344
1345
1346/* Call our continuation (if we have any) */
1347static void
1348call_continuation (struct GSF_PendingRequest *pr)
1349{
1350 GSF_LocalLookupContinuation cont = pr->llc_cont;
1351
1352 GNUNET_assert (NULL == pr->qe);
1353 if (NULL != pr->warn_task)
1354 {
1355 GNUNET_SCHEDULER_cancel (pr->warn_task);
1356 pr->warn_task = NULL;
1357 }
1358 if (NULL == cont)
1359 return; /* no continuation */
1360 pr->llc_cont = NULL;
1361 if (0 != (GSF_PRO_LOCAL_ONLY & pr->public_data.options))
1362 {
1363 if (GNUNET_BLOCK_REPLY_OK_LAST != pr->local_result)
1364 {
1365 /* Signal that we are done and that there won't be any
1366 additional results to allow client to clean up state. */
1367 pr->rh (pr->rh_cls,
1368 GNUNET_BLOCK_REPLY_OK_LAST,
1369 pr,
1370 UINT32_MAX,
1371 GNUNET_TIME_UNIT_ZERO_ABS,
1372 GNUNET_TIME_UNIT_FOREVER_ABS,
1373 GNUNET_BLOCK_TYPE_ANY,
1374 NULL,
1375 0);
1376 }
1377 /* Finally, call our continuation to signal that we are
1378 done with local processing of this request; i.e. to
1379 start reading again from the client. */
1380 cont (pr->llc_cont_cls,
1381 NULL,
1382 GNUNET_BLOCK_REPLY_OK_LAST);
1383 return;
1384 }
1385
1386 cont (pr->llc_cont_cls,
1387 pr,
1388 pr->local_result);
1389}
1390
1391
1392/* Update stats and call continuation */
1393static void
1394no_more_local_results (struct GSF_PendingRequest *pr)
1395{
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1397 "No further local responses available.\n");
1398#if INSANE_STATISTICS
1399 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == pr->public_data.type) ||
1400 (GNUNET_BLOCK_TYPE_FS_IBLOCK == pr->public_data.type))
1401 GNUNET_STATISTICS_update (GSF_stats,
1402 gettext_noop (
1403 "# requested DBLOCK or IBLOCK not found"),
1404 1,
1405 GNUNET_NO);
1406#endif
1407 call_continuation (pr);
1408}
1409
1410
1411/* forward declaration */
1412static void
1413process_local_reply (void *cls,
1414 const struct GNUNET_HashCode *key,
1415 size_t size,
1416 const void *data,
1417 enum GNUNET_BLOCK_Type type,
1418 uint32_t priority,
1419 uint32_t anonymity,
1420 uint32_t replication,
1421 struct GNUNET_TIME_Absolute expiration,
1422 uint64_t uid);
1423
1424
1425/* Start a local query */
1426static void
1427start_local_query (struct GSF_PendingRequest *pr,
1428 uint64_t next_uid,
1429 bool random)
1430{
1431 pr->qe_start = GNUNET_TIME_absolute_get ();
1432 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1433 &warn_delay_task,
1434 pr);
1435 pr->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1436 next_uid,
1437 random,
1438 &pr->public_data.query,
1439 pr->public_data.type ==
1440 GNUNET_BLOCK_TYPE_FS_DBLOCK
1441 ? GNUNET_BLOCK_TYPE_ANY
1442 : pr->public_data.type,
1443 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1444 & pr->public_data.options))
1445 ? UINT_MAX
1446 : 1
1447 /* queue priority */,
1448 (0 != (GSF_PRO_PRIORITY_UNLIMITED
1449 & pr->public_data.options))
1450 ? UINT_MAX
1451 : GSF_datastore_queue_size
1452 /* max queue size */,
1453 &process_local_reply,
1454 pr);
1455 if (NULL != pr->qe)
1456 return;
1457 GNUNET_log (
1458 GNUNET_ERROR_TYPE_DEBUG,
1459 "ERROR Requesting `%s' of type %d with next_uid %llu from datastore.\n",
1460 GNUNET_h2s (&pr->public_data.query),
1461 pr->public_data.type,
1462 (unsigned long long) next_uid);
1463 GNUNET_STATISTICS_update (GSF_stats,
1464 gettext_noop (
1465 "# Datastore lookups concluded (error queueing)"),
1466 1,
1467 GNUNET_NO);
1468 call_continuation (pr);
1469}
1470
1471
1472/**
1473 * We're processing (local) results for a search request
1474 * from another peer. Pass applicable results to the
1475 * peer and if we are done either clean up (operation
1476 * complete) or forward to other peers (more results possible).
1477 *
1478 * @param cls our closure (`struct GSF_PendingRequest *`)
1479 * @param key key for the content
1480 * @param size number of bytes in @a data
1481 * @param data content stored
1482 * @param type type of the content
1483 * @param priority priority of the content
1484 * @param anonymity anonymity-level for the content
1485 * @param replication replication-level for the content
1486 * @param expiration expiration time for the content
1487 * @param uid unique identifier for the datum;
1488 * maybe 0 if no unique identifier is available
1489 */
1490static void
1491process_local_reply (void *cls,
1492 const struct GNUNET_HashCode *key,
1493 size_t size,
1494 const void *data,
1495 enum GNUNET_BLOCK_Type type,
1496 uint32_t priority,
1497 uint32_t anonymity,
1498 uint32_t replication,
1499 struct GNUNET_TIME_Absolute expiration,
1500 uint64_t uid)
1501{
1502 struct GSF_PendingRequest *pr = cls;
1503 struct ProcessReplyClosure prq;
1504 struct GNUNET_HashCode query;
1505 unsigned int old_rf;
1506
1507 GNUNET_SCHEDULER_cancel (pr->warn_task);
1508 pr->warn_task = NULL;
1509 if (NULL == pr->qe)
1510 goto called_from_on_demand;
1511 pr->qe = NULL;
1512 if (
1513 (NULL == key) && pr->seen_null &&
1514 ! pr->have_first_uid) /* We have hit the end for the 2nd time with no results */
1515 {
1516 /* No results */
1517#if INSANE_STATISTICS
1518 GNUNET_STATISTICS_update (GSF_stats,
1519 gettext_noop (
1520 "# Datastore lookups concluded (no results)"),
1521 1,
1522 GNUNET_NO);
1523#endif
1524 no_more_local_results (pr);
1525 return;
1526 }
1527 if (((NULL == key) &&
1528 pr->seen_null) || /* We have hit the end for the 2nd time OR */
1529 (pr->seen_null && pr->have_first_uid &&
1530 (uid >= pr->first_uid))) /* We have hit the end and past first UID */
1531 {
1532 /* Seen all results */
1533 GNUNET_STATISTICS_update (GSF_stats,
1534 gettext_noop (
1535 "# Datastore lookups concluded (seen all)"),
1536 1,
1537 GNUNET_NO);
1538 no_more_local_results (pr);
1539 return;
1540 }
1541 if (NULL == key)
1542 {
1543 GNUNET_assert (! pr->seen_null);
1544 pr->seen_null = true;
1545 start_local_query (pr, 0 /* next_uid */, false /* random */);
1546 return;
1547 }
1548 if (! pr->have_first_uid)
1549 {
1550 pr->first_uid = uid;
1551 pr->have_first_uid = true;
1552 }
1553 pr->result_count++;
1554 if (pr->result_count > MAX_RESULTS)
1555 {
1556 GNUNET_STATISTICS_update (
1557 GSF_stats,
1558 gettext_noop ("# Datastore lookups aborted (more than MAX_RESULTS)"),
1559 1,
1560 GNUNET_NO);
1561 no_more_local_results (pr);
1562 return;
1563 }
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Received reply for `%s' of type %d with UID %llu from datastore.\n",
1566 GNUNET_h2s (key),
1567 type,
1568 (unsigned long long) uid);
1569 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1570 {
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Found ONDEMAND block, performing on-demand encoding\n");
1573 GNUNET_STATISTICS_update (GSF_stats,
1574 gettext_noop (
1575 "# on-demand blocks matched requests"),
1576 1,
1577 GNUNET_NO);
1578 pr->qe_start = GNUNET_TIME_absolute_get ();
1579 pr->warn_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
1580 &odc_warn_delay_task,
1581 pr);
1582 if (GNUNET_OK == GNUNET_FS_handle_on_demand_block (key,
1583 size,
1584 data,
1585 type,
1586 priority,
1587 anonymity,
1588 replication,
1589 expiration,
1590 uid,
1591 &process_local_reply,
1592 pr))
1593 {
1594 GNUNET_STATISTICS_update (GSF_stats,
1595 gettext_noop (
1596 "# on-demand lookups performed successfully"),
1597 1,
1598 GNUNET_NO);
1599 return; /* we're done */
1600 }
1601 GNUNET_STATISTICS_update (GSF_stats,
1602 gettext_noop ("# on-demand lookups failed"),
1603 1,
1604 GNUNET_NO);
1605 GNUNET_SCHEDULER_cancel (pr->warn_task);
1606 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1607 return;
1608 }
1609called_from_on_demand:
1610 old_rf = pr->public_data.results_found;
1611 memset (&prq, 0, sizeof(prq));
1612 prq.data = data;
1613 prq.expiration = expiration;
1614 prq.size = size;
1615 if (GNUNET_OK !=
1616 GNUNET_BLOCK_get_key (GSF_block_ctx,
1617 type,
1618 data,
1619 size,
1620 &query))
1621 {
1622 GNUNET_break (0);
1623 GNUNET_DATASTORE_remove (GSF_dsh,
1624 key,
1625 size,
1626 data,
1627 UINT_MAX,
1628 UINT_MAX,
1629 NULL,
1630 NULL);
1631 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1632 return;
1633 }
1634 prq.type = type;
1635 prq.priority = priority;
1636 prq.request_found = GNUNET_NO;
1637 prq.anonymity_level = anonymity;
1638 if ((0 == old_rf) && (0 == pr->public_data.results_found))
1639 GSF_update_datastore_delay_ (pr->public_data.start_time);
1640 process_reply (&prq,
1641 key,
1642 pr);
1643 pr->local_result = prq.eval;
1644 if (GNUNET_BLOCK_REPLY_OK_LAST == prq.eval)
1645 {
1646 GNUNET_STATISTICS_update (
1647 GSF_stats,
1648 gettext_noop ("# Datastore lookups concluded (found last result)"),
1649 1,
1650 GNUNET_NO);
1651 call_continuation (pr);
1652 return;
1653 }
1654 if ((0 == (GSF_PRO_PRIORITY_UNLIMITED & pr->public_data.options)) &&
1655 ((GNUNET_YES == GSF_test_get_load_too_high_ (0)) ||
1656 (pr->public_data.results_found > 5 + 2 * pr->public_data.priority)))
1657 {
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load too high, done with request\n");
1659 GNUNET_STATISTICS_update (GSF_stats,
1660 gettext_noop (
1661 "# Datastore lookups concluded (load too high)"),
1662 1,
1663 GNUNET_NO);
1664 call_continuation (pr);
1665 return;
1666 }
1667 start_local_query (pr, uid + 1 /* next_uid */, false /* random */);
1668}
1669
1670
1671/**
1672 * Is the given target a legitimate peer for forwarding the given request?
1673 *
1674 * @param pr request
1675 * @param target
1676 * @return #GNUNET_YES if this request could be forwarded to the given peer
1677 */
1678int
1679GSF_pending_request_test_target_ (struct GSF_PendingRequest *pr,
1680 const struct GNUNET_PeerIdentity *target)
1681{
1682 struct GNUNET_PeerIdentity pi;
1683
1684 if (0 == pr->origin_pid)
1685 return GNUNET_YES;
1686 GNUNET_PEER_resolve (pr->origin_pid, &pi);
1687 return (0 == memcmp (&pi, target, sizeof(struct GNUNET_PeerIdentity)))
1688 ? GNUNET_NO
1689 : GNUNET_YES;
1690}
1691
1692
1693/**
1694 * Look up the request in the local datastore.
1695 *
1696 * @param pr the pending request to process
1697 * @param cont function to call at the end
1698 * @param cont_cls closure for @a cont
1699 */
1700void
1701GSF_local_lookup_ (struct GSF_PendingRequest *pr,
1702 GSF_LocalLookupContinuation cont,
1703 void *cont_cls)
1704{
1705 GNUNET_assert (NULL == pr->gh);
1706 GNUNET_assert (NULL == pr->cadet_request);
1707 GNUNET_assert (NULL == pr->llc_cont);
1708 pr->llc_cont = cont;
1709 pr->llc_cont_cls = cont_cls;
1710#if INSANE_STATISTICS
1711 GNUNET_STATISTICS_update (GSF_stats,
1712 gettext_noop ("# Datastore lookups initiated"),
1713 1,
1714 GNUNET_NO);
1715#endif
1716 start_local_query (pr, 0 /* next_uid */, true /* random */);
1717}
1718
1719
1720/**
1721 * Handle P2P "CONTENT" message. Checks that the message is
1722 * well-formed and then checks if there are any pending requests for
1723 * this content and possibly passes it on (to local clients or other
1724 * peers). Does NOT perform migration (content caching at this peer).
1725 *
1726 * @param cls the other peer involved
1727 * @param put the actual message
1728 */
1729void
1730handle_p2p_put (void *cls,
1731 const struct PutMessage *put)
1732{
1733 struct GSF_ConnectedPeer *cp = cls;
1734 uint16_t msize;
1735 size_t dsize;
1736 enum GNUNET_BLOCK_Type type;
1737 struct GNUNET_TIME_Absolute expiration;
1738 struct GNUNET_HashCode query;
1739 struct ProcessReplyClosure prq;
1740 struct GNUNET_TIME_Relative block_time;
1741 double putl;
1742 struct PutMigrationContext *pmc;
1743
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1745 "Received P2P PUT from %s\n",
1746 GNUNET_i2s (GSF_get_peer_performance_data_ (cp)->peer));
1747 GSF_cover_content_count++;
1748 msize = ntohs (put->header.size);
1749 dsize = msize - sizeof(struct PutMessage);
1750 type = ntohl (put->type);
1751 expiration = GNUNET_TIME_absolute_ntoh (put->expiration);
1752 /* do not allow migrated content to live longer than 1 year */
1753 expiration = GNUNET_TIME_absolute_min (GNUNET_TIME_relative_to_absolute (
1754 GNUNET_TIME_UNIT_YEARS),
1755 expiration);
1756 if (GNUNET_OK !=
1757 GNUNET_BLOCK_check_block (GSF_block_ctx,
1758 type,
1759 &put[1],
1760 dsize))
1761 {
1762 GNUNET_break_op (0);
1763 return;
1764 }
1765 if (GNUNET_OK !=
1766 GNUNET_BLOCK_get_key (GSF_block_ctx,
1767 type,
1768 &put[1],
1769 dsize,
1770 &query))
1771 {
1772 GNUNET_break_op (0);
1773 return;
1774 }
1775 GNUNET_STATISTICS_update (GSF_stats,
1776 gettext_noop ("# GAP PUT messages received"),
1777 1,
1778 GNUNET_NO);
1779 /* now, lookup 'query' */
1780 prq.data = (const void *) &put[1];
1781 prq.sender = cp;
1782 prq.size = dsize;
1783 prq.type = type;
1784 prq.expiration = expiration;
1785 prq.priority = 0;
1786 prq.anonymity_level = UINT32_MAX;
1787 prq.request_found = GNUNET_NO;
1788 GNUNET_CONTAINER_multihashmap_get_multiple (pr_map,
1789 &query,
1790 &process_reply,
1791 &prq);
1792 if (NULL != cp)
1793 {
1794 GSF_connected_peer_change_preference_ (cp,
1795 CONTENT_BANDWIDTH_VALUE
1796 + 1000 * prq.priority);
1797 GSF_get_peer_performance_data_ (cp)->respect += prq.priority;
1798 }
1799 if ((GNUNET_YES == active_to_migration) && (NULL != cp) &&
1800 (GNUNET_NO == test_put_load_too_high (prq.priority)))
1801 {
1802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1803 "Replicating result for query `%s' with priority %u\n",
1804 GNUNET_h2s (&query),
1805 prq.priority);
1806 pmc = GNUNET_new (struct PutMigrationContext);
1807 pmc->start = GNUNET_TIME_absolute_get ();
1808 pmc->requested = prq.request_found;
1809 GNUNET_assert (0 != GSF_get_peer_performance_data_ (cp)->pid);
1810 GNUNET_PEER_resolve (GSF_get_peer_performance_data_ (cp)->pid,
1811 &pmc->origin);
1812 if (NULL == GNUNET_DATASTORE_put (GSF_dsh,
1813 0,
1814 &query,
1815 dsize,
1816 &put[1],
1817 type,
1818 prq.priority,
1819 1 /* anonymity */,
1820 0 /* replication */,
1821 expiration,
1822 1 + prq.priority,
1823 MAX_DATASTORE_QUEUE,
1824 &put_migration_continuation,
1825 pmc))
1826 {
1827 put_migration_continuation (pmc,
1828 GNUNET_SYSERR,
1829 GNUNET_TIME_UNIT_ZERO_ABS,
1830 NULL);
1831 }
1832 }
1833 else if (NULL != cp)
1834 {
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 "Choosing not to keep content `%s' (%d/%d)\n",
1837 GNUNET_h2s (&query),
1838 active_to_migration,
1839 test_put_load_too_high (prq.priority));
1840 }
1841 putl = GNUNET_LOAD_get_load (datastore_put_load);
1842 if ((NULL != cp) && (GNUNET_NO == prq.request_found) &&
1843 ((GNUNET_YES != active_to_migration) ||
1844 (putl > 2.5 * (1 + prq.priority))))
1845 {
1846 if (GNUNET_YES != active_to_migration)
1847 putl = 1.0 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 5);
1848 block_time = GNUNET_TIME_relative_multiply (
1849 GNUNET_TIME_UNIT_MILLISECONDS,
1850 5000 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1851 (unsigned int) (60000 * putl * putl)));
1852 GNUNET_log (
1853 GNUNET_ERROR_TYPE_DEBUG,
1854 "Asking to stop migration for %s because of load %f and events %d/%d\n",
1855 GNUNET_STRINGS_relative_time_to_string (block_time, GNUNET_YES),
1856 putl,
1857 active_to_migration,
1858 (GNUNET_NO == prq.request_found));
1859 GSF_block_peer_migration_ (cp,
1860 GNUNET_TIME_relative_to_absolute (block_time));
1861 }
1862}
1863
1864
1865/**
1866 * Check if the given request is still active.
1867 *
1868 * @param pr pending request
1869 * @return #GNUNET_YES if the request is still active
1870 */
1871int
1872GSF_pending_request_test_active_ (struct GSF_PendingRequest *pr)
1873{
1874 return (NULL != pr->rh) ? GNUNET_YES : GNUNET_NO;
1875}
1876
1877
1878/**
1879 * Setup the subsystem.
1880 */
1881void
1882GSF_pending_request_init_ ()
1883{
1884 if (GNUNET_OK !=
1885 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1886 "fs",
1887 "MAX_PENDING_REQUESTS",
1888 &max_pending_requests))
1889 {
1890 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_INFO,
1891 "fs",
1892 "MAX_PENDING_REQUESTS");
1893 }
1894 active_to_migration =
1895 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg, "FS", "CONTENT_CACHING");
1896 datastore_put_load = GNUNET_LOAD_value_init (DATASTORE_LOAD_AUTODECLINE);
1897 pr_map = GNUNET_CONTAINER_multihashmap_create (32 * 1024, GNUNET_YES);
1898 requests_by_expiration_heap =
1899 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1900}
1901
1902
1903/**
1904 * Shutdown the subsystem.
1905 */
1906void
1907GSF_pending_request_done_ ()
1908{
1909 GNUNET_CONTAINER_multihashmap_iterate (pr_map, &clean_request, NULL);
1910 GNUNET_CONTAINER_multihashmap_destroy (pr_map);
1911 pr_map = NULL;
1912 GNUNET_CONTAINER_heap_destroy (requests_by_expiration_heap);
1913 requests_by_expiration_heap = NULL;
1914 GNUNET_LOAD_value_free (datastore_put_load);
1915 datastore_put_load = NULL;
1916}
1917
1918
1919/* end of gnunet-service-fs_pr.c */