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,
994 GNUNET_SERVICE_client_continue (client);
998 GNUNET_SERVICE_client_continue (client);
1003 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET-message.
1005 * @param cls identification of the client
1006 * @param msg the actual message
1009 handle_get (void *cls,
1010 const struct GetMessage *msg)
1012 struct GNUNET_SERVICE_Client *client = cls;
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Processing GET request of type %u\n",
1016 (uint32_t) ntohl (msg->type));
1017 GNUNET_STATISTICS_update (stats,
1018 gettext_noop ("# GET requests received"),
1021 plugin->api->get_key (plugin->api->cls,
1022 GNUNET_ntohll (msg->next_uid),
1029 GNUNET_SERVICE_client_continue (client);
1034 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY-message.
1036 * @param cls closure
1037 * @param msg the actual message
1040 handle_get_key (void *cls,
1041 const struct GetKeyMessage *msg)
1043 struct GNUNET_SERVICE_Client *client = cls;
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Processing GET request for `%s' of type %u\n",
1047 GNUNET_h2s (&msg->key),
1048 (uint32_t) ntohl (msg->type));
1049 GNUNET_STATISTICS_update (stats,
1050 gettext_noop ("# GET KEY requests received"),
1054 GNUNET_CONTAINER_bloomfilter_test (filter,
1057 /* don't bother database... */
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Empty result set for GET request for `%s' (bloomfilter).\n",
1060 GNUNET_h2s (&msg->key));
1061 GNUNET_STATISTICS_update (stats,
1063 ("# requests filtered by bloomfilter"),
1066 transmit_item (client,
1067 NULL, 0, NULL, 0, 0, 0,
1068 GNUNET_TIME_UNIT_ZERO_ABS,
1070 GNUNET_SERVICE_client_continue (client);
1073 plugin->api->get_key (plugin->api->cls,
1074 GNUNET_ntohll (msg->next_uid),
1081 GNUNET_SERVICE_client_continue (client);
1086 * Handle GET_REPLICATION-message.
1088 * @param cls identification of the client
1089 * @param message the actual message
1092 handle_get_replication (void *cls,
1093 const struct GNUNET_MessageHeader *message)
1095 struct GNUNET_SERVICE_Client *client = cls;
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Processing GET_REPLICATION request\n");
1099 GNUNET_STATISTICS_update (stats,
1100 gettext_noop ("# GET REPLICATION requests received"),
1103 plugin->api->get_replication (plugin->api->cls,
1106 GNUNET_SERVICE_client_continue (client);
1111 * Handle GET_ZERO_ANONYMITY-message.
1113 * @param cls client identification of the client
1114 * @param message the actual message
1117 handle_get_zero_anonymity (void *cls,
1118 const struct GetZeroAnonymityMessage *msg)
1120 struct GNUNET_SERVICE_Client *client = cls;
1121 enum GNUNET_BLOCK_Type type;
1123 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1124 if (type == GNUNET_BLOCK_TYPE_ANY)
1127 GNUNET_SERVICE_client_drop (client);
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Processing GET_ZERO_ANONYMITY request\n");
1132 GNUNET_STATISTICS_update (stats,
1133 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1136 plugin->api->get_zero_anonymity (plugin->api->cls,
1137 GNUNET_ntohll (msg->next_uid),
1141 GNUNET_SERVICE_client_continue (client);
1146 * Callback function that will cause the item that is passed
1147 * in to be deleted (by returning #GNUNET_NO).
1149 * @param cls closure
1150 * @param key key for the content
1151 * @param size number of bytes in data
1152 * @param data content stored
1153 * @param type type of the content
1154 * @param priority priority of the content
1155 * @param anonymity anonymity-level for the content
1156 * @param expiration expiration time for the content
1157 * @param uid unique identifier for the datum
1158 * @return #GNUNET_OK to keep the item
1159 * #GNUNET_NO to delete the item
1162 remove_callback (void *cls,
1163 const struct GNUNET_HashCode *key,
1166 enum GNUNET_BLOCK_Type type,
1169 struct GNUNET_TIME_Absolute expiration,
1172 struct GNUNET_SERVICE_Client *client = cls;
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "No further matches for REMOVE request.\n");
1178 transmit_status (client,
1180 _("Content not found"));
1181 return GNUNET_OK; /* last item */
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Item %llu matches REMOVE request for key `%s' and type %u.\n",
1185 (unsigned long long) uid,
1188 GNUNET_STATISTICS_update (stats,
1189 gettext_noop ("# bytes removed (explicit request)"),
1192 GNUNET_CONTAINER_bloomfilter_remove (filter,
1194 transmit_status (client,
1202 * Verify REMOVE-message.
1204 * @param cls identification of the client
1205 * @param message the actual message
1206 * @return #GNUNET_OK if @a dm is well-formed
1209 check_remove (void *cls,
1210 const struct DataMessage *dm)
1212 if (GNUNET_OK != check_data (dm))
1215 return GNUNET_SYSERR;
1222 * Handle REMOVE-message.
1224 * @param cls closure
1225 * @param client identification of the client
1226 * @param message the actual message
1229 handle_remove (void *cls,
1230 const struct DataMessage *dm)
1232 struct GNUNET_SERVICE_Client *client = cls;
1233 struct GNUNET_HashCode vhash;
1235 GNUNET_STATISTICS_update (stats,
1236 gettext_noop ("# REMOVE requests received"),
1238 GNUNET_CRYPTO_hash (&dm[1],
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 "Processing REMOVE request for `%s' of type %u\n",
1243 GNUNET_h2s (&dm->key),
1244 (uint32_t) ntohl (dm->type));
1245 plugin->api->get_key (plugin->api->cls,
1250 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1253 GNUNET_SERVICE_client_continue (client);
1258 * Handle DROP-message.
1260 * @param cls identification of the client
1261 * @param message the actual message
1264 handle_drop (void *cls,
1265 const struct GNUNET_MessageHeader *message)
1267 struct GNUNET_SERVICE_Client *client = cls;
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Processing DROP request\n");
1271 do_drop = GNUNET_YES;
1272 GNUNET_SERVICE_client_continue (client);
1277 * Function called by plugins to notify us about a
1278 * change in their disk utilization.
1280 * @param cls closure (NULL)
1281 * @param delta change in disk utilization,
1282 * 0 for "reset to empty"
1285 disk_utilization_change_cb (void *cls,
1288 if ((delta < 0) && (payload < -delta))
1290 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1291 _("Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1292 (long long) payload,
1293 (long long) -delta);
1294 plugin->api->estimate_size (plugin->api->cls,
1296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1297 _("New payload: %lld\n"),
1298 (long long) payload);
1304 if (last_sync >= MAX_STAT_SYNC_LAG)
1310 * Callback function to process statistic values.
1312 * @param cls closure (struct Plugin*)
1313 * @param subsystem name of subsystem that created the statistic
1314 * @param name the name of the datum
1315 * @param value the current value
1316 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1317 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1320 process_stat_in (void *cls,
1321 const char *subsystem,
1326 GNUNET_assert (GNUNET_NO == stats_worked);
1327 stats_worked = GNUNET_YES;
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1331 (unsigned long long) value,
1332 (unsigned long long) payload);
1338 * Load the datastore plugin.
1340 static struct DatastorePlugin *
1343 struct DatastorePlugin *ret;
1346 ret = GNUNET_new (struct DatastorePlugin);
1348 ret->env.duc = &disk_utilization_change_cb;
1349 ret->env.cls = NULL;
1350 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1351 _("Loading `%s' datastore plugin\n"),
1353 GNUNET_asprintf (&libname,
1354 "libgnunet_plugin_datastore_%s",
1356 ret->short_name = GNUNET_strdup (plugin_name);
1357 ret->lib_name = libname;
1358 ret->api = GNUNET_PLUGIN_load (libname,
1360 if (NULL == ret->api)
1362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1363 _("Failed to load datastore plugin for `%s'\n"),
1365 GNUNET_free (ret->short_name);
1366 GNUNET_free (libname);
1375 * Function called when the service shuts
1376 * down. Unloads our datastore plugin.
1378 * @param plug plugin to unload
1381 unload_plugin (struct DatastorePlugin *plug)
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384 "Datastore service is unloading plugin...\n");
1385 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1386 GNUNET_free (plug->lib_name);
1387 GNUNET_free (plug->short_name);
1393 * Initialization complete, start operating the service.
1398 GNUNET_SERVICE_resume (service);
1400 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1407 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1409 * @param cls the bloomfilter
1410 * @param key key to add
1411 * @param count number of times to add key
1414 add_key_to_bloomfilter (void *cls,
1415 const struct GNUNET_HashCode *key,
1418 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1423 _("Bloomfilter construction complete.\n"));
1429 GNUNET_CONTAINER_bloomfilter_add (bf,
1435 * We finished receiving the statistic. Initialize the plugin; if
1436 * loading the statistic failed, run the estimator.
1439 * @param success #GNUNET_NO if we failed to read the stat
1442 process_stat_done (void *cls,
1446 if (NULL != stat_timeout_task)
1448 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1449 stat_timeout_task = NULL;
1451 plugin = load_plugin ();
1454 GNUNET_CONTAINER_bloomfilter_free (filter);
1458 GNUNET_STATISTICS_destroy (stats,
1465 if (GNUNET_NO == stats_worked)
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1468 "Failed to obtain value from statistics service, recomputing it\n");
1469 plugin->api->estimate_size (plugin->api->cls,
1471 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1472 _("New payload: %lld\n"),
1473 (long long) payload);
1476 if (GNUNET_YES == refresh_bf)
1478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1479 _("Rebuilding bloomfilter. Please be patient.\n"));
1480 if (NULL != plugin->api->get_keys)
1482 plugin->api->get_keys (plugin->api->cls,
1483 &add_key_to_bloomfilter,
1489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1490 _("Plugin does not support get_keys function. Please fix!\n"));
1498 * Fetching stats took to long, run without.
1503 stat_timeout (void *cls)
1505 stat_timeout_task = NULL;
1506 GNUNET_STATISTICS_get_cancel (stat_get);
1507 process_stat_done (NULL,
1513 * Task run during shutdown.
1516 cleaning_task (void *cls)
1518 cleaning_done = GNUNET_YES;
1519 if (NULL != expired_kill_task)
1521 GNUNET_SCHEDULER_cancel (expired_kill_task);
1522 expired_kill_task = NULL;
1524 if (GNUNET_YES == do_drop)
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "Dropping database!\n");
1528 plugin->api->drop (plugin->api->cls);
1534 unload_plugin (plugin);
1539 GNUNET_CONTAINER_bloomfilter_free (filter);
1542 if (NULL != stat_get)
1544 GNUNET_STATISTICS_get_cancel (stat_get);
1547 if (NULL != stat_timeout_task)
1549 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1550 stat_timeout_task = NULL;
1552 GNUNET_free_non_null (plugin_name);
1558 GNUNET_STATISTICS_destroy (stats,
1562 GNUNET_free (quota_stat_name);
1563 quota_stat_name = NULL;
1568 * Add a client to our list of active clients.
1571 * @param client client to add
1572 * @param mq message queue for @a client
1576 client_connect_cb (void *cls,
1577 struct GNUNET_SERVICE_Client *client,
1578 struct GNUNET_MQ_Handle *mq)
1585 * Called whenever a client is disconnected.
1586 * Frees our resources associated with that client.
1588 * @param cls closure
1589 * @param client identification of the client
1590 * @param app_ctx must match @a client
1593 client_disconnect_cb (void *cls,
1594 struct GNUNET_SERVICE_Client *client,
1597 struct ReservationList *pos;
1598 struct ReservationList *prev;
1599 struct ReservationList *next;
1601 GNUNET_assert (app_ctx == client);
1607 if (pos->client == client)
1610 reservations = next;
1613 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1622 GNUNET_STATISTICS_set (stats,
1623 gettext_noop ("# reserved"),
1631 * Process datastore requests.
1633 * @param cls closure
1634 * @param serv the initialized service
1635 * @param c configuration to use
1639 const struct GNUNET_CONFIGURATION_Handle *c,
1640 struct GNUNET_SERVICE_Handle *serv)
1644 unsigned int bf_size;
1649 GNUNET_CONFIGURATION_get_value_string (cfg,
1654 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1659 GNUNET_asprintf ("a_stat_name,
1660 _("# bytes used in file-sharing datastore `%s'"),
1663 GNUNET_CONFIGURATION_get_value_size (cfg,
1668 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1673 stats = GNUNET_STATISTICS_create ("datastore",
1675 GNUNET_STATISTICS_set (stats,
1676 gettext_noop ("# quota"),
1679 cache_size = quota / 8; /* Or should we make this an option? */
1680 GNUNET_STATISTICS_set (stats,
1681 gettext_noop ("# cache size"),
1684 if (quota / (32 * 1024LL) > MAX_BF_SIZE)
1685 bf_size = MAX_BF_SIZE;
1687 bf_size = quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1690 GNUNET_CONFIGURATION_get_value_filename (cfg,
1694 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1697 _("Could not use specified filename `%s' for bloomfilter.\n"),
1698 NULL != fn ? fn : "");
1699 GNUNET_free_non_null (fn);
1704 GNUNET_asprintf (&pfn, "%s.%s", fn, plugin_name);
1705 if (GNUNET_YES == GNUNET_DISK_file_test (pfn))
1707 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1710 /* file exists but not valid, remove and try again, but refresh */
1711 if (0 != UNLINK (pfn))
1713 /* failed to remove, run without file */
1714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1715 _("Failed to remove bogus bloomfilter file `%s'\n"),
1719 filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1720 refresh_bf = GNUNET_YES;
1724 /* try again after remove */
1725 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1726 refresh_bf = GNUNET_YES;
1729 /* failed yet again, give up on using file */
1730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1731 _("Failed to remove bogus bloomfilter file `%s'\n"),
1735 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1741 /* normal case: have an existing valid bf file, no need to refresh */
1742 refresh_bf = GNUNET_NO;
1747 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1748 refresh_bf = GNUNET_YES;
1754 filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
1756 5); /* approx. 3% false positives at max use */
1757 refresh_bf = GNUNET_YES;
1759 GNUNET_free_non_null (fn);
1762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1763 _("Failed to initialize bloomfilter.\n"));
1766 GNUNET_STATISTICS_destroy (stats,
1772 GNUNET_SERVICE_suspend (service);
1774 GNUNET_STATISTICS_get (stats,
1780 if (NULL == stat_get)
1781 process_stat_done (NULL,
1785 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1788 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1794 * Define "main" method using service macro.
1798 GNUNET_SERVICE_OPTION_NONE,
1801 &client_disconnect_cb,
1803 GNUNET_MQ_hd_fixed_size (reserve,
1804 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1805 struct ReserveMessage,
1807 GNUNET_MQ_hd_fixed_size (release_reserve,
1808 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1809 struct ReleaseReserveMessage,
1811 GNUNET_MQ_hd_var_size (put,
1812 GNUNET_MESSAGE_TYPE_DATASTORE_PUT,
1815 GNUNET_MQ_hd_fixed_size (get,
1816 GNUNET_MESSAGE_TYPE_DATASTORE_GET,
1819 GNUNET_MQ_hd_fixed_size (get_key,
1820 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY,
1821 struct GetKeyMessage,
1823 GNUNET_MQ_hd_fixed_size (get_replication,
1824 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1825 struct GNUNET_MessageHeader,
1827 GNUNET_MQ_hd_fixed_size (get_zero_anonymity,
1828 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1829 struct GetZeroAnonymityMessage,
1831 GNUNET_MQ_hd_var_size (remove,
1832 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE,
1835 GNUNET_MQ_hd_fixed_size (drop,
1836 GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1837 struct GNUNET_MessageHeader,
1839 GNUNET_MQ_handler_end ());
1842 /* end of gnunet-service-datastore.c */