2 This file is part of GNUnet
3 Copyright (C) 2004-2014 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 * How long are we at most keeping "expired" content
41 * past the expiration date in the database?
43 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
46 * How fast are we allowed to query the database for deleting
47 * expired content? (1 item per second).
49 #define MIN_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
52 * Name under which we store current space consumption.
54 static char *quota_stat_name;
57 * After how many payload-changing operations
58 * do we sync our statistics?
60 #define MAX_STAT_SYNC_LAG 50
64 * Our datastore plugin.
66 struct DatastorePlugin
70 * API of the transport as returned by the plugin's
71 * initialization function.
73 struct GNUNET_DATASTORE_PluginFunctions *api;
76 * Short name for the plugin (i.e. "sqlite").
81 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
86 * Environment this transport service is using
89 struct GNUNET_DATASTORE_PluginEnvironment env;
95 * Linked list of active reservations.
97 struct ReservationList
101 * This is a linked list.
103 struct ReservationList *next;
106 * Client that made the reservation.
108 struct GNUNET_SERVER_Client *client;
111 * Number of bytes (still) reserved.
116 * Number of items (still) reserved.
121 * Reservation identifier.
130 * Our datastore plugin (NULL if not available).
132 static struct DatastorePlugin *plugin;
135 * Linked list of space reservations made by clients.
137 static struct ReservationList *reservations;
140 * Bloomfilter to quickly tell if we don't have the content.
142 static struct GNUNET_CONTAINER_BloomFilter *filter;
145 * Name of our plugin.
147 static char *plugin_name;
152 static const struct GNUNET_CONFIGURATION_Handle *cfg;
155 * Handle for reporting statistics.
157 static struct GNUNET_STATISTICS_Handle *stats;
160 * How much space are we using for the cache? (space available for
161 * insertions that will be instantly reclaimed by discarding less
162 * important content --- or possibly whatever we just inserted into
165 static unsigned long long cache_size;
168 * How much space have we currently reserved?
170 static unsigned long long reserved;
173 * How much data are we currently storing
176 static unsigned long long payload;
179 * Identity of the task that is used to delete
182 static struct GNUNET_SCHEDULER_Task * expired_kill_task;
185 * Minimum time that content should have to not be discarded instantly
186 * (time stamp of any content that we've been discarding recently to
187 * stay below the quota). FOREVER if we had to expire content with
190 static struct GNUNET_TIME_Absolute min_expiration;
193 * How much space are we allowed to use?
195 static unsigned long long quota;
198 * Should the database be dropped on exit?
203 * Should we refresh the BF when the DB is loaded?
205 static int refresh_bf;
208 * Number of updates that were made to the
209 * payload value since we last synchronized
210 * it with the statistics service.
212 static unsigned int last_sync;
215 * Did we get an answer from statistics?
217 static int stats_worked;
221 * Synchronize our utilization statistics with the
222 * statistics service.
227 GNUNET_STATISTICS_set (stats,
231 GNUNET_STATISTICS_set (stats,
232 "# utilization by current datastore",
240 * Context for transmitting replies to clients.
242 struct TransmitCallbackContext
246 * We keep these in a doubly-linked list (for cleanup).
248 struct TransmitCallbackContext *next;
251 * We keep these in a doubly-linked list (for cleanup).
253 struct TransmitCallbackContext *prev;
256 * The message that we're asked to transmit.
258 struct GNUNET_MessageHeader *msg;
261 * Handle for the transmission request.
263 struct GNUNET_SERVER_TransmitHandle *th;
266 * Client that we are transmitting to.
268 struct GNUNET_SERVER_Client *client;
274 * Head of the doubly-linked list (for cleanup).
276 static struct TransmitCallbackContext *tcc_head;
279 * Tail of the doubly-linked list (for cleanup).
281 static struct TransmitCallbackContext *tcc_tail;
284 * Have we already cleaned up the TCCs and are hence no longer
285 * willing (or able) to transmit anything to anyone?
287 static int cleaning_done;
290 * Handle for pending get request.
292 static struct GNUNET_STATISTICS_GetHandle *stat_get;
295 * Handle to our server.
297 static struct GNUNET_SERVER_Handle *server;
300 * Task that is used to remove expired entries from
301 * the datastore. This task will schedule itself
302 * again automatically to always delete all expired
305 * @param cls not used
308 delete_expired (void *cls);
312 * Iterate over the expired items stored in the datastore.
313 * Delete all expired items; once we have processed all
314 * expired items, re-schedule the "delete_expired" task.
316 * @param cls not used
317 * @param key key for the content
318 * @param size number of bytes in data
319 * @param data content stored
320 * @param type type of the content
321 * @param priority priority of the content
322 * @param anonymity anonymity-level for the content
323 * @param expiration expiration time for the content
324 * @param uid unique identifier for the datum;
325 * maybe 0 if no unique identifier is available
327 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
328 * (continue on call to "next", of course),
329 * #GNUNET_NO to delete the item and continue (if supported)
332 expired_processor (void *cls,
333 const struct GNUNET_HashCode *key,
336 enum GNUNET_BLOCK_Type type,
339 struct GNUNET_TIME_Absolute expiration,
342 struct GNUNET_TIME_Absolute now;
347 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
348 GNUNET_SCHEDULER_PRIORITY_IDLE,
349 &delete_expired, NULL);
350 return GNUNET_SYSERR;
352 now = GNUNET_TIME_absolute_get ();
353 if (expiration.abs_value_us > now.abs_value_us)
355 /* finished processing */
357 GNUNET_SCHEDULER_add_delayed_with_priority (MAX_EXPIRE_DELAY,
358 GNUNET_SCHEDULER_PRIORITY_IDLE,
359 &delete_expired, NULL);
360 return GNUNET_SYSERR;
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Deleting content `%s' of type %u that expired %s ago\n",
364 GNUNET_h2s (key), type,
365 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (expiration,
368 min_expiration = now;
369 GNUNET_STATISTICS_update (stats,
370 gettext_noop ("# bytes expired"),
373 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
375 GNUNET_SCHEDULER_add_delayed_with_priority (MIN_EXPIRE_DELAY,
376 GNUNET_SCHEDULER_PRIORITY_IDLE,
377 &delete_expired, NULL);
383 * Task that is used to remove expired entries from
384 * the datastore. This task will schedule itself
385 * again automatically to always delete all expired
388 * @param cls not used
391 delete_expired (void *cls)
393 expired_kill_task = NULL;
394 plugin->api->get_expiration (plugin->api->cls,
401 * An iterator over a set of items stored in the datastore
402 * that deletes until we're happy with respect to our quota.
405 * @param key key for the content
406 * @param size number of bytes in data
407 * @param data content stored
408 * @param type type of the content
409 * @param priority priority of the content
410 * @param anonymity anonymity-level for the content
411 * @param expiration expiration time for the content
412 * @param uid unique identifier for the datum;
413 * maybe 0 if no unique identifier is available
415 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
416 * (continue on call to "next", of course),
417 * GNUNET_NO to delete the item and continue (if supported)
420 quota_processor (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
421 const void *data, enum GNUNET_BLOCK_Type type,
422 uint32_t priority, uint32_t anonymity,
423 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
425 unsigned long long *need = cls;
428 return GNUNET_SYSERR;
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "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",
431 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
432 (unsigned int) priority,
433 GNUNET_h2s (key), type,
434 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
437 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
440 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
442 min_expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
444 min_expiration = expiration;
445 GNUNET_STATISTICS_update (stats,
446 gettext_noop ("# bytes purged (low-priority)"),
448 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
454 * Manage available disk space by running tasks
455 * that will discard content if necessary. This
456 * function will be run whenever a request for
457 * "need" bytes of storage could only be satisfied
458 * by eating into the "cache" (and we want our cache
461 * @param need number of bytes of content that were
462 * placed into the "cache" (and hence the
463 * number of bytes that should be removed).
466 manage_space (unsigned long long need)
468 unsigned long long last;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Asked to free up %llu bytes of cache space\n", need);
473 while ((need > 0) && (last != need))
476 plugin->api->get_expiration (plugin->api->cls, "a_processor, &need);
482 * Function called to notify a client about the socket
483 * begin ready to queue more data. "buf" will be
484 * NULL and "size" zero if the socket was closed for
485 * writing in the meantime.
488 * @param size number of bytes available in buf
489 * @param buf where the callee should write the message
490 * @return number of bytes written to buf
493 transmit_callback (void *cls, size_t size, void *buf)
495 struct TransmitCallbackContext *tcc = cls;
499 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
500 msize = ntohs (tcc->msg->size);
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504 _("Transmission to client failed!\n"));
505 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
506 GNUNET_SERVER_client_drop (tcc->client);
507 GNUNET_free (tcc->msg);
511 GNUNET_assert (size >= msize);
512 memcpy (buf, tcc->msg, msize);
513 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
514 GNUNET_SERVER_client_drop (tcc->client);
515 GNUNET_free (tcc->msg);
522 * Transmit the given message to the client.
524 * @param client target of the message
525 * @param msg message to transmit, will be freed!
528 transmit (struct GNUNET_SERVER_Client *client, struct GNUNET_MessageHeader *msg)
530 struct TransmitCallbackContext *tcc;
532 if (GNUNET_YES == cleaning_done)
534 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
535 _("Shutdown in progress, aborting transmission.\n"));
536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
540 tcc = GNUNET_new (struct TransmitCallbackContext);
542 tcc->client = client;
545 GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
546 GNUNET_TIME_UNIT_FOREVER_REL,
547 &transmit_callback, tcc)))
550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
555 GNUNET_SERVER_client_keep (client);
556 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
561 * Transmit a status code to the client.
563 * @param client receiver of the response
564 * @param code status code
565 * @param msg optional error message (can be NULL)
568 transmit_status (struct GNUNET_SERVER_Client *client, int code, const char *msg)
570 struct StatusMessage *sm;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Transmitting `%s' message with value %d and message `%s'\n",
575 "STATUS", code, msg != NULL ? msg : "(none)");
576 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
577 sm = GNUNET_malloc (sizeof (struct StatusMessage) + slen);
578 sm->header.size = htons (sizeof (struct StatusMessage) + slen);
579 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
580 sm->status = htonl (code);
581 sm->min_expiration = GNUNET_TIME_absolute_hton (min_expiration);
583 memcpy (&sm[1], msg, slen);
584 transmit (client, &sm->header);
589 * Function that will transmit the given datastore entry
592 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
593 * @param key key for the content
594 * @param size number of bytes in data
595 * @param data content stored
596 * @param type type of the content
597 * @param priority priority of the content
598 * @param anonymity anonymity-level for the content
599 * @param expiration expiration time for the content
600 * @param uid unique identifier for the datum;
601 * maybe 0 if no unique identifier is available
603 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
604 * GNUNET_NO to delete the item and continue (if supported)
607 transmit_item (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
608 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
609 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
612 struct GNUNET_SERVER_Client *client = cls;
613 struct GNUNET_MessageHeader *end;
614 struct DataMessage *dm;
618 /* transmit 'DATA_END' */
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' message\n",
621 end = GNUNET_new (struct GNUNET_MessageHeader);
622 end->size = htons (sizeof (struct GNUNET_MessageHeader));
623 end->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
624 transmit (client, end);
625 GNUNET_SERVER_client_drop (client);
628 GNUNET_assert (sizeof (struct DataMessage) + size <
629 GNUNET_SERVER_MAX_MESSAGE_SIZE);
630 dm = GNUNET_malloc (sizeof (struct DataMessage) + size);
631 dm->header.size = htons (sizeof (struct DataMessage) + size);
632 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
634 dm->size = htonl (size);
635 dm->type = htonl (type);
636 dm->priority = htonl (priority);
637 dm->anonymity = htonl (anonymity);
638 dm->replication = htonl (0);
639 dm->reserved = htonl (0);
640 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
641 dm->uid = GNUNET_htonll (uid);
643 memcpy (&dm[1], data, size);
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Transmitting `%s' message for `%s' of type %u with expiration %s (in: %s)\n",
646 "DATA", GNUNET_h2s (key), type,
647 GNUNET_STRINGS_absolute_time_to_string (expiration),
648 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
650 GNUNET_STATISTICS_update (stats,
651 gettext_noop ("# results found"),
654 transmit (client, &dm->header);
655 GNUNET_SERVER_client_drop (client);
661 * Handle RESERVE-message.
664 * @param client identification of the client
665 * @param message the actual message
668 handle_reserve (void *cls, struct GNUNET_SERVER_Client *client,
669 const struct GNUNET_MessageHeader *message)
672 * Static counter to produce reservation identifiers.
674 static int reservation_gen;
676 const struct ReserveMessage *msg = (const struct ReserveMessage *) message;
677 struct ReservationList *e;
678 unsigned long long used;
679 unsigned long long req;
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Processing RESERVE request\n");
685 amount = GNUNET_ntohll (msg->amount);
686 entries = ntohl (msg->entries);
687 used = payload + reserved;
689 amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
690 if (used + req > quota)
693 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
696 ("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
697 quota - used, "RESERVE", req);
698 if (cache_size < req)
700 /* TODO: document this in the FAQ; essentially, if this
701 * message happens, the insertion request could be blocked
702 * by less-important content from migration because it is
703 * larger than 1/8th of the overall available space, and
704 * we only reserve 1/8th for "fresh" insertions */
705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
707 ("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
709 transmit_status (client, 0,
711 ("Insufficient space to satisfy request and "
712 "requested amount is larger than cache size"));
716 transmit_status (client, 0,
717 gettext_noop ("Insufficient space to satisfy request"));
722 GNUNET_STATISTICS_set (stats,
723 gettext_noop ("# reserved"),
726 e = GNUNET_new (struct ReservationList);
727 e->next = reservations;
731 e->entries = entries;
732 e->rid = ++reservation_gen;
733 if (reservation_gen < 0)
734 reservation_gen = 0; /* wrap around */
735 transmit_status (client, e->rid, NULL);
740 * Handle RELEASE_RESERVE-message.
743 * @param client identification of the client
744 * @param message the actual message
747 handle_release_reserve (void *cls,
748 struct GNUNET_SERVER_Client *client,
749 const struct GNUNET_MessageHeader *message)
751 const struct ReleaseReserveMessage *msg =
752 (const struct ReleaseReserveMessage *) message;
753 struct ReservationList *pos;
754 struct ReservationList *prev;
755 struct ReservationList *next;
756 int rid = ntohl (msg->rid);
757 unsigned long long rem;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Processing RELEASE_RESERVE request\n");
763 while (NULL != (pos = next))
774 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
775 GNUNET_assert (reserved >= rem);
777 GNUNET_STATISTICS_set (stats,
778 gettext_noop ("# reserved"),
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Returning %llu remaining reserved bytes to storage pool\n",
785 transmit_status (client, GNUNET_OK, NULL);
791 transmit_status (client, GNUNET_SYSERR,
792 gettext_noop ("Could not find matching reservation"));
797 * Check that the given message is a valid data message.
799 * @return NULL if the message is not well-formed, otherwise the message
801 static const struct DataMessage *
802 check_data (const struct GNUNET_MessageHeader *message)
806 const struct DataMessage *dm;
808 size = ntohs (message->size);
809 if (size < sizeof (struct DataMessage))
814 dm = (const struct DataMessage *) message;
815 dsize = ntohl (dm->size);
816 if (size != dsize + sizeof (struct DataMessage))
826 * Context for a PUT request used to see if the content is
832 * Client to notify on completion.
834 struct GNUNET_SERVER_Client *client;
836 #if ! HAVE_UNALIGNED_64_ACCESS
840 /* followed by the 'struct DataMessage' */
848 * @param key key for the item stored
849 * @param size size of the item stored
850 * @param status #GNUNET_OK or #GNUNET_SYSERROR
851 * @param msg error message on error
854 put_continuation (void *cls,
855 const struct GNUNET_HashCode *key,
860 struct PutContext *pc = cls;
862 if (GNUNET_OK == status)
864 GNUNET_STATISTICS_update (stats,
865 gettext_noop ("# bytes stored"),
868 GNUNET_CONTAINER_bloomfilter_add (filter, key);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Successfully stored %u bytes under key `%s'\n",
871 size, GNUNET_h2s (key));
873 transmit_status (pc->client, status, msg);
874 GNUNET_SERVER_client_drop (pc->client);
876 if (quota - reserved - cache_size < payload)
878 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
879 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
880 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
881 (unsigned long long) (quota - reserved - cache_size),
882 (unsigned long long) payload);
883 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
889 * Actually put the data message.
891 * @param pc put context
894 execute_put (struct PutContext *pc)
896 const struct DataMessage *dm;
898 dm = (const struct DataMessage *) &pc[1];
899 plugin->api->put (plugin->api->cls, &dm->key, ntohl (dm->size), &dm[1],
900 ntohl (dm->type), ntohl (dm->priority),
901 ntohl (dm->anonymity), ntohl (dm->replication),
902 GNUNET_TIME_absolute_ntoh (dm->expiration),
903 &put_continuation, pc);
910 * @param status #GNUNET_OK or #GNUNET_SYSERR
911 * @param msg error message on error
914 check_present_continuation (void *cls,
918 struct GNUNET_SERVER_Client *client = cls;
920 transmit_status (client, GNUNET_NO, NULL);
921 GNUNET_SERVER_client_drop (client);
926 * Function that will check if the given datastore entry
927 * matches the put and if none match executes the put.
929 * @param cls closure, pointer to the client (of type `struct PutContext`).
930 * @param key key for the content
931 * @param size number of bytes in data
932 * @param data content stored
933 * @param type type of the content
934 * @param priority priority of the content
935 * @param anonymity anonymity-level for the content
936 * @param expiration expiration time for the content
937 * @param uid unique identifier for the datum;
938 * maybe 0 if no unique identifier is available
939 * @return #GNUNET_OK usually
940 * #GNUNET_NO to delete the item
943 check_present (void *cls,
944 const struct GNUNET_HashCode *key,
947 enum GNUNET_BLOCK_Type type,
950 struct GNUNET_TIME_Absolute expiration,
953 struct PutContext *pc = cls;
954 const struct DataMessage *dm;
956 dm = (const struct DataMessage *) &pc[1];
962 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
963 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
964 ( (size == ntohl (dm->size)) &&
965 (0 == memcmp (&dm[1], data, size)) ) )
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Result already present in datastore\n");
969 /* FIXME: change API to allow increasing 'replication' counter */
970 if ((ntohl (dm->priority) > 0) ||
971 (GNUNET_TIME_absolute_ntoh (dm->expiration).abs_value_us >
972 expiration.abs_value_us))
973 plugin->api->update (plugin->api->cls,
975 (int32_t) ntohl (dm->priority),
976 GNUNET_TIME_absolute_ntoh (dm->expiration),
977 &check_present_continuation,
981 transmit_status (pc->client, GNUNET_NO, NULL);
982 GNUNET_SERVER_client_drop (pc->client);
995 * Handle PUT-message.
998 * @param client identification of the client
999 * @param message the actual message
1002 handle_put (void *cls,
1003 struct GNUNET_SERVER_Client *client,
1004 const struct GNUNET_MessageHeader *message)
1006 const struct DataMessage *dm = check_data (message);
1008 struct ReservationList *pos;
1009 struct PutContext *pc;
1010 struct GNUNET_HashCode vhash;
1013 if ((dm == NULL) || (ntohl (dm->type) == 0))
1016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "Processing PUT request for `%s' of type %u\n",
1021 GNUNET_h2s (&dm->key),
1023 rid = ntohl (dm->rid);
1024 size = ntohl (dm->size);
1028 while ((NULL != pos) && (rid != pos->rid))
1030 GNUNET_break (pos != NULL);
1033 GNUNET_break (pos->entries > 0);
1034 GNUNET_break (pos->amount >= size);
1036 pos->amount -= size;
1037 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1038 GNUNET_STATISTICS_set (stats,
1039 gettext_noop ("# reserved"),
1044 pc = GNUNET_malloc (sizeof (struct PutContext) + size +
1045 sizeof (struct DataMessage));
1046 pc->client = client;
1047 GNUNET_SERVER_client_keep (client);
1048 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1049 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter, &dm->key))
1051 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
1052 plugin->api->get_key (plugin->api->cls,
1066 * Handle GET-message.
1068 * @param cls closure
1069 * @param client identification of the client
1070 * @param message the actual message
1073 handle_get (void *cls,
1074 struct GNUNET_SERVER_Client *client,
1075 const struct GNUNET_MessageHeader *message)
1077 const struct GetMessage *msg;
1080 size = ntohs (message->size);
1081 if ((size != sizeof (struct GetMessage)) &&
1082 (size != sizeof (struct GetMessage) - sizeof (struct GNUNET_HashCode)))
1085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1088 msg = (const struct GetMessage *) message;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Processing GET request for `%s' of type %u\n",
1091 GNUNET_h2s (&msg->key),
1093 GNUNET_STATISTICS_update (stats,
1094 gettext_noop ("# GET requests received"),
1097 GNUNET_SERVER_client_keep (client);
1098 if ( (size == sizeof (struct GetMessage)) &&
1099 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter, &msg->key)) )
1101 /* don't bother database... */
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Empty result set for GET request for `%s' (bloomfilter).\n",
1104 GNUNET_h2s (&msg->key));
1105 GNUNET_STATISTICS_update (stats,
1107 ("# requests filtered by bloomfilter"),
1110 transmit_item (client, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1114 plugin->api->get_key (plugin->api->cls, GNUNET_ntohll (msg->offset),
1116 sizeof (struct GetMessage)) ? &msg->key : NULL), NULL,
1117 ntohl (msg->type), &transmit_item, client);
1122 * Function called with the result of an update operation.
1124 * @param cls closure
1125 * @param status #GNUNET_OK or #GNUNET_SYSERR
1126 * @param msg error message on error
1129 update_continuation (void *cls,
1133 struct GNUNET_SERVER_Client *client = cls;
1135 transmit_status (client, status, msg);
1136 GNUNET_SERVER_client_drop (client);
1141 * Handle UPDATE-message.
1143 * @param cls closure
1144 * @param client identification of the client
1145 * @param message the actual message
1148 handle_update (void *cls,
1149 struct GNUNET_SERVER_Client *client,
1150 const struct GNUNET_MessageHeader *message)
1152 const struct UpdateMessage *msg;
1154 GNUNET_STATISTICS_update (stats,
1155 gettext_noop ("# UPDATE requests received"),
1158 msg = (const struct UpdateMessage *) message;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Processing UPDATE request for %llu\n",
1161 (unsigned long long) GNUNET_ntohll (msg->uid));
1162 GNUNET_SERVER_client_keep (client);
1163 plugin->api->update (plugin->api->cls,
1164 GNUNET_ntohll (msg->uid),
1165 (int32_t) ntohl (msg->priority),
1166 GNUNET_TIME_absolute_ntoh (msg->expiration),
1167 &update_continuation, client);
1172 * Handle GET_REPLICATION-message.
1174 * @param cls closure
1175 * @param client identification of the client
1176 * @param message the actual message
1179 handle_get_replication (void *cls,
1180 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *message)
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Processing `%s' request\n",
1186 GNUNET_STATISTICS_update (stats,
1187 gettext_noop ("# GET REPLICATION requests received"),
1190 GNUNET_SERVER_client_keep (client);
1191 plugin->api->get_replication (plugin->api->cls,
1192 &transmit_item, client);
1197 * Handle GET_ZERO_ANONYMITY-message.
1199 * @param cls closure
1200 * @param client identification of the client
1201 * @param message the actual message
1204 handle_get_zero_anonymity (void *cls,
1205 struct GNUNET_SERVER_Client *client,
1206 const struct GNUNET_MessageHeader *message)
1208 const struct GetZeroAnonymityMessage *msg =
1209 (const struct GetZeroAnonymityMessage *) message;
1210 enum GNUNET_BLOCK_Type type;
1212 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1213 if (type == GNUNET_BLOCK_TYPE_ANY)
1216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1220 "Processing `%s' request\n",
1221 "GET_ZERO_ANONYMITY");
1222 GNUNET_STATISTICS_update (stats,
1223 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1226 GNUNET_SERVER_client_keep (client);
1227 plugin->api->get_zero_anonymity (plugin->api->cls,
1228 GNUNET_ntohll (msg->offset),
1230 &transmit_item, client);
1235 * Callback function that will cause the item that is passed
1236 * in to be deleted (by returning #GNUNET_NO).
1238 * @param cls closure
1239 * @param key key for the content
1240 * @param size number of bytes in data
1241 * @param data content stored
1242 * @param type type of the content
1243 * @param priority priority of the content
1244 * @param anonymity anonymity-level for the content
1245 * @param expiration expiration time for the content
1246 * @param uid unique identifier for the datum
1247 * @return #GNUNET_OK to keep the item
1248 * #GNUNET_NO to delete the item
1251 remove_callback (void *cls,
1252 const struct GNUNET_HashCode *key,
1255 enum GNUNET_BLOCK_Type type,
1258 struct GNUNET_TIME_Absolute expiration,
1261 struct GNUNET_SERVER_Client *client = cls;
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "No further matches for REMOVE request.\n");
1267 transmit_status (client,
1269 _("Content not found"));
1270 GNUNET_SERVER_client_drop (client);
1271 return GNUNET_OK; /* last item */
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "Item %llu matches REMOVE request for key `%s' and type %u.\n",
1275 (unsigned long long) uid,
1278 GNUNET_STATISTICS_update (stats,
1279 gettext_noop ("# bytes removed (explicit request)"),
1282 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
1283 transmit_status (client, GNUNET_OK, NULL);
1284 GNUNET_SERVER_client_drop (client);
1290 * Handle REMOVE-message.
1292 * @param cls closure
1293 * @param client identification of the client
1294 * @param message the actual message
1297 handle_remove (void *cls,
1298 struct GNUNET_SERVER_Client *client,
1299 const struct GNUNET_MessageHeader *message)
1301 const struct DataMessage *dm = check_data (message);
1302 struct GNUNET_HashCode vhash;
1307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1310 GNUNET_STATISTICS_update (stats,
1311 gettext_noop ("# REMOVE requests received"),
1313 GNUNET_SERVER_client_keep (client);
1314 GNUNET_CRYPTO_hash (&dm[1],
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 "Processing REMOVE request for `%s' of type %u\n",
1319 GNUNET_h2s (&dm->key),
1321 plugin->api->get_key (plugin->api->cls,
1325 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1326 &remove_callback, client);
1331 * Handle DROP-message.
1333 * @param cls closure
1334 * @param client identification of the client
1335 * @param message the actual message
1338 handle_drop (void *cls,
1339 struct GNUNET_SERVER_Client *client,
1340 const struct GNUNET_MessageHeader *message)
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 "Processing DROP request\n");
1344 do_drop = GNUNET_YES;
1345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1350 * Function called by plugins to notify us about a
1351 * change in their disk utilization.
1353 * @param cls closure (NULL)
1354 * @param delta change in disk utilization,
1355 * 0 for "reset to empty"
1358 disk_utilization_change_cb (void *cls,
1361 if ((delta < 0) && (payload < -delta))
1363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1364 _("Datastore payload must have been inaccurate (%lld < %lld). Recomputing it.\n"),
1365 (long long) payload,
1366 (long long) -delta);
1367 plugin->api->estimate_size (plugin->api->cls, &payload);
1368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1369 _("New payload: %lld\n"),
1370 (long long) payload);
1376 if (last_sync >= MAX_STAT_SYNC_LAG)
1382 * Callback function to process statistic values.
1384 * @param cls closure (struct Plugin*)
1385 * @param subsystem name of subsystem that created the statistic
1386 * @param name the name of the datum
1387 * @param value the current value
1388 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
1389 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
1392 process_stat_in (void *cls,
1393 const char *subsystem,
1398 GNUNET_assert (GNUNET_NO == stats_worked);
1399 stats_worked = GNUNET_YES;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1403 (unsigned long long) value,
1404 (unsigned long long) payload);
1410 * Load the datastore plugin.
1412 static struct DatastorePlugin *
1415 struct DatastorePlugin *ret;
1418 ret = GNUNET_new (struct DatastorePlugin);
1420 ret->env.duc = &disk_utilization_change_cb;
1421 ret->env.cls = NULL;
1422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1423 _("Loading `%s' datastore plugin\n"),
1425 GNUNET_asprintf (&libname,
1426 "libgnunet_plugin_datastore_%s",
1428 ret->short_name = GNUNET_strdup (plugin_name);
1429 ret->lib_name = libname;
1430 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1431 if (NULL == ret->api)
1433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1434 _("Failed to load datastore plugin for `%s'\n"),
1436 GNUNET_free (ret->short_name);
1437 GNUNET_free (libname);
1446 * Function called when the service shuts
1447 * down. Unloads our datastore plugin.
1449 * @param plug plugin to unload
1452 unload_plugin (struct DatastorePlugin *plug)
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Datastore service is unloading plugin...\n");
1456 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1457 GNUNET_free (plug->lib_name);
1458 GNUNET_free (plug->short_name);
1463 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1464 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1465 sizeof (struct ReserveMessage)},
1466 {&handle_release_reserve, NULL,
1467 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1468 sizeof (struct ReleaseReserveMessage)},
1469 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0},
1470 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1471 sizeof (struct UpdateMessage)},
1472 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0},
1473 {&handle_get_replication, NULL,
1474 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1475 sizeof (struct GNUNET_MessageHeader)},
1476 {&handle_get_zero_anonymity, NULL,
1477 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1478 sizeof (struct GetZeroAnonymityMessage)},
1479 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0},
1480 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1481 sizeof (struct GNUNET_MessageHeader)},
1487 * Adds a given @a key to the bloomfilter in @a cls @a count times.
1489 * @param cls the bloomfilter
1490 * @param key key to add
1491 * @param count number of times to add key
1494 add_key_to_bloomfilter (void *cls,
1495 const struct GNUNET_HashCode *key,
1498 struct GNUNET_CONTAINER_BloomFilter *bf = cls;
1502 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1503 _("Bloomfilter construction complete.\n"));
1504 GNUNET_SERVER_add_handlers (server, handlers);
1505 GNUNET_SERVER_resume (server);
1507 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1514 GNUNET_CONTAINER_bloomfilter_add (bf, key);
1519 * We finished receiving the statistic. Initialize the plugin; if
1520 * loading the statistic failed, run the estimator.
1523 * @param success #GNUNET_NO if we failed to read the stat
1526 process_stat_done (void *cls,
1531 plugin = load_plugin ();
1534 GNUNET_CONTAINER_bloomfilter_free (filter);
1538 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1543 if (GNUNET_NO == stats_worked)
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Failed to obtain value from statistics service, recomputing it\n");
1547 plugin->api->estimate_size (plugin->api->cls,
1550 if (GNUNET_YES == refresh_bf)
1552 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1553 _("Rebuilding bloomfilter. Please be patient.\n"));
1554 if (NULL != plugin->api->get_keys)
1556 plugin->api->get_keys (plugin->api->cls,
1557 &add_key_to_bloomfilter,
1562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1563 _("Plugin does not support get_keys function. Please fix!\n"));
1565 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1566 _("Bloomfilter construction complete.\n"));
1569 GNUNET_SERVER_add_handlers (server, handlers);
1570 GNUNET_SERVER_resume (server);
1572 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1579 * Task run during shutdown.
1582 cleaning_task (void *cls)
1584 struct TransmitCallbackContext *tcc;
1586 cleaning_done = GNUNET_YES;
1587 while (NULL != (tcc = tcc_head))
1589 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
1590 if (tcc->th != NULL)
1592 GNUNET_SERVER_notify_transmit_ready_cancel (tcc->th);
1593 GNUNET_SERVER_client_drop (tcc->client);
1595 GNUNET_free (tcc->msg);
1598 if (NULL != expired_kill_task)
1600 GNUNET_SCHEDULER_cancel (expired_kill_task);
1601 expired_kill_task = NULL;
1603 if (GNUNET_YES == do_drop)
1604 plugin->api->drop (plugin->api->cls);
1607 unload_plugin (plugin);
1612 GNUNET_CONTAINER_bloomfilter_free (filter);
1615 if (NULL != stat_get)
1617 GNUNET_STATISTICS_get_cancel (stat_get);
1620 GNUNET_free_non_null (plugin_name);
1626 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1629 GNUNET_free (quota_stat_name);
1630 quota_stat_name = NULL;
1635 * Function that removes all active reservations made
1636 * by the given client and releases the space for other
1639 * @param cls closure
1640 * @param client identification of the client
1643 cleanup_reservations (void *cls,
1644 struct GNUNET_SERVER_Client *client)
1646 struct ReservationList *pos;
1647 struct ReservationList *prev;
1648 struct ReservationList *next;
1657 if (pos->client == client)
1660 reservations = next;
1663 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1672 GNUNET_STATISTICS_set (stats,
1673 gettext_noop ("# reserved"),
1680 * Process datastore requests.
1682 * @param cls closure
1683 * @param serv the initialized server
1684 * @param c configuration to use
1688 struct GNUNET_SERVER_Handle *serv,
1689 const struct GNUNET_CONFIGURATION_Handle *c)
1693 unsigned int bf_size;
1698 GNUNET_CONFIGURATION_get_value_string (cfg,
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1704 _("No `%s' specified for `%s' in configuration!\n"),
1709 GNUNET_asprintf ("a_stat_name,
1710 _("# bytes used in file-sharing datastore `%s'"),
1713 GNUNET_CONFIGURATION_get_value_size (cfg, "DATASTORE", "QUOTA", "a))
1715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1716 _("No `%s' specified for `%s' in configuration!\n"),
1721 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1722 GNUNET_STATISTICS_set (stats, gettext_noop ("# quota"), quota, GNUNET_NO);
1723 cache_size = quota / 8; /* Or should we make this an option? */
1724 GNUNET_STATISTICS_set (stats, gettext_noop ("# cache size"), cache_size,
1726 if (quota / (32 * 1024LL) > (1 << 31))
1727 bf_size = (1 << 31); /* absolute limit: ~2 GB, beyond that BF just won't help anyway */
1729 bf_size = quota / (32 * 1024LL); /* 8 bit per entry, 1 bit per 32 kb in DB */
1732 GNUNET_CONFIGURATION_get_value_filename (cfg,
1736 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1739 _("Could not use specified filename `%s' for bloomfilter.\n"),
1740 NULL != fn ? fn : "");
1741 GNUNET_free_non_null (fn);
1746 GNUNET_asprintf (&pfn, "%s.%s", fn, plugin_name);
1747 if (GNUNET_YES == GNUNET_DISK_file_test (pfn))
1749 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1752 /* file exists but not valid, remove and try again, but refresh */
1753 if (0 != UNLINK (pfn))
1755 /* failed to remove, run without file */
1756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1757 _("Failed to remove bogus bloomfilter file `%s'\n"),
1761 filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1762 refresh_bf = GNUNET_YES;
1766 /* try again after remove */
1767 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1768 refresh_bf = GNUNET_YES;
1771 /* failed yet again, give up on using file */
1772 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1773 _("Failed to remove bogus bloomfilter file `%s'\n"),
1777 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1783 /* normal case: have an existing valid bf file, no need to refresh */
1784 refresh_bf = GNUNET_NO;
1789 filter = GNUNET_CONTAINER_bloomfilter_load (pfn, bf_size, 5); /* approx. 3% false positives at max use */
1790 refresh_bf = GNUNET_YES;
1796 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1797 refresh_bf = GNUNET_YES;
1799 GNUNET_free_non_null (fn);
1802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1803 _("Failed to initialize bloomfilter.\n"));
1806 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1811 GNUNET_SERVER_suspend (server);
1813 GNUNET_STATISTICS_get (stats,
1816 GNUNET_TIME_UNIT_SECONDS,
1820 if (NULL == stat_get)
1821 process_stat_done (NULL, GNUNET_SYSERR);
1822 GNUNET_SERVER_disconnect_notify (server,
1823 &cleanup_reservations,
1825 GNUNET_SCHEDULER_add_shutdown (&cleaning_task,
1831 * The main function for the datastore service.
1833 * @param argc number of arguments from the command line
1834 * @param argv command line arguments
1835 * @return 0 ok, 1 on error
1845 GNUNET_SERVICE_run (argc, argv, "datastore",
1846 GNUNET_SERVICE_OPTION_NONE,
1847 &run, NULL)) ? 0 : 1;
1852 /* end of gnunet-service-datastore.c */