Merge in all of libtest updates.
[m6w6/libmemcached] / libtest / memcached.cc
index 6c019f9d1e586a264f52927ca257bb9577c68a21..e3d90947a2c0e1b99a5395dbb94f0942d27122f4 100644 (file)
@@ -1,9 +1,8 @@
 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  * 
- *  Libmemcached library
+ *  libtest
  *
  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
- *  Copyright (C) 2006-2009 Brian Aker All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
  *
  */
 
+#include <libtest/common.h>
 
-/*
-  Startup, and shutdown the memcached servers.
-*/
-
-#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
+#include <libmemcached/memcached.h>
+#include <libmemcached/util.h>
 
-#include <libtest/common.h>
+using namespace libtest;
 
+#include <cassert>
+#include <cerrno>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
-#include <ctime>
-#include <limits.h>
+#include <iostream>
 #include <signal.h>
-#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
-#include <iostream>
-
-#include <libmemcached/memcached.h>
-#include <libmemcached/util.h>
 
 #include <libtest/server.h>
-#include <libtest/killpid.h>
 #include <libtest/wait.h>
 
-#define SOCKET_FILE "/tmp/memcached.socket"
+#include <libtest/memcached.h>
 
-static pid_t __getpid(server_st& server)
-{
-  memcached_return_t rc;
-  pid_t pid= libmemcached_util_getpid(server.hostname(), server.port(), &rc);
-  return pid;
-}
+#ifndef __INTEL_COMPILER
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#endif
 
-static bool __ping(server_st& server)
-{
-  memcached_return_t rc;
-  bool ret= libmemcached_util_ping(server.hostname(), server.port(), &rc);
-  return ret;
-}
+using namespace libtest;
 
-static bool cycle_server(server_st  *server)
+class Memcached : public Server
 {
-  while (1)
+public:
+  Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
+    Server(host_arg, port_arg, is_socket_arg)
+  { }
+
+  pid_t get_pid(bool error_is_ok)
   {
-    if (libmemcached_util_ping(server->hostname(), server->port(), NULL))
+    // Memcached is slow to start, so we need to do this
+    if (not pid_file().empty())
     {
-      // First we try to kill it, and on fail of that we flush it.
-      pid_t pid= libmemcached_util_getpid(server->hostname(), server->port(), NULL);
+      Wait wait(pid_file(), 0);
 
-      if (pid > 0 and kill_pid(pid))
+      if (not wait.successful())
       {
-        Error << "Killed existing server," << *server << " with pid:" << pid;
-        continue;
+        Error << "Pidfile was not found:" << pid_file();
+        return -1;
       }
-      else if (libmemcached_util_flush(server->hostname(), server->port(), NULL)) // If we can flush it, we will just use it
-      { 
-        Error << "Found server on port " << int(server->port()) << ", flushed it!";
-        server->set_used();
-        return true;
-      } // No idea what is wrong here, so we need to find a different port
-      else
+    }
+
+    memcached_return_t rc;
+    if (has_socket())
+    {
+      _pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
+    }
+    else
+    {
+      _pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
+    }
+
+    if ((memcached_failed(rc) or _pid < 1) and not error_is_ok)
+    {
+      Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << _pid << " for:" << *this;
+    }
+
+    return _pid;
+  }
+
+  bool ping()
+  {
+    // Memcached is slow to start, so we need to do this
+    if (not pid_file().empty())
+    {
+      Wait wait(pid_file(), 0);
+
+      if (not wait.successful())
       {
-        return false;
+        Error << "Pidfile was not found:" << pid_file();
+        return -1;
       }
     }
 
-    break;
+    memcached_return_t rc;
+    bool ret;
+    if (has_socket())
+    {
+      ret= libmemcached_util_ping(socket().c_str(), 0, &rc);
+    }
+    else
+    {
+      ret= libmemcached_util_ping(hostname().c_str(), port(), &rc);
+    }
+
+    if (memcached_failed(rc) or not ret)
+    {
+      Error << "libmemcached_util_ping(" << memcached_strerror(NULL, rc) << ")";
+    }
+    return ret;
   }
 
-  return true;
-}
+  const char *name()
+  {
+    return "memcached";
+  };
 
