From: Brian Aker Date: Mon, 28 Mar 2011 01:06:41 +0000 (-0700) Subject: Organize ketama a bit more (ie lets keep it in its own little basket). X-Git-Tag: 0.51~15^2~64 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=4b9854707a82ad7d6094e9d381f15e887196fe57;p=m6w6%2Flibmemcached Organize ketama a bit more (ie lets keep it in its own little basket). --- diff --git a/libmemcached/behavior.c b/libmemcached/behavior.c index 138e0f17..ab2c0230 100644 --- a/libmemcached/behavior.c +++ b/libmemcached/behavior.c @@ -94,20 +94,10 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, return memcached_behavior_set_distribution(ptr, (memcached_server_distribution_t)data); case MEMCACHED_BEHAVIOR_KETAMA: { - if (data) - { - (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5); - (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5); - (void)memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); - } - else - { - (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_DEFAULT); - (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_DEFAULT); - (void)memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA); - } + if (data) // Turn on + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA); - break; + return memcached_behavior_set_distribution(ptr, MEMCACHED_DISTRIBUTION_MODULA); } case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: { diff --git a/libmemcached/hash.c b/libmemcached/hash.c index ce47a448..99a00709 100644 --- a/libmemcached/hash.c +++ b/libmemcached/hash.c @@ -30,13 +30,13 @@ static uint32_t dispatch_host(const memcached_st *ptr, uint32_t hash) case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: { - uint32_t num= ptr->continuum_points_counter; + uint32_t num= ptr->ketama.continuum_points_counter; WATCHPOINT_ASSERT(ptr->continuum); hash= hash; memcached_continuum_item_st *begin, *end, *left, *right, *middle; - begin= left= ptr->continuum; - end= right= ptr->continuum + num; + begin= left= ptr->ketama.continuum; + end= right= ptr->ketama.continuum + num; while (left < right) { @@ -93,12 +93,12 @@ static inline uint32_t _generate_hash_wrapper(const memcached_st *ptr, const cha static inline void _regen_for_auto_eject(memcached_st *ptr) { - if (_is_auto_eject_host(ptr) && ptr->next_distribution_rebuild) + if (_is_auto_eject_host(ptr) && ptr->ketama.next_distribution_rebuild) { struct timeval now; if (gettimeofday(&now, NULL) == 0 && - now.tv_sec > ptr->next_distribution_rebuild) + now.tv_sec > ptr->ketama.next_distribution_rebuild) { run_distribution(ptr); } diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index 992bd763..709d86fd 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -130,15 +130,15 @@ static memcached_return_t update_continuum(memcached_st *ptr) if (is_auto_ejecting) { live_servers= 0; - ptr->next_distribution_rebuild= 0; + ptr->ketama.next_distribution_rebuild= 0; for (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->next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->next_distribution_rebuild) - ptr->next_distribution_rebuild= list[host_index].next_retry; + 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; } } } @@ -153,18 +153,18 @@ static memcached_return_t update_continuum(memcached_st *ptr) if (live_servers == 0) return MEMCACHED_SUCCESS; - if (live_servers > ptr->continuum_count) + if (live_servers > ptr->ketama.continuum_count) { memcached_continuum_item_st *new_ptr; - new_ptr= libmemcached_realloc(ptr, ptr->continuum, + new_ptr= libmemcached_realloc(ptr, ptr->ketama.continuum, sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server); if (new_ptr == 0) return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - ptr->continuum= new_ptr; - ptr->continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION; + ptr->ketama.continuum= new_ptr; + ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION; } if (is_ketama_weighted) @@ -232,15 +232,15 @@ static memcached_return_t update_continuum(memcached_st *ptr) for (uint32_t x= 0; x < pointer_per_hash; x++) { value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - ptr->continuum[continuum_index].index= host_index; - ptr->continuum[continuum_index++].value= value; + ptr->ketama.continuum[continuum_index].index= host_index; + ptr->ketama.continuum[continuum_index++].value= value; } } else { value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length); - ptr->continuum[continuum_index].index= host_index; - ptr->continuum[continuum_index++].value= value; + ptr->ketama.continuum[continuum_index].index= host_index; + ptr->ketama.continuum[continuum_index++].value= value; } } } @@ -281,15 +281,15 @@ static memcached_return_t update_continuum(memcached_st *ptr) for (uint32_t x = 0; x < pointer_per_hash; x++) { value= ketama_server_hash(sort_host, (size_t)sort_host_length, x); - ptr->continuum[continuum_index].index= host_index; - ptr->continuum[continuum_index++].value= value; + ptr->ketama.continuum[continuum_index].index= host_index; + ptr->ketama.continuum[continuum_index++].value= value; } } else { value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length); - ptr->continuum[continuum_index].index= host_index; - ptr->continuum[continuum_index++].value= value; + ptr->ketama.continuum[continuum_index].index= host_index; + ptr->ketama.continuum[continuum_index++].value= value; } } } @@ -300,8 +300,8 @@ static memcached_return_t update_continuum(memcached_st *ptr) WATCHPOINT_ASSERT(ptr); WATCHPOINT_ASSERT(ptr->continuum); WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE); - ptr->continuum_points_counter= pointer_counter; - qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp); + ptr->ketama.continuum_points_counter= pointer_counter; + qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp); #ifdef DEBUG for (pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++) diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index bc335dd8..8923a86d 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -51,7 +51,7 @@ static inline bool _memcached_init(memcached_st *self) if (! hash_ptr) return false; - self->continuum_points_counter= 0; + self->ketama.continuum_points_counter= 0; self->number_of_hosts= 0; self->servers= NULL; @@ -72,18 +72,18 @@ static inline bool _memcached_init(memcached_st *self) self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT; self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT; self->retry_timeout= 0; - self->continuum_count= 0; + self->ketama.continuum_count= 0; self->send_size= -1; self->recv_size= -1; self->user_data= NULL; - self->next_distribution_rebuild= 0; + self->ketama.next_distribution_rebuild= 0; self->number_of_replicas= 0; hash_ptr= hashkit_create(&self->distribution_hashkit); if (! hash_ptr) return false; - self->continuum= NULL; + self->ketama.continuum= NULL; self->allocators= memcached_allocators_return_default(); @@ -115,8 +115,8 @@ static void _free(memcached_st *ptr, bool release_st) if (ptr->on_cleanup) ptr->on_cleanup(ptr); - if (ptr->continuum) - libmemcached_free(ptr, ptr->continuum); + if (ptr->ketama.continuum) + libmemcached_free(ptr, ptr->ketama.continuum); memcached_array_free(ptr->prefix_key); ptr->prefix_key= NULL; diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index 193eb33c..622f9fb6 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -89,7 +89,6 @@ struct memcached_st { } flags; memcached_server_distribution_t distribution; hashkit_st hashkit; - uint32_t continuum_points_counter; // Ketama uint32_t number_of_hosts; memcached_server_st *servers; memcached_server_st *last_disconnected_server; @@ -104,15 +103,19 @@ struct memcached_st { int32_t poll_timeout; int32_t connect_timeout; int32_t retry_timeout; - uint32_t continuum_count; // Ketama int send_size; int recv_size; void *user_data; - time_t next_distribution_rebuild; // Ketama uint32_t number_of_replicas; hashkit_st distribution_hashkit; memcached_result_st result; - memcached_continuum_item_st *continuum; // Ketama + + struct { + uint32_t continuum_count; // Ketama + uint32_t continuum_points_counter; // Ketama + time_t next_distribution_rebuild; // Ketama + memcached_continuum_item_st *continuum; // Ketama + } ketama; struct _allocators_st { memcached_calloc_fn calloc; diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 4b588431..e675975c 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -2944,7 +2944,7 @@ static test_return_t user_supplied_bug18(memcached_st *trash) /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets * us test the boundary wraparound. */ - test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->ketama.continuum[0].index); /* verify the standard ketama set. */ for (x= 0; x < 99; x++) @@ -3105,7 +3105,7 @@ static test_return_t auto_eject_hosts(memcached_st *trash) instance= memcached_server_instance_by_position(memc, 2); ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) + 15; - memc->next_distribution_rebuild= time(NULL) - 1; + memc->ketama.next_distribution_rebuild= time(NULL) - 1; /* This would not work if there were only two hosts. @@ -3119,7 +3119,7 @@ static test_return_t auto_eject_hosts(memcached_st *trash) /* and re-added when it's back. */ ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) - 1; - memc->next_distribution_rebuild= time(NULL) - 1; + memc->ketama.next_distribution_rebuild= time(NULL) - 1; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, memc->distribution); for (size_t x= 0; x < 99; x++) @@ -4920,7 +4920,7 @@ static test_return_t ketama_compatibility_libmemcached(memcached_st *trash) /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets * us test the boundary wraparound. */ - test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->ketama.continuum[0].index); /* verify the standard ketama set. */ for (x= 0; x < 99; x++) @@ -4943,7 +4943,6 @@ static test_return_t ketama_compatibility_spymemcached(memcached_st *trash) { memcached_return_t rc; uint64_t value; - int x; memcached_server_st *server_pool; memcached_st *memc; @@ -4979,10 +4978,10 @@ static test_return_t ketama_compatibility_spymemcached(memcached_st *trash) /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets * us test the boundary wraparound. */ - test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + test_true(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->ketama.continuum[0].index); /* verify the standard ketama set. */ - for (x= 0; x < 99; x++) + for (uint32_t x= 0; x < 99; x++) { uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key));