fixing reconnect issues
[oweals/gnunet.git] / src / datastore / gnunet-service-datastore.c
index 8a3510946af3faffc58475f26c39ced2f452ee82..6c6155831909dd8b01d485d1de5ac747ea05433a 100644 (file)
@@ -26,8 +26,8 @@
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_arm_service.h"
 #include "gnunet_protocols.h"
+#include "gnunet_statistics_service.h"
 #include "plugin_datastore.h"
 #include "datastore.h"
 
@@ -163,6 +163,12 @@ const struct GNUNET_CONFIGURATION_Handle *cfg;
  */
 struct GNUNET_SCHEDULER_Handle *sched; 
 
+/**
+ * Handle for reporting statistics.
+ */
+static struct GNUNET_STATISTICS_Handle *stats;
+
+
 /**
  * Function called once the transmit operation has
  * either failed or succeeded.
@@ -234,6 +240,11 @@ static struct TransmitCallbackContext *tcc_head;
  */
 static struct TransmitCallbackContext *tcc_tail;
 
+/**
+ * Have we already cleaned up the TCCs and are hence no longer
+ * willing (or able) to transmit anything to anyone?
+ */
+static int cleaning_done;
 
 /**
  * Task that is used to remove expired entries from
@@ -276,7 +287,7 @@ expired_processor (void *cls,
                   const GNUNET_HashCode * key,
                   uint32_t size,
                   const void *data,
-                  uint32_t type,
+                  enum GNUNET_BLOCK_Type type,
                   uint32_t priority,
                   uint32_t anonymity,
                   struct GNUNET_TIME_Absolute
@@ -285,7 +296,6 @@ expired_processor (void *cls,
 {
   struct GNUNET_TIME_Absolute now;
 
-  expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
   if (key == NULL) 
     {
       expired_kill_task 
@@ -308,6 +318,10 @@ expired_processor (void *cls,
              "Deleting content that expired %llu ms ago\n",
              (unsigned long long) (now.value - expiration.value));
 #endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# bytes expired"),
+                           size,
+                           GNUNET_YES);
   GNUNET_CONTAINER_bloomfilter_remove (filter,
                                       key);
   return GNUNET_NO; /* delete */
