X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fhosts.cc;h=80ba0339e5e41b175570a0f66557110a597e2191;hb=d86bfe0802400ad5ebc14a72941d8528a0f795b9;hp=dbfba007bd8d93c9c4d2e648e19e67da4ceab69b;hpb=674c7578fa870c3b57e81e765c355ce98434b310;p=awesomized%2Flibmemcached diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index dbfba007..80ba0339 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -36,6 +36,7 @@ */ #include +#include "libmemcached/assert.hpp" #include #include @@ -62,10 +63,7 @@ static void sort_hosts(memcached_st *ptr) { if (memcached_server_count(ptr)) { - qsort(memcached_instance_list(ptr), memcached_server_count(ptr), sizeof(org::libmemcached::Instance), compare_servers); - org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0); - instance->number_of_hosts= memcached_server_count(ptr); } } @@ -121,11 +119,17 @@ static int continuum_item_cmp(const void *t1, const void *t2) /* Why 153? Hmmm... */ WATCHPOINT_ASSERT(ct1->value != 153); if (ct1->value == ct2->value) + { return 0; + } else if (ct1->value > ct2->value) + { return 1; + } else + { return -1; + } } static memcached_return_t update_continuum(memcached_st *ptr) @@ -170,8 +174,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) live_servers= memcached_server_count(ptr); } - uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); - uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER); + 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) { @@ -192,9 +195,10 @@ static memcached_return_t update_continuum(memcached_st *ptr) ptr->ketama.continuum= new_ptr; ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION; } + assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum"); uint64_t total_weight= 0; - if (is_ketama_weighted) + if (memcached_is_weighted_ketama(ptr)) { for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index) { @@ -212,7 +216,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) continue; } - if (is_ketama_weighted) + if (memcached_is_weighted_ketama(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); @@ -256,7 +260,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) fprintf(stdout, "update_continuum: key is %s\n", sort_host); } - if (is_ketama_weighted) + if (memcached_is_weighted_ketama(ptr)) { for (uint32_t x= 0; x < pointer_per_hash; x++) { @@ -304,7 +308,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) memcached_literal_param("snprintf(sizeof(sort_host)))")); } - if (is_ketama_weighted) + if (memcached_is_weighted_ketama(ptr)) { for (uint32_t x = 0; x < pointer_per_hash; x++) { @@ -325,9 +329,9 @@ static memcached_return_t update_continuum(memcached_st *ptr) pointer_counter+= pointer_per_server; } - WATCHPOINT_ASSERT(ptr); - WATCHPOINT_ASSERT(ptr->ketama.continuum); - WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE); + assert_msg(ptr, "Programmer Error, no valid ptr"); + assert_msg(ptr->ketama.continuum, "Programmer Error, empty ketama continuum"); + assert_msg(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE, "invalid size information being given to qsort()"); ptr->ketama.continuum_points_counter= pointer_counter; qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp); @@ -342,43 +346,52 @@ static memcached_return_t update_continuum(memcached_st *ptr) return MEMCACHED_SUCCESS; } -static memcached_return_t server_add(memcached_st *ptr, +static memcached_return_t server_add(memcached_st *memc, const memcached_string_t& hostname, in_port_t port, uint32_t weight, memcached_connection_t type) { - assert_msg(ptr, "Programmer mistake, somehow server_add() was passed a NULL memcached_st"); + assert_msg(memc, "Programmer mistake, somehow server_add() was passed a NULL memcached_st"); - org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (ptr->number_of_hosts + 1), org::libmemcached::Instance); + if (memc->number_of_hosts) + { + assert(memcached_instance_list(memc)); + } + + if (memcached_instance_list(memc)) + { + assert(memc->number_of_hosts); + } + + 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); if (new_host_list == NULL) { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } - memcached_instance_set(ptr, new_host_list); + memcached_instance_set(memc, new_host_list, host_list_size); + assert(memc->number_of_hosts == host_list_size); /* TODO: Check return type */ - org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, memcached_server_count(ptr)); + org::libmemcached::Instance* instance= memcached_instance_fetch(memc, memcached_server_count(memc) -1); - if (__instance_create_with(ptr, instance, hostname, port, weight, type) == NULL) + if (__instance_create_with(memc, instance, hostname, port, weight, type) == NULL) { - return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + return memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } if (weight > 1) { - ptr->ketama.weighted= true; + if (memcached_is_consistent_distribution(memc)) + { + memcached_set_weighted_ketama(memc, true); + } } - ptr->number_of_hosts++; - - // @note we place the count in the bottom of the server list - instance= memcached_instance_fetch(ptr, 0); - memcached_instance_set_count(instance, memcached_server_count(ptr)); - - return run_distribution(ptr); + return run_distribution(memc); } @@ -389,23 +402,26 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv 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), (count + memcached_server_count(ptr)), org::libmemcached::Instance); + 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); + memcached_instance_set(ptr, new_host_list, host_list_size); - for (uint32_t x= 0; x < count; x++) + ptr->state.is_parsing= true; + for (uint32_t x= 0; x < count; ++x, ++original_host_size) { 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, memcached_server_count(ptr)); + org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size); WATCHPOINT_ASSERT(instance); memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) }; @@ -413,22 +429,16 @@ memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_serv 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) { - ptr->ketama.weighted= true; + memcached_set_weighted_ketama(ptr, true); } - - ptr->number_of_hosts++; - } - - // Provides backwards compatibility with server list. - { - org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0); - instance->number_of_hosts= memcached_server_count(ptr); } + ptr->state.is_parsing= false; return run_distribution(ptr); } @@ -440,22 +450,28 @@ memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org:: return MEMCACHED_SUCCESS; } - org::libmemcached::Instance* new_host_list= libmemcached_xrealloc(ptr, memcached_instance_list(ptr), (number_of_hosts +memcached_server_count(ptr)), org::libmemcached::Instance); + 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); if (new_host_list == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - memcached_instance_set(ptr, new_host_list); + memcached_instance_set(ptr, new_host_list, host_list_size); - for (uint32_t x= 0; x < number_of_hosts; x++) - { + // We don't bother with lookups for this operation + ptr->state.is_parsing= true; + // We use original_host_size since size will now point to the first new + // instance allocated. + for (uint32_t x= 0; x < number_of_hosts; ++x, ++original_host_size) + { 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, memcached_server_count(ptr)); + org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, original_host_size); WATCHPOINT_ASSERT(instance); memcached_string_t hostname= { memcached_string_make_from_cstr(list[x].hostname) }; @@ -463,22 +479,16 @@ memcached_return_t memcached_instance_push(memcached_st *ptr, const struct org:: 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) { - ptr->ketama.weighted= true; + memcached_set_weighted_ketama(ptr, true); } - - ptr->number_of_hosts++; - } - - // Provides backwards compatibility with server list. - { - org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0); - instance->number_of_hosts= memcached_server_count(ptr); } + ptr->state.is_parsing= false; return run_distribution(ptr); } @@ -499,9 +509,9 @@ memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *pt } memcached_string_t _filename= { memcached_string_make_from_cstr(filename) }; - if (memcached_is_valid_servername(_filename) == false) + if (memcached_is_valid_filename(_filename) == false) { - memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid filename for socket provided")); + 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); @@ -572,7 +582,7 @@ memcached_return_t memcached_server_add_parsed(memcached_st *ptr, in_port_t port, uint32_t weight) { - char buffer[NI_MAXHOST]; + char buffer[MEMCACHED_NI_MAXHOST]; memcpy(buffer, hostname, hostname_length); buffer[hostname_length]= 0;