more evenly then previous design).
#define MEMCACHED_MAX_KEY 251 /* We add one to have it null terminated */
#define MEMCACHED_MAX_BUFFER HUGE_STRING_LEN
#define MEMCACHED_MAX_HOST_LENGTH 64
+#define MEMCACHED_WHEEL_SIZE 1024
+#define MEMCACHED_STRIDE 4
typedef enum {
MEMCACHED_SUCCESS,
MEMCACHED_MAXIMUM_RETURN, /* Always add new error code before */
} memcached_return;
+typedef enum {
+ MEMCACHED_DISTRIBUTION_MODULUS,
+ MEMCACHED_DISTRIBUTION_CONSISTENT,
+} memcached_server_distribution;
+
typedef enum {
MEMCACHED_BEHAVIOR_NO_BLOCK,
MEMCACHED_BEHAVIOR_TCP_NODELAY,
int32_t poll_timeout;
memcached_string_st result_buffer;
memcached_hash hash;
+ memcached_server_distribution distribution;
+ unsigned int wheel[MEMCACHED_WHEEL_SIZE];
memcached_return warning; /* Future Use */
};
string_ptr= memcached_string_create(ptr, &ptr->result_buffer, 0);
WATCHPOINT_ASSERT(string_ptr);
ptr->poll_timeout= -1;
+ ptr->distribution= MEMCACHED_DISTRIBUTION_MODULUS;
return ptr;
}
new_clone->send_size= ptr->send_size;
new_clone->recv_size= ptr->recv_size;
new_clone->poll_timeout= ptr->poll_timeout;
+ new_clone->distribution= ptr->distribution;
return new_clone;
}
}
WATCHPOINT_ASSERT(hash);
- if (ptr->flags & MEM_USE_KETAMA)
+
+ if (ptr->distribution == MEMCACHED_DISTRIBUTION_MODULUS)
{
- WATCHPOINT_ASSERT(0);
- return 0;
+ return hash % ptr->number_of_hosts;
}
else
{
unsigned int server_key;
- server_key= hash % ptr->number_of_hosts;
+ server_key= hash % MEMCACHED_WHEEL_SIZE;
- return server_key;
+ return ptr->wheel[server_key];
}
}
unsigned int port,
memcached_connection type);
+#define MEMCACHED_WHEEL_SIZE 1024
+#define MEMCACHED_STRIDE 4
+static void rebalance_wheel(memcached_st *ptr)
+{
+ unsigned int x;
+ unsigned int y;
+ unsigned int latch;
+ unsigned int range;
+
+ range= (MEMCACHED_WHEEL_SIZE / ptr->number_of_hosts);
+
+ /* Seed the Wheel */
+ memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE);
+
+ for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++)
+ {
+ if (latch == MEMCACHED_STRIDE)
+ {
+ y++;
+ if (y == ptr->number_of_hosts)
+ y= 0;
+ latch= 0;
+ }
+
+ ptr->wheel[x]= y;
+ }
+}
+
static void host_reset(memcached_server_st *host, char *hostname, unsigned int port,
memcached_connection type)
{
}
ptr->hosts[0].count= ptr->number_of_hosts;
+ rebalance_wheel(ptr);
+
return MEMCACHED_SUCCESS;
}
ptr->number_of_hosts++;
ptr->hosts[0].count++;
+ rebalance_wheel(ptr);
+
LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
return MEMCACHED_SUCCESS;