libmemcached: fix #41 (ensure stable sort on host key collision)
[awesomized/libmemcached] / libmemcached / hosts.cc
index 80ba0339e5e41b175570a0f66557110a597e2191..98cb3f7d48dc777c1d4ca18f1c3f75b7a471f771 100644 (file)
 #include <sys/time.h>
 
 /* Protoypes (static) */
-static memcached_return_t update_continuum(memcached_st *ptr);
+static memcached_return_t update_continuum(Memcached *ptr);
 
 static int compare_servers(const void *p1, const void *p2)
 {
-  memcached_server_instance_st a= (memcached_server_instance_st)p1;
-  memcached_server_instance_st b= (memcached_server_instance_st)p2;
+  const memcached_instance_st * a= (const memcached_instance_st *)p1;
+  const memcached_instance_st * b= (const memcached_instance_st *)p2;
 
-  int return_value= strcmp(a->hostname, b->hostname);
+  int return_value= strcmp(a->_hostname, b->_hostname);
 
   if (return_value == 0)
   {
@@ -59,16 +59,16 @@ static int compare_servers(const void *p1, const void *p2)
   return return_value;
 }
 
-static void sort_hosts(memcached_st *ptr)
+static void sort_hosts(Memcached *ptr)
 {
   if (memcached_server_count(ptr))
   {
-    qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(org::libmemcached::Instance), compare_servers);
+    qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(memcached_instance_st), compare_servers);
   }
 }
 
 
-memcached_return_t run_distribution(memcached_st *ptr)
+memcached_return_t run_distribution(Memcached *ptr)
 {
   if (ptr->flags.use_sort_hosts)
   {
@@ -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)
   {
@@ -132,7 +143,7 @@ static int continuum_item_cmp(const void *t1, const void *t2)
   }
 }
 
-static memcached_return_t update_continuum(memcached_st *ptr)
+static memcached_return_t update_continuum(Memcached *ptr)
 {
   uint32_t continuum_index= 0;
   uint32_t pointer_counter= 0;
@@ -146,7 +157,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
     return memcached_set_errno(*ptr, errno, MEMCACHED_AT);
   }
 
-  org::libmemcached::Instance* list= memcached_instance_list(ptr);
+  memcached_instance_st* list= memcached_instance_list(ptr);
 
   /* count live servers (those without a retry delay set) */
   bool is_auto_ejecting= _is_auto_eject_host(ptr);
@@ -174,18 +185,20 @@ static memcached_return_t update_continuum(memcached_st *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_st *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,10 +234,10 @@ static memcached_return_t update_continuum(memcached_st *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,
+                 list[host_index]._hostname,
                  list[host_index].port(),
                  (unsigned long long)list[host_index].weight,
                  pointer_per_server);
@@ -245,7 +258,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
         // If hostname is not available then: /ip:port-index
         sort_host_length= snprintf(sort_host, sizeof(sort_host),
                                    "/%s:%u-%u",
-                                   list[host_index].hostname,
+                                   list[host_index]._hostname,
                                    (uint32_t)list[host_index].port(),
                                    pointer_index);
 
@@ -255,7 +268,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
                                      memcached_literal_param("snprintf(sizeof(sort_host))"));
         }
 
-        if (DEBUG)
+        if (0 && DEBUG)
         {
           fprintf(stdout, "update_continuum: key is %s\n", sort_host);
         }
@@ -290,14 +303,14 @@ static memcached_return_t update_continuum(memcached_st *ptr)
         {
           sort_host_length= snprintf(sort_host, sizeof(sort_host),
                                      "%s-%u",
-                                     list[host_index].hostname,
+                                     list[host_index]._hostname,
                                      pointer_index - 1);
         }
         else
         {
           sort_host_length= snprintf(sort_host, sizeof(sort_host),
                                      "%s:%u-%u",
-                                     list[host_index].hostname,
+                                     list[host_index]._hostname,
                                      (uint32_t)list[host_index].port(),
                                      pointer_index - 1);
         }
