From: Mikko Koppanen Date: Fri, 27 Nov 2009 10:40:32 +0000 (+0000) Subject: Added randomized read behavior X-Git-Tag: 0.37~84^2~2 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=a758089c1958d9298ee358d1869d10b1ab79f30c;p=m6w6%2Flibmemcached Added randomized read behavior --- diff --git a/libmemcached/common.h b/libmemcached/common.h index b46d5ba0..99d4ce78 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -93,7 +93,8 @@ typedef enum { MEM_HASH_WITH_PREFIX_KEY= (1 << 13), MEM_NOREPLY= (1 << 14), MEM_USE_UDP= (1 << 15), - MEM_AUTO_EJECT_HOSTS= (1 << 16) + MEM_AUTO_EJECT_HOSTS= (1 << 16), + MEM_RANDOMIZE_REPLICA_READ= (1 << 17) } memcached_flags; /* Hashing algo */ diff --git a/libmemcached/memcached_behavior.c b/libmemcached/memcached_behavior.c index 72b0d5cc..7196a066 100644 --- a/libmemcached/memcached_behavior.c +++ b/libmemcached/memcached_behavior.c @@ -176,6 +176,9 @@ memcached_return memcached_behavior_set(memcached_st *ptr, case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: set_behavior_flag(ptr, MEM_AUTO_EJECT_HOSTS, data); break; + case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: + set_behavior_flag(ptr, MEM_RANDOMIZE_REPLICA_READ, data); + break; default: /* Shouldn't get here */ WATCHPOINT_ASSERT(flag); @@ -304,6 +307,9 @@ uint64_t memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS: temp_flag= MEM_AUTO_EJECT_HOSTS; break; + case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: + temp_flag= MEM_RANDOMIZE_REPLICA_READ; + break; default: WATCHPOINT_ASSERT(flag); break; diff --git a/libmemcached/memcached_constants.h b/libmemcached/memcached_constants.h index 7399d6cb..b19f03c7 100644 --- a/libmemcached/memcached_constants.h +++ b/libmemcached/memcached_constants.h @@ -106,7 +106,8 @@ typedef enum { MEMCACHED_BEHAVIOR_USE_UDP, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS, MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, - MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE + MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE, + MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ } memcached_behavior; #define MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED 0 diff --git a/libmemcached/memcached_get.c b/libmemcached/memcached_get.c index 3436186f..6c730093 100644 --- a/libmemcached/memcached_get.c +++ b/libmemcached/memcached_get.c @@ -432,18 +432,35 @@ static memcached_return replication_binary_mget(memcached_st *ptr, size_t number_of_keys) { memcached_return rc= MEMCACHED_NOTFOUND; - uint32_t x; + uint32_t x, start = 0; + uint64_t randomize_read = memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ); + + if (randomize_read) { + srandom((uint32_t) time(NULL)); + start = (uint32_t)(random() % (ptr->number_of_replicas + 1)); + } + /* Loop for each replica */ for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica) { - bool success= true; - + bool success= true; + for (x= 0; x < number_of_keys; ++x) { + uint32_t server; + if (hash[x] == ptr->number_of_hosts) continue; /* Already successfully sent */ - uint32_t server= hash[x] + replica; + server= hash[x] + replica; + + /* In case of randomized reads */ + if (randomize_read) { + if ((server + start) <= (hash[x] + ptr->number_of_replicas)) { + server += start; + } + } + while (server >= ptr->number_of_hosts) server -= ptr->number_of_hosts;