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)
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Done processing client request\n");
547 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Response transmitted, more pending!\n");
556 GNUNET_SERVER_client_drop (tcc->client);
557 GNUNET_free (tcc->msg);
564 * Transmit the given message to the client.
566 * @param client target of the message
567 * @param msg message to transmit, will be freed!
568 * @param tc function to call afterwards
569 * @param tc_cls closure for tc
570 * @param end is this the last response (and we should
571 * signal the server completion accodingly after
572 * transmitting this message)?
575 transmit (struct GNUNET_SERVER_Client *client,
576 struct GNUNET_MessageHeader *msg,
577 TransmitContinuation tc,
581 struct TransmitCallbackContext *tcc;
583 if (GNUNET_YES == cleaning_done)
586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
587 "Shutdown in progress, aborting transmission.\n");
591 tc (tc_cls, GNUNET_SYSERR);
594 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
596 tcc->client = client;
598 tcc->tc_cls = tc_cls;
601 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
603 GNUNET_TIME_UNIT_FOREVER_REL,
608 if (GNUNET_YES == end)
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
611 _("Forcefully disconnecting client.\n"));
612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
615 tc (tc_cls, GNUNET_SYSERR);
620 GNUNET_SERVER_client_keep (client);
621 GNUNET_CONTAINER_DLL_insert (tcc_head,
628 * Transmit a status code to the client.
630 * @param client receiver of the response
631 * @param code status code
632 * @param msg optional error message (can be NULL)
635 transmit_status (struct GNUNET_SERVER_Client *client,
639 struct StatusMessage *sm;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Transmitting `%s' message with value %d and message `%s'\n",
647 msg != NULL ? msg : "(none)");
649 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
650 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
651 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
652 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
653 sm->status = htonl(code);
655 memcpy (&sm[1], msg, slen);
656 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
661 * Function called once the transmit operation has
662 * either failed or succeeded.
664 * @param next_cls closure for calling "next_request" callback
665 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
668 get_next(void *next_cls,
671 if (status != GNUNET_OK)
673 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
674 _("Failed to transmit an item to the client; aborting iteration.\n"));
676 plugin->api->next_request (next_cls, GNUNET_YES);
679 if (next_cls != NULL)
680 plugin->api->next_request (next_cls, GNUNET_NO);
685 * Function that will transmit the given datastore entry
688 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
689 * @param next_cls closure to use to ask for the next item
690 * @param key key for the content
691 * @param size number of bytes in data
692 * @param data content stored
693 * @param type type of the content
694 * @param priority priority of the content
695 * @param anonymity anonymity-level for the content
696 * @param expiration expiration time for the content
697 * @param uid unique identifier for the datum;
698 * maybe 0 if no unique identifier is available
700 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
701 * GNUNET_NO to delete the item and continue (if supported)
704 transmit_item (void *cls,
706 const GNUNET_HashCode * key,
709 enum GNUNET_BLOCK_Type type,
712 struct GNUNET_TIME_Absolute
713 expiration, uint64_t uid)
715 struct GNUNET_SERVER_Client *client = cls;
716 struct GNUNET_MessageHeader *end;
717 struct DataMessage *dm;
721 /* transmit 'DATA_END' */
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Transmitting `%s' message\n",
727 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
728 end->size = htons(sizeof(struct GNUNET_MessageHeader));
729 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
730 transmit (client, end, NULL, NULL, GNUNET_YES);
731 GNUNET_SERVER_client_drop (client);
734 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
735 dm->header.size = htons(sizeof(struct DataMessage) + size);
736 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
738 dm->size = htonl(size);
739 dm->type = htonl(type);
740 dm->priority = htonl(priority);
741 dm->anonymity = htonl(anonymity);
742 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
743 dm->uid = GNUNET_htonll(uid);
745 memcpy (&dm[1], data, size);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Transmitting `%s' message for `%s' of type %u\n",
753 GNUNET_STATISTICS_update (stats,
754 gettext_noop ("# results found"),
757 transmit (client, &dm->header, &get_next, next_cls,
758 (next_cls != NULL) ? GNUNET_NO : GNUNET_YES);
764 * Handle RESERVE-message.
767 * @param client identification of the client
768 * @param message the actual message
771 handle_reserve (void *cls,
772 struct GNUNET_SERVER_Client *client,
773 const struct GNUNET_MessageHeader *message)
776 * Static counter to produce reservation identifiers.
778 static int reservation_gen;
780 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
781 struct ReservationList *e;
782 unsigned long long used;
783 unsigned long long req;
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Processing `%s' request\n",
792 amount = GNUNET_ntohll(msg->amount);
793 entries = ntohl(msg->entries);
794 used = payload + reserved;
795 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
796 if (used + req > quota)
799 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
801 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
805 if (cache_size < req)
807 /* TODO: document this in the FAQ; essentially, if this
808 message happens, the insertion request could be blocked
809 by less-important content from migration because it is
810 larger than 1/8th of the overall available space, and
811 we only reserve 1/8th for "fresh" insertions */
812 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
813 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
816 transmit_status (client, 0,
817 gettext_noop ("Insufficient space to satisfy request and "
818 "requested amount is larger than cache size"));
822 transmit_status (client, 0,
823 gettext_noop ("Insufficient space to satisfy request"));
828 GNUNET_STATISTICS_set (stats,
829 gettext_noop ("# reserved"),
832 e = GNUNET_malloc (sizeof(struct ReservationList));
833 e->next = reservations;
837 e->entries = entries;
838 e->rid = ++reservation_gen;
839 if (reservation_gen < 0)
840 reservation_gen = 0; /* wrap around */
841 transmit_status (client, e->rid, NULL);
846 * Handle RELEASE_RESERVE-message.
849 * @param client identification of the client
850 * @param message the actual message
853 handle_release_reserve (void *cls,
854 struct GNUNET_SERVER_Client *client,
855 const struct GNUNET_MessageHeader *message)
857 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
858 struct ReservationList *pos;
859 struct ReservationList *prev;
860 struct ReservationList *next;
861 int rid = ntohl(msg->rid);
862 unsigned long long rem;
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Processing `%s' request\n",
871 while (NULL != (pos = next))
880 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
881 GNUNET_assert (reserved >= rem);
883 GNUNET_STATISTICS_set (stats,
884 gettext_noop ("# reserved"),
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Returning %llu remaining reserved bytes to storage pool\n",
893 transmit_status (client, GNUNET_OK, NULL);
899 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
904 * Check that the given message is a valid data message.
906 * @return NULL if the message is not well-formed, otherwise the message
908 static const struct DataMessage *
909 check_data (const struct GNUNET_MessageHeader *message)
913 const struct DataMessage *dm;
915 size = ntohs(message->size);
916 if (size < sizeof(struct DataMessage))
921 dm = (const struct DataMessage *) message;
922 dsize = ntohl(dm->size);
923 if (size != dsize + sizeof(struct DataMessage))
933 * Context for a put request used to see if the content is
939 * Client to notify on completion.
941 struct GNUNET_SERVER_Client *client;
944 * Did we find the data already in the database?
948 /* followed by the 'struct DataMessage' */
953 * Actually put the data message.
956 execute_put (struct GNUNET_SERVER_Client *client,
957 const struct DataMessage *dm)
963 size = ntohl(dm->size);
965 ret = plugin->api->put (plugin->api->cls,
971 ntohl(dm->anonymity),
972 0 /* FIXME: replication */,
973 GNUNET_TIME_absolute_ntoh(dm->expiration),
975 if (GNUNET_OK == ret)
977 GNUNET_STATISTICS_update (stats,
978 gettext_noop ("# bytes stored"),
981 GNUNET_CONTAINER_bloomfilter_add (filter,
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Successfully stored %u bytes of type %u under key `%s'\n",
988 GNUNET_h2s (&dm->key));
991 transmit_status (client,
994 GNUNET_free_non_null (msg);
995 if (quota - reserved - cache_size < payload)
997 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
998 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
999 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
1000 (unsigned long long) (quota - reserved - cache_size),
1001 (unsigned long long) payload);
1002 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1008 * Function that will check if the given datastore entry
1009 * matches the put and if none match executes the put.
1011 * @param cls closure, pointer to the client (of type 'struct PutContext').
1012 * @param next_cls closure to use to ask for the next item
1013 * @param key key for the content
1014 * @param size number of bytes in data
1015 * @param data content stored
1016 * @param type type of the content
1017 * @param priority priority of the content
1018 * @param anonymity anonymity-level for the content
1019 * @param expiration expiration time for the content
1020 * @param uid unique identifier for the datum;
1021 * maybe 0 if no unique identifier is available
1023 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
1024 * GNUNET_NO to delete the item and continue (if supported)
1027 check_present (void *cls,
1029 const GNUNET_HashCode * key,
1032 enum GNUNET_BLOCK_Type type,
1035 struct GNUNET_TIME_Absolute
1036 expiration, uint64_t uid)
1038 struct PutContext *pc = cls;
1039 const struct DataMessage *dm;
1041 dm = (const struct DataMessage*) &pc[1];
1044 if (pc->is_present == GNUNET_YES)
1045 transmit_status (pc->client, GNUNET_NO, NULL);
1047 execute_put (pc->client, dm);
1048 GNUNET_SERVER_client_drop (pc->client);
1050 return GNUNET_SYSERR;
1052 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
1053 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
1054 ( (size == ntohl(dm->size)) &&
1055 (0 == memcmp (&dm[1],
1059 pc->is_present = GNUNET_YES;
1060 plugin->api->next_request (next_cls, GNUNET_YES);
1064 plugin->api->next_request (next_cls, GNUNET_NO);
1071 * Handle PUT-message.
1073 * @param cls closure
1074 * @param client identification of the client
1075 * @param message the actual message
1078 handle_put (void *cls,
1079 struct GNUNET_SERVER_Client *client,
1080 const struct GNUNET_MessageHeader *message)
1082 const struct DataMessage *dm = check_data (message);
1084 struct ReservationList *pos;
1085 struct PutContext *pc;
1088 if ( (dm == NULL) ||
1089 (ntohl(dm->type) == 0) )
1092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1097 "Processing `%s' request for `%s' of type %u\n",
1099 GNUNET_h2s (&dm->key),
1102 rid = ntohl(dm->rid);
1103 size = ntohl(dm->size);
1107 while ( (NULL != pos) &&
1110 GNUNET_break (pos != NULL);
1113 GNUNET_break (pos->entries > 0);
1114 GNUNET_break (pos->amount >= size);
1116 pos->amount -= size;
1117 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1118 GNUNET_STATISTICS_set (stats,
1119 gettext_noop ("# reserved"),
1124 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1127 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1128 pc->client = client;
1129 GNUNET_SERVER_client_keep (client);
1130 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1131 plugin->api->get (plugin->api->cls,
1139 execute_put (client, dm);
1144 * Handle GET-message.
1146 * @param cls closure
1147 * @param client identification of the client
1148 * @param message the actual message
1151 handle_get (void *cls,
1152 struct GNUNET_SERVER_Client *client,
1153 const struct GNUNET_MessageHeader *message)
1155 const struct GetMessage *msg;
1158 size = ntohs(message->size);
1159 if ( (size != sizeof(struct GetMessage)) &&
1160 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166 msg = (const struct GetMessage*) message;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "Processing `%s' request for `%s' of type %u\n",
1171 GNUNET_h2s (&msg->key),
1174 GNUNET_STATISTICS_update (stats,
1175 gettext_noop ("# GET requests received"),
1178 GNUNET_SERVER_client_keep (client);
1179 if ( (size == sizeof(struct GetMessage)) &&
1180 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1183 /* don't bother database... */
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1188 GNUNET_h2s (&msg->key));
1190 GNUNET_STATISTICS_update (stats,
1191 gettext_noop ("# requests filtered by bloomfilter"),
1194 transmit_item (client,
1195 NULL, NULL, 0, NULL, 0, 0, 0,
1196 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1199 plugin->api->get (plugin->api->cls,
1200 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1209 * Handle UPDATE-message.
1211 * @param cls closure
1212 * @param client identification of the client
1213 * @param message the actual message
1216 handle_update (void *cls,
1217 struct GNUNET_SERVER_Client *client,
1218 const struct GNUNET_MessageHeader *message)
1220 const struct UpdateMessage *msg;
1224 GNUNET_STATISTICS_update (stats,
1225 gettext_noop ("# UPDATE requests received"),
1228 msg = (const struct UpdateMessage*) message;
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Processing `%s' request for %llu\n",
1234 (unsigned long long) GNUNET_ntohll (msg->uid));
1236 ret = plugin->api->update (plugin->api->cls,
1237 GNUNET_ntohll(msg->uid),
1238 (int32_t) ntohl(msg->priority),
1239 GNUNET_TIME_absolute_ntoh(msg->expiration),
1241 transmit_status (client, ret, emsg);
1242 GNUNET_free_non_null (emsg);
1247 * Handle GET_REPLICATION-message.
1249 * @param cls closure
1250 * @param client identification of the client
1251 * @param message the actual message
1254 handle_get_replication (void *cls,
1255 struct GNUNET_SERVER_Client *client,
1256 const struct GNUNET_MessageHeader *message)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "Processing `%s' request\n",
1263 GNUNET_STATISTICS_update (stats,
1264 gettext_noop ("# GET REPLICATION requests received"),
1267 GNUNET_SERVER_client_keep (client);
1268 plugin->api->replication_get (plugin->api->cls,
1275 * Handle GET_ZERO_ANONYMITY-message.
1277 * @param cls closure
1278 * @param client identification of the client
1279 * @param message the actual message
1282 handle_get_zero_anonymity (void *cls,
1283 struct GNUNET_SERVER_Client *client,
1284 const struct GNUNET_MessageHeader *message)
1286 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message;
1287 enum GNUNET_BLOCK_Type type;
1289 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1290 if (type == GNUNET_BLOCK_TYPE_ANY)
1293 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Processing `%s' request\n",
1299 "GET_ZERO_ANONYMITY");
1301 GNUNET_STATISTICS_update (stats,
1302 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1305 GNUNET_SERVER_client_keep (client);
1306 plugin->api->iter_zero_anonymity (plugin->api->cls,
1314 * Context for the 'remove_callback'.
1316 struct RemoveContext
1319 * Client for whom we're doing the remvoing.
1321 struct GNUNET_SERVER_Client *client;
1324 * GNUNET_YES if we managed to remove something.
1331 * Callback function that will cause the item that is passed
1332 * in to be deleted (by returning GNUNET_NO).
1335 remove_callback (void *cls,
1337 const GNUNET_HashCode * key,
1340 enum GNUNET_BLOCK_Type type,
1343 struct GNUNET_TIME_Absolute
1344 expiration, uint64_t uid)
1346 struct RemoveContext *rc = cls;
1349 "remove called with key %p\n",
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "No further matches for `%s' request.\n",
1358 if (GNUNET_YES == rc->found)
1359 transmit_status (rc->client, GNUNET_OK, NULL);
1361 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1362 GNUNET_SERVER_client_drop (rc->client);
1364 return GNUNET_OK; /* last item */
1366 rc->found = GNUNET_YES;
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1370 (unsigned long long) uid,
1375 GNUNET_STATISTICS_update (stats,
1376 gettext_noop ("# bytes removed (explicit request)"),
1379 GNUNET_CONTAINER_bloomfilter_remove (filter,
1381 plugin->api->next_request (next_cls, GNUNET_YES);
1387 * Handle REMOVE-message.
1389 * @param cls closure
1390 * @param client identification of the client
1391 * @param message the actual message
1394 handle_remove (void *cls,
1395 struct GNUNET_SERVER_Client *client,
1396 const struct GNUNET_MessageHeader *message)
1398 const struct DataMessage *dm = check_data (message);
1399 GNUNET_HashCode vhash;
1400 struct RemoveContext *rc;
1405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "Processing `%s' request for `%s' of type %u\n",
1412 GNUNET_h2s (&dm->key),
1415 GNUNET_STATISTICS_update (stats,
1416 gettext_noop ("# REMOVE requests received"),
1419 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1420 GNUNET_SERVER_client_keep (client);
1421 rc->client = client;
1422 GNUNET_CRYPTO_hash (&dm[1],
1426 "remove does get for key %s\n",
1427 GNUNET_h2s (&dm->key));
1429 "remove does get for %u vhash %s\n",
1431 GNUNET_h2s (&vhash));
1432 plugin->api->get (plugin->api->cls,
1435 (enum GNUNET_BLOCK_Type) ntohl(dm->type),
1442 * Handle DROP-message.
1444 * @param cls closure
1445 * @param client identification of the client
1446 * @param message the actual message
1449 handle_drop (void *cls,
1450 struct GNUNET_SERVER_Client *client,
1451 const struct GNUNET_MessageHeader *message)
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Processing `%s' request\n",
1458 plugin->api->drop (plugin->api->cls);
1459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1464 * Function called by plugins to notify us about a
1465 * change in their disk utilization.
1467 * @param cls closure (NULL)
1468 * @param delta change in disk utilization,
1469 * 0 for "reset to empty"
1472 disk_utilization_change_cb (void *cls,
1476 (payload < -delta) )
1478 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1479 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1480 (long long) payload,
1481 (long long) -delta);
1482 payload = plugin->api->get_size (plugin->api->cls);
1488 if (lastSync >= MAX_STAT_SYNC_LAG)
1494 * Callback function to process statistic values.
1496 * @param cls closure (struct Plugin*)
1497 * @param subsystem name of subsystem that created the statistic
1498 * @param name the name of the datum
1499 * @param value the current value
1500 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1501 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1504 process_stat_in (void *cls,
1505 const char *subsystem,
1510 GNUNET_assert (stats_worked == GNUNET_NO);
1511 stats_worked = GNUNET_YES;
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1524 process_stat_done (void *cls,
1527 struct DatastorePlugin *plugin = cls;
1530 if (stats_worked == GNUNET_NO)
1531 payload = plugin->api->get_size (plugin->api->cls);
1536 * Load the datastore plugin.
1538 static struct DatastorePlugin *
1541 struct DatastorePlugin *ret;
1546 GNUNET_CONFIGURATION_get_value_string (cfg,
1547 "DATASTORE", "DATABASE", &name))
1549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1550 _("No `%s' specified for `%s' in configuration!\n"),
1555 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1557 ret->env.duc = &disk_utilization_change_cb;
1558 ret->env.cls = NULL;
1559 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1560 _("Loading `%s' datastore plugin\n"), name);
1561 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1562 ret->short_name = name;
1563 ret->lib_name = libname;
1564 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1565 if (ret->api == NULL)
1567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1568 _("Failed to load datastore plugin for `%s'\n"), name);
1569 GNUNET_free (ret->short_name);
1570 GNUNET_free (libname);
1579 * Function called when the service shuts
1580 * down. Unloads our datastore plugin.
1582 * @param plug plugin to unload
1585 unload_plugin (struct DatastorePlugin *plug)
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Datastore service is unloading plugin...\n");
1591 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1592 GNUNET_free (plug->lib_name);
1593 GNUNET_free (plug->short_name);
1599 * Final task run after shutdown. Unloads plugins and disconnects us from
1603 unload_task (void *cls,
1604 const struct GNUNET_SCHEDULER_TaskContext *tc)
1606 unload_plugin (plugin);
1610 GNUNET_CONTAINER_bloomfilter_free (filter);
1615 if (stat_get != NULL)
1617 GNUNET_STATISTICS_get_cancel (stat_get);
1622 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1629 * Last task run during shutdown. Disconnects us from
1630 * the transport and core.
1633 cleaning_task (void *cls,
1634 const struct GNUNET_SCHEDULER_TaskContext *tc)
1636 struct TransmitCallbackContext *tcc;
1638 cleaning_done = GNUNET_YES;
1639 while (NULL != (tcc = tcc_head))
1641 GNUNET_CONTAINER_DLL_remove (tcc_head,
1644 if (tcc->th != NULL)
1646 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1647 GNUNET_SERVER_client_drop (tcc->client);
1649 if (NULL != tcc->tc)
1650 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1651 GNUNET_free (tcc->msg);
1654 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1656 GNUNET_SCHEDULER_cancel (expired_kill_task);
1657 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1659 GNUNET_SCHEDULER_add_continuation (&unload_task,
1661 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1666 * Function that removes all active reservations made
1667 * by the given client and releases the space for other
1670 * @param cls closure
1671 * @param client identification of the client
1674 cleanup_reservations (void *cls,
1675 struct GNUNET_SERVER_Client *client)
1677 struct ReservationList *pos;
1678 struct ReservationList *prev;
1679 struct ReservationList *next;
1688 if (pos->client == client)
1691 reservations = next;
1694 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1703 GNUNET_STATISTICS_set (stats,
1704 gettext_noop ("# reserved"),
1711 * Process datastore requests.
1713 * @param cls closure
1714 * @param server the initialized server
1715 * @param c configuration to use
1719 struct GNUNET_SERVER_Handle *server,
1720 const struct GNUNET_CONFIGURATION_Handle *c)
1722 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1723 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1724 sizeof(struct ReserveMessage) },
1725 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1726 sizeof(struct ReleaseReserveMessage) },
1727 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1728 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1729 sizeof (struct UpdateMessage) },
1730 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1731 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1732 sizeof(struct GNUNET_MessageHeader) },
1733 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1734 sizeof(struct GetZeroAnonymityMessage) },
1735 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1736 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1737 sizeof(struct GNUNET_MessageHeader) },
1741 unsigned int bf_size;
1745 GNUNET_CONFIGURATION_get_value_number (cfg,
1746 "DATASTORE", "QUOTA", "a))
1748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1749 _("No `%s' specified for `%s' in configuration!\n"),
1754 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1755 GNUNET_STATISTICS_set (stats,
1756 gettext_noop ("# quota"),
1759 cache_size = quota / 8; /* Or should we make this an option? */
1760 GNUNET_STATISTICS_set (stats,
1761 gettext_noop ("# cache size"),
1764 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1767 GNUNET_CONFIGURATION_get_value_filename (cfg,
1772 GNUNET_DISK_directory_create_for_file (fn)) )
1774 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1775 _("Could not use specified filename `%s' for bloomfilter.\n"),
1776 fn != NULL ? fn : "");
1777 GNUNET_free_non_null (fn);
1780 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1781 GNUNET_free_non_null (fn);
1784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1785 _("Failed to initialize bloomfilter.\n"));
1788 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1793 plugin = load_plugin ();
1796 GNUNET_CONTAINER_bloomfilter_free (filter);
1800 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1805 stat_get = GNUNET_STATISTICS_get (stats,
1808 GNUNET_TIME_UNIT_SECONDS,
1812 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1813 GNUNET_SERVER_add_handlers (server, handlers);
1815 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1816 &delete_expired, NULL);
1817 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1818 &cleaning_task, NULL);
1823 * The main function for the datastore service.
1825 * @param argc number of arguments from the command line
1826 * @param argv command line arguments
1827 * @return 0 ok, 1 on error
1830 main (int argc, char *const *argv)
1835 GNUNET_SERVICE_run (argc,
1838 GNUNET_SERVICE_OPTION_NONE,
1839 &run, NULL)) ? 0 : 1;
1844 /* end of gnunet-service-datastore.c */