From: Date: Wed, 13 Feb 2008 07:42:57 +0000 (+0530) Subject: We now sort servers to make sure the client applications will always have the X-Git-Tag: _20~20 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=85d5656889bb4a6148974f8817fbd419a45a3797;p=m6w6%2Flibmemcached We now sort servers to make sure the client applications will always have the same order. --- diff --git a/ChangeLog b/ChangeLog index b7f45663..55dff5f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,9 @@ * Added get_by_key, set_by_key tests for C++ API * Fix for limit_maxbytes to be 64bit in stats * Added Atom Smasher test (scale baby, scale!) + * Servers are now sorted, meaning that servers are now ordered so that + clients with the same lists, will have same distribution. (Idea from + Ross McFarland). 0.15 Tue Jan 29 14:55:44 PST 2008 * More work on the C++ API. diff --git a/THANKS b/THANKS index a62f3d94..f91fa705 100644 --- a/THANKS +++ b/THANKS @@ -5,3 +5,4 @@ Tobias Luetke - Performance Feedback Andre Cruz - Help with getting the CRC Hash function to match other connectors Brian Pontz - Hsieh hash Tim Bunce - Perl Driver work and feedback on API +Ross McFarland - Idea for sorting servers. diff --git a/lib/memcached_hosts.c b/lib/memcached_hosts.c index 760eb391..de006dd6 100644 --- a/lib/memcached_hosts.c +++ b/lib/memcached_hosts.c @@ -31,6 +31,25 @@ static void rebalance_wheel(memcached_st *ptr) } } +static int compare_servers(const void *p1, const void *p2) +{ + int return_value; + memcached_server_st *a= (memcached_server_st *)p1; + memcached_server_st *b= (memcached_server_st *)p2; + + return_value= strcmp(a->hostname, b->hostname); + + if (return_value == 0) + { + if (a->port > b->port) + return_value++; + else + return_value--; + } + + return return_value; +} + static void host_reset(memcached_st *ptr, memcached_server_st *host, char *hostname, unsigned int port, memcached_connection type) @@ -96,6 +115,9 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l } ptr->hosts[0].count= ptr->number_of_hosts; + if (ptr->number_of_hosts > 1) + qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); + rebalance_wheel(ptr); return MEMCACHED_SUCCESS; @@ -158,6 +180,9 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, ptr->number_of_hosts++; ptr->hosts[0].count++; + if (ptr->number_of_hosts > 1) + qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); + rebalance_wheel(ptr); LIBMEMCACHED_MEMCACHED_SERVER_ADD_END(); @@ -197,6 +222,13 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr, /* Backwards compatibility hack */ new_host_list[0].count++; + count= new_host_list[0].count; + + if (new_host_list[0].count > 1) + qsort(new_host_list, count, sizeof(memcached_server_st), compare_servers); + + new_host_list[0].count= count; + *error= MEMCACHED_SUCCESS; return new_host_list; diff --git a/tests/function.c b/tests/function.c index 2b01a268..0ead1435 100644 --- a/tests/function.c +++ b/tests/function.c @@ -61,6 +61,28 @@ uint8_t server_list_null_test(memcached_st *ptr) return 0; } +uint8_t server_sort_test(memcached_st *ptr) +{ + memcached_server_st *server_list; + memcached_return rc; + + server_list= memcached_server_list_append(NULL, "arg", 0, &rc); + assert(server_list); + + server_list= memcached_server_list_append(server_list, "localhost", 0, &rc); + assert(server_list); + + server_list= memcached_server_list_append(server_list, "c", 0, &rc); + assert(server_list); + + server_list= memcached_server_list_append(server_list, "abba", 0, &rc); + assert(server_list); + + free(server_list); + + return 0; +} + uint8_t allocation_test(memcached_st *not_used) { memcached_st *memc; @@ -2312,6 +2334,7 @@ test_st tests[] ={ {"init", 0, init_test }, {"allocation", 0, allocation_test }, {"server_list_null_test", 0, server_list_null_test}, + {"server_sort", 0, server_sort_test}, {"clone_test", 0, clone_test }, {"error", 0, error_test }, {"set", 0, set_test }, diff --git a/tests/server.c b/tests/server.c index c8e1d98f..8f4dc298 100644 --- a/tests/server.c +++ b/tests/server.c @@ -25,7 +25,6 @@ void server_startup(server_startup_st *construct) } else { - WATCHPOINT; { char server_string_buffer[8096]; char *end_ptr;