From f73ea7cb0aa7809a55914e61faeed16c168dea3d Mon Sep 17 00:00:00 2001 From: XieHan Date: Wed, 15 Sep 2021 14:23:33 +0800 Subject: [PATCH] remove mysql transaction state checking --- src/factory/MySQLTaskImpl.cc | 96 ++++++----------------------------- src/factory/RedisTaskImpl.cc | 2 +- src/factory/WFTaskFactory.inl | 4 ++ 3 files changed, 21 insertions(+), 81 deletions(-) diff --git a/src/factory/MySQLTaskImpl.cc b/src/factory/MySQLTaskImpl.cc index 6fd99c34..bfc8a34f 100644 --- a/src/factory/MySQLTaskImpl.cc +++ b/src/factory/MySQLTaskImpl.cc @@ -32,7 +32,7 @@ using namespace protocol; -#define MYSQL_KEEPALIVE_DEFAULT (180 * 1000) +#define MYSQL_KEEPALIVE_DEFAULT (60 * 1000) #define MYSQL_KEEPALIVE_TRANSACTION (3600 * 1000) /**********Client**********/ @@ -104,50 +104,20 @@ private: std::string password_; std::string db_; std::string res_charset_; - int character_set_; -#define NO_TRANSACTION -1 -#define TRANSACTION_OUT 0 -#define TRANSACTION_IN 1 -#define TRANSACTION_CONN_RESET -2 - short transaction_state_; -#define PREPARE_IN 2 + short character_set_; short state_; int error_; bool is_ssl_; bool is_user_request_; public: - bool is_transaction() const - { - return transaction_state_ == TRANSACTION_IN || - transaction_state_ == TRANSACTION_OUT; - } - ComplexMySQLTask(int retry_max, mysql_callback_t&& callback): WFComplexClientTask(retry_max, std::move(callback)), character_set_(33), - transaction_state_(NO_TRANSACTION), is_user_request_(true) {} }; -static inline bool is_trans_begin(const std::string& cmd) -{ - return strncasecmp(cmd.c_str(), "BEGIN", 5) == 0 || - strncasecmp(cmd.c_str(), "START TRANSACTION", 17) == 0; -} - -static inline bool is_trans_end(const std::string& cmd) -{ - return strncasecmp(cmd.c_str(), "ROLLBACK", 8) == 0 || - strncasecmp(cmd.c_str(), "COMMIT", 6) == 0; -} - -static inline bool is_prepare(const std::string& cmd) -{ - return strncasecmp(cmd.c_str(), "PREPARE", 7) == 0; -} - bool ComplexMySQLTask::check_request() { if (this->req.query_is_unset() == false) @@ -275,54 +245,22 @@ CommMessageOut *ComplexMySQLTask::message_out() } is_user_request_ = true; - if (is_transaction()) + if (this->is_fixed_addr()) { auto *target = (RouteManager::RouteTarget *)this->get_target(); - if (seqid <= 3 && (seqid == 2 || res_charset_.size() != 0) && - (target->state & (TRANSACTION_IN | PREPARE_IN))) + /* If it's a transaction task, generate a ECONNRESET error when + * the target is reconnected. */ + if (seqid <= 3 && (seqid == 2 || res_charset_.size() != 0)) { - target->state = TRANSACTION_OUT; - transaction_state_ = TRANSACTION_CONN_RESET; - errno = ECONNRESET; - return NULL; - } - else - { - bool need_update = false; - - transaction_state_ = (target->state & 1); - bool in_prepare = ((target->state & 2) != 0); - - if (!in_prepare && is_prepare(this->req.get_query())) + if (target->state) { - in_prepare = true; - need_update = true; - } - - if (transaction_state_ == TRANSACTION_OUT) // not begin - { - if (is_trans_begin(this->req.get_query())) - { - transaction_state_ = TRANSACTION_IN; - need_update = true; - } - } - else if (transaction_state_ == TRANSACTION_IN) // already begin - { - if (is_trans_end(this->req.get_query())) - { - transaction_state_ = TRANSACTION_OUT; - need_update = true; - } - } - - if (need_update) - { - target->state = transaction_state_; - if (in_prepare) - target->state |= PREPARE_IN; + target->state = 0; + errno = ECONNRESET; + return NULL; } + else + target->state = 1; } } @@ -634,16 +572,14 @@ bool ComplexMySQLTask::init_success() if (!transaction.empty()) { - transaction_state_ = TRANSACTION_OUT; this->WFComplexClientTask::set_info(std::string("?maxconn=1&") + info + "|txn:" + transaction); - this->fixed_addr_ = true; + this->set_fixed_addr(true); } else { - transaction_state_ = NO_TRANSACTION; this->WFComplexClientTask::set_info(info); - this->fixed_addr_ = false; + this->set_fixed_addr(false); } delete []info; @@ -685,7 +621,7 @@ WFMySQLTask *WFTaskFactory::create_mysql_task(const std::string& url, URIParser::parse(url, uri); task->init(std::move(uri)); - if (task->is_transaction()) + if (task->is_fixed_addr()) task->set_keep_alive(MYSQL_KEEPALIVE_TRANSACTION); else task->set_keep_alive(MYSQL_KEEPALIVE_DEFAULT); @@ -700,7 +636,7 @@ WFMySQLTask *WFTaskFactory::create_mysql_task(const ParsedURI& uri, auto *task = new ComplexMySQLTask(retry_max, std::move(callback)); task->init(uri); - if (task->is_transaction()) + if (task->is_fixed_addr()) task->set_keep_alive(MYSQL_KEEPALIVE_TRANSACTION); else task->set_keep_alive(MYSQL_KEEPALIVE_DEFAULT); diff --git a/src/factory/RedisTaskImpl.cc b/src/factory/RedisTaskImpl.cc index 8acb87fe..a479b0f6 100644 --- a/src/factory/RedisTaskImpl.cc +++ b/src/factory/RedisTaskImpl.cc @@ -26,7 +26,7 @@ using namespace protocol; -#define REDIS_KEEPALIVE_DEFAULT (180 * 1000) +#define REDIS_KEEPALIVE_DEFAULT (60 * 1000) #define REDIS_REDIRECT_MAX 3 /**********Client**********/ diff --git a/src/factory/WFTaskFactory.inl b/src/factory/WFTaskFactory.inl index 17ac2f56..aa562b48 100644 --- a/src/factory/WFTaskFactory.inl +++ b/src/factory/WFTaskFactory.inl @@ -160,7 +160,11 @@ public: init(type, addr, addrlen, info); } + bool is_fixed_addr() const { return this->fixed_addr_; } + protected: + void set_fixed_addr(int fixed) { this->fixed_addr_ = fixed; } + void set_info(const std::string& info) { info_.assign(info);