-bool server_startup(server_startup_st *construct)
-{
-  Logn();
+  const char *executable()
+  {
+    return MEMCACHED_BINARY;
+  }
 
-  if (getenv(((char *)"MEMCACHED_SERVERS")))
+  const char *pid_file_option()
   {
-    construct->server_list= getenv(((char *)"MEMCACHED_SERVERS"));
-    Log << "MEMCACHED_SERVERS " << construct->server_list;
-    construct->count= 0;
+    return "-P ";
   }
-  else
+
+  const char *socket_file_option() const
   {
-    std::string server_config_string;
+    return "-s ";
+  }
 
-    uint32_t port_base= 0;
-    for (uint32_t x= 0; x < uint32_t(construct->count -1); x++)
-    {
-      server_st *server= NULL;
+  const char *daemon_file_option()
+  {
+    return "-d";
+  }
 
-      {
-        char *var;
-        char variable_buffer[1024];
-
-        snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
-
-        if ((var= getenv(variable_buffer)))
-        {
-          server= new server_st((in_port_t)atoi(var), __getpid, __ping);
-        }
-        else
-        {
-          server= new server_st(in_port_t(x +TEST_PORT_BASE +port_base), __getpid, __ping);
-
-          while (not cycle_server(server))
-          {
-            Error << "Found server " << *server << ", could not flush it, so trying next port.";
-            port_base++;
-            server->set_port(in_port_t(x +TEST_PORT_BASE +port_base));
-          }
-        }
-      }
+  const char *log_file_option()
+  {
+    return NULL;
+  }
 
-      if (server->is_used())
-      {
-        Log << "Using server at : " << server;
-      }
-      else
-      {
-        char buffer[FILENAME_MAX];
-        if (x == 0)
-        {
-          snprintf(buffer, sizeof(buffer), "%s -d -t 1 -p %u -U %u -m 128",
-                   MEMCACHED_BINARY, server->port(), server->port());
-        }
-        else
-        {
-          snprintf(buffer, sizeof(buffer), "%s -d -t 1 -p %u -U %u",
-                   MEMCACHED_BINARY, server->port(), server->port());
-        }
-        server->set_command(buffer);
-
-        if (not server->start())
-        {
-          Error << "Failed system(" << buffer << ")";
-          delete server;
-          return false;
-        }
-        Log << "STARTING SERVER: " << buffer << " pid:" << server->pid();
-      }
-      construct->push_server(server);
+  const char *port_option()
+  {
+    return "-p ";
+  }
 
-      if (x == 0)
-      {
-        assert(server->has_port());
-        set_default_port(server->port());
-      }
+  bool is_libtool()
+  {
+    return false;
+  }
 
-      char port_str[NI_MAXSERV];
-      snprintf(port_str, sizeof(port_str), "%u", int(server->port()));
+  // Memcached's pidfile is broken
+  bool broken_pid_file()
+  {
+    return true;
+  }
 
-      server_config_string+= "--server=";
-      server_config_string+= server->hostname();
-      server_config_string+= ":";
-      server_config_string+= port_str;
-      server_config_string+= " ";
-    }
+  bool build(int argc, const char *argv[]);
+};
 
-    // Socket
-    {
 
-      std::string socket_file(SOCKET_FILE);
-      char *var;
+#include <sstream>
 
-      if ((var= getenv("LIBMEMCACHED_SOCKET")))
-      {
-        socket_file= var;
-      }
+bool Memcached::build(int argc, const char *argv[])
+{
+  std::stringstream arg_buffer;
 
-      server_st *server= new server_st(SOCKET_FILE, __getpid, __ping);
+  if (getuid() == 0 or geteuid() == 0)
+  {
+    arg_buffer << " -u root ";
+  }
 
-      if (not cycle_server(server))
-      {
-        Error << "Found server " << server << ", could not flush it, failing since socket file is not available.";
-        return false;
-      }
+  for (int x= 1 ; x < argc ; x++)
+  {
+    arg_buffer << " " << argv[x] << " ";
+  }
 
-      if (server->is_used())
-      {
-        Log << "Using server at : " << *server;
-      }
-      else
-      {
-        char buffer[FILENAME_MAX];
-        snprintf(buffer, sizeof(buffer), "%s -d -t 1 -s %s", MEMCACHED_BINARY, SOCKET_FILE);
-        server->set_command(buffer);
-
-        if (not server->start())
-        {
-          Error << "Failed system(" << buffer << ")";
-          delete server;
-          return false;
-        }
-        Log << "STARTING SERVER: " << buffer << " pid:" << server->pid();
-      }
-      set_default_socket(server->hostname());
-      construct->push_server(server);
+  set_extra_args(arg_buffer.str());
 
-      {
-        server_config_string+= "--socket=\"";
-        server_config_string+= server->hostname();
-        server_config_string+= "\" ";
-      }
-    }
+  return true;
+}
 
-    server_config_string.resize(server_config_string.size() -1); // Remove final space
-    construct->server_list= server_config_string;
-  }
+namespace libtest {
 
-  Logn();
+Server *build_memcached(const std::string& hostname, const in_port_t try_port)
+{
+  return new Memcached(hostname, try_port, false);
+}
 
-  srandom((unsigned int)time(NULL));
+Server *build_memcached_socket(const std::string& hostname, const in_port_t try_port)
+{
+  return new Memcached(hostname, try_port, true);
+}
 
-  return true;
 }
+