Skip to content

Commit

Permalink
Merge pull request #79 from MOON-CLJ/errCodeToString
Browse files Browse the repository at this point in the history
add func errCodeToString
  • Loading branch information
tclh123 authored Jan 26, 2019
2 parents 8564a87 + c27d7da commit b69f385
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 87 deletions.
2 changes: 2 additions & 0 deletions include/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdlib>
#include <cerrno>

#include "Export.h"

#define PROJECT_NAME "libmc"
#define MC_DEFAULT_PORT 11211
Expand Down Expand Up @@ -223,6 +224,7 @@ typedef enum {
QUIT_OP,
} op_code_t;

const char* errCodeToString(err_code_t err);

} // namespace mc
} // namespace douban
9 changes: 5 additions & 4 deletions include/Keywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ static const char kSTATS[] = "stats";
static const char kQUIT[] = "quit";


// error messages for humman
// error messages for human
static const char kSEND_ERROR[] = "send_error";
static const char kRECV_ERROR[] = "recv_error";
static const char kSERVER_ERROR[] = "server_error";
static const char kCONN_POLL_ERROR[] = "conn_poll_error";
static const char kPOLL_TIMEOUT[] = "poll_timeout";
static const char kPOLL_TIMEOUT_ERROR[] = "poll_timeout_error";
static const char kPOLL_ERROR[] = "poll_error";
static const char kSERVER_ERROR[] = "server_error";
static const char kPROGRAMMING_ERROR[] = "programming_error";
static const char kINVALID_KEY_ERROR[] = "invalid_key_error";
static const char kINCOMPLETE_BUFFER_ERROR[] = "incomplete_buffer_error";

static const char kUPDATE_SERVER[] = "update_server";

