libmemcached: fix #41 (ensure stable sort on host key collision)
[awesomized/libmemcached] / libmemcached / hosts.cc
index 121e8f7c5700b613b64eb217527589f7bce34b1a..98cb3f7d48dc777c1d4ca18f1c3f75b7a471f771 100644 (file)
@@ -120,7 +120,18 @@ static int continuum_item_cmp(const void *t1, const void *t2)
   WATCHPOINT_ASSERT(ct1->value != 153);
   if (ct1->value == ct2->value)
   {
-    return 0;
+    if (ct1->index == ct2->index)
+    {
+      return 0;
+    }
+    else if (ct1->index > ct2->index)
+    {
+      return 1;
+    }
+    else
+    {
+      return -1;
+    }
   }
   else if (ct1->value > ct2->value)
   {
@@ -174,18 +185,20 @@ static memcached_return_t update_continuum(Memcached *ptr)
     live_servers= memcached_server_count(ptr);
   }
 
-  uint32_t points_per_server= (uint32_t) (memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
-
   if (live_servers == 0)
   {
     return MEMCACHED_SUCCESS;
   }
 
-  if (live_servers > ptr->ketama.continuum_count)
+  uint32_t points_per_server = (uint32_t) (memcached_is_weighted_ketama(ptr) ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
+  uint32_t continuum_limit = live_servers * points_per_server;
+  uint32_t continuum_extra = MEMCACHED_CONTINUUM_ADDITION * points_per_server;
+
+  if (continuum_limit > ptr->ketama.continuum_count)
   {
     memcached_continuum_item_st *new_ptr;
 
-    new_ptr= libmemcached_xrealloc(ptr, ptr->ketama.continuum, (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server, memcached_continuum_item_st);
+    new_ptr= libmemcached_xrealloc(ptr, ptr->ketama.continuum, continuum_limit + continuum_extra, memcached_continuum_item_st);
 
     if (new_ptr == 0)
     {
@@ -193,7 +206,7 @@ static memcached_return_t update_continuum(Memcached *ptr)
     }
 
     ptr->ketama.continuum= new_ptr;
-    ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
+    ptr->ketama.continuum_count= continuum_limit + continuum_extra;
   }
   assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum");
 
@@ -221,7 +234,7 @@ static memcached_return_t update_continuum(Memcached *ptr)
         float pct= (float)list[host_index].weight / (float)total_weight;
         pointer_per_server= (uint32_t) ((::floor((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
         pointer_per_hash= 4;
-        if (DEBUG)
+        if (0 && DEBUG)
         {
           printf("ketama_weighted:%s|%d|%llu|%u\n",
                  list[host_index]._hostname,
@@ -255,7 +268,7 @@ static memcached_return_t update_continuum(Memcached *ptr)
                                      memcached_literal_param("snprintf(sizeof(sort_host))"));
         }
 
-        if (DEBUG)
+        if (0 && DEBUG)
         {
           fprintf(stdout, "update_continuum: key is %s\n", sort_host);
         }
@@ -591,7 +604,7 @@ memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
                                                in_port_t port,
                                                uint32_t weight)
 {
-  char buffer[MEMCACHED_NI_MAXHOST];
+  char buffer[MEMCACHED_NI_MAXHOST]= { 0 };
 
   memcpy(buffer, hostname, hostname_length);
   buffer[hostname_length]= 0;