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 "plugin_datastore.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)
45 #define QUOTA_STAT_NAME gettext_noop ("# bytes used in file-sharing datastore")
48 * After how many payload-changing operations
49 * do we sync our statistics?
51 #define MAX_STAT_SYNC_LAG 50
55 * Our datastore plugin.
57 struct DatastorePlugin
61 * API of the transport as returned by the plugin's
62 * initialization function.
64 struct GNUNET_DATASTORE_PluginFunctions *api;
67 * Short name for the plugin (i.e. "sqlite").
72 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
77 * Environment this transport service is using
80 struct GNUNET_DATASTORE_PluginEnvironment env;
86 * Linked list of active reservations.
88 struct ReservationList
92 * This is a linked list.
94 struct ReservationList *next;
97 * Client that made the reservation.
99 struct GNUNET_SERVER_Client *client;
102 * Number of bytes (still) reserved.
107 * Number of items (still) reserved.
112 * Reservation identifier.
121 * Our datastore plugin (NULL if not available).
123 static struct DatastorePlugin *plugin;
126 * Linked list of space reservations made by clients.
128 static struct ReservationList *reservations;
131 * Bloomfilter to quickly tell if we don't have the content.
133 static struct GNUNET_CONTAINER_BloomFilter *filter;
136 * How much space are we allowed to use?
138 static unsigned long long quota;
141 * How much space are we using for the cache? (space available for
142 * insertions that will be instantly reclaimed by discarding less
143 * important content --- or possibly whatever we just inserted into
146 static unsigned long long cache_size;
149 * How much space have we currently reserved?
151 static unsigned long long reserved;
154 * How much data are we currently storing
157 static unsigned long long payload;
160 * Number of updates that were made to the
161 * payload value since we last synchronized
162 * it with the statistics service.
164 static unsigned int lastSync;
167 * Did we get an answer from statistics?
169 static int stats_worked;
172 * Identity of the task that is used to delete
175 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
180 const struct GNUNET_CONFIGURATION_Handle *cfg;
185 struct GNUNET_SCHEDULER_Handle *sched;
188 * Handle for reporting statistics.
190 static struct GNUNET_STATISTICS_Handle *stats;
194 * Synchronize our utilization statistics with the
195 * statistics service.
200 GNUNET_STATISTICS_set (stats,
211 * Function called once the transmit operation has
212 * either failed or succeeded.
215 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
217 typedef void (*TransmitContinuation)(void *cls,
222 * Context for transmitting replies to clients.
224 struct TransmitCallbackContext
228 * We keep these in a doubly-linked list (for cleanup).
230 struct TransmitCallbackContext *next;
233 * We keep these in a doubly-linked list (for cleanup).
235 struct TransmitCallbackContext *prev;
238 * The message that we're asked to transmit.
240 struct GNUNET_MessageHeader *msg;
243 * Handle for the transmission request.
245 struct GNUNET_CONNECTION_TransmitHandle *th;
248 * Client that we are transmitting to.
250 struct GNUNET_SERVER_Client *client;
253 * Function to call once msg has been transmitted
254 * (or at least added to the buffer).
256 TransmitContinuation tc;
264 * GNUNET_YES if we are supposed to signal the server
265 * completion of the client's request.
272 * Head of the doubly-linked list (for cleanup).
274 static struct TransmitCallbackContext *tcc_head;
277 * Tail of the doubly-linked list (for cleanup).
279 static struct TransmitCallbackContext *tcc_tail;
282 * Have we already cleaned up the TCCs and are hence no longer
283 * willing (or able) to transmit anything to anyone?
285 static int cleaning_done;
288 * Handle for pending get request.
290 static struct GNUNET_STATISTICS_GetHandle *stat_get;
294 * Task that is used to remove expired entries from
295 * the datastore. This task will schedule itself
296 * again automatically to always delete all expired
299 * @param cls not used
300 * @param tc task context
303 delete_expired (void *cls,
304 const struct GNUNET_SCHEDULER_TaskContext *tc);
308 * Iterate over the expired items stored in the datastore.
309 * Delete all expired items; once we have processed all
310 * expired items, re-schedule the "delete_expired" task.
312 * @param cls not used
313 * @param next_cls closure to pass to the "next" function.
314 * @param key key for the content
315 * @param size number of bytes in data
316 * @param data content stored
317 * @param type type of the content
318 * @param priority priority of the content
319 * @param anonymity anonymity-level for the content
320 * @param expiration expiration time for the content
321 * @param uid unique identifier for the datum;
322 * maybe 0 if no unique identifier is available
324 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
325 * (continue on call to "next", of course),
326 * GNUNET_NO to delete the item and continue (if supported)
329 expired_processor (void *cls,
331 const GNUNET_HashCode * key,
334 enum GNUNET_BLOCK_Type type,
337 struct GNUNET_TIME_Absolute
341 struct GNUNET_TIME_Absolute now;
346 = GNUNET_SCHEDULER_add_delayed (sched,
350 return GNUNET_SYSERR;
352 now = GNUNET_TIME_absolute_get ();
353 if (expiration.value > now.value)
355 /* finished processing */
356 plugin->api->next_request (next_cls, GNUNET_YES);
357 return GNUNET_SYSERR;
359 plugin->api->next_request (next_cls, GNUNET_NO);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Deleting content `%s' of type %u that expired %llu ms ago\n",
365 (unsigned long long) (now.value - expiration.value));
367 GNUNET_STATISTICS_update (stats,
368 gettext_noop ("# bytes expired"),
371 GNUNET_CONTAINER_bloomfilter_remove (filter,
373 return GNUNET_NO; /* delete */
378 * Task that is used to remove expired entries from
379 * the datastore. This task will schedule itself
380 * again automatically to always delete all expired
383 * @param cls not used
384 * @param tc task context
387 delete_expired (void *cls,
388 const struct GNUNET_SCHEDULER_TaskContext *tc)
390 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
391 plugin->api->iter_ascending_expiration (plugin->api->cls,
399 * An iterator over a set of items stored in the datastore.
402 * @param next_cls closure to pass to the "next" function.
403 * @param key key for the content
404 * @param size number of bytes in data
405 * @param data content stored
406 * @param type type of the content
407 * @param priority priority of the content
408 * @param anonymity anonymity-level for the content
409 * @param expiration expiration time for the content
410 * @param uid unique identifier for the datum;
411 * maybe 0 if no unique identifier is available
413 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
414 * (continue on call to "next", of course),
415 * GNUNET_NO to delete the item and continue (if supported)
420 const GNUNET_HashCode * key,
423 enum GNUNET_BLOCK_Type type,
426 struct GNUNET_TIME_Absolute
430 unsigned long long *need = cls;
435 return GNUNET_SYSERR;
437 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
440 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
441 plugin->api->next_request (next_cls,
442 (0 == *need) ? GNUNET_YES : GNUNET_NO);
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
446 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
451 GNUNET_STATISTICS_update (stats,
452 gettext_noop ("# bytes purged (low-priority)"),
455 GNUNET_CONTAINER_bloomfilter_remove (filter,
462 * Manage available disk space by running tasks
463 * that will discard content if necessary. This
464 * function will be run whenever a request for
465 * "need" bytes of storage could only be satisfied
466 * by eating into the "cache" (and we want our cache
469 * @param need number of bytes of content that were
470 * placed into the "cache" (and hence the
471 * number of bytes that should be removed).
474 manage_space (unsigned long long need)
476 unsigned long long *n;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Asked to free up %llu bytes of cache space\n",
483 n = GNUNET_malloc (sizeof(unsigned long long));
485 plugin->api->iter_low_priority (plugin->api->cls,
493 * Function called to notify a client about the socket
494 * begin ready to queue more data. "buf" will be
495 * NULL and "size" zero if the socket was closed for
496 * writing in the meantime.
499 * @param size number of bytes available in buf
500 * @param buf where the callee should write the message
501 * @return number of bytes written to buf
504 transmit_callback (void *cls,
505 size_t size, void *buf)
507 struct TransmitCallbackContext *tcc = cls;
511 GNUNET_CONTAINER_DLL_remove (tcc_head,
514 msize = ntohs(tcc->msg->size);
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519 "Transmission failed.\n");
522 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
523 if (GNUNET_YES == tcc->end)
524 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
525 GNUNET_SERVER_client_drop (tcc->client);
526 GNUNET_free (tcc->msg);
530 GNUNET_assert (size >= msize);
531 memcpy (buf, tcc->msg, msize);
533 tcc->tc (tcc->tc_cls, GNUNET_OK);
534 if (GNUNET_YES == tcc->end)
536 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Response transmitted, more pending!\n");
545 GNUNET_SERVER_client_drop (tcc->client);
546 GNUNET_free (tcc->msg);
553 * Transmit the given message to the client.
555 * @param client target of the message
556 * @param msg message to transmit, will be freed!
557 * @param tc function to call afterwards
558 * @param tc_cls closure for tc
559 * @param end is this the last response (and we should
560 * signal the server completion accodingly after
561 * transmitting this message)?
564 transmit (struct GNUNET_SERVER_Client *client,
565 struct GNUNET_MessageHeader *msg,
566 TransmitContinuation tc,
570 struct TransmitCallbackContext *tcc;
572 if (GNUNET_YES == cleaning_done)
575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
576 "Shutdown in progress, aborting transmission.\n");
580 tc (tc_cls, GNUNET_SYSERR);
583 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
585 tcc->client = client;
587 tcc->tc_cls = tc_cls;
590 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
592 GNUNET_TIME_UNIT_FOREVER_REL,
597 if (GNUNET_YES == end)
600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
601 "Disconnecting client.\n");
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606 tc (tc_cls, GNUNET_SYSERR);
611 GNUNET_SERVER_client_keep (client);
612 GNUNET_CONTAINER_DLL_insert (tcc_head,
619 * Transmit a status code to the client.
621 * @param client receiver of the response
622 * @param code status code
623 * @param msg optional error message (can be NULL)
626 transmit_status (struct GNUNET_SERVER_Client *client,
630 struct StatusMessage *sm;
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Transmitting `%s' message with value %d and message `%s'\n",
638 msg != NULL ? msg : "(none)");
640 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
641 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
642 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
643 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
644 sm->status = htonl(code);
646 memcpy (&sm[1], msg, slen);
647 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
652 * Function called once the transmit operation has
653 * either failed or succeeded.
655 * @param next_cls closure for calling "next_request" callback
656 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
659 get_next(void *next_cls,
662 if (status != GNUNET_OK)
664 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
665 _("Failed to transmit an item to the client; aborting iteration.\n"));
667 plugin->api->next_request (next_cls, GNUNET_YES);
670 plugin->api->next_request (next_cls, GNUNET_NO);
675 * Function that will transmit the given datastore entry
678 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
679 * @param next_cls closure to use to ask for the next item
680 * @param key key for the content
681 * @param size number of bytes in data
682 * @param data content stored
683 * @param type type of the content
684 * @param priority priority of the content
685 * @param anonymity anonymity-level for the content
686 * @param expiration expiration time for the content
687 * @param uid unique identifier for the datum;
688 * maybe 0 if no unique identifier is available
690 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
691 * GNUNET_NO to delete the item and continue (if supported)
694 transmit_item (void *cls,
696 const GNUNET_HashCode * key,
699 enum GNUNET_BLOCK_Type type,
702 struct GNUNET_TIME_Absolute
703 expiration, uint64_t uid)
705 struct GNUNET_SERVER_Client *client = cls;
706 struct GNUNET_MessageHeader *end;
707 struct DataMessage *dm;
711 /* transmit 'DATA_END' */
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Transmitting `%s' message\n",
717 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
718 end->size = htons(sizeof(struct GNUNET_MessageHeader));
719 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
720 transmit (client, end, NULL, NULL, GNUNET_YES);
721 GNUNET_SERVER_client_drop (client);
724 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
725 dm->header.size = htons(sizeof(struct DataMessage) + size);
726 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
728 dm->size = htonl(size);
729 dm->type = htonl(type);
730 dm->priority = htonl(priority);
731 dm->anonymity = htonl(anonymity);
732 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
733 dm->uid = GNUNET_htonll(uid);
735 memcpy (&dm[1], data, size);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Transmitting `%s' message for `%s' of type %u\n",
743 GNUNET_STATISTICS_update (stats,
744 gettext_noop ("# results found"),
747 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
753 * Handle RESERVE-message.
756 * @param client identification of the client
757 * @param message the actual message
760 handle_reserve (void *cls,
761 struct GNUNET_SERVER_Client *client,
762 const struct GNUNET_MessageHeader *message)
765 * Static counter to produce reservation identifiers.
767 static int reservation_gen;
769 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
770 struct ReservationList *e;
771 unsigned long long used;
772 unsigned long long req;
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Processing `%s' request\n",
781 amount = GNUNET_ntohll(msg->amount);
782 entries = ntohl(msg->entries);
783 used = payload + reserved;
784 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
785 if (used + req > quota)
788 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
790 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
794 if (cache_size < req)
796 /* TODO: document this in the FAQ; essentially, if this
797 message happens, the insertion request could be blocked
798 by less-important content from migration because it is
799 larger than 1/8th of the overall available space, and
800 we only reserve 1/8th for "fresh" insertions */
801 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
802 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
805 transmit_status (client, 0,
806 gettext_noop ("Insufficient space to satisfy request and "
807 "requested amount is larger than cache size"));
811 transmit_status (client, 0,
812 gettext_noop ("Insufficient space to satisfy request"));
817 GNUNET_STATISTICS_set (stats,
818 gettext_noop ("# reserved"),
821 e = GNUNET_malloc (sizeof(struct ReservationList));
822 e->next = reservations;
826 e->entries = entries;
827 e->rid = ++reservation_gen;
828 if (reservation_gen < 0)
829 reservation_gen = 0; /* wrap around */
830 transmit_status (client, e->rid, NULL);
835 * Handle RELEASE_RESERVE-message.
838 * @param client identification of the client
839 * @param message the actual message
842 handle_release_reserve (void *cls,
843 struct GNUNET_SERVER_Client *client,
844 const struct GNUNET_MessageHeader *message)
846 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
847 struct ReservationList *pos;
848 struct ReservationList *prev;
849 struct ReservationList *next;
850 int rid = ntohl(msg->rid);
851 unsigned long long rem;
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Processing `%s' request\n",
860 while (NULL != (pos = next))
869 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
870 GNUNET_assert (reserved >= rem);
872 GNUNET_STATISTICS_set (stats,
873 gettext_noop ("# reserved"),
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878 "Returning %llu remaining reserved bytes to storage pool\n",
882 transmit_status (client, GNUNET_OK, NULL);
888 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
893 * Check that the given message is a valid data message.
895 * @return NULL if the message is not well-formed, otherwise the message
897 static const struct DataMessage *
898 check_data (const struct GNUNET_MessageHeader *message)
902 const struct DataMessage *dm;
904 size = ntohs(message->size);
905 if (size < sizeof(struct DataMessage))
910 dm = (const struct DataMessage *) message;
911 dsize = ntohl(dm->size);
912 if (size != dsize + sizeof(struct DataMessage))
922 * Context for a put request used to see if the content is
928 * Client to notify on completion.
930 struct GNUNET_SERVER_Client *client;
933 * Did we find the data already in the database?
937 /* followed by the 'struct DataMessage' */
942 * Actually put the data message.
945 execute_put (struct GNUNET_SERVER_Client *client,
946 const struct DataMessage *dm)
952 size = ntohl(dm->size);
954 ret = plugin->api->put (plugin->api->cls,
960 ntohl(dm->anonymity),
961 GNUNET_TIME_absolute_ntoh(dm->expiration),
963 if (GNUNET_OK == ret)
965 GNUNET_STATISTICS_update (stats,
966 gettext_noop ("# bytes stored"),
969 GNUNET_CONTAINER_bloomfilter_add (filter,
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Successfully stored %u bytes of type %u under key `%s'\n",
976 GNUNET_h2s (&dm->key));
979 transmit_status (client,
980 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
982 GNUNET_free_non_null (msg);
983 if (quota - reserved - cache_size < payload)
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
986 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
987 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
988 (unsigned long long) (quota - reserved - cache_size),
989 (unsigned long long) payload);
990 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
997 * Function that will check if the given datastore entry
998 * matches the put and if none match executes the put.
1000 * @param cls closure, pointer to the client (of type 'struct PutContext').
1001 * @param next_cls closure to use to ask for the next item
1002 * @param key key for the content
1003 * @param size number of bytes in data
1004 * @param data content stored
1005 * @param type type of the content
1006 * @param priority priority of the content
1007 * @param anonymity anonymity-level for the content
1008 * @param expiration expiration time for the content
1009 * @param uid unique identifier for the datum;
1010 * maybe 0 if no unique identifier is available
1012 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
1013 * GNUNET_NO to delete the item and continue (if supported)
1016 check_present (void *cls,
1018 const GNUNET_HashCode * key,
1021 enum GNUNET_BLOCK_Type type,
1024 struct GNUNET_TIME_Absolute
1025 expiration, uint64_t uid)
1027 struct PutContext *pc = cls;
1028 const struct DataMessage *dm;
1030 dm = (const struct DataMessage*) &pc[1];
1033 if (pc->is_present == GNUNET_YES)
1034 transmit_status (pc->client, GNUNET_OK, NULL);
1036 execute_put (pc->client, dm);
1037 GNUNET_SERVER_client_drop (pc->client);
1039 return GNUNET_SYSERR;
1041 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
1042 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
1043 ( (size == ntohl(dm->size)) &&
1044 (0 == memcmp (&dm[1],
1048 pc->is_present = GNUNET_YES;
1049 plugin->api->next_request (next_cls, GNUNET_YES);
1053 plugin->api->next_request (next_cls, GNUNET_NO);
1060 * Handle PUT-message.
1062 * @param cls closure
1063 * @param client identification of the client
1064 * @param message the actual message
1067 handle_put (void *cls,
1068 struct GNUNET_SERVER_Client *client,
1069 const struct GNUNET_MessageHeader *message)
1071 const struct DataMessage *dm = check_data (message);
1073 struct ReservationList *pos;
1074 struct PutContext *pc;
1077 if ( (dm == NULL) ||
1078 (ntohl(dm->type) == 0) )
1081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Processing `%s' request for `%s' of type %u\n",
1088 GNUNET_h2s (&dm->key),
1091 rid = ntohl(dm->rid);
1092 size = ntohl(dm->size);
1096 while ( (NULL != pos) &&
1099 GNUNET_break (pos != NULL);
1102 GNUNET_break (pos->entries > 0);
1103 GNUNET_break (pos->amount >= size);
1105 pos->amount -= size;
1106 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1107 GNUNET_STATISTICS_set (stats,
1108 gettext_noop ("# reserved"),
1113 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1116 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1117 pc->client = client;
1118 GNUNET_SERVER_client_keep (client);
1119 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1120 plugin->api->get (plugin->api->cls,
1128 execute_put (client, dm);
1133 * Handle GET-message.
1135 * @param cls closure
1136 * @param client identification of the client
1137 * @param message the actual message
1140 handle_get (void *cls,
1141 struct GNUNET_SERVER_Client *client,
1142 const struct GNUNET_MessageHeader *message)
1144 const struct GetMessage *msg;
1147 size = ntohs(message->size);
1148 if ( (size != sizeof(struct GetMessage)) &&
1149 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1155 msg = (const struct GetMessage*) message;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1158 "Processing `%s' request for `%s' of type %u\n",
1160 GNUNET_h2s (&msg->key),
1163 GNUNET_STATISTICS_update (stats,
1164 gettext_noop ("# GET requests received"),
1167 GNUNET_SERVER_client_keep (client);
1168 if ( (size == sizeof(struct GetMessage)) &&
1169 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1172 /* don't bother database... */
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1177 GNUNET_h2s (&msg->key));
1179 GNUNET_STATISTICS_update (stats,
1180 gettext_noop ("# requests filtered by bloomfilter"),
1183 transmit_item (client,
1184 NULL, NULL, 0, NULL, 0, 0, 0,
1185 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1188 plugin->api->get (plugin->api->cls,
1189 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1198 * Handle UPDATE-message.
1200 * @param cls closure
1201 * @param client identification of the client
1202 * @param message the actual message
1205 handle_update (void *cls,
1206 struct GNUNET_SERVER_Client *client,
1207 const struct GNUNET_MessageHeader *message)
1209 const struct UpdateMessage *msg;
1213 GNUNET_STATISTICS_update (stats,
1214 gettext_noop ("# UPDATE requests received"),
1217 msg = (const struct UpdateMessage*) message;
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 "Processing `%s' request for %llu\n",
1223 (unsigned long long) GNUNET_ntohll (msg->uid));
1225 ret = plugin->api->update (plugin->api->cls,
1226 GNUNET_ntohll(msg->uid),
1227 (int32_t) ntohl(msg->priority),
1228 GNUNET_TIME_absolute_ntoh(msg->expiration),
1230 transmit_status (client, ret, emsg);
1231 GNUNET_free_non_null (emsg);
1236 * Handle GET_RANDOM-message.
1238 * @param cls closure
1239 * @param client identification of the client
1240 * @param message the actual message
1243 handle_get_random (void *cls,
1244 struct GNUNET_SERVER_Client *client,
1245 const struct GNUNET_MessageHeader *message)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Processing `%s' request\n",
1252 GNUNET_STATISTICS_update (stats,
1253 gettext_noop ("# GET RANDOM requests received"),
1256 GNUNET_SERVER_client_keep (client);
1257 plugin->api->iter_migration_order (plugin->api->cls,
1265 * Context for the 'remove_callback'.
1267 struct RemoveContext
1270 * Client for whom we're doing the remvoing.
1272 struct GNUNET_SERVER_Client *client;
1275 * GNUNET_YES if we managed to remove something.
1282 * Callback function that will cause the item that is passed
1283 * in to be deleted (by returning GNUNET_NO).
1286 remove_callback (void *cls,
1288 const GNUNET_HashCode * key,
1291 enum GNUNET_BLOCK_Type type,
1294 struct GNUNET_TIME_Absolute
1295 expiration, uint64_t uid)
1297 struct RemoveContext *rc = cls;
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "No further matches for `%s' request.\n",
1306 if (GNUNET_YES == rc->found)
1307 transmit_status (rc->client, GNUNET_OK, NULL);
1309 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1310 GNUNET_SERVER_client_drop (rc->client);
1312 return GNUNET_OK; /* last item */
1314 rc->found = GNUNET_YES;
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1318 (unsigned long long) uid,
1323 GNUNET_STATISTICS_update (stats,
1324 gettext_noop ("# bytes removed (explicit request)"),
1327 GNUNET_CONTAINER_bloomfilter_remove (filter,
1329 plugin->api->next_request (next_cls, GNUNET_YES);
1335 * Handle REMOVE-message.
1337 * @param cls closure
1338 * @param client identification of the client
1339 * @param message the actual message
1342 handle_remove (void *cls,
1343 struct GNUNET_SERVER_Client *client,
1344 const struct GNUNET_MessageHeader *message)
1346 const struct DataMessage *dm = check_data (message);
1347 GNUNET_HashCode vhash;
1348 struct RemoveContext *rc;
1353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "Processing `%s' request for `%s' of type %u\n",
1360 GNUNET_h2s (&dm->key),
1363 GNUNET_STATISTICS_update (stats,
1364 gettext_noop ("# REMOVE requests received"),
1367 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1368 GNUNET_SERVER_client_keep (client);
1369 rc->client = client;
1370 GNUNET_CRYPTO_hash (&dm[1],
1373 plugin->api->get (plugin->api->cls,
1383 * Handle DROP-message.
1385 * @param cls closure
1386 * @param client identification of the client
1387 * @param message the actual message
1390 handle_drop (void *cls,
1391 struct GNUNET_SERVER_Client *client,
1392 const struct GNUNET_MessageHeader *message)
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Processing `%s' request\n",
1399 plugin->api->drop (plugin->api->cls);
1400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1405 * Function called by plugins to notify us about a
1406 * change in their disk utilization.
1408 * @param cls closure (NULL)
1409 * @param delta change in disk utilization,
1410 * 0 for "reset to empty"
1413 disk_utilization_change_cb (void *cls,
1417 (payload < -delta) )
1419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1420 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1421 (long long) payload,
1422 (long long) -delta);
1423 payload = plugin->api->get_size (plugin->api->cls);
1429 if (lastSync >= MAX_STAT_SYNC_LAG)
1435 * Callback function to process statistic values.
1437 * @param cls closure (struct Plugin*)
1438 * @param subsystem name of subsystem that created the statistic
1439 * @param name the name of the datum
1440 * @param value the current value
1441 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1442 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1445 process_stat_in (void *cls,
1446 const char *subsystem,
1451 GNUNET_assert (stats_worked == GNUNET_NO);
1452 stats_worked = GNUNET_YES;
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1465 process_stat_done (void *cls,
1468 struct DatastorePlugin *plugin = cls;
1471 if (stats_worked == GNUNET_NO)
1472 payload = plugin->api->get_size (plugin->api->cls);
1477 * Load the datastore plugin.
1479 static struct DatastorePlugin *
1482 struct DatastorePlugin *ret;
1487 GNUNET_CONFIGURATION_get_value_string (cfg,
1488 "DATASTORE", "DATABASE", &name))
1490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1491 _("No `%s' specified for `%s' in configuration!\n"),
1496 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1498 ret->env.sched = sched;
1499 ret->env.duc = &disk_utilization_change_cb;
1500 ret->env.cls = NULL;
1501 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1502 _("Loading `%s' datastore plugin\n"), name);
1503 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1504 ret->short_name = name;
1505 ret->lib_name = libname;
1506 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1507 if (ret->api == NULL)
1509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1510 _("Failed to load datastore plugin for `%s'\n"), name);
1511 GNUNET_free (ret->short_name);
1512 GNUNET_free (libname);
1521 * Function called when the service shuts
1522 * down. Unloads our datastore plugin.
1524 * @param plug plugin to unload
1527 unload_plugin (struct DatastorePlugin *plug)
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "Datastore service is unloading plugin...\n");
1533 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1534 GNUNET_free (plug->lib_name);
1535 GNUNET_free (plug->short_name);
1541 * Final task run after shutdown. Unloads plugins and disconnects us from
1545 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 unload_plugin (plugin);
1551 GNUNET_CONTAINER_bloomfilter_free (filter);
1556 if (stat_get != NULL)
1558 GNUNET_STATISTICS_get_cancel (stat_get);
1563 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1570 * Last task run during shutdown. Disconnects us from
1571 * the transport and core.
1574 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1576 struct TransmitCallbackContext *tcc;
1578 cleaning_done = GNUNET_YES;
1579 while (NULL != (tcc = tcc_head))
1581 GNUNET_CONTAINER_DLL_remove (tcc_head,
1584 if (tcc->th != NULL)
1586 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1587 GNUNET_SERVER_client_drop (tcc->client);
1589 if (NULL != tcc->tc)
1590 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1591 GNUNET_free (tcc->msg);
1594 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1596 GNUNET_SCHEDULER_cancel (sched,
1598 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1600 GNUNET_SCHEDULER_add_continuation (sched,
1603 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1608 * Function that removes all active reservations made
1609 * by the given client and releases the space for other
1612 * @param cls closure
1613 * @param client identification of the client
1616 cleanup_reservations (void *cls,
1617 struct GNUNET_SERVER_Client
1620 struct ReservationList *pos;
1621 struct ReservationList *prev;
1622 struct ReservationList *next;
1631 if (pos->client == client)
1634 reservations = next;
1637 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1646 GNUNET_STATISTICS_set (stats,
1647 gettext_noop ("# reserved"),
1654 * Process datastore requests.
1656 * @param cls closure
1657 * @param s scheduler to use
1658 * @param server the initialized server
1659 * @param c configuration to use
1663 struct GNUNET_SCHEDULER_Handle *s,
1664 struct GNUNET_SERVER_Handle *server,
1665 const struct GNUNET_CONFIGURATION_Handle *c)
1667 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1668 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1669 sizeof(struct ReserveMessage) },
1670 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1671 sizeof(struct ReleaseReserveMessage) },
1672 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1673 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1674 sizeof (struct UpdateMessage) },
1675 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1676 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1677 sizeof(struct GNUNET_MessageHeader) },
1678 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1679 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1680 sizeof(struct GNUNET_MessageHeader) },
1684 unsigned int bf_size;
1689 GNUNET_CONFIGURATION_get_value_number (cfg,
1690 "DATASTORE", "QUOTA", "a))
1692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1693 _("No `%s' specified for `%s' in configuration!\n"),
1698 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1699 GNUNET_STATISTICS_set (stats,
1700 gettext_noop ("# quota"),
1703 cache_size = quota / 8; /* Or should we make this an option? */
1704 GNUNET_STATISTICS_set (stats,
1705 gettext_noop ("# cache size"),
1708 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1711 GNUNET_CONFIGURATION_get_value_filename (cfg,
1716 GNUNET_DISK_directory_create_for_file (fn)) )
1718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1719 _("Could not use specified filename `%s' for bloomfilter.\n"),
1720 fn != NULL ? fn : "");
1721 GNUNET_free_non_null (fn);
1724 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1725 GNUNET_free_non_null (fn);
1728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1729 _("Failed to initialize bloomfilter.\n"));
1732 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1737 plugin = load_plugin ();
1740 GNUNET_CONTAINER_bloomfilter_free (filter);
1744 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1749 stat_get = GNUNET_STATISTICS_get (stats,
1752 GNUNET_TIME_UNIT_SECONDS,
1756 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1757 GNUNET_SERVER_add_handlers (server, handlers);
1759 = GNUNET_SCHEDULER_add_with_priority (sched,
1760 GNUNET_SCHEDULER_PRIORITY_IDLE,
1761 &delete_expired, NULL);
1762 GNUNET_SCHEDULER_add_delayed (sched,
1763 GNUNET_TIME_UNIT_FOREVER_REL,
1764 &cleaning_task, NULL);
1769 * The main function for the datastore service.
1771 * @param argc number of arguments from the command line
1772 * @param argv command line arguments
1773 * @return 0 ok, 1 on error
1776 main (int argc, char *const *argv)
1781 GNUNET_SERVICE_run (argc,
1784 GNUNET_SERVICE_OPTION_NONE,
1785 &run, NULL)) ? 0 : 1;
1790 /* end of gnunet-service-datastore.c */