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);
669 GNUNET_SERVER_client_drop (client);
675 * Handle RESERVE-message.
678 * @param client identification of the client
679 * @param message the actual message
682 handle_reserve (void *cls,
683 struct GNUNET_SERVER_Client *client,
684 const struct GNUNET_MessageHeader *message)
687 * Static counter to produce reservation identifiers.
689 static int reservation_gen;
691 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
692 struct ReservationList *e;
693 unsigned long long used;
694 unsigned long long req;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Processing `%s' request\n",
703 amount = GNUNET_ntohll(msg->amount);
704 entries = ntohl(msg->entries);
705 used = payload + reserved;
706 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
707 if (used + req > quota)
710 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
711 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
712 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
716 if (cache_size < req)
718 /* TODO: document this in the FAQ; essentially, if this
719 message happens, the insertion request could be blocked
720 by less-important content from migration because it is
721 larger than 1/8th of the overall available space, and
722 we only reserve 1/8th for "fresh" insertions */
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
727 transmit_status (client, 0,
728 gettext_noop ("Insufficient space to satisfy request and "
729 "requested amount is larger than cache size"));
733 transmit_status (client, 0,
734 gettext_noop ("Insufficient space to satisfy request"));
739 GNUNET_STATISTICS_set (stats,
740 gettext_noop ("# reserved"),
743 e = GNUNET_malloc (sizeof(struct ReservationList));
744 e->next = reservations;
748 e->entries = entries;
749 e->rid = ++reservation_gen;
750 if (reservation_gen < 0)
751 reservation_gen = 0; /* wrap around */
752 transmit_status (client, e->rid, NULL);
757 * Handle RELEASE_RESERVE-message.
760 * @param client identification of the client
761 * @param message the actual message
764 handle_release_reserve (void *cls,
765 struct GNUNET_SERVER_Client *client,
766 const struct GNUNET_MessageHeader *message)
768 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
769 struct ReservationList *pos;
770 struct ReservationList *prev;
771 struct ReservationList *next;
772 int rid = ntohl(msg->rid);
773 unsigned long long rem;
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Processing `%s' request\n",
782 while (NULL != (pos = next))
791 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
792 GNUNET_assert (reserved >= rem);
794 GNUNET_STATISTICS_set (stats,
795 gettext_noop ("# reserved"),
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Returning %llu remaining reserved bytes to storage pool\n",
804 transmit_status (client, GNUNET_OK, NULL);
810 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
815 * Check that the given message is a valid data message.
817 * @return NULL if the message is not well-formed, otherwise the message
819 static const struct DataMessage *
820 check_data (const struct GNUNET_MessageHeader *message)
824 const struct DataMessage *dm;
826 size = ntohs(message->size);
827 if (size < sizeof(struct DataMessage))
832 dm = (const struct DataMessage *) message;
833 dsize = ntohl(dm->size);
834 if (size != dsize + sizeof(struct DataMessage))
844 * Context for a PUT request used to see if the content is
850 * Client to notify on completion.
852 struct GNUNET_SERVER_Client *client;
854 #if ! HAVE_UNALIGNED_64_ACCESS
858 /* followed by the 'struct DataMessage' */
863 * Actually put the data message.
866 execute_put (struct GNUNET_SERVER_Client *client,
867 const struct DataMessage *dm)
873 size = ntohl(dm->size);
875 ret = plugin->api->put (plugin->api->cls,
881 ntohl(dm->anonymity),
882 ntohl(dm->replication),
883 GNUNET_TIME_absolute_ntoh(dm->expiration),
885 if (GNUNET_OK == ret)
887 GNUNET_STATISTICS_update (stats,
888 gettext_noop ("# bytes stored"),
891 GNUNET_CONTAINER_bloomfilter_add (filter,
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Successfully stored %u bytes of type %u under key `%s'\n",
898 GNUNET_h2s (&dm->key));
901 transmit_status (client,
904 GNUNET_free_non_null (msg);
905 if (quota - reserved - cache_size < payload)
907 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
908 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
909 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
910 (unsigned long long) (quota - reserved - cache_size),
911 (unsigned long long) payload);
912 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
918 * Function that will check if the given datastore entry
919 * matches the put and if none match executes the put.
921 * @param cls closure, pointer to the client (of type 'struct PutContext').
922 * @param key key for the content
923 * @param size number of bytes in data
924 * @param data content stored
925 * @param type type of the content
926 * @param priority priority of the content
927 * @param anonymity anonymity-level for the content
928 * @param expiration expiration time for the content
929 * @param uid unique identifier for the datum;
930 * maybe 0 if no unique identifier is available
932 * @return GNUNET_OK usually
933 * GNUNET_NO to delete the item
936 check_present (void *cls,
937 const GNUNET_HashCode * key,
940 enum GNUNET_BLOCK_Type type,
943 struct GNUNET_TIME_Absolute
944 expiration, uint64_t uid)
946 struct PutContext *pc = cls;
947 const struct DataMessage *dm;
949 dm = (const struct DataMessage*) &pc[1];
952 execute_put (pc->client, dm);
953 GNUNET_SERVER_client_drop (pc->client);
957 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
958 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
959 ( (size == ntohl(dm->size)) &&
960 (0 == memcmp (&dm[1],
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "Result already present in datastore\n");
968 /* FIXME: change API to allow increasing 'replication' counter */
969 if ( (ntohl (dm->priority) > 0) ||
970 (GNUNET_TIME_absolute_ntoh(dm->expiration).abs_value >
971 expiration.abs_value) )
972 plugin->api->update (plugin->api->cls,
974 (int32_t) ntohl(dm->priority),
975 GNUNET_TIME_absolute_ntoh(dm->expiration),
977 transmit_status (pc->client, GNUNET_NO, NULL);
978 GNUNET_SERVER_client_drop (pc->client);
983 execute_put (pc->client, dm);
984 GNUNET_SERVER_client_drop (pc->client);
992 * Handle PUT-message.
995 * @param client identification of the client
996 * @param message the actual message
999 handle_put (void *cls,
1000 struct GNUNET_SERVER_Client *client,
1001 const struct GNUNET_MessageHeader *message)
1003 const struct DataMessage *dm = check_data (message);
1005 struct ReservationList *pos;
1006 struct PutContext *pc;
1007 GNUNET_HashCode vhash;
1010 if ( (dm == NULL) ||
1011 (ntohl(dm->type) == 0) )
1014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Processing `%s' request for `%s' of type %u\n",
1021 GNUNET_h2s (&dm->key),
1024 rid = ntohl(dm->rid);
1025 size = ntohl(dm->size);
1029 while ( (NULL != pos) &&
1032 GNUNET_break (pos != NULL);
1035 GNUNET_break (pos->entries > 0);
1036 GNUNET_break (pos->amount >= size);
1038 pos->amount -= size;
1039 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1040 GNUNET_STATISTICS_set (stats,
1041 gettext_noop ("# reserved"),
1046 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1049 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
1050 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1051 pc->client = client;
1052 GNUNET_SERVER_client_keep (client);
1053 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1054 plugin->api->get_key (plugin->api->cls,
1063 execute_put (client, dm);
1068 * Handle GET-message.
1070 * @param cls closure
1071 * @param client identification of the client
1072 * @param message the actual message
1075 handle_get (void *cls,
1076 struct GNUNET_SERVER_Client *client,
1077 const struct GNUNET_MessageHeader *message)
1079 const struct GetMessage *msg;
1082 size = ntohs(message->size);
1083 if ( (size != sizeof(struct GetMessage)) &&
1084 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1090 msg = (const struct GetMessage*) message;
1092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1093 "Processing `%s' request for `%s' of type %u\n",
1095 GNUNET_h2s (&msg->key),
1098 GNUNET_STATISTICS_update (stats,
1099 gettext_noop ("# GET requests received"),
1102 GNUNET_SERVER_client_keep (client);
1103 if ( (size == sizeof(struct GetMessage)) &&
1104 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1107 /* don't bother database... */
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1112 GNUNET_h2s (&msg->key));
1114 GNUNET_STATISTICS_update (stats,
1115 gettext_noop ("# requests filtered by bloomfilter"),
1118 transmit_item (client,
1119 NULL, 0, NULL, 0, 0, 0,
1120 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1123 plugin->api->get_key (plugin->api->cls,
1124 GNUNET_ntohll (msg->offset),
1125 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1134 * Handle UPDATE-message.
1136 * @param cls closure
1137 * @param client identification of the client
1138 * @param message the actual message
1141 handle_update (void *cls,
1142 struct GNUNET_SERVER_Client *client,
1143 const struct GNUNET_MessageHeader *message)
1145 const struct UpdateMessage *msg;
1149 GNUNET_STATISTICS_update (stats,
1150 gettext_noop ("# UPDATE requests received"),
1153 msg = (const struct UpdateMessage*) message;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1157 "Processing `%s' request for %llu\n",
1159 (unsigned long long) GNUNET_ntohll (msg->uid));
1161 ret = plugin->api->update (plugin->api->cls,
1162 GNUNET_ntohll(msg->uid),
1163 (int32_t) ntohl(msg->priority),
1164 GNUNET_TIME_absolute_ntoh(msg->expiration),
1166 transmit_status (client, ret, emsg);
1167 GNUNET_free_non_null (emsg);
1172 * Handle GET_REPLICATION-message.
1174 * @param cls closure
1175 * @param client identification of the client
1176 * @param message the actual message
1179 handle_get_replication (void *cls,
1180 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *message)
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Processing `%s' request\n",
1188 GNUNET_STATISTICS_update (stats,
1189 gettext_noop ("# GET REPLICATION requests received"),
1192 GNUNET_SERVER_client_keep (client);
1193 plugin->api->get_replication (plugin->api->cls,
1200 * Handle GET_ZERO_ANONYMITY-message.
1202 * @param cls closure
1203 * @param client identification of the client
1204 * @param message the actual message
1207 handle_get_zero_anonymity (void *cls,
1208 struct GNUNET_SERVER_Client *client,
1209 const struct GNUNET_MessageHeader *message)
1211 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message;
1212 enum GNUNET_BLOCK_Type type;
1214 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1215 if (type == GNUNET_BLOCK_TYPE_ANY)
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Processing `%s' request\n",
1224 "GET_ZERO_ANONYMITY");
1226 GNUNET_STATISTICS_update (stats,
1227 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1230 GNUNET_SERVER_client_keep (client);
1231 plugin->api->get_zero_anonymity (plugin->api->cls,
1232 GNUNET_ntohll (msg->offset),
1240 * Callback function that will cause the item that is passed
1241 * in to be deleted (by returning GNUNET_NO).
1244 remove_callback (void *cls,
1245 const GNUNET_HashCode * key,
1248 enum GNUNET_BLOCK_Type type,
1251 struct GNUNET_TIME_Absolute
1252 expiration, uint64_t uid)
1254 struct GNUNET_SERVER_Client *client = cls;
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "No further matches for `%s' request.\n",
1263 transmit_status (client, GNUNET_NO, _("Content not found"));
1264 GNUNET_SERVER_client_drop (client);
1265 return GNUNET_OK; /* last item */
1268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1270 (unsigned long long) uid,
1275 GNUNET_STATISTICS_update (stats,
1276 gettext_noop ("# bytes removed (explicit request)"),
1279 GNUNET_CONTAINER_bloomfilter_remove (filter,
1281 transmit_status (client, GNUNET_OK, NULL);
1282 GNUNET_SERVER_client_drop (client);
1288 * Handle REMOVE-message.
1290 * @param cls closure
1291 * @param client identification of the client
1292 * @param message the actual message
1295 handle_remove (void *cls,
1296 struct GNUNET_SERVER_Client *client,
1297 const struct GNUNET_MessageHeader *message)
1299 const struct DataMessage *dm = check_data (message);
1300 GNUNET_HashCode vhash;
1305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Processing `%s' request for `%s' of type %u\n",
1312 GNUNET_h2s (&dm->key),
1315 GNUNET_STATISTICS_update (stats,
1316 gettext_noop ("# REMOVE requests received"),
1319 GNUNET_SERVER_client_keep (client);
1320 GNUNET_CRYPTO_hash (&dm[1],
1323 plugin->api->get_key (plugin->api->cls,
1327 (enum GNUNET_BLOCK_Type) ntohl(dm->type),
1334 * Handle DROP-message.
1336 * @param cls closure
1337 * @param client identification of the client
1338 * @param message the actual message
1341 handle_drop (void *cls,
1342 struct GNUNET_SERVER_Client *client,
1343 const struct GNUNET_MessageHeader *message)
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Processing `%s' request\n",
1350 do_drop = GNUNET_YES;
1351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1356 * Function called by plugins to notify us about a
1357 * change in their disk utilization.
1359 * @param cls closure (NULL)
1360 * @param delta change in disk utilization,
1361 * 0 for "reset to empty"
1364 disk_utilization_change_cb (void *cls,
1368 (payload < -delta) )
1370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1371 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1372 (long long) payload,
1373 (long long) -delta);
1374 payload = plugin->api->estimate_size (plugin->api->cls);
1380 if (lastSync >= MAX_STAT_SYNC_LAG)
1386 * Callback function to process statistic values.
1388 * @param cls closure (struct Plugin*)
1389 * @param subsystem name of subsystem that created the statistic
1390 * @param name the name of the datum
1391 * @param value the current value
1392 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1393 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1396 process_stat_in (void *cls,
1397 const char *subsystem,
1402 GNUNET_assert (stats_worked == GNUNET_NO);
1403 stats_worked = GNUNET_YES;
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1416 process_stat_done (void *cls,
1419 struct DatastorePlugin *plugin = cls;
1422 if (stats_worked == GNUNET_NO)
1423 payload = plugin->api->estimate_size (plugin->api->cls);
1428 * Load the datastore plugin.
1430 static struct DatastorePlugin *
1433 struct DatastorePlugin *ret;
1438 GNUNET_CONFIGURATION_get_value_string (cfg,
1439 "DATASTORE", "DATABASE", &name))
1441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1442 _("No `%s' specified for `%s' in configuration!\n"),
1447 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1449 ret->env.duc = &disk_utilization_change_cb;
1450 ret->env.cls = NULL;
1451 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1452 _("Loading `%s' datastore plugin\n"), name);
1453 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1454 ret->short_name = name;
1455 ret->lib_name = libname;
1456 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1457 if (ret->api == NULL)
1459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1460 _("Failed to load datastore plugin for `%s'\n"), name);
1461 GNUNET_free (ret->short_name);
1462 GNUNET_free (libname);
1471 * Function called when the service shuts
1472 * down. Unloads our datastore plugin.
1474 * @param plug plugin to unload
1477 unload_plugin (struct DatastorePlugin *plug)
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Datastore service is unloading plugin...\n");
1483 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1484 GNUNET_free (plug->lib_name);
1485 GNUNET_free (plug->short_name);
1491 * Final task run after shutdown. Unloads plugins and disconnects us from
1495 unload_task (void *cls,
1496 const struct GNUNET_SCHEDULER_TaskContext *tc)
1498 if (GNUNET_YES == do_drop)
1499 plugin->api->drop (plugin->api->cls);
1500 unload_plugin (plugin);
1504 GNUNET_CONTAINER_bloomfilter_free (filter);
1509 if (stat_get != NULL)
1511 GNUNET_STATISTICS_get_cancel (stat_get);
1516 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1523 * Last task run during shutdown. Disconnects us from
1524 * the transport and core.
1527 cleaning_task (void *cls,
1528 const struct GNUNET_SCHEDULER_TaskContext *tc)
1530 struct TransmitCallbackContext *tcc;
1532 cleaning_done = GNUNET_YES;
1533 while (NULL != (tcc = tcc_head))
1535 GNUNET_CONTAINER_DLL_remove (tcc_head,
1538 if (tcc->th != NULL)
1540 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1541 GNUNET_SERVER_client_drop (tcc->client);
1543 GNUNET_free (tcc->msg);
1546 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1548 GNUNET_SCHEDULER_cancel (expired_kill_task);
1549 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1551 GNUNET_SCHEDULER_add_continuation (&unload_task,
1553 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1558 * Function that removes all active reservations made
1559 * by the given client and releases the space for other
1562 * @param cls closure
1563 * @param client identification of the client
1566 cleanup_reservations (void *cls,
1567 struct GNUNET_SERVER_Client *client)
1569 struct ReservationList *pos;
1570 struct ReservationList *prev;
1571 struct ReservationList *next;
1580 if (pos->client == client)
1583 reservations = next;
1586 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1595 GNUNET_STATISTICS_set (stats,
1596 gettext_noop ("# reserved"),
1603 * Process datastore requests.
1605 * @param cls closure
1606 * @param server the initialized server
1607 * @param c configuration to use
1611 struct GNUNET_SERVER_Handle *server,
1612 const struct GNUNET_CONFIGURATION_Handle *c)
1614 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1615 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1616 sizeof(struct ReserveMessage) },
1617 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1618 sizeof(struct ReleaseReserveMessage) },
1619 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1620 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1621 sizeof (struct UpdateMessage) },
1622 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1623 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1624 sizeof(struct GNUNET_MessageHeader) },
1625 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1626 sizeof(struct GetZeroAnonymityMessage) },
1627 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1628 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1629 sizeof(struct GNUNET_MessageHeader) },
1633 unsigned int bf_size;
1637 GNUNET_CONFIGURATION_get_value_number (cfg,
1638 "DATASTORE", "QUOTA", "a))
1640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1641 _("No `%s' specified for `%s' in configuration!\n"),
1646 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1647 GNUNET_STATISTICS_set (stats,
1648 gettext_noop ("# quota"),
1651 cache_size = quota / 8; /* Or should we make this an option? */
1652 GNUNET_STATISTICS_set (stats,
1653 gettext_noop ("# cache size"),
1656 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1659 GNUNET_CONFIGURATION_get_value_filename (cfg,
1664 GNUNET_DISK_directory_create_for_file (fn)) )
1666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1667 _("Could not use specified filename `%s' for bloomfilter.\n"),
1668 fn != NULL ? fn : "");
1669 GNUNET_free_non_null (fn);
1673 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1675 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1676 GNUNET_free_non_null (fn);
1679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1680 _("Failed to initialize bloomfilter.\n"));
1683 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1688 plugin = load_plugin ();
1691 GNUNET_CONTAINER_bloomfilter_free (filter);
1695 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1700 stat_get = GNUNET_STATISTICS_get (stats,
1703 GNUNET_TIME_UNIT_SECONDS,
1707 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1708 GNUNET_SERVER_add_handlers (server, handlers);
1710 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1711 &delete_expired, NULL);
1712 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1713 &cleaning_task, NULL);
1718 * The main function for the datastore service.
1720 * @param argc number of arguments from the command line
1721 * @param argv command line arguments
1722 * @return 0 ok, 1 on error
1725 main (int argc, char *const *argv)
1730 GNUNET_SERVICE_run (argc,
1733 GNUNET_SERVICE_OPTION_NONE,
1734 &run, NULL)) ? 0 : 1;
1739 /* end of gnunet-service-datastore.c */