From e49dc22293ec02994818ee9252ec5034a8e5e199 Mon Sep 17 00:00:00 2001 From: Mikko Koppanen Date: Fri, 27 Nov 2009 19:46:16 +0000 Subject: [PATCH] Coding style changes. Moved the srandom to memcached_behavior_set instead of doing it on every call. Added documentation --- docs/memcached_behavior.pod | 9 +++++++++ libmemcached/memcached_behavior.c | 1 + libmemcached/memcached_get.c | 21 +++++++-------------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/docs/memcached_behavior.pod b/docs/memcached_behavior.pod index dd718454..0288feb0 100644 --- a/docs/memcached_behavior.pod +++ b/docs/memcached_behavior.pod @@ -209,6 +209,15 @@ This replication does not dedicate certain memcached servers to store the replicas in, but instead it will store the replicas together with all of the other objects (on the 'n' next servers specified in your server list). +=item MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ + +Allows randomizing the replica reads starting point. Normally the read is +done from primary server and in case of miss the read is done from primary ++ 1, then primary + 2 all the way to 'n' replicas. If this option is set +on the starting point of the replica reads is randomized between the servers. +This allows distributing read load to multiple servers with the expense of +more write traffic. + =back =head1 RETURN diff --git a/libmemcached/memcached_behavior.c b/libmemcached/memcached_behavior.c index 7196a066..4f462f99 100644 --- a/libmemcached/memcached_behavior.c +++ b/libmemcached/memcached_behavior.c @@ -177,6 +177,7 @@ memcached_return memcached_behavior_set(memcached_st *ptr, set_behavior_flag(ptr, MEM_AUTO_EJECT_HOSTS, data); break; case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ: + srandom((uint32_t) time(NULL)); set_behavior_flag(ptr, MEM_RANDOMIZE_REPLICA_READ, data); break; default: diff --git a/libmemcached/memcached_get.c b/libmemcached/memcached_get.c index 6c730093..0aa0f44c 100644 --- a/libmemcached/memcached_get.c +++ b/libmemcached/memcached_get.c @@ -432,13 +432,11 @@ static memcached_return replication_binary_mget(memcached_st *ptr, size_t number_of_keys) { memcached_return rc= MEMCACHED_NOTFOUND; - uint32_t x, start = 0; - uint64_t randomize_read = memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ); + 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)); - } + if (randomize_read) + start= (uint32_t)(random() % (ptr->number_of_replicas + 1)); /* Loop for each replica */ for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica) @@ -447,19 +445,14 @@ static memcached_return replication_binary_mget(memcached_st *ptr, for (x= 0; x < number_of_keys; ++x) { - uint32_t server; - if (hash[x] == ptr->number_of_hosts) continue; /* Already successfully sent */ - server= hash[x] + replica; + uint32_t server= hash[x] + replica; /* In case of randomized reads */ - if (randomize_read) { - if ((server + start) <= (hash[x] + ptr->number_of_replicas)) { - server += start; - } - } + if (randomize_read && ((server + start) <= (hash[x] + ptr->number_of_replicas))) + server += start; while (server >= ptr->number_of_hosts) server -= ptr->number_of_hosts; -- 2.30.2