2 This file is part of GNUnet.
3 Copyright (C) 2011, 2016 GNUnet e.V.
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.
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.
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/>.
20 * @file fs/gnunet-service-fs_push.c
21 * @brief API to push content from our datastore to other peers
22 * ('anonymous'-content P2P migration)
23 * @author Christian Grothoff
26 #include "gnunet-service-fs.h"
27 #include "gnunet-service-fs_cp.h"
28 #include "gnunet-service-fs_indexing.h"
29 #include "gnunet-service-fs_push.h"
33 * Maximum number of blocks we keep in memory for migration.
35 #define MAX_MIGRATION_QUEUE 8
38 * Blocks are at most migrated to this number of peers
39 * plus one, each time they are fetched from the database.
41 #define MIGRATION_LIST_SIZE 2
44 * How long must content remain valid for us to consider it for migration?
45 * If content will expire too soon, there is clearly no point in pushing
46 * it to other peers. This value gives the threshold for migration. Note
47 * that if this value is increased, the migration testcase may need to be
48 * adjusted as well (especially the CONTENT_LIFETIME in fs_test_lib.c).
50 #define MIN_MIGRATION_CONTENT_LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
54 * Block that is ready for migration to other peers. Actual data is at the end of the block.
56 struct MigrationReadyBlock
60 * This is a doubly-linked list.
62 struct MigrationReadyBlock *next;
65 * This is a doubly-linked list.
67 struct MigrationReadyBlock *prev;
70 * Query for the block.
72 struct GNUNET_HashCode query;
75 * When does this block expire?
77 struct GNUNET_TIME_Absolute expiration;
80 * Peers we already forwarded this
81 * block to. Zero for empty entries.
83 GNUNET_PEER_Id target_list[MIGRATION_LIST_SIZE];
91 * Number of targets already used.
93 unsigned int used_targets;
98 enum GNUNET_BLOCK_Type type;
103 * Information about a peer waiting for migratable data.
105 struct MigrationReadyPeer
108 * This is a doubly-linked list.
110 struct MigrationReadyPeer *next;
113 * This is a doubly-linked list.
115 struct MigrationReadyPeer *prev;
120 struct GSF_ConnectedPeer *peer;
123 * Envelope of the currently pushed message.
125 struct GNUNET_MQ_Envelope *env;
130 * Head of linked list of blocks that can be migrated.
132 static struct MigrationReadyBlock *mig_head;
135 * Tail of linked list of blocks that can be migrated.
137 static struct MigrationReadyBlock *mig_tail;
140 * Head of linked list of peers.
142 static struct MigrationReadyPeer *peer_head;
145 * Tail of linked list of peers.
147 static struct MigrationReadyPeer *peer_tail;
150 * Request to datastore for migration (or NULL).
152 static struct GNUNET_DATASTORE_QueueEntry *mig_qe;
155 * ID of task that collects blocks for migration.
157 static struct GNUNET_SCHEDULER_Task *mig_task;
160 * What is the maximum frequency at which we are allowed to
161 * poll the datastore for migration content?
163 static struct GNUNET_TIME_Relative min_migration_delay;
166 * Size of the doubly-linked list of migration blocks.
168 static unsigned int mig_size;
171 * Is this module enabled?
176 * Did we find anything in the datastore?
178 static int value_found;
182 * Delete the given migration block.
184 * @param mb block to delete
187 delete_migration_block (struct MigrationReadyBlock *mb)
189 GNUNET_CONTAINER_DLL_remove (mig_head,
192 GNUNET_PEER_decrement_rcs (mb->target_list,
193 MIGRATION_LIST_SIZE);
200 * Find content for migration to this peer.
202 * @param cls a `struct MigrationReadyPeer *`
205 find_content (void *cls);
209 * Send the given block to the given peer.
211 * @param peer target peer
212 * @param block the block
213 * @return #GNUNET_YES if the block was deleted (!)
216 transmit_content (struct MigrationReadyPeer *mrp,
217 struct MigrationReadyBlock *block)
219 struct PutMessage *msg;
221 struct GSF_PeerPerformanceData *ppd;
224 ppd = GSF_get_peer_performance_data_ (mrp->peer);
225 GNUNET_assert (NULL == mrp->env);
226 mrp->env = GNUNET_MQ_msg_extra (msg,
228 GNUNET_MESSAGE_TYPE_FS_PUT);
229 msg->type = htonl (block->type);
230 msg->expiration = GNUNET_TIME_absolute_hton (block->expiration);
231 GNUNET_memcpy (&msg[1],
234 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
236 if (block->target_list[i] == 0)
238 block->target_list[i] = ppd->pid;
239 GNUNET_PEER_change_rc (block->target_list[i],
244 if (MIGRATION_LIST_SIZE == i)
246 delete_migration_block (block);
253 GNUNET_MQ_notify_sent (mrp->env,
256 GSF_peer_transmit_ (mrp->peer,
265 * Count the number of peers this block has
266 * already been forwarded to.
268 * @param block the block
269 * @return number of times block was forwarded
272 count_targets (struct MigrationReadyBlock *block)
276 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
277 if (block->target_list[i] == 0)
284 * Check if sending this block to this peer would
287 * @param mrp target peer
288 * @param block the block
289 * @return score (>= 0: feasible, negative: infeasible)
292 score_content (struct MigrationReadyPeer *mrp,
293 struct MigrationReadyBlock *block)
296 struct GSF_PeerPerformanceData *ppd;
297 struct GNUNET_PeerIdentity id;
298 struct GNUNET_HashCode hc;
301 ppd = GSF_get_peer_performance_data_ (mrp->peer);
302 for (i = 0; i < MIGRATION_LIST_SIZE; i++)
303 if (block->target_list[i] == ppd->pid)
305 GNUNET_assert (0 != ppd->pid);
306 GNUNET_PEER_resolve (ppd->pid,
308 GNUNET_CRYPTO_hash (&id,
309 sizeof (struct GNUNET_PeerIdentity),
311 dist = GNUNET_CRYPTO_hash_distance_u32 (&block->query,
313 /* closer distance, higher score: */
314 return UINT32_MAX - dist;
319 * If the migration task is not currently running, consider
320 * (re)scheduling it with the appropriate delay.
323 consider_gathering (void);
327 * Find content for migration to this peer.
329 * @param cls peer to find content for
332 find_content (void *cls)
334 struct MigrationReadyPeer *mrp = cls;
335 struct MigrationReadyBlock *pos;
338 struct MigrationReadyBlock *best;
346 score = score_content (mrp, pos);
347 if (score > best_score)
356 if (mig_size < MAX_MIGRATION_QUEUE)
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "No content found for pushing, waiting for queue to fill\n");
360 return; /* will fill up eventually... */
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "No suitable content found, purging content from full queue\n");
364 /* failed to find migration target AND
365 * queue is full, purge most-forwarded
366 * block from queue to make room for more */
370 score = count_targets (pos);
371 if (score >= best_score)
378 GNUNET_assert (NULL != best);
379 delete_migration_block (best);
380 consider_gathering ();
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Preparing to push best content to peer\n");
385 transmit_content (mrp,
391 * Task that is run periodically to obtain blocks for content
397 gather_migration_blocks (void *cls);
401 * If the migration task is not currently running, consider
402 * (re)scheduling it with the appropriate delay.
405 consider_gathering ()
407 struct GNUNET_TIME_Relative delay;
413 if (NULL != mig_task)
415 if (mig_size >= MAX_MIGRATION_QUEUE)
417 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
419 delay = GNUNET_TIME_relative_divide (delay,
420 MAX_MIGRATION_QUEUE);
421 delay = GNUNET_TIME_relative_max (delay,
422 min_migration_delay);
423 if (GNUNET_NO == value_found)
425 /* wait at least 5s if the datastore is empty */
426 delay = GNUNET_TIME_relative_max (delay,
427 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Scheduling gathering task (queue size: %u)\n",
433 mig_task = GNUNET_SCHEDULER_add_delayed (delay,
434 &gather_migration_blocks,
440 * Process content offered for migration.
443 * @param key key for the content
444 * @param size number of bytes in data
445 * @param data content stored
446 * @param type type of the content
447 * @param priority priority of the content
448 * @param anonymity anonymity-level for the content
449 * @param replication replication-level for the content
450 * @param expiration expiration time for the content
451 * @param uid unique identifier for the datum;
452 * maybe 0 if no unique identifier is available
455 process_migration_content (void *cls,
456 const struct GNUNET_HashCode *key,
459 enum GNUNET_BLOCK_Type type,
462 uint32_t replication,
463 struct GNUNET_TIME_Absolute expiration,
466 struct MigrationReadyBlock *mb;
467 struct MigrationReadyPeer *pos;
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "No content found for migration...\n");
474 consider_gathering ();
477 value_found = GNUNET_YES;
478 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us <
479 MIN_MIGRATION_CONTENT_LIFETIME.rel_value_us)
481 /* content will expire soon, don't bother */
482 consider_gathering ();
485 if (type == GNUNET_BLOCK_TYPE_FS_ONDEMAND)
488 GNUNET_FS_handle_on_demand_block (key,
497 &process_migration_content,
499 consider_gathering ();
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Retrieved block `%s' of type %u for migration (queue size: %u/%u)\n",
506 MAX_MIGRATION_QUEUE);
507 mb = GNUNET_malloc (sizeof (struct MigrationReadyBlock) + size);
509 mb->expiration = expiration;
512 GNUNET_memcpy (&mb[1], data, size);
513 GNUNET_CONTAINER_DLL_insert_after (mig_head,
518 for (pos = peer_head; NULL != pos; pos = pos->next)
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Preparing to push best content to peer %s\n",
522 GNUNET_i2s (GSF_connected_peer_get_identity2_(pos->peer)));
523 if ( (NULL == pos->env) &&
524 (GNUNET_YES == transmit_content (pos,
526 break; /* 'mb' was freed! */
529 consider_gathering ();
534 * Task that is run periodically to obtain blocks for content
540 gather_migration_blocks (void *cls)
543 if (mig_size >= MAX_MIGRATION_QUEUE)
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Asking datastore for content for replication (queue size: %u)\n",
550 value_found = GNUNET_NO;
551 mig_qe = GNUNET_DATASTORE_get_for_replication (GSF_dsh,
554 &process_migration_content,
557 consider_gathering ();
562 * A peer connected to us. Start pushing content
565 * @param peer handle for the peer that connected
568 GSF_push_start_ (struct GSF_ConnectedPeer *peer)
570 struct MigrationReadyPeer *mrp;
572 if (GNUNET_YES != enabled)
574 for (mrp = peer_head; NULL != mrp; mrp = mrp->next)
575 if (mrp->peer == peer)
579 /* same peer added twice, must not happen */
584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
585 "Adding peer %s to list for pushing\n",
586 GNUNET_i2s (GSF_connected_peer_get_identity2_(peer)));
588 mrp = GNUNET_new (struct MigrationReadyPeer);
591 GNUNET_CONTAINER_DLL_insert (peer_head,
598 * A peer disconnected from us. Stop pushing content
601 * @param peer handle for the peer that disconnected
604 GSF_push_stop_ (struct GSF_ConnectedPeer *peer)
606 struct MigrationReadyPeer *pos;
608 for (pos = peer_head; NULL != pos; pos = pos->next)
609 if (pos->peer == peer)
613 if (NULL != pos->env)
614 GNUNET_MQ_send_cancel (pos->env);
615 GNUNET_CONTAINER_DLL_remove (peer_head,
629 GNUNET_CONFIGURATION_get_value_yesno (GSF_cfg,
632 if (GNUNET_YES != enabled)
636 GNUNET_CONFIGURATION_get_value_time (GSF_cfg,
638 "MIN_MIGRATION_DELAY",
639 &min_migration_delay))
641 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
643 "MIN_MIGRATION_DELAY",
644 _("time required, content pushing disabled"));
647 consider_gathering ();
652 * Shutdown the module.
657 if (NULL != mig_task)
659 GNUNET_SCHEDULER_cancel (mig_task);
664 GNUNET_DATASTORE_cancel (mig_qe);
667 while (NULL != mig_head)
668 delete_migration_block (mig_head);
669 GNUNET_assert (0 == mig_size);
672 /* end of gnunet-service-fs_push.c */