@@ -346,7 +359,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return_t server_add(memcached_st *memc, 
+static memcached_return_t server_add(Memcached *memc, 
                                      const memcached_string_t& hostname,
                                      in_port_t port,
                                      uint32_t weight,
@@ -365,7 +378,7 @@ static memcached_return_t server_add(memcached_st *memc,
   }
 
   uint32_t host_list_size= memc->number_of_hosts +1;
-  org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(memc, memcached_instance_list(memc), host_list_size, org::libmemcached::Instance);
+  memcached_instance_st* new_host_list= libmemcached_xrealloc(memc, memcached_instance_list(memc), host_list_size, memcached_instance_st);
 
   if (new_host_list == NULL)
   {
@@ -376,7 +389,7 @@ static memcached_return_t server_add(memcached_st *memc,
   assert(memc->number_of_hosts == host_list_size);
 
   /* TODO: Check return type */
-  org::libmemcached::Instance* instance= memcached_instance_fetch(memc, memcached_server_count(memc) -1);
+  memcached_instance_st* instance= memcached_instance_fetch(memc, memcached_server_count(memc) -1);
 
   if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL)
   {
@@ -395,55 +408,61 @@ static memcached_return_t server_add(memcached_st *memc,
 }
 
 
-memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
+memcached_return_t memcached_server_push(memcached_st *shell, const memcached_server_list_st list)
 {
   if (list == NULL)
   {
     return MEMCACHED_SUCCESS;
   }
 
-  uint32_t original_host_size= memcached_server_count(ptr);
-  uint32_t count= memcached_server_list_count(list);
-  uint32_t host_list_size= count +original_host_size;
-
-  org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
-
-  if (new_host_list == NULL)
-  {
-    return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-  }
-
-  memcached_instance_set(ptr, new_host_list, host_list_size);
-
-  ptr->state.is_parsing= true;
-  for (uint32_t x= 0; x < count; ++x, ++original_host_size)
+  Memcached* ptr= memcached2Memcached(shell);
+  if (ptr)
   {
-    WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
+    uint32_t original_host_size= memcached_server_count(ptr);
+    uint32_t count= memcached_server_list_count(list);
+    uint32_t host_list_size= count +original_host_size;
 
-    // We have extended the array, and now we will find it, and use it.
-    org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size);
-    WATCHPOINT_ASSERT(instance);
+    memcached_instance_st* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st);
 
-    memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
-    if (__instance_create_with(ptr, instance, 
-                               hostname,
-                               list[x].port, list[x].weight, list[x].type) == NULL)
+    if (new_host_list == NULL)
     {
-      ptr->state.is_parsing= false;
-      return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+      return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
     }
 
-    if (list[x].weight > 1)
+    memcached_instance_set(ptr, new_host_list, host_list_size);
+
+    ptr->state.is_parsing= true;
+    for (uint32_t x= 0; x < count; ++x, ++original_host_size)
     {
-      memcached_set_weighted_ketama(ptr, true);
+      WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
+
+      // We have extended the array, and now we will find it, and use it.
+      memcached_instance_st* instance= memcached_instance_fetch(ptr, original_host_size);
+      WATCHPOINT_ASSERT(instance);
+
+      memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+      if (__instance_create_with(ptr, instance, 
+                                 hostname,
+                                 list[x].port, list[x].weight, list[x].type) == NULL)
+      {
+        ptr->state.is_parsing= false;
+        return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+      }
+
+      if (list[x].weight > 1)
+      {
+        memcached_set_weighted_ketama(ptr, true);
+      }
     }
+    ptr->state.is_parsing= false;
+
+    return run_distribution(ptr);
   }
-  ptr->state.is_parsing= false;
 
-  return run_distribution(ptr);
+  return MEMCACHED_INVALID_ARGUMENTS;
 }
 
-memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org::libmemcached::Instance* list, uint32_t number_of_hosts)
+memcached_return_t memcached_instance_push(memcached_st *ptr, const struct memcached_instance_st* list, uint32_t number_of_hosts)
 {
   if (list == NULL)
   {
@@ -452,7 +471,7 @@ memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org::
 
   uint32_t original_host_size= memcached_server_count(ptr);
   uint32_t host_list_size= number_of_hosts +original_host_size;
-  org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, org::libmemcached::Instance);
+  memcached_instance_st* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), host_list_size, memcached_instance_st);
 
   if (new_host_list == NULL)
   {
@@ -468,13 +487,13 @@ memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org::
   // instance allocated.
   for (uint32_t x= 0; x < number_of_hosts; ++x, ++original_host_size)
   {
-    WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
+    WATCHPOINT_ASSERT(list[x]._hostname[0] != 0);
 
     // We have extended the array, and now we will find it, and use it.
-    org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size);
+    memcached_instance_st* instance= memcached_instance_fetch(ptr, original_host_size);
     WATCHPOINT_ASSERT(instance);
 
-    memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) };
+    memcached_string_t hostname= { memcached_string_make_from_cstr(list[x]._hostname) };
     if (__instance_create_with(ptr, instance, 
                                hostname,
                                list[x].port(), list[x].weight, list[x].type) == NULL)
@@ -499,22 +518,23 @@ memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
   return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
 }
 
-memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
+memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *shell,
                                                                 const char *filename,
                                                                 uint32_t weight)
 {
-  if (ptr == NULL)
+  Memcached* ptr= memcached2Memcached(shell);
+  if (ptr)
   {
-    return MEMCACHED_FAILURE;
-  }
+    memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
+    if (memcached_is_valid_filename(_filename) == false)
+    {
+      return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
+    }
 
-  memcached_string_t _filename= { memcached_string_make_from_cstr(filename) };
-  if (memcached_is_valid_filename(_filename) == false)
-  {
-    return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided"));
+    return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
   }
 
-  return server_add(ptr, _filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
+  return MEMCACHED_FAILURE;
 }
 
 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
@@ -524,31 +544,33 @@ memcached_return_t memcached_server_add_udp(memcached_st *ptr,
   return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
 }
 
-memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
+memcached_return_t memcached_server_add_udp_with_weight(memcached_st *shell,
                                                         const char *,
                                                         in_port_t,
                                                         uint32_t)
 {
-  if (ptr == NULL)
+  Memcached* self= memcached2Memcached(shell);
+  if (self)
   {
-    return MEMCACHED_INVALID_ARGUMENTS;
+    return memcached_set_error(*self, MEMCACHED_DEPRECATED, MEMCACHED_AT);
   }
 
-  return memcached_set_error(*ptr, MEMCACHED_DEPRECATED, MEMCACHED_AT);
+  return MEMCACHED_INVALID_ARGUMENTS;
 }
 
-memcached_return_t memcached_server_add(memcached_st *ptr,
+memcached_return_t memcached_server_add(memcached_st *shell,
                                         const char *hostname,
                                         in_port_t port)
 {
-  return memcached_server_add_with_weight(ptr, hostname, port, 0);
+  return memcached_server_add_with_weight(shell, hostname, port, 0);
 }
 
-memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
+memcached_return_t memcached_server_add_with_weight(memcached_st *shell,
                                                     const char *hostname,
                                                     in_port_t port,
                                                     uint32_t weight)
 {
+  Memcached* ptr= memcached2Memcached(shell);
   if (ptr == NULL)
   {
     return MEMCACHED_INVALID_ARGUMENTS;
@@ -582,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;