@@ -327,6 +341,7 @@ static void
 delete_expired (void *cls,
                const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
   plugin->api->iter_ascending_expiration (plugin->api->cls, 
                                          0,
                                          &expired_processor,
@@ -359,7 +374,7 @@ manage (void *cls,
        const GNUNET_HashCode * key,
        uint32_t size,
        const void *data,
-       uint32_t type,
+       enum GNUNET_BLOCK_Type type,
        uint32_t priority,
        uint32_t anonymity,
        struct GNUNET_TIME_Absolute
@@ -385,6 +400,10 @@ manage (void *cls,
              size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
              *need);
 #endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# bytes purged (low-priority)"),
+                           size,
+                           GNUNET_YES);
   GNUNET_CONTAINER_bloomfilter_remove (filter,
                                       key);
   return GNUNET_NO;
@@ -454,9 +473,8 @@ transmit_callback (void *cls,
       if (tcc->tc != NULL)
        tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
       if (GNUNET_YES == tcc->end)
-       {
-         GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
-       }
+       GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);       
+      GNUNET_SERVER_client_drop (tcc->client);
       GNUNET_free (tcc->msg);
       GNUNET_free (tcc);
       return 0;
@@ -476,6 +494,7 @@ transmit_callback (void *cls,
                  "Response transmitted, more pending!\n");
 #endif
     }
+  GNUNET_SERVER_client_drop (tcc->client);
   GNUNET_free (tcc->msg);
   GNUNET_free (tcc);
   return msize;
@@ -502,6 +521,16 @@ transmit (struct GNUNET_SERVER_Client *client,
 {
   struct TransmitCallbackContext *tcc;
 
+  if (GNUNET_YES == cleaning_done)
+    {
+#if DEBUG_DATASTORE
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 "Shutdown in progress, aborting transmission.\n");
+#endif
+     if (NULL != tc)
+       tc (tc_cls, GNUNET_SYSERR);
+      return;
+    }
   tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
   tcc->msg = msg;
   tcc->client = client;
@@ -510,9 +539,9 @@ transmit (struct GNUNET_SERVER_Client *client,
   tcc->end = end;
   if (NULL ==
       (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
-                                                    ntohs(msg->size),
-                                                    GNUNET_TIME_UNIT_FOREVER_REL,
-                                                    &transmit_callback,
+                                                     ntohs(msg->size),
+                                                     GNUNET_TIME_UNIT_FOREVER_REL,
+                                                     &transmit_callback,
                                                      tcc)))
     {
       GNUNET_break (0);
@@ -528,7 +557,9 @@ transmit (struct GNUNET_SERVER_Client *client,
        tc (tc_cls, GNUNET_SYSERR);
       GNUNET_free (msg);
       GNUNET_free (tcc);
+      return;
     }
+  GNUNET_SERVER_client_keep (client);
   GNUNET_CONTAINER_DLL_insert (tcc_head,
                               tcc_tail,
                               tcc);
@@ -616,7 +647,7 @@ transmit_item (void *cls,
               const GNUNET_HashCode * key,
               uint32_t size,
               const void *data,
-              uint32_t type,
+              enum GNUNET_BLOCK_Type type,
               uint32_t priority,
               uint32_t anonymity,
               struct GNUNET_TIME_Absolute
@@ -658,6 +689,10 @@ transmit_item (void *cls,
              "Transmitting `%s' message\n",
              "DATA");
 #endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# results found"),
+                           1,
+                           GNUNET_NO);
   transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
   return GNUNET_OK;
 }
@@ -838,22 +873,19 @@ handle_put (void *cls,
   struct ReservationList *pos;
   uint32_t size;
 
-#if DEBUG_DATASTORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Processing `%s' request\n",
-             "PUT");
-#endif
-  if (ntohl(dm->type) == 0) 
-    {
-      GNUNET_break (0);
-      dm = NULL;
-    }
-  if (dm == NULL)
+  if ( (dm == NULL) ||
+       (ntohl(dm->type) == 0) ) 
     {
       GNUNET_break (0);
       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
       return;
     }
+#if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing `%s' request for `%s'\n",
+             "PUT",
+             GNUNET_h2s (&dm->key));
+#endif
   rid = ntohl(dm->rid);
   size = ntohl(dm->size);
   if (rid > 0)
@@ -884,6 +916,10 @@ handle_put (void *cls,
                          &msg);
   if (GNUNET_OK == ret)
     {
+      GNUNET_STATISTICS_update (stats,
+                               gettext_noop ("# bytes stored"),
+                               size,
+                               GNUNET_YES);
       GNUNET_CONTAINER_bloomfilter_add (filter,
                                        &dm->key);
 #if DEBUG_DATASTORE
@@ -911,17 +947,12 @@ handle_put (void *cls,
  */
 static void
 handle_get (void *cls,
-            struct GNUNET_SERVER_Client *client,
-            const struct GNUNET_MessageHeader *message)
+           struct GNUNET_SERVER_Client *client,
+           const struct GNUNET_MessageHeader *message)
 {
   const struct GetMessage *msg;
   uint16_t size;
 
-#if DEBUG_DATASTORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Processing `%s' request\n",
-             "GET");
-#endif
   size = ntohs(message->size);
   if ( (size != sizeof(struct GetMessage)) &&
        (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
@@ -931,6 +962,18 @@ handle_get (void *cls,
       return;
     }
   msg = (const struct GetMessage*) message;
+#if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing `%s' request for `%s' of type %u\n",
+             "GET",
+             GNUNET_h2s (&msg->key),
+             ntohl (msg->type));
+#endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# GET requests received"),
+                           1,
+                           GNUNET_NO);
+  GNUNET_SERVER_client_keep (client);
   if ( (size == sizeof(struct GetMessage)) &&
        (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
                                                         &msg->key)) )
@@ -942,13 +985,15 @@ handle_get (void *cls,
                  "GET",
                  GNUNET_h2s (&msg->key));
 #endif 
-      GNUNET_SERVER_client_keep (client);
+      GNUNET_STATISTICS_update (stats,
+                               gettext_noop ("# requests filtered by bloomfilter"),
+                               1,
+                               GNUNET_NO);
       transmit_item (client,
                     NULL, NULL, 0, NULL, 0, 0, 0, 
                     GNUNET_TIME_UNIT_ZERO_ABS, 0);
       return;
     }
-  GNUNET_SERVER_client_keep (client);
   plugin->api->get (plugin->api->cls,
                    ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
                    NULL,
@@ -974,13 +1019,18 @@ handle_update (void *cls,
   int ret;
   char *emsg;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# UPDATE requests received"),
+                           1,
+                           GNUNET_NO);
+  msg = (const struct UpdateMessage*) message;
+  emsg = NULL;
 #if DEBUG_DATASTORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Processing `%s' request\n",
-             "UPDATE");
+             "Processing `%s' request for %llu\n",
+             "UPDATE",
+             (unsigned long long) GNUNET_ntohll (msg->uid));
 #endif
-  msg = (const struct UpdateMessage*) message;
-  emsg = NULL;
   ret = plugin->api->update (plugin->api->cls,
                             GNUNET_ntohll(msg->uid),
                             (int32_t) ntohl(msg->priority),
@@ -1008,6 +1058,10 @@ handle_get_random (void *cls,
              "Processing `%s' request\n",
              "GET_RANDOM");
 #endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# GET RANDOM requests received"),
+                           1,
+                           GNUNET_NO);
   GNUNET_SERVER_client_keep (client);
   plugin->api->iter_migration_order (plugin->api->cls,
                                     0,
@@ -1043,7 +1097,7 @@ remove_callback (void *cls,
                 const GNUNET_HashCode * key,
                 uint32_t size,
                 const void *data,
-                uint32_t type,
+                enum GNUNET_BLOCK_Type type,
                 uint32_t priority,
                 uint32_t anonymity,
                 struct GNUNET_TIME_Absolute
@@ -1069,10 +1123,15 @@ remove_callback (void *cls,
   rc->found = GNUNET_YES;
 #if DEBUG_DATASTORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Item %llu matches `%s' request.\n",
+             "Item %llu matches `%s' request for key `%s'.\n",
              (unsigned long long) uid,
-             "REMOVE");
+             "REMOVE",
+             GNUNET_h2s (key));
 #endif 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# bytes removed (explicit request)"),
+                           size,
+                           GNUNET_YES);
   GNUNET_CONTAINER_bloomfilter_remove (filter,
                                       key);
   plugin->api->next_request (next_cls, GNUNET_YES);
