From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id BA65D45C308 for ; Fri, 20 Nov 2020 02:46:20 +0300 (MSK) From: Vladislav Shpilevoy Date: Fri, 20 Nov 2020 00:46:03 +0100 Message-Id: <15d8075b64e37a983e73691abc67e00b58618b86.1605829282.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v2 15/16] raft: introduce RaftError List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Last piece of src/box used in Raft code was error.h. It was added to be able to raise ClientErrors. To get rid of it the libraries usually introduce their own error type available from src/lib/core. Such as CollationError, SwimError, CryptoError. This patch adds RaftError and removes the last box dependency from Raft code. Part of #5303 --- src/box/raftlib.c | 9 ++++----- src/lib/core/diag.h | 2 ++ src/lib/core/exception.cc | 24 ++++++++++++++++++++++++ src/lib/core/exception.h | 7 +++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/box/raftlib.c b/src/box/raftlib.c index 6e6bc658f..909d61a0a 100644 --- a/src/box/raftlib.c +++ b/src/box/raftlib.c @@ -30,7 +30,7 @@ */ #include "raft.h" -#include "error.h" +#include "exception.h" #include "fiber.h" #include "small/region.h" #include "tt_static.h" @@ -315,14 +315,13 @@ raft_process_msg(struct raft *raft, const struct raft_msg *req, uint32_t source) assert(source > 0); assert(source != raft->self); if (req->term == 0 || req->state == 0) { - diag_set(ClientError, ER_PROTOCOL, "Raft term and state can't " - "be zero"); + diag_set(RaftError, "Raft term and state can't be zero"); return -1; } if (req->state == RAFT_STATE_CANDIDATE && (req->vote != source || req->vclock == NULL)) { - diag_set(ClientError, ER_PROTOCOL, "Candidate should always " - "vote for self and provide its vclock"); + diag_set(RaftError, "Candidate should always vote for self and " + "provide its vclock"); return -1; } /* Outdated request. */ diff --git a/src/lib/core/diag.h b/src/lib/core/diag.h index 6bf0b139a..b07eea838 100644 --- a/src/lib/core/diag.h +++ b/src/lib/core/diag.h @@ -335,6 +335,8 @@ struct error * BuildSwimError(const char *file, unsigned line, const char *format, ...); struct error * BuildCryptoError(const char *file, unsigned line, const char *format, ...); +struct error * +BuildRaftError(const char *file, unsigned line, const char *format, ...); struct index_def; diff --git a/src/lib/core/exception.cc b/src/lib/core/exception.cc index 180cb0e97..395baff6f 100644 --- a/src/lib/core/exception.cc +++ b/src/lib/core/exception.cc @@ -288,6 +288,18 @@ CryptoError::CryptoError(const char *file, unsigned line, va_end(ap); } +const struct type_info type_RaftError = + make_type("RaftError", &type_Exception); + +RaftError::RaftError(const char *file, unsigned line, const char *format, ...) + : Exception(&type_RaftError, file, line) +{ + va_list ap; + va_start(ap, format); + error_vformat_msg(this, format, ap); + va_end(ap); +} + #define BuildAlloc(type) \ void *p = malloc(sizeof(type)); \ if (p == NULL) \ @@ -409,6 +421,18 @@ BuildSocketError(const char *file, unsigned line, const char *socketname, return e; } +struct error * +BuildRaftError(const char *file, unsigned line, const char *format, ...) +{ + BuildAlloc(RaftError); + RaftError *e = new (p) RaftError(file, line, ""); + va_list ap; + va_start(ap, format); + error_vformat_msg(e, format, ap); + va_end(ap); + return e; +} + void exception_init() { diff --git a/src/lib/core/exception.h b/src/lib/core/exception.h index 1947b4f00..7277b2784 100644 --- a/src/lib/core/exception.h +++ b/src/lib/core/exception.h @@ -52,6 +52,7 @@ extern const struct type_info type_SystemError; extern const struct type_info type_CollationError; extern const struct type_info type_SwimError; extern const struct type_info type_CryptoError; +extern const struct type_info type_RaftError; const char * exception_get_string(struct error *e, const struct method_info *method); @@ -168,6 +169,12 @@ public: virtual void raise() { throw this; } }; +class RaftError: public Exception { +public: + RaftError(const char *file, unsigned line, const char *format, ...); + virtual void raise() { throw this; } +}; + /** * Initialize the exception subsystem. */ -- 2.24.3 (Apple Git-128)