2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 #define QUOTA_STAT_NAME gettext_noop ("# bytes used in file-sharing datastore")
55 * After how many payload-changing operations
56 * do we sync our statistics?
58 #define MAX_STAT_SYNC_LAG 50
62 * Our datastore plugin.
64 struct DatastorePlugin
68 * API of the transport as returned by the plugin's
69 * initialization function.
71 struct GNUNET_DATASTORE_PluginFunctions *api;
74 * Short name for the plugin (i.e. "sqlite").
79 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
84 * Environment this transport service is using
87 struct GNUNET_DATASTORE_PluginEnvironment env;
93 * Linked list of active reservations.
95 struct ReservationList
99 * This is a linked list.
101 struct ReservationList *next;
104 * Client that made the reservation.
106 struct GNUNET_SERVER_Client *client;
109 * Number of bytes (still) reserved.
114 * Number of items (still) reserved.
119 * Reservation identifier.
128 * Our datastore plugin (NULL if not available).
130 static struct DatastorePlugin *plugin;
133 * Linked list of space reservations made by clients.
135 static struct ReservationList *reservations;
138 * Bloomfilter to quickly tell if we don't have the content.
140 static struct GNUNET_CONTAINER_BloomFilter *filter;
143 * How much space are we allowed to use?
145 static unsigned long long quota;
148 * How much space are we using for the cache? (space available for
149 * insertions that will be instantly reclaimed by discarding less
150 * important content --- or possibly whatever we just inserted into
153 static unsigned long long cache_size;
156 * How much space have we currently reserved?
158 static unsigned long long reserved;
161 * How much data are we currently storing
164 static unsigned long long payload;
167 * Number of updates that were made to the
168 * payload value since we last synchronized
169 * it with the statistics service.
171 static unsigned int lastSync;
174 * Did we get an answer from statistics?
176 static int stats_worked;
179 * Identity of the task that is used to delete
182 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
187 const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Handle for reporting statistics.
193 static struct GNUNET_STATISTICS_Handle *stats;
197 * Synchronize our utilization statistics with the
198 * statistics service.
203 GNUNET_STATISTICS_set (stats,
214 * Function called once the transmit operation has
215 * either failed or succeeded.
218 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
220 typedef void (*TransmitContinuation)(void *cls,
225 * Context for transmitting replies to clients.
227 struct TransmitCallbackContext
231 * We keep these in a doubly-linked list (for cleanup).
233 struct TransmitCallbackContext *next;
236 * We keep these in a doubly-linked list (for cleanup).
238 struct TransmitCallbackContext *prev;
241 * The message that we're asked to transmit.
243 struct GNUNET_MessageHeader *msg;
246 * Handle for the transmission request.
248 struct GNUNET_CONNECTION_TransmitHandle *th;
251 * Client that we are transmitting to.
253 struct GNUNET_SERVER_Client *client;
256 * Function to call once msg has been transmitted
257 * (or at least added to the buffer).
259 TransmitContinuation tc;
267 * GNUNET_YES if we are supposed to signal the server
268 * completion of the client's request.
275 * Head of the doubly-linked list (for cleanup).
277 static struct TransmitCallbackContext *tcc_head;
280 * Tail of the doubly-linked list (for cleanup).
282 static struct TransmitCallbackContext *tcc_tail;
285 * Have we already cleaned up the TCCs and are hence no longer
286 * willing (or able) to transmit anything to anyone?
288 static int cleaning_done;
291 * Handle for pending get request.
293 static struct GNUNET_STATISTICS_GetHandle *stat_get;
297 * Task that is used to remove expired entries from
298 * the datastore. This task will schedule itself
299 * again automatically to always delete all expired
302 * @param cls not used
303 * @param tc task context
306 delete_expired (void *cls,
307 const struct GNUNET_SCHEDULER_TaskContext *tc);
311 * Iterate over the expired items stored in the datastore.
312 * Delete all expired items; once we have processed all
313 * expired items, re-schedule the "delete_expired" task.
315 * @param cls not used
316 * @param next_cls closure to pass to the "next" function.
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,
334 const GNUNET_HashCode * key,
337 enum GNUNET_BLOCK_Type type,
340 struct GNUNET_TIME_Absolute
344 struct GNUNET_TIME_Absolute now;
349 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
352 return GNUNET_SYSERR;
354 now = GNUNET_TIME_absolute_get ();
355 if (expiration.abs_value > now.abs_value)
357 /* finished processing */
359 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
362 return GNUNET_SYSERR;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Deleting content `%s' of type %u that expired %llu ms ago\n",
369 (unsigned long long) (now.abs_value - expiration.abs_value));
371 GNUNET_STATISTICS_update (stats,
372 gettext_noop ("# bytes expired"),
375 GNUNET_CONTAINER_bloomfilter_remove (filter,
378 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY,
386 * Task that is used to remove expired entries from
387 * the datastore. This task will schedule itself
388 * again automatically to always delete all expired
391 * @param cls not used
392 * @param tc task context
395 delete_expired (void *cls,
396 const struct GNUNET_SCHEDULER_TaskContext *tc)
398 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
399 plugin->api->expiration_get (plugin->api->cls,
406 * An iterator over a set of items stored in the datastore
407 * that deletes until we're happy with respect to our quota.
410 * @param next_cls closure to pass to the "next" function.
411 * @param key key for the content
412 * @param size number of bytes in data
413 * @param data content stored
414 * @param type type of the content
415 * @param priority priority of the content
416 * @param anonymity anonymity-level for the content
417 * @param expiration expiration time for the content
418 * @param uid unique identifier for the datum;
419 * maybe 0 if no unique identifier is available
421 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
422 * (continue on call to "next", of course),
423 * GNUNET_NO to delete the item and continue (if supported)
426 quota_processor (void *cls,
428 const GNUNET_HashCode * key,
431 enum GNUNET_BLOCK_Type type,
434 struct GNUNET_TIME_Absolute expiration,
437 unsigned long long *need = cls;
440 return GNUNET_SYSERR;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
444 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
449 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
452 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
453 GNUNET_STATISTICS_update (stats,
454 gettext_noop ("# bytes purged (low-priority)"),
457 GNUNET_CONTAINER_bloomfilter_remove (filter,
464 * Manage available disk space by running tasks
465 * that will discard content if necessary. This
466 * function will be run whenever a request for
467 * "need" bytes of storage could only be satisfied
468 * by eating into the "cache" (and we want our cache
471 * @param need number of bytes of content that were
472 * placed into the "cache" (and hence the
473 * number of bytes that should be removed).
476 manage_space (unsigned long long need)
478 unsigned long long last;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Asked to free up %llu bytes of cache space\n",
486 while ( (need > 0) &&
490 plugin->api->expiration_get (plugin->api->cls,
498 * Function called to notify a client about the socket
499 * begin ready to queue more data. "buf" will be
500 * NULL and "size" zero if the socket was closed for
501 * writing in the meantime.
504 * @param size number of bytes available in buf
505 * @param buf where the callee should write the message
506 * @return number of bytes written to buf
509 transmit_callback (void *cls,
510 size_t size, void *buf)
512 struct TransmitCallbackContext *tcc = cls;
516 GNUNET_CONTAINER_DLL_remove (tcc_head,
519 msize = ntohs(tcc->msg->size);
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
523 _("Transmission to client failed!\n"));
525 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
526 if (GNUNET_YES == tcc->end)
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 _("Disconnecting client due to transmission failure!\n"));
530 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
532 GNUNET_SERVER_client_drop (tcc->client);
533 GNUNET_free (tcc->msg);
537 GNUNET_assert (size >= msize);
538 memcpy (buf, tcc->msg, msize);
540 tcc->tc (tcc->tc_cls, GNUNET_OK);
541 if (GNUNET_YES == tcc->end)
543 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Response transmitted, more pending!\n");
552 GNUNET_SERVER_client_drop (tcc->client);
553 GNUNET_free (tcc->msg);
560 * Transmit the given message to the client.
562 * @param client target of the message
563 * @param msg message to transmit, will be freed!
564 * @param tc function to call afterwards
565 * @param tc_cls closure for tc
566 * @param end is this the last response (and we should
567 * signal the server completion accodingly after
568 * transmitting this message)?
571 transmit (struct GNUNET_SERVER_Client *client,
572 struct GNUNET_MessageHeader *msg,
573 TransmitContinuation tc,
577 struct TransmitCallbackContext *tcc;
579 if (GNUNET_YES == cleaning_done)
582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
583 "Shutdown in progress, aborting transmission.\n");
587 tc (tc_cls, GNUNET_SYSERR);
590 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
592 tcc->client = client;
594 tcc->tc_cls = tc_cls;
597 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
599 GNUNET_TIME_UNIT_FOREVER_REL,
604 if (GNUNET_YES == end)
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607 _("Forcefully disconnecting client.\n"));
608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
611 tc (tc_cls, GNUNET_SYSERR);
616 GNUNET_SERVER_client_keep (client);
617 GNUNET_CONTAINER_DLL_insert (tcc_head,
624 * Transmit a status code to the client.
626 * @param client receiver of the response
627 * @param code status code
628 * @param msg optional error message (can be NULL)
631 transmit_status (struct GNUNET_SERVER_Client *client,
635 struct StatusMessage *sm;
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Transmitting `%s' message with value %d and message `%s'\n",
643 msg != NULL ? msg : "(none)");
645 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
646 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
647 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
648 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
649 sm->status = htonl(code);
651 memcpy (&sm[1], msg, slen);
652 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
657 * Function called once the transmit operation has
658 * either failed or succeeded.
660 * @param next_cls closure for calling "next_request" callback
661 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
664 get_next(void *next_cls,
667 if (status != GNUNET_OK)
669 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
670 _("Failed to transmit an item to the client; aborting iteration.\n"));
672 plugin->api->next_request (next_cls, GNUNET_YES);
675 plugin->api->next_request (next_cls, GNUNET_NO);
680 * Function that will transmit the given datastore entry
683 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
684 * @param next_cls closure to use to ask for the next item
685 * @param key key for the content
686 * @param size number of bytes in data
687 * @param data content stored
688 * @param type type of the content
689 * @param priority priority of the content
690 * @param anonymity anonymity-level for the content
691 * @param expiration expiration time for the content
692 * @param uid unique identifier for the datum;
693 * maybe 0 if no unique identifier is available
695 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
696 * GNUNET_NO to delete the item and continue (if supported)
699 transmit_item (void *cls,
701 const GNUNET_HashCode * key,
704 enum GNUNET_BLOCK_Type type,
707 struct GNUNET_TIME_Absolute
708 expiration, uint64_t uid)
710 struct GNUNET_SERVER_Client *client = cls;
711 struct GNUNET_MessageHeader *end;
712 struct DataMessage *dm;
716 /* transmit 'DATA_END' */
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Transmitting `%s' message\n",
722 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
723 end->size = htons(sizeof(struct GNUNET_MessageHeader));
724 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
725 transmit (client, end, NULL, NULL, GNUNET_YES);
726 GNUNET_SERVER_client_drop (client);
729 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
730 dm->header.size = htons(sizeof(struct DataMessage) + size);
731 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
733 dm->size = htonl(size);
734 dm->type = htonl(type);
735 dm->priority = htonl(priority);
736 dm->anonymity = htonl(anonymity);
737 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
738 dm->uid = GNUNET_htonll(uid);
740 memcpy (&dm[1], data, size);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Transmitting `%s' message for `%s' of type %u\n",
748 GNUNET_STATISTICS_update (stats,
749 gettext_noop ("# results found"),
752 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
758 * Handle RESERVE-message.
761 * @param client identification of the client
762 * @param message the actual message
765 handle_reserve (void *cls,
766 struct GNUNET_SERVER_Client *client,
767 const struct GNUNET_MessageHeader *message)
770 * Static counter to produce reservation identifiers.
772 static int reservation_gen;
774 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
775 struct ReservationList *e;
776 unsigned long long used;
777 unsigned long long req;
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Processing `%s' request\n",
786 amount = GNUNET_ntohll(msg->amount);
787 entries = ntohl(msg->entries);
788 used = payload + reserved;
789 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
790 if (used + req > quota)
793 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
795 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
799 if (cache_size < req)
801 /* TODO: document this in the FAQ; essentially, if this
802 message happens, the insertion request could be blocked
803 by less-important content from migration because it is
804 larger than 1/8th of the overall available space, and
805 we only reserve 1/8th for "fresh" insertions */
806 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
807 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
810 transmit_status (client, 0,
811 gettext_noop ("Insufficient space to satisfy request and "
812 "requested amount is larger than cache size"));
816 transmit_status (client, 0,
817 gettext_noop ("Insufficient space to satisfy request"));
822 GNUNET_STATISTICS_set (stats,
823 gettext_noop ("# reserved"),
826 e = GNUNET_malloc (sizeof(struct ReservationList));
827 e->next = reservations;
831 e->entries = entries;
832 e->rid = ++reservation_gen;
833 if (reservation_gen < 0)
834 reservation_gen = 0; /* wrap around */
835 transmit_status (client, e->rid, NULL);
840 * Handle RELEASE_RESERVE-message.
843 * @param client identification of the client
844 * @param message the actual message
847 handle_release_reserve (void *cls,
848 struct GNUNET_SERVER_Client *client,
849 const struct GNUNET_MessageHeader *message)
851 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
852 struct ReservationList *pos;
853 struct ReservationList *prev;
854 struct ReservationList *next;
855 int rid = ntohl(msg->rid);
856 unsigned long long rem;
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Processing `%s' request\n",
865 while (NULL != (pos = next))
874 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
875 GNUNET_assert (reserved >= rem);
877 GNUNET_STATISTICS_set (stats,
878 gettext_noop ("# reserved"),
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Returning %llu remaining reserved bytes to storage pool\n",
887 transmit_status (client, GNUNET_OK, NULL);
893 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
898 * Check that the given message is a valid data message.
900 * @return NULL if the message is not well-formed, otherwise the message
902 static const struct DataMessage *
903 check_data (const struct GNUNET_MessageHeader *message)
907 const struct DataMessage *dm;
909 size = ntohs(message->size);
910 if (size < sizeof(struct DataMessage))
915 dm = (const struct DataMessage *) message;
916 dsize = ntohl(dm->size);
917 if (size != dsize + sizeof(struct DataMessage))
927 * Context for a put request used to see if the content is
933 * Client to notify on completion.
935 struct GNUNET_SERVER_Client *client;
938 * Did we find the data already in the database?
942 /* followed by the 'struct DataMessage' */
947 * Actually put the data message.
950 execute_put (struct GNUNET_SERVER_Client *client,
951 const struct DataMessage *dm)
957 size = ntohl(dm->size);
959 ret = plugin->api->put (plugin->api->cls,
965 ntohl(dm->anonymity),
966 0 /* FIXME: replication */,
967 GNUNET_TIME_absolute_ntoh(dm->expiration),
969 if (GNUNET_OK == ret)
971 GNUNET_STATISTICS_update (stats,
972 gettext_noop ("# bytes stored"),
975 GNUNET_CONTAINER_bloomfilter_add (filter,
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Successfully stored %u bytes of type %u under key `%s'\n",
982 GNUNET_h2s (&dm->key));
985 transmit_status (client,
988 GNUNET_free_non_null (msg);
989 if (quota - reserved - cache_size < payload)
991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
992 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
993 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
994 (unsigned long long) (quota - reserved - cache_size),
995 (unsigned long long) payload);
996 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1003 * Function that will check if the given datastore entry
1004 * matches the put and if none match executes the put.
1006 * @param cls closure, pointer to the client (of type 'struct PutContext').
1007 * @param next_cls closure to use to ask for the next item
1008 * @param key key for the content
1009 * @param size number of bytes in data
1010 * @param data content stored
1011 * @param type type of the content
1012 * @param priority priority of the content
1013 * @param anonymity anonymity-level for the content
1014 * @param expiration expiration time for the content
1015 * @param uid unique identifier for the datum;
1016 * maybe 0 if no unique identifier is available
1018 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
1019 * GNUNET_NO to delete the item and continue (if supported)
1022 check_present (void *cls,
1024 const GNUNET_HashCode * key,
1027 enum GNUNET_BLOCK_Type type,
1030 struct GNUNET_TIME_Absolute
1031 expiration, uint64_t uid)
1033 struct PutContext *pc = cls;
1034 const struct DataMessage *dm;
1036 dm = (const struct DataMessage*) &pc[1];
1039 if (pc->is_present == GNUNET_YES)
1040 transmit_status (pc->client, GNUNET_NO, NULL);
1042 execute_put (pc->client, dm);
1043 GNUNET_SERVER_client_drop (pc->client);
1045 return GNUNET_SYSERR;
1047 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
1048 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
1049 ( (size == ntohl(dm->size)) &&
1050 (0 == memcmp (&dm[1],
1054 pc->is_present = GNUNET_YES;
1055 plugin->api->next_request (next_cls, GNUNET_YES);
1059 plugin->api->next_request (next_cls, GNUNET_NO);
1066 * Handle PUT-message.
1068 * @param cls closure
1069 * @param client identification of the client
1070 * @param message the actual message
1073 handle_put (void *cls,
1074 struct GNUNET_SERVER_Client *client,
1075 const struct GNUNET_MessageHeader *message)
1077 const struct DataMessage *dm = check_data (message);
1079 struct ReservationList *pos;
1080 struct PutContext *pc;
1083 if ( (dm == NULL) ||
1084 (ntohl(dm->type) == 0) )
1087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Processing `%s' request for `%s' of type %u\n",
1094 GNUNET_h2s (&dm->key),
1097 rid = ntohl(dm->rid);
1098 size = ntohl(dm->size);
1102 while ( (NULL != pos) &&
1105 GNUNET_break (pos != NULL);
1108 GNUNET_break (pos->entries > 0);
1109 GNUNET_break (pos->amount >= size);
1111 pos->amount -= size;
1112 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1113 GNUNET_STATISTICS_set (stats,
1114 gettext_noop ("# reserved"),
1119 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1122 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1123 pc->client = client;
1124 GNUNET_SERVER_client_keep (client);
1125 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1126 plugin->api->get (plugin->api->cls,
1134 execute_put (client, dm);
1139 * Handle GET-message.
1141 * @param cls closure
1142 * @param client identification of the client
1143 * @param message the actual message
1146 handle_get (void *cls,
1147 struct GNUNET_SERVER_Client *client,
1148 const struct GNUNET_MessageHeader *message)
1150 const struct GetMessage *msg;
1153 size = ntohs(message->size);
1154 if ( (size != sizeof(struct GetMessage)) &&
1155 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1161 msg = (const struct GetMessage*) message;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Processing `%s' request for `%s' of type %u\n",
1166 GNUNET_h2s (&msg->key),
1169 GNUNET_STATISTICS_update (stats,
1170 gettext_noop ("# GET requests received"),
1173 GNUNET_SERVER_client_keep (client);
1174 if ( (size == sizeof(struct GetMessage)) &&
1175 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1178 /* don't bother database... */
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1183 GNUNET_h2s (&msg->key));
1185 GNUNET_STATISTICS_update (stats,
1186 gettext_noop ("# requests filtered by bloomfilter"),
1189 transmit_item (client,
1190 NULL, NULL, 0, NULL, 0, 0, 0,
1191 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1194 plugin->api->get (plugin->api->cls,
1195 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1204 * Handle UPDATE-message.
1206 * @param cls closure
1207 * @param client identification of the client
1208 * @param message the actual message
1211 handle_update (void *cls,
1212 struct GNUNET_SERVER_Client *client,
1213 const struct GNUNET_MessageHeader *message)
1215 const struct UpdateMessage *msg;
1219 GNUNET_STATISTICS_update (stats,
1220 gettext_noop ("# UPDATE requests received"),
1223 msg = (const struct UpdateMessage*) message;
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Processing `%s' request for %llu\n",
1229 (unsigned long long) GNUNET_ntohll (msg->uid));
1231 ret = plugin->api->update (plugin->api->cls,
1232 GNUNET_ntohll(msg->uid),
1233 (int32_t) ntohl(msg->priority),
1234 GNUNET_TIME_absolute_ntoh(msg->expiration),
1236 transmit_status (client, ret, emsg);
1237 GNUNET_free_non_null (emsg);
1242 * Handle GET_RANDOM-message.
1244 * @param cls closure
1245 * @param client identification of the client
1246 * @param message the actual message
1249 handle_get_random (void *cls,
1250 struct GNUNET_SERVER_Client *client,
1251 const struct GNUNET_MessageHeader *message)
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Processing `%s' request\n",
1258 GNUNET_STATISTICS_update (stats,
1259 gettext_noop ("# GET RANDOM requests received"),
1262 GNUNET_SERVER_client_keep (client);
1263 plugin->api->replication_get (plugin->api->cls,
1269 * Handle GET_ZERO_ANONYMITY-message.
1271 * @param cls closure
1272 * @param client identification of the client
1273 * @param message the actual message
1276 handle_get_zero_anonymity (void *cls,
1277 struct GNUNET_SERVER_Client *client,
1278 const struct GNUNET_MessageHeader *message)
1280 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message;
1281 enum GNUNET_BLOCK_Type type;
1283 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "Processing `%s' request\n",
1287 "GET_ZERO_ANONYMITY");
1289 GNUNET_STATISTICS_update (stats,
1290 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1293 GNUNET_SERVER_client_keep (client);
1294 plugin->api->iter_zero_anonymity (plugin->api->cls,
1302 * Context for the 'remove_callback'.
1304 struct RemoveContext
1307 * Client for whom we're doing the remvoing.
1309 struct GNUNET_SERVER_Client *client;
1312 * GNUNET_YES if we managed to remove something.
1319 * Callback function that will cause the item that is passed
1320 * in to be deleted (by returning GNUNET_NO).
1323 remove_callback (void *cls,
1325 const GNUNET_HashCode * key,
1328 enum GNUNET_BLOCK_Type type,
1331 struct GNUNET_TIME_Absolute
1332 expiration, uint64_t uid)
1334 struct RemoveContext *rc = cls;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "No further matches for `%s' request.\n",
1343 if (GNUNET_YES == rc->found)
1344 transmit_status (rc->client, GNUNET_OK, NULL);
1346 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1347 GNUNET_SERVER_client_drop (rc->client);
1349 return GNUNET_OK; /* last item */
1351 rc->found = GNUNET_YES;
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1355 (unsigned long long) uid,
1360 GNUNET_STATISTICS_update (stats,
1361 gettext_noop ("# bytes removed (explicit request)"),
1364 GNUNET_CONTAINER_bloomfilter_remove (filter,
1366 plugin->api->next_request (next_cls, GNUNET_YES);
1372 * Handle REMOVE-message.
1374 * @param cls closure
1375 * @param client identification of the client
1376 * @param message the actual message
1379 handle_remove (void *cls,
1380 struct GNUNET_SERVER_Client *client,
1381 const struct GNUNET_MessageHeader *message)
1383 const struct DataMessage *dm = check_data (message);
1384 GNUNET_HashCode vhash;
1385 struct RemoveContext *rc;
1390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Processing `%s' request for `%s' of type %u\n",
1397 GNUNET_h2s (&dm->key),
1400 GNUNET_STATISTICS_update (stats,
1401 gettext_noop ("# REMOVE requests received"),
1404 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1405 GNUNET_SERVER_client_keep (client);
1406 rc->client = client;
1407 GNUNET_CRYPTO_hash (&dm[1],
1410 plugin->api->get (plugin->api->cls,
1413 (enum GNUNET_BLOCK_Type) ntohl(dm->type),
1420 * Handle DROP-message.
1422 * @param cls closure
1423 * @param client identification of the client
1424 * @param message the actual message
1427 handle_drop (void *cls,
1428 struct GNUNET_SERVER_Client *client,
1429 const struct GNUNET_MessageHeader *message)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433 "Processing `%s' request\n",
1436 plugin->api->drop (plugin->api->cls);
1437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1442 * Function called by plugins to notify us about a
1443 * change in their disk utilization.
1445 * @param cls closure (NULL)
1446 * @param delta change in disk utilization,
1447 * 0 for "reset to empty"
1450 disk_utilization_change_cb (void *cls,
1454 (payload < -delta) )
1456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1457 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1458 (long long) payload,
1459 (long long) -delta);
1460 payload = plugin->api->get_size (plugin->api->cls);
1466 if (lastSync >= MAX_STAT_SYNC_LAG)
1472 * Callback function to process statistic values.
1474 * @param cls closure (struct Plugin*)
1475 * @param subsystem name of subsystem that created the statistic
1476 * @param name the name of the datum
1477 * @param value the current value
1478 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1479 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1482 process_stat_in (void *cls,
1483 const char *subsystem,
1488 GNUNET_assert (stats_worked == GNUNET_NO);
1489 stats_worked = GNUNET_YES;
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1502 process_stat_done (void *cls,
1505 struct DatastorePlugin *plugin = cls;
1508 if (stats_worked == GNUNET_NO)
1509 payload = plugin->api->get_size (plugin->api->cls);
1514 * Load the datastore plugin.
1516 static struct DatastorePlugin *
1519 struct DatastorePlugin *ret;
1524 GNUNET_CONFIGURATION_get_value_string (cfg,
1525 "DATASTORE", "DATABASE", &name))
1527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1528 _("No `%s' specified for `%s' in configuration!\n"),
1533 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1535 ret->env.duc = &disk_utilization_change_cb;
1536 ret->env.cls = NULL;
1537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1538 _("Loading `%s' datastore plugin\n"), name);
1539 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1540 ret->short_name = name;
1541 ret->lib_name = libname;
1542 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1543 if (ret->api == NULL)
1545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1546 _("Failed to load datastore plugin for `%s'\n"), name);
1547 GNUNET_free (ret->short_name);
1548 GNUNET_free (libname);
1557 * Function called when the service shuts
1558 * down. Unloads our datastore plugin.
1560 * @param plug plugin to unload
1563 unload_plugin (struct DatastorePlugin *plug)
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 "Datastore service is unloading plugin...\n");
1569 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1570 GNUNET_free (plug->lib_name);
1571 GNUNET_free (plug->short_name);
1577 * Final task run after shutdown. Unloads plugins and disconnects us from
1581 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1583 unload_plugin (plugin);
1587 GNUNET_CONTAINER_bloomfilter_free (filter);
1592 if (stat_get != NULL)
1594 GNUNET_STATISTICS_get_cancel (stat_get);
1599 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1606 * Last task run during shutdown. Disconnects us from
1607 * the transport and core.
1610 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1612 struct TransmitCallbackContext *tcc;
1614 cleaning_done = GNUNET_YES;
1615 while (NULL != (tcc = tcc_head))
1617 GNUNET_CONTAINER_DLL_remove (tcc_head,
1620 if (tcc->th != NULL)
1622 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1623 GNUNET_SERVER_client_drop (tcc->client);
1625 if (NULL != tcc->tc)
1626 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1627 GNUNET_free (tcc->msg);
1630 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1632 GNUNET_SCHEDULER_cancel (expired_kill_task);
1633 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1635 GNUNET_SCHEDULER_add_continuation (&unload_task,
1637 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1642 * Function that removes all active reservations made
1643 * by the given client and releases the space for other
1646 * @param cls closure
1647 * @param client identification of the client
1650 cleanup_reservations (void *cls,
1651 struct GNUNET_SERVER_Client
1654 struct ReservationList *pos;
1655 struct ReservationList *prev;
1656 struct ReservationList *next;
1665 if (pos->client == client)
1668 reservations = next;
1671 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1680 GNUNET_STATISTICS_set (stats,
1681 gettext_noop ("# reserved"),
1688 * Process datastore requests.
1690 * @param cls closure
1691 * @param server the initialized server
1692 * @param c configuration to use
1696 struct GNUNET_SERVER_Handle *server,
1697 const struct GNUNET_CONFIGURATION_Handle *c)
1699 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1700 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1701 sizeof(struct ReserveMessage) },
1702 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1703 sizeof(struct ReleaseReserveMessage) },
1704 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1705 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1706 sizeof (struct UpdateMessage) },
1707 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1708 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1709 sizeof(struct GNUNET_MessageHeader) },
1710 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1711 sizeof(struct GetZeroAnonymityMessage) },
1712 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1713 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1714 sizeof(struct GNUNET_MessageHeader) },
1718 unsigned int bf_size;
1722 GNUNET_CONFIGURATION_get_value_number (cfg,
1723 "DATASTORE", "QUOTA", "a))
1725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1726 _("No `%s' specified for `%s' in configuration!\n"),
1731 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1732 GNUNET_STATISTICS_set (stats,
1733 gettext_noop ("# quota"),
1736 cache_size = quota / 8; /* Or should we make this an option? */
1737 GNUNET_STATISTICS_set (stats,
1738 gettext_noop ("# cache size"),
1741 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1744 GNUNET_CONFIGURATION_get_value_filename (cfg,
1749 GNUNET_DISK_directory_create_for_file (fn)) )
1751 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1752 _("Could not use specified filename `%s' for bloomfilter.\n"),
1753 fn != NULL ? fn : "");
1754 GNUNET_free_non_null (fn);
1757 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1758 GNUNET_free_non_null (fn);
1761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1762 _("Failed to initialize bloomfilter.\n"));
1765 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1770 plugin = load_plugin ();
1773 GNUNET_CONTAINER_bloomfilter_free (filter);
1777 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1782 stat_get = GNUNET_STATISTICS_get (stats,
1785 GNUNET_TIME_UNIT_SECONDS,
1789 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1790 GNUNET_SERVER_add_handlers (server, handlers);
1792 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1793 &delete_expired, NULL);
1794 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1795 &cleaning_task, NULL);
1800 * The main function for the datastore service.
1802 * @param argc number of arguments from the command line
1803 * @param argv command line arguments
1804 * @return 0 ok, 1 on error
1807 main (int argc, char *const *argv)
1812 GNUNET_SERVICE_run (argc,
1815 GNUNET_SERVICE_OPTION_NONE,
1816 &run, NULL)) ? 0 : 1;
1821 /* end of gnunet-service-datastore.c */