static const char kCONN_QUIT[] = "conn_quit";

} // namespace keywords
Expand Down
20 changes: 11 additions & 9 deletions include/c_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ extern "C" {
const char* client_get_realtime_server_address_by_key(void* client, const char* key,
const size_t key_len);

int client_version(void* client, broadcast_result_t** results, size_t* n_hosts);
err_code_t client_version(void* client, broadcast_result_t** results, size_t* n_hosts);
void client_destroy_broadcast_result(void* client);

#define DECL_RETRIEVAL_CMD(M) \
int client_##M(void* client, const char* const* keys, const size_t* key_lens, \
err_code_t client_##M(void* client, const char* const* keys, const size_t* key_lens, \
size_t nKeys, retrieval_result_t*** results, size_t* n_results)
DECL_RETRIEVAL_CMD(get);
DECL_RETRIEVAL_CMD(gets);
Expand All @@ -32,7 +32,7 @@ extern "C" {
void client_destroy_retrieval_result(void* client);

#define DECL_STORAGE_CMD(M) \
int client_##M(void* client, const char* const* keys, const size_t* key_lens, \
err_code_t client_##M(void* client, const char* const* keys, const size_t* key_lens, \
const flags_t* flags, const exptime_t exptime, \
const cas_unique_t* cas_uniques, const bool noreply, \
const char* const* vals, const size_t* val_lens, \
Expand All @@ -45,25 +45,27 @@ extern "C" {
DECL_STORAGE_CMD(cas);
#undef DECL_STORAGE_CMD

int client_touch(void* client, const char* const* keys, const size_t* key_lens,
err_code_t client_touch(void* client, const char* const* keys, const size_t* key_lens,
const exptime_t exptime, const bool noreply, size_t n_items,
message_result_t*** results, size_t* n_results);
void client_destroy_message_result(void* client);

int client_delete(void*client, const char* const* keys, const size_t* key_lens,
err_code_t client_delete(void*client, const char* const* keys, const size_t* key_lens,
const bool noreply, size_t n_items,
message_result_t*** results, size_t* n_results);

int client_incr(void* client, const char* key, const size_t keyLen,
err_code_t client_incr(void* client, const char* key, const size_t keyLen,
const uint64_t delta, const bool noreply,
unsigned_result_t** results, size_t* n_results);
int client_decr(void* client, const char* key, const size_t keyLen,
err_code_t client_decr(void* client, const char* key, const size_t keyLen,
const uint64_t delta, const bool noreply,
unsigned_result_t** results, size_t* n_results);
void client_destroy_unsigned_result(void* client);

int client_stats(void* client, broadcast_result_t** results, size_t* n_servers);
int client_quit(void* client);
err_code_t client_stats(void* client, broadcast_result_t** results, size_t* n_servers);
err_code_t client_quit(void* client);

const char* err_code_to_string(err_code_t err);
#ifdef __cplusplus
}
#endif
31 changes: 10 additions & 21 deletions libmc/_client.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ cdef extern from "Client.h" namespace "douban::mc":
void destroyUnsignedResult() nogil
void _sleep(uint32_t seconds) nogil

const char* errCodeToString(err_code_t err) nogil

cdef uint32_t MC_DEFAULT_PORT = 11211
cdef flags_t _FLAG_EMPTY = 0
cdef flags_t _FLAG_PICKLE = 1 << 0
Expand Down Expand Up @@ -242,19 +244,6 @@ MC_RETURN_INCOMPLETE_BUFFER_ERR = PyInt_FromLong(RET_INCOMPLETE_BUFFER_ERR)
MC_RETURN_OK = PyInt_FromLong(RET_OK)


cdef dict ERROR_CODE_TO_STR = {
MC_RETURN_SEND_ERR: 'send_error',
MC_RETURN_RECV_ERR: 'recv_error',
MC_RETURN_CONN_POLL_ERR: 'conn_poll_error',
MC_RETURN_POLL_TIMEOUT_ERR: 'poll_timeout_error',
MC_RETURN_POLL_ERR: 'poll_error',
MC_RETURN_MC_SERVER_ERR: 'server_error',
MC_RETURN_PROGRAMMING_ERR: 'programming_error',
MC_RETURN_INVALID_KEY_ERR: 'invalid_key_error',
MC_RETURN_INCOMPLETE_BUFFER_ERR: 'incomplete_buffer_error',
MC_RETURN_OK: 'ok'
}


cdef bytes _encode_value(object val, int comp_threshold, flags_t *flags):
type_ = type(val)
Expand Down Expand Up @@ -343,7 +332,7 @@ cdef class PyClient:
cdef hash_function_options_t hash_fn
cdef bool_t failover
cdef basestring encoding
cdef int last_error
cdef err_code_t last_error
cdef object _thread_ident
cdef object _created_stack

Expand All @@ -370,7 +359,7 @@ cdef class PyClient:
else:
self.prefix = None

self.last_error = 0
self.last_error = RET_OK
self._thread_ident = None
self._created_stack = traceback.extract_stack()

Expand Down Expand Up @@ -643,7 +632,7 @@ cdef class PyClient:
else:
pass

rv = self.last_error == 0 and (self.noreply or (n_res == 1 and results[0][0].type_ == MSG_STORED))
rv = self.last_error == RET_OK and (self.noreply or (n_res == 1 and results[0][0].type_ == MSG_STORED))

with nogil:
self._imp.destroyMessageResult()
Expand Down Expand Up @@ -784,7 +773,7 @@ cdef class PyClient:
self.noreply, c_vals, c_val_lens, n, &results, &n_rst)
else:
pass
is_succeed = self.last_error == 0 and (self.noreply or n_rst == n)
is_succeed = self.last_error == RET_OK and (self.noreply or n_rst == n)
cdef list failed_keys = []
if not is_succeed and return_failure:
succeed_keys = [results[i][0].key[:results[i][0].key_len] for i in range(n_rst) if results[i][0].type_ == MSG_STORED]
Expand Down Expand Up @@ -866,7 +855,7 @@ cdef class PyClient:
with nogil:
self.last_error = self._imp._delete(&c_key, &c_key_len, self.noreply, n, &results, &n_res)

rv = self.last_error == 0 and (self.noreply or (n_res == 1 and (results[0][0].type_ == MSG_DELETED or results[0][0].type_ == MSG_NOT_FOUND)))
rv = self.last_error == RET_OK and (self.noreply or (n_res == 1 and (results[0][0].type_ == MSG_DELETED or results[0][0].type_ == MSG_NOT_FOUND)))

with nogil:
self._imp.destroyMessageResult()
Expand All @@ -892,7 +881,7 @@ cdef class PyClient:
with nogil:
self.last_error = self._imp._delete(c_keys, c_key_lens, self.noreply, n, &results, &n_res)

is_succeed = self.last_error == 0 and (self.noreply or n_res == n)
is_succeed = self.last_error == RET_OK and (self.noreply or n_res == n)
cdef list failed_keys = []

if not is_succeed and return_failure:
Expand Down Expand Up @@ -926,7 +915,7 @@ cdef class PyClient:
with nogil:
self.last_error = self._imp.touch(&c_key, &c_key_len, exptime, self.noreply, n, &results, &n_res)

rv = self.last_error == 0 and (self.noreply or (n_res == 1 and results[0][0].type_ == MSG_TOUCHED))
rv = self.last_error == RET_OK and (self.noreply or (n_res == 1 and results[0][0].type_ == MSG_TOUCHED))
with nogil:
self._imp.destroyMessageResult()
Py_DECREF(key)
Expand Down Expand Up @@ -1069,4 +1058,4 @@ cdef class PyClient:
return False

def get_last_strerror(self):
return ERROR_CODE_TO_STR.get(self.last_error, '')
return errCodeToString(self.last_error)
36 changes: 36 additions & 0 deletions src/Common.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "Common.h"
#include "Keywords.h"

#ifdef __GLIBC__
#include <execinfo.h>
#include <cstdlib>
Expand All @@ -16,3 +18,37 @@ void printBacktrace() {
#else
void printBacktrace() {}
#endif

namespace douban {
namespace mc {

const char* errCodeToString(err_code_t err) {
switch (err)
{
case RET_SEND_ERR:
return keywords::kSEND_ERROR;
case RET_RECV_ERR:
return keywords::kRECV_ERROR;
case RET_CONN_POLL_ERR:
return keywords::kCONN_POLL_ERROR;
case RET_POLL_TIMEOUT_ERR:
return keywords::kPOLL_TIMEOUT_ERROR;
case RET_POLL_ERR:
return keywords::kPOLL_ERROR;
case RET_MC_SERVER_ERR:
return keywords::kSERVER_ERROR;
case RET_PROGRAMMING_ERR:
return keywords::kPROGRAMMING_ERROR;
case RET_INVALID_KEY_ERR:
return keywords::kINVALID_KEY_ERROR;
case RET_INCOMPLETE_BUFFER_ERR:
return keywords::kINCOMPLETE_BUFFER_ERROR;
case RET_OK:
return "ok";
default:
return "unknown";
}
}

} // namespace mc
} // namespace douban
2 changes: 1 addition & 1 deletion src/ConnectionPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ err_code_t ConnectionPool::waitPoll() {
} else if (rv == 0) {
log_warn("poll timeout. (m_nActiveConn: %d)", m_nActiveConn);
// NOTE: MUST reset all active TCP connections after timeout.
markDeadAll(pollfds, keywords::kPOLL_TIMEOUT);
markDeadAll(pollfds, keywords::kPOLL_TIMEOUT_ERROR);
ret_code = RET_POLL_TIMEOUT_ERR;
break;
} else {
Expand Down
22 changes: 13 additions & 9 deletions src/c_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const char* client_get_realtime_server_address_by_key(void* client, const char*
}


int client_version(void* client, broadcast_result_t** results, size_t* n_hosts) {
err_code_t client_version(void* client, broadcast_result_t** results, size_t* n_hosts) {
douban::mc::Client* c = static_cast<Client*>(client);
return c->version(results, n_hosts);
}
Expand All @@ -59,7 +59,7 @@ void client_destroy_broadcast_result(void* client) {
}

#define IMPL_RETRIEVAL_CMD(M) \
int client_##M(void* client, const char* const* keys, const size_t* key_lens, \
err_code_t client_##M(void* client, const char* const* keys, const size_t* key_lens, \
size_t n_keys, retrieval_result_t*** results, size_t* n_results) { \
douban::mc::Client* c = static_cast<Client*>(client); \
return c->M(keys, key_lens, n_keys, results, n_results); \
Expand All @@ -76,7 +76,7 @@ void client_destroy_retrieval_result(void* client) {


#define IMPL_STORAGE_CMD(M) \
int client_##M(void* client, const char* const* keys, const size_t* key_lens, \
err_code_t client_##M(void* client, const char* const* keys, const size_t* key_lens, \
const flags_t* flags, const exptime_t exptime, \
const cas_unique_t* cas_uniques, const bool noreply, \
const char* const* vals, const size_t* val_lens, \
Expand All @@ -95,7 +95,7 @@ IMPL_STORAGE_CMD(cas)
#undef IMPL_STORAGE_CMD


int client_touch(void* client, const char* const* keys, const size_t* key_lens,
err_code_t client_touch(void* client, const char* const* keys, const size_t* key_lens,
const exptime_t exptime, const bool noreply, size_t n_items,
message_result_t*** results, size_t* n_results) {
douban::mc::Client* c = static_cast<Client*>(client);
Expand All @@ -109,23 +109,23 @@ void client_destroy_message_result(void* client) {
}


int client_delete(void*client, const char* const* keys, const size_t* key_lens,
err_code_t client_delete(void*client, const char* const* keys, const size_t* key_lens,
const bool noreply, size_t n_items,
message_result_t*** results, size_t* n_results) {
douban::mc::Client* c = static_cast<Client*>(client);
return c->_delete(keys, key_lens, noreply, n_items, results, n_results);
}


int client_incr(void* client, const char* key, const size_t keyLen,
err_code_t client_incr(void* client, const char* key, const size_t keyLen,
const uint64_t delta, const bool noreply,
unsigned_result_t** results, size_t* n_results) {
douban::mc::Client* c = static_cast<Client*>(client);
return c->incr(key, keyLen, delta, noreply, results, n_results);
}


int client_decr(void* client, const char* key, const size_t keyLen,
err_code_t client_decr(void* client, const char* key, const size_t keyLen,
const uint64_t delta, const bool noreply,
unsigned_result_t** results, size_t* n_results) {
douban::mc::Client* c = static_cast<Client*>(client);
Expand All @@ -137,12 +137,16 @@ void client_destroy_unsigned_result(void* client) {
return c->destroyUnsignedResult();
}

int client_stats(void* client, broadcast_result_t** results, size_t* n_servers) {
err_code_t client_stats(void* client, broadcast_result_t** results, size_t* n_servers) {
douban::mc::Client* c = static_cast<Client*>(client);
return c->stats(results, n_servers);
}

int client_quit(void* client) {
err_code_t client_quit(void* client) {
douban::mc::Client* c = static_cast<Client*>(client);
return c->quit();
}

const char* err_code_to_string(err_code_t err) {
return douban::mc::errCodeToString(err);
}
Loading

0 comments on commit b69f385

Please sign in to comment.