You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "rajvarun77 (via GitHub)" <gi...@apache.org> on 2026/06/04 19:25:32 UTC

[PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

rajvarun77 opened a new pull request, #3330:
URL: https://github.com/apache/brpc/pull/3330

   Implements a MySQL client protocol for brpc built on a **clean-room** authentication codec (no GPL lineage), addressing #2093.
   
   ### Scope
   - **Clean-room connection-phase auth** — `mysql_native_password` and `caching_sha2_password` (fast-auth + full-auth RSA / secure-transport cleartext), derived from the public MySQL protocol documentation.
   - **COM_QUERY text protocol** — OK / ERR / EOF / text result-set parsing.
   - **Interactive transactions** via connection affinity (reserve/use a pooled connection; SINGLE rejected, auto-rollback on drop).
   - **Prepared statements** (binary protocol) with per-connection `stmt_id` caching and transparent re-prepare when a statement lands on a fresh connection.
   
   ### Shared-core footprint (kept redis-like / minimal)
   The only generic `Controller`/`Socket` additions are: a socket reserve/use hook and `fd_version` (ABA guard) for transaction/prepared-statement affinity, an auth empty-write guard for MySQL's server-greets-first handshake, and protocol registration. **No protocol-specific type lives on the shared `Controller`** (the prepared-statement pointer rides a generic opaque per-RPC slot).
   
   ### Tests
   Clean-room integration tests (transactions, prepared statements, pooled-connection concurrency, connection-type boundary) run against a self-spawned `mysqld`; **19/19 passing locally**. Concurrency coverage includes per-transaction connection pinning under overlap, abort/auto-rollback under contention, and prepared re-prepare when a connection is stolen by a transaction.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3463962904


##########
src/brpc/socket.h:
##########
@@ -840,6 +843,9 @@ friend class TransportFactory;
     butil::atomic<int> _fd;  // -1 when not connected.
     int _tos;                // Type of service which is actually only 8bits.
     int64_t _reset_fd_real_us; // When _fd was reset, in microseconds.
+    // ABA/version counter; written on fd reset and read via fd_version() from
+    // other threads, so use relaxed atomics to avoid a data race.
+    butil::atomic<uint64_t> _fd_version;  // _fd_version, used only for mysql now.

Review Comment:
   Used to track when the underlying physical connection changes. For the new MySQL protocol implementation, we need to detect socket reconnections so we know exactly when to safely reinitialize the MySQL prepared statements.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3384395487


##########
src/brpc/policy/mysql/mysql_common.h:
##########
@@ -0,0 +1,422 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_COMMON_H
+#define BRPC_MYSQL_COMMON_H
+
+#include <sstream>
+#include <map>
+#include "butil/logging.h"  // LOG()
+
+namespace brpc {
+// Msql Collation
+extern const char* MysqlDefaultCollation;
+extern const char* MysqlBinaryCollation;
+const std::map<std::string, uint8_t> MysqlCollations = {
+    {"big5_chinese_ci", 1},
+    {"latin2_czech_cs", 2},

Review Comment:
   Addressed in the current revision — `MysqlCollations` is declared `extern` in `mysql_common.h` and defined once in `mysql_common.cpp`, so there is no in-header definition / ODR issue.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,530 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER
+
+MysqlResponse::MysqlResponse()
+    : NonreflectableMessage<MysqlResponse>() {
+    SharedCtor();
+}
+
+MysqlResponse::MysqlResponse(const MysqlResponse& from)
+    : NonreflectableMessage<MysqlResponse>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlResponse::SharedCtor() {
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+MysqlResponse::~MysqlResponse() {
+    SharedDtor();
+}
+
+void MysqlResponse::SharedDtor() {
+}
+
+void MysqlResponse::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlResponse::Clear() {
+    // Reset all response state so a reused MysqlResponse does not return
+    // stale replies. Mirror what SharedCtor()/ctor initialize.
+    MysqlReply empty_reply;
+    _first_reply.Swap(empty_reply);
+    _other_replies.clear();
+    _arena.clear();
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+size_t MysqlResponse::ByteSizeLong() const {
+    return _cached_size_;
+}
+
+void MysqlResponse::MergeFrom(const MysqlResponse& from) {
+    CHECK_NE(&from, this);
+}

Review Comment:
   Fixed — `MysqlResponse` is now explicitly non-copyable (deleted copy ctor + assignment), and `MergeFrom` is a hard `CHECK`-failure instead of a silent no-op, so an accidental copy/CopyFrom is caught rather than silently dropping parsed replies.



##########
test/mysql/brpc_mysql_connection_type_unittest.cpp:
##########
@@ -0,0 +1,373 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// ===========================================================================
+// brpc MySQL-client CONNECTION-TYPE BOUNDARY integration test.
+//
+// PROVENANCE / CLEAN-ROOM NOTE
+// ----------------------------
+// This is NOT derived from any upstream MySQL/MariaDB test suite.  It asserts
+// a brpc-ARCHITECTURE boundary: how a MySQL prepared statement (whose
+// server-side handle is connection-scoped) interacts with brpc's
+// CONNECTION_TYPE_SHORT (a fresh TCP connection per request).  The data values
+// are this file's own; no upstream test code or structure was copied.
+//
+// THE BOUNDARY (spec fact, asserted -- not derived from our impl)
+// --------------------------------------------------------------
+// A MySQL prepared statement is created with COM_STMT_PREPARE on ONE TCP
+// connection; the server returns a `stmt_id` that is valid ONLY on that exact
+// connection.  COM_STMT_EXECUTE must therefore run on the SAME connection.
+//
+// CONNECTION_TYPE_SHORT opens a brand-new TCP connection for every request and
+// closes it afterwards, so there is no connection affinity across requests.
+// Consequently an execute under SHORT cannot land on the connection that holds
+// the prepared handle -- the prepare/execute affinity is broken by design.
+//
+//   * PreparedStatementUnderShortMustError (PRIMARY):
+//       build a SHORT channel, prepare "SELECT ? AS v", bind one INT param,
+//       CallMethod.  Must ERROR (cntl.Failed() OR reply(0).is_error()); must
+//       NOT return a correct result set; must NOT crash.  Looped a few times.
+//
+//   * PlainQueryUnderShortMustSucceed (POSITIVE CONTROL):
+//       same SHORT channel; a stateless COM_QUERY "SELECT 7 AS v" must SUCCEED
+//       and return 7.  Proves SHORT is fine for stateless queries; only the
+//       connection-scoped prepared-statement handle breaks under SHORT.

Review Comment:
   Addressed — the file-header comment now describes `PreparedStatementUnderShortRePreparesAndSucceeds` (success via transparent re-prepare on each new short connection), matching the actual test.



##########
docs/cn/mysql_client.md:
##########
@@ -0,0 +1,556 @@
+[MySQL](https://www.mysql.com/)是著名的开源的关系型数据库,为了使用户更快捷地访问mysql并充分利用bthread的并发能力,brpc直接支持mysql协议。示例程序:[example/mysql_c++](https://github.com/brpc/brpc/tree/master/example/mysql_c++/)
+
+**注意**:只支持MySQL 4.1 及之后的版本的文本协议,支持事务,不支持Prepared statement。目前支持的鉴权方式为mysql_native_password,使用事务的时候不支持single模式。

Review Comment:
   Addressed — the overview now states that prepared statements are supported, consistent with the Prepared Statement section.



##########
src/brpc/policy/mysql/mysql_statement.cpp:
##########
@@ -0,0 +1,151 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#include <vector>
+#include <gflags/gflags.h>
+#include "brpc/socket.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+DEFINE_int32(mysql_statment_map_size,
+             100,
+             "Mysql statement map size, usually equal to max bthread number");

Review Comment:
   Fixed — the flag is spelled `mysql_statement_map_size` in the current revision (DEFINE + all uses).



##########
src/brpc/policy/mysql/mysql.h:
##########
@@ -0,0 +1,244 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_H
+#define BRPC_MYSQL_H
+
+#include <string>
+#include <vector>
+
+#include "brpc/nonreflectable_message.h"
+#include "brpc/pb_compat.h"
+#include "butil/iobuf.h"
+#include "butil/strings/string_piece.h"
+#include "butil/arena.h"
+#include "brpc/parse_result.h"
+#include "brpc/policy/mysql/mysql_command.h"
+#include "brpc/policy/mysql/mysql_reply.h"
+#include "brpc/policy/mysql/mysql_transaction.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+// Request to mysql.
+// Notice that you can pipeline multiple commands in one request and sent
+// them to ONE mysql-server together.
+// Example:
+//   MysqlRequest request;
+//   request.Query("select * from table");
+//   MysqlResponse response;
+//   channel.CallMethod(NULL, &controller, &request, &response, NULL/*done*/);
+//   if (!cntl.Failed()) {
+//       LOG(INFO) << response.reply(0);
+//   }
+
+class MysqlStatementStub {
+public:
+    MysqlStatementStub(MysqlStatement* stmt);
+    MysqlStatement* stmt();
+    butil::IOBuf& execute_data();
+    butil::Status PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id);
+    // prepare statement null mask
+    struct NullMask {
+        NullMask() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> mask;
+        butil::IOBuf::Area area;
+    };
+    // prepare statement param types
+    struct ParamTypes {
+        ParamTypes() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> types;
+        butil::IOBuf::Area area;
+    };
+    // null mask and param types
+    NullMask& null_mask();
+    ParamTypes& param_types();
+    // save long data
+    void save_long_data(uint16_t param_id, const butil::StringPiece& value);
+
+private:
+    MysqlStatement* _stmt;
+    butil::IOBuf _execute_data;
+    NullMask _null_mask;
+    ParamTypes _param_types;
+    // long data
+    struct LongData {
+        uint16_t param_id;
+        butil::IOBuf long_data;
+    };
+    std::vector<LongData> _long_data;
+};
+
+inline MysqlStatementStub::MysqlStatementStub(MysqlStatement* stmt) : _stmt(stmt) {}
+
+inline MysqlStatement* MysqlStatementStub::stmt() {
+    return _stmt;
+}
+
+inline butil::IOBuf& MysqlStatementStub::execute_data() {
+    return _execute_data;
+}
+
+inline MysqlStatementStub::NullMask& MysqlStatementStub::null_mask() {
+    return _null_mask;
+}
+
+inline MysqlStatementStub::ParamTypes& MysqlStatementStub::param_types() {
+    return _param_types;
+}
+
+inline void MysqlStatementStub::save_long_data(uint16_t param_id, const butil::StringPiece& value) {
+    LongData d;
+    d.param_id = param_id;
+    d.long_data.append(value.data(), value.size());
+    _long_data.push_back(d);
+}
+
+class MysqlRequest : public NonreflectableMessage<MysqlRequest> {
+public:
+    MysqlRequest();
+    MysqlRequest(const MysqlTransaction* tx);
+    MysqlRequest(MysqlStatement* stmt);
+    MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt);
+    ~MysqlRequest() override;
+    MysqlRequest(const MysqlRequest& from);
+    inline MysqlRequest& operator=(const MysqlRequest& from) {
+        CopyFrom(from);
+        return *this;
+    }
+    void Swap(MysqlRequest* other);
+
+    // Serialize the request into `buf'. Return true on success.
+    bool SerializeTo(butil::IOBuf* buf) const;
+
+    // Protobuf methods.
+    void MergeFrom(const MysqlRequest& from) override;
+    void Clear() override;
+
+    size_t ByteSizeLong() const override;
+    int GetCachedSize() const PB_425_OVERRIDE {
+        return _cached_size_;
+    }
+
+    // call query command
+    bool Query(const butil::StringPiece& command);
+    // add statement params
+    bool AddParam(int8_t p);
+    bool AddParam(uint8_t p);
+    bool AddParam(int16_t p);
+    bool AddParam(uint16_t p);
+    bool AddParam(int32_t p);
+    bool AddParam(uint32_t p);
+    bool AddParam(int64_t p);
+    bool AddParam(uint64_t p);
+    bool AddParam(float p);
+    bool AddParam(double p);
+    bool AddParam(const butil::StringPiece& p);
+
+    // True if previous command failed.
+    bool has_error() const {
+        return _has_error;
+    }
+
+    const MysqlTransaction* get_tx() const {

Review Comment:
   Done — renamed `get_tx()` to `tx()` and updated the callers.



##########
src/brpc/policy/mysql/mysql.h:
##########
@@ -0,0 +1,244 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_H
+#define BRPC_MYSQL_H
+
+#include <string>
+#include <vector>
+
+#include "brpc/nonreflectable_message.h"
+#include "brpc/pb_compat.h"
+#include "butil/iobuf.h"
+#include "butil/strings/string_piece.h"
+#include "butil/arena.h"
+#include "brpc/parse_result.h"
+#include "brpc/policy/mysql/mysql_command.h"
+#include "brpc/policy/mysql/mysql_reply.h"
+#include "brpc/policy/mysql/mysql_transaction.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+// Request to mysql.
+// Notice that you can pipeline multiple commands in one request and sent
+// them to ONE mysql-server together.
+// Example:
+//   MysqlRequest request;
+//   request.Query("select * from table");
+//   MysqlResponse response;
+//   channel.CallMethod(NULL, &controller, &request, &response, NULL/*done*/);
+//   if (!cntl.Failed()) {
+//       LOG(INFO) << response.reply(0);
+//   }
+
+class MysqlStatementStub {
+public:
+    MysqlStatementStub(MysqlStatement* stmt);
+    MysqlStatement* stmt();
+    butil::IOBuf& execute_data();
+    butil::Status PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id);
+    // prepare statement null mask
+    struct NullMask {
+        NullMask() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> mask;
+        butil::IOBuf::Area area;
+    };
+    // prepare statement param types
+    struct ParamTypes {
+        ParamTypes() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> types;
+        butil::IOBuf::Area area;
+    };
+    // null mask and param types
+    NullMask& null_mask();
+    ParamTypes& param_types();
+    // save long data
+    void save_long_data(uint16_t param_id, const butil::StringPiece& value);
+
+private:
+    MysqlStatement* _stmt;
+    butil::IOBuf _execute_data;
+    NullMask _null_mask;
+    ParamTypes _param_types;
+    // long data
+    struct LongData {
+        uint16_t param_id;
+        butil::IOBuf long_data;
+    };
+    std::vector<LongData> _long_data;
+};
+
+inline MysqlStatementStub::MysqlStatementStub(MysqlStatement* stmt) : _stmt(stmt) {}
+
+inline MysqlStatement* MysqlStatementStub::stmt() {
+    return _stmt;
+}
+
+inline butil::IOBuf& MysqlStatementStub::execute_data() {
+    return _execute_data;
+}
+
+inline MysqlStatementStub::NullMask& MysqlStatementStub::null_mask() {
+    return _null_mask;
+}
+
+inline MysqlStatementStub::ParamTypes& MysqlStatementStub::param_types() {
+    return _param_types;
+}
+
+inline void MysqlStatementStub::save_long_data(uint16_t param_id, const butil::StringPiece& value) {
+    LongData d;
+    d.param_id = param_id;
+    d.long_data.append(value.data(), value.size());
+    _long_data.push_back(d);
+}
+
+class MysqlRequest : public NonreflectableMessage<MysqlRequest> {
+public:
+    MysqlRequest();
+    MysqlRequest(const MysqlTransaction* tx);
+    MysqlRequest(MysqlStatement* stmt);
+    MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt);
+    ~MysqlRequest() override;
+    MysqlRequest(const MysqlRequest& from);
+    inline MysqlRequest& operator=(const MysqlRequest& from) {
+        CopyFrom(from);
+        return *this;
+    }
+    void Swap(MysqlRequest* other);
+
+    // Serialize the request into `buf'. Return true on success.
+    bool SerializeTo(butil::IOBuf* buf) const;
+
+    // Protobuf methods.
+    void MergeFrom(const MysqlRequest& from) override;
+    void Clear() override;
+
+    size_t ByteSizeLong() const override;
+    int GetCachedSize() const PB_425_OVERRIDE {
+        return _cached_size_;
+    }
+
+    // call query command
+    bool Query(const butil::StringPiece& command);
+    // add statement params
+    bool AddParam(int8_t p);
+    bool AddParam(uint8_t p);
+    bool AddParam(int16_t p);
+    bool AddParam(uint16_t p);
+    bool AddParam(int32_t p);
+    bool AddParam(uint32_t p);
+    bool AddParam(int64_t p);
+    bool AddParam(uint64_t p);
+    bool AddParam(float p);
+    bool AddParam(double p);
+    bool AddParam(const butil::StringPiece& p);
+
+    // True if previous command failed.
+    bool has_error() const {
+        return _has_error;
+    }
+
+    const MysqlTransaction* get_tx() const {
+        return _tx;
+    }
+
+    MysqlStatementStub* get_stmt() const {

Review Comment:
   Done — renamed `get_stmt()` to `stmt()` and updated the callers.



##########
src/brpc/controller.h:
##########
@@ -915,6 +925,16 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
     // Defined at both sides
     StreamSettings *_remote_stream_settings;
 
+    // Whether/how to reserve the sending socket after the RPC (mysql transactions).
+    BindSockAction _bind_sock_action;

Review Comment:
   Done — the `BindSockAction` is now stored in two bits of `Controller::_flags` (`FLAGS_BIND_SOCK_RESERVE`/`FLAGS_BIND_SOCK_USE`) via `set_bind_sock_action()`/`bind_sock_action()`, instead of a dedicated member. No behavior change.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3408167599


##########
src/brpc/socket.cpp:
##########
@@ -578,6 +579,9 @@ int Socket::ResetFileDescriptor(int fd) {
     _avg_msg_size = 0;
     // MUST store `_fd' before adding itself into epoll device to avoid
     // race conditions with the callback function inside epoll
+    static butil::atomic<uint64_t> BAIDU_CACHELINE_ALIGNMENT fd_version(0);
+    _fd_version.store(fd_version.fetch_add(1, butil::memory_order_relaxed),

Review Comment:
   @wwbmmm could you please take a look.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4686301476

   Merged the latest master (which now includes the merged #3310) into this branch and resolved the add/add conflict on `test/brpc_mysql_auth_handshake_unittest.cpp` by keeping master's version — it is identical to this branch's copy plus the explicit `main()` that #3310 landed (parses the `-mysql_*` flags for the running-server mode). The branch is mergeable again and CI is re-running.
   
   @wwbmmm @yanglimingcn — ready for another look when you have a moment.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4863123709

   @wwbmmm @yanglimingcn @chenBright — addressed the `ResetPods`/`bind_sock_action` comment in `530e1124`; all 15 CI lanes green (incl. the bazel lanes that flaked on `ChannelTest.backup_request`). PTAL and merge if it looks good. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3534083174


##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================

Review Comment:
   Please remove redundant comments.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER
+
+MysqlResponse::MysqlResponse()
+    : NonreflectableMessage<MysqlResponse>() {
+    SharedCtor();
+}
+
+void MysqlResponse::SharedCtor() {
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+MysqlResponse::~MysqlResponse() {
+    SharedDtor();
+}
+
+void MysqlResponse::SharedDtor() {
+}
+
+void MysqlResponse::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlResponse::Clear() {
+    // Reset all response state so a reused MysqlResponse does not return
+    // stale replies. Mirror what SharedCtor()/ctor initialize.
+    MysqlReply empty_reply;
+    _first_reply.Swap(empty_reply);
+    _other_replies.clear();
+    _arena.clear();
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+size_t MysqlResponse::ByteSizeLong() const {
+    return _cached_size_;
+}
+
+void MysqlResponse::MergeFrom(const MysqlResponse&) {
+    CHECK(false) << "MysqlResponse does not support MergeFrom/CopyFrom";
+}
+
+bool MysqlResponse::IsInitialized() const {
+    return true;
+}
+
+void MysqlResponse::Swap(MysqlResponse* other) {
+    if (other != this) {
+        _first_reply.Swap(other->_first_reply);
+        std::swap(_other_replies, other->_other_replies);
+        _arena.swap(other->_arena);
+        std::swap(_nreply, other->_nreply);
+        std::swap(_cached_size_, other->_cached_size_);
+    }
+}
+
+// ===================================================================

Review Comment:
   Please remove redundant comments.



##########
src/brpc/controller.cpp:
##########
@@ -297,6 +297,9 @@ void Controller::ResetPods() {
     _request_streams.clear();
     _response_streams.clear();
     _remote_stream_settings = NULL;
+    set_bind_sock_action(BIND_SOCK_NONE);

Review Comment:
   Since `_flags = 0` has already been set in line 244, there's no need to call `set_bind_sock_action` again.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER

Review Comment:
   Please remove unused code.



##########
src/brpc/controller.cpp:
##########
@@ -297,6 +297,9 @@ void Controller::ResetPods() {
     _request_streams.clear();
     _response_streams.clear();
     _remote_stream_settings = NULL;
+    set_bind_sock_action(BIND_SOCK_NONE);
+    _bind_sock.reset();

Review Comment:
   `_bind_sock.reset();` should be called in `ResetNonPods()`.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================

Review Comment:
   Please remove redundant comments.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4720251359

   Okay, I'll review it when I have time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3360078206


##########
src/brpc/policy/mysql/mysql_common.h:
##########
@@ -0,0 +1,422 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_COMMON_H
+#define BRPC_MYSQL_COMMON_H
+
+#include <sstream>
+#include <map>
+#include "butil/logging.h"  // LOG()
+
+namespace brpc {
+// Msql Collation
+extern const char* MysqlDefaultCollation;
+extern const char* MysqlBinaryCollation;
+const std::map<std::string, uint8_t> MysqlCollations = {
+    {"big5_chinese_ci", 1},
+    {"latin2_czech_cs", 2},

Review Comment:
   `MysqlCollations` is defined (and initialized) in this header as a non-`inline` global variable. In C++11/14 this will cause an ODR violation / multiple-definition linker error when the header is included by more than one translation unit.
   
   Move the definition into `mysql_common.cpp` and keep only an `extern` declaration in the header (or make it an `inline` variable if the project requires C++17+).



##########
test/mysql/brpc_mysql_connection_type_unittest.cpp:
##########
@@ -0,0 +1,373 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// ===========================================================================
+// brpc MySQL-client CONNECTION-TYPE BOUNDARY integration test.
+//
+// PROVENANCE / CLEAN-ROOM NOTE
+// ----------------------------
+// This is NOT derived from any upstream MySQL/MariaDB test suite.  It asserts
+// a brpc-ARCHITECTURE boundary: how a MySQL prepared statement (whose
+// server-side handle is connection-scoped) interacts with brpc's
+// CONNECTION_TYPE_SHORT (a fresh TCP connection per request).  The data values
+// are this file's own; no upstream test code or structure was copied.
+//
+// THE BOUNDARY (spec fact, asserted -- not derived from our impl)
+// --------------------------------------------------------------
+// A MySQL prepared statement is created with COM_STMT_PREPARE on ONE TCP
+// connection; the server returns a `stmt_id` that is valid ONLY on that exact
+// connection.  COM_STMT_EXECUTE must therefore run on the SAME connection.
+//
+// CONNECTION_TYPE_SHORT opens a brand-new TCP connection for every request and
+// closes it afterwards, so there is no connection affinity across requests.
+// Consequently an execute under SHORT cannot land on the connection that holds
+// the prepared handle -- the prepare/execute affinity is broken by design.
+//
+//   * PreparedStatementUnderShortMustError (PRIMARY):
+//       build a SHORT channel, prepare "SELECT ? AS v", bind one INT param,
+//       CallMethod.  Must ERROR (cntl.Failed() OR reply(0).is_error()); must
+//       NOT return a correct result set; must NOT crash.  Looped a few times.
+//
+//   * PlainQueryUnderShortMustSucceed (POSITIVE CONTROL):
+//       same SHORT channel; a stateless COM_QUERY "SELECT 7 AS v" must SUCCEED
+//       and return 7.  Proves SHORT is fine for stateless queries; only the
+//       connection-scoped prepared-statement handle breaks under SHORT.

Review Comment:
   The file header comment still describes a PRIMARY test named `PreparedStatementUnderShortMustError` and claims prepared statements cannot work with `CONNECTION_TYPE_SHORT`. The actual test below is `PreparedStatementUnderShortRePreparesAndSucceeds` and expects success via transparent re-prepare on each new short connection. Please update the comment to match the intended behavior being asserted.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,530 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER
+
+MysqlResponse::MysqlResponse()
+    : NonreflectableMessage<MysqlResponse>() {
+    SharedCtor();
+}
+
+MysqlResponse::MysqlResponse(const MysqlResponse& from)
+    : NonreflectableMessage<MysqlResponse>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlResponse::SharedCtor() {
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+MysqlResponse::~MysqlResponse() {
+    SharedDtor();
+}
+
+void MysqlResponse::SharedDtor() {
+}
+
+void MysqlResponse::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlResponse::Clear() {
+    // Reset all response state so a reused MysqlResponse does not return
+    // stale replies. Mirror what SharedCtor()/ctor initialize.
+    MysqlReply empty_reply;
+    _first_reply.Swap(empty_reply);
+    _other_replies.clear();
+    _arena.clear();
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+size_t MysqlResponse::ByteSizeLong() const {
+    return _cached_size_;
+}
+
+void MysqlResponse::MergeFrom(const MysqlResponse& from) {
+    CHECK_NE(&from, this);
+}

Review Comment:
   `MysqlResponse::MergeFrom` is effectively a no-op. Because `NonreflectableMessage::CopyFrom()` calls `Clear()` then `MergeFrom()`, this means copy-construction / assignment (and any internal protobuf `CopyFrom`) will silently drop all parsed replies.
   
   Either implement a real deep copy (including owned reply data), or explicitly make `MysqlResponse` non-copyable (delete copy ctor/assignment and make `MergeFrom` fail loudly) so callers don’t get empty responses by accident.



##########
src/brpc/policy/mysql/mysql_statement.cpp:
##########
@@ -0,0 +1,151 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#include <vector>
+#include <gflags/gflags.h>
+#include "brpc/socket.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+DEFINE_int32(mysql_statment_map_size,
+             100,
+             "Mysql statement map size, usually equal to max bthread number");

Review Comment:
   The gflag name `mysql_statment_map_size` appears to be misspelled (“statment”). Since this is a user-facing flag name and it’s newly introduced, it’s worth renaming to `mysql_statement_map_size` (and updating the corresponding `DECLARE_`/`FLAGS_` uses) before it becomes part of a stable interface.



##########
docs/cn/mysql_client.md:
##########
@@ -0,0 +1,556 @@
+[MySQL](https://www.mysql.com/)是著名的开源的关系型数据库,为了使用户更快捷地访问mysql并充分利用bthread的并发能力,brpc直接支持mysql协议。示例程序:[example/mysql_c++](https://github.com/brpc/brpc/tree/master/example/mysql_c++/)
+
+**注意**:只支持MySQL 4.1 及之后的版本的文本协议,支持事务,不支持Prepared statement。目前支持的鉴权方式为mysql_native_password,使用事务的时候不支持single模式。

Review Comment:
   The overview says “不支持Prepared statement”, but this doc later includes a full “Prepared Statement” section and the PR adds prepared statement support. This line should be updated to avoid contradicting the rest of the document and the implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4761214870

   LGTM


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3369049434


##########
src/brpc/socket.cpp:
##########
@@ -578,6 +579,9 @@ int Socket::ResetFileDescriptor(int fd) {
     _avg_msg_size = 0;
     // MUST store `_fd' before adding itself into epoll device to avoid
     // race conditions with the callback function inside epoll
+    static butil::atomic<uint64_t> BAIDU_CACHELINE_ALIGNMENT fd_version(0);
+    _fd_version.store(fd_version.fetch_add(1, butil::memory_order_relaxed),

Review Comment:
   It seems that this _fd_version doesn't need to be atomic, since the static fd_version is atomic.



##########
src/brpc/controller.h:
##########
@@ -915,6 +925,16 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
     // Defined at both sides
     StreamSettings *_remote_stream_settings;
 
+    // Whether/how to reserve the sending socket after the RPC (mysql transactions).
+    BindSockAction _bind_sock_action;

Review Comment:
   I think this can be a flag in `Controller::_flags`, no need to add a new member.



##########
src/brpc/policy/mysql/mysql.h:
##########
@@ -0,0 +1,244 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_H
+#define BRPC_MYSQL_H
+
+#include <string>
+#include <vector>
+
+#include "brpc/nonreflectable_message.h"
+#include "brpc/pb_compat.h"
+#include "butil/iobuf.h"
+#include "butil/strings/string_piece.h"
+#include "butil/arena.h"
+#include "brpc/parse_result.h"
+#include "brpc/policy/mysql/mysql_command.h"
+#include "brpc/policy/mysql/mysql_reply.h"
+#include "brpc/policy/mysql/mysql_transaction.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+// Request to mysql.
+// Notice that you can pipeline multiple commands in one request and sent
+// them to ONE mysql-server together.
+// Example:
+//   MysqlRequest request;
+//   request.Query("select * from table");
+//   MysqlResponse response;
+//   channel.CallMethod(NULL, &controller, &request, &response, NULL/*done*/);
+//   if (!cntl.Failed()) {
+//       LOG(INFO) << response.reply(0);
+//   }
+
+class MysqlStatementStub {
+public:
+    MysqlStatementStub(MysqlStatement* stmt);
+    MysqlStatement* stmt();
+    butil::IOBuf& execute_data();
+    butil::Status PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id);
+    // prepare statement null mask
+    struct NullMask {
+        NullMask() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> mask;
+        butil::IOBuf::Area area;
+    };
+    // prepare statement param types
+    struct ParamTypes {
+        ParamTypes() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> types;
+        butil::IOBuf::Area area;
+    };
+    // null mask and param types
+    NullMask& null_mask();
+    ParamTypes& param_types();
+    // save long data
+    void save_long_data(uint16_t param_id, const butil::StringPiece& value);
+
+private:
+    MysqlStatement* _stmt;
+    butil::IOBuf _execute_data;
+    NullMask _null_mask;
+    ParamTypes _param_types;
+    // long data
+    struct LongData {
+        uint16_t param_id;
+        butil::IOBuf long_data;
+    };
+    std::vector<LongData> _long_data;
+};
+
+inline MysqlStatementStub::MysqlStatementStub(MysqlStatement* stmt) : _stmt(stmt) {}
+
+inline MysqlStatement* MysqlStatementStub::stmt() {
+    return _stmt;
+}
+
+inline butil::IOBuf& MysqlStatementStub::execute_data() {
+    return _execute_data;
+}
+
+inline MysqlStatementStub::NullMask& MysqlStatementStub::null_mask() {
+    return _null_mask;
+}
+
+inline MysqlStatementStub::ParamTypes& MysqlStatementStub::param_types() {
+    return _param_types;
+}
+
+inline void MysqlStatementStub::save_long_data(uint16_t param_id, const butil::StringPiece& value) {
+    LongData d;
+    d.param_id = param_id;
+    d.long_data.append(value.data(), value.size());
+    _long_data.push_back(d);
+}
+
+class MysqlRequest : public NonreflectableMessage<MysqlRequest> {
+public:
+    MysqlRequest();
+    MysqlRequest(const MysqlTransaction* tx);
+    MysqlRequest(MysqlStatement* stmt);
+    MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt);
+    ~MysqlRequest() override;
+    MysqlRequest(const MysqlRequest& from);
+    inline MysqlRequest& operator=(const MysqlRequest& from) {
+        CopyFrom(from);
+        return *this;
+    }
+    void Swap(MysqlRequest* other);
+
+    // Serialize the request into `buf'. Return true on success.
+    bool SerializeTo(butil::IOBuf* buf) const;
+
+    // Protobuf methods.
+    void MergeFrom(const MysqlRequest& from) override;
+    void Clear() override;
+
+    size_t ByteSizeLong() const override;
+    int GetCachedSize() const PB_425_OVERRIDE {
+        return _cached_size_;
+    }
+
+    // call query command
+    bool Query(const butil::StringPiece& command);
+    // add statement params
+    bool AddParam(int8_t p);
+    bool AddParam(uint8_t p);
+    bool AddParam(int16_t p);
+    bool AddParam(uint16_t p);
+    bool AddParam(int32_t p);
+    bool AddParam(uint32_t p);
+    bool AddParam(int64_t p);
+    bool AddParam(uint64_t p);
+    bool AddParam(float p);
+    bool AddParam(double p);
+    bool AddParam(const butil::StringPiece& p);
+
+    // True if previous command failed.
+    bool has_error() const {
+        return _has_error;
+    }
+
+    const MysqlTransaction* get_tx() const {
+        return _tx;
+    }
+
+    MysqlStatementStub* get_stmt() const {

Review Comment:
   simple getter, just use `stmt()`



##########
src/brpc/policy/mysql/mysql.h:
##########
@@ -0,0 +1,244 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#ifndef BRPC_MYSQL_H
+#define BRPC_MYSQL_H
+
+#include <string>
+#include <vector>
+
+#include "brpc/nonreflectable_message.h"
+#include "brpc/pb_compat.h"
+#include "butil/iobuf.h"
+#include "butil/strings/string_piece.h"
+#include "butil/arena.h"
+#include "brpc/parse_result.h"
+#include "brpc/policy/mysql/mysql_command.h"
+#include "brpc/policy/mysql/mysql_reply.h"
+#include "brpc/policy/mysql/mysql_transaction.h"
+#include "brpc/policy/mysql/mysql_statement.h"
+
+namespace brpc {
+// Request to mysql.
+// Notice that you can pipeline multiple commands in one request and sent
+// them to ONE mysql-server together.
+// Example:
+//   MysqlRequest request;
+//   request.Query("select * from table");
+//   MysqlResponse response;
+//   channel.CallMethod(NULL, &controller, &request, &response, NULL/*done*/);
+//   if (!cntl.Failed()) {
+//       LOG(INFO) << response.reply(0);
+//   }
+
+class MysqlStatementStub {
+public:
+    MysqlStatementStub(MysqlStatement* stmt);
+    MysqlStatement* stmt();
+    butil::IOBuf& execute_data();
+    butil::Status PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id);
+    // prepare statement null mask
+    struct NullMask {
+        NullMask() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> mask;
+        butil::IOBuf::Area area;
+    };
+    // prepare statement param types
+    struct ParamTypes {
+        ParamTypes() : area(butil::IOBuf::INVALID_AREA) {}
+        std::vector<uint8_t> types;
+        butil::IOBuf::Area area;
+    };
+    // null mask and param types
+    NullMask& null_mask();
+    ParamTypes& param_types();
+    // save long data
+    void save_long_data(uint16_t param_id, const butil::StringPiece& value);
+
+private:
+    MysqlStatement* _stmt;
+    butil::IOBuf _execute_data;
+    NullMask _null_mask;
+    ParamTypes _param_types;
+    // long data
+    struct LongData {
+        uint16_t param_id;
+        butil::IOBuf long_data;
+    };
+    std::vector<LongData> _long_data;
+};
+
+inline MysqlStatementStub::MysqlStatementStub(MysqlStatement* stmt) : _stmt(stmt) {}
+
+inline MysqlStatement* MysqlStatementStub::stmt() {
+    return _stmt;
+}
+
+inline butil::IOBuf& MysqlStatementStub::execute_data() {
+    return _execute_data;
+}
+
+inline MysqlStatementStub::NullMask& MysqlStatementStub::null_mask() {
+    return _null_mask;
+}
+
+inline MysqlStatementStub::ParamTypes& MysqlStatementStub::param_types() {
+    return _param_types;
+}
+
+inline void MysqlStatementStub::save_long_data(uint16_t param_id, const butil::StringPiece& value) {
+    LongData d;
+    d.param_id = param_id;
+    d.long_data.append(value.data(), value.size());
+    _long_data.push_back(d);
+}
+
+class MysqlRequest : public NonreflectableMessage<MysqlRequest> {
+public:
+    MysqlRequest();
+    MysqlRequest(const MysqlTransaction* tx);
+    MysqlRequest(MysqlStatement* stmt);
+    MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt);
+    ~MysqlRequest() override;
+    MysqlRequest(const MysqlRequest& from);
+    inline MysqlRequest& operator=(const MysqlRequest& from) {
+        CopyFrom(from);
+        return *this;
+    }
+    void Swap(MysqlRequest* other);
+
+    // Serialize the request into `buf'. Return true on success.
+    bool SerializeTo(butil::IOBuf* buf) const;
+
+    // Protobuf methods.
+    void MergeFrom(const MysqlRequest& from) override;
+    void Clear() override;
+
+    size_t ByteSizeLong() const override;
+    int GetCachedSize() const PB_425_OVERRIDE {
+        return _cached_size_;
+    }
+
+    // call query command
+    bool Query(const butil::StringPiece& command);
+    // add statement params
+    bool AddParam(int8_t p);
+    bool AddParam(uint8_t p);
+    bool AddParam(int16_t p);
+    bool AddParam(uint16_t p);
+    bool AddParam(int32_t p);
+    bool AddParam(uint32_t p);
+    bool AddParam(int64_t p);
+    bool AddParam(uint64_t p);
+    bool AddParam(float p);
+    bool AddParam(double p);
+    bool AddParam(const butil::StringPiece& p);
+
+    // True if previous command failed.
+    bool has_error() const {
+        return _has_error;
+    }
+
+    const MysqlTransaction* get_tx() const {

Review Comment:
   simple getter, just use `tx()`



##########
src/brpc/policy/mysql/mysql_protocol.cpp:
##########
@@ -0,0 +1,536 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#include <google/protobuf/descriptor.h>  // MethodDescriptor
+#include <google/protobuf/message.h>     // Message
+#include <gflags/gflags.h>
+#include <sstream>
+#include "butil/logging.h"  // LOG()
+#include "butil/time.h"
+#include "butil/iobuf.h"  // butil::IOBuf
+#include "butil/sys_byteorder.h"
+#include "brpc/controller.h"  // Controller
+#include "brpc/details/controller_private_accessor.h"
+#include "brpc/socket.h"  // Socket
+#include "brpc/server.h"  // Server
+#include "brpc/details/server_private_accessor.h"
+#include "brpc/span.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_authenticator.h"
+#include "brpc/policy/mysql/mysql_protocol.h"
+#include "brpc/policy/mysql/mysql_auth_scramble.h"
+
+namespace brpc {
+
+DECLARE_bool(enable_rpcz);
+
+namespace policy {
+
+DEFINE_bool(mysql_verbose, false, "[DEBUG] Print EVERY mysql request/response");
+
+void MysqlParseAuthenticator(const butil::StringPiece& raw,
+                             std::string* user,
+                             std::string* password,
+                             std::string* schema,
+                             std::string* collation);
+void MysqlParseParams(const butil::StringPiece& raw, std::string* params);
+// pack mysql authentication_data
+int MysqlPackAuthenticator(const MysqlReply::Auth& auth,
+                           const butil::StringPiece& user,
+                           const butil::StringPiece& password,
+                           const butil::StringPiece& schema,
+                           const butil::StringPiece& collation,
+                           std::string* auth_cmd);
+int MysqlPackParams(const butil::StringPiece& params, std::string* param_cmd);
+
+namespace {
+// The connection-phase handshake spans several packets, so it needs per-connection
+// (not per-RPC) scratch state. Rather than add a field to the shared Controller, we
+// reuse the per-connection AuthContext: group() tracks the auth step, and (for
+// caching_sha2_password below) roles() stashes the salt across the RSA round trip.
+const char* auth_step[] = {"AUTH_OK", "PARAMS_OK"};
+
+// Extra AuthContext group/state markers for the caching_sha2_password
+// multi-round-trip exchange.  After the client sends the 32-byte fast
+// scramble in its HandshakeResponse41 (group still default/empty), the
+// server may answer with an AuthMoreData status byte.  These markers track
+// where we are in that follow-up handshake so re-entries pick the right
+// branch:
+//   CACHE_SHA2_SENT   : sent the fast scramble; awaiting the server's
+//                       AuthMoreData (0x03 fast-auth / 0x04 full-auth) or OK.
+//   CACHE_SHA2_PUBKEY : on plain TCP full-auth, we requested the RSA public
+//                       key (sent 0x02); awaiting the AuthMoreData carrying
+//                       the PEM, after which we send the RSA-encrypted pw.
+const char* kCacheSha2Sent = "CACHE_SHA2_SENT";
+const char* kCacheSha2Pubkey = "CACHE_SHA2_PUBKEY";
+
+// Frames |payload| as a single MySQL packet: 3-byte little-endian payload
+// length + 1-byte sequence id, then the payload, and writes it to |fd|.
+// |seq| is the sequence id the packet must carry (the previous server
+// packet's seq + 1, per the MySQL packet-sequence rule).
+static void WriteMysqlAuthPacket(int fd, const std::string& payload, uint8_t seq) {
+    butil::IOBuf buf;
+    const uint32_t len = butil::ByteSwapToLE32((uint32_t)payload.size());
+    buf.append(&len, 3);
+    buf.push_back((char)seq);
+    buf.append(payload);
+    buf.cut_into_file_descriptor(fd);
+}
+
+struct InputResponse : public InputMessageBase {
+    bthread_id_t id_wait;
+    MysqlResponse response;
+
+    // @InputMessageBase
+    void DestroyImpl() {
+        delete this;
+    }
+};
+
+bool PackRequest(butil::IOBuf* buf,
+                 ControllerPrivateAccessor& accessor,
+                 const butil::IOBuf& request) {
+    if (accessor.pipelined_count() == MYSQL_PREPARED_STATEMENT) {
+        Socket* sock = accessor.get_sending_socket();
+        if (sock == NULL) {
+            LOG(ERROR) << "[MYSQL PACK] get sending socket with NULL";
+            return false;
+        }
+        auto stub = static_cast<MysqlStatementStub*>(accessor.session_data());
+        if (stub == NULL) {
+            LOG(ERROR) << "[MYSQL PACK] get prepare statement with NULL";
+            return false;
+        }
+        uint32_t stmt_id;
+        // if can't found stmt_id in this socket, create prepared statement on it, store user
+        // request.
+        if ((stmt_id = stub->stmt()->StatementId(sock->id())) == 0) {
+            butil::IOBuf b;
+            butil::Status st = MysqlMakeCommand(&b, MYSQL_COM_STMT_PREPARE, stub->stmt()->str());
+            if (!st.ok()) {
+                LOG(ERROR) << "[MYSQL PACK] make prepare statement error " << st;
+                return false;
+            }
+            accessor.set_pipelined_count(MYSQL_NEED_PREPARE);
+            buf->append(b);
+            return true;
+        }
+        // else pack execute header with stmt_id
+        butil::Status st = stub->PackExecuteCommand(buf, stmt_id);
+        if (!st.ok()) {
+            LOG(ERROR) << "write execute data error " << st;
+            return false;
+        }
+        return true;
+    }
+    buf->append(request);
+    return true;
+}
+
+ParseError HandleAuthentication(const InputResponse* msg, const Socket* socket, PipelinedInfo* pi) {
+    const bthread_id_t cid = pi->id_wait;
+    Controller* cntl = NULL;
+    if (bthread_id_lock(cid, (void**)&cntl) != 0) {
+        LOG(ERROR) << "[MYSQL PARSE] fail to lock controller";
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+
+    ParseError parseCode = PARSE_OK;
+    const AuthContext* ctx = socket->auth_context();
+    if (ctx == NULL) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] auth context is null";
+        goto END_OF_AUTH;
+    }
+    if (msg->response.reply(0).is_auth()) {
+        std::string user, password, schema, collation, auth_cmd;
+        const MysqlReply& reply = msg->response.reply(0);
+        MysqlParseAuthenticator(ctx->user(), &user, &password, &schema, &collation);
+        if (MysqlPackAuthenticator(reply.auth(), user, password, schema, collation, &auth_cmd) ==
+            0) {
+            butil::IOBuf buf;
+            buf.append(auth_cmd);
+            const ssize_t nw = buf.cut_into_file_descriptor(socket->fd());
+            if (nw < 0 || !buf.empty()) {
+                LOG(WARNING) << "[MYSQL PARSE] failed to write auth command to fd="
+                             << socket->fd() << ", nw=" << nw
+                             << ", remaining=" << buf.size();
+            }
+            const bool is_caching_sha2 = (reply.auth().auth_plugin() == "caching_sha2_password");
+            if (is_caching_sha2) {
+                // caching_sha2_password is a multi-round-trip exchange: stash
+                // the 20-byte salt (greeting salt + salt2) for a later RSA
+                // full-auth round, and mark that the fast scramble was sent.
+                // _roles is otherwise unused on the mysql path.
+                std::string salt;
+                salt.append(reply.auth().salt().data(), reply.auth().salt().size());
+                salt.append(reply.auth().salt2().data(), reply.auth().salt2().size());
+                const_cast<AuthContext*>(ctx)->set_roles(salt);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+            } else {
+                const_cast<AuthContext*>(ctx)->set_group(auth_step[0]);
+            }
+        } else {
+            parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+            LOG(ERROR) << "[MYSQL PARSE] wrong pack authentication data";
+        }
+    } else if (msg->response.reply(0).is_auth_more_data()) {
+        // caching_sha2_password follow-up packet (server -> client).  The
+        // first data byte after the 0x01 tag is a status marker, except when
+        // we are awaiting the RSA public key (CACHE_SHA2_PUBKEY), in which
+        // case the whole payload is the PEM public key.
+        std::string user, password, schema, collation;
+        MysqlParseAuthenticator(ctx->user(), &user, &password, &schema, &collation);
+        const MysqlReply::AuthMoreData& amd = msg->response.reply(0).auth_more_data();
+        const butil::StringPiece data = amd.data();
+        const uint8_t next_seq = (uint8_t)(amd.seq() + 1);
+        if (ctx->group() == kCacheSha2Pubkey) {
+            // The payload is the server's PEM RSA public key.  Encrypt the
+            // password with it (plain-TCP full-auth) and send the ciphertext.
+            const std::string rsa = mysql::CachingSha2PasswordRsaEncrypt(
+                data, ctx->roles(), password);
+            if (rsa.empty()) {
+                parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+                LOG(ERROR) << "[MYSQL PARSE] failed to RSA-encrypt caching_sha2 password";
+                goto END_OF_AUTH;
+            }
+            WriteMysqlAuthPacket(socket->fd(), rsa, next_seq);
+            // Stay in CACHE_SHA2_SENT-equivalent: the server replies OK next.
+            const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+        } else if (!data.empty() && (uint8_t)data[0] == 0x03) {
+            // fast_auth_success: server will send OK next; send nothing.
+            const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+        } else if (!data.empty() && (uint8_t)data[0] == 0x04) {
+            // perform_full_authentication.
+            if (socket->is_ssl()) {
+                // Secure channel: send the cleartext password (one round trip).
+                const std::string clear = mysql::CachingSha2PasswordCleartext(password);
+                WriteMysqlAuthPacket(socket->fd(), clear, next_seq);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+            } else {
+                // Plain TCP: request the server's RSA public key (0x02), then
+                // wait for the AuthMoreData carrying the PEM.
+                WriteMysqlAuthPacket(socket->fd(), std::string(1, (char)0x02), next_seq);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Pubkey);
+            }
+        } else {
+            parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+            LOG(ERROR) << "[MYSQL PARSE] unexpected caching_sha2 AuthMoreData marker";
+        }
+    } else if (msg->response.reply_size() > 0) {
+        for (size_t i = 0; i < msg->response.reply_size(); ++i) {
+            if (!msg->response.reply(i).is_ok()) {
+                LOG(ERROR) << "[MYSQL PARSE] auth failed " << msg->response;
+                parseCode = PARSE_ERROR_NO_RESOURCE;
+                goto END_OF_AUTH;
+            }
+        }
+        std::string params, params_cmd;
+        MysqlParseParams(ctx->user(), &params);
+        // Auth just completed (either native's single round trip, group
+        // AUTH_OK, or caching_sha2's multi round-trip, group CACHE_SHA2_SENT)
+        // and connection params have not been sent yet: send them now.
+        const bool auth_just_done =
+            (ctx->group() == auth_step[0] || ctx->group() == kCacheSha2Sent);
+        if (auth_just_done && !params.empty()) {
+            if (MysqlPackParams(params, &params_cmd) == 0) {
+                butil::IOBuf buf;
+                buf.append(params_cmd);
+                buf.cut_into_file_descriptor(socket->fd());
+                const_cast<AuthContext*>(ctx)->set_group(auth_step[1]);
+            } else {
+                parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+                LOG(ERROR) << "[MYSQL PARSE] wrong pack params data";
+            }
+        } else {
+            butil::IOBuf raw_req;
+            raw_req.append(ctx->starter());
+            raw_req.cut_into_file_descriptor(socket->fd());
+            pi->auth_flags = 0;
+        }
+    } else {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] wrong authentication step";
+    }
+
+END_OF_AUTH:
+    if (bthread_id_unlock(cid) != 0) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] fail to unlock controller";
+    }
+    return parseCode;
+}
+
+ParseError HandlePrepareStatement(const InputResponse* msg,
+                                  const Socket* socket,
+                                  PipelinedInfo* pi) {
+    if (!msg->response.reply(0).is_prepare_ok()) {
+        LOG(ERROR) << "[MYSQL PARSE] response is not prepare ok, " << msg->response;
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+    const MysqlReply::PrepareOk& ok = msg->response.reply(0).prepare_ok();
+    const bthread_id_t cid = pi->id_wait;
+    Controller* cntl = NULL;
+    if (bthread_id_lock(cid, (void**)&cntl) != 0) {
+        LOG(ERROR) << "[MYSQL PARSE] fail to lock controller";
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+    ParseError parseCode = PARSE_OK;
+    butil::IOBuf buf;
+    butil::Status st;
+    MysqlStatementStub* stub = NULL;
+    MysqlStatement* stmt = NULL;
+    stub = static_cast<MysqlStatementStub*>(ControllerPrivateAccessor(cntl).session_data());
+    if (stub == NULL) {
+        LOG(ERROR) << "[MYSQL PACK] get prepare statement with NULL";
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    stmt = stub->stmt();
+    if (stmt == NULL || stmt->param_count() != ok.param_count()) {
+        LOG(ERROR) << "[MYSQL PACK] stmt can't be NULL";
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    if (stmt->param_count() != ok.param_count()) {
+        LOG(ERROR) << "[MYSQL PACK] stmt param number " << stmt->param_count()
+                   << " not equal to prepareOk.param_number " << ok.param_count();
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    stmt->SetStatementId(socket->id(), ok.stmt_id());
+    st = stub->PackExecuteCommand(&buf, ok.stmt_id());
+    if (!st.ok()) {
+        LOG(ERROR) << "[MYSQL PACK] make execute header error " << st;
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    {
+        const ssize_t nw = buf.cut_into_file_descriptor(socket->fd());
+        if (nw < 0 || !buf.empty()) {
+            LOG(WARNING) << "[MYSQL PARSE] failed to write execute command to fd="
+                         << socket->fd() << ", nw=" << nw
+                         << ", remaining=" << buf.size();
+        }
+    }
+    pi->count = MYSQL_PREPARED_STATEMENT;
+END_OF_PREPARE:
+    if (bthread_id_unlock(cid) != 0) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] fail to unlock controller";
+    }
+    return parseCode;
+}
+
+}  // namespace
+
+// "Message" = "Response" as we only implement the client for mysql.
+ParseResult ParseMysqlMessage(butil::IOBuf* source,
+                              Socket* socket,
+                              bool /*read_eof*/,
+                              const void* /*arg*/) {
+    if (source->empty()) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+
+    PipelinedInfo pi;
+    if (!socket->PopPipelinedInfo(&pi)) {
+        LOG(WARNING) << "No corresponding PipelinedInfo in socket";
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+
+    InputResponse* msg = static_cast<InputResponse*>(socket->parsing_context());
+    if (msg == NULL) {
+        msg = new InputResponse;
+        socket->reset_parsing_context(msg);
+    }
+
+    MysqlStmtType stmt_type = static_cast<MysqlStmtType>(pi.count);
+    ParseError err = msg->response.ConsumePartialIOBuf(*source, pi.auth_flags != 0, stmt_type);
+    if (FLAGS_mysql_verbose) {
+        LOG(INFO) << "[MYSQL PARSE] " << msg->response;
+    }
+    if (err != PARSE_OK) {
+        if (err == PARSE_ERROR_NOT_ENOUGH_DATA) {
+            socket->GivebackPipelinedInfo(pi);
+        }
+        return MakeParseError(err);
+    }
+    if (pi.auth_flags) {
+        ParseError err = HandleAuthentication(msg, socket, &pi);
+        if (err != PARSE_OK) {
+            return MakeParseError(err, "Fail to authenticate with Mysql");
+        }
+        DestroyingPtr<InputResponse> auth_msg =
+            static_cast<InputResponse*>(socket->release_parsing_context());
+        socket->GivebackPipelinedInfo(pi);
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    if (stmt_type == MYSQL_NEED_PREPARE) {
+        // A failed PREPARE (e.g. ER_PARSE_ERROR 1064) comes back as a normal
+        // ERR packet. Deliver it to the caller like any other error response
+        // and keep the connection open -- matching the command path and other
+        // protocols (redis, baidu_std). Only a successful prepare proceeds to
+        // pack and send the COM_STMT_EXECUTE.
+        if (!msg->response.reply(0).is_prepare_ok()) {
+            msg->id_wait = pi.id_wait;
+            socket->release_parsing_context();
+            return MakeMessage(msg);
+        }
+        // store stmt_id, make execute header.
+        ParseError err = HandlePrepareStatement(msg, socket, &pi);
+        if (err != PARSE_OK) {
+            return MakeParseError(err, "Fail to make parepared statement with Mysql");
+        }
+        DestroyingPtr<InputResponse> prepare_msg =
+            static_cast<InputResponse*>(socket->release_parsing_context());
+        socket->GivebackPipelinedInfo(pi);
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    msg->id_wait = pi.id_wait;
+    socket->release_parsing_context();
+    return MakeMessage(msg);
+}
+
+void ProcessMysqlResponse(InputMessageBase* msg_base) {
+    const int64_t start_parse_us = butil::cpuwide_time_us();
+    DestroyingPtr<InputResponse> msg(static_cast<InputResponse*>(msg_base));
+
+    const bthread_id_t cid = msg->id_wait;
+    Controller* cntl = NULL;
+    const int rc = bthread_id_lock(cid, (void**)&cntl);
+    if (rc != 0) {
+        LOG_IF(ERROR, rc != EINVAL && rc != EPERM)
+            << "Fail to lock correlation_id=" << cid << ": " << berror(rc);
+        return;
+    }
+
+    ControllerPrivateAccessor accessor(cntl);
+    // Controller::span() returns a std::shared_ptr<Span> in current master
+    // (was a raw Span* when #2093 was written).
+    if (auto span = accessor.span()) {
+        span->set_base_real_us(msg->base_real_us());
+        span->set_received_us(msg->received_us());
+        span->set_response_size(msg->response.ByteSize());
+        span->set_start_parse_us(start_parse_us);
+    }
+    const int saved_error = cntl->ErrorCode();
+    if (cntl->response() != NULL) {
+        if (cntl->response()->GetDescriptor() != MysqlResponse::descriptor()) {
+            cntl->SetFailed(ERESPONSE, "Must be MysqlResponse");
+        } else {
+            // We work around ParseFrom of pb which is just a placeholder.
+            ((MysqlResponse*)cntl->response())->Swap(&msg->response);
+        }
+    }  // silently ignore the response.
+
+    // Unlocks correlation_id inside. Revert controller's
+    // error code if it version check of `cid' fails
+    msg.reset();  // optional, just release resourse ASAP
+    accessor.OnResponse(cid, saved_error);
+}
+
+void SerializeMysqlRequest(butil::IOBuf* buf,
+                           Controller* cntl,
+                           const google::protobuf::Message* request) {
+    if (request == NULL) {
+        return cntl->SetFailed(EREQUEST, "request is NULL");
+    }
+    if (request->GetDescriptor() != MysqlRequest::descriptor()) {
+        return cntl->SetFailed(EREQUEST, "The request is not a MysqlRequest");
+    }
+    const MysqlRequest* rr = (const MysqlRequest*)request;
+    // We work around SerializeTo of pb which is just a placeholder.
+    if (!rr->SerializeTo(buf)) {
+        return cntl->SetFailed(EREQUEST, "Fail to serialize MysqlRequest");
+    }
+    // mysql doesn't use pipelined_count to verify the end of a response; instead we
+    // reuse it as a MysqlStmtType tag so the parse function knows which reply shape

Review Comment:
   This is confusing. I think we can add some alias function to `ControllerPriveAccessor` to make it more clear.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4710940758

   @wwbmmm @yanglimingcn @chenBright — this is green on all lanes and ready for another look.
   
   The earlier `brpc_channel_unittest` failure that I'd thought was an unrelated flake turned out to be a real bug this PR introduced: the new `bind_sock_action` member of `Controller::Call` was not default-initialized, so the backup/retry call (built via the `Call` copy constructor) read an indeterminate value in `Call::OnComplete`, which branches on it before the normal pool-return / `SetFailed` path. When the garbage happened to match `BIND_SOCK_RESERVE`/`BIND_SOCK_USE`, the backup call's socket was diverted instead of returned, leaving the in-flight RPC unconcluded and hanging `ChannelTest.backup_request` (nondeterministically, hence the flaky appearance). Fixed by giving the member an in-class default initializer (`BIND_SOCK_NONE`) so every `Call` construction path is safe; a backup/retry never inherits transaction affinity. `ChannelTest.backup_request` now passes in under a second.
   
   All prior review comments are addressed. The one remaining open thread — whether per-`Socket` `_fd_version` needs to be atomic — has a reply explaining why: it is written on the reconnect path with no socket lock held and read from another thread, so the relaxed atomic is there to make that cross-thread access race-free, independent of the global counter's uniqueness. Happy to adjust if you'd prefer a different framing. Could you take another pass when you have a moment?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4720270347

   The MySQL protocol was originally developed by @yanglimingcn, who is most familiar with it. @yanglimingcn Please review it when you have time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4626536107

   @chenBright @wwbmmm — marking this ready for review.
   
   Full MySQL client protocol for brpc — COM_QUERY text protocol, interactive transactions, and prepared statements — built on a **clean-room** authentication codec (no GPL lineage), addressing #2093.
   
   - **Auth (clean-room):** `mysql_native_password` + `caching_sha2_password` (fast-auth + full-auth RSA / secure-transport cleartext), derived from the public MySQL protocol docs.
   - **Minimal shared-core footprint (redis-like):** the only generic `Controller`/`Socket` additions are a socket reserve/use hook + `fd_version` (ABA guard) for transaction/prepared-statement affinity, an auth empty-write guard for the server-greets-first handshake, and protocol registration. No protocol-specific type lives on the shared `Controller`.
   - **Tests:** clean-room integration tests (transactions, prepared statements, pooled-connection concurrency, connection-type, binary TIME/DATETIME) run against a self-spawned `mysqld` — 20/20 passing locally; a standalone 30-case end-to-end run was independently verified.
   
   The clean-room auth codec is also up as a smaller, focused PR (#3310, Stage 1a) if you'd prefer to review that foundation first.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3449586740


##########
src/brpc/socket.h:
##########
@@ -840,6 +843,9 @@ friend class TransportFactory;
     butil::atomic<int> _fd;  // -1 when not connected.
     int _tos;                // Type of service which is actually only 8bits.
     int64_t _reset_fd_real_us; // When _fd was reset, in microseconds.
+    // ABA/version counter; written on fd reset and read via fd_version() from
+    // other threads, so use relaxed atomics to avoid a data race.
+    butil::atomic<uint64_t> _fd_version;  // _fd_version, used only for mysql now.

Review Comment:
   What is `_fd_version` used for?



##########
src/brpc/controller.h:
##########
@@ -762,6 +776,10 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
         // CONNECTION_TYPE_SINGLE. Otherwise, it may be a temporary
         // socket fetched from socket pool
         SocketUniquePtr sending_sock;
+        // In-class default so every Call init path (default ctor, Reset(), and
+        // the Call(Call*) copy ctor used for backup/retry) is NONE unless set
+        // explicitly. A backup/retry never inherits transaction affinity.
+        BindSockAction bind_sock_action = BIND_SOCK_NONE;

Review Comment:
   `bind_sock_action` needs to be set in `ResetPods()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4697085796

   This is ready for review and merge. The branch is up to date with master (the #3310 merge conflict is resolved) and `mergeable`.
   
   The only failing checks are the three bazel unittest jobs, all on the **same unrelated** target `//test:brpc_channel_unittest` (78/79 pass in each):
   
   - `clang-unittest-with-bazel` / `gcc-unittest-with-bazel` — `brpc_channel_unittest` TIMEOUT at 300s
   - `clang-unittest-bazel-with-babylon-and-new-pb` — `ChannelTest.backup_request_policy` failed
   
   This is a pre-existing timing flake, not related to this PR's diff. The failing assertion is `test/brpc_channel_unittest.cpp:2208` — a 100 ms hang-guard on socket teardown — which overshot by <1 ms (100,896 µs) under bazel's parallel execution + 300 s per-test cap on the 2-vCPU runner. The same test passes in the serial make/cmake unittest jobs (`clang-unittest` and `clang-unittest-asan` are green), and the MySQL tests this PR touches all run and pass there (auth handshake 28 / packet 27 / scramble 35).
   
   @chenBright @wwbmmm @yanglimingcn — could you take a look and merge when you have a moment? Happy to open a separate follow-up for the `brpc_channel_unittest` flake/timeout if useful.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4664906986

   @wwbmmm @chenBright — pushed a revision addressing the review feedback and fixing the integration-test setup. Summary:
   
   **Review comments**
   - Stored the mysql-transaction `BindSockAction` in two `Controller::_flags` bits instead of a dedicated member.
   - Made `MysqlResponse` explicitly non-copyable and turned the previously no-op `MergeFrom` into a hard `CHECK`-failure (so an accidental copy is caught instead of silently dropping replies).
   - Renamed `get_tx()`/`get_stmt()` to `tx()`/`stmt()`.
   - Added a clearly named `ControllerPrivateAccessor::set_mysql_statement_type()` alias over the `pipelined_count` slot the mysql protocol reuses.
   - The `MysqlCollations` ODR note, the gflag spelling, and the two doc/comment mismatches were already addressed in the current revision.
   - On `_fd_version`: I left a reply explaining why it needs to stay atomic — it is written in `ResetFileDescriptor` (reconnect path, no lock) and read lock-free from another thread via `fd_version()` in the prepared-statement path, so a non-atomic field would be a data race. It mirrors how `_fd` itself is handled. Happy to discuss further.
   
   **Integration tests now run against a real MySQL server**
   - The suites previously did not exercise a live server in CI: a legacy test pointed at an external host and hard-failed, and the make/bazel wiring built it as a single globbed target. I removed the legacy test, switched the `test/mysql/*` suites to the redis-style self-spawning + skip-if-absent pattern, build one cc_test per file, and merged master so the CI image installs `mysql-server`. The suites now spawn a local throwaway mysqld and run.
   
   **Local verification**
   I built and ran all seven mysql suites locally against a live MySQL 9.6 server: 110 tests, 0 skipped, 0 failed — covering `caching_sha2_password` auth, the text and binary (prepared-statement) protocols, transactions, and connection pooling. (MySQL 9.x removed `mysql_native_password`, so this also exercises the caching_sha2 path end-to-end.)
   
   CI is running on the latest revision. Could you take another look when you have a chance? Thanks for the reviews.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4883629054

   @wwbmmm @yanglimingcn @chenBright — gentle follow-up: this has been green on all CI lanes for a couple of days (the `bind_sock_action`/`ResetPods` comment is addressed in `530e1124`). Could you take another look and merge if it looks good? Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3511126881


##########
src/brpc/controller.h:
##########
@@ -762,6 +776,10 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
         // CONNECTION_TYPE_SINGLE. Otherwise, it may be a temporary
         // socket fetched from socket pool
         SocketUniquePtr sending_sock;
+        // In-class default so every Call init path (default ctor, Reset(), and
+        // the Call(Call*) copy ctor used for backup/retry) is NONE unless set
+        // explicitly. A backup/retry never inherits transaction affinity.
+        BindSockAction bind_sock_action = BIND_SOCK_NONE;

Review Comment:
   Done in `530e1124`: `Call::bind_sock_action` is now initialized explicitly in every constructor (incl. the backup/retry copy ctor), not via an in-class default. That missing init was the cause of the backup/retry hang. CI green.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3330:
URL: https://github.com/apache/brpc/pull/3330


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4665061753

   @wwbmmm @chenBright — heads-up on CI: the two `*-unittest-with-bazel` lanes show one failure, and it is an unrelated flake, not a regression from this PR.
   
   - The failing target is `//test:brpc_channel_unittest`, and within it a single timing-sensitive case, `ChannelTest.backup_request`, hung and blew the 300s test timeout (the timeout stack trace points at `ChannelTest::TestBackupRequest()`). It is a timeout, not an assertion failure.
   - The **same commit passes** that test in the make lanes (`clang-unittest` and `clang-unittest-asan` are green), and **all seven `//test:mysql/*` targets pass** in the bazel lanes.
   - It is independent of this change: the new flags use `_flags` bits 14/15 while backup-request uses `FLAGS_BACKUP_REQUEST` (bit 5) — no collision — and the backup-request path never touches `bind_sock_action()`.
   
   A re-run of the failed jobs should clear it (I cannot trigger re-runs as an outside contributor).
   
   This PR is ready for another look whenever you have time: all review threads are addressed (I left one reply on `_fd_version` explaining why it stays atomic), the mysql integration suites now run against a real CI-provisioned mysqld, and I verified all seven suites locally against a live MySQL 9.6 (110 tests, 0 skipped, 0 failed). Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3463965065


##########
src/brpc/controller.h:
##########
@@ -762,6 +776,10 @@ friend void policy::ProcessThriftRequest(InputMessageBase*);
         // CONNECTION_TYPE_SINGLE. Otherwise, it may be a temporary
         // socket fetched from socket pool
         SocketUniquePtr sending_sock;
+        // In-class default so every Call init path (default ctor, Reset(), and
+        // the Call(Call*) copy ctor used for backup/retry) is NONE unless set
+        // explicitly. A backup/retry never inherits transaction affinity.
+        BindSockAction bind_sock_action = BIND_SOCK_NONE;

Review Comment:
   Thanks for the pointer I will get it to it by eow and then resolve this comment.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3538047770


##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================

Review Comment:
   Removed — also stripped the same separators from the four mysql test files.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================

Review Comment:
   Removed.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER

Review Comment:
   Removed.



##########
src/brpc/policy/mysql/mysql.cpp:
##########
@@ -0,0 +1,537 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
+#include <algorithm>
+#include <gflags/gflags.h>
+#include "butil/string_printf.h"
+#include "butil/macros.h"
+#include "butil/logging.h"
+#include "brpc/controller.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_common.h"
+
+namespace brpc {
+
+DEFINE_int32(mysql_multi_replies_size, 10, "multi replies size in one MysqlResponse");
+
+// ===================================================================
+
+butil::Status MysqlStatementStub::PackExecuteCommand(butil::IOBuf* outbuf, uint32_t stmt_id) {
+    butil::Status st;
+    // long data
+    for (const auto& i : _long_data) {
+        st = MysqlMakeLongDataPacket(outbuf, stmt_id, i.param_id, i.long_data);
+        if (!st.ok()) {
+            LOG(ERROR) << "make long data header error " << st;
+            return st;
+        }
+    }
+    _long_data.clear();
+    // execute data
+    st = MysqlMakeExecutePacket(outbuf, stmt_id, _execute_data);
+    if (!st.ok()) {
+        LOG(ERROR) << "make execute header error " << st;
+        return st;
+    }
+    _execute_data.clear();
+    _null_mask.mask.clear();
+    _null_mask.area = butil::IOBuf::INVALID_AREA;
+    _param_types.types.clear();
+    _param_types.area = butil::IOBuf::INVALID_AREA;
+
+    return st;
+}
+
+MysqlRequest::MysqlRequest()
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+}
+
+MysqlRequest::MysqlRequest(MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlTransaction* tx, MysqlStatement* stmt)
+    : NonreflectableMessage<MysqlRequest>() {
+    SharedCtor();
+    _tx = tx;
+    _stmt = new MysqlStatementStub(stmt);
+}
+
+MysqlRequest::MysqlRequest(const MysqlRequest& from)
+    : NonreflectableMessage<MysqlRequest>(from) {
+    SharedCtor();
+    MergeFrom(from);
+}
+
+void MysqlRequest::SharedCtor() {
+    _has_error = false;
+    _cached_size_ = 0;
+    _has_command = false;
+    _tx = NULL;
+    _stmt = NULL;
+    _param_index = 0;
+}
+
+MysqlRequest::~MysqlRequest() {
+    SharedDtor();
+    if (_stmt != NULL) {
+        delete _stmt;
+    }
+    _stmt = NULL;
+}
+
+void MysqlRequest::SharedDtor() {
+}
+
+void MysqlRequest::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlRequest::Clear() {
+    _has_error = false;
+    _buf.clear();
+    _has_command = false;
+    _tx = NULL;
+    if (_stmt) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    _param_index = 0;
+}
+
+size_t MysqlRequest::ByteSizeLong() const {
+    int total_size = _buf.size();
+    _cached_size_ = total_size;
+    return total_size;
+}
+
+void MysqlRequest::MergeFrom(const MysqlRequest& from) {
+    if (&from == this) {
+        return;
+    }
+    // Copy all members so CopyFrom/copy-construct yields an equivalent request
+    // instead of an empty one.
+    _has_command = from._has_command;
+    _has_error = from._has_error;
+    _buf = from._buf;
+    _cached_size_ = from._cached_size_;
+    _param_index = from._param_index;
+    // _tx is a non-owning pointer (never deleted by MysqlRequest): shallow copy.
+    _tx = from._tx;
+    // _stmt is owned (deleted in the dtor): deep-copy to avoid double free.
+    if (_stmt != NULL) {
+        delete _stmt;
+        _stmt = NULL;
+    }
+    if (from._stmt != NULL) {
+        _stmt = new MysqlStatementStub(*from._stmt);
+    }
+}
+
+void MysqlRequest::Swap(MysqlRequest* other) {
+    if (other != this) {
+        _buf.swap(other->_buf);
+        std::swap(_has_error, other->_has_error);
+        std::swap(_cached_size_, other->_cached_size_);
+        std::swap(_has_command, other->_has_command);
+        std::swap(_tx, other->_tx);
+        std::swap(_stmt, other->_stmt);
+        std::swap(_param_index, other->_param_index);
+    }
+}
+
+bool MysqlRequest::SerializeTo(butil::IOBuf* buf) const {
+    if (_has_error) {
+        LOG(ERROR) << "Reject serialization due to error in CommandXXX[V]";
+        return false;
+    }
+    *buf = _buf;
+    return true;
+}
+
+bool MysqlRequest::Query(const butil::StringPiece& command) {
+    if (_has_error) {
+        return false;
+    }
+
+    if (_has_command) {
+        LOG(WARNING) << "MysqlRequest::Query: a command was already set on this request";
+        return false;
+    }
+
+    const butil::Status st = MysqlMakeCommand(&_buf, MYSQL_COM_QUERY, command);
+    if (st.ok()) {
+        _has_command = true;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+bool MysqlRequest::AddParam(int8_t p) {
+    if (_has_error) {
+        return false;
+    }
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint8_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint8_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_TINY, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint16_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint16_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_SHORT, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint32_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint32_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(int64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(int64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(uint64_t p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(uint64_t): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st =
+        MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_LONGLONG, true);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(float p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(float): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_FLOAT);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(double p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(double): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_DOUBLE);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+bool MysqlRequest::AddParam(const butil::StringPiece& p) {
+    if (_stmt == NULL || _stmt->stmt() == NULL) {
+        LOG(WARNING) << "MysqlRequest::AddParam(StringPiece): no prepared statement bound to request";
+        _has_error = true;
+        return false;
+    }
+    const butil::Status st = MysqlMakeExecuteData(_stmt, _param_index, &p, MYSQL_FIELD_TYPE_STRING);
+    if (st.ok()) {
+        ++_param_index;
+        return true;
+    } else {
+        CHECK(st.ok()) << st;
+        _has_error = true;
+        return false;
+    }
+}
+
+void MysqlRequest::Print(std::ostream& os) const {
+    butil::IOBuf cp = _buf;
+    {
+        uint8_t buf[3];
+        cp.cutn(buf, 3);
+        os << "size:" << mysql_uint3korr(buf) << ",";
+    }
+    {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << "sequence:" << (unsigned)buf << ",";
+    }
+    os << "payload(hex):";
+    while (!cp.empty()) {
+        uint8_t buf;
+        cp.cut1((char*)&buf);
+        os << std::hex << (unsigned)buf;
+    }
+}
+
+std::ostream& operator<<(std::ostream& os, const MysqlRequest& r) {
+    r.Print(os);
+    return os;
+}
+
+// ===================================================================
+
+#ifndef _MSC_VER
+#endif  // !_MSC_VER
+
+MysqlResponse::MysqlResponse()
+    : NonreflectableMessage<MysqlResponse>() {
+    SharedCtor();
+}
+
+void MysqlResponse::SharedCtor() {
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+MysqlResponse::~MysqlResponse() {
+    SharedDtor();
+}
+
+void MysqlResponse::SharedDtor() {
+}
+
+void MysqlResponse::SetCachedSize(int size) const {
+    _cached_size_ = size;
+}
+
+void MysqlResponse::Clear() {
+    // Reset all response state so a reused MysqlResponse does not return
+    // stale replies. Mirror what SharedCtor()/ctor initialize.
+    MysqlReply empty_reply;
+    _first_reply.Swap(empty_reply);
+    _other_replies.clear();
+    _arena.clear();
+    _nreply = 0;
+    _cached_size_ = 0;
+}
+
+size_t MysqlResponse::ByteSizeLong() const {
+    return _cached_size_;
+}
+
+void MysqlResponse::MergeFrom(const MysqlResponse&) {
+    CHECK(false) << "MysqlResponse does not support MergeFrom/CopyFrom";
+}
+
+bool MysqlResponse::IsInitialized() const {
+    return true;
+}
+
+void MysqlResponse::Swap(MysqlResponse* other) {
+    if (other != this) {
+        _first_reply.Swap(other->_first_reply);
+        std::swap(_other_replies, other->_other_replies);
+        _arena.swap(other->_arena);
+        std::swap(_nreply, other->_nreply);
+        std::swap(_cached_size_, other->_cached_size_);
+    }
+}
+
+// ===================================================================

Review Comment:
   Removed.



##########
src/brpc/controller.cpp:
##########
@@ -297,6 +297,9 @@ void Controller::ResetPods() {
     _request_streams.clear();
     _response_streams.clear();
     _remote_stream_settings = NULL;
+    set_bind_sock_action(BIND_SOCK_NONE);

Review Comment:
   Right — the _flags = 0 above already clears those bits. Removed.



##########
src/brpc/controller.cpp:
##########
@@ -297,6 +297,9 @@ void Controller::ResetPods() {
     _request_streams.clear();
     _response_streams.clear();
     _remote_stream_settings = NULL;
+    set_bind_sock_action(BIND_SOCK_NONE);
+    _bind_sock.reset();

Review Comment:
   Done — moved to ResetNonPods().



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4900248768

   @chenBright — checking in on the path to merge here. It has been three weeks since the handoff to @yanglimingcn (Jun 16) with no activity, and the PR has been merge-ready throughout: 15/15 CI lanes green, all review threads resolved (your `ResetPods`/`bind_sock_action` comment addressed in `530e1124`), and @wwbmmm's LGTM. Would @wwbmmm's approval be sufficient to merge, or is @yanglimingcn's review a hard requirement?
   
   @yanglimingcn — happy to walk through any part of the implementation (clean-room auth handshake, prepared-statement/transaction connection affinity, binary protocol) if that would make the review easier. And if you don't have bandwidth right now, no problem at all — just let us know how you'd like to proceed. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4676082368

   Moved all seven MySQL unittests from `test/mysql/` into `test/` (following the review on #3310), so the existing `brpc_*_unittest` glob picks them up. `test/Makefile`, `test/CMakeLists.txt`, and `test/run_tests.sh` revert to master with no mysql-specific lines. In `test/BUILD.bazel` the files join the `brpc_unittests` glob; the five tests that fork a real `mysqld` keep their `["external", "local"]` tags by folding those entries into that target's `per_test_tags` (rather than a separate mysql target).
   
   All seven tests compile and run green in the unittest jobs — auth handshake 28 / packet 27 / scramble 35, plus connection-type, pool-concurrency, prepared, and txn integration. Any remaining red is a pre-existing unrelated flake (`brpc_channel_unittest`/`brpc_load_balancer_unittest` timeouts, `SocketTest`, `EventDispatcherTest` under ASan), not this change.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4710392492

   Restructured for easier review: the generic `Controller`/`Socket` connection-affinity primitives (and the `ChannelTest.backup_request` uninitialized-`bind_sock_action` fix) are split into a small standalone PR #3348. This PR is now **stacked on top of #3348** and contains only the MySQL-specific code (protocol codec, registration, `PROTOCOL_MYSQL`, accessor wrappers, tests, docs).
   
   The diff here will show the core changes too until #3348 merges into master, after which it auto-reduces to MySQL-only. No functional change from the previous revision — the combined tree is byte-identical.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4710429576

   Update: reverted the split — the core/MySQL separation isn't warranted for a one-line fix. #3330 is again the single self-contained PR (MySQL protocol + the generic affinity primitives), and the `ChannelTest.backup_request` fix (uninitialized `bind_sock_action` in the `Call(Call*)` copy constructor) is folded directly into it. #3348 is closed. No functional change.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on PR #3330:
URL: https://github.com/apache/brpc/pull/3330#issuecomment-4717362353

   @chenBright — could you review this PR when you have a chance? It's green on all CI lanes and mergeable. All earlier review threads are addressed, and the previous `brpc_channel_unittest` failure is fixed (it was an uninitialized `bind_sock_action` in the `Call` copy constructor, now default-initialized). Would value your eyes on the MySQL protocol codec and the core `Controller`/`Socket` affinity primitives. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3384327675


##########
src/brpc/socket.cpp:
##########
@@ -578,6 +579,9 @@ int Socket::ResetFileDescriptor(int fd) {
     _avg_msg_size = 0;
     // MUST store `_fd' before adding itself into epoll device to avoid
     // race conditions with the callback function inside epoll
+    static butil::atomic<uint64_t> BAIDU_CACHELINE_ALIGNMENT fd_version(0);
+    _fd_version.store(fd_version.fetch_add(1, butil::memory_order_relaxed),

Review Comment:
   Thanks — you're right that the *uniqueness* of the value comes from the static `fd_version` atomic. But the per-`Socket` `_fd_version` still needs to be atomic for a different reason: it is written and read from different threads with no lock between them.
   
   - Writer: `ResetFileDescriptor()` stores `_fd_version` on the reconnect path (`CheckConnectedAndKeepWrite` -> `ResetFileDescriptor`), with no socket mutex held.
   - Reader: the mysql prepared-statement path reads `socket->fd_version()` on the RPC bthread (`MysqlStatement::StatementId`). It obtains the socket via `Socket::Address()`, which only gates on the `SocketId` version — and `ResetFileDescriptor` does not bump that version — so `Address()` succeeds even while the fd is being reset. The read can therefore overlap the write on the same Socket.
   
   A concurrent non-atomic read/write on the same object is a data race (UB), and a 64-bit load is not tear-free on 32-bit targets, so the member has to be atomic. This mirrors `_fd` itself, which is a `butil::atomic<int>` written in the same `ResetFileDescriptor` and read lock-free (`memory_order_relaxed`) all over the IO path. I kept `_fd_version` on the same relaxed-atomic pattern. Happy to add a brief comment to that effect if it helps.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] Add MySQL client protocol (text protocol, transactions, prepared statements) on clean-room auth (#2093) (brpc)

Posted by "rajvarun77 (via GitHub)" <gi...@apache.org>.
rajvarun77 commented on code in PR #3330:
URL: https://github.com/apache/brpc/pull/3330#discussion_r3384396245


##########
src/brpc/policy/mysql/mysql_protocol.cpp:
##########
@@ -0,0 +1,536 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Authors: Yang,Liming (yangliming01@baidu.com)
+
+#include <google/protobuf/descriptor.h>  // MethodDescriptor
+#include <google/protobuf/message.h>     // Message
+#include <gflags/gflags.h>
+#include <sstream>
+#include "butil/logging.h"  // LOG()
+#include "butil/time.h"
+#include "butil/iobuf.h"  // butil::IOBuf
+#include "butil/sys_byteorder.h"
+#include "brpc/controller.h"  // Controller
+#include "brpc/details/controller_private_accessor.h"
+#include "brpc/socket.h"  // Socket
+#include "brpc/server.h"  // Server
+#include "brpc/details/server_private_accessor.h"
+#include "brpc/span.h"
+#include "brpc/policy/mysql/mysql.h"
+#include "brpc/policy/mysql/mysql_authenticator.h"
+#include "brpc/policy/mysql/mysql_protocol.h"
+#include "brpc/policy/mysql/mysql_auth_scramble.h"
+
+namespace brpc {
+
+DECLARE_bool(enable_rpcz);
+
+namespace policy {
+
+DEFINE_bool(mysql_verbose, false, "[DEBUG] Print EVERY mysql request/response");
+
+void MysqlParseAuthenticator(const butil::StringPiece& raw,
+                             std::string* user,
+                             std::string* password,
+                             std::string* schema,
+                             std::string* collation);
+void MysqlParseParams(const butil::StringPiece& raw, std::string* params);
+// pack mysql authentication_data
+int MysqlPackAuthenticator(const MysqlReply::Auth& auth,
+                           const butil::StringPiece& user,
+                           const butil::StringPiece& password,
+                           const butil::StringPiece& schema,
+                           const butil::StringPiece& collation,
+                           std::string* auth_cmd);
+int MysqlPackParams(const butil::StringPiece& params, std::string* param_cmd);
+
+namespace {
+// The connection-phase handshake spans several packets, so it needs per-connection
+// (not per-RPC) scratch state. Rather than add a field to the shared Controller, we
+// reuse the per-connection AuthContext: group() tracks the auth step, and (for
+// caching_sha2_password below) roles() stashes the salt across the RSA round trip.
+const char* auth_step[] = {"AUTH_OK", "PARAMS_OK"};
+
+// Extra AuthContext group/state markers for the caching_sha2_password
+// multi-round-trip exchange.  After the client sends the 32-byte fast
+// scramble in its HandshakeResponse41 (group still default/empty), the
+// server may answer with an AuthMoreData status byte.  These markers track
+// where we are in that follow-up handshake so re-entries pick the right
+// branch:
+//   CACHE_SHA2_SENT   : sent the fast scramble; awaiting the server's
+//                       AuthMoreData (0x03 fast-auth / 0x04 full-auth) or OK.
+//   CACHE_SHA2_PUBKEY : on plain TCP full-auth, we requested the RSA public
+//                       key (sent 0x02); awaiting the AuthMoreData carrying
+//                       the PEM, after which we send the RSA-encrypted pw.
+const char* kCacheSha2Sent = "CACHE_SHA2_SENT";
+const char* kCacheSha2Pubkey = "CACHE_SHA2_PUBKEY";
+
+// Frames |payload| as a single MySQL packet: 3-byte little-endian payload
+// length + 1-byte sequence id, then the payload, and writes it to |fd|.
+// |seq| is the sequence id the packet must carry (the previous server
+// packet's seq + 1, per the MySQL packet-sequence rule).
+static void WriteMysqlAuthPacket(int fd, const std::string& payload, uint8_t seq) {
+    butil::IOBuf buf;
+    const uint32_t len = butil::ByteSwapToLE32((uint32_t)payload.size());
+    buf.append(&len, 3);
+    buf.push_back((char)seq);
+    buf.append(payload);
+    buf.cut_into_file_descriptor(fd);
+}
+
+struct InputResponse : public InputMessageBase {
+    bthread_id_t id_wait;
+    MysqlResponse response;
+
+    // @InputMessageBase
+    void DestroyImpl() {
+        delete this;
+    }
+};
+
+bool PackRequest(butil::IOBuf* buf,
+                 ControllerPrivateAccessor& accessor,
+                 const butil::IOBuf& request) {
+    if (accessor.pipelined_count() == MYSQL_PREPARED_STATEMENT) {
+        Socket* sock = accessor.get_sending_socket();
+        if (sock == NULL) {
+            LOG(ERROR) << "[MYSQL PACK] get sending socket with NULL";
+            return false;
+        }
+        auto stub = static_cast<MysqlStatementStub*>(accessor.session_data());
+        if (stub == NULL) {
+            LOG(ERROR) << "[MYSQL PACK] get prepare statement with NULL";
+            return false;
+        }
+        uint32_t stmt_id;
+        // if can't found stmt_id in this socket, create prepared statement on it, store user
+        // request.
+        if ((stmt_id = stub->stmt()->StatementId(sock->id())) == 0) {
+            butil::IOBuf b;
+            butil::Status st = MysqlMakeCommand(&b, MYSQL_COM_STMT_PREPARE, stub->stmt()->str());
+            if (!st.ok()) {
+                LOG(ERROR) << "[MYSQL PACK] make prepare statement error " << st;
+                return false;
+            }
+            accessor.set_pipelined_count(MYSQL_NEED_PREPARE);
+            buf->append(b);
+            return true;
+        }
+        // else pack execute header with stmt_id
+        butil::Status st = stub->PackExecuteCommand(buf, stmt_id);
+        if (!st.ok()) {
+            LOG(ERROR) << "write execute data error " << st;
+            return false;
+        }
+        return true;
+    }
+    buf->append(request);
+    return true;
+}
+
+ParseError HandleAuthentication(const InputResponse* msg, const Socket* socket, PipelinedInfo* pi) {
+    const bthread_id_t cid = pi->id_wait;
+    Controller* cntl = NULL;
+    if (bthread_id_lock(cid, (void**)&cntl) != 0) {
+        LOG(ERROR) << "[MYSQL PARSE] fail to lock controller";
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+
+    ParseError parseCode = PARSE_OK;
+    const AuthContext* ctx = socket->auth_context();
+    if (ctx == NULL) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] auth context is null";
+        goto END_OF_AUTH;
+    }
+    if (msg->response.reply(0).is_auth()) {
+        std::string user, password, schema, collation, auth_cmd;
+        const MysqlReply& reply = msg->response.reply(0);
+        MysqlParseAuthenticator(ctx->user(), &user, &password, &schema, &collation);
+        if (MysqlPackAuthenticator(reply.auth(), user, password, schema, collation, &auth_cmd) ==
+            0) {
+            butil::IOBuf buf;
+            buf.append(auth_cmd);
+            const ssize_t nw = buf.cut_into_file_descriptor(socket->fd());
+            if (nw < 0 || !buf.empty()) {
+                LOG(WARNING) << "[MYSQL PARSE] failed to write auth command to fd="
+                             << socket->fd() << ", nw=" << nw
+                             << ", remaining=" << buf.size();
+            }
+            const bool is_caching_sha2 = (reply.auth().auth_plugin() == "caching_sha2_password");
+            if (is_caching_sha2) {
+                // caching_sha2_password is a multi-round-trip exchange: stash
+                // the 20-byte salt (greeting salt + salt2) for a later RSA
+                // full-auth round, and mark that the fast scramble was sent.
+                // _roles is otherwise unused on the mysql path.
+                std::string salt;
+                salt.append(reply.auth().salt().data(), reply.auth().salt().size());
+                salt.append(reply.auth().salt2().data(), reply.auth().salt2().size());
+                const_cast<AuthContext*>(ctx)->set_roles(salt);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+            } else {
+                const_cast<AuthContext*>(ctx)->set_group(auth_step[0]);
+            }
+        } else {
+            parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+            LOG(ERROR) << "[MYSQL PARSE] wrong pack authentication data";
+        }
+    } else if (msg->response.reply(0).is_auth_more_data()) {
+        // caching_sha2_password follow-up packet (server -> client).  The
+        // first data byte after the 0x01 tag is a status marker, except when
+        // we are awaiting the RSA public key (CACHE_SHA2_PUBKEY), in which
+        // case the whole payload is the PEM public key.
+        std::string user, password, schema, collation;
+        MysqlParseAuthenticator(ctx->user(), &user, &password, &schema, &collation);
+        const MysqlReply::AuthMoreData& amd = msg->response.reply(0).auth_more_data();
+        const butil::StringPiece data = amd.data();
+        const uint8_t next_seq = (uint8_t)(amd.seq() + 1);
+        if (ctx->group() == kCacheSha2Pubkey) {
+            // The payload is the server's PEM RSA public key.  Encrypt the
+            // password with it (plain-TCP full-auth) and send the ciphertext.
+            const std::string rsa = mysql::CachingSha2PasswordRsaEncrypt(
+                data, ctx->roles(), password);
+            if (rsa.empty()) {
+                parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+                LOG(ERROR) << "[MYSQL PARSE] failed to RSA-encrypt caching_sha2 password";
+                goto END_OF_AUTH;
+            }
+            WriteMysqlAuthPacket(socket->fd(), rsa, next_seq);
+            // Stay in CACHE_SHA2_SENT-equivalent: the server replies OK next.
+            const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+        } else if (!data.empty() && (uint8_t)data[0] == 0x03) {
+            // fast_auth_success: server will send OK next; send nothing.
+            const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+        } else if (!data.empty() && (uint8_t)data[0] == 0x04) {
+            // perform_full_authentication.
+            if (socket->is_ssl()) {
+                // Secure channel: send the cleartext password (one round trip).
+                const std::string clear = mysql::CachingSha2PasswordCleartext(password);
+                WriteMysqlAuthPacket(socket->fd(), clear, next_seq);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Sent);
+            } else {
+                // Plain TCP: request the server's RSA public key (0x02), then
+                // wait for the AuthMoreData carrying the PEM.
+                WriteMysqlAuthPacket(socket->fd(), std::string(1, (char)0x02), next_seq);
+                const_cast<AuthContext*>(ctx)->set_group(kCacheSha2Pubkey);
+            }
+        } else {
+            parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+            LOG(ERROR) << "[MYSQL PARSE] unexpected caching_sha2 AuthMoreData marker";
+        }
+    } else if (msg->response.reply_size() > 0) {
+        for (size_t i = 0; i < msg->response.reply_size(); ++i) {
+            if (!msg->response.reply(i).is_ok()) {
+                LOG(ERROR) << "[MYSQL PARSE] auth failed " << msg->response;
+                parseCode = PARSE_ERROR_NO_RESOURCE;
+                goto END_OF_AUTH;
+            }
+        }
+        std::string params, params_cmd;
+        MysqlParseParams(ctx->user(), &params);
+        // Auth just completed (either native's single round trip, group
+        // AUTH_OK, or caching_sha2's multi round-trip, group CACHE_SHA2_SENT)
+        // and connection params have not been sent yet: send them now.
+        const bool auth_just_done =
+            (ctx->group() == auth_step[0] || ctx->group() == kCacheSha2Sent);
+        if (auth_just_done && !params.empty()) {
+            if (MysqlPackParams(params, &params_cmd) == 0) {
+                butil::IOBuf buf;
+                buf.append(params_cmd);
+                buf.cut_into_file_descriptor(socket->fd());
+                const_cast<AuthContext*>(ctx)->set_group(auth_step[1]);
+            } else {
+                parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+                LOG(ERROR) << "[MYSQL PARSE] wrong pack params data";
+            }
+        } else {
+            butil::IOBuf raw_req;
+            raw_req.append(ctx->starter());
+            raw_req.cut_into_file_descriptor(socket->fd());
+            pi->auth_flags = 0;
+        }
+    } else {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] wrong authentication step";
+    }
+
+END_OF_AUTH:
+    if (bthread_id_unlock(cid) != 0) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] fail to unlock controller";
+    }
+    return parseCode;
+}
+
+ParseError HandlePrepareStatement(const InputResponse* msg,
+                                  const Socket* socket,
+                                  PipelinedInfo* pi) {
+    if (!msg->response.reply(0).is_prepare_ok()) {
+        LOG(ERROR) << "[MYSQL PARSE] response is not prepare ok, " << msg->response;
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+    const MysqlReply::PrepareOk& ok = msg->response.reply(0).prepare_ok();
+    const bthread_id_t cid = pi->id_wait;
+    Controller* cntl = NULL;
+    if (bthread_id_lock(cid, (void**)&cntl) != 0) {
+        LOG(ERROR) << "[MYSQL PARSE] fail to lock controller";
+        return PARSE_ERROR_ABSOLUTELY_WRONG;
+    }
+    ParseError parseCode = PARSE_OK;
+    butil::IOBuf buf;
+    butil::Status st;
+    MysqlStatementStub* stub = NULL;
+    MysqlStatement* stmt = NULL;
+    stub = static_cast<MysqlStatementStub*>(ControllerPrivateAccessor(cntl).session_data());
+    if (stub == NULL) {
+        LOG(ERROR) << "[MYSQL PACK] get prepare statement with NULL";
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    stmt = stub->stmt();
+    if (stmt == NULL || stmt->param_count() != ok.param_count()) {
+        LOG(ERROR) << "[MYSQL PACK] stmt can't be NULL";
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    if (stmt->param_count() != ok.param_count()) {
+        LOG(ERROR) << "[MYSQL PACK] stmt param number " << stmt->param_count()
+                   << " not equal to prepareOk.param_number " << ok.param_count();
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    stmt->SetStatementId(socket->id(), ok.stmt_id());
+    st = stub->PackExecuteCommand(&buf, ok.stmt_id());
+    if (!st.ok()) {
+        LOG(ERROR) << "[MYSQL PACK] make execute header error " << st;
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        goto END_OF_PREPARE;
+    }
+    {
+        const ssize_t nw = buf.cut_into_file_descriptor(socket->fd());
+        if (nw < 0 || !buf.empty()) {
+            LOG(WARNING) << "[MYSQL PARSE] failed to write execute command to fd="
+                         << socket->fd() << ", nw=" << nw
+                         << ", remaining=" << buf.size();
+        }
+    }
+    pi->count = MYSQL_PREPARED_STATEMENT;
+END_OF_PREPARE:
+    if (bthread_id_unlock(cid) != 0) {
+        parseCode = PARSE_ERROR_ABSOLUTELY_WRONG;
+        LOG(ERROR) << "[MYSQL PARSE] fail to unlock controller";
+    }
+    return parseCode;
+}
+
+}  // namespace
+
+// "Message" = "Response" as we only implement the client for mysql.
+ParseResult ParseMysqlMessage(butil::IOBuf* source,
+                              Socket* socket,
+                              bool /*read_eof*/,
+                              const void* /*arg*/) {
+    if (source->empty()) {
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+
+    PipelinedInfo pi;
+    if (!socket->PopPipelinedInfo(&pi)) {
+        LOG(WARNING) << "No corresponding PipelinedInfo in socket";
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+
+    InputResponse* msg = static_cast<InputResponse*>(socket->parsing_context());
+    if (msg == NULL) {
+        msg = new InputResponse;
+        socket->reset_parsing_context(msg);
+    }
+
+    MysqlStmtType stmt_type = static_cast<MysqlStmtType>(pi.count);
+    ParseError err = msg->response.ConsumePartialIOBuf(*source, pi.auth_flags != 0, stmt_type);
+    if (FLAGS_mysql_verbose) {
+        LOG(INFO) << "[MYSQL PARSE] " << msg->response;
+    }
+    if (err != PARSE_OK) {
+        if (err == PARSE_ERROR_NOT_ENOUGH_DATA) {
+            socket->GivebackPipelinedInfo(pi);
+        }
+        return MakeParseError(err);
+    }
+    if (pi.auth_flags) {
+        ParseError err = HandleAuthentication(msg, socket, &pi);
+        if (err != PARSE_OK) {
+            return MakeParseError(err, "Fail to authenticate with Mysql");
+        }
+        DestroyingPtr<InputResponse> auth_msg =
+            static_cast<InputResponse*>(socket->release_parsing_context());
+        socket->GivebackPipelinedInfo(pi);
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    if (stmt_type == MYSQL_NEED_PREPARE) {
+        // A failed PREPARE (e.g. ER_PARSE_ERROR 1064) comes back as a normal
+        // ERR packet. Deliver it to the caller like any other error response
+        // and keep the connection open -- matching the command path and other
+        // protocols (redis, baidu_std). Only a successful prepare proceeds to
+        // pack and send the COM_STMT_EXECUTE.
+        if (!msg->response.reply(0).is_prepare_ok()) {
+            msg->id_wait = pi.id_wait;
+            socket->release_parsing_context();
+            return MakeMessage(msg);
+        }
+        // store stmt_id, make execute header.
+        ParseError err = HandlePrepareStatement(msg, socket, &pi);
+        if (err != PARSE_OK) {
+            return MakeParseError(err, "Fail to make parepared statement with Mysql");
+        }
+        DestroyingPtr<InputResponse> prepare_msg =
+            static_cast<InputResponse*>(socket->release_parsing_context());
+        socket->GivebackPipelinedInfo(pi);
+        return MakeParseError(PARSE_ERROR_NOT_ENOUGH_DATA);
+    }
+    msg->id_wait = pi.id_wait;
+    socket->release_parsing_context();
+    return MakeMessage(msg);
+}
+
+void ProcessMysqlResponse(InputMessageBase* msg_base) {
+    const int64_t start_parse_us = butil::cpuwide_time_us();
+    DestroyingPtr<InputResponse> msg(static_cast<InputResponse*>(msg_base));
+
+    const bthread_id_t cid = msg->id_wait;
+    Controller* cntl = NULL;
+    const int rc = bthread_id_lock(cid, (void**)&cntl);
+    if (rc != 0) {
+        LOG_IF(ERROR, rc != EINVAL && rc != EPERM)
+            << "Fail to lock correlation_id=" << cid << ": " << berror(rc);
+        return;
+    }
+
+    ControllerPrivateAccessor accessor(cntl);
+    // Controller::span() returns a std::shared_ptr<Span> in current master
+    // (was a raw Span* when #2093 was written).
+    if (auto span = accessor.span()) {
+        span->set_base_real_us(msg->base_real_us());
+        span->set_received_us(msg->received_us());
+        span->set_response_size(msg->response.ByteSize());
+        span->set_start_parse_us(start_parse_us);
+    }
+    const int saved_error = cntl->ErrorCode();
+    if (cntl->response() != NULL) {
+        if (cntl->response()->GetDescriptor() != MysqlResponse::descriptor()) {
+            cntl->SetFailed(ERESPONSE, "Must be MysqlResponse");
+        } else {
+            // We work around ParseFrom of pb which is just a placeholder.
+            ((MysqlResponse*)cntl->response())->Swap(&msg->response);
+        }
+    }  // silently ignore the response.
+
+    // Unlocks correlation_id inside. Revert controller's
+    // error code if it version check of `cid' fails
+    msg.reset();  // optional, just release resourse ASAP
+    accessor.OnResponse(cid, saved_error);
+}
+
+void SerializeMysqlRequest(butil::IOBuf* buf,
+                           Controller* cntl,
+                           const google::protobuf::Message* request) {
+    if (request == NULL) {
+        return cntl->SetFailed(EREQUEST, "request is NULL");
+    }
+    if (request->GetDescriptor() != MysqlRequest::descriptor()) {
+        return cntl->SetFailed(EREQUEST, "The request is not a MysqlRequest");
+    }
+    const MysqlRequest* rr = (const MysqlRequest*)request;
+    // We work around SerializeTo of pb which is just a placeholder.
+    if (!rr->SerializeTo(buf)) {
+        return cntl->SetFailed(EREQUEST, "Fail to serialize MysqlRequest");
+    }
+    // mysql doesn't use pipelined_count to verify the end of a response; instead we
+    // reuse it as a MysqlStmtType tag so the parse function knows which reply shape

Review Comment:
   Done — added `ControllerPrivateAccessor::set_mysql_statement_type()` as a clearly named alias over the `pipelined_count` slot the mysql protocol reuses, and the protocol now calls that instead of `set_pipelined_count()` directly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org