Removed old asserts in client apps.
[awesomized/libmemcached] / libmemcached / memcached_hosts.c
index 72de6d095e1c3ca95f8566dce80c4a1d954d13cd..2678a7c1ae131a9ada0d451dd868fb02528ba534 100644 (file)
@@ -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;
     }