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 * 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 GNUNET_SCHEDULER_TaskIdentifier 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 GNUNET_SCHEDULER_TaskIdentifier 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 buf
298 * @param buf where to copy the drop message
299 * @return number of bytes written to 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_continuation (&disconnect_after_drop, h,
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
315 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
317 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
318 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
319 GNUNET_SCHEDULER_add_continuation (&disconnect_after_drop, h,
320 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
321 return sizeof (struct GNUNET_MessageHeader);
326 * Disconnect from the datastore service (and free
327 * associated resources).
329 * @param h handle to the datastore
330 * @param drop set to GNUNET_YES to delete all data in datastore (!)
333 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop)
335 struct GNUNET_DATASTORE_QueueEntry *qe;
337 LOG (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
340 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
343 if (h->client != NULL)
345 GNUNET_CLIENT_disconnect (h->client);
348 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
350 GNUNET_SCHEDULER_cancel (h->reconnect_task);
351 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
353 while (NULL != (qe = h->queue_head))
355 GNUNET_assert (NULL != qe->response_proc);
356 qe->response_proc (h, NULL);
358 if (GNUNET_YES == drop)
360 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
361 if (h->client != NULL)
364 GNUNET_CLIENT_notify_transmit_ready (h->client,
366 GNUNET_MessageHeader),
367 GNUNET_TIME_UNIT_MINUTES,
368 GNUNET_YES, &transmit_drop, h))
370 GNUNET_CLIENT_disconnect (h->client);
375 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
382 * A request has timed out (before being transmitted to the service).
384 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
385 * @param tc scheduler context
388 timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
390 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
392 GNUNET_STATISTICS_update (qe->h->stats,
393 gettext_noop ("# queue entry timeouts"), 1,
395 qe->task = GNUNET_SCHEDULER_NO_TASK;
396 GNUNET_assert (GNUNET_NO == qe->was_transmitted);
397 LOG (GNUNET_ERROR_TYPE_DEBUG, "Timeout of request in datastore queue\n");
398 qe->response_proc (qe->h, NULL);
403 * Create a new entry for our priority queue (and possibly discard other entires if
404 * the queue is getting too long).
406 * @param h handle to the datastore
407 * @param msize size of the message to queue
408 * @param queue_priority priority of the entry
409 * @param max_queue_size at what queue size should this request be dropped
410 * (if other requests of higher priority are in the queue)
411 * @param timeout timeout for the operation
412 * @param response_proc function to call with replies (can be NULL)
413 * @param qc client context (NOT a closure for response_proc)
414 * @return NULL if the queue is full
416 static struct GNUNET_DATASTORE_QueueEntry *
417 make_queue_entry (struct GNUNET_DATASTORE_Handle *h, size_t msize,
418 unsigned int queue_priority, unsigned int max_queue_size,
419 struct GNUNET_TIME_Relative timeout,
420 GNUNET_CLIENT_MessageHandler response_proc,
421 const union QueueContext *qc)
423 struct GNUNET_DATASTORE_QueueEntry *ret;
424 struct GNUNET_DATASTORE_QueueEntry *pos;
429 while ((pos != NULL) && (c < max_queue_size) &&
430 (pos->priority >= queue_priority))
435 if (c >= max_queue_size)
437 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue overflows"), 1,
441 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
443 ret->response_proc = response_proc;
445 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
446 ret->priority = queue_priority;
447 ret->max_queue = max_queue_size;
448 ret->message_size = msize;
449 ret->was_transmitted = GNUNET_NO;
452 /* append at the tail */
458 /* do not insert at HEAD if HEAD query was already
459 * transmitted and we are still receiving replies! */
460 if ((pos == NULL) && (h->queue_head->was_transmitted))
464 #if INSANE_STATISTICS
465 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# queue entries created"),
468 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
470 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
471 for (pos = ret->next; NULL != pos; pos = pos->next)
473 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
475 GNUNET_assert (pos->response_proc != NULL);
476 /* move 'pos' element to head so that it will be
477 * killed on 'NULL' call below */
478 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);
495 * Process entries in the queue (or do nothing if we are already
498 * @param h handle to the datastore
501 process_queue (struct GNUNET_DATASTORE_Handle *h);
505 * Try reconnecting to the datastore service.
507 * @param cls the 'struct GNUNET_DATASTORE_Handle'
508 * @param tc scheduler context
511 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
513 struct GNUNET_DATASTORE_Handle *h = cls;
515 h->retry_time = GNUNET_TIME_STD_BACKOFF (h->retry_time);
516 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
517 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
518 if (h->client == NULL)
520 LOG (GNUNET_ERROR_TYPE_ERROR, "DATASTORE reconnect failed (fatally)\n");
523 GNUNET_STATISTICS_update (h->stats,
525 ("# datastore connections (re)created"), 1,
527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
533 * Disconnect from the service and then try reconnecting to the datastore service
536 * @param h handle to datastore to disconnect and reconnect
539 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
541 if (h->client == NULL)
543 LOG (GNUNET_ERROR_TYPE_DEBUG,
544 "client NULL in disconnect, will not try to reconnect\n");
547 GNUNET_CLIENT_disconnect (h->client);
548 h->skip_next_messages = 0;
551 GNUNET_SCHEDULER_add_delayed (h->retry_time, &try_reconnect, h);
556 * Function called whenever we receive a message from
557 * the service. Calls the appropriate handler.
559 * @param cls the 'struct GNUNET_DATASTORE_Handle'
560 * @param msg the received message
563 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
565 struct GNUNET_DATASTORE_Handle *h = cls;
566 struct GNUNET_DATASTORE_QueueEntry *qe;
568 h->in_receive = GNUNET_NO;
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving reply from datastore\n");
570 if (h->skip_next_messages > 0)
572 h->skip_next_messages--;
576 if (NULL == (qe = h->queue_head))
582 qe->response_proc (h, msg);
587 * Transmit request from queue to datastore service.
589 * @param cls the 'struct GNUNET_DATASTORE_Handle'
590 * @param size number of bytes that can be copied to buf
591 * @param buf where to copy the drop message
592 * @return number of bytes written to buf
595 transmit_request (void *cls, size_t size, void *buf)
597 struct GNUNET_DATASTORE_Handle *h = cls;
598 struct GNUNET_DATASTORE_QueueEntry *qe;
602 if (NULL == (qe = h->queue_head))
603 return 0; /* no entry in queue */
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to transmit request to DATASTORE.\n");
607 GNUNET_STATISTICS_update (h->stats,
608 gettext_noop ("# transmission request failures"),
613 if (size < (msize = qe->message_size))
618 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u byte request to DATASTORE\n",
620 memcpy (buf, &qe[1], msize);
621 qe->was_transmitted = GNUNET_YES;
622 GNUNET_SCHEDULER_cancel (qe->task);
623 qe->task = GNUNET_SCHEDULER_NO_TASK;
624 GNUNET_assert (GNUNET_NO == h->in_receive);
625 h->in_receive = GNUNET_YES;
626 GNUNET_CLIENT_receive (h->client, &receive_cb, h,
627 GNUNET_TIME_absolute_get_remaining (qe->timeout));
628 #if INSANE_STATISTICS
629 GNUNET_STATISTICS_update (h->stats,
630 gettext_noop ("# bytes sent to datastore"), 1,
638 * Process entries in the queue (or do nothing if we are already
641 * @param h handle to the datastore
644 process_queue (struct GNUNET_DATASTORE_Handle *h)
646 struct GNUNET_DATASTORE_QueueEntry *qe;
648 if (NULL == (qe = h->queue_head))
650 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
651 return; /* no entry in queue */
653 if (qe->was_transmitted == GNUNET_YES)
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
656 return; /* waiting for replies */
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
661 return; /* request pending */
663 if (h->client == NULL)
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
666 return; /* waiting for reconnect */
668 if (GNUNET_YES == h->in_receive)
670 /* wait for response to previous query */
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing %u byte request to DATASTORE\n",
676 GNUNET_CLIENT_notify_transmit_ready (h->client, qe->message_size,
677 GNUNET_TIME_absolute_get_remaining
678 (qe->timeout), GNUNET_YES,
679 &transmit_request, h);
680 GNUNET_assert (GNUNET_NO == h->in_receive);
681 GNUNET_break (NULL != h->th);
686 * Dummy continuation used to do nothing (but be non-zero).
689 * @param result result
690 * @param min_expiration expiration time
691 * @param emsg error message
694 drop_status_cont (void *cls, int32_t result,
695 struct GNUNET_TIME_Absolute min_expiration,
703 * Free a queue entry. Removes the given entry from the
704 * queue and releases associated resources. Does NOT
707 * @param qe entry to free.
710 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
712 struct GNUNET_DATASTORE_Handle *h = qe->h;
714 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
715 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
717 GNUNET_SCHEDULER_cancel (qe->task);
718 qe->task = GNUNET_SCHEDULER_NO_TASK;
721 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
727 * Type of a function to call when we receive a message
731 * @param msg message received, NULL on timeout or fatal error
734 process_status_message (void *cls, const struct GNUNET_MessageHeader *msg)
736 struct GNUNET_DATASTORE_Handle *h = cls;
737 struct GNUNET_DATASTORE_QueueEntry *qe;
738 struct StatusContext rc;
739 const struct StatusMessage *sm;
744 if (NULL == (qe = h->queue_head))
753 was_transmitted = qe->was_transmitted;
754 free_queue_entry (qe);
755 if (was_transmitted == GNUNET_YES)
760 rc.cont (rc.cont_cls, GNUNET_SYSERR,
761 GNUNET_TIME_UNIT_ZERO_ABS,
762 _("Failed to receive status response from database."));
765 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
766 free_queue_entry (qe);
767 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
768 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
771 h->retry_time = GNUNET_TIME_UNIT_ZERO;
774 rc.cont (rc.cont_cls, GNUNET_SYSERR,
775 GNUNET_TIME_UNIT_ZERO_ABS,
776 _("Error reading response from datastore service"));
779 sm = (const struct StatusMessage *) msg;
780 status = ntohl (sm->status);
782 if (ntohs (msg->size) > sizeof (struct StatusMessage))
784 emsg = (const char *) &sm[1];
785 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
788 emsg = _("Invalid error message received from datastore service");
791 if ((status == GNUNET_SYSERR) && (emsg == NULL))
794 emsg = _("Invalid error message received from datastore service");
796 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received status %d/%s\n", (int) status, emsg);
797 GNUNET_STATISTICS_update (h->stats,
798 gettext_noop ("# status messages received"), 1,
800 h->retry_time = GNUNET_TIME_UNIT_ZERO;
803 rc.cont (rc.cont_cls, status,
804 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
810 * Store an item in the datastore. If the item is already present,
811 * the priorities are summed up and the higher expiration time and
812 * lower anonymity level is used.
814 * @param h handle to the datastore
815 * @param rid reservation ID to use (from "reserve"); use 0 if no
816 * prior reservation was made
817 * @param key key for the value
818 * @param size number of bytes in data
819 * @param data content stored
820 * @param type type of the content
821 * @param priority priority of the content
822 * @param anonymity anonymity-level for the content
823 * @param replication how often should the content be replicated to other peers?
824 * @param expiration expiration time for the content
825 * @param queue_priority ranking of this request in the priority queue
826 * @param max_queue_size at what queue size should this request be dropped
827 * (if other requests of higher priority are in the queue)
828 * @param timeout timeout for the operation
829 * @param cont continuation to call when done
830 * @param cont_cls closure for cont
831 * @return NULL if the entry was not queued, otherwise a handle that can be used to
832 * cancel; note that even if NULL is returned, the callback will be invoked
833 * (or rather, will already have been invoked)
835 struct GNUNET_DATASTORE_QueueEntry *
836 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h, uint32_t rid,
837 const struct GNUNET_HashCode * key, size_t size,
838 const void *data, enum GNUNET_BLOCK_Type type,
839 uint32_t priority, uint32_t anonymity,
840 uint32_t replication,
841 struct GNUNET_TIME_Absolute expiration,
842 unsigned int queue_priority, unsigned int max_queue_size,
843 struct GNUNET_TIME_Relative timeout,
844 GNUNET_DATASTORE_ContinuationWithStatus cont,
847 struct GNUNET_DATASTORE_QueueEntry *qe;
848 struct DataMessage *dm;
850 union QueueContext qc;
852 LOG (GNUNET_ERROR_TYPE_DEBUG,
853 "Asked to put %u bytes of data under key `%s' for %llu ms\n", size,
855 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
856 msize = sizeof (struct DataMessage) + size;
857 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
859 qc.sc.cont_cls = cont_cls;
860 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
861 &process_status_message, &qc);
864 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for PUT\n");
867 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# PUT requests executed"),
869 dm = (struct DataMessage *) &qe[1];
870 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
871 dm->header.size = htons (msize);
872 dm->rid = htonl (rid);
873 dm->size = htonl ((uint32_t) size);
874 dm->type = htonl (type);
875 dm->priority = htonl (priority);
876 dm->anonymity = htonl (anonymity);
877 dm->replication = htonl (replication);
878 dm->reserved = htonl (0);
879 dm->uid = GNUNET_htonll (0);
880 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
882 memcpy (&dm[1], data, size);
889 * Reserve space in the datastore. This function should be used
890 * to avoid "out of space" failures during a longer sequence of "put"
891 * operations (for example, when a file is being inserted).
893 * @param h handle to the datastore
894 * @param amount how much space (in bytes) should be reserved (for content only)
895 * @param entries how many entries will be created (to calculate per-entry overhead)
896 * @param queue_priority ranking of this request in the priority queue
897 * @param max_queue_size at what queue size should this request be dropped
898 * (if other requests of higher priority are in the queue)
899 * @param timeout how long to wait at most for a response (or before dying in queue)
900 * @param cont continuation to call when done; "success" will be set to
901 * a positive reservation value if space could be reserved.
902 * @param cont_cls closure for cont
903 * @return NULL if the entry was not queued, otherwise a handle that can be used to
904 * cancel; note that even if NULL is returned, the callback will be invoked
905 * (or rather, will already have been invoked)
907 struct GNUNET_DATASTORE_QueueEntry *
908 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h, uint64_t amount,
909 uint32_t entries, unsigned int queue_priority,
910 unsigned int max_queue_size,
911 struct GNUNET_TIME_Relative timeout,
912 GNUNET_DATASTORE_ContinuationWithStatus cont,
915 struct GNUNET_DATASTORE_QueueEntry *qe;
916 struct ReserveMessage *rm;
917 union QueueContext qc;
920 cont = &drop_status_cont;
921 LOG (GNUNET_ERROR_TYPE_DEBUG,
922 "Asked to reserve %llu bytes of data and %u entries\n",
923 (unsigned long long) amount, (unsigned int) entries);
925 qc.sc.cont_cls = cont_cls;
926 qe = make_queue_entry (h, sizeof (struct ReserveMessage), queue_priority,
927 max_queue_size, timeout, &process_status_message, &qc);
930 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry to reserve\n");
933 GNUNET_STATISTICS_update (h->stats,
934 gettext_noop ("# RESERVE requests executed"), 1,
936 rm = (struct ReserveMessage *) &qe[1];
937 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
938 rm->header.size = htons (sizeof (struct ReserveMessage));
939 rm->entries = htonl (entries);
940 rm->amount = GNUNET_htonll (amount);
947 * Signal that all of the data for which a reservation was made has
948 * been stored and that whatever excess space might have been reserved
949 * can now be released.
951 * @param h handle to the datastore
952 * @param rid reservation ID (value of "success" in original continuation
953 * from the "reserve" function).
954 * @param queue_priority ranking of this request in the priority queue
955 * @param max_queue_size at what queue size should this request be dropped
956 * (if other requests of higher priority are in the queue)
957 * @param queue_priority ranking of this request in the priority queue
958 * @param max_queue_size at what queue size should this request be dropped
959 * (if other requests of higher priority are in the queue)
960 * @param timeout how long to wait at most for a response
961 * @param cont continuation to call when done
962 * @param cont_cls closure for cont
963 * @return NULL if the entry was not queued, otherwise a handle that can be used to
964 * cancel; note that even if NULL is returned, the callback will be invoked
965 * (or rather, will already have been invoked)
967 struct GNUNET_DATASTORE_QueueEntry *
968 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
969 uint32_t rid, unsigned int queue_priority,
970 unsigned int max_queue_size,
971 struct GNUNET_TIME_Relative timeout,
972 GNUNET_DATASTORE_ContinuationWithStatus cont,
975 struct GNUNET_DATASTORE_QueueEntry *qe;
976 struct ReleaseReserveMessage *rrm;
977 union QueueContext qc;
980 cont = &drop_status_cont;
981 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
983 qc.sc.cont_cls = cont_cls;
984 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
985 queue_priority, max_queue_size, timeout,
986 &process_status_message, &qc);
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "Could not create queue entry to release reserve\n");
993 GNUNET_STATISTICS_update (h->stats,
995 ("# RELEASE RESERVE requests executed"), 1,
997 rrm = (struct ReleaseReserveMessage *) &qe[1];
998 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
999 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1000 rrm->rid = htonl (rid);
1007 * Update a value in the datastore.
1009 * @param h handle to the datastore
1010 * @param uid identifier for the value
1011 * @param priority how much to increase the priority of the value
1012 * @param expiration new expiration value should be MAX of existing and this argument
1013 * @param queue_priority ranking of this request in the priority queue
1014 * @param max_queue_size at what queue size should this request be dropped
1015 * (if other requests of higher priority are in the queue)
1016 * @param timeout how long to wait at most for a response
1017 * @param cont continuation to call when done
1018 * @param cont_cls closure for cont
1019 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1020 * cancel; note that even if NULL is returned, the callback will be invoked
1021 * (or rather, will already have been invoked)
1023 struct GNUNET_DATASTORE_QueueEntry *
1024 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h, uint64_t uid,
1026 struct GNUNET_TIME_Absolute expiration,
1027 unsigned int queue_priority,
1028 unsigned int max_queue_size,
1029 struct GNUNET_TIME_Relative timeout,
1030 GNUNET_DATASTORE_ContinuationWithStatus cont,
1033 struct GNUNET_DATASTORE_QueueEntry *qe;
1034 struct UpdateMessage *um;
1035 union QueueContext qc;
1038 cont = &drop_status_cont;
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1041 uid, (unsigned int) priority, (unsigned long long) expiration.abs_value);
1043 qc.sc.cont_cls = cont_cls;
1044 qe = make_queue_entry (h, sizeof (struct UpdateMessage), queue_priority,
1045 max_queue_size, timeout, &process_status_message, &qc);
1048 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for UPDATE\n");
1051 GNUNET_STATISTICS_update (h->stats,
1052 gettext_noop ("# UPDATE requests executed"), 1,
1054 um = (struct UpdateMessage *) &qe[1];
1055 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1056 um->header.size = htons (sizeof (struct UpdateMessage));
1057 um->priority = htonl (priority);
1058 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1059 um->uid = GNUNET_htonll (uid);
1066 * Explicitly remove some content from the database.
1067 * The "cont"inuation will be called with status
1068 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1069 * if no matching entry was found and "GNUNET_SYSERR"
1070 * on all other types of errors.
1072 * @param h handle to the datastore
1073 * @param key key for the value
1074 * @param size number of bytes in data
1075 * @param data content stored
1076 * @param queue_priority ranking of this request in the priority queue
1077 * @param max_queue_size at what queue size should this request be dropped
1078 * (if other requests of higher priority are in the queue)
1079 * @param timeout how long to wait at most for a response
1080 * @param cont continuation to call when done
1081 * @param cont_cls closure for cont
1082 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1083 * cancel; note that even if NULL is returned, the callback will be invoked
1084 * (or rather, will already have been invoked)
1086 struct GNUNET_DATASTORE_QueueEntry *
1087 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1088 const struct GNUNET_HashCode * key, size_t size,
1089 const void *data, unsigned int queue_priority,
1090 unsigned int max_queue_size,
1091 struct GNUNET_TIME_Relative timeout,
1092 GNUNET_DATASTORE_ContinuationWithStatus cont,
1095 struct GNUNET_DATASTORE_QueueEntry *qe;
1096 struct DataMessage *dm;
1098 union QueueContext qc;
1101 cont = &drop_status_cont;
1102 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to remove %u bytes under key `%s'\n",
1103 size, GNUNET_h2s (key));
1105 qc.sc.cont_cls = cont_cls;
1106 msize = sizeof (struct DataMessage) + size;
1107 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1108 qe = make_queue_entry (h, msize, queue_priority, max_queue_size, timeout,
1109 &process_status_message, &qc);
1112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not create queue entry for REMOVE\n");
1115 GNUNET_STATISTICS_update (h->stats,
1116 gettext_noop ("# REMOVE requests executed"), 1,
1118 dm = (struct DataMessage *) &qe[1];
1119 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1120 dm->header.size = htons (msize);
1121 dm->rid = htonl (0);
1122 dm->size = htonl (size);
1123 dm->type = htonl (0);
1124 dm->priority = htonl (0);
1125 dm->anonymity = htonl (0);
1126 dm->uid = GNUNET_htonll (0);
1127 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1129 memcpy (&dm[1], data, size);
1136 * Type of a function to call when we receive a message
1139 * @param cls closure
1140 * @param msg message received, NULL on timeout or fatal error
1143 process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1145 struct GNUNET_DATASTORE_Handle *h = cls;
1146 struct GNUNET_DATASTORE_QueueEntry *qe;
1147 struct ResultContext rc;
1148 const struct DataMessage *dm;
1149 int was_transmitted;
1154 GNUNET_assert (NULL != qe);
1156 was_transmitted = qe->was_transmitted;
1157 free_queue_entry (qe);
1158 if (GNUNET_YES == was_transmitted)
1160 LOG (GNUNET_ERROR_TYPE_WARNING,
1161 _("Failed to receive response from database.\n"));
1168 if (NULL != rc.proc)
1169 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1173 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1175 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1178 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1179 free_queue_entry (qe);
1180 LOG (GNUNET_ERROR_TYPE_DEBUG,
1181 "Received end of result set, new queue size is %u\n", h->queue_size);
1182 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1183 h->result_count = 0;
1185 if (rc.proc != NULL)
1186 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1191 GNUNET_assert (NULL != qe);
1193 if (GNUNET_YES != qe->was_transmitted)
1196 free_queue_entry (qe);
1197 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1199 if (rc.proc != NULL)
1200 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1204 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1205 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1206 (ntohs (msg->size) !=
1207 sizeof (struct DataMessage) +
1208 ntohl (((const struct DataMessage *) msg)->size)))
1211 free_queue_entry (qe);
1212 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1214 if (rc.proc != NULL)
1215 rc.proc (rc.proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1219 #if INSANE_STATISTICS
1220 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# Results received"), 1,
1223 dm = (const struct DataMessage *) msg;
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Received result %llu with type %u and size %u with key %s\n",
1226 (unsigned long long) GNUNET_ntohll (dm->uid), ntohl (dm->type),
1227 ntohl (dm->size), GNUNET_h2s (&dm->key));
1228 free_queue_entry (qe);
1229 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1231 if (rc.proc != NULL)
1232 rc.proc (rc.proc_cls, &dm->key, ntohl (dm->size), &dm[1], ntohl (dm->type),
1233 ntohl (dm->priority), ntohl (dm->anonymity),
1234 GNUNET_TIME_absolute_ntoh (dm->expiration),
1235 GNUNET_ntohll (dm->uid));
1240 * Get a random value from the datastore for content replication.
1241 * Returns a single, random value among those with the highest
1242 * replication score, lowering positive replication scores by one for
1243 * the chosen value (if only content with a replication score exists,
1244 * a random value is returned and replication scores are not changed).
1246 * @param h handle to the datastore
1247 * @param queue_priority ranking of this request in the priority queue
1248 * @param max_queue_size at what queue size should this request be dropped
1249 * (if other requests of higher priority are in the queue)
1250 * @param timeout how long to wait at most for a response
1251 * @param proc function to call on a random value; it
1252 * will be called once with a value (if available)
1253 * and always once with a value of NULL.
1254 * @param proc_cls closure for proc
1255 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1258 struct GNUNET_DATASTORE_QueueEntry *
1259 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1260 unsigned int queue_priority,
1261 unsigned int max_queue_size,
1262 struct GNUNET_TIME_Relative timeout,
1263 GNUNET_DATASTORE_DatumProcessor proc,
1266 struct GNUNET_DATASTORE_QueueEntry *qe;
1267 struct GNUNET_MessageHeader *m;
1268 union QueueContext qc;
1270 GNUNET_assert (NULL != proc);
1271 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to get replication entry in %llu ms\n",
1272 (unsigned long long) timeout.rel_value);
1274 qc.rc.proc_cls = proc_cls;
1275 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1276 queue_priority, max_queue_size, timeout,
1277 &process_result_message, &qc);
1280 LOG (GNUNET_ERROR_TYPE_DEBUG,
1281 "Could not create queue entry for GET REPLICATION\n");
1284 GNUNET_STATISTICS_update (h->stats,
1286 ("# GET REPLICATION requests executed"), 1,
1288 m = (struct GNUNET_MessageHeader *) &qe[1];
1289 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1290 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1297 * Get a single zero-anonymity value from the datastore.
1299 * @param h handle to the datastore
1300 * @param offset offset of the result (modulo num-results); set to
1301 * a random 64-bit value initially; then increment by
1302 * one each time; detect that all results have been found by uid
1303 * being again the first uid ever returned.
1304 * @param queue_priority ranking of this request in the priority queue
1305 * @param max_queue_size at what queue size should this request be dropped
1306 * (if other requests of higher priority are in the queue)
1307 * @param timeout how long to wait at most for a response
1308 * @param type allowed type for the operation (never zero)
1309 * @param proc function to call on a random value; it
1310 * will be called once with a value (if available)
1311 * or with NULL if none value exists.
1312 * @param proc_cls closure for proc
1313 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1316 struct GNUNET_DATASTORE_QueueEntry *
1317 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1319 unsigned int queue_priority,
1320 unsigned int max_queue_size,
1321 struct GNUNET_TIME_Relative timeout,
1322 enum GNUNET_BLOCK_Type type,
1323 GNUNET_DATASTORE_DatumProcessor proc,
1326 struct GNUNET_DATASTORE_QueueEntry *qe;
1327 struct GetZeroAnonymityMessage *m;
1328 union QueueContext qc;
1330 GNUNET_assert (NULL != proc);
1331 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1334 (unsigned long long) offset, type,
1335 (unsigned long long) timeout.rel_value);
1337 qc.rc.proc_cls = proc_cls;
1338 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1339 queue_priority, max_queue_size, timeout,
1340 &process_result_message, &qc);
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "Could not create queue entry for zero-anonymity procation\n");
1347 GNUNET_STATISTICS_update (h->stats,
1349 ("# GET ZERO ANONYMITY requests executed"), 1,
1351 m = (struct GetZeroAnonymityMessage *) &qe[1];
1352 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1353 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1354 m->type = htonl ((uint32_t) type);
1355 m->offset = GNUNET_htonll (offset);
1362 * Get a result for a particular key from the datastore. The processor
1363 * will only be called once.
1365 * @param h handle to the datastore
1366 * @param offset offset of the result (modulo num-results); set to
1367 * a random 64-bit value initially; then increment by
1368 * one each time; detect that all results have been found by uid
1369 * being again the first uid ever returned.
1370 * @param key maybe NULL (to match all entries)
1371 * @param type desired type, 0 for any
1372 * @param queue_priority ranking of this request in the priority queue
1373 * @param max_queue_size at what queue size should this request be dropped
1374 * (if other requests of higher priority are in the queue)
1375 * @param timeout how long to wait at most for a response
1376 * @param proc function to call on each matching value;
1377 * will be called once with a NULL value at the end
1378 * @param proc_cls closure for proc
1379 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1382 struct GNUNET_DATASTORE_QueueEntry *
1383 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h, uint64_t offset,
1384 const struct GNUNET_HashCode * key,
1385 enum GNUNET_BLOCK_Type type,
1386 unsigned int queue_priority,
1387 unsigned int max_queue_size,
1388 struct GNUNET_TIME_Relative timeout,
1389 GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
1391 struct GNUNET_DATASTORE_QueueEntry *qe;
1392 struct GetMessage *gm;
1393 union QueueContext qc;
1395 GNUNET_assert (NULL != proc);
1396 LOG (GNUNET_ERROR_TYPE_DEBUG,
1397 "Asked to look for data of type %u under key `%s'\n",
1398 (unsigned int) type, GNUNET_h2s (key));
1400 qc.rc.proc_cls = proc_cls;
1401 qe = make_queue_entry (h, sizeof (struct GetMessage), queue_priority,
1402 max_queue_size, timeout, &process_result_message, &qc);
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, "Could not queue request for `%s'\n",
1409 #if INSANE_STATISTICS
1410 GNUNET_STATISTICS_update (h->stats, gettext_noop ("# GET requests executed"),
1413 gm = (struct GetMessage *) &qe[1];
1414 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1415 gm->type = htonl (type);
1416 gm->offset = GNUNET_htonll (offset);
1419 gm->header.size = htons (sizeof (struct GetMessage));
1425 htons (sizeof (struct GetMessage) - sizeof (struct GNUNET_HashCode));
1433 * Cancel a datastore operation. The final callback from the
1434 * operation must not have been done yet.
1436 * @param qe operation to cancel
1439 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1441 struct GNUNET_DATASTORE_Handle *h;
1443 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Pending DATASTORE request %p cancelled (%d, %d)\n", qe,
1447 qe->was_transmitted, h->queue_head == qe);
1448 if (GNUNET_YES == qe->was_transmitted)
1450 free_queue_entry (qe);
1451 h->skip_next_messages++;
1454 free_queue_entry (qe);
1459 /* end of datastore_api.c */