From 806525899ce3dff894b829416307a4290cd882e2 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 27 Sep 2007 03:45:48 -0700 Subject: [PATCH] Readjusted a number function names. Added the memstat utility (well... not it does something) Some, though not enough, testing for multiple servers. --- AUTHORS | 3 +- Makefile.am | 7 +- include/memcached.h | 64 ++++++++++------ lib/memcached_connect.c | 56 +++++++------- lib/memcached_get.c | 3 + lib/memcached_quit.c | 6 +- lib/memcached_stats.c | 133 +++++++++++---------------------- lib/memcached_storage.c | 2 +- lib/memcached_strerror.c | 6 ++ src/memcp.c | 1 - src/memstat.c | 45 +++++++++++- src/utilities.c | 19 +++-- tests/Makefile.am | 17 ++++- tests/output.res | 46 ++++++++++++ tests/output2.res | 46 ++++++++++++ tests/test.c | 154 ++++++++++++++++++++++++++++++++++++--- 16 files changed, 432 insertions(+), 176 deletions(-) create mode 100644 tests/output2.res diff --git a/AUTHORS b/AUTHORS index 006aa81a..8fba32a7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ -Brian Aker, brian@mysql.com | brian@tangent.org +Brian Aker, brian@tangent.org -- Client Library, Tools +Mark Atwood, -- Tools diff --git a/Makefile.am b/Makefile.am index 6b05f81f..96abe02c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,10 +4,13 @@ SUBDIRS = docs include lib src tests test: all cd tests; make test; cd .. -ltest: all - ./tests/testapp output.txt +test-extended: all + cd tests; make test-extended; cd .. valgrind: cd tests; make valgrind; cd .. +valgrind-extended: + cd tests; make valgrind-extended; cd .. + diff --git a/include/memcached.h b/include/memcached.h index 3e8cea88..634f8884 100644 --- a/include/memcached.h +++ b/include/memcached.h @@ -31,7 +31,7 @@ extern "C" { typedef struct memcached_st memcached_st; typedef struct memcached_stat_st memcached_stat_st; typedef struct memcached_string_st memcached_string_st; -typedef struct memcached_host_st memcached_host_st; +typedef struct memcached_server_st memcached_server_st; #define MEMCACHED_DEFAULT_PORT 11211 #define MEMCACHED_DEFAULT_COMMAND_SIZE 350 @@ -39,8 +39,6 @@ typedef struct memcached_host_st memcached_host_st; #define HUGE_STRING_LEN 8196 #define MEMCACHED_MAX_KEY 251 /* We add one to have it null terminated */ -#define WATCHPOINT printf("WATCHPOINT %s:%d\n", __FILE__, __LINE__);fflush(stdout); - typedef enum { MEMCACHED_SUCCESS, MEMCACHED_FAILURE, @@ -61,6 +59,8 @@ typedef enum { MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_PARTIAL_READ, MEMCACHED_SOME_ERRORS, + MEMCACHED_NO_SERVERS, + MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */ } memcached_return; typedef enum { @@ -68,7 +68,7 @@ typedef enum { MEMCACHED_ALLOCATED= 1, } memcached_allocated; -struct memcached_host_st { +struct memcached_server_st { char *hostname; unsigned int port; int fd; @@ -112,7 +112,7 @@ struct memcached_string_st { struct memcached_st { memcached_allocated is_allocated; - memcached_host_st *hosts; + memcached_server_st *hosts; unsigned int number_of_hosts; unsigned int cursor_server; char connected; @@ -122,18 +122,6 @@ struct memcached_st { memcached_st *memcached_init(memcached_st *ptr); void memcached_deinit(memcached_st *ptr); -memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length, - char *value, size_t value_length, - time_t expiration, - uint16_t flags); -memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length, - char *value, size_t value_length, - time_t expiration, - uint16_t flags); -memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length, - char *value, size_t value_length, - time_t expiration, - uint16_t flags); memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length, time_t expiration); memcached_return memcached_increment(memcached_st *ptr, @@ -145,11 +133,28 @@ memcached_return memcached_decrement(memcached_st *ptr, unsigned int offset, unsigned int *value); memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error); -memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, - char *hostname, unsigned int port); +memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, + char *hostname, unsigned int port); memcached_return memcached_flush(memcached_st *ptr, time_t expiration); memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity); void memcached_quit(memcached_st *ptr); +char *memcached_strerror(memcached_st *ptr, memcached_return rc); + +/* All of the functions for adding data to the server */ +memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length, + char *value, size_t value_length, + time_t expiration, + uint16_t flags); +memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length, + char *value, size_t value_length, + time_t expiration, + uint16_t flags); +memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length, + char *value, size_t value_length, + time_t expiration, + uint16_t flags); + +/* Get functions */ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, size_t *value_length, uint16_t *flags, @@ -160,9 +165,15 @@ memcached_return memcached_mget(memcached_st *ptr, char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, size_t *value_length, uint16_t *flags, memcached_return *error); + +/* Server Public functions */ +#define memcached_server_count(A) A->number_of_hosts +#define memcached_server_name(A,B) B.hostname +#define memcached_server_port(A,B) B.port +#define memcached_server_list(A) A->hosts + memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port); -char *memcached_strerror(memcached_st *ptr, memcached_return rc); /* These are all private, do not use. */ memcached_return memcached_connect(memcached_st *ptr); @@ -170,9 +181,11 @@ memcached_return memcached_response(memcached_st *ptr, char *buffer, size_t buffer_length, unsigned int server_key); unsigned int memcached_generate_hash(char *key, size_t key_length); -memcached_return memcached_stat_get_value(memcached_stat_st *stat, char *key, - char *value, size_t value_length); -char ** memcached_stat_get_keys(memcached_stat_st *stat, memcached_return *error); +char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, + char *key, memcached_return *error); +char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, + memcached_return *error); + /* String Struct */ #define memcached_string_length(A, B) (size_t)(B->end - B->string) @@ -189,6 +202,11 @@ size_t memcached_string_backspace(memcached_st *ptr, memcached_string_st *string memcached_return memcached_string_reset(memcached_st *ptr, memcached_string_st *string); void memcached_string_free(memcached_st *ptr, memcached_string_st *string); +/* Some personal debugging functions */ +#define WATCHPOINT printf("WATCHPOINT %s:%d\n", __FILE__, __LINE__);fflush(stdout); +#define WATCHPOINT_ERROR(A) printf("WATCHPOINT %s:%d %s\n", __FILE__, __LINE__, memcached_strerror(NULL, A));fflush(stdout); + + #ifdef __cplusplus } #endif diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index 105e7d20..899101a2 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -2,7 +2,7 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port) { - memcached_host_st *new_host_list; + memcached_server_st *new_host_list; char *new_hostname; if (!port) @@ -12,10 +12,10 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne hostname= "localhost"; - new_host_list= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (ptr->number_of_hosts+1)); + new_host_list= (memcached_server_st *)realloc(ptr->hosts, sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); if (!new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - memset(&new_host_list[ptr->number_of_hosts], 0, sizeof(memcached_host_st)); + memset(&new_host_list[ptr->number_of_hosts], 0, sizeof(memcached_server_st)); if (!new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -43,47 +43,45 @@ memcached_return memcached_connect(memcached_st *ptr) struct sockaddr_in localAddr, servAddr; struct hostent *h; - if (ptr->connected) + if (ptr->connected == ptr->number_of_hosts) return MEMCACHED_SUCCESS; if (!ptr->hosts) { - memcached_return rc; - rc= memcached_server_add(ptr, NULL, 0); - - if (rc != MEMCACHED_SUCCESS) - return rc; + return MEMCACHED_NO_SERVERS; } - for (x= 0; x < ptr->number_of_hosts; x++) { - if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL) - return MEMCACHED_HOST_LOCKUP_FAILURE; + if (ptr->hosts[x].fd == -1) + { + if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL) + return MEMCACHED_HOST_LOCKUP_FAILURE; - servAddr.sin_family= h->h_addrtype; - memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); - servAddr.sin_port = htons(ptr->hosts[x].port); + servAddr.sin_family= h->h_addrtype; + memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); + servAddr.sin_port = htons(ptr->hosts[x].port); - /* Create the socket */ - if ((ptr->hosts[0].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0) - return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE; + /* Create the socket */ + if ((ptr->hosts[0].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0) + return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE; - /* bind any port number */ - localAddr.sin_family = AF_INET; - localAddr.sin_addr.s_addr = htonl(INADDR_ANY); - localAddr.sin_port = htons(0); + /* bind any port number */ + localAddr.sin_family = AF_INET; + localAddr.sin_addr.s_addr = htonl(INADDR_ANY); + localAddr.sin_port = htons(0); - if (bind(ptr->hosts[0].fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0) - return(MEMCACHED_CONNECTION_BIND_FAILURE); + if (bind(ptr->hosts[0].fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0) + return(MEMCACHED_CONNECTION_BIND_FAILURE); - /* connect to server */ - if (connect(ptr->hosts[0].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) - return MEMCACHED_HOST_LOCKUP_FAILURE; - } + /* connect to server */ + if (connect(ptr->hosts[0].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) + return MEMCACHED_HOST_LOCKUP_FAILURE; - ptr->connected= 1; + ptr->connected++; + } + } return MEMCACHED_SUCCESS; } diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 4cf4cbec..06b406e3 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -176,6 +176,7 @@ memcached_return memcached_mget(memcached_st *ptr, { memcached_string_st *string= memcached_string_init(ptr, SMALL_STRING_LEN); + /* We need to figure out the correct way to error in case of this failure */ if (!string) assert(0); @@ -227,6 +228,8 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, if (*error == MEMCACHED_NOTFOUND) ptr->cursor_server++; + else if (*error != MEMCACHED_SUCCESS) + return NULL; else return value_check; } diff --git a/lib/memcached_quit.c b/lib/memcached_quit.c index 24a88c2d..8481b990 100644 --- a/lib/memcached_quit.c +++ b/lib/memcached_quit.c @@ -13,9 +13,13 @@ void memcached_quit(memcached_st *ptr) { for (x= 0; x < ptr->number_of_hosts; x++) { - if (ptr->hosts[x].fd > 0) + if (ptr->hosts[x].fd != -1) + { close(ptr->hosts[x].fd); + ptr->hosts[x].fd= -1; + } } } + ptr->connected= 0; } diff --git a/lib/memcached_stats.c b/lib/memcached_stats.c index 5b917fca..ed2c0e20 100644 --- a/lib/memcached_stats.c +++ b/lib/memcached_stats.c @@ -146,123 +146,74 @@ static void set_data(memcached_stat_st *stat, char *key, char *value) } } -memcached_return memcached_stat_get_value(memcached_stat_st *stat, char *key, - char *value, size_t value_length) +char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat, + char *key, memcached_return *error) { - memcached_return rc; + char buffer[SMALL_STRING_LEN]; + *error= MEMCACHED_SUCCESS; - rc= MEMCACHED_SUCCESS; + + memset(buffer, 0, SMALL_STRING_LEN); if (!memcmp("pid", key, strlen("pid"))) - { - snprintf(value, value_length,"%u", stat->pid); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid); else if (!memcmp("uptime", key, strlen("uptime"))) - { - snprintf(value, value_length,"%u", stat->uptime); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime); else if (!memcmp("time", key, strlen("time"))) - { - snprintf(value, value_length,"%llu", (unsigned long long)stat->time); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time); else if (!memcmp("version", key, strlen("version"))) - { - snprintf(value, value_length,"%s", stat->version); - } + snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version); else if (!memcmp("pointer_size", key, strlen("pointer_size"))) - { - snprintf(value, value_length,"%u", stat->pointer_size); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size); else if (!memcmp("rusage_user", key, strlen("rusage_user"))) - { - snprintf(value, value_length,"%u", stat->rusage_user); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user); else if (!memcmp("rusage_system", key, strlen("rusage_system"))) - { - snprintf(value, value_length,"%u", stat->rusage_system); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system); else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds"))) - { - snprintf(value, value_length,"%u", stat->rusage_user_seconds); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_seconds); else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds"))) - { - snprintf(value, value_length,"%u", stat->rusage_user_microseconds); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_user_microseconds); else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds"))) - { - snprintf(value, value_length,"%u", stat->rusage_system_seconds); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_seconds); else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds"))) - { - snprintf(value, value_length,"%u", stat->rusage_system_microseconds); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->rusage_system_microseconds); else if (!memcmp("curr_items", key, strlen("curr_items"))) - { - snprintf(value, value_length,"%u", stat->curr_items); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items); else if (!memcmp("total_items", key, strlen("total_items"))) - { - snprintf(value, value_length,"%u", stat->total_items); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items); else if (!memcmp("bytes", key, strlen("bytes"))) - { - snprintf(value, value_length,"%llu", stat->bytes); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes); else if (!memcmp("curr_connections", key, strlen("curr_connections"))) - { - snprintf(value, value_length,"%u", stat->curr_connections); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections); else if (!memcmp("total_connections", key, strlen("total_connections"))) - { - snprintf(value, value_length,"%u", stat->total_connections); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections); else if (!memcmp("connection_structures", key, strlen("connection_structures"))) - { - snprintf(value, value_length,"%u", stat->connection_structures); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures); else if (!memcmp("cmd_get", key, strlen("cmd_get"))) - { - snprintf(value, value_length,"%llu", stat->cmd_get); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_get); else if (!memcmp("cmd_set", key, strlen("cmd_set"))) - { - snprintf(value, value_length,"%llu", stat->cmd_set); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->cmd_set); else if (!memcmp("get_hits", key, strlen("get_hits"))) - { - snprintf(value, value_length,"%llu", stat->get_hits); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_hits); else if (!memcmp("get_misses", key, strlen("get_misses"))) - { - snprintf(value, value_length,"%llu", stat->get_misses); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->get_misses); else if (!memcmp("evictions", key, strlen("evictions"))) - { - snprintf(value, value_length,"%llu", stat->evictions); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->evictions); else if (!memcmp("bytes_read", key, strlen("bytes_read"))) - { - snprintf(value, value_length,"%llu", stat->bytes_read); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_read); else if (!memcmp("bytes_written", key, strlen("bytes_written"))) - { - snprintf(value, value_length,"%llu", stat->bytes_written); - } + snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->bytes_written); else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) - { - snprintf(value, value_length,"%u", stat->limit_maxbytes); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes); else if (!memcmp("threads", key, strlen("threads"))) - { - snprintf(value, value_length,"%u", stat->threads); - } + snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads); else { - rc= MEMCACHED_NOTFOUND; + *error= MEMCACHED_NOTFOUND; + return NULL; } - return rc; + return strdup(buffer); } static memcached_return memcached_stats_fetch(memcached_st *ptr, @@ -328,8 +279,8 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur unsigned int x; memcached_return rc; memcached_stat_st *stats; - rc= memcached_connect(ptr); + rc= memcached_connect(ptr); if (rc != MEMCACHED_SUCCESS) { *error= rc; @@ -337,9 +288,10 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur } stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1)); - if (stats) + if (!stats) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + free(stats); return NULL; } memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1)); @@ -351,12 +303,12 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur rc= MEMCACHED_SOME_ERRORS; } - *error= x == 0 ? MEMCACHED_SUCCESS : rc; + *error= rc; return stats; } -memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, - char *hostname, unsigned int port) +memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, + char *hostname, unsigned int port) { memcached_return rc; memcached_st memc; @@ -381,7 +333,8 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, We make a copy of the keys since at some point in the not so distant future we will add support for "found" keys. */ -char ** memcached_stat_get_keys(memcached_stat_st *stat, memcached_return *error) +char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, + memcached_return *error) { char **list= (char **)malloc(sizeof(memcached_stat_keys)); diff --git a/lib/memcached_storage.c b/lib/memcached_storage.c index 21dc0934..1ee6c4ba 100644 --- a/lib/memcached_storage.c +++ b/lib/memcached_storage.c @@ -22,10 +22,10 @@ static memcached_return memcached_send(memcached_st *ptr, char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; unsigned int server_key; - rc= memcached_connect(ptr); assert(value); assert(value_length); + rc= memcached_connect(ptr); if (rc != MEMCACHED_SUCCESS) return rc; diff --git a/lib/memcached_strerror.c b/lib/memcached_strerror.c index 335ec26c..c11af89f 100644 --- a/lib/memcached_strerror.c +++ b/lib/memcached_strerror.c @@ -42,6 +42,12 @@ char *memcached_strerror(memcached_st *ptr, memcached_return rc) return "PARTIAL READ"; case MEMCACHED_SOME_ERRORS: return "SOME ERRORS WERE REPORTED"; + case MEMCACHED_NO_SERVERS: + return "NO SERVERS DEFINED"; + case MEMCACHED_MAXIMUM_RETURN: + return "Gibberish returned!"; + default: + return "Gibberish returned!"; }; return "COMPLETELY UNKNOWN ERROR, SOMEONE FORGOT TO UPDATE ERROR MESSAGES"; diff --git a/src/memcp.c b/src/memcp.c index d85fd44d..0001713d 100644 --- a/src/memcp.c +++ b/src/memcp.c @@ -97,7 +97,6 @@ int main(int argc, char *argv[]) fprintf(stderr, "memcp: %s: memcache error %s\n", ptr, memcached_strerror(memc, rc)); - WATCHPOINT; free(file_buffer_ptr); close(fd); optind++; diff --git a/src/memstat.c b/src/memstat.c index 3d30d16b..d6e31507 100644 --- a/src/memstat.c +++ b/src/memstat.c @@ -14,9 +14,9 @@ /* Prototypes */ void options_parse(int argc, char *argv[]); -static int opt_verbose; -static int opt_displayflag; -static char *opt_servers; +static int opt_verbose= 0; +static int opt_displayflag= 0; +static char *opt_servers= NULL; int main(int argc, char *argv[]) { @@ -26,7 +26,44 @@ int main(int argc, char *argv[]) options_parse(argc, argv); if (opt_servers) + { + unsigned int x; + memcached_return rc; + memcached_stat_st *stat; + memcached_server_st *server_list; + parse_opt_servers(memc, opt_servers); + stat= memcached_stat(memc, NULL, &rc); + + server_list= memcached_server_list(memc); + + printf("Listing %u Server\n\n", memcached_server_count(memc)); + for (x= 0; x < memcached_server_count(memc); x++) + { + char **list; + char **ptr; + + list= memcached_stat_get_keys(memc, &stat[x], &rc); + assert(rc == MEMCACHED_SUCCESS); + + printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]), + memcached_server_port(memc, server_list[x])); + for (ptr= list; *ptr; ptr++) + { + memcached_return rc; + char *value= memcached_stat_get_value(memc, &stat[x], *ptr, &rc); + + printf("\t %s: %s\n", *ptr, value); + free(value); + } + + free(list); + printf("\n"); + } + + free(stat); + free(opt_servers); + } memcached_deinit(memc); @@ -72,7 +109,7 @@ void options_parse(int argc, char *argv[]) exit(0); break; case OPT_SERVERS: /* --servers or -s */ - opt_servers= optarg; + opt_servers= strdup(optarg); break; case '?': /* getopt_long already printed an error message. */ diff --git a/src/utilities.c b/src/utilities.c index c866dd54..eeec1688 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -8,28 +8,31 @@ void parse_opt_servers(memcached_st *memc, char *begin_ptr; char *end_ptr; - end_ptr= server_strings + strlen(server_strings); - assert(server_strings); assert(memc); + end_ptr= server_strings + strlen(server_strings); + for (begin_ptr= server_strings, string= index(server_strings, ','); - begin_ptr == end_ptr; - begin_ptr= ++string, string= index(begin_ptr, ',')) + begin_ptr != end_ptr; + string= index(begin_ptr, ',')) { char buffer[HUGE_STRING_LEN]; char *ptr; + port= 0; memset(buffer, 0, HUGE_STRING_LEN); if (string) { memcpy(buffer, begin_ptr, string - begin_ptr); + begin_ptr= string+1; } else { size_t length= strlen(server_strings); memcpy(buffer, begin_ptr, length); string= server_strings+length; + begin_ptr= end_ptr; } ptr= index(buffer, ':'); @@ -41,12 +44,8 @@ void parse_opt_servers(memcached_st *memc, ptr++; port= strtol(ptr, (char **)NULL, 10); - - memcached_server_add(memc, buffer, port); - } - else - { - memcached_server_add(memc, buffer, 0); } + + memcached_server_add(memc, buffer, port); } } diff --git a/tests/Makefile.am b/tests/Makefile.am index 57a6fe9b..deb825c4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,10 +9,21 @@ testapp_SOURCES = test.c testapp_LDADD = $(LDADDS) record: - ./testapp output.txt > output.res + ./testapp > output.res + +record-extended: + ./testapp extended > output2.res + test: testapp - ./testapp output.txt > output.cmp + ./testapp > output.cmp diff output.res output.cmp +test-extended: testapp + ./testapp extended > output.cmp + diff output2.res output.cmp + valgrind: - libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp output.txt + libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp + +valgrind-extended: + libtool --mode=execute valgrind --leak-check=yes --show-reachable=yes testapp extended diff --git a/tests/output.res b/tests/output.res index 445d3783..50cb7861 100644 --- a/tests/output.res +++ b/tests/output.res @@ -1,3 +1,49 @@ +Error 0 -> SUCCESS +Error 1 -> FAILURE +Error 2 -> HOSTNAME LOOKUP FAILURE +Error 3 -> CONNECTION FAILURE +Error 4 -> CONNECTION BIND FAILURE +Error 5 -> WRITE FAILURE +Error 6 -> READ FAILURE +Error 7 -> UNKNOWN READ FAILURE +Error 8 -> PROTOCOL ERROR +Error 9 -> CLIENT ERROR +Error 10 -> SERVER ERROR +Error 11 -> CONNECTION SOCKET CREATE FAILURE +Error 12 -> CONNECTION DATA EXISTS +Error 13 -> CONNECTION DATA DOES NOT EXIST +Error 14 -> NOT STORED +Error 15 -> NOT FOUND +Error 16 -> MEMORY ALLOCATION FAILURE +Error 17 -> PARTIAL READ +Error 18 -> SOME ERRORS WERE REPORTED +Error 19 -> NO SERVERS DEFINED +Found key pid +Found key uptime +Found key time +Found key version +Found key pointer_size +Found key rusage_user +Found key rusage_system +Found key rusage_user_seconds +Found key rusage_user_microseconds +Found key rusage_system_seconds +Found key rusage_system_microseconds +Found key curr_items +Found key total_items +Found key bytes +Found key curr_connections +Found key total_connections +Found key connection_structures +Found key cmd_get +Found key cmd_set +Found key get_hits +Found key get_misses +Found key evictions +Found key bytes_read +Found key bytes_written +Found key limit_maxbytes +Found key threads Found key pid Found key uptime Found key time diff --git a/tests/output2.res b/tests/output2.res new file mode 100644 index 00000000..7bf929ee --- /dev/null +++ b/tests/output2.res @@ -0,0 +1,46 @@ +Error 0 -> SUCCESS +Error 1 -> FAILURE +Error 2 -> HOSTNAME LOOKUP FAILURE +Error 3 -> CONNECTION FAILURE +Error 4 -> CONNECTION BIND FAILURE +Error 5 -> WRITE FAILURE +Error 6 -> READ FAILURE +Error 7 -> UNKNOWN READ FAILURE +Error 8 -> PROTOCOL ERROR +Error 9 -> CLIENT ERROR +Error 10 -> SERVER ERROR +Error 11 -> CONNECTION SOCKET CREATE FAILURE +Error 12 -> CONNECTION DATA EXISTS +Error 13 -> CONNECTION DATA DOES NOT EXIST +Error 14 -> NOT STORED +Error 15 -> NOT FOUND +Error 16 -> MEMORY ALLOCATION FAILURE +Error 17 -> PARTIAL READ +Error 18 -> SOME ERRORS WERE REPORTED +Error 19 -> NO SERVERS DEFINED +Found key pid +Found key uptime +Found key time +Found key version +Found key pointer_size +Found key rusage_user +Found key rusage_system +Found key rusage_user_seconds +Found key rusage_user_microseconds +Found key rusage_system_seconds +Found key rusage_system_microseconds +Found key curr_items +Found key total_items +Found key bytes +Found key curr_connections +Found key total_connections +Found key connection_structures +Found key cmd_get +Found key cmd_set +Found key get_hits +Found key get_misses +Found key evictions +Found key bytes_read +Found key bytes_written +Found key limit_maxbytes +Found key threads diff --git a/tests/test.c b/tests/test.c index 4af1a370..dabb00f4 100644 --- a/tests/test.c +++ b/tests/test.c @@ -28,12 +28,28 @@ void connection_test(void) memcached_return rc; memcached_st *memc; memc= memcached_init(NULL); + assert(memc); rc= memcached_server_add(memc, "localhost", 0); assert(rc == MEMCACHED_SUCCESS); assert(memc); memcached_deinit(memc); } +void error_test(void) +{ + memcached_st *memc; + memc= memcached_init(NULL); + memcached_return rc; + + for (rc= MEMCACHED_SUCCESS; rc < MEMCACHED_MAXIMUM_RETURN; rc++) + { + printf("Error %d -> %s\n", rc, memcached_strerror(memc, rc)); + } + + assert(memc); + memcached_deinit(memc); +} + void set_test(void) { memcached_st *memc; @@ -43,6 +59,8 @@ void set_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -60,6 +78,8 @@ void add_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); rc= memcached_add(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -77,6 +97,8 @@ void replace_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); rc= memcached_replace(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -94,6 +116,8 @@ void delete_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -112,6 +136,8 @@ void flush_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); rc= memcached_flush(memc, 0); assert(rc == MEMCACHED_SUCCESS); @@ -129,6 +155,8 @@ void get_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); string= memcached_get(memc, key, strlen(key), &string_length, &flags, &rc); @@ -152,6 +180,9 @@ void get_test2(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -188,6 +219,9 @@ void get_test3(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_set(memc, key, strlen(key), value, value_length, (time_t)0, (uint16_t)0); @@ -205,13 +239,13 @@ void get_test3(void) memcached_deinit(memc); } -void stats_hostname_test(void) +void stats_servername_test(void) { memcached_return rc; memcached_stat_st stat; - rc= memcached_stat_hostname(&stat, NULL, - "localhost", - MEMCACHED_DEFAULT_PORT); + rc= memcached_stat_servername(&stat, NULL, + "localhost", + MEMCACHED_DEFAULT_PORT); } void increment_test(void) @@ -224,6 +258,9 @@ void increment_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -252,6 +289,9 @@ void decrement_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint16_t)0); @@ -279,6 +319,9 @@ void quit_test(void) memc= memcached_init(NULL); assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)10, (uint16_t)3); @@ -309,15 +352,22 @@ void mget_test(void) memc= memcached_init(NULL); assert(memc); - + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + + /* We need to empty the server before continueing test */ + rc= memcached_flush(memc, 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_mget(memc, keys, key_length, 3); assert(rc == MEMCACHED_SUCCESS); - while (return_value= memcached_fetch(memc, return_key, &return_key_length, - &return_value_length, &flags, &rc)) + while ((return_value= memcached_fetch(memc, return_key, &return_key_length, + &return_value_length, &flags, &rc)) != NULL) { assert(return_value); } + assert(!return_value); assert(return_value_length == 0); assert(rc == MEMCACHED_NOTFOUND); @@ -351,26 +401,101 @@ void get_stats_keys(void) { char **list; char **ptr; + memcached_st *memc; memcached_stat_st stat; memcached_return rc; - list= memcached_stat_get_keys(&stat, &rc); + memc= memcached_init(NULL); + assert(memc); + + list= memcached_stat_get_keys(memc, &stat, &rc); assert(rc == MEMCACHED_SUCCESS); for (ptr= list; *ptr; ptr++) printf("Found key %s\n", *ptr); free(list); + memcached_deinit(memc); +} + +void get_stats(void) +{ + unsigned int x; + char **list; + char **ptr; + memcached_return rc; + memcached_st *memc; + memcached_stat_st *stat; + + memc= memcached_init(NULL); + assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + + stat= memcached_stat(memc, NULL, &rc); + assert(rc == MEMCACHED_SUCCESS); + + assert(rc == MEMCACHED_SUCCESS); + assert(stat); + + for (x= 0; x < memcached_server_count(memc); x++) + { + list= memcached_stat_get_keys(memc, &stat[x], &rc); + assert(rc == MEMCACHED_SUCCESS); + for (ptr= list; *ptr; ptr++) + printf("Found key %s\n", *ptr); + free(list); + } + + free(stat); + memcached_deinit(memc); +} + +void get_stats_multiple(void) +{ + unsigned int x; + char **list; + char **ptr; + memcached_return rc; + memcached_st *memc; + memcached_stat_st *stat; + + memc= memcached_init(NULL); + assert(memc); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + rc= memcached_server_add(memc, "localhost", 5555); + assert(rc == MEMCACHED_SUCCESS); + + stat= memcached_stat(memc, NULL, &rc); + assert(rc == MEMCACHED_SUCCESS); + + assert(rc == MEMCACHED_SUCCESS); + assert(stat); + + for (x= 0; x < memcached_server_count(memc); x++) + { + list= memcached_stat_get_keys(memc, &stat[x], &rc); + assert(rc == MEMCACHED_SUCCESS); + for (ptr= list; *ptr; ptr++) + printf("Found key %s\n", *ptr); + + free(list); + } + + free(stat); + memcached_deinit(memc); } -int main(void) +int main(int argc, char argvp[]) { /* Clean the server before beginning testing */ flush_test(); init_test(); allocation_test(); connection_test(); + error_test(); set_test(); add_test(); replace_test(); @@ -380,12 +505,19 @@ int main(void) get_test(); get_test2(); get_test3(); - stats_hostname_test(); + stats_servername_test(); increment_test(); decrement_test(); quit_test(); -// mget_test(); + mget_test(); + get_stats(); + + /* The multiple tests */ + if (argc == 2) + { + get_stats_multiple(); + } get_stats_keys(); /* Clean up whatever we might have left */ -- 2.30.2