@@ -1096,17 +1155,22 @@ handle_remove (void *cls,
   GNUNET_HashCode vhash;
   struct RemoveContext *rc;
 
-#if DEBUG_DATASTORE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Processing `%s' request\n",
-             "REMOVE");
-#endif
   if (dm == NULL)
     {
       GNUNET_break (0);
       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
       return;
     }
+#if DEBUG_DATASTORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing `%s' request for `%s'\n",
+             "REMOVE",
+             GNUNET_h2s (&dm->key));
+#endif
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# REMOVE requests received"),
+                           1,
+                           GNUNET_NO);
   rc = GNUNET_malloc (sizeof(struct RemoveContext));
   GNUNET_SERVER_client_keep (client);
   rc->client = client;
@@ -1229,6 +1293,28 @@ unload_plugin (struct DatastorePlugin *plug)
 }
 
 
+/**
+ * Final task run after shutdown.  Unloads plugins and disconnects us from
+ * statistics.
+ */
+static void
+unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  unload_plugin (plugin);
+  plugin = NULL;
+  if (filter != NULL)
+    {
+      GNUNET_CONTAINER_bloomfilter_free (filter);
+      filter = NULL;
+    }
+  if (stats != NULL)
+    {
+      GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
+      stats = NULL;
+    }
+}
+
+
 /**
  * Last task run during shutdown.  Disconnects us from
  * the transport and core.
@@ -1236,20 +1322,34 @@ unload_plugin (struct DatastorePlugin *plug)
 static void
 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  struct TransmitCallbackContext *tcc;
+
+  cleaning_done = GNUNET_YES;
+  while (NULL != (tcc = tcc_head))
+    {
+      GNUNET_CONTAINER_DLL_remove (tcc_head,
+                                  tcc_tail,
+                                  tcc);
+      if (tcc->th != NULL)
+       {
+         GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
+         GNUNET_SERVER_client_drop (tcc->client);
+       }
+   if (NULL != tcc->tc)
+       tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
+      GNUNET_free (tcc->msg);
+      GNUNET_free (tcc);
+    }
   if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
     {
       GNUNET_SCHEDULER_cancel (sched,
                               expired_kill_task);
       expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
     }
-  unload_plugin (plugin);
-  plugin = NULL;
-  if (filter != NULL)
-    {
-      GNUNET_CONTAINER_bloomfilter_free (filter);
-      filter = NULL;
-    }
-  GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
+  GNUNET_SCHEDULER_add_continuation (sched,
+                                    &unload_task,
+                                    NULL,
+                                    GNUNET_SCHEDULER_REASON_PREREQ_DONE);
 }
 
 
@@ -1295,36 +1395,6 @@ cleanup_reservations (void *cls,
 }
 
 
-/**
- * Function that removes all active reservations made
- * by the given client and releases the space for other
- * requests.
- *
- * @param cls closure
- * @param client identification of the client
- */
-static void
-cleanup_transmits (void *cls,
-                  struct GNUNET_SERVER_Client
-                  * client)
-{
-  struct TransmitCallbackContext *tcc;
-
-  while (NULL != (tcc = tcc_head))
-    {
-      GNUNET_CONTAINER_DLL_remove (tcc_head,
-                                  tcc_tail,
-                                  tcc);
-      if (tcc->th != NULL)
-       GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
-      GNUNET_free (tcc->msg);
-      GNUNET_free (tcc);
-    }
-
-}
-
-
-
 /**
  * Process datastore requests.
  *
@@ -1354,6 +1424,7 @@ run (void *cls,
                  "DATASTORE");
       return;
     }
+  stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
   cache_size = quota / 8; /* Or should we make this an option? */
   bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
   fn = NULL;
@@ -1377,19 +1448,26 @@ run (void *cls,
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Failed to initialize bloomfilter.\n"));
+      if (stats != NULL)
+       {
+         GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
+         stats = NULL;
+       }
       return;
     }
-  GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
   plugin = load_plugin ();
   if (NULL == plugin)
     {
       GNUNET_CONTAINER_bloomfilter_free (filter);
       filter = NULL;
-      GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
+      if (stats != NULL)
+       {
+         GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
+         stats = NULL;
+       }
       return;
     }
   GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
-  GNUNET_SERVER_disconnect_notify (server, &cleanup_transmits, NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
   expired_kill_task
     = GNUNET_SCHEDULER_add_with_priority (sched,