-#include "common.h"
+#include "common.h"
+#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
-/*
- This function is used to modify the behabior of running client.
+/*
+ This function is used to modify the behavior of running client.
We quit all connections so we can reset the sockets.
*/
-void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, uint64_t data)
+static void set_behavior_flag(memcached_st *ptr, memcached_flags temp_flag, uint64_t data)
{
if (data)
ptr->flags|= temp_flag;
ptr->flags&= ~temp_flag;
}
-memcached_return memcached_behavior_set(memcached_st *ptr,
- memcached_behavior flag,
+memcached_return memcached_behavior_set(memcached_st *ptr,
+ memcached_behavior flag,
uint64_t data)
{
switch (flag)
{
+ case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
+ ptr->number_of_replicas= (uint32_t)data;
+ break;
+ case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
+ ptr->io_msg_watermark= (uint32_t) data;
+ break;
+ case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
+ ptr->io_bytes_watermark= (uint32_t)data;
+ break;
+ case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
+ ptr->io_key_prefetch = (uint32_t)data;
+ break;
case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
ptr->snd_timeout= (int32_t)data;
- break;
+ break;
case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
ptr->rcv_timeout= (int32_t)data;
- break;
+ break;
case MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT:
ptr->server_failure_limit= (uint32_t)data;
- break;
+ break;
case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
+ if (data)
+ set_behavior_flag(ptr, MEM_VERIFY_KEY, 0);
set_behavior_flag(ptr, MEM_BINARY_PROTOCOL, data);
- break;
+ break;
case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
set_behavior_flag(ptr, MEM_SUPPORT_CAS, data);
break;
case MEMCACHED_BEHAVIOR_NO_BLOCK:
set_behavior_flag(ptr, MEM_NO_BLOCK, data);
memcached_quit(ptr);
+ break;
case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
set_behavior_flag(ptr, MEM_BUFFER_REQUESTS, data);
memcached_quit(ptr);
break;
+ case MEMCACHED_BEHAVIOR_USE_UDP:
+ if (ptr->number_of_hosts)
+ return MEMCACHED_FAILURE;
+ set_behavior_flag(ptr, MEM_USE_UDP, data);
+ if (data)
+ set_behavior_flag(ptr,MEM_NOREPLY,data);
+ break;
case MEMCACHED_BEHAVIOR_TCP_NODELAY:
set_behavior_flag(ptr, MEM_TCP_NODELAY, data);
memcached_quit(ptr);
case MEMCACHED_BEHAVIOR_DISTRIBUTION:
{
ptr->distribution= (memcached_server_distribution)(data);
+ if (ptr->distribution == MEMCACHED_DISTRIBUTION_RANDOM)
+ {
+ srandom((uint32_t) time(NULL));
+ }
run_distribution(ptr);
break;
}
run_distribution(ptr);
break;
}
+ case MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE:
+ switch (data)
+ {
+ case MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED:
+ ptr->hash= MEMCACHED_HASH_MD5;
+ ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA;
+ break;
+ case MEMCACHED_KETAMA_COMPAT_SPY:
+ ptr->hash= MEMCACHED_HASH_MD5;
+ ptr->distribution= MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY;
+ break;
+ default:
+ return MEMCACHED_FAILURE;
+ }
+ run_distribution(ptr);
+ break;
case MEMCACHED_BEHAVIOR_HASH:
+#ifndef HAVE_HSIEH_HASH
+ if ((memcached_hash)(data) == MEMCACHED_HASH_HSIEH)
+ return MEMCACHED_FAILURE;
+#endif
ptr->hash= (memcached_hash)(data);
break;
case MEMCACHED_BEHAVIOR_KETAMA_HASH:
memcached_quit(ptr);
break;
case MEMCACHED_BEHAVIOR_VERIFY_KEY:
+ if (ptr->flags & MEM_BINARY_PROTOCOL)
+ break;
set_behavior_flag(ptr, MEM_VERIFY_KEY, data);
break;
case MEMCACHED_BEHAVIOR_SORT_HOSTS:
break;
case MEMCACHED_BEHAVIOR_USER_DATA:
return MEMCACHED_FAILURE;
+ case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
+ set_behavior_flag(ptr, MEM_HASH_WITH_PREFIX_KEY, data);
+ break;
+ case MEMCACHED_BEHAVIOR_NOREPLY:
+ set_behavior_flag(ptr, MEM_NOREPLY, data);
+ break;
+ case MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS:
+ 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:
+ /* Shouldn't get here */
+ WATCHPOINT_ASSERT(flag);
+ break;
}
return MEMCACHED_SUCCESS;
}
-uint64_t memcached_behavior_get(memcached_st *ptr,
+uint64_t memcached_behavior_get(memcached_st *ptr,
memcached_behavior flag)
{
- memcached_flags temp_flag= 0;
+ memcached_flags temp_flag= MEM_NO_BLOCK;
switch (flag)
{
+ case MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS:
+ return ptr->number_of_replicas;
+ case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
+ return ptr->io_msg_watermark;
+ case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
+ return ptr->io_bytes_watermark;
+ case MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH:
+ return ptr->io_key_prefetch;
case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
temp_flag= MEM_BINARY_PROTOCOL;
- break;
+ break;
case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
temp_flag= MEM_SUPPORT_CAS;
break;
case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
temp_flag= MEM_BUFFER_REQUESTS;
break;
+ case MEMCACHED_BEHAVIOR_USE_UDP:
+ temp_flag= MEM_USE_UDP;
+ break;
case MEMCACHED_BEHAVIOR_TCP_NODELAY:
temp_flag= MEM_TCP_NODELAY;
break;
case MEMCACHED_BEHAVIOR_DISTRIBUTION:
return ptr->distribution;
case MEMCACHED_BEHAVIOR_KETAMA:
- return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? 1 : 0;
+ return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0;
+ case MEMCACHED_BEHAVIOR_KETAMA_COMPAT_MODE:
+ switch (ptr->distribution)
+ {
+ case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
+ return MEMCACHED_KETAMA_COMPAT_LIBMEMCACHED;
+ case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
+ return MEMCACHED_KETAMA_COMPAT_SPY;
+ default:
+ return (uint64_t)-1;
+ }
+ /* NOTREACHED */
case MEMCACHED_BEHAVIOR_HASH:
return ptr->hash;
case MEMCACHED_BEHAVIOR_KETAMA_HASH:
temp_flag= MEM_USE_SORT_HOSTS;
break;
case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
- {
- return (unsigned long long)ptr->poll_timeout;
- }
+ return (uint64_t)ptr->poll_timeout;
case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
- {
- return (unsigned long long)ptr->connect_timeout;
- }
+ return (uint64_t)ptr->connect_timeout;
case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
- {
- return (unsigned long long)ptr->retry_timeout;
- }
+ return (uint64_t)ptr->retry_timeout;
+ case MEMCACHED_BEHAVIOR_SND_TIMEOUT:
+ return (uint64_t)ptr->snd_timeout;
+ case MEMCACHED_BEHAVIOR_RCV_TIMEOUT:
+ return (uint64_t)ptr->rcv_timeout;
case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
{
int sock_size;
if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
return 0;
- if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
+ if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
SO_SNDBUF, &sock_size, &sock_length))
return 0; /* Zero means error */
- return sock_size;
+ return (uint64_t) sock_size;
}
case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
{
if ((memcached_connect(&ptr->hosts[0])) != MEMCACHED_SUCCESS)
return 0;
- if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
+ if (getsockopt(ptr->hosts[0].fd, SOL_SOCKET,
SO_RCVBUF, &sock_size, &sock_length))
return 0; /* Zero means error */
- return sock_size;
+ return (uint64_t) sock_size;
}
case MEMCACHED_BEHAVIOR_USER_DATA:
return MEMCACHED_FAILURE;
+ case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
+ temp_flag= MEM_HASH_WITH_PREFIX_KEY;
+ break;
+ case MEMCACHED_BEHAVIOR_NOREPLY:
+ temp_flag= MEM_NOREPLY;
+ break;
+ 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;
}
WATCHPOINT_ASSERT(temp_flag); /* Programming mistake if it gets this far */
return 1;
else
return 0;
-
- return MEMCACHED_SUCCESS;
}