X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;ds=sidebyside;f=libmemcached%2Fhosts.c;h=5ce4b70dde58e024fbf34f5ef5024fef5c1da70a;hb=15e184ac105259cb4691a126ab17cfd449be8dd3;hp=8cc5f3e612bec72a9641e132bf0d9aa50ead2308;hpb=7c7750f02368b570353ea109f23a0ea26d226e02;p=awesomized%2Flibmemcached diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index 8cc5f3e6..5ce4b70d 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -6,7 +6,7 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, in_port_t port, uint32_t weight, memcached_connection_t type); -memcached_return_t update_continuum(memcached_st *ptr); +static memcached_return_t update_continuum(memcached_st *ptr); static int compare_servers(const void *p1, const void *p2) { @@ -36,6 +36,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) { case MEMCACHED_DISTRIBUTION_CONSISTENT: @@ -43,11 +46,11 @@ memcached_return_t run_distribution(memcached_st *ptr) case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: return update_continuum(ptr); case MEMCACHED_DISTRIBUTION_MODULA: - if (ptr->flags.use_sort_hosts) - sort_hosts(ptr); 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 */ } @@ -103,7 +106,7 @@ static int continuum_item_cmp(const void *t1, const void *t2) return -1; } -memcached_return_t update_continuum(memcached_st *ptr) +static memcached_return_t update_continuum(memcached_st *ptr) { uint32_t host_index; uint32_t continuum_index= 0; @@ -213,9 +216,9 @@ memcached_return_t update_continuum(memcached_st *ptr) // Spymemcached ketema key format is: hostname/ip:port-index // If hostname is not available then: /ip:port-index sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, - "/%s:%d-%d", + "/%s:%u-%u", list[host_index].hostname, - list[host_index].port, + (uint32_t)list[host_index].port, pointer_index); #ifdef DEBUG printf("update_continuum: key is %s\n", sort_host); @@ -235,7 +238,7 @@ memcached_return_t update_continuum(memcached_st *ptr) } else { - value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum); + value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->distribution_hash); ptr->continuum[continuum_index].index= host_index; ptr->continuum[continuum_index++].value= value; } @@ -253,16 +256,17 @@ memcached_return_t update_continuum(memcached_st *ptr) if (list[host_index].port == MEMCACHED_DEFAULT_PORT) { sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, - "%s-%d", + "%s-%u", list[host_index].hostname, pointer_index - 1); } else { sort_host_length= (size_t) snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, - "%s:%d-%d", + "%s:%u-%u", list[host_index].hostname, - list[host_index].port, pointer_index - 1); + (uint32_t)list[host_index].port, + pointer_index - 1); } WATCHPOINT_ASSERT(sort_host_length); @@ -279,7 +283,7 @@ memcached_return_t update_continuum(memcached_st *ptr) } else { - value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum); + value= memcached_generate_hash_value(sort_host, sort_host_length, ptr->distribution_hash); ptr->continuum[continuum_index].index= host_index; ptr->continuum[continuum_index++].value= value; } @@ -319,7 +323,7 @@ memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st new_host_list= ptr->call_realloc(ptr, ptr->hosts, sizeof(memcached_server_st) * (count + ptr->number_of_hosts)); - if (!new_host_list) + if (! new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; ptr->hosts= new_host_list;