2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_statistics_service.h"
31 #include "plugin_datastore.h"
32 #include "datastore.h"
35 * How many messages do we queue at most per client?
37 #define MAX_PENDING 1024
40 * How long are we at most keeping "expired" content
41 * past the expiration date in the database?
43 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48 * Our datastore plugin.
50 struct DatastorePlugin
54 * API of the transport as returned by the plugin's
55 * initialization function.
57 struct GNUNET_DATASTORE_PluginFunctions *api;
60 * Short name for the plugin (i.e. "sqlite").
65 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
70 * Environment this transport service is using
73 struct GNUNET_DATASTORE_PluginEnvironment env;
79 * Linked list of active reservations.
81 struct ReservationList
85 * This is a linked list.
87 struct ReservationList *next;
90 * Client that made the reservation.
92 struct GNUNET_SERVER_Client *client;
95 * Number of bytes (still) reserved.
100 * Number of items (still) reserved.
105 * Reservation identifier.
113 * Our datastore plugin (NULL if not available).
115 static struct DatastorePlugin *plugin;
118 * Linked list of space reservations made by clients.
120 static struct ReservationList *reservations;
123 * Bloomfilter to quickly tell if we don't have the content.
125 static struct GNUNET_CONTAINER_BloomFilter *filter;
128 * How much space are we allowed to use?
130 static unsigned long long quota;
133 * How much space are we using for the cache? (space available for
134 * insertions that will be instantly reclaimed by discarding less
135 * important content --- or possibly whatever we just inserted into
138 static unsigned long long cache_size;
141 * How much space have we currently reserved?
143 static unsigned long long reserved;
146 * Identity of the task that is used to delete
149 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
154 const struct GNUNET_CONFIGURATION_Handle *cfg;
159 struct GNUNET_SCHEDULER_Handle *sched;
162 * Handle for reporting statistics.
164 static struct GNUNET_STATISTICS_Handle *stats;
168 * Function called once the transmit operation has
169 * either failed or succeeded.
172 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
174 typedef void (*TransmitContinuation)(void *cls,
179 * Context for transmitting replies to clients.
181 struct TransmitCallbackContext
185 * We keep these in a doubly-linked list (for cleanup).
187 struct TransmitCallbackContext *next;
190 * We keep these in a doubly-linked list (for cleanup).
192 struct TransmitCallbackContext *prev;
195 * The message that we're asked to transmit.
197 struct GNUNET_MessageHeader *msg;
200 * Handle for the transmission request.
202 struct GNUNET_CONNECTION_TransmitHandle *th;
205 * Client that we are transmitting to.
207 struct GNUNET_SERVER_Client *client;
210 * Function to call once msg has been transmitted
211 * (or at least added to the buffer).
213 TransmitContinuation tc;
221 * GNUNET_YES if we are supposed to signal the server
222 * completion of the client's request.
229 * Head of the doubly-linked list (for cleanup).
231 static struct TransmitCallbackContext *tcc_head;
234 * Tail of the doubly-linked list (for cleanup).
236 static struct TransmitCallbackContext *tcc_tail;
239 * Have we already cleaned up the TCCs and are hence no longer
240 * willing (or able) to transmit anything to anyone?
242 static int cleaning_done;
245 * Task that is used to remove expired entries from
246 * the datastore. This task will schedule itself
247 * again automatically to always delete all expired
250 * @param cls not used
251 * @param tc task context
254 delete_expired (void *cls,
255 const struct GNUNET_SCHEDULER_TaskContext *tc);
259 * Iterate over the expired items stored in the datastore.
260 * Delete all expired items; once we have processed all
261 * expired items, re-schedule the "delete_expired" task.
263 * @param cls not used
264 * @param next_cls closure to pass to the "next" function.
265 * @param key key for the content
266 * @param size number of bytes in data
267 * @param data content stored
268 * @param type type of the content
269 * @param priority priority of the content
270 * @param anonymity anonymity-level for the content
271 * @param expiration expiration time for the content
272 * @param uid unique identifier for the datum;
273 * maybe 0 if no unique identifier is available
275 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
276 * (continue on call to "next", of course),
277 * GNUNET_NO to delete the item and continue (if supported)
280 expired_processor (void *cls,
282 const GNUNET_HashCode * key,
285 enum GNUNET_BLOCK_Type type,
288 struct GNUNET_TIME_Absolute
292 struct GNUNET_TIME_Absolute now;
297 = GNUNET_SCHEDULER_add_delayed (sched,
301 return GNUNET_SYSERR;
303 now = GNUNET_TIME_absolute_get ();
304 if (expiration.value > now.value)
306 /* finished processing */
307 plugin->api->next_request (next_cls, GNUNET_YES);
308 return GNUNET_SYSERR;
310 plugin->api->next_request (next_cls, GNUNET_NO);
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Deleting content `%s' of type %u that expired %llu ms ago\n",
316 (unsigned long long) (now.value - expiration.value));
318 GNUNET_STATISTICS_update (stats,
319 gettext_noop ("# bytes expired"),
322 GNUNET_CONTAINER_bloomfilter_remove (filter,
324 return GNUNET_NO; /* delete */
329 * Task that is used to remove expired entries from
330 * the datastore. This task will schedule itself
331 * again automatically to always delete all expired
334 * @param cls not used
335 * @param tc task context
338 delete_expired (void *cls,
339 const struct GNUNET_SCHEDULER_TaskContext *tc)
341 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
342 plugin->api->iter_ascending_expiration (plugin->api->cls,
350 * An iterator over a set of items stored in the datastore.
353 * @param next_cls closure to pass to the "next" function.
354 * @param key key for the content
355 * @param size number of bytes in data
356 * @param data content stored
357 * @param type type of the content
358 * @param priority priority of the content
359 * @param anonymity anonymity-level for the content
360 * @param expiration expiration time for the content
361 * @param uid unique identifier for the datum;
362 * maybe 0 if no unique identifier is available
364 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
365 * (continue on call to "next", of course),
366 * GNUNET_NO to delete the item and continue (if supported)
371 const GNUNET_HashCode * key,
374 enum GNUNET_BLOCK_Type type,
377 struct GNUNET_TIME_Absolute
381 unsigned long long *need = cls;
386 return GNUNET_SYSERR;
388 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
391 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
392 plugin->api->next_request (next_cls,
393 (0 == *need) ? GNUNET_YES : GNUNET_NO);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
397 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
402 GNUNET_STATISTICS_update (stats,
403 gettext_noop ("# bytes purged (low-priority)"),
406 GNUNET_CONTAINER_bloomfilter_remove (filter,
413 * Manage available disk space by running tasks
414 * that will discard content if necessary. This
415 * function will be run whenever a request for
416 * "need" bytes of storage could only be satisfied
417 * by eating into the "cache" (and we want our cache
420 * @param need number of bytes of content that were
421 * placed into the "cache" (and hence the
422 * number of bytes that should be removed).
425 manage_space (unsigned long long need)
427 unsigned long long *n;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Asked to free up %llu bytes of cache space\n",
434 n = GNUNET_malloc (sizeof(unsigned long long));
436 plugin->api->iter_low_priority (plugin->api->cls,
444 * Function called to notify a client about the socket
445 * begin ready to queue more data. "buf" will be
446 * NULL and "size" zero if the socket was closed for
447 * writing in the meantime.
450 * @param size number of bytes available in buf
451 * @param buf where the callee should write the message
452 * @return number of bytes written to buf
455 transmit_callback (void *cls,
456 size_t size, void *buf)
458 struct TransmitCallbackContext *tcc = cls;
462 GNUNET_CONTAINER_DLL_remove (tcc_head,
465 msize = ntohs(tcc->msg->size);
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "Transmission failed.\n");
473 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
474 if (GNUNET_YES == tcc->end)
475 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
476 GNUNET_SERVER_client_drop (tcc->client);
477 GNUNET_free (tcc->msg);
481 GNUNET_assert (size >= msize);
482 memcpy (buf, tcc->msg, msize);
484 tcc->tc (tcc->tc_cls, GNUNET_OK);
485 if (GNUNET_YES == tcc->end)
487 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Response transmitted, more pending!\n");
496 GNUNET_SERVER_client_drop (tcc->client);
497 GNUNET_free (tcc->msg);
504 * Transmit the given message to the client.
506 * @param client target of the message
507 * @param msg message to transmit, will be freed!
508 * @param tc function to call afterwards
509 * @param tc_cls closure for tc
510 * @param end is this the last response (and we should
511 * signal the server completion accodingly after
512 * transmitting this message)?
515 transmit (struct GNUNET_SERVER_Client *client,
516 struct GNUNET_MessageHeader *msg,
517 TransmitContinuation tc,
521 struct TransmitCallbackContext *tcc;
523 if (GNUNET_YES == cleaning_done)
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
527 "Shutdown in progress, aborting transmission.\n");
531 tc (tc_cls, GNUNET_SYSERR);
534 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
536 tcc->client = client;
538 tcc->tc_cls = tc_cls;
541 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
543 GNUNET_TIME_UNIT_FOREVER_REL,
548 if (GNUNET_YES == end)
551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
552 "Disconnecting client.\n");
554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
557 tc (tc_cls, GNUNET_SYSERR);
562 GNUNET_SERVER_client_keep (client);
563 GNUNET_CONTAINER_DLL_insert (tcc_head,
570 * Transmit a status code to the client.
572 * @param client receiver of the response
573 * @param code status code
574 * @param msg optional error message (can be NULL)
577 transmit_status (struct GNUNET_SERVER_Client *client,
581 struct StatusMessage *sm;
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Transmitting `%s' message with value %d and message `%s'\n",
589 msg != NULL ? msg : "(none)");
591 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
592 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
593 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
594 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
595 sm->status = htonl(code);
597 memcpy (&sm[1], msg, slen);
598 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
603 * Function called once the transmit operation has
604 * either failed or succeeded.
606 * @param next_cls closure for calling "next_request" callback
607 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
610 get_next(void *next_cls,
613 if (status != GNUNET_OK)
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 _("Failed to transmit an item to the client; aborting iteration.\n"));
618 plugin->api->next_request (next_cls, GNUNET_YES);
621 plugin->api->next_request (next_cls, GNUNET_NO);
626 * Function that will transmit the given datastore entry
629 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
630 * @param next_cls closure to use to ask for the next item
631 * @param key key for the content
632 * @param size number of bytes in data
633 * @param data content stored
634 * @param type type of the content
635 * @param priority priority of the content
636 * @param anonymity anonymity-level for the content
637 * @param expiration expiration time for the content
638 * @param uid unique identifier for the datum;
639 * maybe 0 if no unique identifier is available
641 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
642 * GNUNET_NO to delete the item and continue (if supported)
645 transmit_item (void *cls,
647 const GNUNET_HashCode * key,
650 enum GNUNET_BLOCK_Type type,
653 struct GNUNET_TIME_Absolute
654 expiration, uint64_t uid)
656 struct GNUNET_SERVER_Client *client = cls;
657 struct GNUNET_MessageHeader *end;
658 struct DataMessage *dm;
662 /* transmit 'DATA_END' */
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Transmitting `%s' message\n",
668 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
669 end->size = htons(sizeof(struct GNUNET_MessageHeader));
670 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
671 transmit (client, end, NULL, NULL, GNUNET_YES);
672 GNUNET_SERVER_client_drop (client);
675 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
676 dm->header.size = htons(sizeof(struct DataMessage) + size);
677 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
679 dm->size = htonl(size);
680 dm->type = htonl(type);
681 dm->priority = htonl(priority);
682 dm->anonymity = htonl(anonymity);
683 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
684 dm->uid = GNUNET_htonll(uid);
686 memcpy (&dm[1], data, size);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Transmitting `%s' message for `%s' of type %u\n",
694 GNUNET_STATISTICS_update (stats,
695 gettext_noop ("# results found"),
698 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
704 * Handle RESERVE-message.
707 * @param client identification of the client
708 * @param message the actual message
711 handle_reserve (void *cls,
712 struct GNUNET_SERVER_Client *client,
713 const struct GNUNET_MessageHeader *message)
716 * Static counter to produce reservation identifiers.
718 static int reservation_gen;
720 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
721 struct ReservationList *e;
722 unsigned long long used;
723 unsigned long long req;
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Processing `%s' request\n",
732 amount = GNUNET_ntohll(msg->amount);
733 entries = ntohl(msg->entries);
734 used = plugin->api->get_size (plugin->api->cls) + reserved;
735 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
736 if (used + req > quota)
739 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
741 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
745 if (cache_size < req)
747 /* TODO: document this in the FAQ; essentially, if this
748 message happens, the insertion request could be blocked
749 by less-important content from migration because it is
750 larger than 1/8th of the overall available space, and
751 we only reserve 1/8th for "fresh" insertions */
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
756 transmit_status (client, 0,
757 gettext_noop ("Insufficient space to satisfy request and "
758 "requested amount is larger than cache size"));
762 transmit_status (client, 0,
763 gettext_noop ("Insufficient space to satisfy request"));
768 GNUNET_STATISTICS_set (stats,
769 gettext_noop ("# reserved"),
772 e = GNUNET_malloc (sizeof(struct ReservationList));
773 e->next = reservations;
777 e->entries = entries;
778 e->rid = ++reservation_gen;
779 if (reservation_gen < 0)
780 reservation_gen = 0; /* wrap around */
781 transmit_status (client, e->rid, NULL);
786 * Handle RELEASE_RESERVE-message.
789 * @param client identification of the client
790 * @param message the actual message
793 handle_release_reserve (void *cls,
794 struct GNUNET_SERVER_Client *client,
795 const struct GNUNET_MessageHeader *message)
797 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
798 struct ReservationList *pos;
799 struct ReservationList *prev;
800 struct ReservationList *next;
801 int rid = ntohl(msg->rid);
802 unsigned long long rem;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Processing `%s' request\n",
811 while (NULL != (pos = next))
820 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
821 GNUNET_assert (reserved >= rem);
823 GNUNET_STATISTICS_set (stats,
824 gettext_noop ("# reserved"),
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Returning %llu remaining reserved bytes to storage pool\n",
833 transmit_status (client, GNUNET_OK, NULL);
839 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
844 * Check that the given message is a valid data message.
846 * @return NULL if the message is not well-formed, otherwise the message
848 static const struct DataMessage *
849 check_data (const struct GNUNET_MessageHeader *message)
853 const struct DataMessage *dm;
855 size = ntohs(message->size);
856 if (size < sizeof(struct DataMessage))
861 dm = (const struct DataMessage *) message;
862 dsize = ntohl(dm->size);
863 if (size != dsize + sizeof(struct DataMessage))
873 * Context for a put request used to see if the content is
879 * Client to notify on completion.
881 struct GNUNET_SERVER_Client *client;
884 * Did we find the data already in the database?
888 /* followed by the 'struct DataMessage' */
893 * Actually put the data message.
896 execute_put (struct GNUNET_SERVER_Client *client,
897 const struct DataMessage *dm)
903 size = ntohl(dm->size);
905 ret = plugin->api->put (plugin->api->cls,
911 ntohl(dm->anonymity),
912 GNUNET_TIME_absolute_ntoh(dm->expiration),
914 if (GNUNET_OK == ret)
916 GNUNET_STATISTICS_update (stats,
917 gettext_noop ("# bytes stored"),
920 GNUNET_CONTAINER_bloomfilter_add (filter,
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Successfully stored %u bytes of type %u under key `%s'\n",
927 GNUNET_h2s (&dm->key));
930 transmit_status (client,
931 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
933 GNUNET_free_non_null (msg);
934 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
936 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
937 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
938 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
939 (unsigned long long) (quota - reserved - cache_size),
940 (unsigned long long) plugin->api->get_size (plugin->api->cls));
941 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
948 * Function that will check if the given datastore entry
949 * matches the put and if none match executes the put.
951 * @param cls closure, pointer to the client (of type 'struct PutContext').
952 * @param next_cls closure to use to ask for the next item
953 * @param key key for the content
954 * @param size number of bytes in data
955 * @param data content stored
956 * @param type type of the content
957 * @param priority priority of the content
958 * @param anonymity anonymity-level for the content
959 * @param expiration expiration time for the content
960 * @param uid unique identifier for the datum;
961 * maybe 0 if no unique identifier is available
963 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
964 * GNUNET_NO to delete the item and continue (if supported)
967 check_present (void *cls,
969 const GNUNET_HashCode * key,
972 enum GNUNET_BLOCK_Type type,
975 struct GNUNET_TIME_Absolute
976 expiration, uint64_t uid)
978 struct PutContext *pc = cls;
979 const struct DataMessage *dm;
981 dm = (const struct DataMessage*) &pc[1];
984 if (pc->is_present == GNUNET_YES)
985 transmit_status (pc->client, GNUNET_OK, NULL);
987 execute_put (pc->client, dm);
988 GNUNET_SERVER_client_drop (pc->client);
990 return GNUNET_SYSERR;
992 if ( (size == ntohl(dm->size)) &&
993 (0 == memcmp (&dm[1],
997 pc->is_present = GNUNET_YES;
998 plugin->api->next_request (next_cls, GNUNET_YES);
1002 plugin->api->next_request (next_cls, GNUNET_NO);
1009 * Handle PUT-message.
1011 * @param cls closure
1012 * @param client identification of the client
1013 * @param message the actual message
1016 handle_put (void *cls,
1017 struct GNUNET_SERVER_Client *client,
1018 const struct GNUNET_MessageHeader *message)
1020 const struct DataMessage *dm = check_data (message);
1022 struct ReservationList *pos;
1023 struct PutContext *pc;
1026 if ( (dm == NULL) ||
1027 (ntohl(dm->type) == 0) )
1030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Processing `%s' request for `%s' of type %u\n",
1037 GNUNET_h2s (&dm->key),
1040 rid = ntohl(dm->rid);
1041 size = ntohl(dm->size);
1045 while ( (NULL != pos) &&
1048 GNUNET_break (pos != NULL);
1051 GNUNET_break (pos->entries > 0);
1052 GNUNET_break (pos->amount > size);
1054 pos->amount -= size;
1055 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1056 GNUNET_STATISTICS_set (stats,
1057 gettext_noop ("# reserved"),
1062 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1065 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1066 pc->client = client;
1067 GNUNET_SERVER_client_keep (client);
1068 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1069 plugin->api->get (plugin->api->cls,
1077 execute_put (client, dm);
1082 * Handle GET-message.
1084 * @param cls closure
1085 * @param client identification of the client
1086 * @param message the actual message
1089 handle_get (void *cls,
1090 struct GNUNET_SERVER_Client *client,
1091 const struct GNUNET_MessageHeader *message)
1093 const struct GetMessage *msg;
1096 size = ntohs(message->size);
1097 if ( (size != sizeof(struct GetMessage)) &&
1098 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1104 msg = (const struct GetMessage*) message;
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1107 "Processing `%s' request for `%s' of type %u\n",
1109 GNUNET_h2s (&msg->key),
1112 GNUNET_STATISTICS_update (stats,
1113 gettext_noop ("# GET requests received"),
1116 GNUNET_SERVER_client_keep (client);
1117 if ( (size == sizeof(struct GetMessage)) &&
1118 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1121 /* don't bother database... */
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1126 GNUNET_h2s (&msg->key));
1128 GNUNET_STATISTICS_update (stats,
1129 gettext_noop ("# requests filtered by bloomfilter"),
1132 transmit_item (client,
1133 NULL, NULL, 0, NULL, 0, 0, 0,
1134 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1137 plugin->api->get (plugin->api->cls,
1138 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1147 * Handle UPDATE-message.
1149 * @param cls closure
1150 * @param client identification of the client
1151 * @param message the actual message
1154 handle_update (void *cls,
1155 struct GNUNET_SERVER_Client *client,
1156 const struct GNUNET_MessageHeader *message)
1158 const struct UpdateMessage *msg;
1162 GNUNET_STATISTICS_update (stats,
1163 gettext_noop ("# UPDATE requests received"),
1166 msg = (const struct UpdateMessage*) message;
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Processing `%s' request for %llu\n",
1172 (unsigned long long) GNUNET_ntohll (msg->uid));
1174 ret = plugin->api->update (plugin->api->cls,
1175 GNUNET_ntohll(msg->uid),
1176 (int32_t) ntohl(msg->priority),
1177 GNUNET_TIME_absolute_ntoh(msg->expiration),
1179 transmit_status (client, ret, emsg);
1180 GNUNET_free_non_null (emsg);
1185 * Handle GET_RANDOM-message.
1187 * @param cls closure
1188 * @param client identification of the client
1189 * @param message the actual message
1192 handle_get_random (void *cls,
1193 struct GNUNET_SERVER_Client *client,
1194 const struct GNUNET_MessageHeader *message)
1197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198 "Processing `%s' request\n",
1201 GNUNET_STATISTICS_update (stats,
1202 gettext_noop ("# GET RANDOM requests received"),
1205 GNUNET_SERVER_client_keep (client);
1206 plugin->api->iter_migration_order (plugin->api->cls,
1214 * Context for the 'remove_callback'.
1216 struct RemoveContext
1219 * Client for whom we're doing the remvoing.
1221 struct GNUNET_SERVER_Client *client;
1224 * GNUNET_YES if we managed to remove something.
1231 * Callback function that will cause the item that is passed
1232 * in to be deleted (by returning GNUNET_NO).
1235 remove_callback (void *cls,
1237 const GNUNET_HashCode * key,
1240 enum GNUNET_BLOCK_Type type,
1243 struct GNUNET_TIME_Absolute
1244 expiration, uint64_t uid)
1246 struct RemoveContext *rc = cls;
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "No further matches for `%s' request.\n",
1255 if (GNUNET_YES == rc->found)
1256 transmit_status (rc->client, GNUNET_OK, NULL);
1258 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1259 GNUNET_SERVER_client_drop (rc->client);
1261 return GNUNET_OK; /* last item */
1263 rc->found = GNUNET_YES;
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1266 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1267 (unsigned long long) uid,
1272 GNUNET_STATISTICS_update (stats,
1273 gettext_noop ("# bytes removed (explicit request)"),
1276 GNUNET_CONTAINER_bloomfilter_remove (filter,
1278 plugin->api->next_request (next_cls, GNUNET_YES);
1284 * Handle REMOVE-message.
1286 * @param cls closure
1287 * @param client identification of the client
1288 * @param message the actual message
1291 handle_remove (void *cls,
1292 struct GNUNET_SERVER_Client *client,
1293 const struct GNUNET_MessageHeader *message)
1295 const struct DataMessage *dm = check_data (message);
1296 GNUNET_HashCode vhash;
1297 struct RemoveContext *rc;
1302 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Processing `%s' request for `%s' of type %u\n",
1309 GNUNET_h2s (&dm->key),
1312 GNUNET_STATISTICS_update (stats,
1313 gettext_noop ("# REMOVE requests received"),
1316 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1317 GNUNET_SERVER_client_keep (client);
1318 rc->client = client;
1319 GNUNET_CRYPTO_hash (&dm[1],
1322 plugin->api->get (plugin->api->cls,
1332 * Handle DROP-message.
1334 * @param cls closure
1335 * @param client identification of the client
1336 * @param message the actual message
1339 handle_drop (void *cls,
1340 struct GNUNET_SERVER_Client *client,
1341 const struct GNUNET_MessageHeader *message)
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1345 "Processing `%s' request\n",
1348 plugin->api->drop (plugin->api->cls);
1349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1354 * Load the datastore plugin.
1356 static struct DatastorePlugin *
1359 struct DatastorePlugin *ret;
1364 GNUNET_CONFIGURATION_get_value_string (cfg,
1365 "DATASTORE", "DATABASE", &name))
1367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1368 _("No `%s' specified for `%s' in configuration!\n"),
1373 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1375 ret->env.sched = sched;
1376 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1377 _("Loading `%s' datastore plugin\n"), name);
1378 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1379 ret->short_name = name;
1380 ret->lib_name = libname;
1381 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1382 if (ret->api == NULL)
1384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1385 _("Failed to load datastore plugin for `%s'\n"), name);
1386 GNUNET_free (ret->short_name);
1387 GNUNET_free (libname);
1396 * Function called when the service shuts
1397 * down. Unloads our datastore plugin.
1399 * @param plug plugin to unload
1402 unload_plugin (struct DatastorePlugin *plug)
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 "Datastore service is unloading plugin...\n");
1408 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1409 GNUNET_free (plug->lib_name);
1410 GNUNET_free (plug->short_name);
1416 * Final task run after shutdown. Unloads plugins and disconnects us from
1420 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1422 unload_plugin (plugin);
1426 GNUNET_CONTAINER_bloomfilter_free (filter);
1431 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1438 * Last task run during shutdown. Disconnects us from
1439 * the transport and core.
1442 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1444 struct TransmitCallbackContext *tcc;
1446 cleaning_done = GNUNET_YES;
1447 while (NULL != (tcc = tcc_head))
1449 GNUNET_CONTAINER_DLL_remove (tcc_head,
1452 if (tcc->th != NULL)
1454 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1455 GNUNET_SERVER_client_drop (tcc->client);
1457 if (NULL != tcc->tc)
1458 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1459 GNUNET_free (tcc->msg);
1462 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1464 GNUNET_SCHEDULER_cancel (sched,
1466 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1468 GNUNET_SCHEDULER_add_continuation (sched,
1471 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1476 * Function that removes all active reservations made
1477 * by the given client and releases the space for other
1480 * @param cls closure
1481 * @param client identification of the client
1484 cleanup_reservations (void *cls,
1485 struct GNUNET_SERVER_Client
1488 struct ReservationList *pos;
1489 struct ReservationList *prev;
1490 struct ReservationList *next;
1499 if (pos->client == client)
1502 reservations = next;
1505 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1514 GNUNET_STATISTICS_set (stats,
1515 gettext_noop ("# reserved"),
1522 * Process datastore requests.
1524 * @param cls closure
1525 * @param s scheduler to use
1526 * @param server the initialized server
1527 * @param c configuration to use
1531 struct GNUNET_SCHEDULER_Handle *s,
1532 struct GNUNET_SERVER_Handle *server,
1533 const struct GNUNET_CONFIGURATION_Handle *c)
1535 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1536 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1537 sizeof(struct ReserveMessage) },
1538 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1539 sizeof(struct ReleaseReserveMessage) },
1540 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1541 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1542 sizeof (struct UpdateMessage) },
1543 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1544 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1545 sizeof(struct GNUNET_MessageHeader) },
1546 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1547 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1548 sizeof(struct GNUNET_MessageHeader) },
1552 unsigned int bf_size;
1557 GNUNET_CONFIGURATION_get_value_number (cfg,
1558 "DATASTORE", "QUOTA", "a))
1560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1561 _("No `%s' specified for `%s' in configuration!\n"),
1566 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1567 GNUNET_STATISTICS_set (stats,
1568 gettext_noop ("# quota"),
1571 cache_size = quota / 8; /* Or should we make this an option? */
1572 GNUNET_STATISTICS_set (stats,
1573 gettext_noop ("# cache size"),
1576 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1579 GNUNET_CONFIGURATION_get_value_filename (cfg,
1584 GNUNET_DISK_directory_create_for_file (fn)) )
1586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1587 _("Could not use specified filename `%s' for bloomfilter.\n"),
1588 fn != NULL ? fn : "");
1589 GNUNET_free_non_null (fn);
1592 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1593 GNUNET_free_non_null (fn);
1596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1597 _("Failed to initialize bloomfilter.\n"));
1600 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1605 plugin = load_plugin ();
1608 GNUNET_CONTAINER_bloomfilter_free (filter);
1612 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1617 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1618 GNUNET_SERVER_add_handlers (server, handlers);
1620 = GNUNET_SCHEDULER_add_with_priority (sched,
1621 GNUNET_SCHEDULER_PRIORITY_IDLE,
1622 &delete_expired, NULL);
1623 GNUNET_SCHEDULER_add_delayed (sched,
1624 GNUNET_TIME_UNIT_FOREVER_REL,
1625 &cleaning_task, NULL);
1631 * The main function for the datastore service.
1633 * @param argc number of arguments from the command line
1634 * @param argv command line arguments
1635 * @return 0 ok, 1 on error
1638 main (int argc, char *const *argv)
1643 GNUNET_SERVICE_run (argc,
1646 GNUNET_SERVICE_OPTION_NONE,
1647 &run, NULL)) ? 0 : 1;
1652 /* end of gnunet-service-datastore.c */