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_SERVER_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 (int32_t) 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),
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",
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),
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 * Function called with the result of an update operation.
1085 * @param cls closure
1086 * @param status #GNUNET_OK or #GNUNET_SYSERR
1087 * @param msg error message on error
1090 update_continuation (void *cls,
1094 struct GNUNET_SERVICE_Client *client = cls;
1096 transmit_status (client,
1103 * Handle UPDATE-message.
1105 * @param cls client identification of the client
1106 * @param message the actual message
1109 handle_update (void *cls,
1110 const struct UpdateMessage *msg)
1112 struct GNUNET_SERVICE_Client *client = cls;
1114 GNUNET_STATISTICS_update (stats,
1115 gettext_noop ("# UPDATE requests received"),
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Processing UPDATE request for %llu\n",
1120 (unsigned long long) GNUNET_ntohll (msg->uid));
1121 plugin->api->update (plugin->api->cls,
1122 GNUNET_ntohll (msg->uid),
1123 (int32_t) ntohl (msg->priority),
1124 GNUNET_TIME_absolute_ntoh (msg->expiration),
1125 &update_continuation,
1127 GNUNET_SERVICE_client_continue (client);
1132 * Handle GET_REPLICATION-message.
1134 * @param cls identification of the client
1135 * @param message the actual message
1138 handle_get_replication (void *cls,
1139 const struct GNUNET_MessageHeader *message)
1141 struct GNUNET_SERVICE_Client *client = cls;
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Processing GET_REPLICATION request\n");
1145 GNUNET_STATISTICS_update (stats,
1146 gettext_noop ("# GET REPLICATION requests received"),
1149 plugin->api->get_replication (plugin->api->cls,
1152 GNUNET_SERVICE_client_continue (client);
1157 * Handle GET_ZERO_ANONYMITY-message.
1159 * @param cls client identification of the client
1160 * @param message the actual message
1163 handle_get_zero_anonymity (void *cls,
1164 const struct GetZeroAnonymityMessage *msg)
1166 struct GNUNET_SERVICE_Client *client = cls;
1167 enum GNUNET_BLOCK_Type type;
1169 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1170 if (type == GNUNET_BLOCK_TYPE_ANY)
1173 GNUNET_SERVICE_client_drop (client);
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Processing GET_ZERO_ANONYMITY request\n");
1178 GNUNET_STATISTICS_update (stats,
1179 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1182 plugin->api->get_zero_anonymity (plugin->api->cls,
1183 GNUNET_ntohll (msg->offset),
1187 GNUNET_SERVICE_client_continue (client);
1192 * Callback function that will cause the item that is passed
1193 * in to be deleted (by returning #GNUNET_NO).
1195 * @param cls closure
1196 * @param key key for the content
1197 * @param size number of bytes in data
1198 * @param data content stored
1199 * @param type type of the content
1200 * @param priority priority of the content
1201 * @param anonymity anonymity-level for the content
1202 * @param expiration expiration time for the content
1203 * @param uid unique identifier for the datum
1204 * @return #GNUNET_OK to keep the item
1205 * #GNUNET_NO to delete the item
1208 remove_callback (void *cls,
1209 const struct GNUNET_HashCode *key,
1212 enum GNUNET_BLOCK_Type type,
1215 struct GNUNET_TIME_Absolute expiration,
1218 struct GNUNET_SERVICE_Client *client = cls;
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "No further matches for REMOVE request.\n");
1224 transmit_status (client,
1226 _("Content not found"));
1227 return GNUNET_OK; /* last item */
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Item %llu matches REMOVE request for key `%s' and type %u.\n",
1231 (unsigned long long) uid,
1234 GNUNET_STATISTICS_update (stats,
1235 gettext_noop ("# bytes removed (explicit request)"),
1238 GNUNET_CONTAINER_bloomfilter_remove (filter,
1240 transmit_status (client,
1248 * Verify REMOVE-message.
1250 * @param cls identification of the client
1251 * @param message the actual message
1252 * @return #GNUNET_OK if @a dm is well-formed
1255 check_remove (void *cls,
1256 const struct DataMessage *dm)
1258 if (GNUNET_OK != check_data (dm))
1261 return GNUNET_SYSERR;
1268 * Handle REMOVE-message.
1270 * @param cls closure
1271 * @param client identification of the client
1272 * @param message the actual message
1275 handle_remove (void *cls,
1276 const struct DataMessage *dm)
1278 struct GNUNET_SERVICE_Client *client = cls;
1279 struct GNUNET_HashCode vhash;
1281 GNUNET_STATISTICS_update (stats,
1282 gettext_noop ("# REMOVE requests received"),
1284 GNUNET_CRYPTO_hash (&dm[1],
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Processing REMOVE request for `%s' of type %u\n",
1289 GNUNET_h2s (&dm->key),
1291 plugin->api->get_key (plugin->api->cls,
1295 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1298 GNUNET_SERVICE_client_continue (client);
1303 * Handle DROP-message.
1305 * @param cls identification of the client
1306 * @param message the actual message
1309 handle_drop (void *cls,
1310 const struct GNUNET_MessageHeader *message)
1312 struct GNUNET_SERVICE_Client *client = cls;
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Processing DROP request\n");
1316 do_drop = GNUNET_YES;
1317 GNUNET_SERVICE_client_continue (client);
1322 * Function called by plugins to notify us about a
1323 * change in their disk utilization.
1325 * @param cls closure (NULL)
1326 * @param delta change in disk utilization,
1327 * 0 for "reset to empty"
1330 disk_utilization_change_cb (void *cls,
1333 if ((delta < 0) && (payload < -delta))
1335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1336 _("Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1337 (long long) payload,
1338 (long long) -delta);
1339 plugin->api->estimate_size (plugin->api->cls,
1341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1342 _("New payload: %lld\n"),
1343 (long long) payload);
1349 if (last_sync >= MAX_STAT_SYNC_LAG)
1355 * Callback function to process statistic values.
1357 * @param cls closure (struct Plugin*)
1358 * @param subsystem name of subsystem that created the statistic
1359 * @param name the name of the datum
1360 * @param value the current value
1361 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1362 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1365 process_stat_in (void *cls,
1366 const char *subsystem,
1371 GNUNET_assert (GNUNET_NO == stats_worked);
1372 stats_worked = GNUNET_YES;
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1376 (unsigned long long) value,
1377 (unsigned long long) payload);
1383 * Load the datastore plugin.
1385 static struct DatastorePlugin *
1388 struct DatastorePlugin *ret;
1391 ret = GNUNET_new (struct DatastorePlugin);
1393 ret->env.duc = &disk_utilization_change_cb;
1394 ret->env.cls = NULL;
1395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1396 _("Loading `%s' datastore plugin\n"),
1398 GNUNET_asprintf (&libname,
1399 "libgnunet_plugin_datastore_%s",
1401 ret->short_name = GNUNET_strdup (plugin_name);
1402 ret->lib_name = libname;
1403 ret->api = GNUNET_PLUGIN_load (libname,
1405 if (NULL == ret->api)
1407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1408 _("Failed to load datastore plugin for `%s'\n"),
1410 GNUNET_free (ret->short_name);
1411 GNUNET_free (libname);
1420 * Function called when the service shuts
1421 * down. Unloads our datastore plugin.
1423 * @param plug plugin to unload
1426 unload_plugin (struct DatastorePlugin *plug)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Datastore service is unloading plugin...\n");
1430 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1431 GNUNET_free (plug->lib_name);
1432 GNUNET_free (plug->short_name);
1438 * Initialization complete, start operating the service.
1443 GNUNET_SERVICE_resume (service);
1445 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1452 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1454 * @param cls the bloomfilter
1455 * @param key key to add
1456 * @param count number of times to add key
1459 add_key_to_bloomfilter (void *cls,
1460 const struct GNUNET_HashCode *key,
1463 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1468 _("Bloomfilter construction complete.\n"));
1474 GNUNET_CONTAINER_bloomfilter_add (bf,
1480 * We finished receiving the statistic. Initialize the plugin; if
1481 * loading the statistic failed, run the estimator.
1484 * @param success #GNUNET_NO if we failed to read the stat
1487 process_stat_done (void *cls,
1491 if (NULL != stat_timeout_task)
1493 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1494 stat_timeout_task = NULL;
1496 plugin = load_plugin ();
1499 GNUNET_CONTAINER_bloomfilter_free (filter);
1503 GNUNET_STATISTICS_destroy (stats,
1510 if (GNUNET_NO == stats_worked)
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 "Failed to obtain value from statistics service, recomputing it\n");
1514 plugin->api->estimate_size (plugin->api->cls,
1516 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1517 _("New payload: %lld\n"),
1518 (long long) payload);
1521 if (GNUNET_YES == refresh_bf)
1523 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1524 _("Rebuilding bloomfilter. Please be patient.\n"));
1525 if (NULL != plugin->api->get_keys)
1527 plugin->api->get_keys (plugin->api->cls,
1528 &add_key_to_bloomfilter,
1534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1535 _("Plugin does not support get_keys function. Please fix!\n"));
1543 * Fetching stats took to long, run without.
1548 stat_timeout (void *cls)
1550 stat_timeout_task = NULL;
1551 GNUNET_STATISTICS_get_cancel (stat_get);
1552 process_stat_done (NULL,
1558 * Task run during shutdown.
1561 cleaning_task (void *cls)
1563 cleaning_done = GNUNET_YES;
1564 if (NULL != expired_kill_task)
1566 GNUNET_SCHEDULER_cancel (expired_kill_task);
1567 expired_kill_task = NULL;
1569 if (GNUNET_YES == do_drop)
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Dropping database!\n");
1573 plugin->api->drop (plugin->api->cls);
1579 unload_plugin (plugin);
1584 GNUNET_CONTAINER_bloomfilter_free (filter);
1587 if (NULL != stat_get)
1589 GNUNET_STATISTICS_get_cancel (stat_get);
1592 if (NULL != stat_timeout_task)
1594 GNUNET_SCHEDULER_cancel (stat_timeout_task);
1595 stat_timeout_task = NULL;
1597 GNUNET_free_non_null (plugin_name);
1603 GNUNET_STATISTICS_destroy (stats,
1607 GNUNET_free (quota_stat_name);
1608 quota_stat_name = NULL;
1613 * Add a client to our list of active clients.
1616 * @param client client to add
1617 * @param mq message queue for @a client
1621 client_connect_cb (void *cls,
1622 struct GNUNET_SERVICE_Client *client,
1623 struct GNUNET_MQ_Handle *mq)
1630 * Called whenever a client is disconnected.
1631 * Frees our resources associated with that client.
1633 * @param cls closure
1634 * @param client identification of the client
1635 * @param app_ctx must match @a client
1638 client_disconnect_cb (void *cls,
1639 struct GNUNET_SERVICE_Client *client,
1642 struct ReservationList *pos;
1643 struct ReservationList *prev;
1644 struct ReservationList *next;
1646 GNUNET_assert (app_ctx == client);
1652 if (pos->client == client)
1655 reservations = next;
1658 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1667 GNUNET_STATISTICS_set (stats,
1668 gettext_noop ("# reserved"),
1676 * Process datastore requests.
1678 * @param cls closure
1679 * @param serv the initialized service
1680 * @param c configuration to use
1684 const struct GNUNET_CONFIGURATION_Handle *c,
1685 struct GNUNET_SERVICE_Handle *serv)
1689 unsigned int bf_size;
1694 GNUNET_CONFIGURATION_get_value_string (cfg,
1699 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1704 GNUNET_asprintf ("a_stat_name,
1705 _("# bytes used in file-sharing datastore `%s'"),
1708 GNUNET_CONFIGURATION_get_value_size (cfg,
1713 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1718 stats = GNUNET_STATISTICS_create ("datastore",
1720 GNUNET_STATISTICS_set (stats,
1721 gettext_noop ("# quota"),
1724 cache_size = quota / 8; /* Or should we make this an option? */
1725 GNUNET_STATISTICS_set (stats,
1726 gettext_noop ("# cache size"),
1729 if (quota / (32 * 1024LL) > MAX_BF_SIZE)
1730 bf_size = MAX_BF_SIZE;
1732 bf_size = quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1735 GNUNET_CONFIGURATION_get_value_filename (cfg,
1739 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1742 _("Could not use specified filename `%s' for bloomfilter.\n"),
1743 NULL != fn ? fn : "");
1744 GNUNET_free_non_null (fn);
1749 GNUNET_asprintf (&pfn, "%s.%s", fn, plugin_name);
1750 if (GNUNET_YES == GNUNET_DISK_file_test (pfn))
1752 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1755 /* file exists but not valid, remove and try again, but refresh */
1756 if (0 != UNLINK (pfn))
1758 /* failed to remove, run without file */
1759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1760 _("Failed to remove bogus bloomfilter file `%s'\n"),
1764 filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1765 refresh_bf = GNUNET_YES;
1769 /* try again after remove */
1770 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1771 refresh_bf = GNUNET_YES;
1774 /* failed yet again, give up on using file */
1775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1776 _("Failed to remove bogus bloomfilter file `%s'\n"),
1780 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1786 /* normal case: have an existing valid bf file, no need to refresh */
1787 refresh_bf = GNUNET_NO;
1792 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1793 refresh_bf = GNUNET_YES;
1799 filter = GNUNET_CONTAINER_bloomfilter_init (NULL,
1801 5); /* approx. 3% false positives at max use */
1802 refresh_bf = GNUNET_YES;
1804 GNUNET_free_non_null (fn);
1807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1808 _("Failed to initialize bloomfilter.\n"));
1811 GNUNET_STATISTICS_destroy (stats,
1817 GNUNET_SERVICE_suspend (service);
1819 GNUNET_STATISTICS_get (stats,
1825 if (NULL == stat_get)
1826 process_stat_done (NULL,
1830 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1833 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1839 * Define "main" method using service macro.
1843 GNUNET_SERVICE_OPTION_NONE,
1846 &client_disconnect_cb,
1848 GNUNET_MQ_hd_fixed_size (reserve,
1849 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1850 struct ReserveMessage,
1852 GNUNET_MQ_hd_fixed_size (release_reserve,
1853 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1854 struct ReleaseReserveMessage,
1856 GNUNET_MQ_hd_var_size (put,
1857 GNUNET_MESSAGE_TYPE_DATASTORE_PUT,
1860 GNUNET_MQ_hd_fixed_size (update,
1861 GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1862 struct UpdateMessage,
1864 GNUNET_MQ_hd_fixed_size (get,
1865 GNUNET_MESSAGE_TYPE_DATASTORE_GET,
1868 GNUNET_MQ_hd_fixed_size (get_key,
1869 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY,
1870 struct GetKeyMessage,
1872 GNUNET_MQ_hd_fixed_size (get_replication,
1873 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1874 struct GNUNET_MessageHeader,
1876 GNUNET_MQ_hd_fixed_size (get_zero_anonymity,
1877 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1878 struct GetZeroAnonymityMessage,
1880 GNUNET_MQ_hd_var_size (remove,
1881 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE,
1884 GNUNET_MQ_hd_fixed_size (drop,
1885 GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1886 struct GNUNET_MessageHeader,
1888 GNUNET_MQ_handler_end ());
1891 /* end of gnunet-service-datastore.c */