From: Date: Mon, 21 Apr 2008 23:10:50 +0000 (-0700) Subject: Committing change to dynamic numbers of servers (need to pull this to a linux X-Git-Tag: 0.20~1^2~12 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=002a16ba3cd4638664880a644d73ddf3dc3b2b6b;p=awesomized%2Flibmemcached Committing change to dynamic numbers of servers (need to pull this to a linux box for testing) --- diff --git a/libmemcached/common.h b/libmemcached/common.h index 44e32dd8..50ee60bf 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -108,7 +108,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers); memcached_return memcachd_key_test(char **keys, size_t *key_length, unsigned int number_of_keys); -void sort_hosts(memcached_st *ptr); +memcached_return run_distribution(memcached_st *ptr); uint32_t generate_hash(memcached_st *ptr, char *key, size_t key_length); #endif /* __COMMON_H__ */ diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index 33a80bab..353d135f 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -105,6 +105,14 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr) new_clone->get_key_failure= ptr->get_key_failure; new_clone->delete_trigger= ptr->delete_trigger; + rc= run_distribution(new_clone); + if (rc != MEMCACHED_SUCCESS) + { + memcached_free(new_clone); + + return NULL; + } + if (ptr->on_clone) ptr->on_clone(ptr, new_clone); diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index cac25a89..45d4085f 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -31,10 +31,11 @@ extern "C" { #define MEMCACHED_VERSION_STRING_LENGTH 12 /* string value */ -struct continuum_item{ - uint32_t index; - uint32_t value; +struct memcached_continuum_item_st { + uint32_t index; + uint32_t value; }; + #define LIBMEMCACHED_VERSION_STRING "0.20" struct memcached_stat_st { @@ -81,7 +82,8 @@ struct memcached_st { memcached_server_distribution distribution; void *user_data; unsigned int wheel[MEMCACHED_WHEEL_SIZE]; - struct continuum_item continuum[MEMCACHED_CONTINUUM_SIZE]; + uint32_t continuum_count; + memcached_continuum_item_st *continuum; memcached_clone_func on_clone; memcached_cleanup_func on_cleanup; memcached_free_function call_free; diff --git a/libmemcached/memcached_behavior.c b/libmemcached/memcached_behavior.c index 1de8f495..287f8f0c 100644 --- a/libmemcached/memcached_behavior.c +++ b/libmemcached/memcached_behavior.c @@ -60,7 +60,7 @@ memcached_return memcached_behavior_set(memcached_st *ptr, if (ptr->flags & MEM_USE_SORT_HOSTS) { memcached_quit(ptr); - sort_hosts(ptr); + run_distribution(ptr); } break; diff --git a/libmemcached/memcached_constants.h b/libmemcached/memcached_constants.h index 237ff54d..e60a03b7 100644 --- a/libmemcached/memcached_constants.h +++ b/libmemcached/memcached_constants.h @@ -24,6 +24,7 @@ extern "C" { #define MEMCACHED_CONTINUUM_SIZE MEMCACHED_POINTS_PER_SERVER*100 /* This would then set max hosts to 100 */ #define MEMCACHED_STRIDE 4 #define MEMCACHED_DEFAULT_TIMEOUT INT32_MAX +#define MEMCACHED_CONTINUUM_ADDITION 10 /* How many extra slots we should build for in the continuum */ typedef enum { MEMCACHED_SUCCESS, diff --git a/libmemcached/memcached_get.c b/libmemcached/memcached_get.c index 354f7bb8..852e90dd 100644 --- a/libmemcached/memcached_get.c +++ b/libmemcached/memcached_get.c @@ -39,7 +39,7 @@ char *memcached_get_by_key(memcached_st *ptr, if (value == NULL) { - if (ptr->get_key_failure) + if (ptr->get_key_failure && *error == MEMCACHED_NOTFOUND) { memcached_return rc; diff --git a/libmemcached/memcached_hash.c b/libmemcached/memcached_hash.c index 8d269faa..3294a0a6 100644 --- a/libmemcached/memcached_hash.c +++ b/libmemcached/memcached_hash.c @@ -112,13 +112,13 @@ unsigned int dispatch_host(memcached_st *ptr, uint32_t hash) int num= ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER; hash= hash; - struct continuum_item *begin, *end, *left, *right, *middle; + memcached_continuum_item_st *begin, *end, *left, *right, *middle; begin= left= ptr->continuum; end= right= ptr->continuum + (num - 1); while (1) { - struct continuum_item *rmiddle; + memcached_continuum_item_st *rmiddle; middle = left + (right - left) / 2; @@ -175,7 +175,7 @@ unsigned int dispatch_host(memcached_st *ptr, uint32_t hash) int num = ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER; hash = hash; - struct continuum_item *begin, *end, *left, *right, *middle; + memcached_continuum_item_st *begin, *end, *left, *right, *middle; begin = left = ptr->continuum; end = right = ptr->continuum + (num - 1); while(1) @@ -189,7 +189,7 @@ unsigned int dispatch_host(memcached_st *ptr, uint32_t hash) { return end->index; } - struct continuum_item *rmiddle = middle+1; + memcached_continuum_item_st *rmiddle = middle+1; if(hashvalue && hash>=middle->value) return middle->index; diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index 79bcb99d..72de6d09 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -4,6 +4,7 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, unsigned int port, memcached_connection type); +memcached_return update_continuum(memcached_st *ptr); #define MEMCACHED_WHEEL_SIZE 1024 #define MEMCACHED_STRIDE 4 @@ -55,6 +56,28 @@ void sort_hosts(memcached_st *ptr) } } + +memcached_return run_distribution(memcached_st *ptr) +{ + switch (ptr->distribution) + { + case MEMCACHED_DISTRIBUTION_CONSISTENT: + case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: + return update_continuum(ptr); + case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL: + rebalance_wheel(ptr); + break; + case MEMCACHED_DISTRIBUTION_MODULA: + if (ptr->flags & MEM_USE_SORT_HOSTS) + sort_hosts(ptr); + break; + default: + WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */ + } + + return MEMCACHED_SUCCESS; +} + static void host_reset(memcached_st *ptr, memcached_server_st *host, char *hostname, unsigned int port, memcached_connection type) @@ -90,8 +113,8 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers) static int continuum_item_cmp(const void *t1, const void *t2) { - struct continuum_item *ct1 = (struct continuum_item *)t1; - struct continuum_item *ct2 = (struct continuum_item *)t2; + memcached_continuum_item_st *ct1 = (memcached_continuum_item_st *)t1; + memcached_continuum_item_st *ct2 = (memcached_continuum_item_st *)t2; WATCHPOINT_ASSERT(ct1->value != 153); if (ct1->value == ct2->value) @@ -114,14 +137,31 @@ static uint32_t internal_generate_ketama_md5(char *key, size_t key_length) | ( results[0] ); } -void update_continuum(memcached_st *ptr) +memcached_return update_continuum(memcached_st *ptr) { uint32_t index; uint32_t host_index; uint32_t continuum_index= 0; uint32_t value; - memcached_server_st *list = ptr->hosts; + memcached_server_st *list; + + if (ptr->number_of_hosts > ptr->continuum_count) + { + memcached_continuum_item_st *new_ptr; + + if (ptr->call_realloc) + new_ptr= (memcached_continuum_item_st *)ptr->call_realloc(ptr, ptr->continuum, sizeof(memcached_continuum_item_st) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * MEMCACHED_POINTS_PER_SERVER); + else + new_ptr= (memcached_continuum_item_st *)realloc(ptr->continuum, sizeof(memcached_continuum_item_st) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * MEMCACHED_POINTS_PER_SERVER); + + if (new_ptr == 0) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + ptr->continuum= new_ptr; + ptr->continuum_count= ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION; + } + list = ptr->hosts; for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index) { for(index= 1; index <= MEMCACHED_POINTS_PER_SERVER; ++index) @@ -139,13 +179,15 @@ void update_continuum(memcached_st *ptr) } WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE); - qsort(ptr->continuum, ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER, sizeof(struct continuum_item), continuum_item_cmp); + qsort(ptr->continuum, ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER, sizeof(memcached_continuum_item_st), continuum_item_cmp); #ifdef HAVE_DEBUG for (index= 0; index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++) { WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value); } #endif + + return MEMCACHED_SUCCESS; } @@ -183,25 +225,7 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l } ptr->hosts[0].count= ptr->number_of_hosts; - if (ptr->flags & MEM_USE_SORT_HOSTS) - sort_hosts(ptr); - - switch (ptr->distribution) - { - case MEMCACHED_DISTRIBUTION_CONSISTENT: - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: - update_continuum(ptr); - break; - case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL: - rebalance_wheel(ptr); - break; - case MEMCACHED_DISTRIBUTION_MODULA: - break; - default: - WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */ - } - - return MEMCACHED_SUCCESS; + return run_distribution(ptr); } memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename) @@ -243,8 +267,6 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, memcached_connection type) { memcached_server_st *new_host_list; - LIBMEMCACHED_MEMCACHED_SERVER_ADD_START(); - if (ptr->call_realloc) new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts, @@ -259,30 +281,9 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type); ptr->number_of_hosts++; - - if (ptr->flags & MEM_USE_SORT_HOSTS) - sort_hosts(ptr); - ptr->hosts[0].count= ptr->number_of_hosts; - switch (ptr->distribution) - { - case MEMCACHED_DISTRIBUTION_CONSISTENT: - case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: - update_continuum(ptr); - break; - case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL: - rebalance_wheel(ptr); - break; - case MEMCACHED_DISTRIBUTION_MODULA: - break; - default: - WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */ - } - - LIBMEMCACHED_MEMCACHED_SERVER_ADD_END(); - - return MEMCACHED_SUCCESS; + return run_distribution(ptr); } memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, diff --git a/libmemcached/memcached_types.h b/libmemcached/memcached_types.h index de0709ba..20837af9 100644 --- a/libmemcached/memcached_types.h +++ b/libmemcached/memcached_types.h @@ -18,6 +18,7 @@ typedef struct memcached_stat_st memcached_stat_st; typedef struct memcached_result_st memcached_result_st; typedef struct memcached_string_st memcached_string_st; typedef struct memcached_server_st memcached_server_st; +typedef struct memcached_continuum_item_st memcached_continuum_item_st; typedef memcached_return (*memcached_clone_func)(memcached_st *parent, memcached_st *clone); typedef memcached_return (*memcached_cleanup_func)(memcached_st *ptr); typedef void (*memcached_free_function)(memcached_st *ptr, void *mem);