X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fhosts.cc;h=db6398914a699472d6163c3d4327b9f882e93055;hb=3a1352a68657a7506f37eab1f91057912063aa69;hp=d38b12e9337d17281c1c66e412863fda68ebd0e9;hpb=088eed04d829b8cf6591201564118c356d3cf935;p=awesomized%2Flibmemcached diff --git a/libmemcached/hosts.cc b/libmemcached/hosts.cc index d38b12e9..db639891 100644 --- a/libmemcached/hosts.cc +++ b/libmemcached/hosts.cc @@ -1,16 +1,44 @@ -/* LibMemcached - * Copyright (C) 2006-2010 Brian Aker - * All rights reserved. +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2006-2010 Brian Aker All rights reserved. * - * Summary: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -#include "common.h" -#include +#include + +#include +#include /* Protoypes (static) */ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, @@ -112,10 +140,9 @@ static memcached_return_t update_continuum(memcached_st *ptr) uint32_t live_servers= 0; struct timeval now; - if (gettimeofday(&now, NULL) != 0) + if (gettimeofday(&now, NULL)) { - memcached_set_errno(ptr, errno, NULL); - return MEMCACHED_ERRNO; + return memcached_set_errno(*ptr, errno, MEMCACHED_AT); } list= memcached_server_list(ptr); @@ -129,11 +156,15 @@ static memcached_return_t update_continuum(memcached_st *ptr) for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index) { if (list[host_index].next_retry <= now.tv_sec) + { live_servers++; + } else { if (ptr->ketama.next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->ketama.next_distribution_rebuild) + { ptr->ketama.next_distribution_rebuild= list[host_index].next_retry; + } } } } @@ -145,15 +176,17 @@ static memcached_return_t update_continuum(memcached_st *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); - if (live_servers == 0) + if (not live_servers) + { return MEMCACHED_SUCCESS; + } if (live_servers > ptr->ketama.continuum_count) { memcached_continuum_item_st *new_ptr; new_ptr= static_cast(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; @@ -232,7 +265,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } else { - uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length); + uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length); ptr->ketama.continuum[continuum_index].index= host_index; ptr->ketama.continuum[continuum_index++].value= value; } @@ -281,7 +314,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } else { - uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length); + uint32_t value= hashkit_digest(&ptr->hashkit, sort_host, (size_t)sort_host_length); ptr->ketama.continuum[continuum_index].index= host_index; ptr->ketama.continuum[continuum_index++].value= value; } @@ -292,7 +325,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); @@ -300,7 +333,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 @@ -311,7 +344,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); @@ -329,24 +364,28 @@ 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 (! 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, NULL); + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); } + + if (list[x].weight > 1) + { + ptr->ketama.weighted= true; + } + ptr->number_of_hosts++; } @@ -388,10 +427,10 @@ memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr, in_port_t port, uint32_t weight) { - if (! port) + if (not port) port= MEMCACHED_DEFAULT_PORT; - if (! hostname) + if (not hostname) hostname= "localhost"; return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP); @@ -409,13 +448,13 @@ memcached_return_t memcached_server_add_with_weight(memcached_st *ptr, in_port_t port, uint32_t weight) { - if (! port) + if (not port) port= MEMCACHED_DEFAULT_PORT; - if (! hostname) + 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, @@ -423,26 +462,37 @@ static memcached_return_t server_add(memcached_st *ptr, const char *hostname, uint32_t weight, memcached_connection_t type) { - memcached_server_st *new_host_list; - memcached_server_write_instance_st instance; - if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP) - || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) ) + if ( (ptr->flags.use_udp and type != MEMCACHED_CONNECTION_UDP) + or ( (type == MEMCACHED_CONNECTION_UDP) and (not ptr->flags.use_udp) ) ) + { return MEMCACHED_INVALID_HOST_PROTOCOL; + } - new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), - sizeof(memcached_server_st) * (ptr->number_of_hosts + 1))); + memcached_server_st *new_host_list= static_cast(libmemcached_realloc(ptr, memcached_server_list(ptr), + sizeof(memcached_server_st) * (ptr->number_of_hosts + 1))); - if (new_host_list == NULL) + if (not new_host_list) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; memcached_server_list_set(ptr, new_host_list); /* TODO: Check return type */ - instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); - (void)memcached_server_create_with(ptr, instance, hostname, port, weight, type); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr)); + + 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) + { + ptr->ketama.weighted= true; + } + 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));