X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fmemcached.cc;h=15fcbffa834722fd09b093b59bc3dd79092e811b;hb=59acef2d2b59a3f1a55337a933b97d901d46133a;hp=adaa30f617df920721d23dc8da5b38ce2b161f6d;hpb=12a07e58df95bb8dbe167e4157b29c910177ade8;p=awesomized%2Flibmemcached diff --git a/libtest/memcached.cc b/libtest/memcached.cc index adaa30f6..15fcbffa 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -1,9 +1,8 @@ /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. + * Data Differential YATL (i.e. libtest) library + * + * Copyright (C) 2012 Data Differential, http://datadifferential.com/ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -35,234 +34,206 @@ * */ +#include "libtest/yatlcon.h" -/* - Startup, and shutdown the memcached servers. -*/ - -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10 - -#include +#include "libtest/common.h" #include #include #include #include #include -#include -#include +#include #include -#include +#include +#include #include -#include - -#include -#include #include -#include #include -#include - +#include -#define CERR_PREFIX std::endl << __FILE__ << ":" << __LINE__ << " " +#ifndef __INTEL_COMPILER +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif -#define SOCKET_FILE "/tmp/memcached.socket" +namespace libtest { -static pid_t __getpid(server_st& server) +class Memcached : public libtest::Server { - memcached_return_t rc; - pid_t pid= libmemcached_util_getpid(server.hostname, server.port(), &rc); - return pid; -} + std::string _username; + std::string _password; + +public: + Memcached(const std::string& host_arg, + const in_port_t port_arg, + const bool is_socket_arg, + const std::string& username_arg, + const std::string& password_arg) : + libtest::Server(host_arg, port_arg, + memcached_binary(), false, is_socket_arg), + _username(username_arg), + _password(password_arg) + { } + + Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) : + libtest::Server(host_arg, port_arg, + memcached_binary(), false, is_socket_arg) + { + set_pid_file(); + } -static bool __ping(server_st& server) -{ - memcached_return_t rc; - bool ret= libmemcached_util_ping(server.hostname, server.port(), &rc); - return ret; -} + virtual const char *sasl() const + { + return NULL; + } -static bool cycle_server(server_st& server) -{ - while (1) + const std::string& password() const { - if (libmemcached_util_ping(server.hostname, server.port(), NULL)) + return _password; + } + + const std::string& username() const + { + return _username; + } + + bool wait_for_pidfile() const + { + Wait wait(pid(), 4); + + return wait.successful(); + } + + bool ping() + { + if (out_of_ban_killed()) + { + return false; + } + + if (is_socket()) { - // 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); - - if (pid > 0 and kill_pid(pid)) - { - std::cerr << CERR_PREFIX << "Killed existing server," << server << " with pid:" << pid << std::endl; - continue; - } - else if (libmemcached_util_flush(server.hostname, server.port(), NULL)) // If we can flush it, we will just use it - { - std::cerr << CERR_PREFIX << "Found server on port " << int(server.port()) << ", flushed it!" << std::endl; - server.set_used(); - return true; - } // No idea what is wrong here, so we need to find a different port - else - { - return false; - } + return _app.check(); } - break; + SimpleClient client(_hostname, _port); + + std::string response; + return client.send_message("version", response); } - return true; -} + const char *name() + { + return "memcached"; + }; -bool server_startup(server_startup_st *construct) -{ - if (getenv(((char *)"MEMCACHED_SERVERS"))) + const char *executable() { - construct->server_list= getenv(((char *)"MEMCACHED_SERVERS")); - printf("servers %s\n", construct->server_list.c_str()); - construct->count= 0; + return memcached_binary(); } - else + + bool is_libtool() + { + return false; + } + + virtual void pid_file_option(Application& app, const std::string& arg) { - for (uint32_t x= 0; x < construct->count; x++) + if (arg.empty() == false) { - server_st &server= construct->server[x]; - server.set_methods(__getpid, __ping); + app.add_option("-P", arg); } + } - std::string server_config_string; + const char *socket_file_option() const + { + return "-s "; + } - uint32_t port_base= 0; - for (uint32_t x= 0; x < (construct->count -1); x++) - { - server_st &server= construct->server[x]; - - { - char *var; - char variable_buffer[1024]; - - snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x); - - if ((var= getenv(variable_buffer))) - { - server.set_port((in_port_t)atoi(var)); - } - else - { - server.set_port(in_port_t(x + TEST_PORT_BASE + port_base)); - - while (not cycle_server(server)) - { - std::cerr << CERR_PREFIX << "Found server " << server << ", could not flush it, so trying next port." << std::endl; - port_base++; - server.set_port(in_port_t(x + TEST_PORT_BASE + port_base)); - } - } - } - - if (server.is_used()) - { - std::cerr << std::endl << "Using server at : " << server << std::endl; - } - 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()) - { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; - return false; - } - std::cerr << "STARTING SERVER: " << buffer << " pid:" << server.pid() << std::endl; - } - - server_config_string+= "--server="; - server_config_string+= server.hostname; - server_config_string+= ":"; - server_config_string+= boost::lexical_cast(server.port()); - server_config_string+= " "; - } + virtual void port_option(Application& app, in_port_t arg) + { + char buffer[30]; + snprintf(buffer, sizeof(buffer), "%d", int(arg)); + app.add_option("-p", buffer); + } + + bool has_port_option() const + { + return true; + } - // Socket + bool has_socket_file_option() const + { + return has_socket(); + } + + void socket_file_option(Application& app, const std::string& socket_arg) + { + if (socket_arg.empty() == false) { - server_st &server= construct->server[construct->count -1]; - - { - std::string socket_file; - char *var; - - server.set_hostname(SOCKET_FILE); - - if ((var= getenv("LIBMEMCACHED_SOCKET"))) - { - socket_file+= var; - } - else - { - if (not cycle_server(server)) - { - std::cerr << CERR_PREFIX << "Found server " << server << ", could not flush it, failing since socket file is not available." << std::endl; - return false; - } - } - } - - if (server.is_used()) - { - std::cerr << std::endl << "Using server at : " << server << std::endl; - } - 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()) - { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; - return false; - } - std::cerr << "STARTING SERVER: " << buffer << " pid:" << server.pid() << std::endl; - } - - { - set_default_socket(server.hostname); - server_config_string+= "--socket=\""; - server_config_string+= server.hostname; - server_config_string+= "\" "; - } + app.add_option("-s", socket_arg); } + } + + bool broken_socket_cleanup() + { + return true; + } - server_config_string.resize(server_config_string.size() -1); // Remove final space - construct->server_list= server_config_string; + // Memcached's pidfile is broken + bool broken_pid_file() + { + return true; } - srandom((unsigned int)time(NULL)); + bool build(); +}; + + +#include + +bool Memcached::build() +{ + if (getuid() == 0 or geteuid() == 0) + { + add_option("-u", "root"); + } + + add_option("-l", "localhost"); +#ifdef __APPLE__ +#else + add_option("-m", "128"); + add_option("-M"); +#endif + + if (sasl()) + { + add_option(sasl()); + } - std::cerr << std::endl; return true; } -void server_shutdown(server_startup_st *construct) +libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port) { - for (uint32_t x= 0; x < construct->count; x++) + if (has_memcached()) { - if (construct->server[x].is_used()) - continue; + return new Memcached(hostname, try_port, false); + } + + return NULL; +} - construct->server[x].kill(); +libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port) +{ + if (has_memcached()) + { + return new Memcached(socket_file, try_port, true); } + + return NULL; } + +} // namespace libtest