X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fmemcached.cc;h=15fcbffa834722fd09b093b59bc3dd79092e811b;hb=dbc62573e1a6dc8cbcc97d0898e8615b0916d6ab;hp=61c3209fc166993076ab10ecec2f7e1dc1376cc6;hpb=3bebb0839f95592b26678370969c474a88099c4c;p=awesomized%2Flibmemcached diff --git a/libtest/memcached.cc b/libtest/memcached.cc index 61c3209f..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,229 +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 -#define SOCKET_FILE "/tmp/memcached.socket" +#include -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; -} +namespace libtest { -static bool cycle_server(server_st *server) +class Memcached : public libtest::Server { - while (1) + 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) { - if (libmemcached_util_ping(server->hostname(), server->port(), NULL)) - { - // 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)) - { - Error << "Killed existing server," << *server << " with pid:" << pid; - continue; - } - 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 - { - return false; - } - } + set_pid_file(); + } - break; + virtual const char *sasl() const + { + return NULL; } - return true; -} + const std::string& password() const + { + return _password; + } -bool server_startup(server_startup_st *construct) -{ - Logn(); + const std::string& username() const + { + return _username; + } - if (getenv(((char *)"MEMCACHED_SERVERS"))) + bool wait_for_pidfile() const { - construct->server_list= getenv(((char *)"MEMCACHED_SERVERS")); - Log << "MEMCACHED_SERVERS " << construct->server_list; - construct->count= 0; + Wait wait(pid(), 4); + + return wait.successful(); } - else + + bool ping() { - std::string server_config_string; + if (out_of_ban_killed()) + { + return false; + } - uint32_t port_base= 0; - for (uint32_t x= 0; x < uint32_t(construct->count -1); x++) + if (is_socket()) { - server_st *server= NULL; - - { - 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)); - } - } - } - - 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); - - if (x == 0) - { - assert(server->has_port()); - set_default_port(server->port()); - } - - char port_str[NI_MAXSERV]; - snprintf(port_str, sizeof(port_str), "%u", int(server->port())); - - server_config_string+= "--server="; - server_config_string+= server->hostname(); - server_config_string+= ":"; - server_config_string+= port_str; - server_config_string+= " "; + return _app.check(); } - // Socket + SimpleClient client(_hostname, _port); + + std::string response; + return client.send_message("version", response); + } + + const char *name() + { + return "memcached"; + }; + + const char *executable() + { + return memcached_binary(); + } + + bool is_libtool() + { + return false; + } + + virtual void pid_file_option(Application& app, const std::string& arg) + { + if (arg.empty() == false) { + app.add_option("-P", arg); + } + } + + const char *socket_file_option() const + { + return "-s "; + } + + 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; + } - std::string socket_file(SOCKET_FILE); - char *var; - - if ((var= getenv("LIBMEMCACHED_SOCKET"))) - { - socket_file= var; - } - - server_st *server= new server_st(SOCKET_FILE, __getpid, __ping); - - if (not cycle_server(server)) - { - Error << "Found server " << server << ", could not flush it, failing since socket file is not available."; - return false; - } - - 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); - - { - server_config_string+= "--socket=\""; - server_config_string+= server->hostname(); - server_config_string+= "\" "; - } + 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) + { + 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; + } + + bool build(); +}; + + +#include + +bool Memcached::build() +{ + if (getuid() == 0 or geteuid() == 0) + { + add_option("-u", "root"); } - Logn(); + add_option("-l", "localhost"); +#ifdef __APPLE__ +#else + add_option("-m", "128"); + add_option("-M"); +#endif - srandom((unsigned int)time(NULL)); + if (sasl()) + { + add_option(sasl()); + } return true; } -void server_shutdown(server_startup_st *construct) +libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port) { - if (not construct) - return; + if (has_memcached()) + { + return new Memcached(hostname, try_port, false); + } - construct->shutdown(); + return NULL; } + +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