From: Brian Aker Date: Sun, 10 Jan 2010 00:49:55 +0000 (-0800) Subject: number_of_hosts <-- encapsulated. X-Git-Tag: 0.37~11 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=ac40b10adf3a182fe62d24899eb96c10364ba91b;p=m6w6%2Flibmemcached number_of_hosts <-- encapsulated. --- diff --git a/ChangeLog b/ChangeLog index 76e5006b..facce491 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,8 @@ * Modified RPM to strict check install. * Added documentation for memcached_server_cursor(); + * Added memcached_servers_reset(). + 0.36 Wed Jan 6 18:23:50 PST 2010 * Merged in new memslap utility. * All of constants.h has been updated to match style (all old identifiers diff --git a/docs/Makefile.am b/docs/Makefile.am index 1dac6ade..03ba2eb6 100644 --- a/docs/Makefile.am +++ b/docs/Makefile.am @@ -31,9 +31,10 @@ CALLBACK_PAGES= \ BUILT_SOURCES += ${CALLBACK_PAGES} CREATE_PAGES= \ + memcached_clone.pop \ memcached_create.pop \ memcached_free.pop \ - memcached_clone.pop + memcached_servers_reset.pop BUILT_SOURCES += ${CREATE_PAGES} DELETE_PAGES= \ diff --git a/docs/memcached_create.pod b/docs/memcached_create.pod index 421ba131..4323723b 100644 --- a/docs/memcached_create.pod +++ b/docs/memcached_create.pod @@ -1,6 +1,6 @@ =head1 NAME -memcached_create, memcached_free - Create a memcached_st structure +memcached_create, memcached_free, memcached_clone, memcached_servers_reset- Create a memcached_st structure =head1 LIBRARY @@ -16,6 +16,8 @@ C Client Library for memcached (libmemcached, -lmemcached) memcached_st *memcached_clone (memcached_st *destination, memcached_st *source); + void memcached_servers_reset(memcached_st); + =head1 DESCRIPTION memcached_create() is used to create a C structure that will then @@ -28,6 +30,9 @@ defaults and list of servers from the source C. If you pass a null the argument for the source to clone, it is the same as a call to memcached_create(). If the destination argument is NULL a C will be allocated for you. +memcached_servers_reset() allows you to zero out the list of servers that +the memcached_st has. + To clean up memory associated with a C structure you should pass it to memcached_free() when you are finished using it. memcached_free() is the only way to make sure all memory is deallocated when you finish using diff --git a/docs/memcached_servers.pod b/docs/memcached_servers.pod index 05b50a29..6ffd8e22 100644 --- a/docs/memcached_servers.pod +++ b/docs/memcached_servers.pod @@ -10,7 +10,7 @@ C Client Library for memcached (libmemcached, -lmemcached) #include - unsigned int memcached_server_count (memcached_st *ptr); + uint32_t memcached_server_count (memcached_st *ptr); memcached_server_st * memcached_server_list (memcached_st *ptr); diff --git a/libmemcached/analyze.c b/libmemcached/analyze.c index d5d09751..7dcbf8cb 100644 --- a/libmemcached/analyze.c +++ b/libmemcached/analyze.c @@ -70,7 +70,7 @@ memcached_analysis_st *memcached_analyze(memcached_st *memc, *error= MEMCACHED_SUCCESS; server_count= memcached_server_count(memc); - result= (memcached_analysis_st*)calloc(memc->number_of_hosts, + result= (memcached_analysis_st*)calloc(memcached_server_count(memc), sizeof(memcached_analysis_st)); if (!result) diff --git a/libmemcached/auto.c b/libmemcached/auto.c index b7466404..5e3e0261 100644 --- a/libmemcached/auto.c +++ b/libmemcached/auto.c @@ -24,7 +24,7 @@ static memcached_return_t memcached_auto(memcached_st *ptr, unsigned int server_key; bool no_reply= ptr->flags.no_reply; - unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) + unlikely (ptr->hosts == NULL || memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) @@ -82,7 +82,7 @@ static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, unsigned int server_key; bool no_reply= ptr->flags.no_reply; - unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) + unlikely (ptr->hosts == NULL || memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; server_key= memcached_generate_hash(ptr, master_key, master_key_length); diff --git a/libmemcached/behavior.c b/libmemcached/behavior.c index 5deaccd8..4813a3b8 100644 --- a/libmemcached/behavior.c +++ b/libmemcached/behavior.c @@ -90,7 +90,7 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, memcached_quit(ptr); break; case MEMCACHED_BEHAVIOR_USE_UDP: - if (ptr->number_of_hosts) + if (memcached_server_count(ptr)) { return MEMCACHED_FAILURE; } diff --git a/libmemcached/delete.c b/libmemcached/delete.c index 8250d58e..324e3c2c 100644 --- a/libmemcached/delete.c +++ b/libmemcached/delete.c @@ -32,7 +32,7 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr, unlikely (rc != MEMCACHED_SUCCESS) return rc; - unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0) + unlikely (ptr->hosts == NULL || memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; server_key= memcached_generate_hash(ptr, master_key, master_key_length); @@ -176,7 +176,7 @@ static inline memcached_return_t binary_delete(memcached_st *ptr, for (uint32_t x= 0; x < ptr->number_of_replicas; ++x) { ++server_key; - if (server_key == ptr->number_of_hosts) + if (server_key == memcached_server_count(ptr)) server_key= 0; memcached_server_st* server= &ptr->hosts[server_key]; diff --git a/libmemcached/dump.c b/libmemcached/dump.c index 3db6784d..cd2489e0 100644 --- a/libmemcached/dump.c +++ b/libmemcached/dump.c @@ -15,10 +15,10 @@ static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callb uint32_t server_key; uint32_t x; - unlikely (ptr->number_of_hosts == 0) + unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; - for (server_key= 0; server_key < ptr->number_of_hosts; server_key++) + for (server_key= 0; server_key < memcached_server_count(ptr); server_key++) { /* 256 I BELIEVE is the upper limit of slabs */ for (x= 0; x < 256; x++) diff --git a/libmemcached/flush.c b/libmemcached/flush.c index d05d7442..e9c63705 100644 --- a/libmemcached/flush.c +++ b/libmemcached/flush.c @@ -26,10 +26,10 @@ static memcached_return_t memcached_flush_textual(memcached_st *ptr, memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; - unlikely (ptr->number_of_hosts == 0) + unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { bool no_reply= ptr->flags.no_reply; @@ -56,7 +56,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, unsigned int x; protocol_binary_request_flush request= {.bytes= {0}}; - unlikely (ptr->number_of_hosts == 0) + unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ; @@ -66,7 +66,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, request.message.header.request.bodylen= htonl(request.message.header.request.extlen); request.message.body.expiration= htonl((uint32_t) expiration); - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { if (ptr->flags.no_reply) request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ; @@ -80,7 +80,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, } } - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { if (memcached_server_response_count(&ptr->hosts[x]) > 0) (void)memcached_response(&ptr->hosts[x], NULL, 0, NULL); diff --git a/libmemcached/flush_buffers.c b/libmemcached/flush_buffers.c index 950fa1e8..aeae5205 100644 --- a/libmemcached/flush_buffers.c +++ b/libmemcached/flush_buffers.c @@ -1,19 +1,19 @@ #include "common.h" -memcached_return_t memcached_flush_buffers(memcached_st *mem) +memcached_return_t memcached_flush_buffers(memcached_st *memc) { memcached_return_t ret= MEMCACHED_SUCCESS; - for (uint32_t x= 0; x < mem->number_of_hosts; ++x) - if (mem->hosts[x].write_buffer_offset != 0) + for (uint32_t x= 0; x < memcached_server_count(memc); ++x) + if (memc->hosts[x].write_buffer_offset != 0) { - if (mem->hosts[x].fd == -1 && - (ret= memcached_connect(&mem->hosts[x])) != MEMCACHED_SUCCESS) + if (memc->hosts[x].fd == -1 && + (ret= memcached_connect(&memc->hosts[x])) != MEMCACHED_SUCCESS) { WATCHPOINT_ERROR(ret); return ret; } - if (memcached_io_write(&mem->hosts[x], NULL, 0, 1) == -1) + if (memcached_io_write(&memc->hosts[x], NULL, 0, 1) == -1) ret= MEMCACHED_SOME_ERRORS; } diff --git a/libmemcached/get.c b/libmemcached/get.c index 9a570585..67670e5c 100644 --- a/libmemcached/get.c +++ b/libmemcached/get.c @@ -160,7 +160,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, if (number_of_keys == 0) return MEMCACHED_NOTFOUND; - if (ptr->number_of_hosts == 0) + if (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; if (ptr->flags.verify_key && (memcached_key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED)) @@ -180,7 +180,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, It might be optimum to bounce the connection if count > some number. */ - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { if (memcached_server_response_count(&ptr->hosts[x])) { @@ -263,7 +263,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, /* Should we muddle on if some servers are dead? */ - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { if (memcached_server_response_count(&ptr->hosts[x])) { @@ -412,7 +412,7 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, request.message.header.request.opcode= PROTOCOL_BINARY_CMD_NOOP; request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) if (memcached_server_response_count(&ptr->hosts[x])) { if (memcached_io_write(&ptr->hosts[x], NULL, 0, 1) == -1) @@ -457,7 +457,7 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, for (x= 0; x < number_of_keys; ++x) { - if (hash[x] == ptr->number_of_hosts) + if (hash[x] == memcached_server_count(ptr)) continue; /* Already successfully sent */ uint32_t server= hash[x] + replica; @@ -466,8 +466,8 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, if (randomize_read && ((server + start) <= (hash[x] + ptr->number_of_replicas))) server += start; - while (server >= ptr->number_of_hosts) - server -= ptr->number_of_hosts; + while (server >= memcached_server_count(ptr)) + server -= memcached_server_count(ptr); if (dead_servers[server]) continue; @@ -516,7 +516,7 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, } memcached_server_response_increment(&ptr->hosts[server]); - hash[x]= ptr->number_of_hosts; + hash[x]= memcached_server_count(ptr); } if (success) @@ -547,7 +547,7 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, bool* dead_servers; hash= ptr->call_malloc(ptr, sizeof(uint32_t) * number_of_keys); - dead_servers= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(bool)); + dead_servers= ptr->call_calloc(ptr, memcached_server_count(ptr), sizeof(bool)); if (hash == NULL || dead_servers == NULL) { diff --git a/libmemcached/hash.c b/libmemcached/hash.c index 7683761e..6ead8d46 100644 --- a/libmemcached/hash.c +++ b/libmemcached/hash.c @@ -111,9 +111,9 @@ uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length) uint32_t hash= 1; /* Just here to remove compile warning */ - WATCHPOINT_ASSERT(ptr->number_of_hosts); + WATCHPOINT_ASSERT(memcached_server_count(ptr)); - if (ptr->number_of_hosts == 1) + if (memcached_server_count(ptr) == 1) return 0; hash= memcached_generate_hash_value(key, key_length, ptr->hash); @@ -150,13 +150,13 @@ static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash) return right->index; } case MEMCACHED_DISTRIBUTION_MODULA: - return hash % ptr->number_of_hosts; + return hash % memcached_server_count(ptr); case MEMCACHED_DISTRIBUTION_RANDOM: - return (uint32_t) random() % ptr->number_of_hosts; + return (uint32_t) random() % memcached_server_count(ptr); case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: default: WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */ - return hash % ptr->number_of_hosts; + return hash % memcached_server_count(ptr); } /* NOTREACHED */ } @@ -169,9 +169,9 @@ uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_ { uint32_t hash= 1; /* Just here to remove compile warning */ - WATCHPOINT_ASSERT(ptr->number_of_hosts); + WATCHPOINT_ASSERT(memcached_server_count(ptr)); - if (ptr->number_of_hosts == 1) + if (memcached_server_count(ptr) == 1) return 0; if (ptr->flags.hash_with_prefix_key) diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index cb231026..e8180720 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -26,10 +26,10 @@ static int compare_servers(const void *p1, const void *p2) static void sort_hosts(memcached_st *ptr) { - if (ptr->number_of_hosts) + if (memcached_server_count(ptr)) { - qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); - ptr->hosts[0].number_of_hosts= ptr->number_of_hosts; + qsort(ptr->hosts, memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers); + ptr->hosts[0].number_of_hosts= memcached_server_count(ptr); } } @@ -60,26 +60,6 @@ memcached_return_t run_distribution(memcached_st *ptr) return MEMCACHED_SUCCESS; } -void server_list_free(memcached_st *ptr, memcached_server_st *servers) -{ - uint32_t x; - - if (servers == NULL) - return; - - for (x= 0; x < memcached_servers_count(servers); x++) - if (servers[x].address_info) - { - freeaddrinfo(servers[x].address_info); - servers[x].address_info= NULL; - } - - if (ptr) - ptr->call_free(ptr, servers); - else - free(servers); -} - static uint32_t ketama_server_hash(const char *key, unsigned int key_length, int alignment) { unsigned char results[16]; @@ -137,7 +117,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) { live_servers= 0; ptr->next_distribution_rebuild= 0; - for (host_index= 0; host_index < ptr->number_of_hosts; ++host_index) + for (host_index= 0; host_index < memcached_server_count(ptr); ++host_index) { if (list[host_index].next_retry <= now.tv_sec) live_servers++; @@ -149,7 +129,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } } else - live_servers= ptr->number_of_hosts; + live_servers= memcached_server_count(ptr); is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER); @@ -173,7 +153,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) if (is_ketama_weighted) { - for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index) + for (host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { if (list[host_index].weight == 0) { @@ -184,7 +164,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } } - for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index) + for (host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec) continue; @@ -295,12 +275,12 @@ static memcached_return_t update_continuum(memcached_st *ptr) WATCHPOINT_ASSERT(ptr); WATCHPOINT_ASSERT(ptr->continuum); - WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE); + WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE); ptr->continuum_points_counter= pointer_counter; qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp); #ifdef DEBUG - for (pointer_index= 0; ptr->number_of_hosts && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++) + for (pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++) { WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value); } @@ -321,7 +301,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st count= memcached_servers_count(list); new_host_list= ptr->call_realloc(ptr, ptr->hosts, - sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); + sizeof(memcached_server_st) * (count + memcached_server_count(ptr))); if (! new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -336,13 +316,13 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st return MEMCACHED_INVALID_HOST_PROTOCOL; WATCHPOINT_ASSERT(list[x].hostname[0] != 0); - memcached_server_create(ptr, &ptr->hosts[ptr->number_of_hosts]); + memcached_server_create(ptr, &ptr->hosts[memcached_server_count(ptr)]); /* TODO check return type */ - (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname, + (void)memcached_server_create_with(ptr, &ptr->hosts[memcached_server_count(ptr)], list[x].hostname, list[x].port, list[x].weight, list[x].type); ptr->number_of_hosts++; } - ptr->hosts[0].number_of_hosts= (uint16_t) ptr->number_of_hosts; + ptr->hosts[0].number_of_hosts= memcached_server_count(ptr); return run_distribution(ptr); } @@ -417,7 +397,7 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, return MEMCACHED_INVALID_HOST_PROTOCOL; new_host_list= ptr->call_realloc(ptr, ptr->hosts, - sizeof(memcached_server_st) * (ptr->number_of_hosts+1)); + sizeof(memcached_server_st) * (ptr->number_of_hosts + 1)); if (new_host_list == NULL) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; @@ -425,10 +405,10 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, ptr->hosts= new_host_list; /* TODO: Check return type */ - (void)memcached_server_create_with(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, weight, type); + (void)memcached_server_create_with(ptr, &ptr->hosts[memcached_server_count(ptr)], hostname, port, weight, type); ptr->number_of_hosts++; - memcached_servers_set_count(&ptr->hosts[0], ptr->number_of_hosts); + memcached_servers_set_count(&ptr->hosts[0], memcached_server_count(ptr)); return run_distribution(ptr); } @@ -439,7 +419,7 @@ memcached_return_t memcached_server_remove(memcached_server_st *st_ptr) memcached_st *ptr= st_ptr->root; memcached_server_st *list= ptr->hosts; - for (x= 0, host_index= 0; x < ptr->number_of_hosts; x++) + for (x= 0, host_index= 0; x < memcached_server_count(ptr); x++) { if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH) != 0 || list[x].port != st_ptr->port) { diff --git a/libmemcached/io.c b/libmemcached/io.c index 712e11af..6d19c672 100644 --- a/libmemcached/io.c +++ b/libmemcached/io.c @@ -156,7 +156,7 @@ void memcached_io_preread(memcached_st *ptr) return; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { if (memcached_server_response_count(ptr, x) && ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER ) @@ -361,7 +361,7 @@ memcached_server_st *memcached_io_get_readable_server(memcached_st *memc) unsigned int host_index= 0; for (unsigned int x= 0; - x< memc->number_of_hosts && host_index < MAX_SERVERS_TO_POLL; + x< memcached_server_count(memc) && host_index < MAX_SERVERS_TO_POLL; ++x) { if (memc->hosts[x].read_buffer_length > 0) /* I have data in the buffer */ @@ -379,7 +379,7 @@ memcached_server_st *memcached_io_get_readable_server(memcached_st *memc) if (host_index < 2) { /* We have 0 or 1 server with pending events.. */ - for (unsigned int x= 0; x< memc->number_of_hosts; ++x) + for (unsigned int x= 0; x< memcached_server_count(memc); ++x) if (memcached_server_response_count(&memc->hosts[x]) > 0) return &memc->hosts[x]; @@ -394,11 +394,17 @@ memcached_server_st *memcached_io_get_readable_server(memcached_st *memc) case 0: break; default: - for (unsigned int x= 0; x < host_index; ++x) + for (size_t x= 0; x < host_index; ++x) + { if (fds[x].revents & POLLIN) - for (unsigned int y= 0; y < memc->number_of_hosts; ++y) + { + for (unsigned int y= 0; y < memcached_server_count(memc); ++y) + { if (memc->hosts[y].fd == fds[x].fd) return &memc->hosts[y]; + } + } + } } return NULL; diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 329e170d..8e385da0 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -44,6 +44,39 @@ memcached_st *memcached_create(memcached_st *ptr) return ptr; } +void server_list_free(memcached_st *ptr, memcached_server_st *servers) +{ + uint32_t x; + + if (servers == NULL) + return; + + for (x= 0; x < memcached_servers_count(servers); x++) + if (servers[x].address_info) + { + freeaddrinfo(servers[x].address_info); + servers[x].address_info= NULL; + } + + if (ptr) + { + ptr->call_free(ptr, servers); + } + else + free(servers); +} + +void memcached_servers_reset(memcached_st *ptr) +{ + server_list_free(ptr, ptr->hosts); + + ptr->hosts= NULL; + ptr->number_of_hosts= 0; + ptr->cursor_server= 0; + ptr->last_disconnected_server= NULL; + ptr->server_failure_limit= 0; +} + void memcached_free(memcached_st *ptr) { /* If we have anything open, lets close it now */ diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 4ea5ded2..4337f4fb 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -109,6 +109,9 @@ struct memcached_st { LIBMEMCACHED_API memcached_return_t memcached_version(memcached_st *ptr); +LIBMEMCACHED_API +void memcached_servers_reset(memcached_st *ptr); + /* Public API */ LIBMEMCACHED_API diff --git a/libmemcached/quit.c b/libmemcached/quit.c index dc11e97f..a8f70402 100644 --- a/libmemcached/quit.c +++ b/libmemcached/quit.c @@ -72,12 +72,12 @@ void memcached_quit(memcached_st *ptr) unsigned int x; if (ptr->hosts == NULL || - ptr->number_of_hosts == 0) + memcached_server_count(ptr) == 0) return; - if (ptr->hosts && ptr->number_of_hosts) + if (ptr->hosts && memcached_server_count(ptr)) { - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) memcached_quit_server(&ptr->hosts[x], 0); } } diff --git a/libmemcached/server.c b/libmemcached/server.c index 8616657c..f3bc6998 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -115,7 +115,7 @@ memcached_return_t memcached_server_cursor(memcached_st *ptr, { uint32_t y; - for (y= 0; y < ptr->number_of_hosts; y++) + for (y= 0; y < memcached_server_count(ptr); y++) { uint32_t x; @@ -142,7 +142,7 @@ memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key unlikely (*error != MEMCACHED_SUCCESS) return NULL; - unlikely (ptr->number_of_hosts == 0) + unlikely (memcached_server_count(ptr) == 0) { *error= MEMCACHED_NO_SERVERS; return NULL; @@ -162,10 +162,9 @@ memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key const char *memcached_server_error(memcached_server_st *ptr) { - if (ptr) - return ptr->cached_server_error; - else - return NULL; + return ptr + ? ptr->cached_server_error + : NULL; } void memcached_server_error_reset(memcached_server_st *ptr) @@ -180,10 +179,9 @@ memcached_server_st *memcached_server_get_last_disconnect(memcached_st *ptr) uint32_t memcached_server_list_count(memcached_server_st *ptr) { - if (ptr == NULL) - return 0; - - return memcached_servers_count(ptr); + return (ptr == NULL) + ? 0 + : memcached_servers_count(ptr); } void memcached_server_list_free(memcached_server_st *ptr) diff --git a/libmemcached/stats.c b/libmemcached/stats.c index 4f3bdb0d..957c5b61 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -369,7 +369,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur return NULL; } - stats= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(memcached_stat_st)); + stats= ptr->call_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st)); stats->root= ptr; @@ -380,7 +380,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur } rc= MEMCACHED_SUCCESS; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t temp_return; diff --git a/libmemcached/storage.c b/libmemcached/storage.c index f121364d..6f1857f3 100644 --- a/libmemcached/storage.c +++ b/libmemcached/storage.c @@ -77,7 +77,7 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, unlikely (rc != MEMCACHED_SUCCESS) return rc; - unlikely (ptr->number_of_hosts == 0) + unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) @@ -493,7 +493,7 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, for (uint32_t x= 0; x < ptr->number_of_replicas; x++) { ++server_key; - if (server_key == ptr->number_of_hosts) + if (server_key == memcached_server_count(ptr)) server_key= 0; memcached_server_st *srv= &ptr->hosts[server_key]; diff --git a/libmemcached/verbosity.c b/libmemcached/verbosity.c index 3d34880d..46b120e9 100644 --- a/libmemcached/verbosity.c +++ b/libmemcached/verbosity.c @@ -13,7 +13,7 @@ memcached_return_t memcached_verbosity(memcached_st *ptr, unsigned int verbosity return MEMCACHED_WRITE_FAILURE; rc= MEMCACHED_SUCCESS; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; diff --git a/libmemcached/version.c b/libmemcached/version.c index db2419cf..91e926e3 100644 --- a/libmemcached/version.c +++ b/libmemcached/version.c @@ -31,7 +31,7 @@ static inline memcached_return_t memcached_version_textual(memcached_st *ptr) send_length= strlen(command); rc= MEMCACHED_SUCCESS; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; @@ -75,7 +75,7 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES; rc= MEMCACHED_SUCCESS; - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; @@ -88,7 +88,8 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) } } - for (x= 0; x < ptr->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(ptr); x++) + { if (memcached_server_response_count(&ptr->hosts[x]) > 0) { memcached_return_t rrc; @@ -107,6 +108,7 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) ptr->hosts[x].minor_version= (uint8_t)strtol(p + 1, &p, 10); ptr->hosts[x].micro_version= (uint8_t)strtol(p + 1, NULL, 10); } + } return rc; } diff --git a/tests/function.c b/tests/function.c index 3df86f31..71e13695 100644 --- a/tests/function.c +++ b/tests/function.c @@ -108,7 +108,7 @@ static test_return_t server_sort_test(memcached_st *ptr __attribute__((unused)) { test_ports[x]= (uint32_t)random() % 64000; rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); - test_truth(local_memc->number_of_hosts == x + 1); + test_truth(memcached_server_count(local_memc) == x + 1); test_truth(memcached_servers_count(local_memc->hosts) == x+1); test_truth(rc == MEMCACHED_SUCCESS); } @@ -179,7 +179,7 @@ static test_return_t server_unsort_test(memcached_st *ptr __attribute__((unused { test_ports[x]= (uint32_t)(random() % 64000); rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); - test_truth(local_memc->number_of_hosts == x+1); + test_truth(memcached_server_count(local_memc) == x+1); test_truth(memcached_servers_count(local_memc->hosts) == x+1); test_truth(rc == MEMCACHED_SUCCESS); } @@ -2030,11 +2030,7 @@ static test_return_t user_supplied_bug4(memcached_st *memc) size_t return_value_length; /* Here we free everything before running a bunch of mget tests */ - { - memcached_server_list_free(memc->hosts); - memc->hosts= NULL; - memc->number_of_hosts= 0; - } + memcached_servers_reset(memc); /* We need to empty the server before continueing test */ @@ -2715,7 +2711,7 @@ static test_return_t user_supplied_bug18(memcached_st *trash) memcached_server_push(memc, server_pool); /* verify that the server list was parsed okay. */ - test_truth(memc->number_of_hosts == 8); + test_truth(memcached_server_count(memc) == 8); test_truth(strcmp(server_pool[0].hostname, "10.0.1.1") == 0); test_truth(server_pool[0].port == 11211); test_truth(server_pool[0].weight == 600); @@ -2866,7 +2862,7 @@ static test_return_t auto_eject_hosts(memcached_st *trash) memcached_server_push(memc, server_pool); /* verify that the server list was parsed okay. */ - test_truth(memc->number_of_hosts == 8); + test_truth(memcached_server_count(memc) == 8); test_truth(strcmp(server_pool[0].hostname, "10.0.1.1") == 0); test_truth(server_pool[0].port == 11211); test_truth(server_pool[0].weight == 600); @@ -3536,9 +3532,9 @@ static test_return_t pre_replication(memcached_st *memc) */ memcached_return_t rc; rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, - memc->number_of_hosts - 1); + memcached_server_count(memc) - 1); test_truth(rc == MEMCACHED_SUCCESS); - test_truth(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memc->number_of_hosts - 1); + test_truth(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS) == memcached_server_count(memc) - 1); return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED; } @@ -3843,9 +3839,7 @@ static test_return_t pre_unix_socket(memcached_st *memc) memcached_return_t rc; struct stat buf; - memcached_server_list_free(memc->hosts); - memc->hosts= NULL; - memc->number_of_hosts= 0; + memcached_servers_reset(memc); if (stat("/tmp/memcached.socket", &buf)) return TEST_SKIPPED; @@ -3935,7 +3929,7 @@ static test_return_t noreply_test(memcached_st *memc) ** way it is supposed to do!!!! */ int no_msg=0; - for (uint32_t x=0; x < memc->number_of_hosts; ++x) + for (uint32_t x=0; x < memcached_server_count(memc); ++x) no_msg+=(int)(memc->hosts[x].cursor_active); test_truth(no_msg == 0); @@ -4212,7 +4206,7 @@ static test_return_t replication_get_test(memcached_st *memc) * within the library, and this is not a supported interface. * This is to verify correct behavior in the library */ - for (uint32_t host= 0; host < memc->number_of_hosts; ++host) + for (uint32_t host= 0; host < memcached_server_count(memc); ++host) { memcached_st *memc_clone= memcached_clone(NULL, memc); memc_clone->hosts[host].port= 0; @@ -4410,11 +4404,11 @@ static void increment_request_id(uint16_t *id) static uint16_t *get_udp_request_ids(memcached_st *memc) { - uint16_t *ids= malloc(sizeof(uint16_t) * memc->number_of_hosts); + uint16_t *ids= malloc(sizeof(uint16_t) * memcached_server_count(memc)); assert(ids != NULL); unsigned int x; - for (x= 0; x < memc->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(memc); x++) ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) memc->hosts[x].write_buffer); return ids; @@ -4426,7 +4420,7 @@ static test_return_t post_udp_op_check(memcached_st *memc, uint16_t *expected_re memcached_server_st *cur_server = memc->hosts; uint16_t *cur_req_ids = get_udp_request_ids(memc); - for (x= 0; x < memc->number_of_hosts; x++) + for (x= 0; x < memcached_server_count(memc); x++) { test_truth(cur_server[x].cursor_active == 0); test_truth(cur_req_ids[x] == expected_req_ids[x]); @@ -4449,7 +4443,7 @@ static test_return_t init_udp(memcached_st *memc) || memc->hosts[0].micro_version < 6) return TEST_SKIPPED; - uint32_t num_hosts= memc->number_of_hosts; + uint32_t num_hosts= memcached_server_count(memc); unsigned int x= 0; memcached_server_st servers[num_hosts]; memcpy(servers, memc->hosts, sizeof(memcached_server_st) * num_hosts); @@ -4512,7 +4506,7 @@ static test_return_t set_udp_behavior_test(memcached_st *memc) test_truth(memc->flags.use_udp); test_truth(memc->flags.no_reply); - test_truth(memc->number_of_hosts == 0); + test_truth(memcached_server_count(memc) == 0); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,0); test_truth(! (memc->flags.use_udp)); @@ -4619,7 +4613,7 @@ static test_return_t udp_verbosity_test(memcached_st *memc) memcached_return_t rc; uint16_t *expected_ids= get_udp_request_ids(memc); unsigned int x; - for (x= 0; x < memc->number_of_hosts;x++) + for (x= 0; x < memcached_server_count(memc); x++) increment_request_id(&expected_ids[x]); rc= memcached_verbosity(memc,3); @@ -4639,7 +4633,7 @@ static test_return_t udp_flush_test(memcached_st *memc) memcached_return_t rc; uint16_t *expected_ids= get_udp_request_ids(memc); unsigned int x; - for (x= 0; x < memc->number_of_hosts;x++) + for (x= 0; x < memcached_server_count(memc);x++) increment_request_id(&expected_ids[x]); rc= memcached_flush(memc,0); @@ -4964,7 +4958,7 @@ static test_return_t ketama_compatibility_libmemcached(memcached_st *trash) memcached_server_push(memc, server_pool); /* verify that the server list was parsed okay. */ - test_truth(memc->number_of_hosts == 8); + test_truth(memcached_server_count(memc) == 8); test_strcmp(server_pool[0].hostname, "10.0.1.1"); test_truth(server_pool[0].port == 11211); test_truth(server_pool[0].weight == 600); @@ -5021,7 +5015,7 @@ static test_return_t ketama_compatibility_spymemcached(memcached_st *trash) memcached_server_push(memc, server_pool); /* verify that the server list was parsed okay. */ - test_truth(memc->number_of_hosts == 8); + test_truth(memcached_server_count(memc) == 8); test_strcmp(server_pool[0].hostname, "10.0.1.1"); test_truth(server_pool[0].port == 11211); test_truth(server_pool[0].weight == 600); @@ -5094,7 +5088,7 @@ static test_return_t regression_bug_434843(memcached_st *memc) * 1024 (that should satisfy most users don't you think?). Future versions * will include a mget_execute function call if you need a higher number. */ - uint32_t number_of_hosts= memc->number_of_hosts; + uint32_t number_of_hosts= memcached_server_count(memc); memc->number_of_hosts= 1; const size_t max_keys= 1024; char **keys= calloc(max_keys, sizeof(char*)); @@ -5143,6 +5137,7 @@ static test_return_t regression_bug_434843(memcached_st *memc) free(key_length); memc->number_of_hosts= number_of_hosts; + return TEST_SUCCESS; } @@ -5204,7 +5199,7 @@ static test_return_t regression_bug_442914(memcached_st *memc) test_truth(rc == MEMCACHED_SUCCESS); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1); - uint32_t number_of_hosts= memc->number_of_hosts; + uint32_t number_of_hosts= memcached_server_count(memc); memc->number_of_hosts= 1; char k[250]; @@ -5233,7 +5228,7 @@ static test_return_t regression_bug_442914(memcached_st *memc) static test_return_t regression_bug_447342(memcached_st *memc) { - if (memc->number_of_hosts < 3 || pre_replication(memc) != MEMCACHED_SUCCESS) + if (memcached_server_count(memc) < 3 || pre_replication(memc) != MEMCACHED_SUCCESS) return TEST_SKIPPED; memcached_return_t rc; @@ -5469,7 +5464,7 @@ static test_return_t wrong_failure_counter_test(memcached_st *memc) * in a non-portable way and you shouldn't be doing this. I'm only * doing this in order to verify that the library works the way it should */ - uint32_t number_of_hosts= memc->number_of_hosts; + uint32_t number_of_hosts= memcached_server_count(memc); memc->number_of_hosts= 1; /* Ensure that we are connected to the server by setting a value */