X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=libmemcached%2Fmemcached_hosts.c;h=231b2167ba37b2f876e3527baea682eee73316c1;hb=92104790f8c3330d957f43aac5cc73134482fd83;hp=72de6d095e1c3ca95f8566dce80c4a1d954d13cd;hpb=002a16ba3cd4638664880a644d73ddf3dc3b2b6b;p=awesomized%2Flibmemcached diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index 72de6d09..231b2167 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -8,16 +8,32 @@ memcached_return update_continuum(memcached_st *ptr); #define MEMCACHED_WHEEL_SIZE 1024 #define MEMCACHED_STRIDE 4 -static void rebalance_wheel(memcached_st *ptr) +static memcached_return rebalance_wheel(memcached_st *ptr) { unsigned int x; unsigned int y; unsigned int latch; + if (ptr->number_of_hosts > ptr->wheel_count) + { + uint32_t *new_ptr; + + if (ptr->call_realloc) + new_ptr= (uint32_t *)ptr->call_realloc(ptr, ptr->wheel, sizeof(uint32_t) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * MEMCACHED_STRIDE); + else + new_ptr= (uint32_t *)realloc(ptr->wheel, sizeof(uint32_t) * (ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION) * MEMCACHED_STRIDE); + + if (new_ptr == 0) + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + + ptr->wheel= new_ptr; + ptr->wheel_count= ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION; + } + /* Seed the Wheel */ - memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE); + memset(ptr->wheel, 0, sizeof(uint32_t) * MEMCACHED_STRIDE * ptr->wheel_count); - for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++) + for (latch= y= x= 0; x < MEMCACHED_STRIDE * ptr->wheel_count; x++, latch++) { if (latch == MEMCACHED_STRIDE) { @@ -29,6 +45,8 @@ static void rebalance_wheel(memcached_st *ptr) ptr->wheel[x]= y; } + + return MEMCACHED_SUCCESS; } static int compare_servers(const void *p1, const void *p2) @@ -65,7 +83,7 @@ memcached_return run_distribution(memcached_st *ptr) case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: return update_continuum(ptr); case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL: - rebalance_wheel(ptr); + return rebalance_wheel(ptr); break; case MEMCACHED_DISTRIBUTION_MODULA: if (ptr->flags & MEM_USE_SORT_HOSTS) @@ -103,7 +121,10 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers) for (x= 0; x < servers->count; x++) if (servers[x].address_info) + { freeaddrinfo(servers[x].address_info); + servers[x].address_info= NULL; + } if (ptr && ptr->call_free) ptr->call_free(ptr, servers); @@ -125,18 +146,6 @@ static int continuum_item_cmp(const void *t1, const void *t2) return -1; } -static uint32_t internal_generate_ketama_md5(char *key, size_t key_length) -{ - unsigned char results[16]; - - md5_signature((unsigned char*)key, (unsigned int)key_length, results); - - return ( (results[3] ) << 24) - | ( (results[2] ) << 16) - | ( (results[1] ) << 8) - | ( results[0] ); -} - memcached_return update_continuum(memcached_st *ptr) { uint32_t index; @@ -172,7 +181,7 @@ memcached_return update_continuum(memcached_st *ptr) sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d", list[host_index].hostname, list[host_index].port, index); WATCHPOINT_ASSERT(sort_host_length); - value= internal_generate_ketama_md5(sort_host, sort_host_length); + value= generate_hash(ptr, sort_host, sort_host_length); ptr->continuum[continuum_index].index= host_index; ptr->continuum[continuum_index++].value= value; } @@ -212,7 +221,10 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); if (!new_host_list) + { + assert(0); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } ptr->hosts= new_host_list;