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 expiration expiration time for the content
290 * @param uid unique identifier for the datum;
291 * maybe 0 if no unique identifier is available
293 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
294 * (continue on call to "next", of course),
295 * #GNUNET_NO to delete the item and continue (if supported)
298 expired_processor (void *cls,
299 const struct GNUNET_HashCode *key,
302 enum GNUNET_BLOCK_Type type,
305 struct GNUNET_TIME_Absolute expiration,
308 struct GNUNET_TIME_Absolute now;
313 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
314 GNUNET_SCHEDULER_PRIORITY_IDLE,
315 &delete_expired, NULL);
316 return GNUNET_SYSERR;
318 now = GNUNET_TIME_absolute_get ();
319 if (expiration.abs_value_us > now.abs_value_us)
321 /* finished processing */
323 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
324 GNUNET_SCHEDULER_PRIORITY_IDLE,
325 &delete_expired, NULL);
326 return GNUNET_SYSERR;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Deleting content `%s' of type %u that expired %s ago\n",
330 GNUNET_h2s (key), type,
331 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (expiration,
334 min_expiration = now;
335 GNUNET_STATISTICS_update (stats,
336 gettext_noop ("# bytes expired"),
339 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
341 GNUNET_SCHEDULER_add_delayed_with_priority (MIN_EXPIRE_DELAY,
342 GNUNET_SCHEDULER_PRIORITY_IDLE,
343 &delete_expired, NULL);
349 * Task that is used to remove expired entries from
350 * the datastore. This task will schedule itself
351 * again automatically to always delete all expired
354 * @param cls not used
357 delete_expired (void *cls)
359 expired_kill_task = NULL;
360 plugin->api->get_expiration (plugin->api->cls,
367 * An iterator over a set of items stored in the datastore
368 * that deletes until we're happy with respect to our quota.
371 * @param key key for the content
372 * @param size number of bytes in data
373 * @param data content stored
374 * @param type type of the content
375 * @param priority priority of the content
376 * @param anonymity anonymity-level for the content
377 * @param expiration expiration time for the content
378 * @param uid unique identifier for the datum;
379 * maybe 0 if no unique identifier is available
380 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
381 * (continue on call to "next", of course),
382 * #GNUNET_NO to delete the item and continue (if supported)
385 quota_processor (void *cls,
386 const struct GNUNET_HashCode *key,
389 enum GNUNET_BLOCK_Type type,
392 struct GNUNET_TIME_Absolute expiration,
395 unsigned long long *need = cls;
398 return GNUNET_SYSERR;
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "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",
401 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
402 (unsigned int) priority,
403 GNUNET_h2s (key), type,
404 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
407 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
410 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
412 min_expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
414 min_expiration = expiration;
415 GNUNET_STATISTICS_update (stats,
416 gettext_noop ("# bytes purged (low-priority)"),
418 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
424 * Manage available disk space by running tasks
425 * that will discard content if necessary. This
426 * function will be run whenever a request for
427 * "need" bytes of storage could only be satisfied
428 * by eating into the "cache" (and we want our cache
431 * @param need number of bytes of content that were
432 * placed into the "cache" (and hence the
433 * number of bytes that should be removed).
436 manage_space (unsigned long long need)
438 unsigned long long last;
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Asked to free up %llu bytes of cache space\n",
444 while ((need > 0) && (last != need))
447 plugin->api->get_expiration (plugin->api->cls,
455 * Transmit a status code to the client.
457 * @param client receiver of the response
458 * @param code status code
459 * @param msg optional error message (can be NULL)
462 transmit_status (struct GNUNET_SERVICE_Client *client,
466 struct GNUNET_MQ_Envelope *env;
467 struct StatusMessage *sm;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Transmitting `%s' message with value %d and message `%s'\n",
472 "STATUS", code, msg != NULL ? msg : "(none)");
473 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
474 env = GNUNET_MQ_msg_extra (sm,
476 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
477 sm->status = htonl (code);
478 sm->min_expiration = GNUNET_TIME_absolute_hton (min_expiration);
479 GNUNET_memcpy (&sm[1],
482 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
488 * Function that will transmit the given datastore entry
491 * @param cls closure, pointer to the client (of type `struct GNUNET_SERVICE_Client`).
492 * @param key key for the content
493 * @param size number of bytes in data
494 * @param data content stored
495 * @param type type of the content
496 * @param priority priority of the content
497 * @param anonymity anonymity-level for the content
498 * @param expiration expiration time for the content
499 * @param uid unique identifier for the datum;
500 * maybe 0 if no unique identifier is available
501 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue,
502 * #GNUNET_NO to delete the item and continue (if supported)
505 transmit_item (void *cls,
506 const struct GNUNET_HashCode *key,
509 enum GNUNET_BLOCK_Type type,
512 struct GNUNET_TIME_Absolute expiration,
515 struct GNUNET_SERVICE_Client *client = cls;
516 struct GNUNET_MQ_Envelope *env;
517 struct GNUNET_MessageHeader *end;
518 struct DataMessage *dm;
522 /* transmit 'DATA_END' */
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Transmitting DATA_END message\n");
525 env = GNUNET_MQ_msg (end,
526 GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
527 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
531 GNUNET_assert (sizeof (struct DataMessage) + size <
532 GNUNET_MAX_MESSAGE_SIZE);
533 env = GNUNET_MQ_msg_extra (dm,
535 GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
537 dm->size = htonl (size);
538 dm->type = htonl (type);
539 dm->priority = htonl (priority);
540 dm->anonymity = htonl (anonymity);
541 dm->replication = htonl (0);
542 dm->reserved = htonl (0);
543 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
544 dm->uid = GNUNET_htonll (uid);
546 GNUNET_memcpy (&dm[1],
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Transmitting DATA message for `%s' of type %u with expiration %s (in: %s)\n",
553 GNUNET_STRINGS_absolute_time_to_string (expiration),
554 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
556 GNUNET_STATISTICS_update (stats,
557 gettext_noop ("# results found"),
560 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
567 * Handle RESERVE-message.
569 * @param cls identification of the client
570 * @param message the actual message
573 handle_reserve (void *cls,
574 const struct ReserveMessage *msg)
577 * Static counter to produce reservation identifiers.
579 static int reservation_gen;
580 struct GNUNET_SERVICE_Client *client = cls;
581 struct ReservationList *e;
582 unsigned long long used;
583 unsigned long long req;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Processing RESERVE request\n");
589 amount = GNUNET_ntohll (msg->amount);
590 entries = ntohl (msg->entries);
591 used = payload + reserved;
592 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
593 if (used + req > quota)
596 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
598 _("Insufficient space (%llu bytes are available) to satisfy RESERVE request for %llu bytes\n"),
601 if (cache_size < req)
603 /* TODO: document this in the FAQ; essentially, if this
604 * message happens, the insertion request could be blocked
605 * by less-important content from migration because it is
606 * larger than 1/8th of the overall available space, and
607 * we only reserve 1/8th for "fresh" insertions */
608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
609 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
612 transmit_status (client,
615 ("Insufficient space to satisfy request and "
616 "requested amount is larger than cache size"));
620 transmit_status (client,
622 gettext_noop ("Insufficient space to satisfy request"));
624 GNUNET_SERVICE_client_continue (client);
628 GNUNET_STATISTICS_set (stats,
629 gettext_noop ("# reserved"),
632 e = GNUNET_new (struct ReservationList);
633 e->next = reservations;
637 e->entries = entries;
638 e->rid = ++reservation_gen;
639 if (reservation_gen < 0)
640 reservation_gen = 0; /* wrap around */
641 transmit_status (client,
644 GNUNET_SERVICE_client_continue (client);
649 * Handle RELEASE_RESERVE-message.
651 * @param cls identification of the client
652 * @param message the actual message
655 handle_release_reserve (void *cls,
656 const struct ReleaseReserveMessage *msg)
658 struct GNUNET_SERVICE_Client *client = cls;
659 struct ReservationList *pos;
660 struct ReservationList *prev;
661 struct ReservationList *next;
662 int rid = ntohl (msg->rid);
663 unsigned long long rem;
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Processing RELEASE_RESERVE request\n");
669 while (NULL != (pos = next))
680 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
681 GNUNET_assert (reserved >= rem);
683 GNUNET_STATISTICS_set (stats,
684 gettext_noop ("# reserved"),
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Returning %llu remaining reserved bytes to storage pool\n",
691 transmit_status (client,
694 GNUNET_SERVICE_client_continue (client);
700 transmit_status (client,
702 gettext_noop ("Could not find matching reservation"));
703 GNUNET_SERVICE_client_continue (client);
708 * Check that the given message is a valid data message.
710 * @param dm message to check
711 * @return #GNUNET_SYSERR is not well-formed, otherwise #GNUNET_OK
714 check_data (const struct DataMessage *dm)
719 size = ntohs (dm->header.size);
720 dsize = ntohl (dm->size);
721 if (size != dsize + sizeof (struct DataMessage))
724 return GNUNET_SYSERR;
731 * Context for a PUT request used to see if the content is
737 * Client to notify on completion.
739 struct GNUNET_SERVICE_Client *client;
741 #if ! HAVE_UNALIGNED_64_ACCESS
745 /* followed by the 'struct DataMessage' */
753 * @param key key for the item stored
754 * @param size size of the item stored
755 * @param status #GNUNET_OK or #GNUNET_SYSERROR
756 * @param msg error message on error
759 put_continuation (void *cls,
760 const struct GNUNET_HashCode *key,
765 struct PutContext *pc = cls;
767 if (GNUNET_OK == status)
769 GNUNET_STATISTICS_update (stats,
770 gettext_noop ("# bytes stored"),
773 GNUNET_CONTAINER_bloomfilter_add (filter,
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Successfully stored %u bytes under key `%s'\n",
780 transmit_status (pc->client,
784 if (quota - reserved - cache_size < payload)
786 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
787 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
788 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
789 (unsigned long long) (quota - reserved - cache_size),
790 (unsigned long long) payload);
791 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
797 * Actually put the data message.
799 * @param pc put context
802 execute_put (struct PutContext *pc)
804 const struct DataMessage *dm;
806 dm = (const struct DataMessage *) &pc[1];
807 plugin->api->put (plugin->api->cls,
812 ntohl (dm->priority),
813 ntohl (dm->anonymity),
814 ntohl (dm->replication),
815 GNUNET_TIME_absolute_ntoh (dm->expiration),
824 * @param status #GNUNET_OK or #GNUNET_SYSERR
825 * @param msg error message on error
828 check_present_continuation (void *cls,
832 struct GNUNET_SERVICE_Client *client = cls;
834 transmit_status (client,
841 * Function that will check if the given datastore entry
842 * matches the put and if none match executes the put.
844 * @param cls closure, pointer to the client (of type `struct PutContext`).
845 * @param key key for the content
846 * @param size number of bytes in data
847 * @param data content stored
848 * @param type type of the content
849 * @param priority priority of the content
850 * @param anonymity anonymity-level for the content
851 * @param expiration expiration time for the content
852 * @param uid unique identifier for the datum;
853 * maybe 0 if no unique identifier is available
854 * @return #GNUNET_OK usually
855 * #GNUNET_NO to delete the item
858 check_present (void *cls,
859 const struct GNUNET_HashCode *key,
862 enum GNUNET_BLOCK_Type type,
865 struct GNUNET_TIME_Absolute expiration,
868 struct PutContext *pc = cls;
869 const struct DataMessage *dm;
871 dm = (const struct DataMessage *) &pc[1];
877 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
878 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
879 ( (size == ntohl (dm->size)) &&
880 (0 == memcmp (&dm[1],
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Result already present in datastore\n");
886 /* FIXME: change API to allow increasing 'replication' counter */
887 if ((ntohl (dm->priority) > 0) ||
888 (GNUNET_TIME_absolute_ntoh (dm->expiration).abs_value_us >
889 expiration.abs_value_us))
890 plugin->api->update (plugin->api->cls,
892 ntohl (dm->priority),
893 GNUNET_TIME_absolute_ntoh (dm->expiration),
894 &check_present_continuation,
898 transmit_status (pc->client,
913 * Verify PUT-message.
915 * @param cls identification of the client
916 * @param message the actual message
917 * @return #GNUNET_OK if @a dm is well-formed
920 check_put (void *cls,
921 const struct DataMessage *dm)
923 if (GNUNET_OK != check_data (dm))
926 return GNUNET_SYSERR;
933 * Handle PUT-message.
935 * @param cls identification of the client
936 * @param message the actual message
939 handle_put (void *cls,
940 const struct DataMessage *dm)
942 struct GNUNET_SERVICE_Client *client = cls;
944 struct ReservationList *pos;
945 struct PutContext *pc;
946 struct GNUNET_HashCode vhash;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Processing PUT request for `%s' of type %u\n",
951 GNUNET_h2s (&dm->key),
952 (uint32_t) ntohl (dm->type));
953 rid = ntohl (dm->rid);
954 size = ntohl (dm->size);
958 while ((NULL != pos) && (rid != pos->rid))
960 GNUNET_break (pos != NULL);
963 GNUNET_break (pos->entries > 0);
964 GNUNET_break (pos->amount >= size);
967 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
968 GNUNET_STATISTICS_set (stats,
969 gettext_noop ("# reserved"),
974 pc = GNUNET_malloc (sizeof (struct PutContext) + size +
975 sizeof (struct DataMessage));
977 GNUNET_memcpy (&pc[1],
979 size + sizeof (struct DataMessage));
980 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
983 GNUNET_CRYPTO_hash (&dm[1],
986 plugin->api->get_key (plugin->api->cls,
993 GNUNET_SERVICE_client_continue (client);
997 GNUNET_SERVICE_client_continue (client);
1002 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET-message.
1004 * @param cls identification of the client
1005 * @param msg the actual message
1008 handle_get (void *cls,
1009 const struct GetMessage *msg)
1011 struct GNUNET_SERVICE_Client *client = cls;
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "Processing GET request of type %u\n",
1015 (uint32_t) ntohl (msg->type));
1016 GNUNET_STATISTICS_update (stats,
1017 gettext_noop ("# GET requests received"),
1020 plugin->api->get_key (plugin->api->cls,
1021 GNUNET_ntohll (msg->offset),
1027 GNUNET_SERVICE_client_continue (client);
1032 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY-message.
1034 * @param cls closure
1035 * @param msg the actual message
1038 handle_get_key (void *cls,
1039 const struct GetKeyMessage *msg)
1041 struct GNUNET_SERVICE_Client *client = cls;
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Processing GET request for `%s' of type %u\n",
1045 GNUNET_h2s (&msg->key),
1046 (uint32_t) ntohl (msg->type));
1047 GNUNET_STATISTICS_update (stats,
1048 gettext_noop ("# GET KEY requests received"),
1052 GNUNET_CONTAINER_bloomfilter_test (filter,
1055 /* don't bother database... */
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Empty result set for GET request for `%s' (bloomfilter).\n",
1058 GNUNET_h2s (&msg->key));
1059 GNUNET_STATISTICS_update (stats,
1061 ("# requests filtered by bloomfilter"),
1064 transmit_item (client,
1065 NULL, 0, NULL, 0, 0, 0,
1066 GNUNET_TIME_UNIT_ZERO_ABS,
1068 GNUNET_SERVICE_client_continue (client);
1071 plugin->api->get_key (plugin->api->cls,
1072 GNUNET_ntohll (msg->offset),
1078 GNUNET_SERVICE_client_continue (client);
1083 * Handle GET_REPLICATION-message.
1085 * @param cls identification of the client
1086 * @param message the actual message
1089 handle_get_replication (void *cls,
1090 const struct GNUNET_MessageHeader *message)
1092 struct GNUNET_SERVICE_Client *client = cls;
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Processing GET_REPLICATION request\n");
1096 GNUNET_STATISTICS_update (stats,
1097 gettext_noop ("# GET REPLICATION requests received"),
1100 plugin->api->get_replication (plugin->api->cls,
1103 GNUNET_SERVICE_client_continue (client);
1108 * Handle GET_ZERO_ANONYMITY-message.
1110 * @param cls client identification of the client
1111 * @param message the actual message
1114 handle_get_zero_anonymity (void *cls,
1115 const struct GetZeroAnonymityMessage *msg)
1117 struct GNUNET_SERVICE_Client *client = cls;
1118 enum GNUNET_BLOCK_Type type;
1120 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1121 if (type == GNUNET_BLOCK_TYPE_ANY)
1124 GNUNET_SERVICE_client_drop (client);
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Processing GET_ZERO_ANONYMITY request\n");
1129 GNUNET_STATISTICS_update (stats,
1130 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1133 plugin->api->get_zero_anonymity (plugin->api->cls,
1134 GNUNET_ntohll (msg->offset),
1138 GNUNET_SERVICE_client_continue (client);
1143 * Callback function that will cause the item that is passed
1144 * in to be deleted (by returning #GNUNET_NO).
1146 * @param cls closure
1147 * @param key key for the content
1148 * @param size number of bytes in data
1149 * @param data content stored
1150 * @param type type of the content
1151 * @param priority priority of the content
1152 * @param anonymity anonymity-level for the content
1153 * @param expiration expiration time for the content
1154 * @param uid unique identifier for the datum
1155 * @return #GNUNET_OK to keep the item
1156 * #GNUNET_NO to delete the item
1159 remove_callback (void *cls,
1160 const struct GNUNET_HashCode *key,
1163 enum GNUNET_BLOCK_Type type,
1166 struct GNUNET_TIME_Absolute expiration,
1169 struct GNUNET_SERVICE_Client *client = cls;
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "No further matches for REMOVE request.\n");
1175 transmit_status (client,
1177 _("Content not found"));
1178 return GNUNET_OK; /* last item */
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Item %llu matches REMOVE request for key `%s' and type %u.\n",
1182 (unsigned long long) uid,
1185 GNUNET_STATISTICS_update (stats,
1186 gettext_noop ("# bytes removed (explicit request)"),
1189 GNUNET_CONTAINER_bloomfilter_remove (filter,
1191 transmit_status (client,
1199 * Verify REMOVE-message.
1201 * @param cls identification of the client
1202 * @param message the actual message
1203 * @return #GNUNET_OK if @a dm is well-formed
1206 check_remove (void *cls,
1207 const struct DataMessage *dm)
1209 if (GNUNET_OK != check_data (dm))
1212 return GNUNET_SYSERR;
1219 * Handle REMOVE-message.
1221 * @param cls closure
1222 * @param client identification of the client
1223 * @param message the actual message
1226 handle_remove (void *cls,
1227 const struct DataMessage *dm)
1229 struct GNUNET_SERVICE_Client *client = cls;
1230 struct GNUNET_HashCode vhash;
1232 GNUNET_STATISTICS_update (stats,
1233 gettext_noop ("# REMOVE requests received"),
1235 GNUNET_CRYPTO_hash (&dm[1],
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Processing REMOVE request for `%s' of type %u\n",
1240 GNUNET_h2s (&dm->key),
1241 (uint32_t) ntohl (dm->type));
1242 plugin->api->get_key (plugin->api->cls,
1246 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1249 GNUNET_SERVICE_client_continue (client);
1254 * Handle DROP-message.
1256 * @param cls identification of the client
1257 * @param message the actual message
1260 handle_drop (void *cls,
1261 const struct GNUNET_MessageHeader *message)
1263 struct GNUNET_SERVICE_Client *client = cls;
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Processing DROP request\n");
1267 do_drop = GNUNET_YES;
1268 GNUNET_SERVICE_client_continue (client);
1273 * Function called by plugins to notify us about a
1274 * change in their disk utilization.
1276 * @param cls closure (NULL)
1277 * @param delta change in disk utilization,
1278 * 0 for "reset to empty"
1281 disk_utilization_change_cb (void *cls,
1284 if ((delta < 0) && (payload < -delta))
1286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1287 _("Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1288 (long long) payload,
1289 (long long) -delta);
1290 plugin->api->estimate_size (plugin->api->cls,
1292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1293 _("New payload: %lld\n"),
1294 (long long) payload);
1300 if (last_sync >= MAX_STAT_SYNC_LAG)
1306 * Callback function to process statistic values.
1308 * @param cls closure (struct Plugin*)
1309 * @param subsystem name of subsystem that created the statistic
1310 * @param name the name of the datum
1311 * @param value the current value
1312 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1313 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1316 process_stat_in (void *cls,
1317 const char *subsystem,
1322 GNUNET_assert (GNUNET_NO == stats_worked);
1323 stats_worked = GNUNET_YES;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1327 (unsigned long long) value,
1328 (unsigned long long) payload);
1334 * Load the datastore plugin.
1336 static struct DatastorePlugin *
1339 struct DatastorePlugin *ret;
1342 ret = GNUNET_new (struct DatastorePlugin);
1344 ret->env.duc = &disk_utilization_change_cb;
1345 ret->env.cls = NULL;
1346 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1347 _("Loading `%s' datastore plugin\n"),
1349 GNUNET_asprintf (&libname,
1350 "libgnunet_plugin_datastore_%s",
1352 ret->short_name = GNUNET_strdup (plugin_name);
1353 ret->lib_name = libname;
1354 ret->api = GNUNET_PLUGIN_load (libname,
1356 if (NULL == ret->api)
1358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1359 _("Failed to load datastore plugin for `%s'\n"),
1361 GNUNET_free (ret->short_name);
1362 GNUNET_free (libname);
1371 * Function called when the service shuts
1372 * down. Unloads our datastore plugin.
1374 * @param plug plugin to unload
1377 unload_plugin (struct DatastorePlugin *plug)
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Datastore service is unloading plugin...\n");
1381 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1382 GNUNET_free (plug->lib_name);
1383 GNUNET_free (plug->short_name);
1389 * Initialization complete, start operating the service.
1394 GNUNET_SERVICE_resume (service);
1396 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1403 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1405 * @param cls the bloomfilter
1406 * @param key key to add
1407 * @param count number of times to add key
1410 add_key_to_bloomfilter (void *cls,
1411 const struct GNUNET_HashCode *key,
1414 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1419 _("Bloomfilter construction complete.\n"));
1425 GNUNET_CONTAINER_bloomfilter_add (bf,
1431 * We finished receiving the statistic. Initialize the plugin; if
1432 * loading the statistic failed, run the estimator.
1435 * @param success #GNUNET_NO if we failed to read the stat
1438 process_stat_done (void *cls,
1442 if (NULL != stat_timeout_task)
1444 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1445 stat_timeout_task = NULL;
1447 plugin = load_plugin ();
1450 GNUNET_CONTAINER_bloomfilter_free (filter);
1454 GNUNET_STATISTICS_destroy (stats,
1461 if (GNUNET_NO == stats_worked)
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Failed to obtain value from statistics service, recomputing it\n");
1465 plugin->api->estimate_size (plugin->api->cls,
1467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1468 _("New payload: %lld\n"),
1469 (long long) payload);
1472 if (GNUNET_YES == refresh_bf)
1474 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1475 _("Rebuilding bloomfilter. Please be patient.\n"));
1476 if (NULL != plugin->api->get_keys)
1478 plugin->api->get_keys (plugin->api->cls,
1479 &add_key_to_bloomfilter,
1485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1486 _("Plugin does not support get_keys function. Please fix!\n"));
1494 * Fetching stats took to long, run without.
1499 stat_timeout (void *cls)
1501 stat_timeout_task = NULL;
1502 GNUNET_STATISTICS_get_cancel (stat_get);
1503 process_stat_done (NULL,
1509 * Task run during shutdown.
1512 cleaning_task (void *cls)
1514 cleaning_done = GNUNET_YES;
1515 if (NULL != expired_kill_task)
1517 GNUNET_SCHEDULER_cancel (expired_kill_task);
1518 expired_kill_task = NULL;
1520 if (GNUNET_YES == do_drop)
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 "Dropping database!\n");
1524 plugin->api->drop (plugin->api->cls);
1530 unload_plugin (plugin);
1535 GNUNET_CONTAINER_bloomfilter_free (filter);
1538 if (NULL != stat_get)
1540 GNUNET_STATISTICS_get_cancel (stat_get);
1543 if (NULL != stat_timeout_task)
1545 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1546 stat_timeout_task = NULL;
1548 GNUNET_free_non_null (plugin_name);
1554 GNUNET_STATISTICS_destroy (stats,
1558 GNUNET_free (quota_stat_name);
1559 quota_stat_name = NULL;
1564 * Add a client to our list of active clients.
1567 * @param client client to add
1568 * @param mq message queue for @a client
1572 client_connect_cb (void *cls,
1573 struct GNUNET_SERVICE_Client *client,
1574 struct GNUNET_MQ_Handle *mq)
1581 * Called whenever a client is disconnected.
1582 * Frees our resources associated with that client.
1584 * @param cls closure
1585 * @param client identification of the client
1586 * @param app_ctx must match @a client
1589 client_disconnect_cb (void *cls,
1590 struct GNUNET_SERVICE_Client *client,
1593 struct ReservationList *pos;
1594 struct ReservationList *prev;
1595 struct ReservationList *next;
1597 GNUNET_assert (app_ctx == client);
1603 if (pos->client == client)
1606 reservations = next;
1609 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1618 GNUNET_STATISTICS_set (stats,
1619 gettext_noop ("# reserved"),
1627 * Process datastore requests.
1629 * @param cls closure
1630 * @param serv the initialized service
1631 * @param c configuration to use
1635 const struct GNUNET_CONFIGURATION_Handle *c,
1636 struct GNUNET_SERVICE_Handle *serv)
1640 unsigned int bf_size;
1645 GNUNET_CONFIGURATION_get_value_string (cfg,
1650 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1655 GNUNET_asprintf ("a_stat_name,
1656 _("# bytes used in file-sharing datastore `%s'"),
1659 GNUNET_CONFIGURATION_get_value_size (cfg,
1664 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1669 stats = GNUNET_STATISTICS_create ("datastore",
1671 GNUNET_STATISTICS_set (stats,
1672 gettext_noop ("# quota"),
1675 cache_size = quota / 8; /* Or should we make this an option? */
1676 GNUNET_STATISTICS_set (stats,
1677 gettext_noop ("# cache size"),
1680 if (quota / (32 * 1024LL) > MAX_BF_SIZE)
1681 bf_size = MAX_BF_SIZE;
1683 bf_size = quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1686 GNUNET_CONFIGURATION_get_value_filename (cfg,
1690 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1693 _("Could not use specified filename `%s' for bloomfilter.\n"),
1694 NULL != fn ? fn : "");
1695 GNUNET_free_non_null (fn);
1700 GNUNET_asprintf (&pfn, "%s.%s", fn, plugin_name);
1701 if (GNUNET_YES == GNUNET_DISK_file_test (pfn))
1703 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1706 /* file exists but not valid, remove and try again, but refresh */
1707 if (0 != UNLINK (pfn))
1709 /* failed to remove, run without file */
1710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1711 _("Failed to remove bogus bloomfilter file `%s'\n"),
1715 filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1716 refresh_bf = GNUNET_YES;
1720 /* try again after remove */
1721 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1722 refresh_bf = GNUNET_YES;
1725 /* failed yet again, give up on using file */
1726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1727 _("Failed to remove bogus bloomfilter file `%s'\n"),
1731 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1737 /* normal case: have an existing valid bf file, no need to refresh */
1738 refresh_bf = GNUNET_NO;
1743 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1744 refresh_bf = GNUNET_YES;
1750 filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
1752 5); /* approx. 3% false positives at max use */
1753 refresh_bf = GNUNET_YES;
1755 GNUNET_free_non_null (fn);
1758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1759 _("Failed to initialize bloomfilter.\n"));
1762 GNUNET_STATISTICS_destroy (stats,
1768 GNUNET_SERVICE_suspend (service);
1770 GNUNET_STATISTICS_get (stats,
1776 if (NULL == stat_get)
1777 process_stat_done (NULL,
1781 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1784 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1790 * Define "main" method using service macro.
1794 GNUNET_SERVICE_OPTION_NONE,
1797 &client_disconnect_cb,
1799 GNUNET_MQ_hd_fixed_size (reserve,
1800 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1801 struct ReserveMessage,
1803 GNUNET_MQ_hd_fixed_size (release_reserve,
1804 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1805 struct ReleaseReserveMessage,
1807 GNUNET_MQ_hd_var_size (put,
1808 GNUNET_MESSAGE_TYPE_DATASTORE_PUT,
1811 GNUNET_MQ_hd_fixed_size (get,
1812 GNUNET_MESSAGE_TYPE_DATASTORE_GET,
1815 GNUNET_MQ_hd_fixed_size (get_key,
1816 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY,
1817 struct GetKeyMessage,
1819 GNUNET_MQ_hd_fixed_size (get_replication,
1820 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1821 struct GNUNET_MessageHeader,
1823 GNUNET_MQ_hd_fixed_size (get_zero_anonymity,
1824 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1825 struct GetZeroAnonymityMessage,
1827 GNUNET_MQ_hd_var_size (remove,
1828 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE,
1831 GNUNET_MQ_hd_fixed_size (drop,
1832 GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1833 struct GNUNET_MessageHeader,
1835 GNUNET_MQ_handler_end ());
1838 /* end of gnunet-service-datastore.c */