2 This file is part of GNUnet
3 Copyright (C) 2004-2014, 2016 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 datastore/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_datastore_plugin.h"
32 #include "datastore.h"
35 * How many messages do we queue at most per client?
37 #define MAX_PENDING 1024
40 * Limit size of bloom filter to 2 GB.
42 #define MAX_BF_SIZE ((uint32_t) (1LL << 31))
45 * How long are we at most keeping "expired" content
46 * past the expiration date in the database?
48 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
51 * How fast are we allowed to query the database for deleting
52 * expired content? (1 item per second).
54 #define MIN_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
57 * Name under which we store current space consumption.
59 static char *quota_stat_name;
62 * Task to timeout stat GET.
64 static struct GNUNET_SCHEDULER_Task *stat_timeout_task;
67 * After how many payload-changing operations
68 * do we sync our statistics?
70 #define MAX_STAT_SYNC_LAG 50
74 * Our datastore plugin.
76 struct DatastorePlugin
80 * API of the transport as returned by the plugin's
81 * initialization function.
83 struct GNUNET_DATASTORE_PluginFunctions *api;
86 * Short name for the plugin (i.e. "sqlite").
91 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
96 * Environment this transport service is using
99 struct GNUNET_DATASTORE_PluginEnvironment env;
105 * Linked list of active reservations.
107 struct ReservationList
111 * This is a linked list.
113 struct ReservationList *next;
116 * Client that made the reservation.
118 struct GNUNET_SERVICE_Client *client;
121 * Number of bytes (still) reserved.
126 * Number of items (still) reserved.
131 * Reservation identifier.
140 * Our datastore plugin (NULL if not available).
142 static struct DatastorePlugin *plugin;
145 * Linked list of space reservations made by clients.
147 static struct ReservationList *reservations;
150 * Bloomfilter to quickly tell if we don't have the content.
152 static struct GNUNET_CONTAINER_BloomFilter *filter;
155 * Name of our plugin.
157 static char *plugin_name;
162 static const struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Handle for reporting statistics.
167 static struct GNUNET_STATISTICS_Handle *stats;
170 * How much space are we using for the cache? (space available for
171 * insertions that will be instantly reclaimed by discarding less
172 * important content --- or possibly whatever we just inserted into
175 static unsigned long long cache_size;
178 * How much space have we currently reserved?
180 static unsigned long long reserved;
183 * How much data are we currently storing
186 static unsigned long long payload;
189 * Identity of the task that is used to delete
192 static struct GNUNET_SCHEDULER_Task *expired_kill_task;
195 * Minimum time that content should have to not be discarded instantly
196 * (time stamp of any content that we've been discarding recently to
197 * stay below the quota). FOREVER if we had to expire content with
200 static struct GNUNET_TIME_Absolute min_expiration;
203 * How much space are we allowed to use?
205 static unsigned long long quota;
208 * Should the database be dropped on exit?
213 * Should we refresh the BF when the DB is loaded?
215 static int refresh_bf;
218 * Number of updates that were made to the
219 * payload value since we last synchronized
220 * it with the statistics service.
222 static unsigned int last_sync;
225 * Did we get an answer from statistics?
227 static int stats_worked;
231 * Synchronize our utilization statistics with the
232 * statistics service.
237 GNUNET_STATISTICS_set (stats,
241 GNUNET_STATISTICS_set (stats,
242 "# utilization by current datastore",
250 * Have we already cleaned up the TCCs and are hence no longer
251 * willing (or able) to transmit anything to anyone?
253 static int cleaning_done;
256 * Handle for pending get request.
258 static struct GNUNET_STATISTICS_GetHandle *stat_get;
261 * Handle to our server.
263 static struct GNUNET_SERVICE_Handle *service;
266 * Task that is used to remove expired entries from
267 * the datastore. This task will schedule itself
268 * again automatically to always delete all expired
271 * @param cls not used
274 delete_expired (void *cls);
278 * Iterate over the expired items stored in the datastore.
279 * Delete all expired items; once we have processed all
280 * expired items, re-schedule the "delete_expired" task.
282 * @param cls not used
283 * @param key key for the content
284 * @param size number of bytes in data
285 * @param data content stored
286 * @param type type of the content
287 * @param priority priority of the content
288 * @param anonymity anonymity-level for the content
289 * @param replication replication-level for the content
290 * @param expiration expiration time for the content
291 * @param uid unique identifier for the datum;
292 * maybe 0 if no unique identifier is available
294 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
295 * (continue on call to "next", of course),
296 * #GNUNET_NO to delete the item and continue (if supported)
299 expired_processor (void *cls,
300 const struct GNUNET_HashCode *key,
303 enum GNUNET_BLOCK_Type type,
306 uint32_t replication,
307 struct GNUNET_TIME_Absolute expiration,
310 struct GNUNET_TIME_Absolute now;
315 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
316 GNUNET_SCHEDULER_PRIORITY_IDLE,
317 &delete_expired, NULL);
318 return GNUNET_SYSERR;
320 now = GNUNET_TIME_absolute_get ();
321 if (expiration.abs_value_us > now.abs_value_us)
323 /* finished processing */
325 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
326 GNUNET_SCHEDULER_PRIORITY_IDLE,
327 &delete_expired, NULL);
328 return GNUNET_SYSERR;
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Deleting content `%s' of type %u that expired %s ago\n",
332 GNUNET_h2s (key), type,
333 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (expiration,
336 min_expiration = now;
337 GNUNET_STATISTICS_update (stats,
338 gettext_noop ("# bytes expired"),
341 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
343 GNUNET_SCHEDULER_add_delayed_with_priority (MIN_EXPIRE_DELAY,
344 GNUNET_SCHEDULER_PRIORITY_IDLE,
345 &delete_expired, NULL);
351 * Task that is used to remove expired entries from
352 * the datastore. This task will schedule itself
353 * again automatically to always delete all expired
356 * @param cls not used
359 delete_expired (void *cls)
361 expired_kill_task = NULL;
362 plugin->api->get_expiration (plugin->api->cls,
369 * An iterator over a set of items stored in the datastore
370 * that deletes until we're happy with respect to our quota.
373 * @param key key for the content
374 * @param size number of bytes in data
375 * @param data content stored
376 * @param type type of the content
377 * @param priority priority of the content
378 * @param anonymity anonymity-level for the content
379 * @param replication replication-level for the content
380 * @param expiration expiration time for the content
381 * @param uid unique identifier for the datum;
382 * maybe 0 if no unique identifier is available
383 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
384 * (continue on call to "next", of course),
385 * #GNUNET_NO to delete the item and continue (if supported)
388 quota_processor (void *cls,
389 const struct GNUNET_HashCode *key,
392 enum GNUNET_BLOCK_Type type,
395 uint32_t replication,
396 struct GNUNET_TIME_Absolute expiration,
399 unsigned long long *need = cls;
402 return GNUNET_SYSERR;
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Deleting %llu bytes of low-priority (%u) content `%s' of type %u at %s prior to expiration (still trying to free another %llu bytes)\n",
405 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
406 (unsigned int) priority,
407 GNUNET_h2s (key), type,
408 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
411 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
414 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
416 min_expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
418 min_expiration = expiration;
419 GNUNET_STATISTICS_update (stats,
420 gettext_noop ("# bytes purged (low-priority)"),
422 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
428 * Manage available disk space by running tasks
429 * that will discard content if necessary. This
430 * function will be run whenever a request for
431 * "need" bytes of storage could only be satisfied
432 * by eating into the "cache" (and we want our cache
435 * @param need number of bytes of content that were
436 * placed into the "cache" (and hence the
437 * number of bytes that should be removed).
440 manage_space (unsigned long long need)
442 unsigned long long last;
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Asked to free up %llu bytes of cache space\n",
448 while ((need > 0) && (last != need))
451 plugin->api->get_expiration (plugin->api->cls,
459 * Transmit a status code to the client.
461 * @param client receiver of the response
462 * @param code status code
463 * @param msg optional error message (can be NULL)
466 transmit_status (struct GNUNET_SERVICE_Client *client,
470 struct GNUNET_MQ_Envelope *env;
471 struct StatusMessage *sm;
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Transmitting `%s' message with value %d and message `%s'\n",
476 "STATUS", code, msg != NULL ? msg : "(none)");
477 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
478 env = GNUNET_MQ_msg_extra (sm,
480 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
481 sm->status = htonl (code);
482 sm->min_expiration = GNUNET_TIME_absolute_hton (min_expiration);
483 GNUNET_memcpy (&sm[1],
486 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
492 * Function that will transmit the given datastore entry
495 * @param cls closure, pointer to the client (of type `struct GNUNET_SERVICE_Client`).
496 * @param key key for the content
497 * @param size number of bytes in data
498 * @param data content stored
499 * @param type type of the content
500 * @param priority priority of the content
501 * @param anonymity anonymity-level for the content
502 * @param replication replication-level for the content
503 * @param expiration expiration time for the content
504 * @param uid unique identifier for the datum;
505 * maybe 0 if no unique identifier is available
506 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue,
507 * #GNUNET_NO to delete the item and continue (if supported)
510 transmit_item (void *cls,
511 const struct GNUNET_HashCode *key,
514 enum GNUNET_BLOCK_Type type,
517 uint32_t replication,
518 struct GNUNET_TIME_Absolute expiration,
521 struct GNUNET_SERVICE_Client *client = cls;
522 struct GNUNET_MQ_Envelope *env;
523 struct GNUNET_MessageHeader *end;
524 struct DataMessage *dm;
528 /* transmit 'DATA_END' */
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Transmitting DATA_END message\n");
531 env = GNUNET_MQ_msg (end,
532 GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
533 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
537 GNUNET_assert (sizeof (struct DataMessage) + size <
538 GNUNET_MAX_MESSAGE_SIZE);
539 env = GNUNET_MQ_msg_extra (dm,
541 GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
543 dm->size = htonl (size);
544 dm->type = htonl (type);
545 dm->priority = htonl (priority);
546 dm->anonymity = htonl (anonymity);
547 dm->replication = htonl (replication);
548 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
549 dm->uid = GNUNET_htonll (uid);
551 GNUNET_memcpy (&dm[1],
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Transmitting DATA message for `%s' of type %u with expiration %s (in: %s)\n",
558 GNUNET_STRINGS_absolute_time_to_string (expiration),
559 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
561 GNUNET_STATISTICS_update (stats,
562 gettext_noop ("# results found"),
565 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
572 * Handle RESERVE-message.
574 * @param cls identification of the client
575 * @param message the actual message
578 handle_reserve (void *cls,
579 const struct ReserveMessage *msg)
582 * Static counter to produce reservation identifiers.
584 static int reservation_gen;
585 struct GNUNET_SERVICE_Client *client = cls;
586 struct ReservationList *e;
587 unsigned long long used;
588 unsigned long long req;
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Processing RESERVE request\n");
594 amount = GNUNET_ntohll (msg->amount);
595 entries = ntohl (msg->entries);
596 used = payload + reserved;
597 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
598 if (used + req > quota)
601 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
602 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
603 _("Insufficient space (%llu bytes are available) to satisfy RESERVE request for %llu bytes\n"),
606 if (cache_size < req)
608 /* TODO: document this in the FAQ; essentially, if this
609 * message happens, the insertion request could be blocked
610 * by less-important content from migration because it is
611 * larger than 1/8th of the overall available space, and
612 * we only reserve 1/8th for "fresh" insertions */
613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
614 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
617 transmit_status (client,
620 ("Insufficient space to satisfy request and "
621 "requested amount is larger than cache size"));
625 transmit_status (client,
627 gettext_noop ("Insufficient space to satisfy request"));
629 GNUNET_SERVICE_client_continue (client);
633 GNUNET_STATISTICS_set (stats,
634 gettext_noop ("# reserved"),
637 e = GNUNET_new (struct ReservationList);
638 e->next = reservations;
642 e->entries = entries;
643 e->rid = ++reservation_gen;
644 if (reservation_gen < 0)
645 reservation_gen = 0; /* wrap around */
646 transmit_status (client,
649 GNUNET_SERVICE_client_continue (client);
654 * Handle RELEASE_RESERVE-message.
656 * @param cls identification of the client
657 * @param message the actual message
660 handle_release_reserve (void *cls,
661 const struct ReleaseReserveMessage *msg)
663 struct GNUNET_SERVICE_Client *client = cls;
664 struct ReservationList *pos;
665 struct ReservationList *prev;
666 struct ReservationList *next;
667 int rid = ntohl (msg->rid);
668 unsigned long long rem;
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Processing RELEASE_RESERVE request\n");
674 while (NULL != (pos = next))
685 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
686 GNUNET_assert (reserved >= rem);
688 GNUNET_STATISTICS_set (stats,
689 gettext_noop ("# reserved"),
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Returning %llu remaining reserved bytes to storage pool\n",
696 transmit_status (client,
699 GNUNET_SERVICE_client_continue (client);
705 transmit_status (client,
707 gettext_noop ("Could not find matching reservation"));
708 GNUNET_SERVICE_client_continue (client);
713 * Check that the given message is a valid data message.
715 * @param dm message to check
716 * @return #GNUNET_SYSERR is not well-formed, otherwise #GNUNET_OK
719 check_data (const struct DataMessage *dm)
724 size = ntohs (dm->header.size);
725 dsize = ntohl (dm->size);
726 if (size != dsize + sizeof (struct DataMessage))
729 return GNUNET_SYSERR;
739 * @param key key for the item stored
740 * @param size size of the item stored
741 * @param status #GNUNET_OK if inserted, #GNUNET_NO if updated,
742 * or #GNUNET_SYSERROR if error
743 * @param msg error message on error
746 put_continuation (void *cls,
747 const struct GNUNET_HashCode *key,
752 struct GNUNET_SERVICE_Client *client = cls;
754 if (GNUNET_OK == status)
756 GNUNET_STATISTICS_update (stats,
757 gettext_noop ("# bytes stored"),
760 GNUNET_CONTAINER_bloomfilter_add (filter,
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "Successfully stored %u bytes under key `%s'\n",
767 transmit_status (client,
768 GNUNET_SYSERR == status ? GNUNET_SYSERR : GNUNET_OK,
770 if (quota - reserved - cache_size < payload)
772 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
773 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
774 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
775 (unsigned long long) (quota - reserved - cache_size),
776 (unsigned long long) payload);
777 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
783 * Verify PUT-message.
785 * @param cls identification of the client
786 * @param message the actual message
787 * @return #GNUNET_OK if @a dm is well-formed
790 check_put (void *cls,
791 const struct DataMessage *dm)
793 if (GNUNET_OK != check_data (dm))
796 return GNUNET_SYSERR;
803 * Handle PUT-message.
805 * @param cls identification of the client
806 * @param message the actual message
809 handle_put (void *cls,
810 const struct DataMessage *dm)
812 struct GNUNET_SERVICE_Client *client = cls;
814 struct ReservationList *pos;
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Processing PUT request for `%s' of type %u\n",
819 GNUNET_h2s (&dm->key),
820 (uint32_t) ntohl (dm->type));
821 rid = ntohl (dm->rid);
822 size = ntohl (dm->size);
826 while ((NULL != pos) && (rid != pos->rid))
828 GNUNET_break (pos != NULL);
831 GNUNET_break (pos->entries > 0);
832 GNUNET_break (pos->amount >= size);
835 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
836 GNUNET_STATISTICS_set (stats,
837 gettext_noop ("# reserved"),
842 bool absent = GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (filter,
844 plugin->api->put (plugin->api->cls,
850 ntohl (dm->priority),
851 ntohl (dm->anonymity),
852 ntohl (dm->replication),
853 GNUNET_TIME_absolute_ntoh (dm->expiration),
856 GNUNET_SERVICE_client_continue (client);
861 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET-message.
863 * @param cls identification of the client
864 * @param msg the actual message
867 handle_get (void *cls,
868 const struct GetMessage *msg)
870 struct GNUNET_SERVICE_Client *client = cls;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Processing GET request of type %u\n",
874 (uint32_t) ntohl (msg->type));
875 GNUNET_STATISTICS_update (stats,
876 gettext_noop ("# GET requests received"),
879 plugin->api->get_key (plugin->api->cls,
880 GNUNET_ntohll (msg->next_uid),
886 GNUNET_SERVICE_client_continue (client);
891 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY-message.
894 * @param msg the actual message
897 handle_get_key (void *cls,
898 const struct GetKeyMessage *msg)
900 struct GNUNET_SERVICE_Client *client = cls;
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "Processing GET request for `%s' of type %u\n",
904 GNUNET_h2s (&msg->key),
905 (uint32_t) ntohl (msg->type));
906 GNUNET_STATISTICS_update (stats,
907 gettext_noop ("# GET KEY requests received"),
911 GNUNET_CONTAINER_bloomfilter_test (filter,
914 /* don't bother database... */
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Empty result set for GET request for `%s' (bloomfilter).\n",
917 GNUNET_h2s (&msg->key));
918 GNUNET_STATISTICS_update (stats,
920 ("# requests filtered by bloomfilter"),
923 transmit_item (client,
924 NULL, 0, NULL, 0, 0, 0, 0,
925 GNUNET_TIME_UNIT_ZERO_ABS,
927 GNUNET_SERVICE_client_continue (client);
930 plugin->api->get_key (plugin->api->cls,
931 GNUNET_ntohll (msg->next_uid),
937 GNUNET_SERVICE_client_continue (client);
942 * Handle GET_REPLICATION-message.
944 * @param cls identification of the client
945 * @param message the actual message
948 handle_get_replication (void *cls,
949 const struct GNUNET_MessageHeader *message)
951 struct GNUNET_SERVICE_Client *client = cls;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Processing GET_REPLICATION request\n");
955 GNUNET_STATISTICS_update (stats,
956 gettext_noop ("# GET REPLICATION requests received"),
959 plugin->api->get_replication (plugin->api->cls,
962 GNUNET_SERVICE_client_continue (client);
967 * Handle GET_ZERO_ANONYMITY-message.
969 * @param cls client identification of the client
970 * @param message the actual message
973 handle_get_zero_anonymity (void *cls,
974 const struct GetZeroAnonymityMessage *msg)
976 struct GNUNET_SERVICE_Client *client = cls;
977 enum GNUNET_BLOCK_Type type;
979 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
980 if (type == GNUNET_BLOCK_TYPE_ANY)
983 GNUNET_SERVICE_client_drop (client);
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987 "Processing GET_ZERO_ANONYMITY request\n");
988 GNUNET_STATISTICS_update (stats,
989 gettext_noop ("# GET ZERO ANONYMITY requests received"),
992 plugin->api->get_zero_anonymity (plugin->api->cls,
993 GNUNET_ntohll (msg->next_uid),
997 GNUNET_SERVICE_client_continue (client);
1002 * Remove continuation.
1004 * @param cls closure
1005 * @param key key for the content
1006 * @param size number of bytes in data
1007 * @param status #GNUNET_OK if removed, #GNUNET_NO if not found,
1008 * or #GNUNET_SYSERROR if error
1009 * @param msg error message on error
1012 remove_continuation (void *cls,
1013 const struct GNUNET_HashCode *key,
1018 struct GNUNET_SERVICE_Client *client = cls;
1020 if (GNUNET_SYSERR == status)
1022 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1023 "REMOVE request failed: %s.\n",
1025 transmit_status (client,
1030 if (GNUNET_NO == status)
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Content not found for REMOVE request.\n");
1034 transmit_status (client,
1036 _("Content not found"));
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Item matches REMOVE request for key `%s'.\n",
1042 GNUNET_STATISTICS_update (stats,
1043 gettext_noop ("# bytes removed (explicit request)"),
1046 GNUNET_CONTAINER_bloomfilter_remove (filter,
1048 transmit_status (client,
1055 * Verify REMOVE-message.
1057 * @param cls identification of the client
1058 * @param message the actual message
1059 * @return #GNUNET_OK if @a dm is well-formed
1062 check_remove (void *cls,
1063 const struct DataMessage *dm)
1065 if (GNUNET_OK != check_data (dm))
1068 return GNUNET_SYSERR;
1075 * Handle REMOVE-message.
1077 * @param cls closure
1078 * @param client identification of the client
1079 * @param message the actual message
1082 handle_remove (void *cls,
1083 const struct DataMessage *dm)
1085 struct GNUNET_SERVICE_Client *client = cls;
1087 GNUNET_STATISTICS_update (stats,
1088 gettext_noop ("# REMOVE requests received"),
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Processing REMOVE request for `%s'\n",
1092 GNUNET_h2s (&dm->key));
1093 plugin->api->remove_key (plugin->api->cls,
1097 &remove_continuation,
1099 GNUNET_SERVICE_client_continue (client);
1104 * Handle DROP-message.
1106 * @param cls identification of the client
1107 * @param message the actual message
1110 handle_drop (void *cls,
1111 const struct GNUNET_MessageHeader *message)
1113 struct GNUNET_SERVICE_Client *client = cls;
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Processing DROP request\n");
1117 do_drop = GNUNET_YES;
1118 GNUNET_SERVICE_client_continue (client);
1123 * Function called by plugins to notify us about a
1124 * change in their disk utilization.
1126 * @param cls closure (NULL)
1127 * @param delta change in disk utilization,
1128 * 0 for "reset to empty"
1131 disk_utilization_change_cb (void *cls,
1134 if ((delta < 0) && (payload < -delta))
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137 _("Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1138 (long long) payload,
1139 (long long) -delta);
1140 plugin->api->estimate_size (plugin->api->cls,
1142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1143 _("New payload: %lld\n"),
1144 (long long) payload);
1150 if (last_sync >= MAX_STAT_SYNC_LAG)
1156 * Callback function to process statistic values.
1158 * @param cls closure (struct Plugin*)
1159 * @param subsystem name of subsystem that created the statistic
1160 * @param name the name of the datum
1161 * @param value the current value
1162 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1163 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1166 process_stat_in (void *cls,
1167 const char *subsystem,
1172 GNUNET_assert (GNUNET_NO == stats_worked);
1173 stats_worked = GNUNET_YES;
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1177 (unsigned long long) value,
1178 (unsigned long long) payload);
1184 * Load the datastore plugin.
1186 static struct DatastorePlugin *
1189 struct DatastorePlugin *ret;
1192 ret = GNUNET_new (struct DatastorePlugin);
1194 ret->env.duc = &disk_utilization_change_cb;
1195 ret->env.cls = NULL;
1196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1197 _("Loading `%s' datastore plugin\n"),
1199 GNUNET_asprintf (&libname,
1200 "libgnunet_plugin_datastore_%s",
1202 ret->short_name = GNUNET_strdup (plugin_name);
1203 ret->lib_name = libname;
1204 ret->api = GNUNET_PLUGIN_load (libname,
1206 if (NULL == ret->api)
1208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1209 _("Failed to load datastore plugin for `%s'\n"),
1211 GNUNET_free (ret->short_name);
1212 GNUNET_free (libname);
1221 * Function called when the service shuts
1222 * down. Unloads our datastore plugin.
1224 * @param plug plugin to unload
1227 unload_plugin (struct DatastorePlugin *plug)
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Datastore service is unloading plugin...\n");
1231 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1232 GNUNET_free (plug->lib_name);
1233 GNUNET_free (plug->short_name);
1239 * Initialization complete, start operating the service.
1244 GNUNET_SERVICE_resume (service);
1246 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1253 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1255 * @param cls the bloomfilter
1256 * @param key key to add
1257 * @param count number of times to add key
1260 add_key_to_bloomfilter (void *cls,
1261 const struct GNUNET_HashCode *key,
1264 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1268 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1269 _("Bloomfilter construction complete.\n"));
1275 GNUNET_CONTAINER_bloomfilter_add (bf,
1281 * We finished receiving the statistic. Initialize the plugin; if
1282 * loading the statistic failed, run the estimator.
1285 * @param success #GNUNET_NO if we failed to read the stat
1288 process_stat_done (void *cls,
1292 if (NULL != stat_timeout_task)
1294 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1295 stat_timeout_task = NULL;
1297 plugin = load_plugin ();
1300 GNUNET_CONTAINER_bloomfilter_free (filter);
1304 GNUNET_STATISTICS_destroy (stats,
1311 if (GNUNET_NO == stats_worked)
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 "Failed to obtain value from statistics service, recomputing it\n");
1315 plugin->api->estimate_size (plugin->api->cls,
1317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1318 _("New payload: %lld\n"),
1319 (long long) payload);
1322 if (GNUNET_YES == refresh_bf)
1324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1325 _("Rebuilding bloomfilter. Please be patient.\n"));
1326 if (NULL != plugin->api->get_keys)
1328 plugin->api->get_keys (plugin->api->cls,
1329 &add_key_to_bloomfilter,
1335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1336 _("Plugin does not support get_keys function. Please fix!\n"));
1344 * Fetching stats took to long, run without.
1349 stat_timeout (void *cls)
1351 stat_timeout_task = NULL;
1352 GNUNET_STATISTICS_get_cancel (stat_get);
1353 process_stat_done (NULL,
1359 * Task run during shutdown.
1362 cleaning_task (void *cls)
1364 cleaning_done = GNUNET_YES;
1365 if (NULL != expired_kill_task)
1367 GNUNET_SCHEDULER_cancel (expired_kill_task);
1368 expired_kill_task = NULL;
1370 if (GNUNET_YES == do_drop)
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373 "Dropping database!\n");
1374 plugin->api->drop (plugin->api->cls);
1380 unload_plugin (plugin);
1385 GNUNET_CONTAINER_bloomfilter_free (filter);
1388 if (NULL != stat_get)
1390 GNUNET_STATISTICS_get_cancel (stat_get);
1393 if (NULL != stat_timeout_task)
1395 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1396 stat_timeout_task = NULL;
1398 GNUNET_free_non_null (plugin_name);
1404 GNUNET_STATISTICS_destroy (stats,
1408 GNUNET_free (quota_stat_name);
1409 quota_stat_name = NULL;
1414 * Add a client to our list of active clients.
1417 * @param client client to add
1418 * @param mq message queue for @a client
1422 client_connect_cb (void *cls,
1423 struct GNUNET_SERVICE_Client *client,
1424 struct GNUNET_MQ_Handle *mq)
1431 * Called whenever a client is disconnected.
1432 * Frees our resources associated with that client.
1434 * @param cls closure
1435 * @param client identification of the client
1436 * @param app_ctx must match @a client
1439 client_disconnect_cb (void *cls,
1440 struct GNUNET_SERVICE_Client *client,
1443 struct ReservationList *pos;
1444 struct ReservationList *prev;
1445 struct ReservationList *next;
1447 GNUNET_assert (app_ctx == client);
1453 if (pos->client == client)
1456 reservations = next;
1459 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1468 GNUNET_STATISTICS_set (stats,
1469 gettext_noop ("# reserved"),
1477 * Process datastore requests.
1479 * @param cls closure
1480 * @param serv the initialized service
1481 * @param c configuration to use
1485 const struct GNUNET_CONFIGURATION_Handle *c,
1486 struct GNUNET_SERVICE_Handle *serv)
1490 unsigned int bf_size;
1495 GNUNET_CONFIGURATION_get_value_string (cfg,
1500 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1505 GNUNET_asprintf ("a_stat_name,
1506 _("# bytes used in file-sharing datastore `%s'"),
1509 GNUNET_CONFIGURATION_get_value_size (cfg,
1514 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1519 stats = GNUNET_STATISTICS_create ("datastore",
1521 GNUNET_STATISTICS_set (stats,
1522 gettext_noop ("# quota"),
1525 cache_size = quota / 8; /* Or should we make this an option? */
1526 GNUNET_STATISTICS_set (stats,
1527 gettext_noop ("# cache size"),
1530 if (quota / (32 * 1024LL) > MAX_BF_SIZE)
1531 bf_size = MAX_BF_SIZE;
1533 bf_size = quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1536 GNUNET_CONFIGURATION_get_value_filename (cfg,
1540 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1543 _("Could not use specified filename `%s' for bloomfilter.\n"),
1544 NULL != fn ? fn : "");
1545 GNUNET_free_non_null (fn);
1550 GNUNET_asprintf (&pfn, "%s.%s", fn, plugin_name);
1551 if (GNUNET_YES == GNUNET_DISK_file_test (pfn))
1553 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1556 /* file exists but not valid, remove and try again, but refresh */
1557 if (0 != UNLINK (pfn))
1559 /* failed to remove, run without file */
1560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1561 _("Failed to remove bogus bloomfilter file `%s'\n"),
1565 filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1566 refresh_bf = GNUNET_YES;
1570 /* try again after remove */
1571 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1572 refresh_bf = GNUNET_YES;
1575 /* failed yet again, give up on using file */
1576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1577 _("Failed to remove bogus bloomfilter file `%s'\n"),
1581 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1587 /* normal case: have an existing valid bf file, no need to refresh */
1588 refresh_bf = GNUNET_NO;
1593 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1594 refresh_bf = GNUNET_YES;
1600 filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
1602 5); /* approx. 3% false positives at max use */
1603 refresh_bf = GNUNET_YES;
1605 GNUNET_free_non_null (fn);
1608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1609 _("Failed to initialize bloomfilter.\n"));
1612 GNUNET_STATISTICS_destroy (stats,
1618 GNUNET_SERVICE_suspend (service);
1620 GNUNET_STATISTICS_get (stats,
1626 if (NULL == stat_get)
1627 process_stat_done (NULL,
1631 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1634 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1640 * Define "main" method using service macro.
1644 GNUNET_SERVICE_OPTION_NONE,
1647 &client_disconnect_cb,
1649 GNUNET_MQ_hd_fixed_size (reserve,
1650 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1651 struct ReserveMessage,
1653 GNUNET_MQ_hd_fixed_size (release_reserve,
1654 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1655 struct ReleaseReserveMessage,
1657 GNUNET_MQ_hd_var_size (put,
1658 GNUNET_MESSAGE_TYPE_DATASTORE_PUT,
1661 GNUNET_MQ_hd_fixed_size (get,
1662 GNUNET_MESSAGE_TYPE_DATASTORE_GET,
1665 GNUNET_MQ_hd_fixed_size (get_key,
1666 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY,
1667 struct GetKeyMessage,
1669 GNUNET_MQ_hd_fixed_size (get_replication,
1670 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1671 struct GNUNET_MessageHeader,
1673 GNUNET_MQ_hd_fixed_size (get_zero_anonymity,
1674 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1675 struct GetZeroAnonymityMessage,
1677 GNUNET_MQ_hd_var_size (remove,
1678 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE,
1681 GNUNET_MQ_hd_fixed_size (drop,
1682 GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1683 struct GNUNET_MessageHeader,
1685 GNUNET_MQ_handler_end ());
1688 /* end of gnunet-service-datastore.c */