Merge in fixes for SASL.
[awesomized/libmemcached] / libmemcached / hosts.cc
index 2f600197d7ea9ce6e89ef8bbd95b89b9d07f1583..1bcaaa7aca4bc9b7e90dff04e6d4ffbe3c67fcf4 100644 (file)
@@ -80,7 +80,9 @@ static void sort_hosts(memcached_st *ptr)
 memcached_return_t run_distribution(memcached_st *ptr)
 {
   if (ptr->flags.use_sort_hosts)
+  {
     sort_hosts(ptr);
+  }
 
   switch (ptr->distribution)
   {
@@ -89,15 +91,18 @@ memcached_return_t run_distribution(memcached_st *ptr)
   case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
   case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
     return update_continuum(ptr);
+
   case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
   case MEMCACHED_DISTRIBUTION_MODULA:
     break;
+
   case MEMCACHED_DISTRIBUTION_RANDOM:
     srandom((uint32_t) time(NULL));
     break;
+
   case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
   default:
-    WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
+    assert_msg(0, "Invalid distribution type passed to run_distribution()");
   }
 
   return MEMCACHED_SUCCESS;
@@ -186,7 +191,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
     memcached_continuum_item_st *new_ptr;
 
     new_ptr= static_cast<memcached_continuum_item_st*>(libmemcached_realloc(ptr, ptr->ketama.continuum,
-                                                                           sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
+                                                                            sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server));
 
     if (new_ptr == 0)
       return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
@@ -246,21 +251,19 @@ static memcached_return_t update_continuum(memcached_st *ptr)
 
         if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
         {
-          return MEMCACHED_FAILURE;
+          return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                                     memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
         }
 #ifdef DEBUG
         printf("update_continuum: key is %s\n", sort_host);
 #endif
-
-        WATCHPOINT_ASSERT(sort_host_length);
-
         if (is_ketama_weighted)
         {
           for (uint32_t x= 0; x < pointer_per_hash; x++)
           {
             uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
-             ptr->ketama.continuum[continuum_index].index= host_index;
-             ptr->ketama.continuum[continuum_index++].value= value;
+            ptr->ketama.continuum[continuum_index].index= host_index;
+            ptr->ketama.continuum[continuum_index++].value= value;
           }
         }
         else
@@ -298,18 +301,17 @@ static memcached_return_t update_continuum(memcached_st *ptr)
 
         if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
         {
-          return MEMCACHED_FAILURE;
+          return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                                     memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
         }
 
-        WATCHPOINT_ASSERT(sort_host_length);
-
         if (is_ketama_weighted)
         {
           for (uint32_t x = 0; x < pointer_per_hash; x++)
           {
-             uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
-             ptr->ketama.continuum[continuum_index].index= host_index;
-             ptr->ketama.continuum[continuum_index++].value= value;
+            uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
+            ptr->ketama.continuum[continuum_index].index= host_index;
+            ptr->ketama.continuum[continuum_index++].value= value;
           }
         }
         else
@@ -325,7 +327,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
   }
 
   WATCHPOINT_ASSERT(ptr);
-  WATCHPOINT_ASSERT(ptr->continuum);
+  WATCHPOINT_ASSERT(ptr->ketama.continuum);
   WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
   ptr->ketama.continuum_points_counter= pointer_counter;
   qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
@@ -333,7 +335,7 @@ static memcached_return_t update_continuum(memcached_st *ptr)
 #ifdef DEBUG
   for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
   {
-    WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
+    WATCHPOINT_ASSERT(ptr->ketama.continuum[pointer_index].value <= ptr->ketama.continuum[pointer_index + 1].value);
   }
 #endif
 
@@ -344,7 +346,9 @@ static memcached_return_t update_continuum(memcached_st *ptr)
 memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
 {
   if (not list)
+  {
     return MEMCACHED_SUCCESS;
+  }
 
   uint32_t count= memcached_server_list_count(list);
 
@@ -362,21 +366,19 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv
     memcached_server_write_instance_st instance;
 
     if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
-            || ((list[x].type == MEMCACHED_CONNECTION_UDP)
-            && ! (ptr->flags.use_udp)) )
+        or ((list[x].type == MEMCACHED_CONNECTION_UDP) and not (ptr->flags.use_udp)) )
     {
       return MEMCACHED_INVALID_HOST_PROTOCOL;
     }
 
     WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
 
+    // We have extended the array, and now we will find it, and use it.
     instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
     WATCHPOINT_ASSERT(instance);
 
-    /* TODO check return type */
-    instance= memcached_server_create_with(ptr, instance, list[x].hostname,
-                                           list[x].port, list[x].weight, list[x].type);
-    if (not instance)
+    if (not __server_create_with(ptr, instance, list[x].hostname,
+                                 list[x].port, list[x].weight, list[x].type))
     {
       return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
     }
@@ -454,7 +456,7 @@ memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
   if (not hostname)
     hostname= "localhost";
 
-  return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
+  return server_add(ptr, hostname, port, weight, hostname[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET  : MEMCACHED_CONNECTION_TCP);
 }
 
 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
@@ -480,7 +482,10 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
   /* TODO: Check return type */
   memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
 
-  (void)memcached_server_create_with(ptr, instance, hostname, port, weight, type);
+  if (not __server_create_with(ptr, instance, hostname, port, weight, type))
+  {
+    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
+  }
 
   if (weight > 1)
   {
@@ -489,8 +494,26 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
 
   ptr->number_of_hosts++;
 
+  // @note we place the count in the bottom of the server list
   instance= memcached_server_instance_fetch(ptr, 0);
   memcached_servers_set_count(instance, memcached_server_count(ptr));
 
   return run_distribution(ptr);
 }
+
+memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
+                                               const char *hostname,
+                                               size_t hostname_length,
+                                               in_port_t port,
+                                               uint32_t weight)
+{
+  char buffer[NI_MAXHOST];
+
+  memcpy(buffer, hostname, hostname_length);
+  buffer[hostname_length]= 0;
+
+  return server_add(ptr, buffer,
+                    port,
+                    weight,
+                    MEMCACHED_CONNECTION_TCP);
+}