2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 * If a client stopped asking for more results, how many more do
38 * we receive from the DB before killing the connection? Trade-off
39 * between re-doing TCP handshakes and (needlessly) receiving
42 #define MAX_EXCESS_RESULTS 8
45 * Context for processing status messages.
50 * Continuation to call with the status.
52 GNUNET_DATASTORE_ContinuationWithStatus cont;
63 * Context for processing result messages.
68 * Function to call with the result.
70 GNUNET_DATASTORE_DatumProcessor proc;
81 * Context for a queue operation.
86 struct StatusContext sc;
88 struct ResultContext rc;
95 * Entry in our priority queue.
97 struct GNUNET_DATASTORE_QueueEntry
101 * This is a linked list.
103 struct GNUNET_DATASTORE_QueueEntry *next;
106 * This is a linked list.
108 struct GNUNET_DATASTORE_QueueEntry *prev;
111 * Handle to the master context.
113 struct GNUNET_DATASTORE_Handle *h;
116 * Response processor (NULL if we are not waiting for a response).
117 * This struct should be used for the closure, function-specific
118 * arguments can be passed via 'qc'.
120 GNUNET_CLIENT_MessageHandler response_proc;
123 * Function to call after transmission of the request.
125 GNUNET_DATASTORE_ContinuationWithStatus cont;
128 * Closure for 'cont'.
133 * Context for the operation.
135 union QueueContext qc;
138 * Task for timeout signalling.
140 GNUNET_SCHEDULER_TaskIdentifier task;
143 * Timeout for the current operation.
145 struct GNUNET_TIME_Absolute timeout;
148 * Priority in the queue.
150 unsigned int priority;
153 * Maximum allowed length of queue (otherwise
154 * this request should be discarded).
156 unsigned int max_queue;
159 * Number of bytes in the request message following
160 * this struct. 32-bit value for nicer memory
161 * access (and overall struct alignment).
163 uint32_t message_size;
166 * Has this message been transmitted to the service?
167 * Only ever GNUNET_YES for the head of the queue.
168 * Note that the overall struct should end at a
169 * multiple of 64 bits.
176 * Handle to the datastore service.
178 struct GNUNET_DATASTORE_Handle
184 const struct GNUNET_CONFIGURATION_Handle *cfg;
187 * Current connection to the datastore service.
189 struct GNUNET_CLIENT_Connection *client;
192 * Handle for statistics.
194 struct GNUNET_STATISTICS_Handle *stats;
197 * Current transmit handle.
199 struct GNUNET_CLIENT_TransmitHandle *th;
202 * Current head of priority queue.
204 struct GNUNET_DATASTORE_QueueEntry *queue_head;
207 * Current tail of priority queue.
209 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
212 * Task for trying to reconnect.
214 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
217 * How quickly should we retry? Used for exponential back-off on
220 struct GNUNET_TIME_Relative retry_time;
223 * Number of entries in the queue.
225 unsigned int queue_size;
228 * Number of results we're receiving for the current query
229 * after application stopped to care. Used to determine when
230 * to reset the connection.
232 unsigned int result_count;
235 * Are we currently trying to receive from the service?
240 * We should ignore the next message(s) from the service.
242 unsigned int skip_next_messages;
249 * Connect to the datastore service.
251 * @param cfg configuration to use
252 * @return handle to use to access the service
254 struct GNUNET_DATASTORE_Handle *
255 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
257 struct GNUNET_CLIENT_Connection *c;
258 struct GNUNET_DATASTORE_Handle *h;
260 c = GNUNET_CLIENT_connect ("datastore", cfg);
262 return NULL; /* oops */
263 h = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_Handle) +
264 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
267 h->stats = GNUNET_STATISTICS_create ("datastore-api", cfg);
273 * Task used by 'transmit_drop' to disconnect the datastore.
275 * @param cls the datastore handle
276 * @param tc scheduler context
279 disconnect_after_drop (void *cls,
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
282 struct GNUNET_DATASTORE_Handle *h = cls;
284 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
289 * Transmit DROP message to datastore service.
291 * @param cls the 'struct GNUNET_DATASTORE_Handle'
292 * @param size number of bytes that can be copied to buf
293 * @param buf where to copy the drop message
294 * @return number of bytes written to buf
297 transmit_drop (void *cls, size_t size, void *buf)
299 struct GNUNET_DATASTORE_Handle *h = cls;
300 struct GNUNET_MessageHeader *hdr;
304 LOG (GNUNET_ERROR_TYPE_WARNING,
305 _("Failed to transmit request to drop database.\n"));
306 GNUNET_SCHEDULER_add_continuation (&disconnect_after_drop, h,
307 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
310 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
312 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
313 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
314 GNUNET_SCHEDULER_add_continuation (&disconnect_after_drop, h,
315 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
316 return sizeof (struct GNUNET_MessageHeader);
321 * Disconnect from the datastore service (and free
322 * associated resources).
324 * @param h handle to the datastore
325 * @param drop set to GNUNET_YES to delete all data in datastore (!)
328 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop)
330 struct GNUNET_DATASTORE_QueueEntry *qe;
333 LOG (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
337 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
340 if (h->client != NULL)
342 GNUNET_CLIENT_disconnect (h->client);
345 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
347 GNUNET_SCHEDULER_cancel (h->reconnect_task);
348 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
350 while (NULL != (qe = h->queue_head))
352 GNUNET_assert (NULL != qe->response_proc);
353 qe->response_proc (h, NULL);
355 if (GNUNET_YES == drop)
357 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
358 if (h->client != NULL)
361 GNUNET_CLIENT_notify_transmit_ready (h->client,
363 GNUNET_MessageHeader),
364 GNUNET_TIME_UNIT_MINUTES,
365 GNUNET_YES, &transmit_drop, h))
367 GNUNET_CLIENT_disconnect (h->client);
372 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
379 * A request has timed out (before being transmitted to the service).
381 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
382 * @param tc scheduler context
385 timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
389 GNUNET_STATISTICS_update (qe->h->stats,
390 gettext_noop ("# queue entry timeouts"), 1,
392 qe->task = GNUNET_SCHEDULER_NO_TASK;
393 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
395 LOG (GNUNET_ERROR_TYPE_DEBUG, "Timeout of request in datastore queue\n");
397 qe->response_proc (qe->h, NULL);
402 * Create a new entry for our priority queue (and possibly discard other entires if
403 * the queue is getting too long).
405 * @param h handle to the datastore
406 * @param msize size of the message to queue
407 * @param queue_priority priority of the entry
408 * @param max_queue_size at what queue size should this request be dropped
409 * (if other requests of higher priority are in the queue)
410 * @param timeout timeout for the operation
411 * @param response_proc function to call with replies (can be NULL)
412 * @param qc client context (NOT a closure for response_proc)
413 * @return NULL if the queue is full
415 static struct GNUNET_DATASTORE_QueueEntry *
416 make_queue_entry (struct GNUNET_DATASTORE_Handle *h, size_t msize,
417 unsigned int queue_priority, unsigned int max_queue_size,
418 struct GNUNET_TIME_Relative timeout,
419 GNUNET_CLIENT_MessageHandler response_proc,
420 const union QueueContext *qc)
422 struct GNUNET_DATASTORE_QueueEntry *ret;
423 struct GNUNET_DATASTORE_QueueEntry *pos;
428 while ((pos != NULL) && (c < max_queue_size) &&
429 (pos->priority >= queue_priority))
434 if (c >= max_queue_size)
436 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue overflows"), 1,
440 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
442 ret->response_proc = response_proc;
444 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
445 ret->priority = queue_priority;
446 ret->max_queue = max_queue_size;
447 ret->message_size = msize;
448 ret->was_transmitted = GNUNET_NO;
451 /* append at the tail */
457 /* do not insert at HEAD if HEAD query was already
458 * transmitted and we are still receiving replies! */
459 if ((pos == NULL) && (h->queue_head->was_transmitted))
463 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue entries created"),
465 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
467 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
471 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
473 GNUNET_assert (pos->response_proc != NULL);
474 /* move 'pos' element to head so that it will be
475 * killed on 'NULL' call below */
477 LOG (GNUNET_ERROR_TYPE_DEBUG, "Dropping request from datastore queue\n");
479 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, pos);
480 GNUNET_CONTAINER_DLL_insert (h->queue_head, h->queue_tail, pos);
481 GNUNET_STATISTICS_update (h->stats,
483 ("# Requests dropped from datastore queue"), 1,
485 GNUNET_assert (h->queue_head == pos);
486 pos->response_proc (h, NULL);
496 * Process entries in the queue (or do nothing if we are already
499 * @param h handle to the datastore
502 process_queue (struct GNUNET_DATASTORE_Handle *h);
506 * Try reconnecting to the datastore service.
508 * @param cls the 'struct GNUNET_DATASTORE_Handle'
509 * @param tc scheduler context
512 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
514 struct GNUNET_DATASTORE_Handle *h = cls;
516 if (h->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
517 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
519 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
520 if (h->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
521 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
522 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
523 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
524 if (h->client == NULL)
526 LOG (GNUNET_ERROR_TYPE_ERROR, "DATASTORE reconnect failed (fatally)\n");
529 GNUNET_STATISTICS_update (h->stats,
531 ("# datastore connections (re)created"), 1,
534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
541 * Disconnect from the service and then try reconnecting to the datastore service
544 * @param h handle to datastore to disconnect and reconnect
547 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
549 if (h->client == NULL)
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "client NULL in disconnect, will not try to reconnect\n");
558 GNUNET_STATISTICS_update (stats, gettext_noop ("# reconnected to DATASTORE"),
561 GNUNET_CLIENT_disconnect (h->client);
562 h->skip_next_messages = 0;
565 GNUNET_SCHEDULER_add_delayed (h->retry_time, &try_reconnect, h);
570 * Function called whenever we receive a message from
571 * the service. Calls the appropriate handler.
573 * @param cls the 'struct GNUNET_DATASTORE_Handle'
574 * @param msg the received message
577 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
579 struct GNUNET_DATASTORE_Handle *h = cls;
580 struct GNUNET_DATASTORE_QueueEntry *qe;
582 h->in_receive = GNUNET_NO;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving reply from datastore\n");
586 if (h->skip_next_messages > 0)
588 h->skip_next_messages--;
592 if (NULL == (qe = h->queue_head))
598 qe->response_proc (h, msg);
603 * Transmit request from queue to datastore service.
605 * @param cls the 'struct GNUNET_DATASTORE_Handle'
606 * @param size number of bytes that can be copied to buf
607 * @param buf where to copy the drop message
608 * @return number of bytes written to buf
611 transmit_request (void *cls, size_t size, void *buf)
613 struct GNUNET_DATASTORE_Handle *h = cls;
614 struct GNUNET_DATASTORE_QueueEntry *qe;
618 if (NULL == (qe = h->queue_head))
619 return 0; /* no entry in queue */
623 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to DATASTORE.\n");
625 GNUNET_STATISTICS_update (h->stats,
626 gettext_noop ("# transmission request failures"),
631 if (size < (msize = qe->message_size))
637 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u byte request to DATASTORE\n",
640 memcpy (buf, &qe[1], msize);
641 qe->was_transmitted = GNUNET_YES;
642 GNUNET_SCHEDULER_cancel (qe->task);
643 qe->task = GNUNET_SCHEDULER_NO_TASK;
644 GNUNET_assert (GNUNET_NO == h->in_receive);
645 h->in_receive = GNUNET_YES;
646 GNUNET_CLIENT_receive (h->client, &receive_cb, h,
647 GNUNET_TIME_absolute_get_remaining (qe->timeout));
648 GNUNET_STATISTICS_update (h->stats,
649 gettext_noop ("# bytes sent to datastore"), 1,
656 * Process entries in the queue (or do nothing if we are already
659 * @param h handle to the datastore
662 process_queue (struct GNUNET_DATASTORE_Handle *h)
664 struct GNUNET_DATASTORE_QueueEntry *qe;
666 if (NULL == (qe = h->queue_head))
668 #if DEBUG_DATASTORE > 1
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
671 return; /* no entry in queue */
673 if (qe->was_transmitted == GNUNET_YES)
675 #if DEBUG_DATASTORE > 1
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
678 return; /* waiting for replies */
682 #if DEBUG_DATASTORE > 1
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
685 return; /* request pending */
687 if (h->client == NULL)
689 #if DEBUG_DATASTORE > 1
690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
692 return; /* waiting for reconnect */
694 if (GNUNET_YES == h->in_receive)
696 /* wait for response to previous query */
700 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing %u byte request to DATASTORE\n",
704 GNUNET_CLIENT_notify_transmit_ready (h->client, qe->message_size,
705 GNUNET_TIME_absolute_get_remaining
706 (qe->timeout), GNUNET_YES,
707 &transmit_request, h);
708 GNUNET_assert (GNUNET_NO == h->in_receive);
709 GNUNET_break (NULL != h->th);
714 * Dummy continuation used to do nothing (but be non-zero).
717 * @param result result
718 * @param min_expiration expiration time
719 * @param emsg error message
722 drop_status_cont (void *cls, int32_t result,
723 struct GNUNET_TIME_Absolute min_expiration,
731 * Free a queue entry. Removes the given entry from the
732 * queue and releases associated resources. Does NOT
735 * @param qe entry to free.
738 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
740 struct GNUNET_DATASTORE_Handle *h = qe->h;
742 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
743 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
745 GNUNET_SCHEDULER_cancel (qe->task);
746 qe->task = GNUNET_SCHEDULER_NO_TASK;
749 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
755 * Type of a function to call when we receive a message
759 * @param msg message received, NULL on timeout or fatal error
762 process_status_message (void *cls, const struct GNUNET_MessageHeader *msg)
764 struct GNUNET_DATASTORE_Handle *h = cls;
765 struct GNUNET_DATASTORE_QueueEntry *qe;
766 struct StatusContext rc;
767 const struct StatusMessage *sm;
772 if (NULL == (qe = h->queue_head))
781 was_transmitted = qe->was_transmitted;
782 free_queue_entry (qe);
783 if (was_transmitted == GNUNET_YES)
788 rc.cont (rc.cont_cls, GNUNET_SYSERR,
789 GNUNET_TIME_UNIT_ZERO_ABS,
790 _("Failed to receive status response from database."));
793 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
794 free_queue_entry (qe);
795 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
796 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
799 h->retry_time = GNUNET_TIME_UNIT_ZERO;
802 rc.cont (rc.cont_cls, GNUNET_SYSERR,
803 GNUNET_TIME_UNIT_ZERO_ABS,
804 _("Error reading response from datastore service"));
807 sm = (const struct StatusMessage *) msg;
808 status = ntohl (sm->status);
810 if (ntohs (msg->size) > sizeof (struct StatusMessage))
812 emsg = (const char *) &sm[1];
813 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
816 emsg = _("Invalid error message received from datastore service");
819 if ((status == GNUNET_SYSERR) && (emsg == NULL))
822 emsg = _("Invalid error message received from datastore service");
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received status %d/%s\n", (int) status, emsg);
827 GNUNET_STATISTICS_update (h->stats,
828 gettext_noop ("# status messages received"), 1,
830 h->retry_time.rel_value = 0;
833 rc.cont (rc.cont_cls, status,
834 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
840 * Store an item in the datastore. If the item is already present,
841 * the priorities are summed up and the higher expiration time and
842 * lower anonymity level is used.
844 * @param h handle to the datastore
845 * @param rid reservation ID to use (from "reserve"); use 0 if no
846 * prior reservation was made
847 * @param key key for the value
848 * @param size number of bytes in data
849 * @param data content stored
850 * @param type type of the content
851 * @param priority priority of the content
852 * @param anonymity anonymity-level for the content
853 * @param replication how often should the content be replicated to other peers?
854 * @param expiration expiration time for the content
855 * @param queue_priority ranking of this request in the priority queue
856 * @param max_queue_size at what queue size should this request be dropped
857 * (if other requests of higher priority are in the queue)
858 * @param timeout timeout for the operation
859 * @param cont continuation to call when done
860 * @param cont_cls closure for cont
861 * @return NULL if the entry was not queued, otherwise a handle that can be used to
862 * cancel; note that even if NULL is returned, the callback will be invoked
863 * (or rather, will already have been invoked)
865 struct GNUNET_DATASTORE_QueueEntry *
866 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
867 const GNUNET_HashCode * key, size_t size,
868 const void *data, enum GNUNET_BLOCK_Type type,
869 uint32_t priority, uint32_t anonymity,
870 uint32_t replication,
871 struct GNUNET_TIME_Absolute expiration,
872 unsigned int queue_priority, unsigned int max_queue_size,
873 struct GNUNET_TIME_Relative timeout,
874 GNUNET_DATASTORE_ContinuationWithStatus cont,
877 struct GNUNET_DATASTORE_QueueEntry *qe;
878 struct DataMessage *dm;
880 union QueueContext qc;
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "Asked to put %u bytes of data under key `%s' for %llu ms\n", size,
886 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
888 msize = sizeof (struct DataMessage) + size;
889 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
891 qc.sc.cont_cls = cont_cls;
892 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
893 &process_status_message, &qc);
897 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for PUT\n");
901 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# PUT requests executed"),
903 dm = (struct DataMessage *) &qe[1];
904 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
905 dm->header.size = htons (msize);
906 dm->rid = htonl (rid);
907 dm->size = htonl ((uint32_t) size);
908 dm->type = htonl (type);
909 dm->priority = htonl (priority);
910 dm->anonymity = htonl (anonymity);
911 dm->replication = htonl (replication);
912 dm->reserved = htonl (0);
913 dm->uid = GNUNET_htonll (0);
914 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
916 memcpy (&dm[1], data, size);
923 * Reserve space in the datastore. This function should be used
924 * to avoid "out of space" failures during a longer sequence of "put"
925 * operations (for example, when a file is being inserted).
927 * @param h handle to the datastore
928 * @param amount how much space (in bytes) should be reserved (for content only)
929 * @param entries how many entries will be created (to calculate per-entry overhead)
930 * @param queue_priority ranking of this request in the priority queue
931 * @param max_queue_size at what queue size should this request be dropped
932 * (if other requests of higher priority are in the queue)
933 * @param timeout how long to wait at most for a response (or before dying in queue)
934 * @param cont continuation to call when done; "success" will be set to
935 * a positive reservation value if space could be reserved.
936 * @param cont_cls closure for cont
937 * @return NULL if the entry was not queued, otherwise a handle that can be used to
938 * cancel; note that even if NULL is returned, the callback will be invoked
939 * (or rather, will already have been invoked)
941 struct GNUNET_DATASTORE_QueueEntry *
942 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
943 uint32_t entries, unsigned int queue_priority,
944 unsigned int max_queue_size,
945 struct GNUNET_TIME_Relative timeout,
946 GNUNET_DATASTORE_ContinuationWithStatus cont,
949 struct GNUNET_DATASTORE_QueueEntry *qe;
950 struct ReserveMessage *rm;
951 union QueueContext qc;
954 cont = &drop_status_cont;
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "Asked to reserve %llu bytes of data and %u entries\n",
958 (unsigned long long) amount, (unsigned int) entries);
961 qc.sc.cont_cls = cont_cls;
962 qe = make_queue_entry (h, sizeof (struct ReserveMessage), queue_priority,
963 max_queue_size, timeout, &process_status_message, &qc);
967 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry to reserve\n");
971 GNUNET_STATISTICS_update (h->stats,
972 gettext_noop ("# RESERVE requests executed"), 1,
974 rm = (struct ReserveMessage *) &qe[1];
975 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
976 rm->header.size = htons (sizeof (struct ReserveMessage));
977 rm->entries = htonl (entries);
978 rm->amount = GNUNET_htonll (amount);
985 * Signal that all of the data for which a reservation was made has
986 * been stored and that whatever excess space might have been reserved
987 * can now be released.
989 * @param h handle to the datastore
990 * @param rid reservation ID (value of "success" in original continuation
991 * from the "reserve" function).
992 * @param queue_priority ranking of this request in the priority queue
993 * @param max_queue_size at what queue size should this request be dropped
994 * (if other requests of higher priority are in the queue)
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 timeout how long to wait at most for a response
999 * @param cont continuation to call when done
1000 * @param cont_cls closure for cont
1001 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1002 * cancel; note that even if NULL is returned, the callback will be invoked
1003 * (or rather, will already have been invoked)
1005 struct GNUNET_DATASTORE_QueueEntry *
1006 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1007 uint32_t rid, unsigned int queue_priority,
1008 unsigned int max_queue_size,
1009 struct GNUNET_TIME_Relative timeout,
1010 GNUNET_DATASTORE_ContinuationWithStatus cont,
1013 struct GNUNET_DATASTORE_QueueEntry *qe;
1014 struct ReleaseReserveMessage *rrm;
1015 union QueueContext qc;
1018 cont = &drop_status_cont;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
1023 qc.sc.cont_cls = cont_cls;
1024 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
1025 queue_priority, max_queue_size, timeout,
1026 &process_status_message, &qc);
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Could not create queue entry to release reserve\n");
1035 GNUNET_STATISTICS_update (h->stats,
1037 ("# RELEASE RESERVE requests executed"), 1,
1039 rrm = (struct ReleaseReserveMessage *) &qe[1];
1040 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1041 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1042 rrm->rid = htonl (rid);
1049 * Update a value in the datastore.
1051 * @param h handle to the datastore
1052 * @param uid identifier for the value
1053 * @param priority how much to increase the priority of the value
1054 * @param expiration new expiration value should be MAX of existing and this argument
1055 * @param queue_priority ranking of this request in the priority queue
1056 * @param max_queue_size at what queue size should this request be dropped
1057 * (if other requests of higher priority are in the queue)
1058 * @param timeout how long to wait at most for a response
1059 * @param cont continuation to call when done
1060 * @param cont_cls closure for cont
1061 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1062 * cancel; note that even if NULL is returned, the callback will be invoked
1063 * (or rather, will already have been invoked)
1065 struct GNUNET_DATASTORE_QueueEntry *
1066 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
1068 struct GNUNET_TIME_Absolute expiration,
1069 unsigned int queue_priority,
1070 unsigned int max_queue_size,
1071 struct GNUNET_TIME_Relative timeout,
1072 GNUNET_DATASTORE_ContinuationWithStatus cont,
1075 struct GNUNET_DATASTORE_QueueEntry *qe;
1076 struct UpdateMessage *um;
1077 union QueueContext qc;
1080 cont = &drop_status_cont;
1082 LOG (GNUNET_ERROR_TYPE_DEBUG,
1083 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1084 uid, (unsigned int) priority, (unsigned long long) expiration.abs_value);
1087 qc.sc.cont_cls = cont_cls;
1088 qe = make_queue_entry (h, sizeof (struct UpdateMessage), queue_priority,
1089 max_queue_size, timeout, &process_status_message, &qc);
1093 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for UPDATE\n");
1097 GNUNET_STATISTICS_update (h->stats,
1098 gettext_noop ("# UPDATE requests executed"), 1,
1100 um = (struct UpdateMessage *) &qe[1];
1101 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1102 um->header.size = htons (sizeof (struct UpdateMessage));
1103 um->priority = htonl (priority);
1104 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1105 um->uid = GNUNET_htonll (uid);
1112 * Explicitly remove some content from the database.
1113 * The "cont"inuation will be called with status
1114 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1115 * if no matching entry was found and "GNUNET_SYSERR"
1116 * on all other types of errors.
1118 * @param h handle to the datastore
1119 * @param key key for the value
1120 * @param size number of bytes in data
1121 * @param data content stored
1122 * @param queue_priority ranking of this request in the priority queue
1123 * @param max_queue_size at what queue size should this request be dropped
1124 * (if other requests of higher priority are in the queue)
1125 * @param timeout how long to wait at most for a response
1126 * @param cont continuation to call when done
1127 * @param cont_cls closure for cont
1128 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1129 * cancel; note that even if NULL is returned, the callback will be invoked
1130 * (or rather, will already have been invoked)
1132 struct GNUNET_DATASTORE_QueueEntry *
1133 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1134 const GNUNET_HashCode * key, size_t size,
1135 const void *data, unsigned int queue_priority,
1136 unsigned int max_queue_size,
1137 struct GNUNET_TIME_Relative timeout,
1138 GNUNET_DATASTORE_ContinuationWithStatus cont,
1141 struct GNUNET_DATASTORE_QueueEntry *qe;
1142 struct DataMessage *dm;
1144 union QueueContext qc;
1147 cont = &drop_status_cont;
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to remove %u bytes under key `%s'\n",
1150 size, GNUNET_h2s (key));
1153 qc.sc.cont_cls = cont_cls;
1154 msize = sizeof (struct DataMessage) + size;
1155 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1156 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
1157 &process_status_message, &qc);
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for REMOVE\n");
1165 GNUNET_STATISTICS_update (h->stats,
1166 gettext_noop ("# REMOVE requests executed"), 1,
1168 dm = (struct DataMessage *) &qe[1];
1169 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1170 dm->header.size = htons (msize);
1171 dm->rid = htonl (0);
1172 dm->size = htonl (size);
1173 dm->type = htonl (0);
1174 dm->priority = htonl (0);
1175 dm->anonymity = htonl (0);
1176 dm->uid = GNUNET_htonll (0);
1177 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1179 memcpy (&dm[1], data, size);
1186 * Type of a function to call when we receive a message
1189 * @param cls closure
1190 * @param msg message received, NULL on timeout or fatal error
1193 process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1195 struct GNUNET_DATASTORE_Handle *h = cls;
1196 struct GNUNET_DATASTORE_QueueEntry *qe;
1197 struct ResultContext rc;
1198 const struct DataMessage *dm;
1199 int was_transmitted;
1204 GNUNET_assert (NULL != qe);
1206 was_transmitted = qe->was_transmitted;
1207 free_queue_entry (qe);
1208 if (was_transmitted == GNUNET_YES)
1210 LOG (GNUNET_ERROR_TYPE_WARNING,
1211 _("Failed to receive response from database.\n"));
1218 if (rc.proc != NULL)
1219 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1223 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1225 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1228 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1229 free_queue_entry (qe);
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received end of result set, new queue size is %u\n", h->queue_size);
1234 h->retry_time.rel_value = 0;
1235 h->result_count = 0;
1237 if (rc.proc != NULL)
1238 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1243 GNUNET_assert (NULL != qe);
1245 if (GNUNET_YES != qe->was_transmitted)
1248 free_queue_entry (qe);
1249 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1251 if (rc.proc != NULL)
1252 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1256 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1257 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1258 (ntohs (msg->size) !=
1259 sizeof (struct DataMessage) +
1260 ntohl (((const struct DataMessage *) msg)->size)))
1263 free_queue_entry (qe);
1264 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1266 if (rc.proc != NULL)
1267 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1271 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# Results received"), 1,
1273 dm = (const struct DataMessage *) msg;
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "Received result %llu with type %u and size %u with key %s\n",
1277 (unsigned long long) GNUNET_ntohll (dm->uid), ntohl (dm->type),
1278 ntohl (dm->size), GNUNET_h2s (&dm->key));
1280 free_queue_entry (qe);
1281 h->retry_time.rel_value = 0;
1283 if (rc.proc != NULL)
1284 rc.proc (rc.proc_cls, &dm->key, ntohl (dm->size), &dm[1], ntohl (dm->type),
1285 ntohl (dm->priority), ntohl (dm->anonymity),
1286 GNUNET_TIME_absolute_ntoh (dm->expiration),
1287 GNUNET_ntohll (dm->uid));
1292 * Get a random value from the datastore for content replication.
1293 * Returns a single, random value among those with the highest
1294 * replication score, lowering positive replication scores by one for
1295 * the chosen value (if only content with a replication score exists,
1296 * a random value is returned and replication scores are not changed).
1298 * @param h handle to the datastore
1299 * @param queue_priority ranking of this request in the priority queue
1300 * @param max_queue_size at what queue size should this request be dropped
1301 * (if other requests of higher priority are in the queue)
1302 * @param timeout how long to wait at most for a response
1303 * @param proc function to call on a random value; it
1304 * will be called once with a value (if available)
1305 * and always once with a value of NULL.
1306 * @param proc_cls closure for proc
1307 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1310 struct GNUNET_DATASTORE_QueueEntry *
1311 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1312 unsigned int queue_priority,
1313 unsigned int max_queue_size,
1314 struct GNUNET_TIME_Relative timeout,
1315 GNUNET_DATASTORE_DatumProcessor proc,
1318 struct GNUNET_DATASTORE_QueueEntry *qe;
1319 struct GNUNET_MessageHeader *m;
1320 union QueueContext qc;
1322 GNUNET_assert (NULL != proc);
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to get replication entry in %llu ms\n",
1325 (unsigned long long) timeout.rel_value);
1328 qc.rc.proc_cls = proc_cls;
1329 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1330 queue_priority, max_queue_size, timeout,
1331 &process_result_message, &qc);
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "Could not create queue entry for GET REPLICATION\n");
1340 GNUNET_STATISTICS_update (h->stats,
1342 ("# GET REPLICATION requests executed"), 1,
1344 m = (struct GNUNET_MessageHeader *) &qe[1];
1345 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1346 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1353 * Get a single zero-anonymity value from the datastore.
1355 * @param h handle to the datastore
1356 * @param offset offset of the result (modulo num-results); set to
1357 * a random 64-bit value initially; then increment by
1358 * one each time; detect that all results have been found by uid
1359 * being again the first uid ever returned.
1360 * @param queue_priority ranking of this request in the priority queue
1361 * @param max_queue_size at what queue size should this request be dropped
1362 * (if other requests of higher priority are in the queue)
1363 * @param timeout how long to wait at most for a response
1364 * @param type allowed type for the operation (never zero)
1365 * @param proc function to call on a random value; it
1366 * will be called once with a value (if available)
1367 * or with NULL if none value exists.
1368 * @param proc_cls closure for proc
1369 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1372 struct GNUNET_DATASTORE_QueueEntry *
1373 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1375 unsigned int queue_priority,
1376 unsigned int max_queue_size,
1377 struct GNUNET_TIME_Relative timeout,
1378 enum GNUNET_BLOCK_Type type,
1379 GNUNET_DATASTORE_DatumProcessor proc,
1382 struct GNUNET_DATASTORE_QueueEntry *qe;
1383 struct GetZeroAnonymityMessage *m;
1384 union QueueContext qc;
1386 GNUNET_assert (NULL != proc);
1387 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1390 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1391 (unsigned long long) offset, type,
1392 (unsigned long long) timeout.rel_value);
1395 qc.rc.proc_cls = proc_cls;
1396 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1397 queue_priority, max_queue_size, timeout,
1398 &process_result_message, &qc);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "Could not create queue entry for zero-anonymity procation\n");
1407 GNUNET_STATISTICS_update (h->stats,
1409 ("# GET ZERO ANONYMITY requests executed"), 1,
1411 m = (struct GetZeroAnonymityMessage *) &qe[1];
1412 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1413 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1414 m->type = htonl ((uint32_t) type);
1415 m->offset = GNUNET_htonll (offset);
1422 * Get a result for a particular key from the datastore. The processor
1423 * will only be called once.
1425 * @param h handle to the datastore
1426 * @param offset offset of the result (modulo num-results); set to
1427 * a random 64-bit value initially; then increment by
1428 * one each time; detect that all results have been found by uid
1429 * being again the first uid ever returned.
1430 * @param key maybe NULL (to match all entries)
1431 * @param type desired type, 0 for any
1432 * @param queue_priority ranking of this request in the priority queue
1433 * @param max_queue_size at what queue size should this request be dropped
1434 * (if other requests of higher priority are in the queue)
1435 * @param timeout how long to wait at most for a response
1436 * @param proc function to call on each matching value;
1437 * will be called once with a NULL value at the end
1438 * @param proc_cls closure for proc
1439 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1442 struct GNUNET_DATASTORE_QueueEntry *
1443 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, uint64_t offset,
1444 const GNUNET_HashCode * key,
1445 enum GNUNET_BLOCK_Type type,
1446 unsigned int queue_priority,
1447 unsigned int max_queue_size,
1448 struct GNUNET_TIME_Relative timeout,
1449 GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
1451 struct GNUNET_DATASTORE_QueueEntry *qe;
1452 struct GetMessage *gm;
1453 union QueueContext qc;
1455 GNUNET_assert (NULL != proc);
1457 LOG (GNUNET_ERROR_TYPE_DEBUG,
1458 "Asked to look for data of type %u under key `%s'\n",
1459 (unsigned int) type, GNUNET_h2s (key));
1462 qc.rc.proc_cls = proc_cls;
1463 qe = make_queue_entry (h, sizeof (struct GetMessage), queue_priority,
1464 max_queue_size, timeout, &process_result_message, &qc);
1468 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not queue request for `%s'\n",
1473 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# GET requests executed"),
1475 gm = (struct GetMessage *) &qe[1];
1476 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1477 gm->type = htonl (type);
1478 gm->offset = GNUNET_htonll (offset);
1481 gm->header.size = htons (sizeof (struct GetMessage));
1487 htons (sizeof (struct GetMessage) - sizeof (GNUNET_HashCode));
1495 * Cancel a datastore operation. The final callback from the
1496 * operation must not have been done yet.
1498 * @param qe operation to cancel
1501 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1503 struct GNUNET_DATASTORE_Handle *h;
1505 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1508 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Pending DATASTORE request %p cancelled (%d, %d)\n", qe,
1510 qe->was_transmitted, h->queue_head == qe);
1512 if (GNUNET_YES == qe->was_transmitted)
1514 free_queue_entry (qe);
1515 h->skip_next_messages++;
1518 free_queue_entry (qe);
1523 /* end of datastore_api.c */