src/rrd_daemon.c: Updated the enqueueing function to provide insertion at the head.
[rrdtool.git] / src / rrd_daemon.c
index 3af33dc..7423088 100644 (file)
 # define __attribute__(x) /**/
 #endif
 
-#if 0
-/* FIXME: I don't want to declare strdup myself! */
-extern char *strdup (const char *s);
-#endif
-
 /*
  * Types
  */
@@ -121,6 +116,13 @@ struct cache_item_s
   cache_item_t *next;
 };
 
+enum queue_side_e
+{
+  HEAD,
+  TAIL
+};
+typedef enum queue_side_e queue_side_t;
+
 /*
  * Variables
  */
@@ -165,24 +167,69 @@ static void sig_term_handler (int s __attribute__((unused))) /* {{{ */
  * enqueue_cache_item:
  * `cache_lock' must be acquired before calling this function!
  */
-static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
+static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
+    queue_side_t side)
 {
-  RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
+  RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
       ci->file);
 
   if (ci == NULL)
     return (-1);
 
-  if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
-    return (-1);
+  if (ci->values_num == 0)
+    return (0);
 
-  assert (ci->next == NULL);
+  if (side == HEAD)
+  {
+    if ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
+    {
+      assert (ci->next == NULL);
+      ci->next = cache_queue_head;
+      cache_queue_head = ci;
 
-  if (cache_queue_tail == NULL)
-    cache_queue_head = ci;
-  else
-    cache_queue_tail->next = ci;
-  cache_queue_tail = ci;
+      if (cache_queue_tail == NULL)
+        cache_queue_tail = cache_queue_head;
+    }
+    else if (cache_queue_head == ci)
+    {
+      /* do nothing */
+    }
+    else /* enqueued, but not first entry */
+    {
+      cache_item_t *prev;
+
+      /* find previous entry */
+      for (prev = cache_queue_head; prev != NULL; prev = prev->next)
+        if (prev->next == ci)
+          break;
+      assert (prev != NULL);
+
+      /* move to the front */
+      prev->next = ci->next;
+      ci->next = cache_queue_head;
+      cache_queue_head = ci;
+
+      /* check if we need to adapt the tail */
+      if (cache_queue_tail == ci)
+        cache_queue_tail = prev;
+    }
+  }
+  else /* (side == TAIL) */
+  {
+    /* We don't move values back in the list.. */
+    if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
+      return (0);
+
+    assert (ci->next == NULL);
+
+    if (cache_queue_tail == NULL)
+      cache_queue_head = ci;
+    else
+      cache_queue_tail->next = ci;
+    cache_queue_tail = ci;
+  }
+
+  ci->flags |= CI_FLAGS_IN_QUEUE;
 
   return (0);
 } /* }}} int enqueue_cache_item */
@@ -204,8 +251,9 @@ static gboolean tree_callback_flush (gpointer key /* {{{ */
   now = *((time_t *) data);
 
   if (((now - ci->last_flush_time) >= config_write_interval)
-      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
-    enqueue_cache_item (ci);
+      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
+      && (ci->values_num > 0))
+    enqueue_cache_item (ci, TAIL);
 
   return (TRUE);
 } /* }}} gboolean tree_callback_flush */
@@ -252,21 +300,12 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
      * something comes in or it's time to do the cache flush. */
     if (cache_queue_head == NULL)
     {
-      struct timespec timeout;
-
-      timeout.tv_sec = next_flush.tv_sec - now.tv_sec;
-      if (next_flush.tv_nsec < (1000 * now.tv_usec))
+      status = pthread_cond_timedwait (&cache_cond, &cache_lock, &next_flush);
+      if ((status != 0) && (status != ETIMEDOUT))
       {
-        timeout.tv_sec--;
-        timeout.tv_nsec = 1000000000 + next_flush.tv_nsec
-          - (1000 * now.tv_usec);
+        RRDD_LOG (LOG_ERR, "queue_thread_main: "
+            "pthread_cond_timedwait returned %i.", status);
       }
-      else
-      {
-        timeout.tv_nsec = next_flush.tv_nsec - (1000 * now.tv_usec);
-      }
-
-      pthread_cond_timedwait (&cache_cond, &cache_lock, &timeout);
     }
 
     /* Check if a value has arrived. This may be NULL if we timed out or there
@@ -324,12 +363,70 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
   return (NULL);
 } /* }}} void *queue_thread_main */
 
+static int buffer_get_field (char **buffer_ret, /* {{{ */
+    size_t *buffer_size_ret, char **field_ret)
+{
+  char *buffer;
+  size_t buffer_pos;
+  size_t buffer_size;
+  char *field;
+  size_t field_size;
+  int status;
+
+  buffer = *buffer_ret;
+  buffer_pos = 0;
+  buffer_size = *buffer_size_ret;
+  field = *buffer_ret;
+  field_size = 0;
+
+  /* This is ensured by `handle_request'. */
+  assert (buffer[buffer_size - 1] == ' ');
+
+  status = -1;
+  while (buffer_pos < buffer_size)
+  {
+    /* Check for end-of-field or end-of-buffer */
+    if (buffer[buffer_pos] == ' ')
+    {
+      field[field_size] = 0;
+      field_size++;
+      buffer_pos++;
+      status = 0;
+      break;
+    }
+    /* Handle escaped characters. */
+    else if (buffer[buffer_pos] == '\\')
+    {
+      if (buffer_pos >= (buffer_size - 1))
+        break;
+      buffer_pos++;
+      field[field_size] = buffer[buffer_pos];
+      field_size++;
+      buffer_pos++;
+    }
+    /* Normal operation */ 
+    else
+    {
+      field[field_size] = buffer[buffer_pos];
+      field_size++;
+      buffer_pos++;
+    }
+  } /* while (buffer_pos < buffer_size) */
+
+  if (status != 0)
+    return (status);
+
+  *buffer_ret = buffer + buffer_pos;
+  *buffer_size_ret = buffer_size - buffer_pos;
+  *field_ret = field;
+
+  return (0);
+} /* }}} int buffer_get_field */
+
 static int handle_request_update (int fd, /* {{{ */
-    char *buffer, int buffer_size)
+    char *buffer, size_t buffer_size)
 {
   char *file;
-  char *value;
-  char *buffer_ptr;
   int values_num = 0;
   int status;
 
@@ -340,18 +437,17 @@ static int handle_request_update (int fd, /* {{{ */
 
   now = time (NULL);
 
-  RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
-      fd, (void *) buffer, buffer_size);
-
-  buffer_ptr = buffer;
-
-  file = buffer_ptr;
-  buffer_ptr += strlen (file) + 1;
+  status = buffer_get_field (&buffer, &buffer_size, &file);
+  if (status != 0)
+  {
+    RRDD_LOG (LOG_INFO, "handle_request_update: Cannot get file name.");
+    return (-1);
+  }
 
   pthread_mutex_lock (&cache_lock);
 
   ci = g_tree_lookup (cache_tree, file);
-  if (ci == NULL)
+  if (ci == NULL) /* {{{ */
   {
     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
     if (ci == NULL)
@@ -380,15 +476,20 @@ static int handle_request_update (int fd, /* {{{ */
 
     RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
         ci->file);
-  }
+  } /* }}} */
   assert (ci != NULL);
 
