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"
35 * If a client stopped asking for more results, how many more do
36 * we receive from the DB before killing the connection? Trade-off
37 * between re-doing TCP handshakes and (needlessly) receiving
40 #define MAX_EXCESS_RESULTS 8
43 * Context for processing status messages.
48 * Continuation to call with the status.
50 GNUNET_DATASTORE_ContinuationWithStatus cont;
61 * Context for processing result messages.
66 * Iterator to call with the result.
68 GNUNET_DATASTORE_Iterator iter;
79 * Context for a queue operation.
84 struct StatusContext sc;
86 struct ResultContext rc;
93 * Entry in our priority queue.
95 struct GNUNET_DATASTORE_QueueEntry
99 * This is a linked list.
101 struct GNUNET_DATASTORE_QueueEntry *next;
104 * This is a linked list.
106 struct GNUNET_DATASTORE_QueueEntry *prev;
109 * Handle to the master context.
111 struct GNUNET_DATASTORE_Handle *h;
114 * Response processor (NULL if we are not waiting for a response).
115 * This struct should be used for the closure, function-specific
116 * arguments can be passed via 'qc'.
118 GNUNET_CLIENT_MessageHandler response_proc;
121 * Function to call after transmission of the request.
123 GNUNET_DATASTORE_ContinuationWithStatus cont;
126 * Closure for 'cont'.
131 * Context for the operation.
133 union QueueContext qc;
136 * Task for timeout signalling.
138 GNUNET_SCHEDULER_TaskIdentifier task;
141 * Timeout for the current operation.
143 struct GNUNET_TIME_Absolute timeout;
146 * Priority in the queue.
148 unsigned int priority;
151 * Maximum allowed length of queue (otherwise
152 * this request should be discarded).
154 unsigned int max_queue;
157 * Number of bytes in the request message following
158 * this struct. 32-bit value for nicer memory
159 * access (and overall struct alignment).
161 uint32_t message_size;
164 * Has this message been transmitted to the service?
165 * Only ever GNUNET_YES for the head of the queue.
166 * Note that the overall struct should end at a
167 * multiple of 64 bits.
172 * Are we expecting a single message in response to this
173 * request (and, if it is data, no 'END' message)?
180 * Handle to the datastore service.
182 struct GNUNET_DATASTORE_Handle
188 const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Current connection to the datastore service.
193 struct GNUNET_CLIENT_Connection *client;
196 * Handle for statistics.
198 struct GNUNET_STATISTICS_Handle *stats;
201 * Current transmit handle.
203 struct GNUNET_CLIENT_TransmitHandle *th;
206 * Current head of priority queue.
208 struct GNUNET_DATASTORE_QueueEntry *queue_head;
211 * Current tail of priority queue.
213 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
216 * Task for trying to reconnect.
218 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
221 * How quickly should we retry? Used for exponential back-off on
224 struct GNUNET_TIME_Relative retry_time;
227 * Number of entries in the queue.
229 unsigned int queue_size;
232 * Number of results we're receiving for the current query
233 * after application stopped to care. Used to determine when
234 * to reset the connection.
236 unsigned int result_count;
239 * Are we currently trying to receive from the service?
244 * We should either receive (and ignore) an 'END' message or force a
245 * disconnect for the next message from the service.
247 unsigned int expect_end_or_disconnect;
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
261 GNUNET_CONFIGURATION_Handle
264 struct GNUNET_CLIENT_Connection *c;
265 struct GNUNET_DATASTORE_Handle *h;
267 c = GNUNET_CLIENT_connect ("datastore", cfg);
269 return NULL; /* oops */
270 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
271 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
274 h->stats = GNUNET_STATISTICS_create ("datastore-api",
281 * Transmit DROP message to datastore service.
283 * @param cls the 'struct GNUNET_DATASTORE_Handle'
284 * @param size number of bytes that can be copied to buf
285 * @param buf where to copy the drop message
286 * @return number of bytes written to buf
289 transmit_drop (void *cls,
293 struct GNUNET_DATASTORE_Handle *h = cls;
294 struct GNUNET_MessageHeader *hdr;
298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
299 _("Failed to transmit request to drop database.\n"));
300 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
303 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
305 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
306 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
307 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
308 return sizeof(struct GNUNET_MessageHeader);
313 * Disconnect from the datastore service (and free
314 * associated resources).
316 * @param h handle to the datastore
317 * @param drop set to GNUNET_YES to delete all data in datastore (!)
320 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
323 struct GNUNET_DATASTORE_QueueEntry *qe;
325 if (h->client != NULL)
327 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
330 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
332 GNUNET_SCHEDULER_cancel (h->reconnect_task);
333 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
335 while (NULL != (qe = h->queue_head))
337 GNUNET_assert (NULL != qe->response_proc);
338 qe->response_proc (qe, NULL);
340 if (GNUNET_YES == drop)
342 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
343 if (h->client != NULL)
346 GNUNET_CLIENT_notify_transmit_ready (h->client,
347 sizeof(struct GNUNET_MessageHeader),
348 GNUNET_TIME_UNIT_MINUTES,
353 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
357 GNUNET_STATISTICS_destroy (h->stats,
364 * A request has timed out (before being transmitted to the service).
366 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
367 * @param tc scheduler context
370 timeout_queue_entry (void *cls,
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
373 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
375 GNUNET_STATISTICS_update (qe->h->stats,
376 gettext_noop ("# queue entry timeouts"),
379 qe->task = GNUNET_SCHEDULER_NO_TASK;
380 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
381 qe->response_proc (qe, NULL);
386 * Create a new entry for our priority queue (and possibly discard other entires if
387 * the queue is getting too long).
389 * @param h handle to the datastore
390 * @param msize size of the message to queue
391 * @param queue_priority priority of the entry
392 * @param max_queue_size at what queue size should this request be dropped
393 * (if other requests of higher priority are in the queue)
394 * @param timeout timeout for the operation
395 * @param response_proc function to call with replies (can be NULL)
396 * @param qc client context (NOT a closure for response_proc)
397 * @return NULL if the queue is full (and this entry was dropped)
399 static struct GNUNET_DATASTORE_QueueEntry *
400 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
402 unsigned int queue_priority,
403 unsigned int max_queue_size,
404 struct GNUNET_TIME_Relative timeout,
405 GNUNET_CLIENT_MessageHandler response_proc,
406 const union QueueContext *qc)
408 struct GNUNET_DATASTORE_QueueEntry *ret;
409 struct GNUNET_DATASTORE_QueueEntry *pos;
414 while ( (pos != NULL) &&
415 (c < max_queue_size) &&
416 (pos->priority >= queue_priority) )
421 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
423 ret->response_proc = response_proc;
425 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
426 ret->priority = queue_priority;
427 ret->max_queue = max_queue_size;
428 ret->message_size = msize;
429 ret->was_transmitted = GNUNET_NO;
432 /* append at the tail */
438 /* do not insert at HEAD if HEAD query was already
439 transmitted and we are still receiving replies! */
440 if ( (pos == NULL) &&
441 (h->queue_head->was_transmitted) )
445 GNUNET_STATISTICS_update (h->stats,
446 gettext_noop ("# queue entries created"),
449 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
454 if (c > max_queue_size)
456 GNUNET_STATISTICS_update (h->stats,
457 gettext_noop ("# queue overflows"),
460 response_proc (ret, NULL);
463 ret->task = GNUNET_SCHEDULER_add_delayed (timeout,
464 &timeout_queue_entry,
469 if (pos->max_queue < h->queue_size)
471 GNUNET_assert (pos->response_proc != NULL);
472 pos->response_proc (pos, NULL);
482 * Process entries in the queue (or do nothing if we are already
485 * @param h handle to the datastore
488 process_queue (struct GNUNET_DATASTORE_Handle *h);
492 * Try reconnecting to the datastore service.
494 * @param cls the 'struct GNUNET_DATASTORE_Handle'
495 * @param tc scheduler context
498 try_reconnect (void *cls,
499 const struct GNUNET_SCHEDULER_TaskContext *tc)
501 struct GNUNET_DATASTORE_Handle *h = cls;
503 if (h->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
504 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
506 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
507 if (h->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
508 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
509 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
510 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
511 if (h->client == NULL)
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "DATASTORE reconnect failed (fatally)\n");
517 GNUNET_STATISTICS_update (h->stats,
518 gettext_noop ("# datastore connections (re)created"),
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Reconnected to DATASTORE\n");
530 * Disconnect from the service and then try reconnecting to the datastore service
533 * @param h handle to datastore to disconnect and reconnect
536 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
538 if (h->client == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "client NULL in disconnect, will not try to reconnect\n");
547 GNUNET_STATISTICS_update (stats,
548 gettext_noop ("# reconnected to DATASTORE"),
552 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
554 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time,
561 * Transmit request from queue to datastore service.
563 * @param cls the 'struct GNUNET_DATASTORE_Handle'
564 * @param size number of bytes that can be copied to buf
565 * @param buf where to copy the drop message
566 * @return number of bytes written to buf
569 transmit_request (void *cls,
573 struct GNUNET_DATASTORE_Handle *h = cls;
574 struct GNUNET_DATASTORE_QueueEntry *qe;
578 if (NULL == (qe = h->queue_head))
579 return 0; /* no entry in queue */
582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
583 _("Failed to transmit request to DATASTORE.\n"));
584 GNUNET_STATISTICS_update (h->stats,
585 gettext_noop ("# transmission request failures"),
591 if (size < (msize = qe->message_size))
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Transmitting %u byte request to DATASTORE\n",
601 memcpy (buf, &qe[1], msize);
602 qe->was_transmitted = GNUNET_YES;
603 GNUNET_SCHEDULER_cancel (qe->task);
604 qe->task = GNUNET_SCHEDULER_NO_TASK;
605 h->in_receive = GNUNET_YES;
606 GNUNET_CLIENT_receive (h->client,
609 GNUNET_TIME_absolute_get_remaining (qe->timeout));
610 GNUNET_STATISTICS_update (h->stats,
611 gettext_noop ("# bytes sent to datastore"),
619 * Process entries in the queue (or do nothing if we are already
622 * @param h handle to the datastore
625 process_queue (struct GNUNET_DATASTORE_Handle *h)
627 struct GNUNET_DATASTORE_QueueEntry *qe;
629 if (NULL == (qe = h->queue_head))
631 #if DEBUG_DATASTORE > 1
632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 return; /* no entry in queue */
637 if (qe->was_transmitted == GNUNET_YES)
639 #if DEBUG_DATASTORE > 1
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Head request already transmitted\n");
643 return; /* waiting for replies */
647 #if DEBUG_DATASTORE > 1
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Pending transmission request\n");
651 return; /* request pending */
653 if (h->client == NULL)
655 #if DEBUG_DATASTORE > 1
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 return; /* waiting for reconnect */
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Queueing %u byte request to DATASTORE\n",
666 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
668 GNUNET_TIME_absolute_get_remaining (qe->timeout),
676 * Dummy continuation used to do nothing (but be non-zero).
679 * @param result result
680 * @param emsg error message
683 drop_status_cont (void *cls, int32_t result, const char *emsg)
690 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
692 struct GNUNET_DATASTORE_Handle *h = qe->h;
694 GNUNET_CONTAINER_DLL_remove (h->queue_head,
697 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
699 GNUNET_SCHEDULER_cancel (qe->task);
700 qe->task = GNUNET_SCHEDULER_NO_TASK;
707 * Type of a function to call when we receive a message
711 * @param msg message received, NULL on timeout or fatal error
714 process_status_message (void *cls,
716 GNUNET_MessageHeader * msg)
718 struct GNUNET_DATASTORE_Handle *h = cls;
719 struct GNUNET_DATASTORE_QueueEntry *qe;
720 struct StatusContext rc;
721 const struct StatusMessage *sm;
726 h->in_receive = GNUNET_NO;
727 if (NULL == (qe = h->queue_head))
733 was_transmitted = qe->was_transmitted;
737 free_queue_entry (qe);
738 if (NULL == h->client)
739 return; /* forced disconnect */
741 rc.cont (rc.cont_cls,
743 _("Failed to receive status response from database."));
744 if (was_transmitted == GNUNET_YES)
748 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
749 free_queue_entry (qe);
750 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
751 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
754 h->retry_time = GNUNET_TIME_UNIT_ZERO;
757 rc.cont (rc.cont_cls,
759 _("Error reading response from datastore service"));
762 sm = (const struct StatusMessage*) msg;
763 status = ntohl(sm->status);
765 if (ntohs(msg->size) > sizeof(struct StatusMessage))
767 emsg = (const char*) &sm[1];
768 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
771 emsg = _("Invalid error message received from datastore service");
774 if ( (status == GNUNET_SYSERR) &&
778 emsg = _("Invalid error message received from datastore service");
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Received status %d/%s\n",
786 GNUNET_STATISTICS_update (h->stats,
787 gettext_noop ("# status messages received"),
790 h->retry_time.rel_value = 0;
793 rc.cont (rc.cont_cls,
800 * Store an item in the datastore. If the item is already present,
801 * the priorities are summed up and the higher expiration time and
802 * lower anonymity level is used.
804 * @param h handle to the datastore
805 * @param rid reservation ID to use (from "reserve"); use 0 if no
806 * prior reservation was made
807 * @param key key for the value
808 * @param size number of bytes in data
809 * @param data content stored
810 * @param type type of the content
811 * @param priority priority of the content
812 * @param anonymity anonymity-level for the content
813 * @param replication how often should the content be replicated to other peers?
814 * @param expiration expiration time for the content
815 * @param queue_priority ranking of this request in the priority queue
816 * @param max_queue_size at what queue size should this request be dropped
817 * (if other requests of higher priority are in the queue)
818 * @param timeout timeout for the operation
819 * @param cont continuation to call when done
820 * @param cont_cls closure for cont
821 * @return NULL if the entry was not queued, otherwise a handle that can be used to
822 * cancel; note that even if NULL is returned, the callback will be invoked
823 * (or rather, will already have been invoked)
825 struct GNUNET_DATASTORE_QueueEntry *
826 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
828 const GNUNET_HashCode * key,
831 enum GNUNET_BLOCK_Type type,
834 uint32_t replication,
835 struct GNUNET_TIME_Absolute expiration,
836 unsigned int queue_priority,
837 unsigned int max_queue_size,
838 struct GNUNET_TIME_Relative timeout,
839 GNUNET_DATASTORE_ContinuationWithStatus cont,
842 struct GNUNET_DATASTORE_QueueEntry *qe;
843 struct DataMessage *dm;
845 union QueueContext qc;
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Asked to put %u bytes of data under key `%s'\n",
853 msize = sizeof(struct DataMessage) + size;
854 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
856 qc.sc.cont_cls = cont_cls;
857 qe = make_queue_entry (h, msize,
858 queue_priority, max_queue_size, timeout,
859 &process_status_message, &qc);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Could not create queue entry for PUT\n");
868 GNUNET_STATISTICS_update (h->stats,
869 gettext_noop ("# PUT requests executed"),
872 dm = (struct DataMessage* ) &qe[1];
873 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
874 dm->header.size = htons(msize);
875 dm->rid = htonl(rid);
876 dm->size = htonl( (uint32_t) size);
877 dm->type = htonl(type);
878 dm->priority = htonl(priority);
879 dm->anonymity = htonl(anonymity);
880 dm->uid = GNUNET_htonll(0);
881 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
883 memcpy (&dm[1], data, size);
890 * Reserve space in the datastore. This function should be used
891 * to avoid "out of space" failures during a longer sequence of "put"
892 * operations (for example, when a file is being inserted).
894 * @param h handle to the datastore
895 * @param amount how much space (in bytes) should be reserved (for content only)
896 * @param entries how many entries will be created (to calculate per-entry overhead)
897 * @param queue_priority ranking of this request in the priority queue
898 * @param max_queue_size at what queue size should this request be dropped
899 * (if other requests of higher priority are in the queue)
900 * @param timeout how long to wait at most for a response (or before dying in queue)
901 * @param cont continuation to call when done; "success" will be set to
902 * a positive reservation value if space could be reserved.
903 * @param cont_cls closure for cont
904 * @return NULL if the entry was not queued, otherwise a handle that can be used to
905 * cancel; note that even if NULL is returned, the callback will be invoked
906 * (or rather, will already have been invoked)
908 struct GNUNET_DATASTORE_QueueEntry *
909 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
912 unsigned int queue_priority,
913 unsigned int max_queue_size,
914 struct GNUNET_TIME_Relative timeout,
915 GNUNET_DATASTORE_ContinuationWithStatus cont,
918 struct GNUNET_DATASTORE_QueueEntry *qe;
919 struct ReserveMessage *rm;
920 union QueueContext qc;
923 cont = &drop_status_cont;
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Asked to reserve %llu bytes of data and %u entries'\n",
927 (unsigned long long) amount,
928 (unsigned int) entries);
931 qc.sc.cont_cls = cont_cls;
932 qe = make_queue_entry (h, sizeof(struct ReserveMessage),
933 queue_priority, max_queue_size, timeout,
934 &process_status_message, &qc);
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "Could not create queue entry to reserve\n");
943 GNUNET_STATISTICS_update (h->stats,
944 gettext_noop ("# RESERVE requests executed"),
947 rm = (struct ReserveMessage*) &qe[1];
948 rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
949 rm->header.size = htons(sizeof (struct ReserveMessage));
950 rm->entries = htonl(entries);
951 rm->amount = GNUNET_htonll(amount);
958 * Signal that all of the data for which a reservation was made has
959 * been stored and that whatever excess space might have been reserved
960 * can now be released.
962 * @param h handle to the datastore
963 * @param rid reservation ID (value of "success" in original continuation
964 * from the "reserve" function).
965 * @param queue_priority ranking of this request in the priority queue
966 * @param max_queue_size at what queue size should this request be dropped
967 * (if other requests of higher priority are in the queue)
968 * @param queue_priority ranking of this request in the priority queue
969 * @param max_queue_size at what queue size should this request be dropped
970 * (if other requests of higher priority are in the queue)
971 * @param timeout how long to wait at most for a response
972 * @param cont continuation to call when done
973 * @param cont_cls closure for cont
974 * @return NULL if the entry was not queued, otherwise a handle that can be used to
975 * cancel; note that even if NULL is returned, the callback will be invoked
976 * (or rather, will already have been invoked)
978 struct GNUNET_DATASTORE_QueueEntry *
979 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
981 unsigned int queue_priority,
982 unsigned int max_queue_size,
983 struct GNUNET_TIME_Relative timeout,
984 GNUNET_DATASTORE_ContinuationWithStatus cont,
987 struct GNUNET_DATASTORE_QueueEntry *qe;
988 struct ReleaseReserveMessage *rrm;
989 union QueueContext qc;
992 cont = &drop_status_cont;
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Asked to release reserve %d\n",
999 qc.sc.cont_cls = cont_cls;
1000 qe = make_queue_entry (h, sizeof(struct ReleaseReserveMessage),
1001 queue_priority, max_queue_size, timeout,
1002 &process_status_message, &qc);
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "Could not create queue entry to release reserve\n");
1011 GNUNET_STATISTICS_update (h->stats,
1012 gettext_noop ("# RELEASE RESERVE requests executed"),
1015 rrm = (struct ReleaseReserveMessage*) &qe[1];
1016 rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1017 rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
1018 rrm->rid = htonl(rid);
1025 * Update a value in the datastore.
1027 * @param h handle to the datastore
1028 * @param uid identifier for the value
1029 * @param priority how much to increase the priority of the value
1030 * @param expiration new expiration value should be MAX of existing and this argument
1031 * @param queue_priority ranking of this request in the priority queue
1032 * @param max_queue_size at what queue size should this request be dropped
1033 * (if other requests of higher priority are in the queue)
1034 * @param timeout how long to wait at most for a response
1035 * @param cont continuation to call when done
1036 * @param cont_cls closure for cont
1037 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1038 * cancel; note that even if NULL is returned, the callback will be invoked
1039 * (or rather, will already have been invoked)
1041 struct GNUNET_DATASTORE_QueueEntry *
1042 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1045 struct GNUNET_TIME_Absolute expiration,
1046 unsigned int queue_priority,
1047 unsigned int max_queue_size,
1048 struct GNUNET_TIME_Relative timeout,
1049 GNUNET_DATASTORE_ContinuationWithStatus cont,
1052 struct GNUNET_DATASTORE_QueueEntry *qe;
1053 struct UpdateMessage *um;
1054 union QueueContext qc;
1057 cont = &drop_status_cont;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1062 (unsigned int) priority,
1063 (unsigned long long) expiration.abs_value);
1066 qc.sc.cont_cls = cont_cls;
1067 qe = make_queue_entry (h, sizeof(struct UpdateMessage),
1068 queue_priority, max_queue_size, timeout,
1069 &process_status_message, &qc);
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Could not create queue entry for UPDATE\n");
1078 GNUNET_STATISTICS_update (h->stats,
1079 gettext_noop ("# UPDATE requests executed"),
1082 um = (struct UpdateMessage*) &qe[1];
1083 um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1084 um->header.size = htons(sizeof (struct UpdateMessage));
1085 um->priority = htonl(priority);
1086 um->expiration = GNUNET_TIME_absolute_hton(expiration);
1087 um->uid = GNUNET_htonll(uid);
1094 * Explicitly remove some content from the database.
1095 * The "cont"inuation will be called with status
1096 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1097 * if no matching entry was found and "GNUNET_SYSERR"
1098 * on all other types of errors.
1100 * @param h handle to the datastore
1101 * @param key key for the value
1102 * @param size number of bytes in data
1103 * @param data content stored
1104 * @param queue_priority ranking of this request in the priority queue
1105 * @param max_queue_size at what queue size should this request be dropped
1106 * (if other requests of higher priority are in the queue)
1107 * @param timeout how long to wait at most for a response
1108 * @param cont continuation to call when done
1109 * @param cont_cls closure for cont
1110 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1111 * cancel; note that even if NULL is returned, the callback will be invoked
1112 * (or rather, will already have been invoked)
1114 struct GNUNET_DATASTORE_QueueEntry *
1115 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1116 const GNUNET_HashCode *key,
1119 unsigned int queue_priority,
1120 unsigned int max_queue_size,
1121 struct GNUNET_TIME_Relative timeout,
1122 GNUNET_DATASTORE_ContinuationWithStatus cont,
1125 struct GNUNET_DATASTORE_QueueEntry *qe;
1126 struct DataMessage *dm;
1128 union QueueContext qc;
1131 cont = &drop_status_cont;
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Asked to remove %u bytes under key `%s'\n",
1139 qc.sc.cont_cls = cont_cls;
1140 msize = sizeof(struct DataMessage) + size;
1141 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1142 qe = make_queue_entry (h, msize,
1143 queue_priority, max_queue_size, timeout,
1144 &process_status_message, &qc);
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1149 "Could not create queue entry for REMOVE\n");
1153 GNUNET_STATISTICS_update (h->stats,
1154 gettext_noop ("# REMOVE requests executed"),
1157 dm = (struct DataMessage*) &qe[1];
1158 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1159 dm->header.size = htons(msize);
1161 dm->size = htonl(size);
1162 dm->type = htonl(0);
1163 dm->priority = htonl(0);
1164 dm->anonymity = htonl(0);
1165 dm->uid = GNUNET_htonll(0);
1166 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
1168 memcpy (&dm[1], data, size);
1175 * Type of a function to call when we receive a message
1178 * @param cls closure
1179 * @param msg message received, NULL on timeout or fatal error
1182 process_result_message (void *cls,
1183 const struct GNUNET_MessageHeader *msg)
1185 struct GNUNET_DATASTORE_Handle *h = cls;
1186 struct GNUNET_DATASTORE_QueueEntry *qe;
1187 struct ResultContext rc;
1188 const struct DataMessage *dm;
1189 int was_transmitted;
1191 h->in_receive = GNUNET_NO;
1194 if (NULL != (qe = h->queue_head))
1196 was_transmitted = qe->was_transmitted;
1197 free_queue_entry (qe);
1199 if (was_transmitted == GNUNET_YES)
1201 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1202 _("Failed to receive response from database.\n"));
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Request dropped due to finite datastore queue length.\n");
1212 if (rc.iter != NULL)
1213 rc.iter (rc.iter_cls,
1214 NULL, 0, NULL, 0, 0, 0,
1215 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1219 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1221 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
1222 if (h->expect_end_or_disconnect > 0)
1224 h->expect_end_or_disconnect--;
1230 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1231 free_queue_entry (qe);
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "Received end of result set, new queue size is %u\n",
1237 if (rc.iter != NULL)
1238 rc.iter (rc.iter_cls,
1239 NULL, 0, NULL, 0, 0, 0,
1240 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1241 h->retry_time.rel_value = 0;
1242 h->result_count = 0;
1246 if (h->expect_end_or_disconnect > 0)
1248 /* only 'END' allowed, must reconnect */
1249 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1255 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1256 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
1257 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1258 (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
1261 free_queue_entry (qe);
1262 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1264 if (rc.iter != NULL)
1265 rc.iter (rc.iter_cls,
1266 NULL, 0, NULL, 0, 0, 0,
1267 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1270 GNUNET_STATISTICS_update (h->stats,
1271 gettext_noop ("# Results received"),
1274 if (rc.iter == NULL)
1277 GNUNET_STATISTICS_update (h->stats,
1278 gettext_noop ("# Excess results received"),
1281 if (h->result_count > MAX_EXCESS_RESULTS)
1283 free_queue_entry (qe);
1284 GNUNET_STATISTICS_update (h->stats,
1285 gettext_noop ("# Forced database connection resets"),
1288 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1292 if (GNUNET_YES == qe->one_shot)
1293 free_queue_entry (qe);
1295 GNUNET_DATASTORE_iterate_get_next (h);
1298 dm = (const struct DataMessage*) msg;
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Received result %llu with type %u and size %u with key %s\n",
1302 (unsigned long long) GNUNET_ntohll(dm->uid),
1305 GNUNET_h2s(&dm->key));
1307 if (GNUNET_YES == qe->one_shot)
1308 free_queue_entry (qe);
1309 h->retry_time.rel_value = 0;
1310 rc.iter (rc.iter_cls,
1315 ntohl(dm->priority),
1316 ntohl(dm->anonymity),
1317 GNUNET_TIME_absolute_ntoh(dm->expiration),
1318 GNUNET_ntohll(dm->uid));
1323 * Get a random value from the datastore for content replication.
1324 * Returns a single, random value among those with the highest
1325 * replication score, lowering positive replication scores by one for
1326 * the chosen value (if only content with a replication score exists,
1327 * a random value is returned and replication scores are not changed).
1329 * @param h handle to the datastore
1330 * @param queue_priority ranking of this request in the priority queue
1331 * @param max_queue_size at what queue size should this request be dropped
1332 * (if other requests of higher priority are in the queue)
1333 * @param timeout how long to wait at most for a response
1334 * @param iter function to call on a random value; it
1335 * will be called once with a value (if available)
1336 * and always once with a value of NULL.
1337 * @param iter_cls closure for iter
1338 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1339 * cancel; note that even if NULL is returned, the callback will be invoked
1340 * (or rather, will already have been invoked)
1342 struct GNUNET_DATASTORE_QueueEntry *
1343 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1344 unsigned int queue_priority,
1345 unsigned int max_queue_size,
1346 struct GNUNET_TIME_Relative timeout,
1347 GNUNET_DATASTORE_Iterator iter,
1350 struct GNUNET_DATASTORE_QueueEntry *qe;
1351 struct GNUNET_MessageHeader *m;
1352 union QueueContext qc;
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 "Asked to get replication entry in %llu ms\n",
1357 (unsigned long long) timeout.rel_value);
1360 qc.rc.iter_cls = iter_cls;
1361 qe = make_queue_entry (h, sizeof(struct GNUNET_MessageHeader),
1362 queue_priority, max_queue_size, timeout,
1363 &process_result_message, &qc);
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Could not create queue entry for GET REPLICATION\n");
1372 qe->one_shot = GNUNET_YES;
1373 GNUNET_STATISTICS_update (h->stats,
1374 gettext_noop ("# GET REPLICATION requests executed"),
1377 m = (struct GNUNET_MessageHeader*) &qe[1];
1378 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1379 m->size = htons(sizeof (struct GNUNET_MessageHeader));
1386 * Get a zero-anonymity value from the datastore.
1388 * @param h handle to the datastore
1389 * @param queue_priority ranking of this request in the priority queue
1390 * @param max_queue_size at what queue size should this request be dropped
1391 * (if other requests of higher priority are in the queue)
1392 * @param timeout how long to wait at most for a response
1393 * @param type allowed type for the operation
1394 * @param iter function to call on a random value; it
1395 * will be called once with a value (if available)
1396 * and always once with a value of NULL.
1397 * @param iter_cls closure for iter
1398 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1399 * cancel; note that even if NULL is returned, the callback will be invoked
1400 * (or rather, will already have been invoked)
1402 struct GNUNET_DATASTORE_QueueEntry *
1403 GNUNET_DATASTORE_iterate_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1404 unsigned int queue_priority,
1405 unsigned int max_queue_size,
1406 struct GNUNET_TIME_Relative timeout,
1407 enum GNUNET_BLOCK_Type type,
1408 GNUNET_DATASTORE_Iterator iter,
1411 struct GNUNET_DATASTORE_QueueEntry *qe;
1412 struct GetZeroAnonymityMessage *m;
1413 union QueueContext qc;
1415 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Asked to get zero-anonymity entry in %llu ms\n",
1419 (unsigned long long) timeout.rel_value);
1422 qc.rc.iter_cls = iter_cls;
1423 qe = make_queue_entry (h, sizeof(struct GetZeroAnonymityMessage),
1424 queue_priority, max_queue_size, timeout,
1425 &process_result_message, &qc);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "Could not create queue entry for zero-anonymity iteration\n");
1434 GNUNET_STATISTICS_update (h->stats,
1435 gettext_noop ("# GET ZERO ANONYMITY requests executed"),
1438 m = (struct GetZeroAnonymityMessage*) &qe[1];
1439 m->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1440 m->header.size = htons(sizeof (struct GetZeroAnonymityMessage));
1441 m->type = htonl ((uint32_t) type);
1449 * Iterate over the results for a particular key
1450 * in the datastore. The iterator will only be called
1451 * once initially; if the first call did contain a
1452 * result, further results can be obtained by calling
1453 * "GNUNET_DATASTORE_iterate_get_next" with the given argument.
1455 * @param h handle to the datastore
1456 * @param key maybe NULL (to match all entries)
1457 * @param type desired type, 0 for any
1458 * @param queue_priority ranking of this request in the priority queue
1459 * @param max_queue_size at what queue size should this request be dropped
1460 * (if other requests of higher priority are in the queue)
1461 * @param timeout how long to wait at most for a response
1462 * @param iter function to call on each matching value;
1463 * will be called once with a NULL value at the end
1464 * @param iter_cls closure for iter
1465 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1466 * cancel; note that even if NULL is returned, the callback will be invoked
1467 * (or rather, will already have been invoked)
1469 struct GNUNET_DATASTORE_QueueEntry *
1470 GNUNET_DATASTORE_iterate_key (struct GNUNET_DATASTORE_Handle *h,
1471 const GNUNET_HashCode * key,
1472 enum GNUNET_BLOCK_Type type,
1473 unsigned int queue_priority,
1474 unsigned int max_queue_size,
1475 struct GNUNET_TIME_Relative timeout,
1476 GNUNET_DATASTORE_Iterator iter,
1479 struct GNUNET_DATASTORE_QueueEntry *qe;
1480 struct GetMessage *gm;
1481 union QueueContext qc;
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Asked to look for data of type %u under key `%s'\n",
1486 (unsigned int) type,
1490 qc.rc.iter_cls = iter_cls;
1491 qe = make_queue_entry (h, sizeof(struct GetMessage),
1492 queue_priority, max_queue_size, timeout,
1493 &process_result_message, &qc);
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "Could not queue request for `%s'\n",
1503 GNUNET_STATISTICS_update (h->stats,
1504 gettext_noop ("# GET requests executed"),
1507 gm = (struct GetMessage*) &qe[1];
1508 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1509 gm->type = htonl(type);
1512 gm->header.size = htons(sizeof (struct GetMessage));
1517 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
1525 * Function called to trigger obtaining the next result
1526 * from the datastore.
1528 * @param h handle to the datastore
1531 GNUNET_DATASTORE_iterate_get_next (struct GNUNET_DATASTORE_Handle *h)
1533 struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
1535 h->in_receive = GNUNET_YES;
1536 GNUNET_CLIENT_receive (h->client,
1537 &process_result_message,
1539 GNUNET_TIME_absolute_get_remaining (qe->timeout));
1544 * Cancel a datastore operation. The final callback from the
1545 * operation must not have been done yet.
1547 * @param qe operation to cancel
1550 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1552 struct GNUNET_DATASTORE_Handle *h;
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1559 qe->was_transmitted,
1560 h->queue_head == qe);
1562 if (GNUNET_YES == qe->was_transmitted)
1564 free_queue_entry (qe);
1565 h->expect_end_or_disconnect++;
1568 free_queue_entry (qe);
1573 /* end of datastore_api.c */