From fbdda95bad712d301373b64193eb3ca00f281e55 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Sat, 7 Jan 2012 16:54:08 -0800 Subject: [PATCH] Adding in patch for DEAD server completion. --- libmemcached-1.0/defaults.h | 1 + libmemcached-1.0/struct/memcached.h | 1 + libmemcached-1.0/types/behavior.h | 1 + libmemcached/behavior.cc | 8 ++++++++ libmemcached/connect.cc | 22 ++++++++++++++++++---- libmemcached/memcached.cc | 4 +++- tests/libmemcached-1.0/mem_functions.cc | 2 +- tests/mem_udp.cc | 2 +- 8 files changed, 34 insertions(+), 7 deletions(-) diff --git a/libmemcached-1.0/defaults.h b/libmemcached-1.0/defaults.h index 355e63aa..38c29ba3 100644 --- a/libmemcached-1.0/defaults.h +++ b/libmemcached-1.0/defaults.h @@ -50,5 +50,6 @@ #define MEMCACHED_EXPIRATION_NOT_ADD 0xffffffffU #define MEMCACHED_SERVER_FAILURE_LIMIT 5 #define MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT 2 +#define MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT 0 diff --git a/libmemcached-1.0/struct/memcached.h b/libmemcached-1.0/struct/memcached.h index 564eb74e..a3f9d279 100644 --- a/libmemcached-1.0/struct/memcached.h +++ b/libmemcached-1.0/struct/memcached.h @@ -82,6 +82,7 @@ struct memcached_st { int32_t poll_timeout; int32_t connect_timeout; // How long we will wait on connect() before we will timeout int32_t retry_timeout; + int32_t dead_timeout; int send_size; int recv_size; void *user_data; diff --git a/libmemcached-1.0/types/behavior.h b/libmemcached-1.0/types/behavior.h index 192b89e4..d0af189a 100644 --- a/libmemcached-1.0/types/behavior.h +++ b/libmemcached-1.0/types/behavior.h @@ -75,6 +75,7 @@ enum memcached_behavior_t { MEMCACHED_BEHAVIOR_TCP_KEEPIDLE, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE, MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, + MEMCACHED_BEHAVIOR_DEAD_TIMEOUT, MEMCACHED_BEHAVIOR_MAX }; diff --git a/libmemcached/behavior.cc b/libmemcached/behavior.cc index 84e3aa58..8ffbd665 100644 --- a/libmemcached/behavior.cc +++ b/libmemcached/behavior.cc @@ -213,6 +213,10 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr, ptr->retry_timeout= int32_t(data); break; + case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: + ptr->dead_timeout= int32_t(data); + break; + case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE: ptr->send_size= (int32_t)data; send_quit(ptr); @@ -357,6 +361,9 @@ uint64_t memcached_behavior_get(memcached_st *ptr, case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: return (uint64_t)ptr->retry_timeout; + case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: + return uint64_t(ptr->dead_timeout); + case MEMCACHED_BEHAVIOR_SND_TIMEOUT: return (uint64_t)ptr->snd_timeout; @@ -555,6 +562,7 @@ const char *libmemcached_string_behavior(const memcached_behavior_t flag) case MEMCACHED_BEHAVIOR_VERIFY_KEY: return "MEMCACHED_BEHAVIOR_VERIFY_KEY"; case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT: return "MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT"; case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT: return "MEMCACHED_BEHAVIOR_RETRY_TIMEOUT"; + case MEMCACHED_BEHAVIOR_DEAD_TIMEOUT: return "MEMCACHED_BEHAVIOR_DEAD_TIMEOUT"; case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED: return "MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED"; case MEMCACHED_BEHAVIOR_KETAMA_HASH: return "MEMCACHED_BEHAVIOR_KETAMA_HASH"; case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL: return "MEMCACHED_BEHAVIOR_BINARY_PROTOCOL"; diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index b81b25b9..42cbe9bc 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -554,6 +554,9 @@ static memcached_return_t network_connect(memcached_server_st *server) */ static memcached_return_t backoff_handling(memcached_server_write_instance_st server, bool& in_timeout) { + struct timeval curr_time; + bool _gettime_success= (gettimeofday(&curr_time, NULL) == 0); + /* If we hit server_failure_limit then something is completely wrong about the server. @@ -568,7 +571,21 @@ static memcached_return_t backoff_handling(memcached_server_write_instance_st se if (_is_auto_eject_host(server->root)) { set_last_disconnected_host(server); - run_distribution((memcached_st *)server->root); + + // Retry dead servers if requested + if (_gettime_success and server->root->dead_timeout > 0) + { + server->next_retry= curr_time.tv_sec +server->root->dead_timeout; + + // We only retry dead servers once before assuming failure again + server->server_failure_counter= server->root->server_failure_limit -1; + } + + memcached_return_t rc; + if (memcached_failed(rc= run_distribution((memcached_st *)server->root))) + { + return memcached_set_error(*server, rc, MEMCACHED_AT, memcached_literal_param("Backoff handling failed during run_distribution")); + } return memcached_set_error(*server, MEMCACHED_SERVER_MARKED_DEAD, MEMCACHED_AT); } @@ -584,9 +601,6 @@ static memcached_return_t backoff_handling(memcached_server_write_instance_st se if (server->state == MEMCACHED_SERVER_STATE_IN_TIMEOUT) { - struct timeval curr_time; - bool _gettime_success= (gettimeofday(&curr_time, NULL) == 0); - /* If next_retry is less then our current time, then we reset and try everything again. */ diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index 62052ada..1726b35f 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -96,6 +96,7 @@ static inline bool _memcached_init(memcached_st *self) self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT; self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT; self->retry_timeout= MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT; + self->dead_timeout= MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT; self->send_size= -1; self->recv_size= -1; @@ -234,7 +235,7 @@ memcached_st *memcached(const char *string, size_t length) memcached_return_t memcached_reset(memcached_st *ptr) { WATCHPOINT_ASSERT(ptr); - if (not ptr) + if (ptr == NULL) { return MEMCACHED_INVALID_ARGUMENTS; } @@ -314,6 +315,7 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) new_clone->poll_timeout= source->poll_timeout; new_clone->connect_timeout= source->connect_timeout; new_clone->retry_timeout= source->retry_timeout; + new_clone->dead_timeout= source->dead_timeout; new_clone->distribution= source->distribution; if (hashkit_clone(&new_clone->hashkit, &source->hashkit) == NULL) diff --git a/tests/libmemcached-1.0/mem_functions.cc b/tests/libmemcached-1.0/mem_functions.cc index dd2abef2..62b32473 100644 --- a/tests/libmemcached-1.0/mem_functions.cc +++ b/tests/libmemcached-1.0/mem_functions.cc @@ -465,7 +465,7 @@ static test_return_t libmemcached_string_behavior_test(memcached_st *) { test_true(libmemcached_string_behavior(memcached_behavior_t(x))); } - test_compare(36, int(MEMCACHED_BEHAVIOR_MAX)); + test_compare(37, int(MEMCACHED_BEHAVIOR_MAX)); return TEST_SUCCESS; } diff --git a/tests/mem_udp.cc b/tests/mem_udp.cc index 1591bdcf..1ef95bec 100644 --- a/tests/mem_udp.cc +++ b/tests/mem_udp.cc @@ -434,7 +434,7 @@ static test_return_t udp_decr_test(memcached_st *memc) memcached_set(memc, test_literal_param(__func__), test_literal_param("1"), - (time_t)0, (uint32_t)0)); + time_t(0), uint32_t(0))); Expected expected_ids; get_udp_request_ids(memc, expected_ids); -- 2.30.2