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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 \
49 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15)
52 * How fast are we allowed to query the database for deleting
53 * expired content? (1 item per second).
55 #define MIN_EXPIRE_DELAY \
56 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
59 * Name under which we store current space consumption.
61 static char *quota_stat_name;
64 * Task to timeout stat GET.
66 static struct GNUNET_SCHEDULER_Task *stat_timeout_task;
69 * After how many payload-changing operations
70 * do we sync our statistics?
72 #define MAX_STAT_SYNC_LAG 50
76 * Our datastore plugin.
78 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;
104 * Linked list of active reservations.
106 struct ReservationList {
108 * This is a linked list.
110 struct ReservationList *next;
113 * Client that made the reservation.
115 struct GNUNET_SERVICE_Client *client;
118 * Number of bytes (still) reserved.
123 * Number of items (still) reserved.
128 * Reservation identifier.
135 * Our datastore plugin (NULL if not available).
137 static struct DatastorePlugin *plugin;
140 * Linked list of space reservations made by clients.
142 static struct ReservationList *reservations;
145 * Bloomfilter to quickly tell if we don't have the content.
147 static struct GNUNET_CONTAINER_BloomFilter *filter;
150 * Name of our plugin.
152 static char *plugin_name;
157 static const struct GNUNET_CONFIGURATION_Handle *cfg;
160 * Handle for reporting statistics.
162 static struct GNUNET_STATISTICS_Handle *stats;
165 * How much space are we using for the cache? (space available for
166 * insertions that will be instantly reclaimed by discarding less
167 * important content --- or possibly whatever we just inserted into
170 static unsigned long long cache_size;
173 * How much space have we currently reserved?
175 static unsigned long long reserved;
178 * How much data are we currently storing
181 static unsigned long long payload;
184 * Identity of the task that is used to delete
187 static struct GNUNET_SCHEDULER_Task *expired_kill_task;
190 * Minimum time that content should have to not be discarded instantly
191 * (time stamp of any content that we've been discarding recently to
192 * stay below the quota). FOREVER if we had to expire content with
195 static struct GNUNET_TIME_Absolute min_expiration;
198 * How much space are we allowed to use?
200 static unsigned long long quota;
203 * Should the database be dropped on exit?
208 * Should we refresh the BF when the DB is loaded?
210 static int refresh_bf;
213 * Number of updates that were made to the
214 * payload value since we last synchronized
215 * it with the statistics service.
217 static unsigned int last_sync;
220 * Did we get an answer from statistics?
222 static int stats_worked;
226 * Synchronize our utilization statistics with the
227 * statistics service.
232 GNUNET_STATISTICS_set(stats, quota_stat_name, payload, GNUNET_YES);
233 GNUNET_STATISTICS_set(stats,
234 "# utilization by current datastore",
242 * Have we already cleaned up the TCCs and are hence no longer
243 * willing (or able) to transmit anything to anyone?
245 static int cleaning_done;
248 * Handle for pending get request.
250 static struct GNUNET_STATISTICS_GetHandle *stat_get;
253 * Handle to our server.
255 static struct GNUNET_SERVICE_Handle *service;
258 * Task that is used to remove expired entries from
259 * the datastore. This task will schedule itself
260 * again automatically to always delete all expired
263 * @param cls not used
266 delete_expired(void *cls);
270 * Iterate over the expired items stored in the datastore.
271 * Delete all expired items; once we have processed all
272 * expired items, re-schedule the "delete_expired" task.
274 * @param cls not used
275 * @param key key for the content
276 * @param size number of bytes in data
277 * @param data content stored
278 * @param type type of the content
279 * @param priority priority of the content
280 * @param anonymity anonymity-level for the content
281 * @param replication replication-level for the content
282 * @param expiration expiration time for the content
283 * @param uid unique identifier for the datum;
284 * maybe 0 if no unique identifier is available
286 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
287 * (continue on call to "next", of course),
288 * #GNUNET_NO to delete the item and continue (if supported)
291 expired_processor(void *cls,
292 const struct GNUNET_HashCode *key,
295 enum GNUNET_BLOCK_Type type,
298 uint32_t replication,
299 struct GNUNET_TIME_Absolute expiration,
302 struct GNUNET_TIME_Absolute now;
307 GNUNET_SCHEDULER_add_delayed_with_priority(MAX_EXPIRE_DELAY,
308 GNUNET_SCHEDULER_PRIORITY_IDLE,
311 return GNUNET_SYSERR;
313 now = GNUNET_TIME_absolute_get();
314 if (expiration.abs_value_us > now.abs_value_us)
316 /* finished processing */
318 GNUNET_SCHEDULER_add_delayed_with_priority(MAX_EXPIRE_DELAY,
319 GNUNET_SCHEDULER_PRIORITY_IDLE,
322 return GNUNET_SYSERR;
324 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
325 "Deleting content `%s' of type %u that expired %s ago\n",
328 GNUNET_STRINGS_relative_time_to_string(
329 GNUNET_TIME_absolute_get_difference(expiration, now),
331 min_expiration = now;
332 GNUNET_STATISTICS_update(stats,
333 gettext_noop("# bytes expired"),
336 GNUNET_CONTAINER_bloomfilter_remove(filter, key);
338 GNUNET_SCHEDULER_add_delayed_with_priority(MIN_EXPIRE_DELAY,
339 GNUNET_SCHEDULER_PRIORITY_IDLE,
347 * Task that is used to remove expired entries from
348 * the datastore. This task will schedule itself
349 * again automatically to always delete all expired
352 * @param cls not used
355 delete_expired(void *cls)
357 expired_kill_task = NULL;
358 plugin->api->get_expiration(plugin->api->cls, &expired_processor, NULL);
363 * An iterator over a set of items stored in the datastore
364 * that deletes until we're happy with respect to our quota.
367 * @param key key for the content
368 * @param size number of bytes in data
369 * @param data content stored
370 * @param type type of the content
371 * @param priority priority of the content
372 * @param anonymity anonymity-level for the content
373 * @param replication replication-level for the content
374 * @param expiration expiration time for the content
375 * @param uid unique identifier for the datum;
376 * maybe 0 if no unique identifier is available
377 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
378 * (continue on call to "next", of course),
379 * #GNUNET_NO to delete the item and continue (if supported)
382 quota_processor(void *cls,
383 const struct GNUNET_HashCode *key,
386 enum GNUNET_BLOCK_Type type,
389 uint32_t replication,
390 struct GNUNET_TIME_Absolute expiration,
393 unsigned long long *need = cls;
396 return GNUNET_SYSERR;
398 GNUNET_ERROR_TYPE_DEBUG,
399 "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",
400 (unsigned long long)(size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
401 (unsigned int)priority,
404 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining(
408 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
411 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
413 min_expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
415 min_expiration = expiration;
416 GNUNET_STATISTICS_update(stats,
417 gettext_noop("# bytes purged (low-priority)"),
420 GNUNET_CONTAINER_bloomfilter_remove(filter, key);
426 * Manage available disk space by running tasks
427 * that will discard content if necessary. This
428 * function will be run whenever a request for
429 * "need" bytes of storage could only be satisfied
430 * by eating into the "cache" (and we want our cache
433 * @param need number of bytes of content that were
434 * placed into the "cache" (and hence the
435 * number of bytes that should be removed).
438 manage_space(unsigned long long need)
440 unsigned long long last;
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
443 "Asked to free up %llu bytes of cache space\n",
446 while ((need > 0) && (last != need))
449 plugin->api->get_expiration(plugin->api->cls, "a_processor, &need);
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",
474 msg != NULL ? msg : "(none)");
475 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
476 env = GNUNET_MQ_msg_extra(sm, slen, 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], msg, slen);
480 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env);
485 * Function that will transmit the given datastore entry
488 * @param cls closure, pointer to the client (of type `struct GNUNET_SERVICE_Client`).
489 * @param key key for the content
490 * @param size number of bytes in data
491 * @param data content stored
492 * @param type type of the content
493 * @param priority priority of the content
494 * @param anonymity anonymity-level for the content
495 * @param replication replication-level for the content
496 * @param expiration expiration time for the content
497 * @param uid unique identifier for the datum;
498 * maybe 0 if no unique identifier is available
499 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue,
500 * #GNUNET_NO to delete the item and continue (if supported)
503 transmit_item(void *cls,
504 const struct GNUNET_HashCode *key,
507 enum GNUNET_BLOCK_Type type,
510 uint32_t replication,
511 struct GNUNET_TIME_Absolute expiration,
514 struct GNUNET_SERVICE_Client *client = cls;
515 struct GNUNET_MQ_Envelope *env;
516 struct GNUNET_MessageHeader *end;
517 struct DataMessage *dm;
521 /* transmit 'DATA_END' */
522 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Transmitting DATA_END message\n");
523 env = GNUNET_MQ_msg(end, GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
524 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env);
527 GNUNET_assert(sizeof(struct DataMessage) + size < GNUNET_MAX_MESSAGE_SIZE);
528 env = GNUNET_MQ_msg_extra(dm, size, GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
530 dm->size = htonl(size);
531 dm->type = htonl(type);
532 dm->priority = htonl(priority);
533 dm->anonymity = htonl(anonymity);
534 dm->replication = htonl(replication);
535 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
536 dm->uid = GNUNET_htonll(uid);
538 GNUNET_memcpy(&dm[1], data, size);
540 GNUNET_ERROR_TYPE_DEBUG,
541 "Transmitting DATA message for `%s' of type %u with expiration %s (in: %s)\n",
544 GNUNET_STRINGS_absolute_time_to_string(expiration),
545 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining(
548 GNUNET_STATISTICS_update(stats,
549 gettext_noop("# results found"),
552 GNUNET_MQ_send(GNUNET_SERVICE_client_get_mq(client), env);
558 * Handle RESERVE-message.
560 * @param cls identification of the client
561 * @param message the actual message
564 handle_reserve(void *cls, const struct ReserveMessage *msg)
567 * Static counter to produce reservation identifiers.
569 static int reservation_gen;
570 struct GNUNET_SERVICE_Client *client = cls;
571 struct ReservationList *e;
572 unsigned long long used;
573 unsigned long long req;
577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Processing RESERVE request\n");
578 amount = GNUNET_ntohll(msg->amount);
579 entries = ntohl(msg->entries);
580 used = payload + reserved;
582 amount + ((unsigned long long)GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
583 if (used + req > quota)
587 quota; /* cheat a bit for error message (to avoid negative numbers) */
589 GNUNET_ERROR_TYPE_WARNING,
591 "Insufficient space (%llu bytes are available) to satisfy RESERVE request for %llu bytes\n"),
594 if (cache_size < req)
596 /* TODO: document this in the FAQ; essentially, if this
597 * message happens, the insertion request could be blocked
598 * by less-important content from migration because it is
599 * larger than 1/8th of the overall available space, and
600 * we only reserve 1/8th for "fresh" insertions */
602 GNUNET_ERROR_TYPE_WARNING,
604 "The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
607 transmit_status(client,
610 "Insufficient space to satisfy request and "
611 "requested amount is larger than cache size"));
615 transmit_status(client,
617 gettext_noop("Insufficient space to satisfy request"));
619 GNUNET_SERVICE_client_continue(client);
623 GNUNET_STATISTICS_set(stats,
624 gettext_noop("# reserved"),
627 e = GNUNET_new(struct ReservationList);
628 e->next = reservations;
632 e->entries = entries;
633 e->rid = ++reservation_gen;
634 if (reservation_gen < 0)
635 reservation_gen = 0; /* wrap around */
636 transmit_status(client, e->rid, NULL);
637 GNUNET_SERVICE_client_continue(client);
642 * Handle RELEASE_RESERVE-message.
644 * @param cls identification of the client
645 * @param message the actual message
648 handle_release_reserve(void *cls, const struct ReleaseReserveMessage *msg)
650 struct GNUNET_SERVICE_Client *client = cls;
651 struct ReservationList *pos;
652 struct ReservationList *prev;
653 struct ReservationList *next;
654 int rid = ntohl(msg->rid);
655 unsigned long long rem;
657 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Processing RELEASE_RESERVE request\n");
660 while (NULL != (pos = next))
671 ((unsigned long long)GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
672 GNUNET_assert(reserved >= rem);
674 GNUNET_STATISTICS_set(stats,
675 gettext_noop("# reserved"),
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
679 "Returning %llu remaining reserved bytes to storage pool\n",
682 transmit_status(client, GNUNET_OK, NULL);
683 GNUNET_SERVICE_client_continue(client);
689 transmit_status(client,
691 gettext_noop("Could not find matching reservation"));
692 GNUNET_SERVICE_client_continue(client);
697 * Check that the given message is a valid data message.
699 * @param dm message to check
700 * @return #GNUNET_SYSERR is not well-formed, otherwise #GNUNET_OK
703 check_data(const struct DataMessage *dm)
708 size = ntohs(dm->header.size);
709 dsize = ntohl(dm->size);
710 if (size != dsize + sizeof(struct DataMessage))
713 return GNUNET_SYSERR;
723 * @param key key for the item stored
724 * @param size size of the item stored
725 * @param status #GNUNET_OK if inserted, #GNUNET_NO if updated,
726 * or #GNUNET_SYSERROR if error
727 * @param msg error message on error
730 put_continuation(void *cls,
731 const struct GNUNET_HashCode *key,
736 struct GNUNET_SERVICE_Client *client = cls;
738 if (GNUNET_OK == status)
740 GNUNET_STATISTICS_update(stats,
741 gettext_noop("# bytes stored"),
744 GNUNET_CONTAINER_bloomfilter_add(filter, key);
745 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
746 "Successfully stored %u bytes under key `%s'\n",
750 transmit_status(client,
751 GNUNET_SYSERR == status ? GNUNET_SYSERR : GNUNET_OK,
753 if (quota - reserved - cache_size < payload)
755 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
756 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
757 (unsigned long long)size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
758 (unsigned long long)(quota - reserved - cache_size),
759 (unsigned long long)payload);
760 manage_space(size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
766 * Verify PUT-message.
768 * @param cls identification of the client
769 * @param message the actual message
770 * @return #GNUNET_OK if @a dm is well-formed
773 check_put(void *cls, const struct DataMessage *dm)
775 if (GNUNET_OK != check_data(dm))
778 return GNUNET_SYSERR;
785 * Handle PUT-message.
787 * @param cls identification of the client
788 * @param message the actual message
791 handle_put(void *cls, const struct DataMessage *dm)
793 struct GNUNET_SERVICE_Client *client = cls;
795 struct ReservationList *pos;
798 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
799 "Processing PUT request for `%s' of type %u\n",
800 GNUNET_h2s(&dm->key),
801 (uint32_t)ntohl(dm->type));
802 rid = ntohl(dm->rid);
803 size = ntohl(dm->size);
807 while ((NULL != pos) && (rid != pos->rid))
809 GNUNET_break(pos != NULL);
812 GNUNET_break(pos->entries > 0);
813 GNUNET_break(pos->amount >= size);
816 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
817 GNUNET_STATISTICS_set(stats,
818 gettext_noop("# reserved"),
824 GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test(filter, &dm->key);
825 plugin->api->put(plugin->api->cls,
832 ntohl(dm->anonymity),
833 ntohl(dm->replication),
834 GNUNET_TIME_absolute_ntoh(dm->expiration),
837 GNUNET_SERVICE_client_continue(client);
842 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET-message.
844 * @param cls identification of the client
845 * @param msg the actual message
848 handle_get(void *cls, const struct GetMessage *msg)
850 struct GNUNET_SERVICE_Client *client = cls;
852 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
853 "Processing GET request of type %u\n",
854 (uint32_t)ntohl(msg->type));
855 GNUNET_STATISTICS_update(stats,
856 gettext_noop("# GET requests received"),
859 plugin->api->get_key(plugin->api->cls,
860 GNUNET_ntohll(msg->next_uid),
866 GNUNET_SERVICE_client_continue(client);
871 * Handle #GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY-message.
874 * @param msg the actual message
877 handle_get_key(void *cls, const struct GetKeyMessage *msg)
879 struct GNUNET_SERVICE_Client *client = cls;
881 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
882 "Processing GET request for `%s' of type %u\n",
883 GNUNET_h2s(&msg->key),
884 (uint32_t)ntohl(msg->type));
885 GNUNET_STATISTICS_update(stats,
886 gettext_noop("# GET KEY requests received"),
889 if (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test(filter, &msg->key))
891 /* don't bother database... */
892 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
893 "Empty result set for GET request for `%s' (bloomfilter).\n",
894 GNUNET_h2s(&msg->key));
895 GNUNET_STATISTICS_update(stats,
897 "# requests filtered by bloomfilter"),
900 transmit_item(client,
908 GNUNET_TIME_UNIT_ZERO_ABS,
910 GNUNET_SERVICE_client_continue(client);
913 plugin->api->get_key(plugin->api->cls,
914 GNUNET_ntohll(msg->next_uid),
920 GNUNET_SERVICE_client_continue(client);
925 * Handle GET_REPLICATION-message.
927 * @param cls identification of the client
928 * @param message the actual message
931 handle_get_replication(void *cls, const struct GNUNET_MessageHeader *message)
933 struct GNUNET_SERVICE_Client *client = cls;
935 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Processing GET_REPLICATION request\n");
936 GNUNET_STATISTICS_update(stats,
938 "# GET REPLICATION requests received"),
941 plugin->api->get_replication(plugin->api->cls, &transmit_item, client);
942 GNUNET_SERVICE_client_continue(client);
947 * Handle GET_ZERO_ANONYMITY-message.
949 * @param cls client identification of the client
950 * @param message the actual message
953 handle_get_zero_anonymity(void *cls, const struct GetZeroAnonymityMessage *msg)
955 struct GNUNET_SERVICE_Client *client = cls;
956 enum GNUNET_BLOCK_Type type;
958 type = (enum GNUNET_BLOCK_Type)ntohl(msg->type);
959 if (type == GNUNET_BLOCK_TYPE_ANY)
962 GNUNET_SERVICE_client_drop(client);
965 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
966 "Processing GET_ZERO_ANONYMITY request\n");
967 GNUNET_STATISTICS_update(stats,
969 "# GET ZERO ANONYMITY requests received"),
972 plugin->api->get_zero_anonymity(plugin->api->cls,
973 GNUNET_ntohll(msg->next_uid),
977 GNUNET_SERVICE_client_continue(client);
982 * Remove continuation.
985 * @param key key for the content
986 * @param size number of bytes in data
987 * @param status #GNUNET_OK if removed, #GNUNET_NO if not found,
988 * or #GNUNET_SYSERROR if error
989 * @param msg error message on error
992 remove_continuation(void *cls,
993 const struct GNUNET_HashCode *key,
998 struct GNUNET_SERVICE_Client *client = cls;
1000 if (GNUNET_SYSERR == status)
1002 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "REMOVE request failed: %s.\n", msg);
1003 transmit_status(client, GNUNET_NO, msg);
1006 if (GNUNET_NO == status)
1008 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1009 "Content not found for REMOVE request.\n");
1010 transmit_status(client, GNUNET_NO, _("Content not found"));
1013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1014 "Item matches REMOVE request for key `%s'.\n",
1016 GNUNET_STATISTICS_update(stats,
1017 gettext_noop("# bytes removed (explicit request)"),
1020 GNUNET_CONTAINER_bloomfilter_remove(filter, key);
1021 transmit_status(client, GNUNET_OK, NULL);
1026 * Verify REMOVE-message.
1028 * @param cls identification of the client
1029 * @param message the actual message
1030 * @return #GNUNET_OK if @a dm is well-formed
1033 check_remove(void *cls, const struct DataMessage *dm)
1035 if (GNUNET_OK != check_data(dm))
1038 return GNUNET_SYSERR;
1045 * Handle REMOVE-message.
1047 * @param cls closure
1048 * @param client identification of the client
1049 * @param message the actual message
1052 handle_remove(void *cls, const struct DataMessage *dm)
1054 struct GNUNET_SERVICE_Client *client = cls;
1056 GNUNET_STATISTICS_update(stats,
1057 gettext_noop("# REMOVE requests received"),
1060 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1061 "Processing REMOVE request for `%s'\n",
1062 GNUNET_h2s(&dm->key));
1063 plugin->api->remove_key(plugin->api->cls,
1067 &remove_continuation,
1069 GNUNET_SERVICE_client_continue(client);
1074 * Handle DROP-message.
1076 * @param cls identification of the client
1077 * @param message the actual message
1080 handle_drop(void *cls, const struct GNUNET_MessageHeader *message)
1082 struct GNUNET_SERVICE_Client *client = cls;
1084 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Processing DROP request\n");
1085 do_drop = GNUNET_YES;
1086 GNUNET_SERVICE_client_continue(client);
1091 * Function called by plugins to notify us about a
1092 * change in their disk utilization.
1094 * @param cls closure (NULL)
1095 * @param delta change in disk utilization,
1096 * 0 for "reset to empty"
1099 disk_utilization_change_cb(void *cls, int delta)
1101 if ((delta < 0) && (payload < -delta))
1104 GNUNET_ERROR_TYPE_WARNING,
1106 "Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1109 plugin->api->estimate_size(plugin->api->cls, &payload);
1110 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1111 _("New payload: %lld\n"),
1112 (long long)payload);
1118 if (last_sync >= MAX_STAT_SYNC_LAG)
1124 * Callback function to process statistic values.
1126 * @param cls closure (struct Plugin*)
1127 * @param subsystem name of subsystem that created the statistic
1128 * @param name the name of the datum
1129 * @param value the current value
1130 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1131 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1134 process_stat_in(void *cls,
1135 const char *subsystem,
1140 GNUNET_assert(GNUNET_NO == stats_worked);
1141 stats_worked = GNUNET_YES;
1144 GNUNET_ERROR_TYPE_DEBUG,
1145 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1146 (unsigned long long)value,
1147 (unsigned long long)payload);
1153 * Load the datastore plugin.
1155 static struct DatastorePlugin *
1158 struct DatastorePlugin *ret;
1161 ret = GNUNET_new(struct DatastorePlugin);
1163 ret->env.duc = &disk_utilization_change_cb;
1164 ret->env.cls = NULL;
1165 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1166 _("Loading `%s' datastore plugin\n"),
1168 GNUNET_asprintf(&libname, "libgnunet_plugin_datastore_%s", plugin_name);
1169 ret->short_name = GNUNET_strdup(plugin_name);
1170 ret->lib_name = libname;
1171 ret->api = GNUNET_PLUGIN_load(libname, &ret->env);
1172 if (NULL == ret->api)
1174 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1175 _("Failed to load datastore plugin for `%s'\n"),
1177 GNUNET_free(ret->short_name);
1178 GNUNET_free(libname);
1187 * Function called when the service shuts
1188 * down. Unloads our datastore plugin.
1190 * @param plug plugin to unload
1193 unload_plugin(struct DatastorePlugin *plug)
1195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1196 "Datastore service is unloading plugin...\n");
1197 GNUNET_break(NULL == GNUNET_PLUGIN_unload(plug->lib_name, plug->api));
1198 GNUNET_free(plug->lib_name);
1199 GNUNET_free(plug->short_name);
1205 * Initialization complete, start operating the service.
1210 GNUNET_SERVICE_resume(service);
1212 GNUNET_SCHEDULER_add_with_priority(GNUNET_SCHEDULER_PRIORITY_IDLE,
1219 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1221 * @param cls the bloomfilter
1222 * @param key key to add
1223 * @param count number of times to add key
1226 add_key_to_bloomfilter(void *cls,
1227 const struct GNUNET_HashCode *key,
1230 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1234 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1235 _("Bloomfilter construction complete.\n"));
1241 GNUNET_CONTAINER_bloomfilter_add(bf, key);
1246 * We finished receiving the statistic. Initialize the plugin; if
1247 * loading the statistic failed, run the estimator.
1250 * @param success #GNUNET_NO if we failed to read the stat
1253 process_stat_done(void *cls, int success)
1256 if (NULL != stat_timeout_task)
1258 GNUNET_SCHEDULER_cancel(stat_timeout_task);
1259 stat_timeout_task = NULL;
1261 plugin = load_plugin();
1264 GNUNET_CONTAINER_bloomfilter_free(filter);
1268 GNUNET_STATISTICS_destroy(stats, GNUNET_YES);
1274 if (GNUNET_NO == stats_worked)
1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1277 "Failed to obtain value from statistics service, recomputing it\n");
1278 plugin->api->estimate_size(plugin->api->cls, &payload);
1279 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1280 _("New payload: %lld\n"),
1281 (long long)payload);
1284 if (GNUNET_YES == refresh_bf)
1286 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1287 _("Rebuilding bloomfilter. Please be patient.\n"));
1288 if (NULL != plugin->api->get_keys)
1290 plugin->api->get_keys(plugin->api->cls, &add_key_to_bloomfilter, filter);
1295 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1297 "Plugin does not support get_keys function. Please fix!\n"));
1305 * Fetching stats took to long, run without.
1310 stat_timeout(void *cls)
1312 stat_timeout_task = NULL;
1313 GNUNET_STATISTICS_get_cancel(stat_get);
1314 process_stat_done(NULL, GNUNET_NO);
1319 * Task run during shutdown.
1322 cleaning_task(void *cls)
1324 cleaning_done = GNUNET_YES;
1325 if (NULL != expired_kill_task)
1327 GNUNET_SCHEDULER_cancel(expired_kill_task);
1328 expired_kill_task = NULL;
1330 if (GNUNET_YES == do_drop)
1332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Dropping database!\n");
1333 plugin->api->drop(plugin->api->cls);
1339 unload_plugin(plugin);
1344 GNUNET_CONTAINER_bloomfilter_free(filter);
1347 if (NULL != stat_get)
1349 GNUNET_STATISTICS_get_cancel(stat_get);
1352 if (NULL != stat_timeout_task)
1354 GNUNET_SCHEDULER_cancel(stat_timeout_task);
1355 stat_timeout_task = NULL;
1357 GNUNET_free_non_null(plugin_name);
1363 GNUNET_STATISTICS_destroy(stats, GNUNET_YES);
1366 GNUNET_free(quota_stat_name);
1367 quota_stat_name = NULL;
1372 * Add a client to our list of active clients.
1375 * @param client client to add
1376 * @param mq message queue for @a client
1380 client_connect_cb(void *cls,
1381 struct GNUNET_SERVICE_Client *client,
1382 struct GNUNET_MQ_Handle *mq)
1389 * Called whenever a client is disconnected.
1390 * Frees our resources associated with that client.
1392 * @param cls closure
1393 * @param client identification of the client
1394 * @param app_ctx must match @a client
1397 client_disconnect_cb(void *cls,
1398 struct GNUNET_SERVICE_Client *client,
1401 struct ReservationList *pos;
1402 struct ReservationList *prev;
1403 struct ReservationList *next;
1405 GNUNET_assert(app_ctx == client);
1411 if (pos->client == client)
1414 reservations = next;
1417 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1426 GNUNET_STATISTICS_set(stats,
1427 gettext_noop("# reserved"),
1434 * Process datastore requests.
1436 * @param cls closure
1437 * @param serv the initialized service
1438 * @param c configuration to use
1442 const struct GNUNET_CONFIGURATION_Handle *c,
1443 struct GNUNET_SERVICE_Handle *serv)
1447 unsigned int bf_size;
1451 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
1456 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
1461 GNUNET_asprintf("a_stat_name,
1462 _("# bytes used in file-sharing datastore `%s'"),
1465 GNUNET_CONFIGURATION_get_value_size(cfg, "DATASTORE", "QUOTA", "a))
1467 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, "QUOTA", "DATASTORE");
1470 stats = GNUNET_STATISTICS_create("datastore", cfg);
1471 GNUNET_STATISTICS_set(stats, gettext_noop("# quota"), quota, GNUNET_NO);
1472 cache_size = quota / 8; /* Or should we make this an option? */
1473 GNUNET_STATISTICS_set(stats,
1474 gettext_noop("# cache size"),
1477 if (quota / (32 * 1024LL) > MAX_BF_SIZE)
1478 bf_size = MAX_BF_SIZE;
1481 quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1483 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
1487 (GNUNET_OK != GNUNET_DISK_directory_create_for_file(fn)))
1489 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1490 _("Could not use specified filename `%s' for bloomfilter.\n"),
1491 NULL != fn ? fn : "");
1492 GNUNET_free_non_null(fn);
1497 GNUNET_asprintf(&pfn, "%s.%s", fn, plugin_name);
1498 if (GNUNET_YES == GNUNET_DISK_file_test(pfn))
1501 GNUNET_CONTAINER_bloomfilter_load(pfn,
1503 5); /* approx. 3% false positives at max use */
1506 /* file exists but not valid, remove and try again, but refresh */
1507 if (0 != unlink(pfn))
1509 /* failed to remove, run without file */
1510 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1511 _("Failed to remove bogus bloomfilter file `%s'\n"),
1515 filter = GNUNET_CONTAINER_bloomfilter_load(
1518 5); /* approx. 3% false positives at max use */
1519 refresh_bf = GNUNET_YES;
1523 /* try again after remove */
1524 filter = GNUNET_CONTAINER_bloomfilter_load(
1527 5); /* approx. 3% false positives at max use */
1528 refresh_bf = GNUNET_YES;
1531 /* failed yet again, give up on using file */
1532 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1533 _("Failed to remove bogus bloomfilter file `%s'\n"),
1537 filter = GNUNET_CONTAINER_bloomfilter_init(
1540 5); /* approx. 3% false positives at max use */
1546 /* normal case: have an existing valid bf file, no need to refresh */
1547 refresh_bf = GNUNET_NO;
1553 GNUNET_CONTAINER_bloomfilter_load(pfn,
1555 5); /* approx. 3% false positives at max use */
1556 refresh_bf = GNUNET_YES;
1563 GNUNET_CONTAINER_bloomfilter_init(NULL,
1565 5); /* approx. 3% false positives at max use */
1566 refresh_bf = GNUNET_YES;
1568 GNUNET_free_non_null(fn);
1571 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1572 _("Failed to initialize bloomfilter.\n"));
1575 GNUNET_STATISTICS_destroy(stats, GNUNET_YES);
1580 GNUNET_SERVICE_suspend(service);
1581 stat_get = GNUNET_STATISTICS_get(stats,
1587 if (NULL == stat_get)
1588 process_stat_done(NULL, GNUNET_SYSERR);
1590 stat_timeout_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
1593 GNUNET_SCHEDULER_add_shutdown(&cleaning_task, NULL);
1598 * Define "main" method using service macro.
1600 GNUNET_SERVICE_MAIN(
1602 GNUNET_SERVICE_OPTION_NONE,
1605 &client_disconnect_cb,
1607 GNUNET_MQ_hd_fixed_size(reserve,
1608 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1609 struct ReserveMessage,
1611 GNUNET_MQ_hd_fixed_size(release_reserve,
1612 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1613 struct ReleaseReserveMessage,
1615 GNUNET_MQ_hd_var_size(put,
1616 GNUNET_MESSAGE_TYPE_DATASTORE_PUT,
1619 GNUNET_MQ_hd_fixed_size(get,
1620 GNUNET_MESSAGE_TYPE_DATASTORE_GET,
1623 GNUNET_MQ_hd_fixed_size(get_key,
1624 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY,
1625 struct GetKeyMessage,
1627 GNUNET_MQ_hd_fixed_size(get_replication,
1628 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1629 struct GNUNET_MessageHeader,
1631 GNUNET_MQ_hd_fixed_size(get_zero_anonymity,
1632 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1633 struct GetZeroAnonymityMessage,
1635 GNUNET_MQ_hd_var_size(remove,
1636 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE,
1639 GNUNET_MQ_hd_fixed_size(drop,
1640 GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1641 struct GNUNET_MessageHeader,
1643 GNUNET_MQ_handler_end());
1646 /* end of gnunet-service-datastore.c */