2 This file is part of GNUnet
3 Copyright (C) 2004-2013 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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file datastore/datastore_api.c
23 * @brief Management for the datastore for files stored on a GNUnet node. Implements
24 * a priority queue for requests (with timeouts).
25 * @author Christian Grothoff
28 #include "gnunet_arm_service.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_datastore_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "datastore.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "datastore-api",__VA_ARGS__)
37 * Collect an instane number of statistics? May cause excessive IPC.
39 #define INSANE_STATISTICS GNUNET_NO
42 * If a client stopped asking for more results, how many more do
43 * we receive from the DB before killing the connection? Trade-off
44 * between re-doing TCP handshakes and (needlessly) receiving
47 #define MAX_EXCESS_RESULTS 8
50 * Context for processing status messages.
55 * Continuation to call with the status.
57 GNUNET_DATASTORE_ContinuationWithStatus cont;
68 * Context for processing result messages.
73 * Function to call with the result.
75 GNUNET_DATASTORE_DatumProcessor proc;
86 * Context for a queue operation.
91 struct StatusContext sc;
93 struct ResultContext rc;
100 * Entry in our priority queue.
102 struct GNUNET_DATASTORE_QueueEntry
106 * This is a linked list.
108 struct GNUNET_DATASTORE_QueueEntry *next;
111 * This is a linked list.
113 struct GNUNET_DATASTORE_QueueEntry *prev;
116 * Handle to the master context.
118 struct GNUNET_DATASTORE_Handle *h;
121 * Response processor (NULL if we are not waiting for a response).
122 * This struct should be used for the closure, function-specific
123 * arguments can be passed via 'qc'.
125 GNUNET_CLIENT_MessageHandler response_proc;
128 * Function to call after transmission of the request.
130 GNUNET_DATASTORE_ContinuationWithStatus cont;
133 * Closure for 'cont'.
138 * Context for the operation.
140 union QueueContext qc;
143 * Task for timeout signalling.
145 struct GNUNET_SCHEDULER_Task * task;
148 * Timeout for the current operation.
150 struct GNUNET_TIME_Absolute timeout;
153 * Priority in the queue.
155 unsigned int priority;
158 * Maximum allowed length of queue (otherwise
159 * this request should be discarded).
161 unsigned int max_queue;
164 * Number of bytes in the request message following
165 * this struct. 32-bit value for nicer memory
166 * access (and overall struct alignment).
168 uint32_t message_size;
171 * Has this message been transmitted to the service?
172 * Only ever GNUNET_YES for the head of the queue.
173 * Note that the overall struct should end at a
174 * multiple of 64 bits.
181 * Handle to the datastore service.
183 struct GNUNET_DATASTORE_Handle
189 const struct GNUNET_CONFIGURATION_Handle *cfg;
192 * Current connection to the datastore service.
194 struct GNUNET_CLIENT_Connection *client;
197 * Handle for statistics.
199 struct GNUNET_STATISTICS_Handle *stats;
202 * Current transmit handle.
204 struct GNUNET_CLIENT_TransmitHandle *th;
207 * Current head of priority queue.
209 struct GNUNET_DATASTORE_QueueEntry *queue_head;
212 * Current tail of priority queue.
214 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
217 * Task for trying to reconnect.
219 struct GNUNET_SCHEDULER_Task * reconnect_task;
222 * How quickly should we retry? Used for exponential back-off on
225 struct GNUNET_TIME_Relative retry_time;
228 * Number of entries in the queue.
230 unsigned int queue_size;
233 * Number of results we're receiving for the current query
234 * after application stopped to care. Used to determine when
235 * to reset the connection.
237 unsigned int result_count;
240 * Are we currently trying to receive from the service?
245 * We should ignore the next message(s) from the service.
247 unsigned int skip_next_messages;
254 * Connect to the datastore service.
256 * @param cfg configuration to use
257 * @return handle to use to access the service
259 struct GNUNET_DATASTORE_Handle *
260 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
262 struct GNUNET_CLIENT_Connection *c;
263 struct GNUNET_DATASTORE_Handle *h;
265 c = GNUNET_CLIENT_connect ("datastore", cfg);
267 return NULL; /* oops */
268 h = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_Handle) +
269 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
272 h->stats = GNUNET_STATISTICS_create ("datastore-api", cfg);
278 * Task used by 'transmit_drop' to disconnect the datastore.
280 * @param cls the datastore handle
281 * @param tc scheduler context
284 disconnect_after_drop (void *cls,
285 const struct GNUNET_SCHEDULER_TaskContext *tc)
287 struct GNUNET_DATASTORE_Handle *h = cls;
289 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
294 * Transmit DROP message to datastore service.
296 * @param cls the `struct GNUNET_DATASTORE_Handle`
297 * @param size number of bytes that can be copied to @a buf
298 * @param buf where to copy the drop message
299 * @return number of bytes written to @a buf
302 transmit_drop (void *cls, size_t size, void *buf)
304 struct GNUNET_DATASTORE_Handle *h = cls;
305 struct GNUNET_MessageHeader *hdr;
309 LOG (GNUNET_ERROR_TYPE_WARNING,
310 _("Failed to transmit request to drop database.\n"));
311 GNUNET_SCHEDULER_add_now (&disconnect_after_drop, h);
314 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
316 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
317 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
318 GNUNET_SCHEDULER_add_now (&disconnect_after_drop, h);
319 return sizeof (struct GNUNET_MessageHeader);
324 * Disconnect from the datastore service (and free
325 * associated resources).
327 * @param h handle to the datastore
328 * @param drop set to #GNUNET_YES to delete all data in datastore (!)
331 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
334 struct GNUNET_DATASTORE_QueueEntry *qe;
336 LOG (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
339 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
342 if (NULL != h->client)
344 GNUNET_CLIENT_disconnect (h->client);
347 if (NULL != h->reconnect_task)
349 GNUNET_SCHEDULER_cancel (h->reconnect_task);
350 h->reconnect_task = NULL;
352 while (NULL != (qe = h->queue_head))
354 GNUNET_assert (NULL != qe->response_proc);
355 qe->response_proc (h, NULL);
357 if (GNUNET_YES == drop)
359 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
360 if (NULL != h->client)
363 GNUNET_CLIENT_notify_transmit_ready (h->client,
365 GNUNET_MessageHeader),
366 GNUNET_TIME_UNIT_SECONDS,
370 GNUNET_CLIENT_disconnect (h->client);
375 GNUNET_STATISTICS_destroy (h->stats,
383 * A request has timed out (before being transmitted to the service).
385 * @param cls the `struct GNUNET_DATASTORE_QueueEntry`
386 * @param tc scheduler context
389 timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
391 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
392 struct GNUNET_DATASTORE_Handle *h = qe->h;
394 GNUNET_STATISTICS_update (h->stats,
395 gettext_noop ("# queue entry timeouts"), 1,
398 GNUNET_assert (GNUNET_NO == qe->was_transmitted);
399 LOG (GNUNET_ERROR_TYPE_DEBUG,
400 "Timeout of request in datastore queue\n");
401 /* response_proc's expect request at the head of the queue! */
402 GNUNET_CONTAINER_DLL_remove (h->queue_head,
405 GNUNET_CONTAINER_DLL_insert (h->queue_head,
408 GNUNET_assert (h->queue_head == qe);
409 qe->response_proc (qe->h, NULL);
414 * Create a new entry for our priority queue (and possibly discard other entires if
415 * the queue is getting too long).
417 * @param h handle to the datastore
418 * @param msize size of the message to queue
419 * @param queue_priority priority of the entry
420 * @param max_queue_size at what queue size should this request be dropped
421 * (if other requests of higher priority are in the queue)
422 * @param timeout timeout for the operation
423 * @param response_proc function to call with replies (can be NULL)
424 * @param qc client context (NOT a closure for @a response_proc)
425 * @return NULL if the queue is full
427 static struct GNUNET_DATASTORE_QueueEntry *
428 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
430 unsigned int queue_priority,
431 unsigned int max_queue_size,
432 struct GNUNET_TIME_Relative timeout,
433 GNUNET_CLIENT_MessageHandler response_proc,
434 const union QueueContext *qc)
436 struct GNUNET_DATASTORE_QueueEntry *ret;
437 struct GNUNET_DATASTORE_QueueEntry *pos;
442 while ((pos != NULL) && (c < max_queue_size) &&
443 (pos->priority >= queue_priority))
448 if (c >= max_queue_size)
450 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue overflows"), 1,
454 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
456 ret->response_proc = response_proc;
458 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
459 ret->priority = queue_priority;
460 ret->max_queue = max_queue_size;
461 ret->message_size = msize;
462 ret->was_transmitted = GNUNET_NO;
465 /* append at the tail */
471 /* do not insert at HEAD if HEAD query was already
472 * transmitted and we are still receiving replies! */
473 if ((pos == NULL) && (h->queue_head->was_transmitted))
477 #if INSANE_STATISTICS
478 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue entries created"),
481 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
483 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
484 for (pos = ret->next; NULL != pos; pos = pos->next)
486 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
488 GNUNET_assert (NULL != pos->response_proc);
489 /* move 'pos' element to head so that it will be
490 * killed on 'NULL' call below */
491 LOG (GNUNET_ERROR_TYPE_DEBUG,
492 "Dropping request from datastore queue\n");
493 /* response_proc's expect request at the head of the queue! */
494 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, pos);
495 GNUNET_CONTAINER_DLL_insert (h->queue_head, h->queue_tail, pos);
496 GNUNET_STATISTICS_update (h->stats,
498 ("# Requests dropped from datastore queue"), 1,
500 GNUNET_assert (h->queue_head == pos);
501 pos->response_proc (h, NULL);
510 * Process entries in the queue (or do nothing if we are already
513 * @param h handle to the datastore
516 process_queue (struct GNUNET_DATASTORE_Handle *h);
520 * Try reconnecting to the datastore service.
522 * @param cls the `struct GNUNET_DATASTORE_Handle`
523 * @param tc scheduler context
526 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct GNUNET_DATASTORE_Handle *h = cls;
530 h->retry_time = GNUNET_TIME_STD_BACKOFF (h->retry_time);
531 h->reconnect_task = NULL;
532 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
533 if (h->client == NULL)
535 LOG (GNUNET_ERROR_TYPE_ERROR, "DATASTORE reconnect failed (fatally)\n");
538 GNUNET_STATISTICS_update (h->stats,
540 ("# datastore connections (re)created"), 1,
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
548 * Disconnect from the service and then try reconnecting to the datastore service
551 * @param h handle to datastore to disconnect and reconnect
554 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
556 if (NULL == h->client)
558 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 "Client NULL in disconnect, will not try to reconnect\n");
562 GNUNET_CLIENT_disconnect (h->client);
563 h->skip_next_messages = 0;
566 GNUNET_SCHEDULER_add_delayed (h->retry_time, &try_reconnect, h);
571 * Function called whenever we receive a message from
572 * the service. Calls the appropriate handler.
574 * @param cls the `struct GNUNET_DATASTORE_Handle`
575 * @param msg the received message
578 receive_cb (void *cls,
579 const struct GNUNET_MessageHeader *msg)
581 struct GNUNET_DATASTORE_Handle *h = cls;
582 struct GNUNET_DATASTORE_QueueEntry *qe;
584 h->in_receive = GNUNET_NO;
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "Receiving reply from datastore\n");
587 if (h->skip_next_messages > 0)
589 h->skip_next_messages--;
593 if (NULL == (qe = h->queue_head))
599 qe->response_proc (h, msg);
604 * Transmit request from queue to datastore service.
606 * @param cls the `struct GNUNET_DATASTORE_Handle`
607 * @param size number of bytes that can be copied to @a buf
608 * @param buf where to copy the drop message
609 * @return number of bytes written to @a buf
612 transmit_request (void *cls,
616 struct GNUNET_DATASTORE_Handle *h = cls;
617 struct GNUNET_DATASTORE_QueueEntry *qe;
621 if (NULL == (qe = h->queue_head))
622 return 0; /* no entry in queue */
625 LOG (GNUNET_ERROR_TYPE_DEBUG,
626 "Failed to transmit request to DATASTORE.\n");
627 GNUNET_STATISTICS_update (h->stats,
628 gettext_noop ("# transmission request failures"),
633 if (size < (msize = qe->message_size))
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Transmitting %u byte request to DATASTORE\n",
641 memcpy (buf, &qe[1], msize);
642 qe->was_transmitted = GNUNET_YES;
643 GNUNET_SCHEDULER_cancel (qe->task);
645 GNUNET_assert (GNUNET_NO == h->in_receive);
646 h->in_receive = GNUNET_YES;
647 GNUNET_CLIENT_receive (h->client,
649 GNUNET_TIME_absolute_get_remaining (qe->timeout));
650 #if INSANE_STATISTICS
651 GNUNET_STATISTICS_update (h->stats,
652 gettext_noop ("# bytes sent to datastore"), msize,
660 * Process entries in the queue (or do nothing if we are already
663 * @param h handle to the datastore
666 process_queue (struct GNUNET_DATASTORE_Handle *h)
668 struct GNUNET_DATASTORE_QueueEntry *qe;
670 if (NULL == (qe = h->queue_head))
672 /* no entry in queue */
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
677 if (GNUNET_YES == qe->was_transmitted)
679 /* waiting for replies */
680 LOG (GNUNET_ERROR_TYPE_DEBUG,
681 "Head request already transmitted\n");
686 /* request pending */
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Pending transmission request\n");
691 if (NULL == h->client)
693 /* waiting for reconnect */
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 if (GNUNET_YES == h->in_receive)
700 /* wait for response to previous query */
703 LOG (GNUNET_ERROR_TYPE_DEBUG,
704 "Queueing %u byte request to DATASTORE\n",
707 = GNUNET_CLIENT_notify_transmit_ready (h->client, qe->message_size,
708 GNUNET_TIME_absolute_get_remaining (qe->timeout),
710 &transmit_request, h);
711 GNUNET_assert (GNUNET_NO == h->in_receive);
712 GNUNET_break (NULL != h->th);
717 * Dummy continuation used to do nothing (but be non-zero).
720 * @param result result
721 * @param min_expiration expiration time
722 * @param emsg error message
725 drop_status_cont (void *cls, int32_t result,
726 struct GNUNET_TIME_Absolute min_expiration,
734 * Free a queue entry. Removes the given entry from the
735 * queue and releases associated resources. Does NOT
738 * @param qe entry to free.
741 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
743 struct GNUNET_DATASTORE_Handle *h = qe->h;
745 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
746 if (qe->task != NULL)
748 GNUNET_SCHEDULER_cancel (qe->task);
752 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
758 * Type of a function to call when we receive a message
762 * @param msg message received, NULL on timeout or fatal error
765 process_status_message (void *cls,
766 const struct GNUNET_MessageHeader *msg)
768 struct GNUNET_DATASTORE_Handle *h = cls;
769 struct GNUNET_DATASTORE_QueueEntry *qe;
770 struct StatusContext rc;
771 const struct StatusMessage *sm;
776 if (NULL == (qe = h->queue_head))
785 was_transmitted = qe->was_transmitted;
786 free_queue_entry (qe);
787 if (was_transmitted == GNUNET_YES)
792 rc.cont (rc.cont_cls, GNUNET_SYSERR,
793 GNUNET_TIME_UNIT_ZERO_ABS,
794 _("Failed to receive status response from database."));
797 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
798 free_queue_entry (qe);
799 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
800 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
803 h->retry_time = GNUNET_TIME_UNIT_ZERO;
806 rc.cont (rc.cont_cls, GNUNET_SYSERR,
807 GNUNET_TIME_UNIT_ZERO_ABS,
808 _("Error reading response from datastore service"));
811 sm = (const struct StatusMessage *) msg;
812 status = ntohl (sm->status);
814 if (ntohs (msg->size) > sizeof (struct StatusMessage))
816 emsg = (const char *) &sm[1];
817 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
820 emsg = _("Invalid error message received from datastore service");
823 if ((status == GNUNET_SYSERR) && (emsg == NULL))
826 emsg = _("Invalid error message received from datastore service");
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received status %d/%s\n", (int) status, emsg);
829 GNUNET_STATISTICS_update (h->stats,
830 gettext_noop ("# status messages received"), 1,
832 h->retry_time = GNUNET_TIME_UNIT_ZERO;
835 rc.cont (rc.cont_cls, status,
836 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
842 * Store an item in the datastore. If the item is already present,
843 * the priorities are summed up and the higher expiration time and
844 * lower anonymity level is used.
846 * @param h handle to the datastore
847 * @param rid reservation ID to use (from "reserve"); use 0 if no
848 * prior reservation was made
849 * @param key key for the value
850 * @param size number of bytes in data
851 * @param data content stored
852 * @param type type of the content
853 * @param priority priority of the content
854 * @param anonymity anonymity-level for the content
855 * @param replication how often should the content be replicated to other peers?
856 * @param expiration expiration time for the content
857 * @param queue_priority ranking of this request in the priority queue
858 * @param max_queue_size at what queue size should this request be dropped
859 * (if other requests of higher priority are in the queue)
860 * @param timeout timeout for the operation
861 * @param cont continuation to call when done
862 * @param cont_cls closure for @a cont
863 * @return NULL if the entry was not queued, otherwise a handle that can be used to
864 * cancel; note that even if NULL is returned, the callback will be invoked
865 * (or rather, will already have been invoked)
867 struct GNUNET_DATASTORE_QueueEntry *
868 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
870 const struct GNUNET_HashCode *key,
873 enum GNUNET_BLOCK_Type type,
876 uint32_t replication,
877 struct GNUNET_TIME_Absolute expiration,
878 unsigned int queue_priority,
879 unsigned int max_queue_size,
880 struct GNUNET_TIME_Relative timeout,
881 GNUNET_DATASTORE_ContinuationWithStatus cont,
884 struct GNUNET_DATASTORE_QueueEntry *qe;
885 struct DataMessage *dm;
887 union QueueContext qc;
889 LOG (GNUNET_ERROR_TYPE_DEBUG,
890 "Asked to put %u bytes of data under key `%s' for %s\n", size,
892 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
894 msize = sizeof (struct DataMessage) + size;
895 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
897 qc.sc.cont_cls = cont_cls;
898 qe = make_queue_entry (h,
903 &process_status_message, &qc);
906 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for PUT\n");
909 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# PUT requests executed"),
911 dm = (struct DataMessage *) &qe[1];
912 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
913 dm->header.size = htons (msize);
914 dm->rid = htonl (rid);
915 dm->size = htonl ((uint32_t) size);
916 dm->type = htonl (type);
917 dm->priority = htonl (priority);
918 dm->anonymity = htonl (anonymity);
919 dm->replication = htonl (replication);
920 dm->reserved = htonl (0);
921 dm->uid = GNUNET_htonll (0);
922 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
924 memcpy (&dm[1], data, size);
931 * Reserve space in the datastore. This function should be used
932 * to avoid "out of space" failures during a longer sequence of "put"
933 * operations (for example, when a file is being inserted).
935 * @param h handle to the datastore
936 * @param amount how much space (in bytes) should be reserved (for content only)
937 * @param entries how many entries will be created (to calculate per-entry overhead)
938 * @param cont continuation to call when done; "success" will be set to
939 * a positive reservation value if space could be reserved.
940 * @param cont_cls closure for @a cont
941 * @return NULL if the entry was not queued, otherwise a handle that can be used to
942 * cancel; note that even if NULL is returned, the callback will be invoked
943 * (or rather, will already have been invoked)
945 struct GNUNET_DATASTORE_QueueEntry *
946 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
948 GNUNET_DATASTORE_ContinuationWithStatus cont,
951 struct GNUNET_DATASTORE_QueueEntry *qe;
952 struct ReserveMessage *rm;
953 union QueueContext qc;
956 cont = &drop_status_cont;
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Asked to reserve %llu bytes of data and %u entries\n",
959 (unsigned long long) amount, (unsigned int) entries);
961 qc.sc.cont_cls = cont_cls;
962 qe = make_queue_entry (h,
963 sizeof (struct ReserveMessage),
966 GNUNET_TIME_UNIT_FOREVER_REL,
967 &process_status_message, &qc);
970 LOG (GNUNET_ERROR_TYPE_DEBUG,
971 "Could not create queue entry to reserve\n");
974 GNUNET_STATISTICS_update (h->stats,
975 gettext_noop ("# RESERVE requests executed"), 1,
977 rm = (struct ReserveMessage *) &qe[1];
978 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
979 rm->header.size = htons (sizeof (struct ReserveMessage));
980 rm->entries = htonl (entries);
981 rm->amount = GNUNET_htonll (amount);
988 * Signal that all of the data for which a reservation was made has
989 * been stored and that whatever excess space might have been reserved
990 * can now be released.
992 * @param h handle to the datastore
993 * @param rid reservation ID (value of "success" in original continuation
994 * from the "reserve" function).
995 * @param queue_priority ranking of this request in the priority queue
996 * @param max_queue_size at what queue size should this request be dropped
997 * (if other requests of higher priority are in the queue)
998 * @param queue_priority ranking of this request in the priority queue
999 * @param max_queue_size at what queue size should this request be dropped
1000 * (if other requests of higher priority are in the queue)
1001 * @param timeout how long to wait at most for a response
1002 * @param cont continuation to call when done
1003 * @param cont_cls closure for @a cont
1004 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1005 * cancel; note that even if NULL is returned, the callback will be invoked
1006 * (or rather, will already have been invoked)
1008 struct GNUNET_DATASTORE_QueueEntry *
1009 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1010 uint32_t rid, unsigned int queue_priority,
1011 unsigned int max_queue_size,
1012 struct GNUNET_TIME_Relative timeout,
1013 GNUNET_DATASTORE_ContinuationWithStatus cont,
1016 struct GNUNET_DATASTORE_QueueEntry *qe;
1017 struct ReleaseReserveMessage *rrm;
1018 union QueueContext qc;
1021 cont = &drop_status_cont;
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
1024 qc.sc.cont_cls = cont_cls;
1025 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
1026 queue_priority, max_queue_size, timeout,
1027 &process_status_message, &qc);
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Could not create queue entry to release reserve\n");
1034 GNUNET_STATISTICS_update (h->stats,
1036 ("# RELEASE RESERVE requests executed"), 1,
1038 rrm = (struct ReleaseReserveMessage *) &qe[1];
1039 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1040 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1041 rrm->rid = htonl (rid);
1048 * Update a value in the datastore.
1050 * @param h handle to the datastore
1051 * @param uid identifier for the value
1052 * @param priority how much to increase the priority of the value
1053 * @param expiration new expiration value should be MAX of existing and this argument
1054 * @param queue_priority ranking of this request in the priority queue
1055 * @param max_queue_size at what queue size should this request be dropped
1056 * (if other requests of higher priority are in the queue)
1057 * @param timeout how long to wait at most for a response
1058 * @param cont continuation to call when done
1059 * @param cont_cls closure for @a cont
1060 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1061 * cancel; note that even if NULL is returned, the callback will be invoked
1062 * (or rather, will already have been invoked)
1064 struct GNUNET_DATASTORE_QueueEntry *
1065 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
1067 struct GNUNET_TIME_Absolute expiration,
1068 unsigned int queue_priority,
1069 unsigned int max_queue_size,
1070 struct GNUNET_TIME_Relative timeout,
1071 GNUNET_DATASTORE_ContinuationWithStatus cont,
1074 struct GNUNET_DATASTORE_QueueEntry *qe;
1075 struct UpdateMessage *um;
1076 union QueueContext qc;
1079 cont = &drop_status_cont;
1080 LOG (GNUNET_ERROR_TYPE_DEBUG,
1081 "Asked to update entry %llu raising priority by %u and expiration to %s\n",
1083 (unsigned int) priority,
1084 GNUNET_STRINGS_absolute_time_to_string (expiration));
1086 qc.sc.cont_cls = cont_cls;
1087 qe = make_queue_entry (h, sizeof (struct UpdateMessage), queue_priority,
1088 max_queue_size, timeout, &process_status_message, &qc);
1091 LOG (GNUNET_ERROR_TYPE_DEBUG,
1092 "Could not create queue entry for UPDATE\n");
1095 GNUNET_STATISTICS_update (h->stats,
1096 gettext_noop ("# UPDATE requests executed"), 1,
1098 um = (struct UpdateMessage *) &qe[1];
1099 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1100 um->header.size = htons (sizeof (struct UpdateMessage));
1101 um->priority = htonl (priority);
1102 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1103 um->uid = GNUNET_htonll (uid);
1110 * Explicitly remove some content from the database.
1111 * The @a cont continuation will be called with `status`
1112 * #GNUNET_OK" if content was removed, #GNUNET_NO
1113 * if no matching entry was found and #GNUNET_SYSERR
1114 * on all other types of errors.
1116 * @param h handle to the datastore
1117 * @param key key for the value
1118 * @param size number of bytes in data
1119 * @param data content stored
1120 * @param queue_priority ranking of this request in the priority queue
1121 * @param max_queue_size at what queue size should this request be dropped
1122 * (if other requests of higher priority are in the queue)
1123 * @param timeout how long to wait at most for a response
1124 * @param cont continuation to call when done
1125 * @param cont_cls closure for @a cont
1126 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1127 * cancel; note that even if NULL is returned, the callback will be invoked
1128 * (or rather, will already have been invoked)
1130 struct GNUNET_DATASTORE_QueueEntry *
1131 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1132 const struct GNUNET_HashCode * key, size_t size,
1133 const void *data, unsigned int queue_priority,
1134 unsigned int max_queue_size,
1135 struct GNUNET_TIME_Relative timeout,
1136 GNUNET_DATASTORE_ContinuationWithStatus cont,
1139 struct GNUNET_DATASTORE_QueueEntry *qe;
1140 struct DataMessage *dm;
1142 union QueueContext qc;
1145 cont = &drop_status_cont;
1146 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to remove %u bytes under key `%s'\n",
1147 size, GNUNET_h2s (key));
1149 qc.sc.cont_cls = cont_cls;
1150 msize = sizeof (struct DataMessage) + size;
1151 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1152 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
1153 &process_status_message, &qc);
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for REMOVE\n");
1159 GNUNET_STATISTICS_update (h->stats,
1160 gettext_noop ("# REMOVE requests executed"), 1,
1162 dm = (struct DataMessage *) &qe[1];
1163 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1164 dm->header.size = htons (msize);
1165 dm->rid = htonl (0);
1166 dm->size = htonl (size);
1167 dm->type = htonl (0);
1168 dm->priority = htonl (0);
1169 dm->anonymity = htonl (0);
1170 dm->uid = GNUNET_htonll (0);
1171 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1173 memcpy (&dm[1], data, size);
1180 * Type of a function to call when we receive a message
1183 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
1184 * @param msg message received, NULL on timeout or fatal error
1187 process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1189 struct GNUNET_DATASTORE_Handle *h = cls;
1190 struct GNUNET_DATASTORE_QueueEntry *qe;
1191 struct ResultContext rc;
1192 const struct DataMessage *dm;
1193 int was_transmitted;
1198 GNUNET_assert (NULL != qe);
1200 was_transmitted = qe->was_transmitted;
1201 free_queue_entry (qe);
1202 if (GNUNET_YES == was_transmitted)
1204 LOG (GNUNET_ERROR_TYPE_DEBUG,
1205 "Failed to receive response from database.\n");
1212 if (NULL != rc.proc)
1213 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1217 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1219 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1222 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1223 free_queue_entry (qe);
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Received end of result set, new queue size is %u\n", h->queue_size);
1226 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1227 h->result_count = 0;
1229 if (NULL != rc.proc)
1230 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1235 GNUNET_assert (NULL != qe);
1237 if (GNUNET_YES != qe->was_transmitted)
1240 free_queue_entry (qe);
1241 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1243 if (rc.proc != NULL)
1244 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1248 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1249 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1250 (ntohs (msg->size) !=
1251 sizeof (struct DataMessage) +
1252 ntohl (((const struct DataMessage *) msg)->size)))
1255 free_queue_entry (qe);
1256 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1258 if (rc.proc != NULL)
1259 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1263 #if INSANE_STATISTICS
1264 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# Results received"), 1,
1267 dm = (const struct DataMessage *) msg;
1268 LOG (GNUNET_ERROR_TYPE_DEBUG,
1269 "Received result %llu with type %u and size %u with key %s\n",
1270 (unsigned long long) GNUNET_ntohll (dm->uid), ntohl (dm->type),
1271 ntohl (dm->size), GNUNET_h2s (&dm->key));
1272 free_queue_entry (qe);
1273 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1275 if (rc.proc != NULL)
1276 rc.proc (rc.proc_cls, &dm->key, ntohl (dm->size), &dm[1], ntohl (dm->type),
1277 ntohl (dm->priority), ntohl (dm->anonymity),
1278 GNUNET_TIME_absolute_ntoh (dm->expiration),
1279 GNUNET_ntohll (dm->uid));
1284 * Get a random value from the datastore for content replication.
1285 * Returns a single, random value among those with the highest
1286 * replication score, lowering positive replication scores by one for
1287 * the chosen value (if only content with a replication score exists,
1288 * a random value is returned and replication scores are not changed).
1290 * @param h handle to the datastore
1291 * @param queue_priority ranking of this request in the priority queue
1292 * @param max_queue_size at what queue size should this request be dropped
1293 * (if other requests of higher priority are in the queue)
1294 * @param timeout how long to wait at most for a response
1295 * @param proc function to call on a random value; it
1296 * will be called once with a value (if available)
1297 * and always once with a value of NULL.
1298 * @param proc_cls closure for @a proc
1299 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1302 struct GNUNET_DATASTORE_QueueEntry *
1303 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1304 unsigned int queue_priority,
1305 unsigned int max_queue_size,
1306 struct GNUNET_TIME_Relative timeout,
1307 GNUNET_DATASTORE_DatumProcessor proc,
1310 struct GNUNET_DATASTORE_QueueEntry *qe;
1311 struct GNUNET_MessageHeader *m;
1312 union QueueContext qc;
1314 GNUNET_assert (NULL != proc);
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "Asked to get replication entry in %s\n",
1317 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
1319 qc.rc.proc_cls = proc_cls;
1320 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1321 queue_priority, max_queue_size, timeout,
1322 &process_result_message, &qc);
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 "Could not create queue entry for GET REPLICATION\n");
1329 GNUNET_STATISTICS_update (h->stats,
1331 ("# GET REPLICATION requests executed"), 1,
1333 m = (struct GNUNET_MessageHeader *) &qe[1];
1334 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1335 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1342 * Get a single zero-anonymity value from the datastore.
1344 * @param h handle to the datastore
1345 * @param offset offset of the result (modulo num-results); set to
1346 * a random 64-bit value initially; then increment by
1347 * one each time; detect that all results have been found by uid
1348 * being again the first uid ever returned.
1349 * @param queue_priority ranking of this request in the priority queue
1350 * @param max_queue_size at what queue size should this request be dropped
1351 * (if other requests of higher priority are in the queue)
1352 * @param timeout how long to wait at most for a response
1353 * @param type allowed type for the operation (never zero)
1354 * @param proc function to call on a random value; it
1355 * will be called once with a value (if available)
1356 * or with NULL if none value exists.
1357 * @param proc_cls closure for @a proc
1358 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1361 struct GNUNET_DATASTORE_QueueEntry *
1362 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1364 unsigned int queue_priority,
1365 unsigned int max_queue_size,
1366 struct GNUNET_TIME_Relative timeout,
1367 enum GNUNET_BLOCK_Type type,
1368 GNUNET_DATASTORE_DatumProcessor proc,
1371 struct GNUNET_DATASTORE_QueueEntry *qe;
1372 struct GetZeroAnonymityMessage *m;
1373 union QueueContext qc;
1375 GNUNET_assert (NULL != proc);
1376 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1377 LOG (GNUNET_ERROR_TYPE_DEBUG,
1378 "Asked to get %llu-th zero-anonymity entry of type %d in %s\n",
1379 (unsigned long long) offset, type,
1380 GNUNET_STRINGS_relative_time_to_string (timeout, GNUNET_YES));
1382 qc.rc.proc_cls = proc_cls;
1383 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1384 queue_priority, max_queue_size, timeout,
1385 &process_result_message, &qc);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "Could not create queue entry for zero-anonymity procation\n");
1392 GNUNET_STATISTICS_update (h->stats,
1394 ("# GET ZERO ANONYMITY requests executed"), 1,
1396 m = (struct GetZeroAnonymityMessage *) &qe[1];
1397 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1398 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1399 m->type = htonl ((uint32_t) type);
1400 m->offset = GNUNET_htonll (offset);
1407 * Get a result for a particular key from the datastore. The processor
1408 * will only be called once.
1410 * @param h handle to the datastore
1411 * @param offset offset of the result (modulo num-results); set to
1412 * a random 64-bit value initially; then increment by
1413 * one each time; detect that all results have been found by uid
1414 * being again the first uid ever returned.
1415 * @param key maybe NULL (to match all entries)
1416 * @param type desired type, 0 for any
1417 * @param queue_priority ranking of this request in the priority queue
1418 * @param max_queue_size at what queue size should this request be dropped
1419 * (if other requests of higher priority are in the queue)
1420 * @param timeout how long to wait at most for a response
1421 * @param proc function to call on each matching value;
1422 * will be called once with a NULL value at the end
1423 * @param proc_cls closure for @a proc
1424 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1427 struct GNUNET_DATASTORE_QueueEntry *
1428 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1430 const struct GNUNET_HashCode * key,
1431 enum GNUNET_BLOCK_Type type,
1432 unsigned int queue_priority,
1433 unsigned int max_queue_size,
1434 struct GNUNET_TIME_Relative timeout,
1435 GNUNET_DATASTORE_DatumProcessor proc,
1438 struct GNUNET_DATASTORE_QueueEntry *qe;
1439 struct GetMessage *gm;
1440 union QueueContext qc;
1442 GNUNET_assert (NULL != proc);
1443 LOG (GNUNET_ERROR_TYPE_DEBUG,
1444 "Asked to look for data of type %u under key `%s'\n",
1445 (unsigned int) type, GNUNET_h2s (key));
1447 qc.rc.proc_cls = proc_cls;
1448 qe = make_queue_entry (h,
1449 sizeof (struct GetMessage),
1453 &process_result_message,
1457 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not queue request for `%s'\n",
1461 #if INSANE_STATISTICS
1462 GNUNET_STATISTICS_update (h->stats,
1463 gettext_noop ("# GET requests executed"),
1467 gm = (struct GetMessage *) &qe[1];
1468 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1469 gm->type = htonl (type);
1470 gm->offset = GNUNET_htonll (offset);
1473 gm->header.size = htons (sizeof (struct GetMessage));
1479 htons (sizeof (struct GetMessage) - sizeof (struct GNUNET_HashCode));
1487 * Cancel a datastore operation. The final callback from the
1488 * operation must not have been done yet.
1490 * @param qe operation to cancel
1493 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1495 struct GNUNET_DATASTORE_Handle *h;
1497 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1499 LOG (GNUNET_ERROR_TYPE_DEBUG,
1500 "Pending DATASTORE request %p cancelled (%d, %d)\n", qe,
1501 qe->was_transmitted, h->queue_head == qe);
1502 if (GNUNET_YES == qe->was_transmitted)
1504 free_queue_entry (qe);
1505 h->skip_next_messages++;
1508 free_queue_entry (qe);
1513 /* end of datastore_api.c */