From 04ef974c23973986d4475e3cb8a876012264e2da Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 12 Feb 2010 18:23:49 -0800 Subject: [PATCH] Lots of compiler fixes. --- ChangeLog | 2 + clients/memstat.c | 45 ++++---- libmemcached/auto.c | 8 +- libmemcached/behavior.c | 11 +- libmemcached/behavior.h | 3 + libmemcached/common.h | 23 ++-- libmemcached/connect.c | 2 +- libmemcached/delete.c | 8 +- libmemcached/do.c | 2 +- libmemcached/do.h | 6 +- libmemcached/dump.c | 2 +- libmemcached/fetch.c | 2 +- libmemcached/flush.c | 6 +- libmemcached/flush_buffers.c | 2 +- libmemcached/get.c | 20 ++-- libmemcached/hash.c | 50 +++++---- libmemcached/hash.h | 7 +- libmemcached/hosts.c | 24 ++--- libmemcached/io.c | 37 +++---- libmemcached/io.h | 16 +-- libmemcached/memcached.c | 21 +++- libmemcached/memcached.h | 16 +-- libmemcached/memcached.hpp | 56 +++++----- libmemcached/purge.c | 2 +- libmemcached/quit.c | 2 +- libmemcached/quit.h | 3 + libmemcached/response.c | 14 +-- libmemcached/response.h | 4 +- libmemcached/server.c | 106 ++++++++++++------- libmemcached/server.h | 53 +++++----- libmemcached/server_list.c | 6 +- libmemcached/stats.c | 8 +- libmemcached/storage.c | 12 +-- libmemcached/string.c | 5 + libmemcached/string.h | 3 +- libmemcached/types.h | 4 +- libmemcached/verbosity.c | 2 +- libmemcached/version.c | 6 +- tests/libmemcached_world.h | 4 +- tests/mem_functions.c | 200 ++++++++++++++++++++++------------- tests/server.c | 2 +- 41 files changed, 467 insertions(+), 338 deletions(-) diff --git a/ChangeLog b/ChangeLog index 32427630..84f2aee1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ 0.39 * Modify inline calls for compatibility. +* The undocumented call memcached_server_by_key() now returns const. + 0.38 Wed Feb 10 09:40:33 PST 2010 * C++ interface for libhashkit. * Modified memcached_set_memory_allocators() so that it requires a context pointer. diff --git a/clients/memstat.c b/clients/memstat.c index 42503d55..ceaba36f 100644 --- a/clients/memstat.c +++ b/clients/memstat.c @@ -134,7 +134,7 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, else if (strcmp(analyze_mode, "latency") == 0) { memcached_st **servers; - uint32_t x, y, flags, server_count= memcached_server_count(memc); + uint32_t flags, server_count= memcached_server_count(memc); uint32_t num_of_tests= 32; const char *test_key= "libmemcached_test_key"; @@ -145,7 +145,7 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, return; } - for (x= 0; x < server_count; x++) + for (uint32_t x= 0; x < server_count; x++) { if((servers[x]= memcached_create(NULL)) == NULL) { @@ -160,18 +160,18 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, return; } memcached_server_add(servers[x], - memcached_server_name(memc, server_list[x]), - memcached_server_port(memc, server_list[x])); + memcached_server_name(&server_list[x]), + memcached_server_port(&server_list[x])); } printf("Network Latency Test:\n\n"); struct timeval start_time, end_time; long elapsed_time, slowest_time= 0, slowest_server= 0; - for (x= 0; x < server_count; x++) + for (uint32_t x= 0; x < server_count; x++) { gettimeofday(&start_time, NULL); - for (y= 0; y < num_of_tests; y++) + for (uint32_t y= 0; y < num_of_tests; y++) { size_t vlen; char *val= memcached_get(servers[x], test_key, strlen(test_key), @@ -194,14 +194,14 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS) { printf("\t %s (%d) => failed to reach the server\n", - memcached_server_name(memc, server_list[x]), - memcached_server_port(memc, server_list[x])); + memcached_server_name(&server_list[x]), + memcached_server_port(&server_list[x])); } else { printf("\t %s (%d) => %ld.%ld seconds\n", - memcached_server_name(memc, server_list[x]), - memcached_server_port(memc, server_list[x]), + memcached_server_name(&server_list[x]), + memcached_server_port(&server_list[x]), elapsed_time / 1000, elapsed_time % 1000); } } @@ -210,13 +210,13 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, { printf("---\n"); printf("Slowest Server: %s (%d) => %ld.%ld seconds\n", - memcached_server_name(memc, server_list[slowest_server]), - memcached_server_port(memc, server_list[slowest_server]), + memcached_server_name(&server_list[slowest_server]), + memcached_server_port(&server_list[slowest_server]), slowest_time / 1000, slowest_time % 1000); } printf("\n"); - for (x= 0; x < server_count; x++) + for (uint32_t x= 0; x < server_count; x++) memcached_free(servers[x]); free(servers); @@ -232,19 +232,18 @@ static void run_analyzer(memcached_st *memc, memcached_stat_st *memc_stat, static void print_server_listing(memcached_st *memc, memcached_stat_st *memc_stat, memcached_server_st *server_list) { - unsigned int x; memcached_return_t rc; printf("Listing %u Server\n\n", memcached_server_count(memc)); - for (x= 0; x < memcached_server_count(memc); x++) + for (uint32_t x= 0; x < memcached_server_count(memc); x++) { char **list; char **ptr; list= memcached_stat_get_keys(memc, &memc_stat[x], &rc); - printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]), - (uint32_t)memcached_server_port(memc, server_list[x])); + printf("Server: %s (%u)\n", memcached_server_name(&server_list[x]), + (uint32_t)memcached_server_port(&server_list[x])); for (ptr= list; *ptr; ptr++) { char *value= memcached_stat_get_value(memc, &memc_stat[x], *ptr, &rc); @@ -278,16 +277,16 @@ static void print_analysis_report(memcached_st *memc, printf("\n"); printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n", - memcached_server_name(memc, server_list[report->most_consumed_server]), - (uint32_t)memcached_server_port(memc, server_list[report->most_consumed_server]), + memcached_server_name(&server_list[report->most_consumed_server]), + (uint32_t)memcached_server_port(&server_list[report->most_consumed_server]), (unsigned long long)report->most_used_bytes); printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n", - memcached_server_name(memc, server_list[report->least_free_server]), - (uint32_t)memcached_server_port(memc, server_list[report->least_free_server]), + memcached_server_name(&server_list[report->least_free_server]), + (uint32_t)memcached_server_port(&server_list[report->least_free_server]), (unsigned long long)report->least_remaining_bytes); printf("\tNode with longest uptime : %s:%u (%us)\n", - memcached_server_name(memc, server_list[report->oldest_server]), - (uint32_t)memcached_server_port(memc, server_list[report->oldest_server]), + memcached_server_name(&server_list[report->oldest_server]), + (uint32_t)memcached_server_port(&server_list[report->oldest_server]), report->longest_uptime); printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio); printf("\n"); diff --git a/libmemcached/auto.c b/libmemcached/auto.c index 608324df..c36cd92f 100644 --- a/libmemcached/auto.c +++ b/libmemcached/auto.c @@ -22,7 +22,7 @@ static memcached_return_t text_incr_decr(memcached_st *ptr, memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; uint32_t server_key; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; bool no_reply= ptr->flags.no_reply; unlikely (memcached_server_count(ptr) == 0) @@ -31,7 +31,7 @@ static memcached_return_t text_incr_decr(memcached_st *ptr, if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; - server_key= memcached_generate_hash(ptr, master_key, master_key_length); + server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length); instance= memcached_server_instance_fetch(ptr, server_key); send_length= (size_t)snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, @@ -83,13 +83,13 @@ static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd, uint64_t *value) { uint32_t server_key; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; bool no_reply= ptr->flags.no_reply; unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; - server_key= memcached_generate_hash(ptr, master_key, master_key_length); + server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length); instance= memcached_server_instance_fetch(ptr, server_key); if (no_reply) diff --git a/libmemcached/behavior.c b/libmemcached/behavior.c index ba4db737..886457a5 100644 --- a/libmemcached/behavior.c +++ b/libmemcached/behavior.c @@ -170,7 +170,7 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, break; case MEMCACHED_BEHAVIOR_CORK: { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; bool action= set_flag(data); if (action == false) @@ -230,6 +230,11 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, return MEMCACHED_SUCCESS; } +inline bool _is_auto_eject_host(const memcached_st *ptr) +{ + return ptr->flags.auto_eject_hosts; +} + uint64_t memcached_behavior_get(memcached_st *ptr, const memcached_behavior_t flag) { @@ -287,7 +292,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr, { int sock_size= 0; socklen_t sock_length= sizeof(int); - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; if (ptr->send_size != -1) // If value is -1 then we are using the default return (uint64_t) ptr->send_size; @@ -312,7 +317,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr, { int sock_size= 0; socklen_t sock_length= sizeof(int); - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; if (ptr->recv_size != -1) // If value is -1 then we are using the default return (uint64_t) ptr->recv_size; diff --git a/libmemcached/behavior.h b/libmemcached/behavior.h index 1bc51b7d..39a430d7 100644 --- a/libmemcached/behavior.h +++ b/libmemcached/behavior.h @@ -40,6 +40,9 @@ memcached_return_t memcached_behavior_set_distribution_hash(memcached_st *ptr, m LIBMEMCACHED_API memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr); +LIBMEMCACHED_LOCAL +bool _is_auto_eject_host(const memcached_st *ptr); + #ifdef __cplusplus } diff --git a/libmemcached/common.h b/libmemcached/common.h index 3391c229..67d7cb05 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -54,7 +54,10 @@ #include "libmemcached/memcached.h" #include "libmemcached/watchpoint.h" -typedef struct memcached_server_st memcached_server_instance_st; +typedef struct memcached_server_st * memcached_server_write_instance_st; + +LIBMEMCACHED_LOCAL +memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key); /* These are private not to be installed headers */ #include "libmemcached/do.h" @@ -101,10 +104,7 @@ extern "C" { #endif LIBMEMCACHED_LOCAL -memcached_return_t memcached_connect(memcached_server_instance_st *ptr); - -LIBMEMCACHED_LOCAL -void memcached_quit_server(memcached_server_instance_st *ptr, bool io_death); +memcached_return_t memcached_connect(memcached_server_write_instance_st ptr); LIBMEMCACHED_LOCAL memcached_return_t run_distribution(memcached_st *ptr); @@ -128,19 +128,22 @@ LIBMEMCACHED_LOCAL memcached_return_t value_fetch(memcached_server_instance_st *ptr, char *buffer, memcached_result_st *result); -LIBMEMCACHED_LOCAL -void server_list_free(memcached_st *ptr, memcached_server_instance_st *servers); - LIBMEMCACHED_LOCAL memcached_return_t memcached_key_test(const char * const *keys, const size_t *key_length, size_t number_of_keys); LIBMEMCACHED_LOCAL -uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length); +memcached_return_t memcached_purge(memcached_server_write_instance_st ptr); LIBMEMCACHED_LOCAL -memcached_return_t memcached_purge(memcached_server_instance_st *ptr); +memcached_server_st *memcached_server_create_with(const memcached_st *memc, + memcached_server_write_instance_st host, + const char *hostname, + in_port_t port, + uint32_t weight, + memcached_connection_t type); + static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary) { diff --git a/libmemcached/connect.c b/libmemcached/connect.c index b599977f..4b8939c1 100644 --- a/libmemcached/connect.c +++ b/libmemcached/connect.c @@ -313,7 +313,7 @@ static memcached_return_t network_connect(memcached_server_st *ptr) } -memcached_return_t memcached_connect(memcached_server_st *ptr) +memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) { memcached_return_t rc= MEMCACHED_NO_SERVERS; LIBMEMCACHED_MEMCACHED_CONNECT_START(); diff --git a/libmemcached/delete.c b/libmemcached/delete.c index 75a44ed8..7fd1f2aa 100644 --- a/libmemcached/delete.c +++ b/libmemcached/delete.c @@ -24,7 +24,7 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr, memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; uint32_t server_key; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; LIBMEMCACHED_MEMCACHED_DELETE_START(); @@ -36,7 +36,7 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr, unlikely (memcached_server_count(ptr) == 0) return MEMCACHED_NO_SERVERS; - server_key= memcached_generate_hash(ptr, master_key, master_key_length); + server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length); instance= memcached_server_instance_fetch(ptr, server_key); to_write= (ptr->flags.buffer_requests) ? false : true; @@ -148,7 +148,7 @@ static inline memcached_return_t binary_delete(memcached_st *ptr, size_t key_length, bool flush) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; protocol_binary_request_delete request= {.bytes= {0}}; instance= memcached_server_instance_fetch(ptr, server_key); @@ -188,7 +188,7 @@ static inline memcached_return_t binary_delete(memcached_st *ptr, for (uint32_t x= 0; x < ptr->number_of_replicas; ++x) { - memcached_server_instance_st *replica; + memcached_server_write_instance_st replica; ++server_key; if (server_key == memcached_server_count(ptr)) diff --git a/libmemcached/do.c b/libmemcached/do.c index d5a75484..2fd1f327 100644 --- a/libmemcached/do.c +++ b/libmemcached/do.c @@ -11,7 +11,7 @@ #include "common.h" -memcached_return_t memcached_do(memcached_server_st *ptr, const void *command, +memcached_return_t memcached_do(memcached_server_write_instance_st ptr, const void *command, size_t command_length, bool with_flush) { memcached_return_t rc; diff --git a/libmemcached/do.h b/libmemcached/do.h index d62cd489..8f95c8b5 100644 --- a/libmemcached/do.h +++ b/libmemcached/do.h @@ -17,8 +17,10 @@ extern "C" { #endif LIBMEMCACHED_LOCAL -memcached_return_t memcached_do(memcached_server_instance_st *ptr, const void *commmand, - size_t command_length, bool with_flush); +memcached_return_t memcached_do(memcached_server_write_instance_st ptr, + const void *commmand, + size_t command_length, + bool with_flush); #ifdef __cplusplus } diff --git a/libmemcached/dump.c b/libmemcached/dump.c index 8d9c525f..a0d62df0 100644 --- a/libmemcached/dump.c +++ b/libmemcached/dump.c @@ -20,7 +20,7 @@ static memcached_return_t ascii_dump(memcached_st *ptr, memcached_dump_fn *callb for (server_key= 0; server_key < memcached_server_count(ptr); server_key++) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; instance= memcached_server_instance_fetch(ptr, server_key); /* 256 I BELIEVE is the upper limit of slabs */ diff --git a/libmemcached/fetch.c b/libmemcached/fetch.c index d76f19b2..44f64f33 100644 --- a/libmemcached/fetch.c +++ b/libmemcached/fetch.c @@ -61,7 +61,7 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr, if ((result= memcached_result_create(ptr, NULL)) == NULL) return NULL; - while ((server = memcached_io_get_readable_server(ptr)) != NULL) + while ((server= memcached_io_get_readable_server(ptr)) != NULL) { char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; *error= memcached_response(server, buffer, sizeof(buffer), result); diff --git a/libmemcached/flush.c b/libmemcached/flush.c index 8e1361a8..c8428f70 100644 --- a/libmemcached/flush.c +++ b/libmemcached/flush.c @@ -32,7 +32,7 @@ static memcached_return_t memcached_flush_textual(memcached_st *ptr, for (x= 0; x < memcached_server_count(ptr); x++) { bool no_reply= ptr->flags.no_reply; - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (expiration) @@ -70,7 +70,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (ptr->flags.no_reply) @@ -92,7 +92,7 @@ static memcached_return_t memcached_flush_binary(memcached_st *ptr, for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance) > 0) diff --git a/libmemcached/flush_buffers.c b/libmemcached/flush_buffers.c index cf391440..0a902757 100644 --- a/libmemcached/flush_buffers.c +++ b/libmemcached/flush_buffers.c @@ -6,7 +6,7 @@ memcached_return_t memcached_flush_buffers(memcached_st *memc) for (uint32_t x= 0; x < memcached_server_count(memc); ++x) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(memc, x); if (instance->write_buffer_offset != 0) diff --git a/libmemcached/get.c b/libmemcached/get.c index ba4d4bc3..1df6247c 100644 --- a/libmemcached/get.c +++ b/libmemcached/get.c @@ -169,7 +169,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, { if (ptr->flags.verify_key && (memcached_key_test((const char * const *)&master_key, &master_key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) return MEMCACHED_BAD_KEY_PROVIDED; - master_server_key= memcached_generate_hash(ptr, master_key, master_key_length); + master_server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length); is_master_key_set= true; } @@ -181,7 +181,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, */ for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance)) @@ -212,7 +212,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, */ for (x= 0; x < number_of_keys; x++) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; uint32_t server_key; if (is_master_key_set) @@ -221,7 +221,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, } else { - server_key= memcached_generate_hash(ptr, keys[x], key_length[x]); + server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } instance= memcached_server_instance_fetch(ptr, server_key); @@ -274,7 +274,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, */ for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance)) @@ -362,7 +362,7 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, for (x= 0; x < number_of_keys; x++) { uint32_t server_key; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; if (is_master_key_set) { @@ -370,7 +370,7 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, } else { - server_key= memcached_generate_hash(ptr, keys[x], key_length[x]); + server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } instance= memcached_server_instance_fetch(ptr, server_key); @@ -436,7 +436,7 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance)) @@ -484,7 +484,7 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, for (x= 0; x < number_of_keys; ++x) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; if (hash[x] == memcached_server_count(ptr)) continue; /* Already successfully sent */ @@ -598,7 +598,7 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, { for (size_t x= 0; x < number_of_keys; x++) { - hash[x]= memcached_generate_hash(ptr, keys[x], key_length[x]); + hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } } diff --git a/libmemcached/hash.c b/libmemcached/hash.c index 1c11c50d..b418d2e1 100644 --- a/libmemcached/hash.c +++ b/libmemcached/hash.c @@ -17,22 +17,12 @@ uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memca return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm); } -uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length) +static inline uint32_t generate_hash(const memcached_st *ptr, const char *key, size_t key_length) { - uint32_t hash= 1; /* Just here to remove compile warning */ - - - WATCHPOINT_ASSERT(memcached_server_count(ptr)); - - if (memcached_server_count(ptr) == 1) - return 0; - - hash= hashkit_digest(&ptr->hashkit, key, key_length); - - return hash; + return hashkit_digest(&ptr->hashkit, key, key_length); } -static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash) +static uint32_t dispatch_host(const memcached_st *ptr, uint32_t hash) { switch (ptr->distribution) { @@ -73,13 +63,10 @@ static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash) } /* - One day make this public, and have it return the actual memcached_server_st - to the calling application. + One version is public and will not modify the distribution hash, the other will. */ -uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length) +static inline uint32_t _generate_hash_wrapper(const memcached_st *ptr, const char *key, size_t key_length) { - uint32_t hash= 1; /* Just here to remove compile warning */ - WATCHPOINT_ASSERT(memcached_server_count(ptr)); if (memcached_server_count(ptr) == 1) @@ -95,14 +82,18 @@ uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_ strncpy(temp, ptr->prefix_key, ptr->prefix_key_length); strncpy(temp + ptr->prefix_key_length, key, key_length); - hash= generate_hash(ptr, temp, temp_length); + + return generate_hash(ptr, temp, temp_length); } else { - hash= generate_hash(ptr, key, key_length); + return generate_hash(ptr, key, key_length); } +} - if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild) +static inline void _regen_for_auto_eject(memcached_st *ptr) +{ + if (_is_auto_eject_host(ptr) && ptr->next_distribution_rebuild) { struct timeval now; @@ -112,10 +103,27 @@ uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_ run_distribution(ptr); } } +} + +void memcached_autoeject(memcached_st *ptr) +{ + _regen_for_auto_eject(ptr); +} + +uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length) +{ + uint32_t hash= _generate_hash_wrapper(ptr, key, key_length); + + _regen_for_auto_eject(ptr); return dispatch_host(ptr, hash); } +uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length) +{ + return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length)); +} + hashkit_st *memcached_get_hashkit(memcached_st *ptr) { return &ptr->hashkit; diff --git a/libmemcached/hash.h b/libmemcached/hash.h index 40f1f565..5f601229 100644 --- a/libmemcached/hash.h +++ b/libmemcached/hash.h @@ -24,8 +24,13 @@ LIBMEMCACHED_API hashkit_st *memcached_get_hashkit(memcached_st *ptr); LIBMEMCACHED_API -uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length); +uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length); +LIBMEMCACHED_LOCAL +uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length); + +LIBMEMCACHED_API +void memcached_autoeject(memcached_st *ptr); #ifdef __cplusplus } diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index b98a18e8..e907be4a 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -39,7 +39,7 @@ static void sort_hosts(memcached_st *ptr) { if (memcached_server_count(ptr)) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_instance_st), compare_servers); instance= memcached_server_instance_fetch(ptr, 0); @@ -106,7 +106,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) uint32_t host_index; uint32_t continuum_index= 0; uint32_t value; - memcached_server_instance_st *list; + memcached_server_st *list; uint32_t pointer_index; uint32_t pointer_counter= 0; uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER; @@ -127,7 +127,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) list = memcached_server_list(ptr); /* count live servers (those without a retry delay set) */ - is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS); + is_auto_ejecting= _is_auto_eject_host(ptr); if (is_auto_ejecting) { live_servers= 0; @@ -176,12 +176,12 @@ static memcached_return_t update_continuum(memcached_st *ptr) { list[host_index].weight = 1; } - if (!is_auto_ejecting || list[host_index].next_retry <= now.tv_sec) + if (! is_auto_ejecting || list[host_index].next_retry <= now.tv_sec) total_weight += list[host_index].weight; } } - for (host_index = 0; host_index < memcached_server_count(ptr); ++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; @@ -305,7 +305,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } -memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list) +memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_st *list) { uint32_t count; memcached_server_st *new_host_list; @@ -313,7 +313,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st if (! list) return MEMCACHED_SUCCESS; - count= memcached_servers_count(list); + count= memcached_server_list_count(list); new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr), sizeof(memcached_server_instance_st) * (count + memcached_server_count(ptr))); @@ -324,7 +324,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st for (uint32_t x= 0; x < count; x++) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP) || ((list[x].type == MEMCACHED_CONNECTION_UDP) @@ -343,7 +343,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st // Provides backwards compatibility with server list. { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; instance= memcached_server_instance_fetch(ptr, 0); instance->number_of_hosts= memcached_server_count(ptr); } @@ -414,15 +414,15 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, uint32_t weight, memcached_connection_t type) { - memcached_server_instance_st *new_host_list; - memcached_server_instance_st *instance; + memcached_server_st *new_host_list; + memcached_server_write_instance_st instance; if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP) || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) ) return MEMCACHED_INVALID_HOST_PROTOCOL; new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_instance_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; diff --git a/libmemcached/io.c b/libmemcached/io.c index ef46a0d9..f1afc669 100644 --- a/libmemcached/io.c +++ b/libmemcached/io.c @@ -19,10 +19,11 @@ typedef enum { MEM_WRITE } memc_read_or_write; -static ssize_t io_flush(memcached_server_instance_st *ptr, memcached_return_t *error); -static void increment_udp_message_id(memcached_server_instance_st *ptr); +static ssize_t io_flush(memcached_server_write_instance_st ptr, + memcached_return_t *error); +static void increment_udp_message_id(memcached_server_write_instance_st ptr); -static memcached_return_t io_wait(memcached_server_instance_st *ptr, +static memcached_return_t io_wait(memcached_server_write_instance_st ptr, memc_read_or_write read_or_write) { struct pollfd fds= { @@ -91,7 +92,7 @@ static memcached_return_t io_wait(memcached_server_instance_st *ptr, * * @param ptr the server to pack */ -static bool repack_input_buffer(memcached_server_instance_st *ptr) +static bool repack_input_buffer(memcached_server_write_instance_st ptr) { if (ptr->read_ptr != ptr->read_buffer) { @@ -131,7 +132,7 @@ static bool repack_input_buffer(memcached_server_instance_st *ptr) * @param ptr the server to star processing iput messages for * @return true if we processed anything, false otherwise */ -static bool process_input_buffer(memcached_server_instance_st *ptr) +static bool process_input_buffer(memcached_server_write_instance_st ptr) { /* ** We might be able to process some of the response messages if we @@ -234,7 +235,7 @@ void memcached_io_preread(memcached_st *ptr) } #endif -memcached_return_t memcached_io_read(memcached_server_instance_st *ptr, +memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, void *buffer, size_t length, ssize_t *nread) { char *buffer_ptr; @@ -325,7 +326,7 @@ memcached_return_t memcached_io_read(memcached_server_instance_st *ptr, return MEMCACHED_SUCCESS; } -ssize_t memcached_io_write(memcached_server_instance_st *ptr, +ssize_t memcached_io_write(memcached_server_write_instance_st ptr, const void *buffer, size_t length, bool with_flush) { size_t original_length; @@ -402,7 +403,7 @@ ssize_t memcached_io_write(memcached_server_instance_st *ptr, return (ssize_t) original_length; } -memcached_return_t memcached_io_close(memcached_server_instance_st *ptr) +memcached_return_t memcached_io_close(memcached_server_write_instance_st ptr) { if (ptr->fd == -1) { @@ -425,7 +426,7 @@ memcached_return_t memcached_io_close(memcached_server_instance_st *ptr) return MEMCACHED_SUCCESS; } -memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *memc) +memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc) { #define MAX_SERVERS_TO_POLL 100 struct pollfd fds[MAX_SERVERS_TO_POLL]; @@ -435,7 +436,7 @@ memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *mem x< memcached_server_count(memc) && host_index < MAX_SERVERS_TO_POLL; ++x) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(memc, x); if (instance->read_buffer_length > 0) /* I have data in the buffer */ @@ -455,7 +456,7 @@ memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *mem /* We have 0 or 1 server with pending events.. */ for (uint32_t x= 0; x< memcached_server_count(memc); ++x) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(memc, x); if (memcached_server_response_count(instance) > 0) @@ -481,7 +482,7 @@ memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *mem { for (uint32_t y= 0; y < memcached_server_count(memc); ++y) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(memc, y); if (instance->fd == fds[x].fd) @@ -494,7 +495,7 @@ memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *mem return NULL; } -static ssize_t io_flush(memcached_server_instance_st *ptr, +static ssize_t io_flush(memcached_server_write_instance_st ptr, memcached_return_t *error) { /* @@ -610,7 +611,7 @@ static ssize_t io_flush(memcached_server_instance_st *ptr, /* Eventually we will just kill off the server with the problem. */ -void memcached_io_reset(memcached_server_instance_st *ptr) +void memcached_io_reset(memcached_server_write_instance_st ptr) { memcached_quit_server(ptr, true); } @@ -619,7 +620,7 @@ void memcached_io_reset(memcached_server_instance_st *ptr) * Read a given number of bytes from the server and place it into a specific * buffer. Reset the IO channel on this server if an error occurs. */ -memcached_return_t memcached_safe_read(memcached_server_instance_st *ptr, +memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, void *dta, size_t size) { @@ -640,7 +641,7 @@ memcached_return_t memcached_safe_read(memcached_server_instance_st *ptr, return MEMCACHED_SUCCESS; } -memcached_return_t memcached_io_readline(memcached_server_instance_st *ptr, +memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, char *buffer_ptr, size_t size) { @@ -698,7 +699,7 @@ memcached_return_t memcached_io_readline(memcached_server_instance_st *ptr, * extracts the message number from message_id, increments it and then * writes the new value back into the header */ -static void increment_udp_message_id(memcached_server_instance_st *ptr) +static void increment_udp_message_id(memcached_server_write_instance_st ptr) { struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer; uint16_t cur_req= get_udp_datagram_request_id(header); @@ -711,7 +712,7 @@ static void increment_udp_message_id(memcached_server_instance_st *ptr) header->request_id= htons((uint16_t) (thread_id | msg_num)); } -memcached_return_t memcached_io_init_udp_header(memcached_server_instance_st *ptr, uint16_t thread_id) +memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, uint16_t thread_id) { if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID) return MEMCACHED_FAILURE; diff --git a/libmemcached/io.h b/libmemcached/io.h index ca22c27d..dc6a672f 100644 --- a/libmemcached/io.h +++ b/libmemcached/io.h @@ -41,37 +41,37 @@ struct udp_datagram_header_st }; LIBMEMCACHED_LOCAL -ssize_t memcached_io_write(memcached_server_instance_st *ptr, +ssize_t memcached_io_write(memcached_server_write_instance_st ptr, const void *buffer, size_t length, bool with_flush); LIBMEMCACHED_LOCAL -void memcached_io_reset(memcached_server_instance_st *ptr); +void memcached_io_reset(memcached_server_write_instance_st ptr); LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_read(memcached_server_instance_st *ptr, +memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr, void *buffer, size_t length, ssize_t *nread); /* Read a line (terminated by '\n') into the buffer */ LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_readline(memcached_server_instance_st *ptr, +memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, char *buffer_ptr, size_t size); LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_close(memcached_server_instance_st *ptr); +memcached_return_t memcached_io_close(memcached_server_write_instance_st ptr); /* Read n bytes of data from the server and store them in dta */ LIBMEMCACHED_LOCAL -memcached_return_t memcached_safe_read(memcached_server_instance_st *ptr, +memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, void *dta, size_t size); LIBMEMCACHED_LOCAL -memcached_return_t memcached_io_init_udp_header(memcached_server_instance_st *ptr, +memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, uint16_t thread_id); LIBMEMCACHED_LOCAL -memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *memc); +memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc); #ifdef __cplusplus } diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index c0fee34d..f7946241 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -139,7 +139,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers) if (servers == NULL) return; - for (x= 0; x < memcached_servers_count(servers); x++) + for (x= 0; x < memcached_server_list_count(servers); x++) { if (servers[x].address_info) { @@ -252,8 +252,8 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) new_clone->io_key_prefetch= source->io_key_prefetch; new_clone->number_of_replicas= source->number_of_replicas; - if (memcached_server_list(source)) - rc= memcached_server_push(new_clone, memcached_server_list(source)); + if (memcached_server_count(source)) + rc= memcached_push(new_clone, source); if (rc != MEMCACHED_SUCCESS) { @@ -296,3 +296,18 @@ void *memcached_set_user_data(memcached_st *ptr, void *data) return ret; } + +memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source) +{ + return memcached_server_push(destination, source->servers); +} + +inline memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key) +{ + return &ptr->servers[server_key]; +} + +inline memcached_server_instance_st *memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key) +{ + return &ptr->servers[server_key]; +} diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 9415f432..2ee25812 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -149,17 +149,21 @@ void *memcached_get_user_data(const memcached_st *ptr); LIBMEMCACHED_API void *memcached_set_user_data(memcached_st *ptr, void *data); +LIBMEMCACHED_API +memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source); + +LIBMEMCACHED_API +memcached_server_instance_st *memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key); + +LIBMEMCACHED_LOCAL +void server_list_free(memcached_st *ptr, memcached_server_st *servers); + + #ifdef __cplusplus } // extern "C" #endif -// Local Only Inline -static inline memcached_server_st *memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key) -{ - return &ptr->servers[server_key]; -} - #ifdef __cplusplus class Memcached : private memcached_st { public: diff --git a/libmemcached/memcached.hpp b/libmemcached/memcached.hpp index 17c306e9..39ca36d8 100644 --- a/libmemcached/memcached.hpp +++ b/libmemcached/memcached.hpp @@ -40,7 +40,6 @@ public: : servers_list(), memc(), - servers(NULL), result() { memcached_create(&memc); @@ -50,12 +49,10 @@ public: : servers_list(in_servers_list), memc(), - servers(NULL), result() { memcached_create(&memc); - servers= memcached_servers_parse(servers_list.c_str()); - memcached_server_push(&memc, servers); + init(); } Memcache(const std::string &hostname, @@ -63,24 +60,23 @@ public: : servers_list(), memc(), - servers(NULL), result() { memcached_create(&memc); + servers_list.append(hostname); servers_list.append(":"); std::ostringstream strsmt; strsmt << port; servers_list.append(strsmt.str()); - servers= memcached_servers_parse(servers_list.c_str()); - memcached_server_push(&memc, servers); + + init(); } Memcache(memcached_st *clone) : servers_list(), memc(), - servers(NULL), result() { memcached_clone(&memc, clone); @@ -90,12 +86,10 @@ public: : servers_list(rhs.servers_list), memc(), - servers(NULL), result() { memcached_clone(&memc, const_cast(&rhs.getImpl())); - servers= memcached_servers_parse(servers_list.c_str()); - memcached_server_push(&memc, servers); + init(); } Memcache &operator=(const Memcache &rhs) @@ -103,16 +97,23 @@ public: if (this != &rhs) { memcached_clone(&memc, const_cast(&rhs.getImpl())); - servers= memcached_servers_parse(servers_list.c_str()); - memcached_server_push(&memc, servers); + init(); } + return *this; } ~Memcache() { memcached_free(&memc); - memcached_server_list_free(servers); + } + + void init() + { + memcached_server_st *servers; + servers= memcached_servers_parse(servers_list.c_str()); + memcached_server_push(&memc, servers); + memcached_server_free(servers); } /** @@ -175,9 +176,9 @@ public: bool setServers(const std::string &in_servers_list) { servers_list.assign(in_servers_list); - servers= memcached_servers_parse(in_servers_list.c_str()); - memcached_server_push(&memc, servers); - return (servers == NULL); + init(); + + return (memcached_server_count(&memc)); } /** @@ -190,17 +191,9 @@ public: bool addServer(const std::string &server_name, in_port_t port) { memcached_return_t rc; - std::ostringstream strstm; - servers_list.append(","); - servers_list.append(server_name); - servers_list.append(":"); - strstm << port; - servers_list.append(strstm.str()); - servers= memcached_server_list_append(servers, - server_name.c_str(), - port, - &rc); - memcached_server_push(&memc, servers); + + rc= memcached_server_add(&memc, server_name.c_str(), port); + return (rc == MEMCACHED_SUCCESS); } @@ -967,10 +960,12 @@ public: */ for (uint32_t x= 0; x < server_count; x++) { + const memcached_server_instance_st *instance= + memcached_server_instance_by_position(&memc, x); std::ostringstream strstm; - std::string server_name(memcached_server_name(&memc, servers[x])); + std::string server_name(memcached_server_name(instance)); server_name.append(":"); - strstm << memcached_server_port(&memc, servers[x]); + strstm << memcached_server_port(instance); server_name.append(strstm.str()); std::map server_stats; @@ -997,7 +992,6 @@ private: std::string servers_list; memcached_st memc; - memcached_server_st *servers; memcached_result_st result; }; diff --git a/libmemcached/purge.c b/libmemcached/purge.c index bee5c921..07cd135f 100644 --- a/libmemcached/purge.c +++ b/libmemcached/purge.c @@ -1,6 +1,6 @@ #include "common.h" -memcached_return_t memcached_purge(memcached_server_instance_st *ptr) +memcached_return_t memcached_purge(memcached_server_write_instance_st ptr) { uint32_t x; memcached_return_t ret= MEMCACHED_SUCCESS; diff --git a/libmemcached/quit.c b/libmemcached/quit.c index 292bc6e4..35d434bc 100644 --- a/libmemcached/quit.c +++ b/libmemcached/quit.c @@ -81,7 +81,7 @@ void memcached_quit(memcached_st *ptr) { for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); memcached_quit_server(instance, false); diff --git a/libmemcached/quit.h b/libmemcached/quit.h index 453adeaa..e252a1ea 100644 --- a/libmemcached/quit.h +++ b/libmemcached/quit.h @@ -19,6 +19,9 @@ extern "C" { LIBMEMCACHED_API void memcached_quit(memcached_st *ptr); +LIBMEMCACHED_LOCAL +void memcached_quit_server(memcached_server_st *ptr, bool io_death); + #ifdef __cplusplus } #endif diff --git a/libmemcached/response.c b/libmemcached/response.c index abd92e4e..c14dd1ff 100644 --- a/libmemcached/response.c +++ b/libmemcached/response.c @@ -11,14 +11,14 @@ #include "common.h" -static memcached_return_t textual_read_one_response(memcached_server_instance_st *ptr, +static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result); -static memcached_return_t binary_read_one_response(memcached_server_instance_st *ptr, +static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result); -memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr, +memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result) { @@ -45,7 +45,7 @@ memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr return rc; } -memcached_return_t memcached_response(memcached_server_instance_st *ptr, +memcached_return_t memcached_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result) { @@ -79,7 +79,7 @@ memcached_return_t memcached_response(memcached_server_instance_st *ptr, return memcached_read_one_response(ptr, buffer, buffer_length, result); } -static memcached_return_t textual_value_fetch(memcached_server_instance_st *ptr, +static memcached_return_t textual_value_fetch(memcached_server_write_instance_st ptr, char *buffer, memcached_result_st *result) { @@ -210,7 +210,7 @@ read_error: return MEMCACHED_PARTIAL_READ; } -static memcached_return_t textual_read_one_response(memcached_server_instance_st *ptr, +static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result) { @@ -327,7 +327,7 @@ static memcached_return_t textual_read_one_response(memcached_server_instance_st /* NOTREACHED */ } -static memcached_return_t binary_read_one_response(memcached_server_instance_st *ptr, +static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result) { diff --git a/libmemcached/response.h b/libmemcached/response.h index 6599b8a7..da6f2b42 100644 --- a/libmemcached/response.h +++ b/libmemcached/response.h @@ -18,12 +18,12 @@ extern "C" { /* Read a single response from the server */ LIBMEMCACHED_LOCAL -memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr, +memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result); LIBMEMCACHED_LOCAL -memcached_return_t memcached_response(memcached_server_instance_st *ptr, +memcached_return_t memcached_response(memcached_server_write_instance_st ptr, char *buffer, size_t buffer_length, memcached_result_st *result); diff --git a/libmemcached/server.c b/libmemcached/server.c index e7c54b98..13216bcf 100644 --- a/libmemcached/server.c +++ b/libmemcached/server.c @@ -78,7 +78,8 @@ static memcached_server_st *_server_create(memcached_server_st *self, const memc return self; } -memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *self, +memcached_server_st *memcached_server_create_with(const memcached_st *memc, + memcached_server_write_instance_st self, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type) { @@ -144,24 +145,21 @@ memcached_server_st *memcached_server_clone(memcached_server_st *destination, } -memcached_return_t memcached_server_cursor(memcached_st *ptr, - memcached_server_fn *callback, +memcached_return_t memcached_server_cursor(const memcached_st *ptr, + const memcached_server_fn *callback, void *context, uint32_t number_of_callbacks) { - uint32_t y; - - for (y= 0; y < memcached_server_count(ptr); y++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { - uint32_t x; - memcached_server_instance_st *instance= - memcached_server_instance_fetch(ptr, y); + const memcached_server_instance_st *instance= + memcached_server_instance_by_position(ptr, x); - for (x= 0; x < number_of_callbacks; x++) + for (uint32_t y= 0; y < number_of_callbacks; y++) { unsigned int iferror; - iferror= (*callback[x])(ptr, instance, context); + iferror= (*callback[y])(ptr, instance, context); if (iferror) continue; @@ -171,10 +169,13 @@ memcached_return_t memcached_server_cursor(memcached_st *ptr, return MEMCACHED_SUCCESS; } -memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return_t *error) +const memcached_server_st *memcached_server_by_key(const memcached_st *ptr, + const char *key, + size_t key_length, + memcached_return_t *error) { uint32_t server_key; - memcached_server_instance_st *instance; + const memcached_server_instance_st *instance; *error= memcached_validate_key_length(key_length, ptr->flags.binary_protocol); @@ -194,15 +195,15 @@ memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key } server_key= memcached_generate_hash(ptr, key, key_length); - instance= memcached_server_instance_fetch(ptr, server_key); + instance= memcached_server_instance_by_position(ptr, server_key); - return memcached_server_clone(NULL, instance); + return instance; } -const char *memcached_server_error(memcached_server_st *ptr) +const char *memcached_server_error(const memcached_server_st *ptr) { - return ptr + return ptr ? ptr->cached_server_error : NULL; } @@ -217,25 +218,25 @@ memcached_server_st *memcached_server_get_last_disconnect(memcached_st *ptr) return ptr->last_disconnected_server; } -uint32_t memcached_server_list_count(memcached_server_st *ptr) +inline uint32_t memcached_server_list_count(const memcached_server_st *self) { - return (ptr == NULL) + return (self == NULL) ? 0 - : memcached_servers_count(ptr); + : self->number_of_hosts; } -void memcached_server_list_free(memcached_server_st *ptr) +void memcached_server_list_free(memcached_server_st *self) { - server_list_free(NULL, ptr); + server_list_free(NULL, self); } /** @todo allow lists to query themselves even if they lack a root */ -memcached_return_t memcached_server_remove(memcached_server_st *st_ptr) +memcached_return_t memcached_server_remove(memcached_server_st *self) { - uint32_t x, host_index; - memcached_st *root= (memcached_st *)st_ptr->root; + uint32_t host_index= 0; + memcached_st *root= (memcached_st *)self->root; memcached_server_st *list; if (root == NULL) @@ -243,35 +244,66 @@ memcached_return_t memcached_server_remove(memcached_server_st *st_ptr) list= memcached_server_list(root); - for (x= 0, host_index= 0; x < memcached_server_count(root); x++) + /* Until we devise a way to mark servers that are about to be harvested, we need to shutdown before doing the clone. */ + memcached_quit(root); + + for (uint32_t x= 0; x < memcached_server_count(root); x++) { - if (strncmp(list[x].hostname, st_ptr->hostname, NI_MAXHOST) != 0 || list[x].port != st_ptr->port) + if (strncmp(list[x].hostname, self->hostname, NI_MAXHOST) != 0 || list[x].port != self->port) { if (host_index != x) - memcpy(list+host_index, list+x, sizeof(memcached_server_st)); + { + memcached_server_st *check= + memcached_server_clone(list+host_index, list+x); + + if (! check) // Now we are in trouble, allocation didn't happen and we are midway through an operation. Bail! + { + return MEMCACHED_FAILURE; + } + memcached_server_free(list+x); + } host_index++; } } root->number_of_hosts= host_index; - if (st_ptr->address_info) - { - freeaddrinfo(st_ptr->address_info); - st_ptr->address_info= NULL; - } run_distribution(root); return MEMCACHED_SUCCESS; } -inline uint32_t memcached_servers_count(memcached_server_st *servers) +inline uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count) { - return servers->number_of_hosts; + return servers->number_of_hosts= count; } +inline uint32_t memcached_server_count(const memcached_st *self) +{ + return self->number_of_hosts; +} -inline uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count) +inline const char *memcached_server_name(const memcached_server_st *self) { - return servers->number_of_hosts= count; + return self->hostname; +} + +inline in_port_t memcached_server_port(const memcached_server_st *self) +{ + return self->port; +} + +inline memcached_server_st *memcached_server_list(memcached_st *self) +{ + return self->servers; +} + +inline void memcached_server_list_set(memcached_st *self, memcached_server_st *list) +{ + self->servers= list; +} + +inline uint32_t memcached_server_response_count(const memcached_server_st *self) +{ + return self->cursor_active; } diff --git a/libmemcached/server.h b/libmemcached/server.h index 9e468038..8a9b7a36 100644 --- a/libmemcached/server.h +++ b/libmemcached/server.h @@ -55,35 +55,23 @@ extern "C" { #endif LIBMEMCACHED_API -memcached_return_t memcached_server_cursor(memcached_st *ptr, - memcached_server_fn *callback, +memcached_return_t memcached_server_cursor(const memcached_st *ptr, + const memcached_server_fn *callback, void *context, uint32_t number_of_callbacks); LIBMEMCACHED_API -memcached_server_st *memcached_server_by_key(memcached_st *ptr, - const char *key, - size_t key_length, - memcached_return_t *error); + const memcached_server_st *memcached_server_by_key(const memcached_st *ptr, + const char *key, + size_t key_length, + memcached_return_t *error); LIBMEMCACHED_API -const char *memcached_server_error(memcached_server_st *ptr); +const char *memcached_server_error(const memcached_server_st *ptr); LIBMEMCACHED_API void memcached_server_error_reset(memcached_server_st *ptr); -/* These should not currently be used by end users */ -/* TODO: Is the above comment valid? If so, how can we unit test these if they - * aren't exported. If not, we should remove the comment */ - -LIBMEMCACHED_LOCAL -memcached_server_st *memcached_server_create_with(const memcached_st *memc, - memcached_server_st *host, - const char *hostname, - in_port_t port, - uint32_t weight, - memcached_connection_t type); - LIBMEMCACHED_API void memcached_server_free(memcached_server_st *ptr); @@ -129,7 +117,7 @@ void memcached_server_list_free(memcached_server_st *ptr); LIBMEMCACHED_API -memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list); +memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_st *list); LIBMEMCACHED_API memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, @@ -143,21 +131,30 @@ memcached_server_st *memcached_server_list_append_with_weight(memcached_server_s uint32_t weight, memcached_return_t *error); LIBMEMCACHED_API -unsigned int memcached_server_list_count(memcached_server_st *ptr); +uint32_t memcached_server_list_count(const memcached_server_st *ptr); LIBMEMCACHED_API -uint32_t memcached_servers_count(memcached_server_st *servers); +uint32_t memcached_server_count(const memcached_st *); + LIBMEMCACHED_LOCAL uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count); +LIBMEMCACHED_API +memcached_server_st *memcached_server_list(memcached_st *); + +LIBMEMCACHED_LOCAL +void memcached_server_list_set(memcached_st *self, memcached_server_st *list); + +LIBMEMCACHED_API +uint32_t memcached_server_response_count(const memcached_server_st *self); + +LIBMEMCACHED_API +const char *memcached_server_name(const memcached_server_st *self); + +LIBMEMCACHED_API +in_port_t memcached_server_port(const memcached_server_st *self); -#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)->servers -#define memcached_server_list_set(A,B) (A)->servers=(B) -#define memcached_server_response_count(A) (A)->cursor_active #ifdef __cplusplus } // extern "C" diff --git a/libmemcached/server_list.c b/libmemcached/server_list.c index f3750ea1..c8f18368 100644 --- a/libmemcached/server_list.c +++ b/libmemcached/server_list.c @@ -18,7 +18,7 @@ memcached_server_st *memcached_server_list_append_with_weight(memcached_server_s memcached_return_t *error) { unsigned int count; - memcached_server_instance_st *new_host_list; + memcached_server_st *new_host_list; if (hostname == NULL || error == NULL) return NULL; @@ -30,10 +30,10 @@ memcached_server_st *memcached_server_list_append_with_weight(memcached_server_s count= 1; if (ptr != NULL) { - count+= memcached_servers_count(ptr); + count+= memcached_server_list_count(ptr); } - new_host_list= (memcached_server_instance_st *)realloc(ptr, sizeof(memcached_server_instance_st) * count); + new_host_list= (memcached_server_write_instance_st)realloc(ptr, sizeof(memcached_server_st) * count); if (!new_host_list) { *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE; diff --git a/libmemcached/stats.c b/libmemcached/stats.c index 1f3ca5bf..0a0e4cc5 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -229,7 +229,7 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *memc_stat, static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, char *args, - memcached_server_instance_st *instance) + memcached_server_write_instance_st instance) { memcached_return_t rc; @@ -299,7 +299,7 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat, static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat, char *args, - memcached_server_instance_st *instance) + memcached_server_write_instance_st instance) { memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; @@ -379,7 +379,7 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t temp_return; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; memcached_stat_st *stat_instance; stat_instance= stats + x; @@ -411,7 +411,7 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char memcached_return_t rc; memcached_st memc; memcached_st *memc_ptr; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; memc_ptr= memcached_create(&memc); WATCHPOINT_ASSERT(memc_ptr); diff --git a/libmemcached/storage.c b/libmemcached/storage.c index 7a96b778..6225af37 100644 --- a/libmemcached/storage.c +++ b/libmemcached/storage.c @@ -70,7 +70,7 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, memcached_return_t rc; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; uint32_t server_key; - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; WATCHPOINT_ASSERT(!(value == NULL && value_length > 0)); @@ -92,7 +92,7 @@ static inline memcached_return_t memcached_send(memcached_st *ptr, flags, cas, verb); } - server_key= memcached_generate_hash(ptr, master_key, master_key_length); + server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length); instance= memcached_server_instance_fetch(ptr, server_key); if (cas) @@ -444,10 +444,10 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, bool flush; protocol_binary_request_set request= {.bytes= {0}}; size_t send_length= sizeof(request.bytes); - uint32_t server_key= memcached_generate_hash(ptr, master_key, - master_key_length); + uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, master_key, + master_key_length); - memcached_server_instance_st *server= + memcached_server_write_instance_st server= memcached_server_instance_fetch(ptr, server_key); bool noreply= server->root->flags.no_reply; @@ -502,7 +502,7 @@ static memcached_return_t memcached_send_binary(memcached_st *ptr, for (uint32_t x= 0; x < ptr->number_of_replicas; x++) { - memcached_server_instance_st *instance; + memcached_server_write_instance_st instance; ++server_key; if (server_key == memcached_server_count(ptr)) diff --git a/libmemcached/string.c b/libmemcached/string.c index 3a6ee9ac..b6943390 100644 --- a/libmemcached/string.c +++ b/libmemcached/string.c @@ -196,3 +196,8 @@ inline char *memcached_string_value_mutable(const memcached_string_st *self) { return self->string; } + +inline void memcached_string_set_length(memcached_string_st *self, size_t length) +{ + self->end= self->string + length; +} diff --git a/libmemcached/string.h b/libmemcached/string.h index d517a4b6..ef567874 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -71,7 +71,8 @@ const char *memcached_string_value(const memcached_string_st *self); LIBMEMCACHED_LOCAL char *memcached_string_value_mutable(const memcached_string_st *self); -#define memcached_string_set_length(A, B) (A)->end= (A)->string + B +LIBMEMCACHED_LOCAL +void memcached_string_set_length(memcached_string_st *self, size_t length); #ifdef __cplusplus } diff --git a/libmemcached/types.h b/libmemcached/types.h index deaf2f49..057c9e6d 100644 --- a/libmemcached/types.h +++ b/libmemcached/types.h @@ -17,6 +17,8 @@ typedef struct memcached_stat_st memcached_stat_st; typedef struct memcached_analysis_st memcached_analysis_st; typedef struct memcached_result_st memcached_result_st; typedef struct memcached_server_st memcached_server_st; +typedef const struct memcached_server_st memcached_server_instance_st; + typedef struct memcached_callback_st memcached_callback_st; typedef struct memcached_string_st memcached_string_st; @@ -40,7 +42,7 @@ typedef void *(*memcached_calloc_fn)(const memcached_st *ptr, size_t nelem, cons typedef memcached_return_t (*memcached_execute_fn)(const memcached_st *ptr, memcached_result_st *result, void *context); -typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, memcached_server_st *server, void *context); +typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, const memcached_server_st *server, void *context); /** Trigger functions. diff --git a/libmemcached/verbosity.c b/libmemcached/verbosity.c index e9fa3ef4..00c89fbf 100644 --- a/libmemcached/verbosity.c +++ b/libmemcached/verbosity.c @@ -16,7 +16,7 @@ memcached_return_t memcached_verbosity(memcached_st *ptr, uint32_t verbosity) for (x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); rrc= memcached_do(instance, buffer, send_length, true); diff --git a/libmemcached/version.c b/libmemcached/version.c index e3541d06..7ab48880 100644 --- a/libmemcached/version.c +++ b/libmemcached/version.c @@ -33,7 +33,7 @@ static inline memcached_return_t memcached_version_textual(memcached_st *ptr) for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_return_t rrc; - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); rrc= memcached_do(instance, command, send_length, true); @@ -80,7 +80,7 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) { memcached_return_t rrc; - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true); @@ -94,7 +94,7 @@ static inline memcached_return_t memcached_version_binary(memcached_st *ptr) for (x= 0; x < memcached_server_count(ptr); x++) { - memcached_server_instance_st *instance= + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); if (memcached_server_response_count(instance) > 0) diff --git a/tests/libmemcached_world.h b/tests/libmemcached_world.h index 45927a23..ce0c44a4 100644 --- a/tests/libmemcached_world.h +++ b/tests/libmemcached_world.h @@ -98,8 +98,8 @@ test_return_t world_pre_run(libmemcached_test_container_st *container) { for (uint32_t loop= 0; loop < memcached_server_list_count(container->construct.servers); loop++) { - memcached_server_st *instance= - memcached_server_instance_fetch(container->memc, loop); + const memcached_server_instance_st *instance= + memcached_server_instance_by_position(container->memc, loop); test_true(instance->fd == -1); test_true(instance->cursor_active == 0); diff --git a/tests/mem_functions.c b/tests/mem_functions.c index cf48940b..32fc3c6d 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -83,12 +83,13 @@ static test_return_t server_list_null_test(memcached_st *ptr __attribute__((unus in_port_t test_ports[TEST_PORT_COUNT]; static memcached_return_t server_display_function(const memcached_st *ptr __attribute__((unused)), - memcached_server_st *server, void *context) + const memcached_server_st *server, + void *context) { /* Do Nothing */ size_t bigger= *((size_t *)(context)); - assert(bigger <= server->port); - *((size_t *)(context))= server->port; + assert(bigger <= memcached_server_port(server)); + *((size_t *)(context))= memcached_server_port(server); return MEMCACHED_SUCCESS; } @@ -110,7 +111,7 @@ static test_return_t server_sort_test(memcached_st *ptr __attribute__((unused))) test_ports[x]= (in_port_t)random() % 64000; rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); test_true(memcached_server_count(local_memc) == x + 1); - test_true(memcached_servers_count(memcached_server_list(local_memc)) == x+1); + test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1); test_true(rc == MEMCACHED_SUCCESS); } @@ -138,17 +139,17 @@ static test_return_t server_sort2_test(memcached_st *ptr __attribute__((unused)) rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0); test_true(rc == MEMCACHED_SUCCESS); - instance= memcached_server_instance_fetch(local_memc, 0); - test_true(instance->port == 43043); + instance= memcached_server_instance_by_position(local_memc, 0); + test_true(memcached_server_port(instance) == 43043); rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0); test_true(rc == MEMCACHED_SUCCESS); - instance= memcached_server_instance_fetch(local_memc, 0); - test_true(instance->port == 43042); + instance= memcached_server_instance_by_position(local_memc, 0); + test_true(memcached_server_port(instance) == 43042); - instance= memcached_server_instance_fetch(local_memc, 1); - test_true(instance->port == 43043); + instance= memcached_server_instance_by_position(local_memc, 1); + test_true(memcached_server_port(instance) == 43043); callbacks[0]= server_display_function; memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1); @@ -159,7 +160,46 @@ static test_return_t server_sort2_test(memcached_st *ptr __attribute__((unused)) return TEST_SUCCESS; } -static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context) +static memcached_return_t server_print_callback(const memcached_st *ptr __attribute__((unused)), + const memcached_server_st *server, + void *context __attribute__((unused))) +{ + (void)server; // Just in case we aren't printing. + +#if 0 + fprintf(stderr, "%s(%d)", memcached_server_name(server), memcached_server_port(server)); +#endif + + return MEMCACHED_SUCCESS; +} + +static test_return_t memcached_server_remove_test(memcached_st *ptr __attribute__((unused))) +{ + memcached_return_t rc; + memcached_st local_memc; + memcached_st *memc; + memcached_server_st *servers; + memcached_server_fn callbacks[1]; + + const char *server_string= "localhost:4444, localhost:4445, localhost:4446, localhost:4447, localhost, memcache1.memcache.bk.sapo.pt:11211, memcache1.memcache.bk.sapo.pt:11212, memcache1.memcache.bk.sapo.pt:11213, memcache1.memcache.bk.sapo.pt:11214, memcache2.memcache.bk.sapo.pt:11211, memcache2.memcache.bk.sapo.pt:11212, memcache2.memcache.bk.sapo.pt:11213, memcache2.memcache.bk.sapo.pt:11214"; + + memc= memcached_create(&local_memc); + + servers= memcached_servers_parse(server_string); + + rc= memcached_server_push(memc, servers); + + callbacks[0]= server_print_callback; + memcached_server_cursor(memc, callbacks, NULL, 1); + + memcached_free(memc); + + return TEST_SUCCESS; +} + +static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)), + const memcached_server_st *server, + void *context) { /* Do Nothing */ uint32_t x= *((uint32_t *)(context)); @@ -186,7 +226,7 @@ static test_return_t server_unsort_test(memcached_st *ptr __attribute__((unused) test_ports[x]= (in_port_t)(random() % 64000); rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); test_true(memcached_server_count(local_memc) == x+1); - test_true(memcached_servers_count(memcached_server_list(local_memc)) == x+1); + test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1); test_true(rc == MEMCACHED_SUCCESS); } @@ -530,7 +570,7 @@ static test_return_t cas_test(memcached_st *memc) test_true(rc == MEMCACHED_SUCCESS); uint64_t cas = memcached_result_cas(results); - #if 0 +#if 0 results= memcached_fetch_result(memc, &results_obj, &rc); test_true(rc == MEMCACHED_END); test_true(results == NULL); @@ -695,7 +735,7 @@ static test_return_t flush_test(memcached_st *memc) } static memcached_return_t server_function(const memcached_st *ptr __attribute__((unused)), - memcached_server_st *server __attribute__((unused)), + const memcached_server_st *server __attribute__((unused)), void *context __attribute__((unused))) { /* Do Nothing */ @@ -1169,11 +1209,11 @@ static test_return_t stats_servername_test(memcached_st *memc) memcached_return_t rc; memcached_stat_st memc_stat; memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); rc= memcached_stat_servername(&memc_stat, NULL, - instance->hostname, - instance->port); + memcached_server_name(instance), + memcached_server_port(instance)); return TEST_SUCCESS; } @@ -2666,22 +2706,19 @@ static test_return_t user_supplied_bug17(memcached_st *memc) From Andrei on IRC */ -static test_return_t user_supplied_bug19(memcached_st *memc) +static test_return_t user_supplied_bug19(memcached_st *not_used) { - memcached_st *m; - memcached_server_st *s; + memcached_st *memc; + const memcached_server_st *server; memcached_return_t res; - (void)memc; + (void)not_used; - m= memcached_create(NULL); - memcached_server_add_with_weight(m, "localhost", 11311, 100); - memcached_server_add_with_weight(m, "localhost", 11312, 100); - - s= memcached_server_by_key(m, "a", 1, &res); - memcached_server_free(s); + memc= memcached_create(NULL); + memcached_server_add_with_weight(memc, "localhost", 11311, 100); + memcached_server_add_with_weight(memc, "localhost", 11312, 100); - memcached_free(m); + server= memcached_server_by_key(memc, "a", 1, &res); return TEST_SUCCESS; } @@ -2768,9 +2805,11 @@ static test_return_t user_supplied_bug18(memcached_st *trash) for (x= 0; x < 99; x++) { uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); + memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, server_idx); - char *hostname = instance->hostname; + memcached_server_instance_by_position(memc, server_idx); + + const char *hostname = memcached_server_name(instance); test_strcmp(hostname, ketama_test_cases[x].server); } @@ -2913,18 +2952,22 @@ static test_return_t auto_eject_hosts(memcached_st *trash) test_true(server_pool[7].port == 11211); test_true(server_pool[7].weight == 100); - instance= memcached_server_instance_fetch(memc, 2); - instance->next_retry = time(NULL) + 15; + instance= memcached_server_instance_by_position(memc, 2); + ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) + 15; memc->next_distribution_rebuild= time(NULL) - 1; + /* + This would not work if there were only two hosts. + */ for (size_t x= 0; x < 99; x++) { + memcached_autoeject(memc); uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); test_true(server_idx != 2); } /* and re-added when it's back. */ - instance->next_retry = time(NULL) - 1; + ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) - 1; memc->next_distribution_rebuild= time(NULL) - 1; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution); @@ -2933,8 +2976,8 @@ static test_return_t auto_eject_hosts(memcached_st *trash) uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); // We re-use instance from above. instance= - memcached_server_instance_fetch(memc, server_idx); - char *hostname = instance->hostname; + memcached_server_instance_by_position(memc, server_idx); + const char *hostname = memcached_server_name(instance); test_true(strcmp(hostname, ketama_test_cases[x].server) == 0); } @@ -3204,7 +3247,7 @@ static test_return_t generate_data_with_stats(memcached_st *memc) /* This test was changes so that "make test" would work properlly */ #ifdef DEBUG memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, host_index); + memcached_server_instance_by_position(memc, host_index); printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes); #endif @@ -3445,7 +3488,7 @@ static test_return_t pre_nonblock_binary(memcached_st *memc) // will not toggle protocol on an connection. memcached_version(memc_clone); - instance= memcached_server_instance_fetch(memc_clone, 0); + instance= memcached_server_instance_by_position(memc_clone, 0); if (instance->major_version >= 1 && instance->minor_version > 2) { @@ -3581,7 +3624,7 @@ static test_return_t pre_binary(memcached_st *memc) // will not toggle protocol on an connection. memcached_version(memc_clone); - instance= memcached_server_instance_fetch(memc_clone, 0); + instance= memcached_server_instance_by_position(memc_clone, 0); if (instance->major_version >= 1 && instance->minor_version > 2) { @@ -3893,7 +3936,7 @@ static test_return_t enable_cas(memcached_st *memc) unsigned int set= 1; memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); memcached_version(memc); @@ -3911,8 +3954,9 @@ static test_return_t enable_cas(memcached_st *memc) static test_return_t check_for_1_2_3(memcached_st *memc) { memcached_version(memc); + memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4)) || instance->minor_version > 2) @@ -4019,7 +4063,7 @@ static test_return_t noreply_test(memcached_st *memc) for (uint32_t x= 0; x < memcached_server_count(memc); ++x) { memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, x); + memcached_server_instance_by_position(memc, x); no_msg+=(int)(instance->cursor_active); } @@ -4306,9 +4350,9 @@ static test_return_t replication_get_test(memcached_st *memc) { memcached_st *memc_clone= memcached_clone(NULL, memc); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc_clone, host); + memcached_server_instance_by_position(memc_clone, host); - instance->port= 0; + ((memcached_server_write_instance_st)instance)->port= 0; for (int x= 'a'; x <= 'z'; ++x) { @@ -4368,8 +4412,8 @@ static test_return_t replication_mget_test(memcached_st *memc) { memcached_st *new_clone= memcached_clone(NULL, memc); memcached_server_instance_st *instance= - memcached_server_instance_fetch(new_clone, host); - instance->port= 0; + memcached_server_instance_by_position(new_clone, host); + ((memcached_server_write_instance_st)instance)->port= 0; for (int x= 'a'; x <= 'z'; ++x) { @@ -4466,9 +4510,9 @@ static test_return_t replication_delete_test(memcached_st *memc) for (uint32_t x= 0; x < (repl + 1); ++x) { memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc_clone, x); + memcached_server_instance_by_position(memc_clone, x); - instance->port= 0; + ((memcached_server_write_instance_st)instance)->port= 0; if (++hash == memc_clone->number_of_hosts) hash= 0; } @@ -4515,9 +4559,9 @@ static uint16_t *get_udp_request_ids(memcached_st *memc) for (uint32_t x= 0; x < memcached_server_count(memc); x++) { memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, x); + memcached_server_instance_by_position(memc, x); - ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) instance->write_buffer); + ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st *)instance)->write_buffer); } return ids; @@ -4547,7 +4591,7 @@ static test_return_t init_udp(memcached_st *memc) { memcached_version(memc); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); /* For the time being, only support udp test for >= 1.2.6 && < 1.3 */ if (instance->major_version != 1 || instance->minor_version != 2 @@ -4559,10 +4603,10 @@ static test_return_t init_udp(memcached_st *memc) memcpy(servers, memcached_server_list(memc), sizeof(memcached_server_st) * num_hosts); for (uint32_t x= 0; x < num_hosts; x++) { - memcached_server_instance_st *set_instance= - memcached_server_instance_fetch(memc, x); + memcached_server_instance_st *set_instance= + memcached_server_instance_by_position(memc, x); - memcached_server_free(set_instance); + memcached_server_free(((memcached_server_write_instance_st)set_instance)); } memc->number_of_hosts= 0; @@ -4570,7 +4614,7 @@ static test_return_t init_udp(memcached_st *memc) for (uint32_t x= 0; x < num_hosts; x++) { memcached_server_instance_st *set_instance= - memcached_server_instance_fetch(memc, x); + memcached_server_instance_by_position(memc, x); test_true(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS); test_true(set_instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH); @@ -4597,7 +4641,7 @@ static test_return_t add_tcp_server_udp_client_test(memcached_st *memc) #if 0 memcached_server_st server; memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); memcached_server_clone(&server, &memc->hosts[0]); test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS); test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL); @@ -4612,7 +4656,7 @@ static test_return_t add_udp_server_tcp_client_test(memcached_st *memc) #if 0 memcached_server_st server; memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, 0); + memcached_server_instance_by_position(memc, 0); memcached_server_clone(&server, &memc->hosts[0]); test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS); @@ -4656,7 +4700,7 @@ static test_return_t udp_set_test(memcached_st *memc) uint16_t *expected_ids= get_udp_request_ids(memc); unsigned int server_key= memcached_generate_hash(memc, key, strlen(key)); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, server_key); + memcached_server_instance_by_position(memc, server_key); size_t init_offset= instance->write_buffer_offset; rc= memcached_set(memc, key, strlen(key), @@ -4716,7 +4760,7 @@ static test_return_t udp_delete_test(memcached_st *memc) uint16_t *expected_ids=get_udp_request_ids(memc); unsigned int server_key= memcached_generate_hash(memc, key, strlen(key)); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, server_key); + memcached_server_instance_by_position(memc, server_key); size_t init_offset= instance->write_buffer_offset; rc= memcached_delete(memc, key, strlen(key), 0); @@ -5210,8 +5254,8 @@ static test_return_t ketama_compatibility_libmemcached(memcached_st *trash) { uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key)); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, server_idx); - char *hostname = instance->hostname; + memcached_server_instance_by_position(memc, server_idx); + const char *hostname = memcached_server_name(instance); test_strcmp(hostname, ketama_test_cases[x].server); } @@ -5267,10 +5311,13 @@ static test_return_t ketama_compatibility_spymemcached(memcached_st *trash) /* verify the standard ketama set. */ for (x= 0; x < 99; x++) { - uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key)); + uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key)); + memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc, server_idx); - char *hostname = instance->hostname; + memcached_server_instance_by_position(memc, server_idx); + + const char *hostname= memcached_server_name(instance); + test_strcmp(hostname, ketama_test_cases_spy[x].server); } @@ -5531,13 +5578,13 @@ static test_return_t regression_bug_447342(memcached_st *memc) * This is to verify correct behavior in the library. Fake that two servers * are dead.. */ - instance_one= memcached_server_instance_fetch(memc, 0); - instance_two= memcached_server_instance_fetch(memc, 2); + instance_one= memcached_server_instance_by_position(memc, 0); + instance_two= memcached_server_instance_by_position(memc, 2); in_port_t port0= instance_one->port; in_port_t port2= instance_two->port; - instance_one->port= 0; - instance_two->port= 0; + ((memcached_server_write_instance_st)instance_one)->port= 0; + ((memcached_server_write_instance_st)instance_two)->port= 0; rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys); test_true(rc == MEMCACHED_SUCCESS); @@ -5547,8 +5594,8 @@ static test_return_t regression_bug_447342(memcached_st *memc) test_true(counter == (unsigned int)max_keys); /* restore the memc handle */ - instance_one->port= port0; - instance_two->port= port2; + ((memcached_server_write_instance_st)instance_one)->port= port0; + ((memcached_server_write_instance_st)instance_two)->port= port2; memcached_quit(memc); @@ -5563,8 +5610,8 @@ static test_return_t regression_bug_447342(memcached_st *memc) } memcached_quit(memc); - instance_one->port= 0; - instance_two->port= 0; + ((memcached_server_write_instance_st)instance_one)->port= 0; + ((memcached_server_write_instance_st)instance_two)->port= 0; /* now retry the command, this time we should have cache misses */ rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys); @@ -5583,8 +5630,8 @@ static test_return_t regression_bug_447342(memcached_st *memc) free(key_length); /* restore the memc handle */ - instance_one->port= port0; - instance_two->port= port2; + ((memcached_server_write_instance_st)instance_one)->port= port0; + ((memcached_server_write_instance_st)instance_two)->port= port2; return TEST_SUCCESS; } @@ -5596,7 +5643,7 @@ static test_return_t regression_bug_463297(memcached_st *memc) test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS); memcached_server_instance_st *instance= - memcached_server_instance_fetch(memc_clone, 0); + memcached_server_instance_by_position(memc_clone, 0); if (instance->major_version > 1 || (instance->major_version == 1 && @@ -5729,12 +5776,12 @@ static test_return_t wrong_failure_counter_test(memcached_st *memc) test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); - instance= memcached_server_instance_fetch(memc, 0); + instance= memcached_server_instance_by_position(memc, 0); /* The test is to see that the memcached_quit doesn't increase the * the server failure conter, so let's ensure that it is zero * before sending quit */ - instance->server_failure_counter= 0; + ((memcached_server_write_instance_st)instance)->server_failure_counter= 0; memcached_quit(memc); @@ -5869,6 +5916,7 @@ test_st tests[] ={ {"server_unsort", 0, (test_callback_fn)server_unsort_test}, {"server_sort", 0, (test_callback_fn)server_sort_test}, {"server_sort2", 0, (test_callback_fn)server_sort2_test}, + {"memcached_server_remove", 0, (test_callback_fn)memcached_server_remove_test}, {"clone_test", 0, (test_callback_fn)clone_test }, {"connection_test", 0, (test_callback_fn)connection_test}, {"callback_test", 0, (test_callback_fn)callback_test}, diff --git a/tests/server.c b/tests/server.c index 68b3d801..383469bd 100644 --- a/tests/server.c +++ b/tests/server.c @@ -99,7 +99,7 @@ void server_startup(server_startup_st *construct) for (x= 0; x < memcached_server_list_count(construct->servers); x++) { - printf("\t%s : %d\n", construct->servers[x].hostname, construct->servers[x].port); + printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x])); assert(construct->servers[x].fd == -1); assert(construct->servers[x].cursor_active == 0); } -- 2.30.2