Updating for merge
[m6w6/libmemcached] / libmemcached / memcached_hosts.c
index c62c3b41984402665b324b097f3717ef930a1a01..79bcb99d021eb2bf895c5f088dea109f53cc519c 100644 (file)
@@ -40,15 +40,21 @@ static int compare_servers(const void *p1, const void *p2)
 
   if (return_value == 0)
   {
-    if (a->port > b->port)
-      return_value++;
-    else
-      return_value--;
+    return_value= (int) (a->port - b->port);
   }
 
   return return_value;
 }
 
+void sort_hosts(memcached_st *ptr)
+{
+  if (ptr->number_of_hosts)
+  {
+    qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
+    ptr->hosts[0].count= ptr->number_of_hosts;
+  }
+}
+
 static void host_reset(memcached_st *ptr, memcached_server_st *host, 
                        char *hostname, unsigned int port,
                        memcached_connection type)
@@ -82,6 +88,67 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers)
     free(servers);
 }
 
+static int continuum_item_cmp(const void *t1, const void *t2)
+{
+  struct continuum_item *ct1 = (struct continuum_item *)t1;
+  struct continuum_item *ct2 = (struct continuum_item *)t2;
+
+  WATCHPOINT_ASSERT(ct1->value != 153);
+  if (ct1->value == ct2->value)
+    return 0;
+  else if (ct1->value > ct2->value)
+    return 1;
+  else
+    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] );
+}
+
+void update_continuum(memcached_st *ptr)
+{
+  uint32_t index;
+  uint32_t host_index;
+  uint32_t continuum_index= 0;
+  uint32_t value;
+  memcached_server_st *list = ptr->hosts;
+
+  for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index) 
+  {
+    for(index= 1; index <= MEMCACHED_POINTS_PER_SERVER; ++index) 
+    {
+      char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
+      size_t sort_host_length;
+
+      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);
+      ptr->continuum[continuum_index].index= host_index;
+      ptr->continuum[continuum_index++].value= value;
+    }
+  }
+
+  WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
+  qsort(ptr->continuum, ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER, sizeof(struct continuum_item), continuum_item_cmp);
+#ifdef HAVE_DEBUG
+  for (index= 0; index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++) 
+  {
+    WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value);
+  }
+#endif
+}
+
+
 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
 {
   unsigned int x;
@@ -116,10 +183,23 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l
   }
   ptr->hosts[0].count= ptr->number_of_hosts;
 
-  if (ptr->number_of_hosts > 1)
-    qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
+  if (ptr->flags & MEM_USE_SORT_HOSTS)
+    sort_hosts(ptr);
 
-  rebalance_wheel(ptr);
+  switch (ptr->distribution) 
+  {
+  case MEMCACHED_DISTRIBUTION_CONSISTENT:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
+    update_continuum(ptr);
+    break;
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL:
+    rebalance_wheel(ptr);
+    break;
+  case MEMCACHED_DISTRIBUTION_MODULA:
+    break;
+  default:
+    WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
+  }
 
   return MEMCACHED_SUCCESS;
 }
@@ -179,12 +259,26 @@ static memcached_return server_add(memcached_st *ptr, char *hostname,
 
   host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
   ptr->number_of_hosts++;
-  ptr->hosts[0].count++;
 
-  if (ptr->number_of_hosts > 1)
-    qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
+  if (ptr->flags & MEM_USE_SORT_HOSTS)
+    sort_hosts(ptr);
 
-  rebalance_wheel(ptr);
+  ptr->hosts[0].count= ptr->number_of_hosts;
+
+  switch (ptr->distribution) 
+  {
+  case MEMCACHED_DISTRIBUTION_CONSISTENT:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
+    update_continuum(ptr);
+    break;
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL:
+    rebalance_wheel(ptr);
+    break;
+  case MEMCACHED_DISTRIBUTION_MODULA:
+    break;
+  default:
+    WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
+  }
 
   LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
 
@@ -221,16 +315,8 @@ memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
   host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
 
   /* Backwards compatibility hack */
-  new_host_list[0].count++;
-
-  count= new_host_list[0].count;
-
-  if (new_host_list[0].count > 1)
-    qsort(new_host_list, count, sizeof(memcached_server_st), compare_servers);
-
   new_host_list[0].count= count;
 
-
   *error= MEMCACHED_SUCCESS;
   return new_host_list;
 }