0.39
* Modify inline calls for compatibility.
+* The undocumented call memcached_server_by_key() now returns const.
+
0.38 Wed Feb 10 09:40:33 PST 2010
* C++ interface for libhashkit.
* Modified memcached_set_memory_allocators() so that it requires a context pointer.
else if (strcmp(analyze_mode, "latency") == 0)
{
memcached_st **servers;
- uint32_t x, y, flags, server_count= memcached_server_count(memc);
+ uint32_t flags, server_count= memcached_server_count(memc);
uint32_t num_of_tests= 32;
const char *test_key= "libmemcached_test_key";
return;
}
- for (x= 0; x < server_count; x++)
+ for (uint32_t x= 0; x < server_count; x++)
{
if((servers[x]= memcached_create(NULL)) == NULL)
{
return;
}
memcached_server_add(servers[x],
- memcached_server_name(memc, server_list[x]),
- memcached_server_port(memc, server_list[x]));
+ memcached_server_name(&server_list[x]),
+ memcached_server_port(&server_list[x]));
}
printf("Network Latency Test:\n\n");
struct timeval start_time, end_time;
long elapsed_time, slowest_time= 0, slowest_server= 0;
- for (x= 0; x < server_count; x++)
+ for (uint32_t x= 0; x < server_count; x++)
{
gettimeofday(&start_time, NULL);
- for (y= 0; y < num_of_tests; y++)
+ for (uint32_t y= 0; y < num_of_tests; y++)
{
size_t vlen;
char *val= memcached_get(servers[x], test_key, strlen(test_key),
if (rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_SUCCESS)
{
printf("\t %s (%d) => failed to reach the server\n",
- memcached_server_name(memc, server_list[x]),
- memcached_server_port(memc, server_list[x]));
+ memcached_server_name(&server_list[x]),
+ memcached_server_port(&server_list[x]));
}
else
{
printf("\t %s (%d) => %ld.%ld seconds\n",
- memcached_server_name(memc, server_list[x]),
- memcached_server_port(memc, server_list[x]),
+ memcached_server_name(&server_list[x]),
+ memcached_server_port(&server_list[x]),
elapsed_time / 1000, elapsed_time % 1000);
}
}
{
printf("---\n");
printf("Slowest Server: %s (%d) => %ld.%ld seconds\n",
- memcached_server_name(memc, server_list[slowest_server]),
- memcached_server_port(memc, server_list[slowest_server]),
+ memcached_server_name(&server_list[slowest_server]),
+ memcached_server_port(&server_list[slowest_server]),
slowest_time / 1000, slowest_time % 1000);
}
printf("\n");
- for (x= 0; x < server_count; x++)
+ for (uint32_t x= 0; x < server_count; x++)
memcached_free(servers[x]);
free(servers);
static void print_server_listing(memcached_st *memc, memcached_stat_st *memc_stat,
memcached_server_st *server_list)
{
- unsigned int x;
memcached_return_t rc;
printf("Listing %u Server\n\n", memcached_server_count(memc));
- for (x= 0; x < memcached_server_count(memc); x++)
+ for (uint32_t x= 0; x < memcached_server_count(memc); x++)
{
char **list;
char **ptr;
list= memcached_stat_get_keys(memc, &memc_stat[x], &rc);
- printf("Server: %s (%u)\n", memcached_server_name(memc, server_list[x]),
- (uint32_t)memcached_server_port(memc, server_list[x]));
+ printf("Server: %s (%u)\n", memcached_server_name(&server_list[x]),
+ (uint32_t)memcached_server_port(&server_list[x]));
for (ptr= list; *ptr; ptr++)
{
char *value= memcached_stat_get_value(memc, &memc_stat[x], *ptr, &rc);
printf("\n");
printf("\tNode with most memory consumption : %s:%u (%llu bytes)\n",
- memcached_server_name(memc, server_list[report->most_consumed_server]),
- (uint32_t)memcached_server_port(memc, server_list[report->most_consumed_server]),
+ memcached_server_name(&server_list[report->most_consumed_server]),
+ (uint32_t)memcached_server_port(&server_list[report->most_consumed_server]),
(unsigned long long)report->most_used_bytes);
printf("\tNode with least free space : %s:%u (%llu bytes remaining)\n",
- memcached_server_name(memc, server_list[report->least_free_server]),
- (uint32_t)memcached_server_port(memc, server_list[report->least_free_server]),
+ memcached_server_name(&server_list[report->least_free_server]),
+ (uint32_t)memcached_server_port(&server_list[report->least_free_server]),
(unsigned long long)report->least_remaining_bytes);
printf("\tNode with longest uptime : %s:%u (%us)\n",
- memcached_server_name(memc, server_list[report->oldest_server]),
- (uint32_t)memcached_server_port(memc, server_list[report->oldest_server]),
+ memcached_server_name(&server_list[report->oldest_server]),
+ (uint32_t)memcached_server_port(&server_list[report->oldest_server]),
report->longest_uptime);
printf("\tPool-wide Hit Ratio : %1.f%%\n", report->pool_hit_ratio);
printf("\n");
memcached_return_t rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
uint32_t server_key;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
bool no_reply= ptr->flags.no_reply;
unlikely (memcached_server_count(ptr) == 0)
if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
return MEMCACHED_BAD_KEY_PROVIDED;
- server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
instance= memcached_server_instance_fetch(ptr, server_key);
send_length= (size_t)snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
uint64_t *value)
{
uint32_t server_key;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
bool no_reply= ptr->flags.no_reply;
unlikely (memcached_server_count(ptr) == 0)
return MEMCACHED_NO_SERVERS;
- server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
instance= memcached_server_instance_fetch(ptr, server_key);
if (no_reply)
break;
case MEMCACHED_BEHAVIOR_CORK:
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
bool action= set_flag(data);
if (action == false)
return MEMCACHED_SUCCESS;
}
+inline bool _is_auto_eject_host(const memcached_st *ptr)
+{
+ return ptr->flags.auto_eject_hosts;
+}
+
uint64_t memcached_behavior_get(memcached_st *ptr,
const memcached_behavior_t flag)
{
{
int sock_size= 0;
socklen_t sock_length= sizeof(int);
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
if (ptr->send_size != -1) // If value is -1 then we are using the default
return (uint64_t) ptr->send_size;
{
int sock_size= 0;
socklen_t sock_length= sizeof(int);
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
if (ptr->recv_size != -1) // If value is -1 then we are using the default
return (uint64_t) ptr->recv_size;
LIBMEMCACHED_API
memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr);
+LIBMEMCACHED_LOCAL
+bool _is_auto_eject_host(const memcached_st *ptr);
+
#ifdef __cplusplus
}
#include "libmemcached/memcached.h"
#include "libmemcached/watchpoint.h"
-typedef struct memcached_server_st memcached_server_instance_st;
+typedef struct memcached_server_st * memcached_server_write_instance_st;
+
+LIBMEMCACHED_LOCAL
+memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key);
/* These are private not to be installed headers */
#include "libmemcached/do.h"
#endif
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_connect(memcached_server_instance_st *ptr);
-
-LIBMEMCACHED_LOCAL
-void memcached_quit_server(memcached_server_instance_st *ptr, bool io_death);
+memcached_return_t memcached_connect(memcached_server_write_instance_st ptr);
LIBMEMCACHED_LOCAL
memcached_return_t run_distribution(memcached_st *ptr);
memcached_return_t value_fetch(memcached_server_instance_st *ptr,
char *buffer,
memcached_result_st *result);
-LIBMEMCACHED_LOCAL
-void server_list_free(memcached_st *ptr, memcached_server_instance_st *servers);
-
LIBMEMCACHED_LOCAL
memcached_return_t memcached_key_test(const char * const *keys,
const size_t *key_length,
size_t number_of_keys);
LIBMEMCACHED_LOCAL
-uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length);
+memcached_return_t memcached_purge(memcached_server_write_instance_st ptr);
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_purge(memcached_server_instance_st *ptr);
+memcached_server_st *memcached_server_create_with(const memcached_st *memc,
+ memcached_server_write_instance_st host,
+ const char *hostname,
+ in_port_t port,
+ uint32_t weight,
+ memcached_connection_t type);
+
static inline memcached_return_t memcached_validate_key_length(size_t key_length, bool binary)
{
}
-memcached_return_t memcached_connect(memcached_server_st *ptr)
+memcached_return_t memcached_connect(memcached_server_write_instance_st ptr)
{
memcached_return_t rc= MEMCACHED_NO_SERVERS;
LIBMEMCACHED_MEMCACHED_CONNECT_START();
memcached_return_t rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
uint32_t server_key;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
LIBMEMCACHED_MEMCACHED_DELETE_START();
unlikely (memcached_server_count(ptr) == 0)
return MEMCACHED_NO_SERVERS;
- server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
instance= memcached_server_instance_fetch(ptr, server_key);
to_write= (ptr->flags.buffer_requests) ? false : true;
size_t key_length,
bool flush)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
protocol_binary_request_delete request= {.bytes= {0}};
instance= memcached_server_instance_fetch(ptr, server_key);
for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
{
- memcached_server_instance_st *replica;
+ memcached_server_write_instance_st replica;
++server_key;
if (server_key == memcached_server_count(ptr))
#include "common.h"
-memcached_return_t memcached_do(memcached_server_st *ptr, const void *command,
+memcached_return_t memcached_do(memcached_server_write_instance_st ptr, const void *command,
size_t command_length, bool with_flush)
{
memcached_return_t rc;
#endif
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_do(memcached_server_instance_st *ptr, const void *commmand,
- size_t command_length, bool with_flush);
+memcached_return_t memcached_do(memcached_server_write_instance_st ptr,
+ const void *commmand,
+ size_t command_length,
+ bool with_flush);
#ifdef __cplusplus
}
for (server_key= 0; server_key < memcached_server_count(ptr); server_key++)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
instance= memcached_server_instance_fetch(ptr, server_key);
/* 256 I BELIEVE is the upper limit of slabs */
if ((result= memcached_result_create(ptr, NULL)) == NULL)
return NULL;
- while ((server = memcached_io_get_readable_server(ptr)) != NULL)
+ while ((server= memcached_io_get_readable_server(ptr)) != NULL)
{
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
*error= memcached_response(server, buffer, sizeof(buffer), result);
for (x= 0; x < memcached_server_count(ptr); x++)
{
bool no_reply= ptr->flags.no_reply;
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (expiration)
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (ptr->flags.no_reply)
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance) > 0)
for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(memc, x);
if (instance->write_buffer_offset != 0)
{
if (ptr->flags.verify_key && (memcached_key_test((const char * const *)&master_key, &master_key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
return MEMCACHED_BAD_KEY_PROVIDED;
- master_server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ master_server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
is_master_key_set= true;
}
*/
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance))
*/
for (x= 0; x < number_of_keys; x++)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
uint32_t server_key;
if (is_master_key_set)
}
else
{
- server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
+ server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
}
instance= memcached_server_instance_fetch(ptr, server_key);
*/
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance))
for (x= 0; x < number_of_keys; x++)
{
uint32_t server_key;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
if (is_master_key_set)
{
}
else
{
- server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
+ server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
}
instance= memcached_server_instance_fetch(ptr, server_key);
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance))
for (x= 0; x < number_of_keys; ++x)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
if (hash[x] == memcached_server_count(ptr))
continue; /* Already successfully sent */
{
for (size_t x= 0; x < number_of_keys; x++)
{
- hash[x]= memcached_generate_hash(ptr, keys[x], key_length[x]);
+ hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
}
}
return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm);
}
-uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length)
+static inline uint32_t generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
{
- uint32_t hash= 1; /* Just here to remove compile warning */
-
-
- WATCHPOINT_ASSERT(memcached_server_count(ptr));
-
- if (memcached_server_count(ptr) == 1)
- return 0;
-
- hash= hashkit_digest(&ptr->hashkit, key, key_length);
-
- return hash;
+ return hashkit_digest(&ptr->hashkit, key, key_length);
}
-static uint32_t dispatch_host(memcached_st *ptr, uint32_t hash)
+static uint32_t dispatch_host(const memcached_st *ptr, uint32_t hash)
{
switch (ptr->distribution)
{
}
/*
- One day make this public, and have it return the actual memcached_server_st
- to the calling application.
+ One version is public and will not modify the distribution hash, the other will.
*/
-uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length)
+static inline uint32_t _generate_hash_wrapper(const memcached_st *ptr, const char *key, size_t key_length)
{
- uint32_t hash= 1; /* Just here to remove compile warning */
-
WATCHPOINT_ASSERT(memcached_server_count(ptr));
if (memcached_server_count(ptr) == 1)
strncpy(temp, ptr->prefix_key, ptr->prefix_key_length);
strncpy(temp + ptr->prefix_key_length, key, key_length);
- hash= generate_hash(ptr, temp, temp_length);
+
+ return generate_hash(ptr, temp, temp_length);
}
else
{
- hash= generate_hash(ptr, key, key_length);
+ return generate_hash(ptr, key, key_length);
}
+}
- if (memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS) && ptr->next_distribution_rebuild)
+static inline void _regen_for_auto_eject(memcached_st *ptr)
+{
+ if (_is_auto_eject_host(ptr) && ptr->next_distribution_rebuild)
{
struct timeval now;
run_distribution(ptr);
}
}
+}
+
+void memcached_autoeject(memcached_st *ptr)
+{
+ _regen_for_auto_eject(ptr);
+}
+
+uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length)
+{
+ uint32_t hash= _generate_hash_wrapper(ptr, key, key_length);
+
+ _regen_for_auto_eject(ptr);
return dispatch_host(ptr, hash);
}
+uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
+{
+ return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length));
+}
+
hashkit_st *memcached_get_hashkit(memcached_st *ptr)
{
return &ptr->hashkit;
hashkit_st *memcached_get_hashkit(memcached_st *ptr);
LIBMEMCACHED_API
-uint32_t memcached_generate_hash(memcached_st *ptr, const char *key, size_t key_length);
+uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length);
+LIBMEMCACHED_LOCAL
+uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length);
+
+LIBMEMCACHED_API
+void memcached_autoeject(memcached_st *ptr);
#ifdef __cplusplus
}
{
if (memcached_server_count(ptr))
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_instance_st), compare_servers);
instance= memcached_server_instance_fetch(ptr, 0);
uint32_t host_index;
uint32_t continuum_index= 0;
uint32_t value;
- memcached_server_instance_st *list;
+ memcached_server_st *list;
uint32_t pointer_index;
uint32_t pointer_counter= 0;
uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
list = memcached_server_list(ptr);
/* count live servers (those without a retry delay set) */
- is_auto_ejecting= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS);
+ is_auto_ejecting= _is_auto_eject_host(ptr);
if (is_auto_ejecting)
{
live_servers= 0;
{
list[host_index].weight = 1;
}
- if (!is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
+ if (! is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
total_weight += list[host_index].weight;
}
}
- for (host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
+ for (host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
{
if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
continue;
}
-memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list)
+memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_st *list)
{
uint32_t count;
memcached_server_st *new_host_list;
if (! list)
return MEMCACHED_SUCCESS;
- count= memcached_servers_count(list);
+ count= memcached_server_list_count(list);
new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr),
sizeof(memcached_server_instance_st) * (count + memcached_server_count(ptr)));
for (uint32_t x= 0; x < count; x++)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
|| ((list[x].type == MEMCACHED_CONNECTION_UDP)
// Provides backwards compatibility with server list.
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
instance= memcached_server_instance_fetch(ptr, 0);
instance->number_of_hosts= memcached_server_count(ptr);
}
uint32_t weight,
memcached_connection_t type)
{
- memcached_server_instance_st *new_host_list;
- memcached_server_instance_st *instance;
+ memcached_server_st *new_host_list;
+ memcached_server_write_instance_st instance;
if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP)
|| ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) )
return MEMCACHED_INVALID_HOST_PROTOCOL;
new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr),
- sizeof(memcached_server_instance_st) * (ptr->number_of_hosts + 1));
+ sizeof(memcached_server_st) * (ptr->number_of_hosts + 1));
if (new_host_list == NULL)
return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
MEM_WRITE
} memc_read_or_write;
-static ssize_t io_flush(memcached_server_instance_st *ptr, memcached_return_t *error);
-static void increment_udp_message_id(memcached_server_instance_st *ptr);
+static ssize_t io_flush(memcached_server_write_instance_st ptr,
+ memcached_return_t *error);
+static void increment_udp_message_id(memcached_server_write_instance_st ptr);
-static memcached_return_t io_wait(memcached_server_instance_st *ptr,
+static memcached_return_t io_wait(memcached_server_write_instance_st ptr,
memc_read_or_write read_or_write)
{
struct pollfd fds= {
*
* @param ptr the server to pack
*/
-static bool repack_input_buffer(memcached_server_instance_st *ptr)
+static bool repack_input_buffer(memcached_server_write_instance_st ptr)
{
if (ptr->read_ptr != ptr->read_buffer)
{
* @param ptr the server to star processing iput messages for
* @return true if we processed anything, false otherwise
*/
-static bool process_input_buffer(memcached_server_instance_st *ptr)
+static bool process_input_buffer(memcached_server_write_instance_st ptr)
{
/*
** We might be able to process some of the response messages if we
}
#endif
-memcached_return_t memcached_io_read(memcached_server_instance_st *ptr,
+memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
void *buffer, size_t length, ssize_t *nread)
{
char *buffer_ptr;
return MEMCACHED_SUCCESS;
}
-ssize_t memcached_io_write(memcached_server_instance_st *ptr,
+ssize_t memcached_io_write(memcached_server_write_instance_st ptr,
const void *buffer, size_t length, bool with_flush)
{
size_t original_length;
return (ssize_t) original_length;
}
-memcached_return_t memcached_io_close(memcached_server_instance_st *ptr)
+memcached_return_t memcached_io_close(memcached_server_write_instance_st ptr)
{
if (ptr->fd == -1)
{
return MEMCACHED_SUCCESS;
}
-memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *memc)
+memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc)
{
#define MAX_SERVERS_TO_POLL 100
struct pollfd fds[MAX_SERVERS_TO_POLL];
x< memcached_server_count(memc) && host_index < MAX_SERVERS_TO_POLL;
++x)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(memc, x);
if (instance->read_buffer_length > 0) /* I have data in the buffer */
/* We have 0 or 1 server with pending events.. */
for (uint32_t x= 0; x< memcached_server_count(memc); ++x)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(memc, x);
if (memcached_server_response_count(instance) > 0)
{
for (uint32_t y= 0; y < memcached_server_count(memc); ++y)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(memc, y);
if (instance->fd == fds[x].fd)
return NULL;
}
-static ssize_t io_flush(memcached_server_instance_st *ptr,
+static ssize_t io_flush(memcached_server_write_instance_st ptr,
memcached_return_t *error)
{
/*
/*
Eventually we will just kill off the server with the problem.
*/
-void memcached_io_reset(memcached_server_instance_st *ptr)
+void memcached_io_reset(memcached_server_write_instance_st ptr)
{
memcached_quit_server(ptr, true);
}
* Read a given number of bytes from the server and place it into a specific
* buffer. Reset the IO channel on this server if an error occurs.
*/
-memcached_return_t memcached_safe_read(memcached_server_instance_st *ptr,
+memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
void *dta,
size_t size)
{
return MEMCACHED_SUCCESS;
}
-memcached_return_t memcached_io_readline(memcached_server_instance_st *ptr,
+memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
char *buffer_ptr,
size_t size)
{
* extracts the message number from message_id, increments it and then
* writes the new value back into the header
*/
-static void increment_udp_message_id(memcached_server_instance_st *ptr)
+static void increment_udp_message_id(memcached_server_write_instance_st ptr)
{
struct udp_datagram_header_st *header= (struct udp_datagram_header_st *)ptr->write_buffer;
uint16_t cur_req= get_udp_datagram_request_id(header);
header->request_id= htons((uint16_t) (thread_id | msg_num));
}
-memcached_return_t memcached_io_init_udp_header(memcached_server_instance_st *ptr, uint16_t thread_id)
+memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr, uint16_t thread_id)
{
if (thread_id > UDP_REQUEST_ID_MAX_THREAD_ID)
return MEMCACHED_FAILURE;
};
LIBMEMCACHED_LOCAL
-ssize_t memcached_io_write(memcached_server_instance_st *ptr,
+ssize_t memcached_io_write(memcached_server_write_instance_st ptr,
const void *buffer, size_t length, bool with_flush);
LIBMEMCACHED_LOCAL
-void memcached_io_reset(memcached_server_instance_st *ptr);
+void memcached_io_reset(memcached_server_write_instance_st ptr);
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_read(memcached_server_instance_st *ptr,
+memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
void *buffer, size_t length, ssize_t *nread);
/* Read a line (terminated by '\n') into the buffer */
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_readline(memcached_server_instance_st *ptr,
+memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
char *buffer_ptr,
size_t size);
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_close(memcached_server_instance_st *ptr);
+memcached_return_t memcached_io_close(memcached_server_write_instance_st ptr);
/* Read n bytes of data from the server and store them in dta */
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_safe_read(memcached_server_instance_st *ptr,
+memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
void *dta,
size_t size);
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_init_udp_header(memcached_server_instance_st *ptr,
+memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr,
uint16_t thread_id);
LIBMEMCACHED_LOCAL
-memcached_server_instance_st *memcached_io_get_readable_server(memcached_st *memc);
+memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc);
#ifdef __cplusplus
}
if (servers == NULL)
return;
- for (x= 0; x < memcached_servers_count(servers); x++)
+ for (x= 0; x < memcached_server_list_count(servers); x++)
{
if (servers[x].address_info)
{
new_clone->io_key_prefetch= source->io_key_prefetch;
new_clone->number_of_replicas= source->number_of_replicas;
- if (memcached_server_list(source))
- rc= memcached_server_push(new_clone, memcached_server_list(source));
+ if (memcached_server_count(source))
+ rc= memcached_push(new_clone, source);
if (rc != MEMCACHED_SUCCESS)
{
return ret;
}
+
+memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
+{
+ return memcached_server_push(destination, source->servers);
+}
+
+inline memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
+{
+ return &ptr->servers[server_key];
+}
+
+inline memcached_server_instance_st *memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
+{
+ return &ptr->servers[server_key];
+}
LIBMEMCACHED_API
void *memcached_set_user_data(memcached_st *ptr, void *data);
+LIBMEMCACHED_API
+memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source);
+
+LIBMEMCACHED_API
+memcached_server_instance_st *memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key);
+
+LIBMEMCACHED_LOCAL
+void server_list_free(memcached_st *ptr, memcached_server_st *servers);
+
+
#ifdef __cplusplus
} // extern "C"
#endif
-// Local Only Inline
-static inline memcached_server_st *memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
-{
- return &ptr->servers[server_key];
-}
-
#ifdef __cplusplus
class Memcached : private memcached_st {
public:
:
servers_list(),
memc(),
- servers(NULL),
result()
{
memcached_create(&memc);
:
servers_list(in_servers_list),
memc(),
- servers(NULL),
result()
{
memcached_create(&memc);
- servers= memcached_servers_parse(servers_list.c_str());
- memcached_server_push(&memc, servers);
+ init();
}
Memcache(const std::string &hostname,
:
servers_list(),
memc(),
- servers(NULL),
result()
{
memcached_create(&memc);
+
servers_list.append(hostname);
servers_list.append(":");
std::ostringstream strsmt;
strsmt << port;
servers_list.append(strsmt.str());
- servers= memcached_servers_parse(servers_list.c_str());
- memcached_server_push(&memc, servers);
+
+ init();
}
Memcache(memcached_st *clone)
:
servers_list(),
memc(),
- servers(NULL),
result()
{
memcached_clone(&memc, clone);
:
servers_list(rhs.servers_list),
memc(),
- servers(NULL),
result()
{
memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
- servers= memcached_servers_parse(servers_list.c_str());
- memcached_server_push(&memc, servers);
+ init();
}
Memcache &operator=(const Memcache &rhs)
if (this != &rhs)
{
memcached_clone(&memc, const_cast<memcached_st *>(&rhs.getImpl()));
- servers= memcached_servers_parse(servers_list.c_str());
- memcached_server_push(&memc, servers);
+ init();
}
+
return *this;
}
~Memcache()
{
memcached_free(&memc);
- memcached_server_list_free(servers);
+ }
+
+ void init()
+ {
+ memcached_server_st *servers;
+ servers= memcached_servers_parse(servers_list.c_str());
+ memcached_server_push(&memc, servers);
+ memcached_server_free(servers);
}
/**
bool setServers(const std::string &in_servers_list)
{
servers_list.assign(in_servers_list);
- servers= memcached_servers_parse(in_servers_list.c_str());
- memcached_server_push(&memc, servers);
- return (servers == NULL);
+ init();
+
+ return (memcached_server_count(&memc));
}
/**
bool addServer(const std::string &server_name, in_port_t port)
{
memcached_return_t rc;
- std::ostringstream strstm;
- servers_list.append(",");
- servers_list.append(server_name);
- servers_list.append(":");
- strstm << port;
- servers_list.append(strstm.str());
- servers= memcached_server_list_append(servers,
- server_name.c_str(),
- port,
- &rc);
- memcached_server_push(&memc, servers);
+
+ rc= memcached_server_add(&memc, server_name.c_str(), port);
+
return (rc == MEMCACHED_SUCCESS);
}
*/
for (uint32_t x= 0; x < server_count; x++)
{
+ const memcached_server_instance_st *instance=
+ memcached_server_instance_by_position(&memc, x);
std::ostringstream strstm;
- std::string server_name(memcached_server_name(&memc, servers[x]));
+ std::string server_name(memcached_server_name(instance));
server_name.append(":");
- strstm << memcached_server_port(&memc, servers[x]);
+ strstm << memcached_server_port(instance);
server_name.append(strstm.str());
std::map<std::string, std::string> server_stats;
std::string servers_list;
memcached_st memc;
- memcached_server_st *servers;
memcached_result_st result;
};
#include "common.h"
-memcached_return_t memcached_purge(memcached_server_instance_st *ptr)
+memcached_return_t memcached_purge(memcached_server_write_instance_st ptr)
{
uint32_t x;
memcached_return_t ret= MEMCACHED_SUCCESS;
{
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
memcached_quit_server(instance, false);
LIBMEMCACHED_API
void memcached_quit(memcached_st *ptr);
+LIBMEMCACHED_LOCAL
+void memcached_quit_server(memcached_server_st *ptr, bool io_death);
+
#ifdef __cplusplus
}
#endif
#include "common.h"
-static memcached_return_t textual_read_one_response(memcached_server_instance_st *ptr,
+static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result);
-static memcached_return_t binary_read_one_response(memcached_server_instance_st *ptr,
+static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result);
-memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr,
+memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result)
{
return rc;
}
-memcached_return_t memcached_response(memcached_server_instance_st *ptr,
+memcached_return_t memcached_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result)
{
return memcached_read_one_response(ptr, buffer, buffer_length, result);
}
-static memcached_return_t textual_value_fetch(memcached_server_instance_st *ptr,
+static memcached_return_t textual_value_fetch(memcached_server_write_instance_st ptr,
char *buffer,
memcached_result_st *result)
{
return MEMCACHED_PARTIAL_READ;
}
-static memcached_return_t textual_read_one_response(memcached_server_instance_st *ptr,
+static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result)
{
/* NOTREACHED */
}
-static memcached_return_t binary_read_one_response(memcached_server_instance_st *ptr,
+static memcached_return_t binary_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result)
{
/* Read a single response from the server */
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_read_one_response(memcached_server_instance_st *ptr,
+memcached_return_t memcached_read_one_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result);
LIBMEMCACHED_LOCAL
-memcached_return_t memcached_response(memcached_server_instance_st *ptr,
+memcached_return_t memcached_response(memcached_server_write_instance_st ptr,
char *buffer, size_t buffer_length,
memcached_result_st *result);
return self;
}
-memcached_server_st *memcached_server_create_with(const memcached_st *memc, memcached_server_st *self,
+memcached_server_st *memcached_server_create_with(const memcached_st *memc,
+ memcached_server_write_instance_st self,
const char *hostname, in_port_t port,
uint32_t weight, memcached_connection_t type)
{
}
-memcached_return_t memcached_server_cursor(memcached_st *ptr,
- memcached_server_fn *callback,
+memcached_return_t memcached_server_cursor(const memcached_st *ptr,
+ const memcached_server_fn *callback,
void *context,
uint32_t number_of_callbacks)
{
- uint32_t y;
-
- for (y= 0; y < memcached_server_count(ptr); y++)
+ for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
{
- uint32_t x;
- memcached_server_instance_st *instance=
- memcached_server_instance_fetch(ptr, y);
+ const memcached_server_instance_st *instance=
+ memcached_server_instance_by_position(ptr, x);
- for (x= 0; x < number_of_callbacks; x++)
+ for (uint32_t y= 0; y < number_of_callbacks; y++)
{
unsigned int iferror;
- iferror= (*callback[x])(ptr, instance, context);
+ iferror= (*callback[y])(ptr, instance, context);
if (iferror)
continue;
return MEMCACHED_SUCCESS;
}
-memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return_t *error)
+const memcached_server_st *memcached_server_by_key(const memcached_st *ptr,
+ const char *key,
+ size_t key_length,
+ memcached_return_t *error)
{
uint32_t server_key;
- memcached_server_instance_st *instance;
+ const memcached_server_instance_st *instance;
*error= memcached_validate_key_length(key_length,
ptr->flags.binary_protocol);
}
server_key= memcached_generate_hash(ptr, key, key_length);
- instance= memcached_server_instance_fetch(ptr, server_key);
+ instance= memcached_server_instance_by_position(ptr, server_key);
- return memcached_server_clone(NULL, instance);
+ return instance;
}
-const char *memcached_server_error(memcached_server_st *ptr)
+const char *memcached_server_error(const memcached_server_st *ptr)
{
- return ptr
+ return ptr
? ptr->cached_server_error
: NULL;
}
return ptr->last_disconnected_server;
}
-uint32_t memcached_server_list_count(memcached_server_st *ptr)
+inline uint32_t memcached_server_list_count(const memcached_server_st *self)
{
- return (ptr == NULL)
+ return (self == NULL)
? 0
- : memcached_servers_count(ptr);
+ : self->number_of_hosts;
}
-void memcached_server_list_free(memcached_server_st *ptr)
+void memcached_server_list_free(memcached_server_st *self)
{
- server_list_free(NULL, ptr);
+ server_list_free(NULL, self);
}
/**
@todo allow lists to query themselves even if they lack a root
*/
-memcached_return_t memcached_server_remove(memcached_server_st *st_ptr)
+memcached_return_t memcached_server_remove(memcached_server_st *self)
{
- uint32_t x, host_index;
- memcached_st *root= (memcached_st *)st_ptr->root;
+ uint32_t host_index= 0;
+ memcached_st *root= (memcached_st *)self->root;
memcached_server_st *list;
if (root == NULL)
list= memcached_server_list(root);
- for (x= 0, host_index= 0; x < memcached_server_count(root); x++)
+ /* Until we devise a way to mark servers that are about to be harvested, we need to shutdown before doing the clone. */
+ memcached_quit(root);
+
+ for (uint32_t x= 0; x < memcached_server_count(root); x++)
{
- if (strncmp(list[x].hostname, st_ptr->hostname, NI_MAXHOST) != 0 || list[x].port != st_ptr->port)
+ if (strncmp(list[x].hostname, self->hostname, NI_MAXHOST) != 0 || list[x].port != self->port)
{
if (host_index != x)
- memcpy(list+host_index, list+x, sizeof(memcached_server_st));
+ {
+ memcached_server_st *check=
+ memcached_server_clone(list+host_index, list+x);
+
+ if (! check) // Now we are in trouble, allocation didn't happen and we are midway through an operation. Bail!
+ {
+ return MEMCACHED_FAILURE;
+ }
+ memcached_server_free(list+x);
+ }
host_index++;
}
}
root->number_of_hosts= host_index;
- if (st_ptr->address_info)
- {
- freeaddrinfo(st_ptr->address_info);
- st_ptr->address_info= NULL;
- }
run_distribution(root);
return MEMCACHED_SUCCESS;
}
-inline uint32_t memcached_servers_count(memcached_server_st *servers)
+inline uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
{
- return servers->number_of_hosts;
+ return servers->number_of_hosts= count;
}
+inline uint32_t memcached_server_count(const memcached_st *self)
+{
+ return self->number_of_hosts;
+}
-inline uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
+inline const char *memcached_server_name(const memcached_server_st *self)
{
- return servers->number_of_hosts= count;
+ return self->hostname;
+}
+
+inline in_port_t memcached_server_port(const memcached_server_st *self)
+{
+ return self->port;
+}
+
+inline memcached_server_st *memcached_server_list(memcached_st *self)
+{
+ return self->servers;
+}
+
+inline void memcached_server_list_set(memcached_st *self, memcached_server_st *list)
+{
+ self->servers= list;
+}
+
+inline uint32_t memcached_server_response_count(const memcached_server_st *self)
+{
+ return self->cursor_active;
}
#endif
LIBMEMCACHED_API
-memcached_return_t memcached_server_cursor(memcached_st *ptr,
- memcached_server_fn *callback,
+memcached_return_t memcached_server_cursor(const memcached_st *ptr,
+ const memcached_server_fn *callback,
void *context,
uint32_t number_of_callbacks);
LIBMEMCACHED_API
-memcached_server_st *memcached_server_by_key(memcached_st *ptr,
- const char *key,
- size_t key_length,
- memcached_return_t *error);
+ const memcached_server_st *memcached_server_by_key(const memcached_st *ptr,
+ const char *key,
+ size_t key_length,
+ memcached_return_t *error);
LIBMEMCACHED_API
-const char *memcached_server_error(memcached_server_st *ptr);
+const char *memcached_server_error(const memcached_server_st *ptr);
LIBMEMCACHED_API
void memcached_server_error_reset(memcached_server_st *ptr);
-/* These should not currently be used by end users */
-/* TODO: Is the above comment valid? If so, how can we unit test these if they
- * aren't exported. If not, we should remove the comment */
-
-LIBMEMCACHED_LOCAL
-memcached_server_st *memcached_server_create_with(const memcached_st *memc,
- memcached_server_st *host,
- const char *hostname,
- in_port_t port,
- uint32_t weight,
- memcached_connection_t type);
-
LIBMEMCACHED_API
void memcached_server_free(memcached_server_st *ptr);
LIBMEMCACHED_API
-memcached_return_t memcached_server_push(memcached_st *ptr, memcached_server_st *list);
+memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_st *list);
LIBMEMCACHED_API
memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
uint32_t weight,
memcached_return_t *error);
LIBMEMCACHED_API
-unsigned int memcached_server_list_count(memcached_server_st *ptr);
+uint32_t memcached_server_list_count(const memcached_server_st *ptr);
LIBMEMCACHED_API
-uint32_t memcached_servers_count(memcached_server_st *servers);
+uint32_t memcached_server_count(const memcached_st *);
+
LIBMEMCACHED_LOCAL
uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count);
+LIBMEMCACHED_API
+memcached_server_st *memcached_server_list(memcached_st *);
+
+LIBMEMCACHED_LOCAL
+void memcached_server_list_set(memcached_st *self, memcached_server_st *list);
+
+LIBMEMCACHED_API
+uint32_t memcached_server_response_count(const memcached_server_st *self);
+
+LIBMEMCACHED_API
+const char *memcached_server_name(const memcached_server_st *self);
+
+LIBMEMCACHED_API
+in_port_t memcached_server_port(const memcached_server_st *self);
-#define memcached_server_count(A) (A)->number_of_hosts
-#define memcached_server_name(A,B) (B).hostname
-#define memcached_server_port(A,B) (B).port
-#define memcached_server_list(A) (A)->servers
-#define memcached_server_list_set(A,B) (A)->servers=(B)
-#define memcached_server_response_count(A) (A)->cursor_active
#ifdef __cplusplus
} // extern "C"
memcached_return_t *error)
{
unsigned int count;
- memcached_server_instance_st *new_host_list;
+ memcached_server_st *new_host_list;
if (hostname == NULL || error == NULL)
return NULL;
count= 1;
if (ptr != NULL)
{
- count+= memcached_servers_count(ptr);
+ count+= memcached_server_list_count(ptr);
}
- new_host_list= (memcached_server_instance_st *)realloc(ptr, sizeof(memcached_server_instance_st) * count);
+ new_host_list= (memcached_server_write_instance_st)realloc(ptr, sizeof(memcached_server_st) * count);
if (!new_host_list)
{
*error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
char *args,
- memcached_server_instance_st *instance)
+ memcached_server_write_instance_st instance)
{
memcached_return_t rc;
static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
char *args,
- memcached_server_instance_st *instance)
+ memcached_server_write_instance_st instance)
{
memcached_return_t rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
{
memcached_return_t temp_return;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
memcached_stat_st *stat_instance;
stat_instance= stats + x;
memcached_return_t rc;
memcached_st memc;
memcached_st *memc_ptr;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
memc_ptr= memcached_create(&memc);
WATCHPOINT_ASSERT(memc_ptr);
memcached_return_t rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
uint32_t server_key;
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
flags, cas, verb);
}
- server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
instance= memcached_server_instance_fetch(ptr, server_key);
if (cas)
bool flush;
protocol_binary_request_set request= {.bytes= {0}};
size_t send_length= sizeof(request.bytes);
- uint32_t server_key= memcached_generate_hash(ptr, master_key,
- master_key_length);
+ uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, master_key,
+ master_key_length);
- memcached_server_instance_st *server=
+ memcached_server_write_instance_st server=
memcached_server_instance_fetch(ptr, server_key);
bool noreply= server->root->flags.no_reply;
for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
{
- memcached_server_instance_st *instance;
+ memcached_server_write_instance_st instance;
++server_key;
if (server_key == memcached_server_count(ptr))
{
return self->string;
}
+
+inline void memcached_string_set_length(memcached_string_st *self, size_t length)
+{
+ self->end= self->string + length;
+}
LIBMEMCACHED_LOCAL
char *memcached_string_value_mutable(const memcached_string_st *self);
-#define memcached_string_set_length(A, B) (A)->end= (A)->string + B
+LIBMEMCACHED_LOCAL
+void memcached_string_set_length(memcached_string_st *self, size_t length);
#ifdef __cplusplus
}
typedef struct memcached_analysis_st memcached_analysis_st;
typedef struct memcached_result_st memcached_result_st;
typedef struct memcached_server_st memcached_server_st;
+typedef const struct memcached_server_st memcached_server_instance_st;
+
typedef struct memcached_callback_st memcached_callback_st;
typedef struct memcached_string_st memcached_string_st;
typedef memcached_return_t (*memcached_execute_fn)(const memcached_st *ptr, memcached_result_st *result, void *context);
-typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, memcached_server_st *server, void *context);
+typedef memcached_return_t (*memcached_server_fn)(const memcached_st *ptr, const memcached_server_st *server, void *context);
/**
Trigger functions.
for (x= 0; x < memcached_server_count(ptr); x++)
{
memcached_return_t rrc;
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
rrc= memcached_do(instance, buffer, send_length, true);
for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
{
memcached_return_t rrc;
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
rrc= memcached_do(instance, command, send_length, true);
{
memcached_return_t rrc;
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true);
for (x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_instance_st *instance=
+ memcached_server_write_instance_st instance=
memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance) > 0)
{
for (uint32_t loop= 0; loop < memcached_server_list_count(container->construct.servers); loop++)
{
- memcached_server_st *instance=
- memcached_server_instance_fetch(container->memc, loop);
+ const memcached_server_instance_st *instance=
+ memcached_server_instance_by_position(container->memc, loop);
test_true(instance->fd == -1);
test_true(instance->cursor_active == 0);
in_port_t test_ports[TEST_PORT_COUNT];
static memcached_return_t server_display_function(const memcached_st *ptr __attribute__((unused)),
- memcached_server_st *server, void *context)
+ const memcached_server_st *server,
+ void *context)
{
/* Do Nothing */
size_t bigger= *((size_t *)(context));
- assert(bigger <= server->port);
- *((size_t *)(context))= server->port;
+ assert(bigger <= memcached_server_port(server));
+ *((size_t *)(context))= memcached_server_port(server);
return MEMCACHED_SUCCESS;
}
test_ports[x]= (in_port_t)random() % 64000;
rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
test_true(memcached_server_count(local_memc) == x + 1);
- test_true(memcached_servers_count(memcached_server_list(local_memc)) == x+1);
+ test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1);
test_true(rc == MEMCACHED_SUCCESS);
}
rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0);
test_true(rc == MEMCACHED_SUCCESS);
- instance= memcached_server_instance_fetch(local_memc, 0);
- test_true(instance->port == 43043);
+ instance= memcached_server_instance_by_position(local_memc, 0);
+ test_true(memcached_server_port(instance) == 43043);
rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0);
test_true(rc == MEMCACHED_SUCCESS);
- instance= memcached_server_instance_fetch(local_memc, 0);
- test_true(instance->port == 43042);
+ instance= memcached_server_instance_by_position(local_memc, 0);
+ test_true(memcached_server_port(instance) == 43042);
- instance= memcached_server_instance_fetch(local_memc, 1);
- test_true(instance->port == 43043);
+ instance= memcached_server_instance_by_position(local_memc, 1);
+ test_true(memcached_server_port(instance) == 43043);
callbacks[0]= server_display_function;
memcached_server_cursor(local_memc, callbacks, (void *)&bigger, 1);
return TEST_SUCCESS;
}
-static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)), memcached_server_st *server, void *context)
+static memcached_return_t server_print_callback(const memcached_st *ptr __attribute__((unused)),
+ const memcached_server_st *server,
+ void *context __attribute__((unused)))
+{
+ (void)server; // Just in case we aren't printing.
+
+#if 0
+ fprintf(stderr, "%s(%d)", memcached_server_name(server), memcached_server_port(server));
+#endif
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return_t memcached_server_remove_test(memcached_st *ptr __attribute__((unused)))
+{
+ memcached_return_t rc;
+ memcached_st local_memc;
+ memcached_st *memc;
+ memcached_server_st *servers;
+ memcached_server_fn callbacks[1];
+
+ const char *server_string= "localhost:4444, localhost:4445, localhost:4446, localhost:4447, localhost, memcache1.memcache.bk.sapo.pt:11211, memcache1.memcache.bk.sapo.pt:11212, memcache1.memcache.bk.sapo.pt:11213, memcache1.memcache.bk.sapo.pt:11214, memcache2.memcache.bk.sapo.pt:11211, memcache2.memcache.bk.sapo.pt:11212, memcache2.memcache.bk.sapo.pt:11213, memcache2.memcache.bk.sapo.pt:11214";
+
+ memc= memcached_create(&local_memc);
+
+ servers= memcached_servers_parse(server_string);
+
+ rc= memcached_server_push(memc, servers);
+
+ callbacks[0]= server_print_callback;
+ memcached_server_cursor(memc, callbacks, NULL, 1);
+
+ memcached_free(memc);
+
+ return TEST_SUCCESS;
+}
+
+static memcached_return_t server_display_unsort_function(const memcached_st *ptr __attribute__((unused)),
+ const memcached_server_st *server,
+ void *context)
{
/* Do Nothing */
uint32_t x= *((uint32_t *)(context));
test_ports[x]= (in_port_t)(random() % 64000);
rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0);
test_true(memcached_server_count(local_memc) == x+1);
- test_true(memcached_servers_count(memcached_server_list(local_memc)) == x+1);
+ test_true(memcached_server_list_count(memcached_server_list(local_memc)) == x+1);
test_true(rc == MEMCACHED_SUCCESS);
}
test_true(rc == MEMCACHED_SUCCESS);
uint64_t cas = memcached_result_cas(results);
- #if 0
+#if 0
results= memcached_fetch_result(memc, &results_obj, &rc);
test_true(rc == MEMCACHED_END);
test_true(results == NULL);
}
static memcached_return_t server_function(const memcached_st *ptr __attribute__((unused)),
- memcached_server_st *server __attribute__((unused)),
+ const memcached_server_st *server __attribute__((unused)),
void *context __attribute__((unused)))
{
/* Do Nothing */
memcached_return_t rc;
memcached_stat_st memc_stat;
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
rc= memcached_stat_servername(&memc_stat, NULL,
- instance->hostname,
- instance->port);
+ memcached_server_name(instance),
+ memcached_server_port(instance));
return TEST_SUCCESS;
}
From Andrei on IRC
*/
-static test_return_t user_supplied_bug19(memcached_st *memc)
+static test_return_t user_supplied_bug19(memcached_st *not_used)
{
- memcached_st *m;
- memcached_server_st *s;
+ memcached_st *memc;
+ const memcached_server_st *server;
memcached_return_t res;
- (void)memc;
+ (void)not_used;
- m= memcached_create(NULL);
- memcached_server_add_with_weight(m, "localhost", 11311, 100);
- memcached_server_add_with_weight(m, "localhost", 11312, 100);
-
- s= memcached_server_by_key(m, "a", 1, &res);
- memcached_server_free(s);
+ memc= memcached_create(NULL);
+ memcached_server_add_with_weight(memc, "localhost", 11311, 100);
+ memcached_server_add_with_weight(memc, "localhost", 11312, 100);
- memcached_free(m);
+ server= memcached_server_by_key(memc, "a", 1, &res);
return TEST_SUCCESS;
}
for (x= 0; x < 99; x++)
{
uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
+
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, server_idx);
- char *hostname = instance->hostname;
+ memcached_server_instance_by_position(memc, server_idx);
+
+ const char *hostname = memcached_server_name(instance);
test_strcmp(hostname, ketama_test_cases[x].server);
}
test_true(server_pool[7].port == 11211);
test_true(server_pool[7].weight == 100);
- instance= memcached_server_instance_fetch(memc, 2);
- instance->next_retry = time(NULL) + 15;
+ 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;
+ /*
+ This would not work if there were only two hosts.
+ */
for (size_t x= 0; x < 99; x++)
{
+ memcached_autoeject(memc);
uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
test_true(server_idx != 2);
}
/* and re-added when it's back. */
- instance->next_retry = time(NULL) - 1;
+ ((memcached_server_write_instance_st)instance)->next_retry = time(NULL) - 1;
memc->next_distribution_rebuild= time(NULL) - 1;
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION,
memc->distribution);
uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
// We re-use instance from above.
instance=
- memcached_server_instance_fetch(memc, server_idx);
- char *hostname = instance->hostname;
+ memcached_server_instance_by_position(memc, server_idx);
+ const char *hostname = memcached_server_name(instance);
test_true(strcmp(hostname, ketama_test_cases[x].server) == 0);
}
/* This test was changes so that "make test" would work properlly */
#ifdef DEBUG
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, host_index);
+ memcached_server_instance_by_position(memc, host_index);
printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
#endif
// will not toggle protocol on an connection.
memcached_version(memc_clone);
- instance= memcached_server_instance_fetch(memc_clone, 0);
+ instance= memcached_server_instance_by_position(memc_clone, 0);
if (instance->major_version >= 1 && instance->minor_version > 2)
{
// will not toggle protocol on an connection.
memcached_version(memc_clone);
- instance= memcached_server_instance_fetch(memc_clone, 0);
+ instance= memcached_server_instance_by_position(memc_clone, 0);
if (instance->major_version >= 1 && instance->minor_version > 2)
{
unsigned int set= 1;
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
memcached_version(memc);
static test_return_t check_for_1_2_3(memcached_st *memc)
{
memcached_version(memc);
+
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
if ((instance->major_version >= 1 && (instance->minor_version == 2 && instance->micro_version >= 4))
|| instance->minor_version > 2)
for (uint32_t x= 0; x < memcached_server_count(memc); ++x)
{
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, x);
+ memcached_server_instance_by_position(memc, x);
no_msg+=(int)(instance->cursor_active);
}
{
memcached_st *memc_clone= memcached_clone(NULL, memc);
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc_clone, host);
+ memcached_server_instance_by_position(memc_clone, host);
- instance->port= 0;
+ ((memcached_server_write_instance_st)instance)->port= 0;
for (int x= 'a'; x <= 'z'; ++x)
{
{
memcached_st *new_clone= memcached_clone(NULL, memc);
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(new_clone, host);
- instance->port= 0;
+ memcached_server_instance_by_position(new_clone, host);
+ ((memcached_server_write_instance_st)instance)->port= 0;
for (int x= 'a'; x <= 'z'; ++x)
{
for (uint32_t x= 0; x < (repl + 1); ++x)
{
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc_clone, x);
+ memcached_server_instance_by_position(memc_clone, x);
- instance->port= 0;
+ ((memcached_server_write_instance_st)instance)->port= 0;
if (++hash == memc_clone->number_of_hosts)
hash= 0;
}
for (uint32_t x= 0; x < memcached_server_count(memc); x++)
{
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, x);
+ memcached_server_instance_by_position(memc, x);
- ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) instance->write_buffer);
+ ids[x]= get_udp_datagram_request_id((struct udp_datagram_header_st *) ((memcached_server_instance_st *)instance)->write_buffer);
}
return ids;
{
memcached_version(memc);
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
/* For the time being, only support udp test for >= 1.2.6 && < 1.3 */
if (instance->major_version != 1 || instance->minor_version != 2
memcpy(servers, memcached_server_list(memc), sizeof(memcached_server_st) * num_hosts);
for (uint32_t x= 0; x < num_hosts; x++)
{
- memcached_server_instance_st *set_instance=
- memcached_server_instance_fetch(memc, x);
+ memcached_server_instance_st *set_instance=
+ memcached_server_instance_by_position(memc, x);
- memcached_server_free(set_instance);
+ memcached_server_free(((memcached_server_write_instance_st)set_instance));
}
memc->number_of_hosts= 0;
for (uint32_t x= 0; x < num_hosts; x++)
{
memcached_server_instance_st *set_instance=
- memcached_server_instance_fetch(memc, x);
+ memcached_server_instance_by_position(memc, x);
test_true(memcached_server_add_udp(memc, servers[x].hostname, servers[x].port) == MEMCACHED_SUCCESS);
test_true(set_instance->write_buffer_offset == UDP_DATAGRAM_HEADER_LENGTH);
#if 0
memcached_server_st server;
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
memcached_server_clone(&server, &memc->hosts[0]);
test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
test_true(memcached_server_add(memc, server.hostname, server.port) == MEMCACHED_INVALID_HOST_PROTOCOL);
#if 0
memcached_server_st server;
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, 0);
+ memcached_server_instance_by_position(memc, 0);
memcached_server_clone(&server, &memc->hosts[0]);
test_true(memcached_server_remove(&(memc->hosts[0])) == MEMCACHED_SUCCESS);
uint16_t *expected_ids= get_udp_request_ids(memc);
unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, server_key);
+ memcached_server_instance_by_position(memc, server_key);
size_t init_offset= instance->write_buffer_offset;
rc= memcached_set(memc, key, strlen(key),
uint16_t *expected_ids=get_udp_request_ids(memc);
unsigned int server_key= memcached_generate_hash(memc, key, strlen(key));
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, server_key);
+ memcached_server_instance_by_position(memc, server_key);
size_t init_offset= instance->write_buffer_offset;
rc= memcached_delete(memc, key, strlen(key), 0);
{
uint32_t server_idx = memcached_generate_hash(memc, ketama_test_cases[x].key, strlen(ketama_test_cases[x].key));
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, server_idx);
- char *hostname = instance->hostname;
+ memcached_server_instance_by_position(memc, server_idx);
+ const char *hostname = memcached_server_name(instance);
test_strcmp(hostname, ketama_test_cases[x].server);
}
/* verify the standard ketama set. */
for (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));
+ uint32_t server_idx= memcached_generate_hash(memc, ketama_test_cases_spy[x].key, strlen(ketama_test_cases_spy[x].key));
+
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc, server_idx);
- char *hostname = instance->hostname;
+ memcached_server_instance_by_position(memc, server_idx);
+
+ const char *hostname= memcached_server_name(instance);
+
test_strcmp(hostname, ketama_test_cases_spy[x].server);
}
* This is to verify correct behavior in the library. Fake that two servers
* are dead..
*/
- instance_one= memcached_server_instance_fetch(memc, 0);
- instance_two= memcached_server_instance_fetch(memc, 2);
+ instance_one= memcached_server_instance_by_position(memc, 0);
+ instance_two= memcached_server_instance_by_position(memc, 2);
in_port_t port0= instance_one->port;
in_port_t port2= instance_two->port;
- instance_one->port= 0;
- instance_two->port= 0;
+ ((memcached_server_write_instance_st)instance_one)->port= 0;
+ ((memcached_server_write_instance_st)instance_two)->port= 0;
rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
test_true(rc == MEMCACHED_SUCCESS);
test_true(counter == (unsigned int)max_keys);
/* restore the memc handle */
- instance_one->port= port0;
- instance_two->port= port2;
+ ((memcached_server_write_instance_st)instance_one)->port= port0;
+ ((memcached_server_write_instance_st)instance_two)->port= port2;
memcached_quit(memc);
}
memcached_quit(memc);
- instance_one->port= 0;
- instance_two->port= 0;
+ ((memcached_server_write_instance_st)instance_one)->port= 0;
+ ((memcached_server_write_instance_st)instance_two)->port= 0;
/* now retry the command, this time we should have cache misses */
rc= memcached_mget(memc, (const char* const *)keys, key_length, max_keys);
free(key_length);
/* restore the memc handle */
- instance_one->port= port0;
- instance_two->port= port2;
+ ((memcached_server_write_instance_st)instance_one)->port= port0;
+ ((memcached_server_write_instance_st)instance_two)->port= port2;
return TEST_SUCCESS;
}
test_true(memcached_version(memc_clone) == MEMCACHED_SUCCESS);
memcached_server_instance_st *instance=
- memcached_server_instance_fetch(memc_clone, 0);
+ memcached_server_instance_by_position(memc_clone, 0);
if (instance->major_version > 1 ||
(instance->major_version == 1 &&
test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
- instance= memcached_server_instance_fetch(memc, 0);
+ instance= memcached_server_instance_by_position(memc, 0);
/* The test is to see that the memcached_quit doesn't increase the
* the server failure conter, so let's ensure that it is zero
* before sending quit
*/
- instance->server_failure_counter= 0;
+ ((memcached_server_write_instance_st)instance)->server_failure_counter= 0;
memcached_quit(memc);
{"server_unsort", 0, (test_callback_fn)server_unsort_test},
{"server_sort", 0, (test_callback_fn)server_sort_test},
{"server_sort2", 0, (test_callback_fn)server_sort2_test},
+ {"memcached_server_remove", 0, (test_callback_fn)memcached_server_remove_test},
{"clone_test", 0, (test_callback_fn)clone_test },
{"connection_test", 0, (test_callback_fn)connection_test},
{"callback_test", 0, (test_callback_fn)callback_test},
for (x= 0; x < memcached_server_list_count(construct->servers); x++)
{
- printf("\t%s : %d\n", construct->servers[x].hostname, construct->servers[x].port);
+ printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
assert(construct->servers[x].fd == -1);
assert(construct->servers[x].cursor_active == 0);
}