-  while (*buffer_ptr != 0)
+  while (buffer_size > 0)
   {
     char **temp;
+    char *value;
 
-    value = buffer_ptr;
-    buffer_ptr += strlen (value) + 1;
+    status = buffer_get_field (&buffer, &buffer_size, &value);
+    if (status != 0)
+    {
+      RRDD_LOG (LOG_INFO, "handle_request_update: Error reading field.");
+      break;
+    }
 
     temp = (char **) realloc (ci->values,
         sizeof (char *) * (ci->values_num + 1));
@@ -411,9 +512,10 @@ static int handle_request_update (int fd, /* {{{ */
   }
 
   if (((now - ci->last_flush_time) >= config_write_interval)
-      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
+      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0)
+      && (ci->values_num > 0))
   {
-    enqueue_cache_item (ci);
+    enqueue_cache_item (ci, TAIL);
     pthread_cond_signal (&cache_cond);
   }
 
@@ -422,7 +524,7 @@ static int handle_request_update (int fd, /* {{{ */
   snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
   answer[sizeof (answer) - 1] = 0;
 
-  status = write (fd, answer, sizeof (answer));
+  status = write (fd, answer, strlen (answer));
   if (status < 0)
   {
     status = errno;
@@ -436,31 +538,41 @@ static int handle_request_update (int fd, /* {{{ */
 static int handle_request (int fd) /* {{{ */
 {
   char buffer[4096];
-  int buffer_size;
-
-  RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
+  size_t buffer_size;
+  char *buffer_ptr;
+  char *command;
+  int status;
 
-  buffer_size = read (fd, buffer, sizeof (buffer));
-  if (buffer_size < 1)
+  status = read (fd, buffer, sizeof (buffer));
+  if (status < 1)
   {
     RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
     return (-1);
   }
+  buffer_size = status;
   assert (((size_t) buffer_size) <= sizeof (buffer));
 
-  if ((buffer[buffer_size - 2] != 0)
-      || (buffer[buffer_size - 1] != 0))
+  if (buffer[buffer_size - 1] != '\n')
   {
     RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
     return (-1);
   }
+  /* Place the normal field separator at the end to simplify
+   * `buffer_get_field's work. */
+  buffer[buffer_size - 1] = ' ';
 
-  /* fields in the buffer a separated by null bytes. */
-  if (strcmp (buffer, "update") == 0)
+  buffer_ptr = buffer;
+  command = NULL;
+  status = buffer_get_field (&buffer_ptr, &buffer_size, &command);
+  if (status != 0)
   {
-    int offset = strlen ("update") + 1;
-    return (handle_request_update (fd, buffer + offset,
-          buffer_size - offset));
+    RRDD_LOG (LOG_INFO, "handle_request: Unable parse command.");
+    return (-1);
+  }
+
+  if (strcmp (command, "update") == 0)
+  {
+    return (handle_request_update (fd, buffer_ptr, buffer_size));
   }
   else
   {
@@ -478,8 +590,6 @@ static void *connection_thread_main (void *args /* {{{ */
   
   fd = *((int *) args);
 
-  RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
-      "connetion_threads[]..");
   pthread_mutex_lock (&connetion_threads_lock);
   {
     pthread_t *temp;
@@ -498,7 +608,6 @@ static void *connection_thread_main (void *args /* {{{ */
     }
   }
   pthread_mutex_unlock (&connetion_threads_lock);
-  RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
 
   while (do_shutdown == 0)
   {
@@ -523,8 +632,6 @@ static void *connection_thread_main (void *args /* {{{ */
 
     if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
     {
-      RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
-          "poll(2) returned POLLHUP.");
       close (fd);
       break;
     }
@@ -808,9 +915,6 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
         continue;
       }
 
-      RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
-          *client_sd);
-
       status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
           /* args = */ (void *) client_sd);
       if (status != 0)
@@ -820,10 +924,6 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
         free (client_sd);
         continue;
       }
-
-      RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
-          "tid = %lu",
-          *((unsigned long *) &tid));
     } /* for (pollfds_num) */
   } /* while (do_shutdown == 0) */
 
@@ -894,7 +994,7 @@ static int daemonize (void) /* {{{ */
     sigaction (SIGPIPE, &sa, NULL);
   }
 
-  openlog ("rrdd", LOG_PID, LOG_DAEMON);
+  openlog ("rrdcached", LOG_PID, LOG_DAEMON);
 
   cache_tree = g_tree_new ((GCompareFunc) strcmp);
   if (cache_tree == NULL)
@@ -997,7 +1097,7 @@ static int read_options (int argc, char **argv) /* {{{ */
       case '?':
         printf ("RRDd %s  Copyright (C) 2008 Florian octo Forster\n"
             "\n"
-            "Usage: rrdd [options]\n"
+            "Usage: rrdcached [options]\n"
             "\n"
             "Valid options are:\n"
             "  -l <address>  Socket address to listen to.\n"
@@ -1006,7 +1106,7 @@ static int read_options (int argc, char **argv) /* {{{ */
             "\n"
             "For more information and a detailed description of all options "
             "please refer\n"
-            "to the rrdd(1) manual page.\n",
+            "to the rrdcached(1) manual page.\n",
             VERSION);
         status = -1;
         break;