X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libtest%2Fmemcached.cc;h=24bc3b8d18352da7d19656678e1cd6ef79d62a57;hb=4e7c4a986b51441df4241e94ee39cabe17cd3709;hp=e7e87f262006b0ed7f04bdccc93e61c806ceaf42;hpb=7abcaebdc4c3dd11b779eaef58a7371fb82ae888;p=awesomized%2Flibmemcached diff --git a/libtest/memcached.cc b/libtest/memcached.cc index e7e87f26..24bc3b8d 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,265 +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 +#ifndef __INTEL_COMPILER +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif -#define CERR_PREFIX std::endl << __FILE__ << ":" << __LINE__ << " " +namespace libtest { -static void global_sleep(void) +class Memcached : public libtest::Server { - static struct timespec global_sleep_value= { 0, 50000 }; + 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(); + } -#ifdef WIN32 - sleep(1); -#else - nanosleep(&global_sleep_value, NULL); -#endif -} + virtual const char *sasl() const + { + return NULL; + } -#define SOCKET_FILE "/tmp/memcached.socket" + const std::string& password() const + { + return _password; + } -static bool cycle_server(server_st& server) -{ - while (1) + const std::string& username() const { - if (libmemcached_util_ping(server.hostname, server.port(), NULL)) + return _username; + } + + bool wait_for_pidfile() const + { + Wait wait(pid(), 4); + + return wait.successful(); + } + + bool ping() + { + if (out_of_ban_killed()) { - // 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 false; } - break; + if (is_socket()) + { + return _app.check(); + } + + 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() { - std::string server_config_string; + return false; + } - uint32_t port_base= 0; - for (uint32_t x= 0; x < (construct->count -1); x++) + virtual void pid_file_option(Application& app, const std::string& arg) + { + if (arg.empty() == false) { - 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()); - } - - int status= system(buffer); - if (status == -1) - { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; - return false; - } - fprintf(stderr, "STARTING SERVER: %s\n", buffer); - - int count= 30; - memcached_return_t rc; - while (not libmemcached_util_ping(server.hostname, server.port(), &rc) and --count) - { - global_sleep(); - } - - if (memcached_failed(rc)) - { - std::cerr << CERR_PREFIX << "libmemcached_util_ping() failed:" << memcached_strerror(NULL, rc) << " Connection:" << server << std::endl; - return false; - } - - server.set_pid(libmemcached_util_getpid(server.hostname, server.port(), &rc)); - if (not server.has_pid()) - { - std::cerr << CERR_PREFIX << "libmemcached_util_getpid() failed" << memcached_strerror(NULL, rc) << " Connection: " << server << std::endl; - return false; - } - } - - server_config_string+= "--server="; - server_config_string+= server.hostname; - server_config_string+= ":"; - server_config_string+= boost::lexical_cast(server.port()); - server_config_string+= " "; - fprintf(stderr, " Port %d\n", server.port()); + 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; + } + + 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, so trying next port." << 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); - - int status= system(buffer); - if (status == -1) - { - std::cerr << CERR_PREFIX << "Failed system(" << buffer << ")" << std::endl; - return false; - } - fprintf(stderr, "STARTING SERVER: %s\n", buffer); - - int count= 30; - memcached_return_t rc; - while (not libmemcached_util_ping(server.hostname, server.port(), &rc) and --count) - { - global_sleep(); - } - - if (memcached_failed(rc)) - { - std::cerr << CERR_PREFIX << "libmemcached_util_ping() failed:" << memcached_strerror(NULL, rc) << " Connection:" << server << std::endl; - return false; - } - - server.set_pid(libmemcached_util_getpid(server.hostname, server.port(), &rc)); - if (not server.has_pid()) - { - std::cerr << CERR_PREFIX << "libmemcached_util_getpid() failed" << memcached_strerror(NULL, rc) << " Connection: " << server << std::endl; - return false; - } - } - - { - 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; + } + + // Memcached's pidfile is broken + bool broken_pid_file() + { + return true; + } + + bool build(); +}; + - server_config_string.resize(server_config_string.size() -1); // Remove final space - construct->server_list= server_config_string; +#include + +bool Memcached::build() +{ + if (getuid() == 0 or geteuid() == 0) + { + add_option("-u", "root"); } - srandom((unsigned int)time(NULL)); + 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 (HAVE_MEMCACHED_BINARY) { - 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 (HAVE_MEMCACHED_BINARY) + { + return new Memcached(socket_file, try_port, true); } + + return NULL; } + +} // namespace libtest