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 * Should the database be dropped on exit?
153 * How much space are we using for the cache? (space available for
154 * insertions that will be instantly reclaimed by discarding less
155 * important content --- or possibly whatever we just inserted into
158 static unsigned long long cache_size;
161 * How much space have we currently reserved?
163 static unsigned long long reserved;
166 * How much data are we currently storing
169 static unsigned long long payload;
172 * Number of updates that were made to the
173 * payload value since we last synchronized
174 * it with the statistics service.
176 static unsigned int lastSync;
179 * Did we get an answer from statistics?
181 static int stats_worked;
184 * Identity of the task that is used to delete
187 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
192 const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Handle for reporting statistics.
198 static struct GNUNET_STATISTICS_Handle *stats;
202 * Synchronize our utilization statistics with the
203 * statistics service.
208 GNUNET_STATISTICS_set (stats,
218 * Context for transmitting replies to clients.
220 struct TransmitCallbackContext
224 * We keep these in a doubly-linked list (for cleanup).
226 struct TransmitCallbackContext *next;
229 * We keep these in a doubly-linked list (for cleanup).
231 struct TransmitCallbackContext *prev;
234 * The message that we're asked to transmit.
236 struct GNUNET_MessageHeader *msg;
239 * Handle for the transmission request.
241 struct GNUNET_CONNECTION_TransmitHandle *th;
244 * Client that we are transmitting to.
246 struct GNUNET_SERVER_Client *client;
252 * Head of the doubly-linked list (for cleanup).
254 static struct TransmitCallbackContext *tcc_head;
257 * Tail of the doubly-linked list (for cleanup).
259 static struct TransmitCallbackContext *tcc_tail;
262 * Have we already cleaned up the TCCs and are hence no longer
263 * willing (or able) to transmit anything to anyone?
265 static int cleaning_done;
268 * Handle for pending get request.
270 static struct GNUNET_STATISTICS_GetHandle *stat_get;
274 * Task that is used to remove expired entries from
275 * the datastore. This task will schedule itself
276 * again automatically to always delete all expired
279 * @param cls not used
280 * @param tc task context
283 delete_expired (void *cls,
284 const struct GNUNET_SCHEDULER_TaskContext *tc);
288 * Iterate over the expired items stored in the datastore.
289 * Delete all expired items; once we have processed all
290 * expired items, re-schedule the "delete_expired" task.
292 * @param cls not used
293 * @param key key for the content
294 * @param size number of bytes in data
295 * @param data content stored
296 * @param type type of the content
297 * @param priority priority of the content
298 * @param anonymity anonymity-level for the content
299 * @param expiration expiration time for the content
300 * @param uid unique identifier for the datum;
301 * maybe 0 if no unique identifier is available
303 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
304 * (continue on call to "next", of course),
305 * GNUNET_NO to delete the item and continue (if supported)
308 expired_processor (void *cls,
309 const GNUNET_HashCode * key,
312 enum GNUNET_BLOCK_Type type,
315 struct GNUNET_TIME_Absolute
319 struct GNUNET_TIME_Absolute now;
324 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
327 return GNUNET_SYSERR;
329 now = GNUNET_TIME_absolute_get ();
330 if (expiration.abs_value > now.abs_value)
332 /* finished processing */
334 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
337 return GNUNET_SYSERR;
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Deleting content `%s' of type %u that expired %llu ms ago\n",
344 (unsigned long long) (now.abs_value - expiration.abs_value));
346 GNUNET_STATISTICS_update (stats,
347 gettext_noop ("# bytes expired"),
350 GNUNET_CONTAINER_bloomfilter_remove (filter,
353 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY,
361 * Task that is used to remove expired entries from
362 * the datastore. This task will schedule itself
363 * again automatically to always delete all expired
366 * @param cls not used
367 * @param tc task context
370 delete_expired (void *cls,
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
373 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
374 plugin->api->get_expiration (plugin->api->cls,
381 * An iterator over a set of items stored in the datastore
382 * that deletes until we're happy with respect to our quota.
385 * @param key key for the content
386 * @param size number of bytes in data
387 * @param data content stored
388 * @param type type of the content
389 * @param priority priority of the content
390 * @param anonymity anonymity-level for the content
391 * @param expiration expiration time for the content
392 * @param uid unique identifier for the datum;
393 * maybe 0 if no unique identifier is available
395 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
396 * (continue on call to "next", of course),
397 * GNUNET_NO to delete the item and continue (if supported)
400 quota_processor (void *cls,
401 const GNUNET_HashCode * key,
404 enum GNUNET_BLOCK_Type type,
407 struct GNUNET_TIME_Absolute expiration,
410 unsigned long long *need = cls;
413 return GNUNET_SYSERR;
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
417 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
422 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
425 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
426 GNUNET_STATISTICS_update (stats,
427 gettext_noop ("# bytes purged (low-priority)"),
430 GNUNET_CONTAINER_bloomfilter_remove (filter,
437 * Manage available disk space by running tasks
438 * that will discard content if necessary. This
439 * function will be run whenever a request for
440 * "need" bytes of storage could only be satisfied
441 * by eating into the "cache" (and we want our cache
444 * @param need number of bytes of content that were
445 * placed into the "cache" (and hence the
446 * number of bytes that should be removed).
449 manage_space (unsigned long long need)
451 unsigned long long last;
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Asked to free up %llu bytes of cache space\n",
459 while ( (need > 0) &&
463 plugin->api->get_expiration (plugin->api->cls,
471 * Function called to notify a client about the socket
472 * begin ready to queue more data. "buf" will be
473 * NULL and "size" zero if the socket was closed for
474 * writing in the meantime.
477 * @param size number of bytes available in buf
478 * @param buf where the callee should write the message
479 * @return number of bytes written to buf
482 transmit_callback (void *cls,
483 size_t size, void *buf)
485 struct TransmitCallbackContext *tcc = cls;
489 GNUNET_CONTAINER_DLL_remove (tcc_head,
492 msize = ntohs(tcc->msg->size);
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
496 _("Transmission to client failed!\n"));
497 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
498 GNUNET_SERVER_client_drop (tcc->client);
499 GNUNET_free (tcc->msg);
503 GNUNET_assert (size >= msize);
504 memcpy (buf, tcc->msg, msize);
505 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
506 GNUNET_SERVER_client_drop (tcc->client);
507 GNUNET_free (tcc->msg);
514 * Transmit the given message to the client.
516 * @param client target of the message
517 * @param msg message to transmit, will be freed!
520 transmit (struct GNUNET_SERVER_Client *client,
521 struct GNUNET_MessageHeader *msg)
523 struct TransmitCallbackContext *tcc;
525 if (GNUNET_YES == cleaning_done)
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 "Shutdown in progress, aborting transmission.\n");
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
537 tcc->client = client;
539 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
541 GNUNET_TIME_UNIT_FOREVER_REL,
546 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
551 GNUNET_SERVER_client_keep (client);
552 GNUNET_CONTAINER_DLL_insert (tcc_head,
559 * Transmit a status code to the client.
561 * @param client receiver of the response
562 * @param code status code
563 * @param msg optional error message (can be NULL)
566 transmit_status (struct GNUNET_SERVER_Client *client,
570 struct StatusMessage *sm;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Transmitting `%s' message with value %d and message `%s'\n",
578 msg != NULL ? msg : "(none)");
580 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
581 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
582 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
583 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
584 sm->status = htonl(code);
586 memcpy (&sm[1], msg, slen);
587 transmit (client, &sm->header);
593 * Function that will transmit the given datastore entry
596 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
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 do_drop = GNUNET_YES;
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 if (GNUNET_YES == do_drop)
1494 plugin->api->drop (plugin->api->cls);
1495 unload_plugin (plugin);
1499 GNUNET_CONTAINER_bloomfilter_free (filter);
1504 if (stat_get != NULL)
1506 GNUNET_STATISTICS_get_cancel (stat_get);
1511 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1518 * Last task run during shutdown. Disconnects us from
1519 * the transport and core.
1522 cleaning_task (void *cls,
1523 const struct GNUNET_SCHEDULER_TaskContext *tc)
1525 struct TransmitCallbackContext *tcc;
1527 cleaning_done = GNUNET_YES;
1528 while (NULL != (tcc = tcc_head))
1530 GNUNET_CONTAINER_DLL_remove (tcc_head,
1533 if (tcc->th != NULL)
1535 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1536 GNUNET_SERVER_client_drop (tcc->client);
1538 GNUNET_free (tcc->msg);
1541 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1543 GNUNET_SCHEDULER_cancel (expired_kill_task);
1544 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1546 GNUNET_SCHEDULER_add_continuation (&unload_task,
1548 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1553 * Function that removes all active reservations made
1554 * by the given client and releases the space for other
1557 * @param cls closure
1558 * @param client identification of the client
1561 cleanup_reservations (void *cls,
1562 struct GNUNET_SERVER_Client *client)
1564 struct ReservationList *pos;
1565 struct ReservationList *prev;
1566 struct ReservationList *next;
1575 if (pos->client == client)
1578 reservations = next;
1581 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1590 GNUNET_STATISTICS_set (stats,
1591 gettext_noop ("# reserved"),
1598 * Process datastore requests.
1600 * @param cls closure
1601 * @param server the initialized server
1602 * @param c configuration to use
1606 struct GNUNET_SERVER_Handle *server,
1607 const struct GNUNET_CONFIGURATION_Handle *c)
1609 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1610 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1611 sizeof(struct ReserveMessage) },
1612 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1613 sizeof(struct ReleaseReserveMessage) },
1614 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1615 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1616 sizeof (struct UpdateMessage) },
1617 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1618 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1619 sizeof(struct GNUNET_MessageHeader) },
1620 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1621 sizeof(struct GetZeroAnonymityMessage) },
1622 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1623 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1624 sizeof(struct GNUNET_MessageHeader) },
1628 unsigned int bf_size;
1632 GNUNET_CONFIGURATION_get_value_number (cfg,
1633 "DATASTORE", "QUOTA", "a))
1635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1636 _("No `%s' specified for `%s' in configuration!\n"),
1641 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1642 GNUNET_STATISTICS_set (stats,
1643 gettext_noop ("# quota"),
1646 cache_size = quota / 8; /* Or should we make this an option? */
1647 GNUNET_STATISTICS_set (stats,
1648 gettext_noop ("# cache size"),
1651 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1654 GNUNET_CONFIGURATION_get_value_filename (cfg,
1659 GNUNET_DISK_directory_create_for_file (fn)) )
1661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1662 _("Could not use specified filename `%s' for bloomfilter.\n"),
1663 fn != NULL ? fn : "");
1664 GNUNET_free_non_null (fn);
1668 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1670 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1671 GNUNET_free_non_null (fn);
1674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1675 _("Failed to initialize bloomfilter.\n"));
1678 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1683 plugin = load_plugin ();
1686 GNUNET_CONTAINER_bloomfilter_free (filter);
1690 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1695 stat_get = GNUNET_STATISTICS_get (stats,
1698 GNUNET_TIME_UNIT_SECONDS,
1702 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1703 GNUNET_SERVER_add_handlers (server, handlers);
1705 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1706 &delete_expired, NULL);
1707 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1708 &cleaning_task, NULL);
1713 * The main function for the datastore service.
1715 * @param argc number of arguments from the command line
1716 * @param argv command line arguments
1717 * @return 0 ok, 1 on error
1720 main (int argc, char *const *argv)
1725 GNUNET_SERVICE_run (argc,
1728 GNUNET_SERVICE_OPTION_NONE,
1729 &run, NULL)) ? 0 : 1;
1734 /* end of gnunet-service-datastore.c */