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,
213 * Context for transmitting replies to clients.
215 struct TransmitCallbackContext
219 * We keep these in a doubly-linked list (for cleanup).
221 struct TransmitCallbackContext *next;
224 * We keep these in a doubly-linked list (for cleanup).
226 struct TransmitCallbackContext *prev;
229 * The message that we're asked to transmit.
231 struct GNUNET_MessageHeader *msg;
234 * Handle for the transmission request.
236 struct GNUNET_CONNECTION_TransmitHandle *th;
239 * Client that we are transmitting to.
241 struct GNUNET_SERVER_Client *client;
247 * Head of the doubly-linked list (for cleanup).
249 static struct TransmitCallbackContext *tcc_head;
252 * Tail of the doubly-linked list (for cleanup).
254 static struct TransmitCallbackContext *tcc_tail;
257 * Have we already cleaned up the TCCs and are hence no longer
258 * willing (or able) to transmit anything to anyone?
260 static int cleaning_done;
263 * Handle for pending get request.
265 static struct GNUNET_STATISTICS_GetHandle *stat_get;
269 * Task that is used to remove expired entries from
270 * the datastore. This task will schedule itself
271 * again automatically to always delete all expired
274 * @param cls not used
275 * @param tc task context
278 delete_expired (void *cls,
279 const struct GNUNET_SCHEDULER_TaskContext *tc);
283 * Iterate over the expired items stored in the datastore.
284 * Delete all expired items; once we have processed all
285 * expired items, re-schedule the "delete_expired" task.
287 * @param cls not used
288 * @param next_cls closure to pass to the "next" function.
289 * @param key key for the content
290 * @param size number of bytes in data
291 * @param data content stored
292 * @param type type of the content
293 * @param priority priority of the content
294 * @param anonymity anonymity-level for the content
295 * @param expiration expiration time for the content
296 * @param uid unique identifier for the datum;
297 * maybe 0 if no unique identifier is available
299 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
300 * (continue on call to "next", of course),
301 * GNUNET_NO to delete the item and continue (if supported)
304 expired_processor (void *cls,
305 const GNUNET_HashCode * key,
308 enum GNUNET_BLOCK_Type type,
311 struct GNUNET_TIME_Absolute
315 struct GNUNET_TIME_Absolute now;
320 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
323 return GNUNET_SYSERR;
325 now = GNUNET_TIME_absolute_get ();
326 if (expiration.abs_value > now.abs_value)
328 /* finished processing */
330 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
333 return GNUNET_SYSERR;
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Deleting content `%s' of type %u that expired %llu ms ago\n",
340 (unsigned long long) (now.abs_value - expiration.abs_value));
342 GNUNET_STATISTICS_update (stats,
343 gettext_noop ("# bytes expired"),
346 GNUNET_CONTAINER_bloomfilter_remove (filter,
349 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY,
357 * Task that is used to remove expired entries from
358 * the datastore. This task will schedule itself
359 * again automatically to always delete all expired
362 * @param cls not used
363 * @param tc task context
366 delete_expired (void *cls,
367 const struct GNUNET_SCHEDULER_TaskContext *tc)
369 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
370 plugin->api->get_expiration (plugin->api->cls,
377 * An iterator over a set of items stored in the datastore
378 * that deletes until we're happy with respect to our quota.
381 * @param next_cls closure to pass to the "next" function.
382 * @param key key for the content
383 * @param size number of bytes in data
384 * @param data content stored
385 * @param type type of the content
386 * @param priority priority of the content
387 * @param anonymity anonymity-level for the content
388 * @param expiration expiration time for the content
389 * @param uid unique identifier for the datum;
390 * maybe 0 if no unique identifier is available
392 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
393 * (continue on call to "next", of course),
394 * GNUNET_NO to delete the item and continue (if supported)
397 quota_processor (void *cls,
398 const GNUNET_HashCode * key,
401 enum GNUNET_BLOCK_Type type,
404 struct GNUNET_TIME_Absolute expiration,
407 unsigned long long *need = cls;
410 return GNUNET_SYSERR;
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
414 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
419 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
422 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
423 GNUNET_STATISTICS_update (stats,
424 gettext_noop ("# bytes purged (low-priority)"),
427 GNUNET_CONTAINER_bloomfilter_remove (filter,
434 * Manage available disk space by running tasks
435 * that will discard content if necessary. This
436 * function will be run whenever a request for
437 * "need" bytes of storage could only be satisfied
438 * by eating into the "cache" (and we want our cache
441 * @param need number of bytes of content that were
442 * placed into the "cache" (and hence the
443 * number of bytes that should be removed).
446 manage_space (unsigned long long need)
448 unsigned long long last;
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Asked to free up %llu bytes of cache space\n",
456 while ( (need > 0) &&
460 plugin->api->get_expiration (plugin->api->cls,
468 * Function called to notify a client about the socket
469 * begin ready to queue more data. "buf" will be
470 * NULL and "size" zero if the socket was closed for
471 * writing in the meantime.
474 * @param size number of bytes available in buf
475 * @param buf where the callee should write the message
476 * @return number of bytes written to buf
479 transmit_callback (void *cls,
480 size_t size, void *buf)
482 struct TransmitCallbackContext *tcc = cls;
486 GNUNET_CONTAINER_DLL_remove (tcc_head,
489 msize = ntohs(tcc->msg->size);
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 _("Transmission to client failed!\n"));
494 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
495 GNUNET_SERVER_client_drop (tcc->client);
496 GNUNET_free (tcc->msg);
500 GNUNET_assert (size >= msize);
501 memcpy (buf, tcc->msg, msize);
502 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
503 GNUNET_SERVER_client_drop (tcc->client);
504 GNUNET_free (tcc->msg);
511 * Transmit the given message to the client.
513 * @param client target of the message
514 * @param msg message to transmit, will be freed!
515 * @param tc function to call afterwards
516 * @param tc_cls closure for tc
519 transmit (struct GNUNET_SERVER_Client *client,
520 struct GNUNET_MessageHeader *msg)
522 struct TransmitCallbackContext *tcc;
524 if (GNUNET_YES == cleaning_done)
527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
528 "Shutdown in progress, aborting transmission.\n");
530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
534 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
536 tcc->client = client;
538 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
540 GNUNET_TIME_UNIT_FOREVER_REL,
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550 GNUNET_SERVER_client_keep (client);
551 GNUNET_CONTAINER_DLL_insert (tcc_head,
558 * Transmit a status code to the client.
560 * @param client receiver of the response
561 * @param code status code
562 * @param msg optional error message (can be NULL)
565 transmit_status (struct GNUNET_SERVER_Client *client,
569 struct StatusMessage *sm;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Transmitting `%s' message with value %d and message `%s'\n",
577 msg != NULL ? msg : "(none)");
579 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
580 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
581 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
582 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
583 sm->status = htonl(code);
585 memcpy (&sm[1], msg, slen);
586 transmit (client, &sm->header);
592 * Function that will transmit the given datastore entry
595 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
596 * @param next_cls closure to use to ask for the next item
597 * @param key key for the content
598 * @param size number of bytes in data
599 * @param data content stored
600 * @param type type of the content
601 * @param priority priority of the content
602 * @param anonymity anonymity-level for the content
603 * @param expiration expiration time for the content
604 * @param uid unique identifier for the datum;
605 * maybe 0 if no unique identifier is available
607 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
608 * GNUNET_NO to delete the item and continue (if supported)
611 transmit_item (void *cls,
612 const GNUNET_HashCode * key,
615 enum GNUNET_BLOCK_Type type,
618 struct GNUNET_TIME_Absolute
619 expiration, uint64_t uid)
621 struct GNUNET_SERVER_Client *client = cls;
622 struct GNUNET_MessageHeader *end;
623 struct DataMessage *dm;
627 /* transmit 'DATA_END' */
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630 "Transmitting `%s' message\n",
633 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
634 end->size = htons(sizeof(struct GNUNET_MessageHeader));
635 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
636 transmit (client, end);
637 GNUNET_SERVER_client_drop (client);
640 GNUNET_assert (sizeof (struct DataMessage) + size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
641 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
642 dm->header.size = htons(sizeof(struct DataMessage) + size);
643 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
645 dm->size = htonl(size);
646 dm->type = htonl(type);
647 dm->priority = htonl(priority);
648 dm->anonymity = htonl(anonymity);
649 dm->replication = htonl (0);
650 dm->reserved = htonl (0);
651 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
652 dm->uid = GNUNET_htonll(uid);
654 memcpy (&dm[1], data, size);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Transmitting `%s' message for `%s' of type %u with expiration %llu (now: %llu)\n",
661 (unsigned long long) expiration.abs_value,
662 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value);
664 GNUNET_STATISTICS_update (stats,
665 gettext_noop ("# results found"),
668 transmit (client, &dm->header);
674 * Handle RESERVE-message.
677 * @param client identification of the client
678 * @param message the actual message
681 handle_reserve (void *cls,
682 struct GNUNET_SERVER_Client *client,
683 const struct GNUNET_MessageHeader *message)
686 * Static counter to produce reservation identifiers.
688 static int reservation_gen;
690 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
691 struct ReservationList *e;
692 unsigned long long used;
693 unsigned long long req;
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Processing `%s' request\n",
702 amount = GNUNET_ntohll(msg->amount);
703 entries = ntohl(msg->entries);
704 used = payload + reserved;
705 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
706 if (used + req > quota)
709 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
711 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
715 if (cache_size < req)
717 /* TODO: document this in the FAQ; essentially, if this
718 message happens, the insertion request could be blocked
719 by less-important content from migration because it is
720 larger than 1/8th of the overall available space, and
721 we only reserve 1/8th for "fresh" insertions */
722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
723 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
726 transmit_status (client, 0,
727 gettext_noop ("Insufficient space to satisfy request and "
728 "requested amount is larger than cache size"));
732 transmit_status (client, 0,
733 gettext_noop ("Insufficient space to satisfy request"));
738 GNUNET_STATISTICS_set (stats,
739 gettext_noop ("# reserved"),
742 e = GNUNET_malloc (sizeof(struct ReservationList));
743 e->next = reservations;
747 e->entries = entries;
748 e->rid = ++reservation_gen;
749 if (reservation_gen < 0)
750 reservation_gen = 0; /* wrap around */
751 transmit_status (client, e->rid, NULL);
756 * Handle RELEASE_RESERVE-message.
759 * @param client identification of the client
760 * @param message the actual message
763 handle_release_reserve (void *cls,
764 struct GNUNET_SERVER_Client *client,
765 const struct GNUNET_MessageHeader *message)
767 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
768 struct ReservationList *pos;
769 struct ReservationList *prev;
770 struct ReservationList *next;
771 int rid = ntohl(msg->rid);
772 unsigned long long rem;
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Processing `%s' request\n",
781 while (NULL != (pos = next))
790 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
791 GNUNET_assert (reserved >= rem);
793 GNUNET_STATISTICS_set (stats,
794 gettext_noop ("# reserved"),
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Returning %llu remaining reserved bytes to storage pool\n",
803 transmit_status (client, GNUNET_OK, NULL);
809 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
814 * Check that the given message is a valid data message.
816 * @return NULL if the message is not well-formed, otherwise the message
818 static const struct DataMessage *
819 check_data (const struct GNUNET_MessageHeader *message)
823 const struct DataMessage *dm;
825 size = ntohs(message->size);
826 if (size < sizeof(struct DataMessage))
831 dm = (const struct DataMessage *) message;
832 dsize = ntohl(dm->size);
833 if (size != dsize + sizeof(struct DataMessage))
843 * Context for a put request used to see if the content is
849 * Client to notify on completion.
851 struct GNUNET_SERVER_Client *client;
853 /* followed by the 'struct DataMessage' */
858 * Actually put the data message.
861 execute_put (struct GNUNET_SERVER_Client *client,
862 const struct DataMessage *dm)
868 size = ntohl(dm->size);
870 ret = plugin->api->put (plugin->api->cls,
876 ntohl(dm->anonymity),
877 ntohl(dm->replication),
878 GNUNET_TIME_absolute_ntoh(dm->expiration),
880 if (GNUNET_OK == ret)
882 GNUNET_STATISTICS_update (stats,
883 gettext_noop ("# bytes stored"),
886 GNUNET_CONTAINER_bloomfilter_add (filter,
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Successfully stored %u bytes of type %u under key `%s'\n",
893 GNUNET_h2s (&dm->key));
896 transmit_status (client,
899 GNUNET_free_non_null (msg);
900 if (quota - reserved - cache_size < payload)
902 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
903 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
904 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
905 (unsigned long long) (quota - reserved - cache_size),
906 (unsigned long long) payload);
907 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
913 * Function that will check if the given datastore entry
914 * matches the put and if none match executes the put.
916 * @param cls closure, pointer to the client (of type 'struct PutContext').
917 * @param key key for the content
918 * @param size number of bytes in data
919 * @param data content stored
920 * @param type type of the content
921 * @param priority priority of the content
922 * @param anonymity anonymity-level for the content
923 * @param expiration expiration time for the content
924 * @param uid unique identifier for the datum;
925 * maybe 0 if no unique identifier is available
927 * @return GNUNET_OK usually
928 * GNUNET_NO to delete the item
931 check_present (void *cls,
932 const GNUNET_HashCode * key,
935 enum GNUNET_BLOCK_Type type,
938 struct GNUNET_TIME_Absolute
939 expiration, uint64_t uid)
941 struct PutContext *pc = cls;
942 const struct DataMessage *dm;
944 dm = (const struct DataMessage*) &pc[1];
947 execute_put (pc->client, dm);
948 GNUNET_SERVER_client_drop (pc->client);
952 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
953 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
954 ( (size == ntohl(dm->size)) &&
955 (0 == memcmp (&dm[1],
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "Result already present in datastore\n");
963 /* FIXME: change API to allow increasing 'replication' counter */
964 if ( (ntohl (dm->priority) > 0) ||
965 (GNUNET_TIME_absolute_ntoh(dm->expiration).abs_value >
966 expiration.abs_value) )
967 plugin->api->update (plugin->api->cls,
969 (int32_t) ntohl(dm->priority),
970 GNUNET_TIME_absolute_ntoh(dm->expiration),
972 transmit_status (pc->client, GNUNET_NO, NULL);
973 GNUNET_SERVER_client_drop (pc->client);
978 execute_put (pc->client, dm);
979 GNUNET_SERVER_client_drop (pc->client);
987 * Handle PUT-message.
990 * @param client identification of the client
991 * @param message the actual message
994 handle_put (void *cls,
995 struct GNUNET_SERVER_Client *client,
996 const struct GNUNET_MessageHeader *message)
998 const struct DataMessage *dm = check_data (message);
1000 struct ReservationList *pos;
1001 struct PutContext *pc;
1002 GNUNET_HashCode vhash;
1005 if ( (dm == NULL) ||
1006 (ntohl(dm->type) == 0) )
1009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "Processing `%s' request for `%s' of type %u\n",
1016 GNUNET_h2s (&dm->key),
1019 rid = ntohl(dm->rid);
1020 size = ntohl(dm->size);
1024 while ( (NULL != pos) &&
1027 GNUNET_break (pos != NULL);
1030 GNUNET_break (pos->entries > 0);
1031 GNUNET_break (pos->amount >= size);
1033 pos->amount -= size;
1034 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1035 GNUNET_STATISTICS_set (stats,
1036 gettext_noop ("# reserved"),
1041 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1044 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
1045 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1046 pc->client = client;
1047 GNUNET_SERVER_client_keep (client);
1048 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1049 plugin->api->get_key (plugin->api->cls,
1058 execute_put (client, dm);
1063 * Handle GET-message.
1065 * @param cls closure
1066 * @param client identification of the client
1067 * @param message the actual message
1070 handle_get (void *cls,
1071 struct GNUNET_SERVER_Client *client,
1072 const struct GNUNET_MessageHeader *message)
1074 const struct GetMessage *msg;
1077 size = ntohs(message->size);
1078 if ( (size != sizeof(struct GetMessage)) &&
1079 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1085 msg = (const struct GetMessage*) message;
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Processing `%s' request for `%s' of type %u\n",
1090 GNUNET_h2s (&msg->key),
1093 GNUNET_STATISTICS_update (stats,
1094 gettext_noop ("# GET requests received"),
1097 GNUNET_SERVER_client_keep (client);
1098 if ( (size == sizeof(struct GetMessage)) &&
1099 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1102 /* don't bother database... */
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1107 GNUNET_h2s (&msg->key));
1109 GNUNET_STATISTICS_update (stats,
1110 gettext_noop ("# requests filtered by bloomfilter"),
1113 transmit_item (client,
1114 NULL, 0, NULL, 0, 0, 0,
1115 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1118 plugin->api->get_key (plugin->api->cls,
1119 GNUNET_ntohll (msg->offset),
1120 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1129 * Handle UPDATE-message.
1131 * @param cls closure
1132 * @param client identification of the client
1133 * @param message the actual message
1136 handle_update (void *cls,
1137 struct GNUNET_SERVER_Client *client,
1138 const struct GNUNET_MessageHeader *message)
1140 const struct UpdateMessage *msg;
1144 GNUNET_STATISTICS_update (stats,
1145 gettext_noop ("# UPDATE requests received"),
1148 msg = (const struct UpdateMessage*) message;
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "Processing `%s' request for %llu\n",
1154 (unsigned long long) GNUNET_ntohll (msg->uid));
1156 ret = plugin->api->update (plugin->api->cls,
1157 GNUNET_ntohll(msg->uid),
1158 (int32_t) ntohl(msg->priority),
1159 GNUNET_TIME_absolute_ntoh(msg->expiration),
1161 transmit_status (client, ret, emsg);
1162 GNUNET_free_non_null (emsg);
1167 * Handle GET_REPLICATION-message.
1169 * @param cls closure
1170 * @param client identification of the client
1171 * @param message the actual message
1174 handle_get_replication (void *cls,
1175 struct GNUNET_SERVER_Client *client,
1176 const struct GNUNET_MessageHeader *message)
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Processing `%s' request\n",
1183 GNUNET_STATISTICS_update (stats,
1184 gettext_noop ("# GET REPLICATION requests received"),
1187 GNUNET_SERVER_client_keep (client);
1188 plugin->api->get_replication (plugin->api->cls,
1195 * Handle GET_ZERO_ANONYMITY-message.
1197 * @param cls closure
1198 * @param client identification of the client
1199 * @param message the actual message
1202 handle_get_zero_anonymity (void *cls,
1203 struct GNUNET_SERVER_Client *client,
1204 const struct GNUNET_MessageHeader *message)
1206 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message;
1207 enum GNUNET_BLOCK_Type type;
1209 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1210 if (type == GNUNET_BLOCK_TYPE_ANY)
1213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "Processing `%s' request\n",
1219 "GET_ZERO_ANONYMITY");
1221 GNUNET_STATISTICS_update (stats,
1222 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1225 GNUNET_SERVER_client_keep (client);
1226 plugin->api->get_zero_anonymity (plugin->api->cls,
1227 GNUNET_ntohll (msg->offset),
1235 * Callback function that will cause the item that is passed
1236 * in to be deleted (by returning GNUNET_NO).
1239 remove_callback (void *cls,
1240 const GNUNET_HashCode * key,
1243 enum GNUNET_BLOCK_Type type,
1246 struct GNUNET_TIME_Absolute
1247 expiration, uint64_t uid)
1249 struct GNUNET_SERVER_Client *client = cls;
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "No further matches for `%s' request.\n",
1258 transmit_status (client, GNUNET_NO, _("Content not found"));
1259 GNUNET_SERVER_client_drop (client);
1260 return GNUNET_OK; /* last item */
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1265 (unsigned long long) uid,
1270 GNUNET_STATISTICS_update (stats,
1271 gettext_noop ("# bytes removed (explicit request)"),
1274 GNUNET_CONTAINER_bloomfilter_remove (filter,
1276 transmit_status (client, GNUNET_OK, NULL);
1277 GNUNET_SERVER_client_drop (client);
1283 * Handle REMOVE-message.
1285 * @param cls closure
1286 * @param client identification of the client
1287 * @param message the actual message
1290 handle_remove (void *cls,
1291 struct GNUNET_SERVER_Client *client,
1292 const struct GNUNET_MessageHeader *message)
1294 const struct DataMessage *dm = check_data (message);
1295 GNUNET_HashCode vhash;
1300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Processing `%s' request for `%s' of type %u\n",
1307 GNUNET_h2s (&dm->key),
1310 GNUNET_STATISTICS_update (stats,
1311 gettext_noop ("# REMOVE requests received"),
1314 GNUNET_SERVER_client_keep (client);
1315 GNUNET_CRYPTO_hash (&dm[1],
1318 plugin->api->get_key (plugin->api->cls,
1322 (enum GNUNET_BLOCK_Type) ntohl(dm->type),
1329 * Handle DROP-message.
1331 * @param cls closure
1332 * @param client identification of the client
1333 * @param message the actual message
1336 handle_drop (void *cls,
1337 struct GNUNET_SERVER_Client *client,
1338 const struct GNUNET_MessageHeader *message)
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Processing `%s' request\n",
1345 plugin->api->drop (plugin->api->cls);
1346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1351 * Function called by plugins to notify us about a
1352 * change in their disk utilization.
1354 * @param cls closure (NULL)
1355 * @param delta change in disk utilization,
1356 * 0 for "reset to empty"
1359 disk_utilization_change_cb (void *cls,
1363 (payload < -delta) )
1365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1366 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1367 (long long) payload,
1368 (long long) -delta);
1369 payload = plugin->api->estimate_size (plugin->api->cls);
1375 if (lastSync >= MAX_STAT_SYNC_LAG)
1381 * Callback function to process statistic values.
1383 * @param cls closure (struct Plugin*)
1384 * @param subsystem name of subsystem that created the statistic
1385 * @param name the name of the datum
1386 * @param value the current value
1387 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1388 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1391 process_stat_in (void *cls,
1392 const char *subsystem,
1397 GNUNET_assert (stats_worked == GNUNET_NO);
1398 stats_worked = GNUNET_YES;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1411 process_stat_done (void *cls,
1414 struct DatastorePlugin *plugin = cls;
1417 if (stats_worked == GNUNET_NO)
1418 payload = plugin->api->estimate_size (plugin->api->cls);
1423 * Load the datastore plugin.
1425 static struct DatastorePlugin *
1428 struct DatastorePlugin *ret;
1433 GNUNET_CONFIGURATION_get_value_string (cfg,
1434 "DATASTORE", "DATABASE", &name))
1436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1437 _("No `%s' specified for `%s' in configuration!\n"),
1442 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1444 ret->env.duc = &disk_utilization_change_cb;
1445 ret->env.cls = NULL;
1446 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1447 _("Loading `%s' datastore plugin\n"), name);
1448 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1449 ret->short_name = name;
1450 ret->lib_name = libname;
1451 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1452 if (ret->api == NULL)
1454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1455 _("Failed to load datastore plugin for `%s'\n"), name);
1456 GNUNET_free (ret->short_name);
1457 GNUNET_free (libname);
1466 * Function called when the service shuts
1467 * down. Unloads our datastore plugin.
1469 * @param plug plugin to unload
1472 unload_plugin (struct DatastorePlugin *plug)
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Datastore service is unloading plugin...\n");
1478 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1479 GNUNET_free (plug->lib_name);
1480 GNUNET_free (plug->short_name);
1486 * Final task run after shutdown. Unloads plugins and disconnects us from
1490 unload_task (void *cls,
1491 const struct GNUNET_SCHEDULER_TaskContext *tc)
1493 unload_plugin (plugin);
1497 GNUNET_CONTAINER_bloomfilter_free (filter);
1502 if (stat_get != NULL)
1504 GNUNET_STATISTICS_get_cancel (stat_get);
1509 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1516 * Last task run during shutdown. Disconnects us from
1517 * the transport and core.
1520 cleaning_task (void *cls,
1521 const struct GNUNET_SCHEDULER_TaskContext *tc)
1523 struct TransmitCallbackContext *tcc;
1525 cleaning_done = GNUNET_YES;
1526 while (NULL != (tcc = tcc_head))
1528 GNUNET_CONTAINER_DLL_remove (tcc_head,
1531 if (tcc->th != NULL)
1533 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1534 GNUNET_SERVER_client_drop (tcc->client);
1536 GNUNET_free (tcc->msg);
1539 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1541 GNUNET_SCHEDULER_cancel (expired_kill_task);
1542 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1544 GNUNET_SCHEDULER_add_continuation (&unload_task,
1546 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1551 * Function that removes all active reservations made
1552 * by the given client and releases the space for other
1555 * @param cls closure
1556 * @param client identification of the client
1559 cleanup_reservations (void *cls,
1560 struct GNUNET_SERVER_Client *client)
1562 struct ReservationList *pos;
1563 struct ReservationList *prev;
1564 struct ReservationList *next;
1573 if (pos->client == client)
1576 reservations = next;
1579 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1588 GNUNET_STATISTICS_set (stats,
1589 gettext_noop ("# reserved"),
1596 * Process datastore requests.
1598 * @param cls closure
1599 * @param server the initialized server
1600 * @param c configuration to use
1604 struct GNUNET_SERVER_Handle *server,
1605 const struct GNUNET_CONFIGURATION_Handle *c)
1607 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1608 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1609 sizeof(struct ReserveMessage) },
1610 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1611 sizeof(struct ReleaseReserveMessage) },
1612 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1613 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1614 sizeof (struct UpdateMessage) },
1615 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1616 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1617 sizeof(struct GNUNET_MessageHeader) },
1618 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1619 sizeof(struct GetZeroAnonymityMessage) },
1620 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1621 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1622 sizeof(struct GNUNET_MessageHeader) },
1626 unsigned int bf_size;
1630 GNUNET_CONFIGURATION_get_value_number (cfg,
1631 "DATASTORE", "QUOTA", "a))
1633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1634 _("No `%s' specified for `%s' in configuration!\n"),
1639 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1640 GNUNET_STATISTICS_set (stats,
1641 gettext_noop ("# quota"),
1644 cache_size = quota / 8; /* Or should we make this an option? */
1645 GNUNET_STATISTICS_set (stats,
1646 gettext_noop ("# cache size"),
1649 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1652 GNUNET_CONFIGURATION_get_value_filename (cfg,
1657 GNUNET_DISK_directory_create_for_file (fn)) )
1659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1660 _("Could not use specified filename `%s' for bloomfilter.\n"),
1661 fn != NULL ? fn : "");
1662 GNUNET_free_non_null (fn);
1665 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1666 GNUNET_free_non_null (fn);
1669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1670 _("Failed to initialize bloomfilter.\n"));
1673 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1678 plugin = load_plugin ();
1681 GNUNET_CONTAINER_bloomfilter_free (filter);
1685 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1690 stat_get = GNUNET_STATISTICS_get (stats,
1693 GNUNET_TIME_UNIT_SECONDS,
1697 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1698 GNUNET_SERVER_add_handlers (server, handlers);
1700 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1701 &delete_expired, NULL);
1702 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1703 &cleaning_task, NULL);
1708 * The main function for the datastore service.
1710 * @param argc number of arguments from the command line
1711 * @param argv command line arguments
1712 * @return 0 ok, 1 on error
1715 main (int argc, char *const *argv)
1720 GNUNET_SERVICE_run (argc,
1723 GNUNET_SERVICE_OPTION_NONE,
1724 &run, NULL)) ? 0 : 1;
1729 /* end of gnunet-service-datastore.c */