#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)
{
ptr->wheel[x]= y;
}
+
+ return MEMCACHED_SUCCESS;
}
static int compare_servers(const void *p1, const void *p2)
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)
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);
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;
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;
}
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;