From 8985b4b6e67215904383dac7fca792b4d9be89c4 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 9 Jun 2011 13:14:00 -0700 Subject: [PATCH] Update test case for timeout. --- libmemcached/connect.cc | 6 ++-- libmemcached/error.cc | 13 +++++++++ libmemcached/error.hpp | 3 ++ libmemcached/get.cc | 63 +++++++++++++++++++++++++++++++---------- libmemcached/io.cc | 15 ++++++---- tests/parser.cc | 9 ++++-- 6 files changed, 83 insertions(+), 26 deletions(-) diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 90d3facb..eda4b88f 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -542,7 +542,7 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) { set_last_disconnected_host(ptr); - return MEMCACHED_SERVER_MARKED_DEAD; + return memcached_set_error(*ptr, MEMCACHED_SERVER_MARKED_DEAD, MEMCACHED_AT); } } @@ -559,7 +559,7 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) run_distribution((memcached_st *)ptr->root); } - return MEMCACHED_SERVER_MARKED_DEAD; + return memcached_set_error(*ptr, MEMCACHED_SERVER_MARKED_DEAD, MEMCACHED_AT); } /* We need to clean up the multi startup piece */ @@ -602,8 +602,8 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) } else { + memcached_set_error(*ptr, rc, MEMCACHED_AT); ptr->server_failure_counter++; - set_last_disconnected_host(ptr); } diff --git a/libmemcached/error.cc b/libmemcached/error.cc index aaf0015d..01975956 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -332,6 +332,19 @@ const char *memcached_last_error_message(memcached_st *memc) return memc->error_messages->message; } + +bool memcached_has_current_error(memcached_st &memc) +{ + if (memc.error_messages + and memc.error_messages->query_id == memc.query_id + and memcached_failed(memc.error_messages->rc)) + { + return true; + } + + return false; +} + memcached_return_t memcached_last_error(memcached_st *memc) { if (not memc) diff --git a/libmemcached/error.hpp b/libmemcached/error.hpp index c3c83fad..1c683fc5 100644 --- a/libmemcached/error.hpp +++ b/libmemcached/error.hpp @@ -81,4 +81,7 @@ LIBMEMCACHED_LOCAL LIBMEMCACHED_LOCAL memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, const char *at); +LIBMEMCACHED_LOCAL +bool memcached_has_current_error(memcached_st &memc); + #endif diff --git a/libmemcached/get.cc b/libmemcached/get.cc index 576a9b7e..bcc468a0 100644 --- a/libmemcached/get.cc +++ b/libmemcached/get.cc @@ -35,8 +35,8 @@ * */ -#include "libmemcached/common.h" -#include +#include +#include /* What happens if no servers exist? @@ -69,17 +69,40 @@ char *memcached_get_by_key(memcached_st *ptr, { unlikely (ptr->flags.use_udp) { - *error= MEMCACHED_NOT_SUPPORTED; + if (value_length) + *value_length= 0; + + *error= memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); return NULL; } + uint64_t query_id= ptr->query_id; + (void)query_id; + /* Request the key */ *error= memcached_mget_by_key_real(ptr, group_key, group_key_length, (const char * const *)&key, &key_length, 1, false); + assert(ptr->query_id == query_id +1); + + + if (memcached_failed(*error)) + { + if (memcached_has_current_error(*ptr)) // Find the most accurate error + { + *error= memcached_last_error(ptr); + } + + if (value_length) + *value_length= 0; + + return NULL; + } char *value= memcached_fetch(ptr, NULL, NULL, value_length, flags, error); + assert(ptr->query_id == query_id +1); + /* This is for historical reasons */ if (*error == MEMCACHED_END) *error= MEMCACHED_NOTFOUND; @@ -129,6 +152,7 @@ char *memcached_get_by_key(memcached_st *ptr, } } } + assert(ptr->query_id == query_id +1); return NULL; } @@ -142,6 +166,7 @@ char *memcached_get_by_key(memcached_st *ptr, &dummy_error); WATCHPOINT_ASSERT(dummy_length == 0); WATCHPOINT_ASSERT(dummy_value == 0); + assert(ptr->query_id == query_id +1); return value; } @@ -174,7 +199,6 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, const char *get_command= "get "; uint8_t get_command_length= 4; unsigned int master_server_key= (unsigned int)-1; /* 0 is a valid server id! */ - bool is_group_key_set= false; memcached_return_t rc; if (memcached_failed(rc= initialize_query(ptr))) @@ -183,22 +207,29 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, } unlikely (ptr->flags.use_udp) - return MEMCACHED_NOT_SUPPORTED; + { + return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + } LIBMEMCACHED_MEMCACHED_MGET_START(); if (number_of_keys == 0) - return MEMCACHED_NOTFOUND; + { + return memcached_set_error(*ptr, MEMCACHED_NOTFOUND, MEMCACHED_AT, memcached_literal_param("number_of_keys was zero")); + } if (ptr->flags.verify_key && (memcached_key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED)) { - return MEMCACHED_BAD_KEY_PROVIDED; + return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad key value was provided")); } + bool is_group_key_set= false; if (group_key && group_key_length) { if (ptr->flags.verify_key and (memcached_key_test((const char * const *)&group_key, &group_key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) - return MEMCACHED_BAD_KEY_PROVIDED; + { + return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad group key was provided.")); + } master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); is_group_key_set= true; @@ -274,8 +305,9 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, { rc= memcached_connect(instance); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { + memcached_set_error(*instance, rc, MEMCACHED_AT); continue; } hosts_connected++; @@ -304,10 +336,10 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, { LIBMEMCACHED_MEMCACHED_MGET_END(); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) return rc; - return MEMCACHED_NO_SERVERS; + return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT); } @@ -337,12 +369,14 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, LIBMEMCACHED_MEMCACHED_MGET_END(); if (failures_occured_in_sending && success_happened) + { return MEMCACHED_SOME_ERRORS; + } if (success_happened) return MEMCACHED_SUCCESS; - return MEMCACHED_FAILURE; + return MEMCACHED_FAILURE; // Complete failure occurred } memcached_return_t memcached_mget_by_key(memcached_st *ptr, @@ -415,7 +449,6 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, for (uint32_t x= 0; x < number_of_keys; ++x) { uint32_t server_key; - memcached_server_write_instance_st instance; if (is_group_key_set) { @@ -426,12 +459,12 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } - instance= memcached_server_instance_fetch(ptr, server_key); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key); if (memcached_server_response_count(instance) == 0) { rc= memcached_connect(instance); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) continue; } diff --git a/libmemcached/io.cc b/libmemcached/io.cc index 51032f00..3a3cd45b 100644 --- a/libmemcached/io.cc +++ b/libmemcached/io.cc @@ -393,11 +393,10 @@ static ssize_t _io_write(memcached_server_write_instance_st ptr, if (ptr->write_buffer_offset == buffer_end && ptr->type != MEMCACHED_CONNECTION_UDP) { - memcached_return_t rc; - ssize_t sent_length; - WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); - sent_length= io_flush(ptr, with_flush, &rc); + + memcached_return_t rc; + ssize_t sent_length= io_flush(ptr, with_flush, &rc); if (sent_length == -1) { return -1; @@ -646,7 +645,9 @@ static ssize_t io_flush(memcached_server_write_instance_st ptr, rc= io_wait(ptr, MEM_WRITE); if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT) + { continue; + } memcached_quit_server(ptr, true); return -1; @@ -655,7 +656,7 @@ static ssize_t io_flush(memcached_server_write_instance_st ptr, case EPIPE: default: memcached_quit_server(ptr, true); - *error= MEMCACHED_ERRNO; + *error= memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); WATCHPOINT_ASSERT(ptr->fd == -1); return -1; } @@ -714,7 +715,9 @@ memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr, memcached_return_t rc= memcached_io_read(ptr, data + offset, size - offset, &nread); if (rc != MEMCACHED_SUCCESS) + { return rc; + } offset+= (size_t) nread; } @@ -741,7 +744,9 @@ memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr, ssize_t nread; memcached_return_t rc= memcached_io_read(ptr, buffer_ptr, 1, &nread); if (rc != MEMCACHED_SUCCESS) + { return rc; + } if (*buffer_ptr == '\n') line_complete= true; diff --git a/tests/parser.cc b/tests/parser.cc index e2f6b2f8..113d6e00 100644 --- a/tests/parser.cc +++ b/tests/parser.cc @@ -46,6 +46,7 @@ #define BUILDING_LIBMEMCACHED // !NEVER use common.h, always use memcached.h in your own apps #include +#include #include "tests/parser.h" #include "tests/print.h" @@ -548,7 +549,8 @@ test_return_t test_hostname_port_weight(memcached_st *) test_return_t regression_bug_71231153(memcached_st *) { - if (1) return TEST_SKIPPED; + if (libmemcached_util_ping("10.0.2.252", 0, NULL)) // If for whatever reason someone has a host at this address, skip + return TEST_SKIPPED; memcached_st *memc= memcached(memcached_literal_param("--SERVER=10.0.2.252 --CONNECT-TIMEOUT=1 --POLL-TIMEOUT=1")); test_true(memc); @@ -557,9 +559,10 @@ test_return_t regression_bug_71231153(memcached_st *) memcached_return_t rc; size_t value_len; - char *value= memcached_get(memc, "test", 4, &value_len, NULL, &rc); + char *value= memcached_get(memc, memcached_literal_param("test"), &value_len, NULL, &rc); test_false(value); - test_compare_got(MEMCACHED_TIMEOUT, rc, memcached_last_error_message(memc)); + test_compare(0, value_len); + test_true_got(rc == MEMCACHED_TIMEOUT or rc == MEMCACHED_FAILURE or rc == MEMCACHED_ERRNO, memcached_strerror(memc, rc)); memcached_free(memc); -- 2.30.2