chenBright commented on code in PR #3535:
URL: https://github.com/apache/brpc/pull/3535#discussion_r4658847730


##########
src/brpc/handshake/ubshm_handshake.cpp:
##########
@@ -0,0 +1,574 @@
+// 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.
+
+#include "brpc/handshake/ubshm_handshake.h"
+
+#include <errno.h>
+#include <cstdio>
+
+#include "butil/raw_pack.h"
+#include "butil/sys_byteorder.h"
+#include "brpc/adapter_transport.h"
+#include "brpc/socket.h"
+
+#if BRPC_WITH_UBRING
+
+#include <array>
+#include <cstring>
+
+#include "butil/logging.h"
+#include "brpc/reloadable_flags.h"
+#include "brpc/ubshm/common/common.h"
+#include "brpc/ubshm/shm/shm_mgr.h"
+#include "brpc/ubshm/ub_endpoint.h"
+#include "brpc/ubshm/ub_helper.h"
+#include "brpc/ubshm/ubr_trx.h"
+#include "brpc/ubshm_transport.h"
+
+#endif
+
+namespace brpc {
+namespace handshake {
+namespace ubshm_wire {
+
+static const char* const MAGIC = "UB";
+static const size_t MAGIC_LEN = 2;
+static const size_t HELLO_LEN = 64;
+static const size_t ACK_LEN = 4;
+#if BRPC_WITH_UBRING
+static const uint16_t HELLO_VERSION = 3;
+static const uint16_t IMPL_VERSION = 1;
+#endif  // BRPC_WITH_UBRING
+static const FrameSpec& HelloFrameSpec() {
+    static const FrameSpec spec(
+        MAGIC, MAGIC_LEN, HELLO_LEN, HELLO_LEN, FrameSpec::FIXED);
+    return spec;

Review Comment:
   A regular NSHEAD request can also start with `UB`. For example, on a 
little-endian machine, with `id=0x4255` and `version=0`, a complete 37-byte 
request will keep waiting here until it reaches 64 bytes.



##########
src/brpc/policy/transport_handshake_protocol.cpp:
##########
@@ -0,0 +1,43 @@
+// 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.
+
+#include "brpc/policy/transport_handshake_protocol.h"
+
+#include "butil/logging.h"
+#include "brpc/adapter_transport.h"
+#include "brpc/socket.h"
+
+namespace brpc {
+namespace policy {
+
+ParseResult ParseTransportHandshake(butil::IOBuf* source, Socket* socket,
+                                     bool /*read_eof*/, const void* /*arg*/) {
+    // URMA still uses its own Transport. Do not cast it to AdapterTransport
+    // when this globally registered parser is tried during protocol detection.
+    if (socket->socket_mode() == SOCKET_MODE_URMA) {
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }

Review Comment:
   After URMA falls back to TCP on its own, the socket mode remains 
`SOCKET_MODE_URMA`. If the fallback request is an RDMA hello, this branch 
passes it to the regular RPC parser, and the client does not receive the 
disabled response that the baseline would send.



##########
src/brpc/handshake/handshake_io.cpp:
##########
@@ -0,0 +1,157 @@
+// 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.
+
+#include "brpc/handshake/handshake_io.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <errno.h>
+#include <unistd.h>
+
+#include "bthread/butex.h"
+#include "butil/time.h"
+#include "brpc/errno.pb.h"
+#include "brpc/socket.h"
+
+namespace brpc {
+namespace handshake {
+
+static const int WAIT_TIMEOUT_MS = 50;
+
+SocketHandshakeIO::SocketHandshakeIO(Socket* socket)
+    : _socket(socket)
+    , _read_butex(bthread::butex_create_checked<butil::atomic<int> >()) {
+    _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+SocketHandshakeIO::~SocketHandshakeIO() {
+    bthread::butex_destroy(_read_butex);
+}
+
+void SocketHandshakeIO::Reset(Socket* socket) {
+    _socket = socket;
+}
+
+void SocketHandshakeIO::NotifyReadable() {
+    _read_butex->fetch_add(1, butil::memory_order_release);
+    bthread::butex_wake(_read_butex);
+}
+
+int SocketHandshakeIO::GetWaitDeadline(timespec* deadline) const {
+    if (_socket->Failed()) {
+        errno = EFAILEDSOCKET;
+        return -1;
+    }
+    const int64_t connect_deadline = _socket->_connect_deadline_us;
+    const int64_t now = butil::gettimeofday_us();
+    if (connect_deadline >= 0 && now >= connect_deadline) {
+        errno = ETIMEDOUT;
+        return -1;
+    }
+    const int64_t wait_until = now + WAIT_TIMEOUT_MS * 1000;
+    *deadline = butil::microseconds_to_timespec(
+        connect_deadline < 0 ? wait_until : std::min(wait_until, 
connect_deadline));
+    return 0;
+}
+
+int SocketHandshakeIO::ReadExact(void* data, size_t len) {
+    CHECK(data != nullptr);
+    CHECK(_socket != nullptr);
+    const int fd = _socket->fd();
+    size_t received = 0;
+    while (received < len) {
+        const int expected = _read_butex->load(butil::memory_order_acquire);
+        timespec duetime;
+        if (GetWaitDeadline(&duetime) != 0) {
+            return -1;
+        }
+        const ssize_t nr = read(
+            fd, static_cast<uint8_t*>(data) + received, len - received);
+        if (nr < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            if (errno != EAGAIN) {
+                return -1;
+            }
+            if (bthread::butex_wait(_read_butex, expected, &duetime) < 0 &&
+                errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+                return -1;
+            }
+        } else if (nr == 0) {
+            errno = EEOF;
+            return -1;
+        } else {
+            received += nr;
+        }
+    }
+    return 0;
+}
+
+int SocketHandshakeIO::WriteAll(const void* data, size_t len) {
+    CHECK(data != nullptr);
+    CHECK(_socket != nullptr);
+    const int fd = _socket->fd();
+    size_t written = 0;
+    while (written < len) {
+        timespec duetime;
+        if (GetWaitDeadline(&duetime) != 0) {
+            return -1;
+        }
+        const ssize_t nw = write(
+            fd, static_cast<const uint8_t*>(data) + written, len - written);

Review Comment:
   The server can receive a TLS-decrypted RDMA hello through `Socket::DoRead`, 
for example when a front proxy forwards the connection over TLS. Using 
`write(fd, ...)` here writes the fallback hello in plaintext into the TLS 
stream.



##########
src/brpc/handshake/ubshm_handshake.cpp:
##########
@@ -0,0 +1,574 @@
+// 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.
+
+#include "brpc/handshake/ubshm_handshake.h"
+
+#include <errno.h>
+#include <cstdio>
+
+#include "butil/raw_pack.h"
+#include "butil/sys_byteorder.h"
+#include "brpc/adapter_transport.h"
+#include "brpc/socket.h"
+
+#if BRPC_WITH_UBRING
+
+#include <array>
+#include <cstring>
+
+#include "butil/logging.h"
+#include "brpc/reloadable_flags.h"
+#include "brpc/ubshm/common/common.h"
+#include "brpc/ubshm/shm/shm_mgr.h"
+#include "brpc/ubshm/ub_endpoint.h"
+#include "brpc/ubshm/ub_helper.h"
+#include "brpc/ubshm/ubr_trx.h"
+#include "brpc/ubshm_transport.h"
+
+#endif
+
+namespace brpc {
+namespace handshake {
+namespace ubshm_wire {
+
+static const char* const MAGIC = "UB";
+static const size_t MAGIC_LEN = 2;
+static const size_t HELLO_LEN = 64;
+static const size_t ACK_LEN = 4;
+#if BRPC_WITH_UBRING
+static const uint16_t HELLO_VERSION = 3;
+static const uint16_t IMPL_VERSION = 1;
+#endif  // BRPC_WITH_UBRING
+static const FrameSpec& HelloFrameSpec() {
+    static const FrameSpec spec(
+        MAGIC, MAGIC_LEN, HELLO_LEN, HELLO_LEN, FrameSpec::FIXED);
+    return spec;
+}
+
+static const FrameSpec& AckFrameSpec() {
+    static const FrameSpec spec(
+        nullptr, 0, ACK_LEN, ACK_LEN, FrameSpec::FIXED);
+    return spec;
+}
+
+}  // namespace ubshm_wire
+}  // namespace handshake
+}  // namespace brpc
+
+#if BRPC_WITH_UBRING
+
+namespace brpc {
+namespace ubring {
+
+DEFINE_int32(data_queue_size, 4, "data queue size for UB");
+DEFINE_bool(ub_trace_verbose, false, "Print log message verbosely");
+BRPC_VALIDATE_GFLAG(ub_trace_verbose, brpc::PassValidate);
+
+UbrDataFormat PreferredDataFormatForShmType(SHM_TYPE shm_type) {
+    switch (shm_type) {
+    case SHM_TYPE_IPC:
+        return UBR_DATA_FORMAT_IPC_V2;
+    case SHM_TYPE_UBS:
+        return UBR_DATA_FORMAT_LEGACY_64;
+    default:
+        return UBR_DATA_FORMAT_NONE;
+    }
+}
+
+UbrDataFormat SelectDataFormat(UbrDataFormat local_format,
+                               uint16_t remote_format_id) {
+    if ((local_format != UBR_DATA_FORMAT_LEGACY_64 &&
+         local_format != UBR_DATA_FORMAT_IPC_V2) ||
+        remote_format_id != static_cast<uint16_t>(local_format)) {
+        return UBR_DATA_FORMAT_NONE;
+    }
+    return local_format;
+}
+
+void HelloMessage::Serialize(void* data) const {
+    char* current_pos = static_cast<char*>(data);
+    const uint16_t net_msg_len = butil::HostToNet16(msg_len);
+    memcpy(current_pos, &net_msg_len, sizeof(net_msg_len));
+    current_pos += sizeof(net_msg_len);
+    const uint16_t net_hello_ver = butil::HostToNet16(hello_ver);
+    memcpy(current_pos, &net_hello_ver, sizeof(net_hello_ver));
+    current_pos += sizeof(net_hello_ver);
+    const uint16_t net_impl_ver = butil::HostToNet16(impl_ver);
+    memcpy(current_pos, &net_impl_ver, sizeof(net_impl_ver));
+    current_pos += sizeof(net_impl_ver);
+    const uint64_t net_len = butil::HostToNet64(len);
+    memcpy(current_pos, &net_len, sizeof(net_len));
+    current_pos += sizeof(net_len);
+    memcpy(current_pos, shm_name, SHM_MAX_NAME_BUFF_LEN);
+}
+
+void HelloFormatExtension::Serialize(void* data) const {
+    char* current = static_cast<char*>(data);
+    const uint16_t length = butil::HostToNet16(extension_len);
+    const uint16_t format = butil::HostToNet16(format_id);
+    memcpy(current, &length, sizeof(length));
+    memcpy(current + sizeof(length), &format, sizeof(format));
+}
+
+void HelloFormatExtension::Deserialize(const void* data) {
+    const char* current = static_cast<const char*>(data);
+    uint16_t length;
+    uint16_t format;
+    memcpy(&length, current, sizeof(length));
+    memcpy(&format, current + sizeof(length), sizeof(format));
+    extension_len = butil::NetToHost16(length);
+    format_id = butil::NetToHost16(format);
+}
+
+void HelloMessage::Deserialize(const void* data) {
+    const char* current_pos = static_cast<const char*>(data);
+    uint16_t net_msg_len;
+    memcpy(&net_msg_len, current_pos, sizeof(net_msg_len));
+    msg_len = butil::NetToHost16(net_msg_len);
+    current_pos += sizeof(net_msg_len);
+    uint16_t net_hello_ver;
+    memcpy(&net_hello_ver, current_pos, sizeof(net_hello_ver));
+    hello_ver = butil::NetToHost16(net_hello_ver);
+    current_pos += sizeof(net_hello_ver);
+    uint16_t net_impl_ver;
+    memcpy(&net_impl_ver, current_pos, sizeof(net_impl_ver));
+    impl_ver = butil::NetToHost16(net_impl_ver);
+    current_pos += sizeof(net_impl_ver);
+    uint64_t net_len;
+    memcpy(&net_len, current_pos, sizeof(net_len));
+    len = butil::NetToHost64(net_len);
+    current_pos += sizeof(net_len);
+    memcpy(shm_name, current_pos, SHM_MAX_NAME_BUFF_LEN);
+    // The decoded name is also used directly as a C-string by callers.
+    shm_name[SHM_MAX_NAME_BUFF_LEN - 1] = '\0';
+}
+
+std::string HelloMessage::toString() const {
+    constexpr size_t MAX_LEN =
+        16 + 6 + 16 + 6 + 16 + 6 + 20 + 6 + SHM_MAX_NAME_BUFF_LEN + 32;
+    std::array<char, MAX_LEN> buf;
+    const int n = snprintf(
+        buf.data(), buf.size(),
+        "msg_len=%u, hello_ver=%u, impl_ver=%u, len=%lu, shm_name=%.*s",
+        msg_len, hello_ver, impl_ver,
+        static_cast<unsigned long>(len),
+        static_cast<int>(SHM_MAX_NAME_BUFF_LEN), shm_name);
+    return std::string(buf.data(), static_cast<size_t>(n));
+}
+
+const handshake::FrameSpec& UBShmHandshakeAdapter::HelloFrameSpec() const {
+    return handshake::ubshm_wire::HelloFrameSpec();
+}
+
+const handshake::FrameSpec& UBShmHandshakeAdapter::AckFrameSpec() const {
+    return handshake::ubshm_wire::AckFrameSpec();
+}
+
+const handshake::FrameSpec&
+UBShmHandshakeAdapter::ExtensionFrameSpec() const {
+    static const handshake::FrameSpec spec(
+        nullptr, 0, HelloFormatExtension::WIRE_SIZE,
+        HelloFormatExtension::WIRE_SIZE, handshake::FrameSpec::FIXED);
+    return spec;
+}
+
+void UBShmHandshakeAdapter::ConfigureClientHello(
+    uint64_t len, const char* shm_name) {
+    _local_len = len;
+    _local_name = shm_name != nullptr ? shm_name : "";
+    _server_reply = false;
+}
+
+handshake::StepResult UBShmHandshakeAdapter::BuildHello(
+    bool enabled, std::string* payload) {
+    const char* name = nullptr;
+    if (enabled) {
+        name = _server_reply ? _remote.shm_name : _local_name.c_str();
+    }
+    return BuildHello(enabled, enabled ? _local_len : 0, name, payload);
+}
+
+handshake::StepResult UBShmHandshakeAdapter::ParseHello(
+    const std::string& payload) {
+    return ParseHello(payload, &_remote);
+}
+
+handshake::StepResult UBShmHandshakeAdapter::BuildExtension(
+    bool enabled, std::string* payload) {
+    const HelloFormatExtension extension = {
+        HelloFormatExtension::WIRE_SIZE,
+        static_cast<uint16_t>(enabled ? _local_format
+                                      : UBR_DATA_FORMAT_NONE)};
+    payload->resize(HelloFormatExtension::WIRE_SIZE);
+    extension.Serialize(&(*payload)[0]);
+    return handshake::STEP_OK;
+}
+
+handshake::StepResult UBShmHandshakeAdapter::ParseExtension(
+    const std::string& payload) {
+    if (payload.size() != HelloFormatExtension::WIRE_SIZE) {
+        errno = EPROTO;
+        return handshake::STEP_ERROR;
+    }
+    HelloFormatExtension extension{};
+    extension.Deserialize(payload.data());
+    return extension.extension_len == HelloFormatExtension::WIRE_SIZE &&
+                   SelectDataFormat(_local_format, extension.format_id) !=
+                       UBR_DATA_FORMAT_NONE
+               ? handshake::STEP_OK : handshake::STEP_FALLBACK;
+}
+
+handshake::StepResult UBShmHandshakeAdapter::BuildHello(
+    bool enabled, uint64_t len, const char* shm_name,
+    std::string* payload) const {
+    HelloMessage message{};
+    message.msg_len = static_cast<uint16_t>(
+        handshake::ubshm_wire::HELLO_LEN);
+    if (enabled) {
+        message.hello_ver = handshake::ubshm_wire::HELLO_VERSION;
+        message.impl_ver = handshake::ubshm_wire::IMPL_VERSION;
+        message.len = len;
+        if (shm_name == nullptr) {
+            errno = EINVAL;
+            return handshake::STEP_ERROR;
+        }
+        const size_t shm_name_len =
+            strnlen(shm_name, SHM_MAX_NAME_LEN);
+        memcpy(message.shm_name, shm_name, shm_name_len);
+    }
+    payload->assign(
+        handshake::ubshm_wire::HELLO_LEN -
+            handshake::ubshm_wire::MAGIC_LEN,
+        '\0');
+    message.Serialize(&(*payload)[0]);
+    return handshake::STEP_OK;
+}
+
+handshake::StepResult UBShmHandshakeAdapter::ParseHello(
+    const std::string& payload, HelloMessage* message) const {
+    if (payload.size() != handshake::ubshm_wire::HELLO_LEN -
+                              handshake::ubshm_wire::MAGIC_LEN) {
+        errno = EPROTO;
+        return handshake::STEP_ERROR;
+    }
+    message->Deserialize(payload.data());
+    if (message->msg_len != handshake::ubshm_wire::HELLO_LEN) {
+        errno = EPROTO;
+        return handshake::STEP_ERROR;
+    }
+    if (!NegotiationValid(*message)) {
+        return handshake::STEP_FALLBACK;
+    }
+    // Deserialize terminates the decoded name for safe C-string use. Inspect
+    // the original wire bytes so that truncation cannot hide a malformed name.
+    const char* wire_name =
+        payload.data() + payload.size() - SHM_MAX_NAME_BUFF_LEN;
+    if (memchr(wire_name, '\0', SHM_MAX_NAME_BUFF_LEN) == nullptr) {
+        errno = EPROTO;
+        return handshake::STEP_ERROR;
+    }
+    return handshake::STEP_OK;
+}
+
+bool UBShmHandshakeAdapter::NegotiationValid(
+    const HelloMessage& message) const {
+    return message.hello_ver == handshake::ubshm_wire::HELLO_VERSION &&
+           message.impl_ver == handshake::ubshm_wire::IMPL_VERSION;
+}
+
+}  // namespace ubring
+}  // namespace brpc
+
+#endif  // BRPC_WITH_UBRING
+
+namespace brpc {
+namespace handshake {
+
+class UBShmServerHandshakeAdapter : public HandshakeAdapter {
+public:
+    UBShmServerHandshakeAdapter() = default;
+
+protected:
+    StepResult RunServerStep(
+        butil::IOBuf* source, Socket* socket) override;
+    HandshakeSession* GetSession(Socket* socket) const override;
+
+private:
+    StepResult RunFallbackServerHandshake(
+        butil::IOBuf* source, Socket* socket);
+#if BRPC_WITH_UBRING
+    StepResult RunUBShmServerHandshake(
+        butil::IOBuf* source, Socket* socket);
+#endif
+
+    DISALLOW_COPY_AND_ASSIGN(UBShmServerHandshakeAdapter);
+};
+
+
+class UBShmFallbackProtocol : public FallbackHandshakeProtocol {
+public:
+    int ProtocolVersion() const override { return 2; }
+    const FrameSpec& HelloFrameSpec() const override {
+        return ubshm_wire::HelloFrameSpec();
+    }
+    const FrameSpec& AckFrameSpec() const override {
+        return ubshm_wire::AckFrameSpec();
+    }
+    StepResult BuildHello(bool enabled, std::string* payload) override {
+        if (enabled) {
+            errno = EPROTO;
+            return STEP_ERROR;
+        }
+        payload->assign(
+            ubshm_wire::HELLO_LEN - ubshm_wire::MAGIC_LEN, '\0');
+        butil::RawPacker(&(*payload)[0])
+            .pack16(static_cast<uint16_t>(ubshm_wire::HELLO_LEN));
+        return STEP_OK;
+    }
+};
+
+#if BRPC_WITH_UBRING
+namespace {
+
+class UBShmClientHandshakeTransport : public HandshakeTransport {
+public:
+    UBShmClientHandshakeTransport(
+        UBShmTransport* transport, ubring::SHM* local_shm,
+        const char* shm_name, ubring::UBShmHandshakeAdapter* protocol,
+        Socket* socket)
+        : _transport(transport), _local_shm(local_shm),
+          _shm_name(shm_name), _protocol(protocol), _socket(socket) {}
+
+    StepResult PrepareResources() override {
+        if (_transport->PrepareUpgradeResources(
+                _local_shm, _shm_name) != 0) {
+            return STEP_FALLBACK;
+        }
+        _protocol->ConfigureClientHello(*_local_shm);
+        return STEP_OK;
+    }
+
+    StepResult NegotiateResources() override {
+        return _transport->NegotiateUpgradeResources(
+                   _local_shm, _shm_name, _protocol->remote().len,
+                   _protocol->local_format()) == 0
+            ? STEP_OK : STEP_FALLBACK;
+    }
+
+    void OnEstablished() override {
+        _transport->GetUBShmEp()->SetNegotiatedDataFormat(
+            _protocol->local_format());
+        _transport->ActivateUpgrade();
+    }
+    void OnFallback() override { _transport->DeactivateUpgrade(); }
+
+    void OnFailed() override {
+        const int error = errno != 0 ? errno : EPROTO;
+        _transport->DeactivateUpgrade();
+        _socket->SetFailed(error, "Fail to complete ubring handshake from %s: 
%s",
+                           _socket->description().c_str(), berror(error));

Review Comment:
   `SetFailed(ETIMEDOUT)` is called first here, so even if `AfterAppConnected` 
reaches the consecutive timeout threshold, it cannot change the error already 
recorded on the socket to `ENETUNREACH`.



##########
src/brpc/ubshm/ub_ring.cpp:
##########
@@ -61,7 +76,52 @@ RETURN_CODE UBRing::UbrTrxMapShm(SHM *local_shm, SHM 
*remote_shm)
     return UBRING_OK;
 }
 
+RETURN_CODE UBRing::UbrTrxAbort() {
+    if (_trx == nullptr) {
+        return UBRING_OK;
+    }
+    if (ATOMIC_LOAD(_trx->close_state) != UBR_CLOSE_FIRST) {
+        return ReleaseTrx();
+    }
+    if (_trx->ubr_tx.local_tx_event_q.addr != nullptr) {
+        const RETURN_CODE close_rc = UbrTrxCloseCheck(_trx);
+        if (close_rc != UBRING_OK) {
+            return close_rc;
+        }
+    }
+    int expected = UBR_CLOSE_FIRST;
+    if (!ATOMIC_COMPARE_EXCHANGE_STRONG(
+            _trx->close_state, expected, UBR_CLOSE_SECOND)) {
+        return ReleaseTrx();
+    }
+    // Drain timer callbacks before unmapping the queues they access.
+    DeleteTimerSafe(_trx->timer_fd);
+    DeleteTimerSafe(_trx->hb_timer_fd);
+    DeleteTimerSafe(_trx->clear_timer_fd);

Review Comment:
   All three fields are `0` before a new transaction creates a timer, but `0` 
is also a valid timerfd. If another transaction happens to hold fd `0`, running 
`UbrTrxAbort` after this transaction's mapping fails will cancel the other 
transaction's timer.



##########
src/brpc/ubshm/ub_ring_manager.cpp:
##########
@@ -239,26 +257,34 @@ int32_t UBRingManager::UbEventCallback(const char 
*shm_name)
         LOG(ERROR) << "Ub event callback failed, shm name is null.";
         return UBRING_ERR;
     }
-    if (UNLIKELY(g_ubr_mgr.trx_mgr == nullptr)) {
-        LOG(ERROR) << "Ub event callback failed, trx mgr is null.";
-        return UBRING_ERR;
-    }
     LOG(INFO) << "Ub event callback is processing. shm_name=" << shm_name;
-
-    for (uint32_t i = 0; i < g_ubr_mgr.trx_cap; ++i) {
-        if (g_ubr_mgr.trx_mgr_unit_status[i] == UBR_MGR_UNIT_FREE) {
-            continue;
-        }
-
-        if (strcmp(g_ubr_mgr.trx_mgr[i].local_shm.name, shm_name) == 0 ||   // 
the failed link is this trx's local shm
-            strcmp(g_ubr_mgr.trx_mgr[i].remote_shm.name, shm_name) == 0) {  // 
the failed link is this trx's remote shm
-            ++g_ub_event_cnt;
-            int fd = (int)g_ubr_mgr.trx_mgr[i].local_shm.fd;
-            LOG(WARNING) << "Ub event callback, the fd of the faulty link is " 
<< fd;
-            return UBRing::UbrPassiveClearTrx(&g_ubr_mgr.trx_mgr[i], fd, 
UBR_UB_EVENT);
+    UbrTrx* trx = nullptr;
+    {
+        LOCK_GUARD(g_ubr_trx_mgr_mtx);
+        for (uint32_t i = 0; i < g_ubr_mgr.trx_cap; ++i) {
+            if (g_ubr_mgr.trx_mgr_unit_status[i] == UBR_MGR_UNIT_FREE) {
+                continue;
+            }
+            UbrTrx* candidate = &g_ubr_mgr.trx_mgr[i];
+            if (strcmp(candidate->local_shm.name, shm_name) == 0 ||
+                strcmp(candidate->remote_shm.name, shm_name) == 0) {
+                ATOMIC_STORE(candidate->device_fault, true);
+                if (ATOMIC_LOAD(candidate->initializing)) {
+                    return UBRING_OK;
+                }
+                trx = candidate;
+                ATOMIC_ADD(trx->ref_count, 1);
+                ++g_ub_event_cnt;
+                break;

Review Comment:
   After passive cleanup completes, a ring reference can still retain the old 
slot and name. When the name is reused, this code finds the old transaction 
first and executes `break`; the subsequent duplicate close returns success, and 
the actual new transaction never receives the device failure notification.



##########
src/brpc/adapter_transport.cpp:
##########
@@ -0,0 +1,416 @@
+// 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.
+
+#include "brpc/adapter_transport.h"
+
+#include <cstdint>
+#include <errno.h>
+#include <unistd.h>
+
+#include "brpc/input_messenger.h"
+#include "brpc/handshake/handshake_adapter.h"
+#include "brpc/tcp_transport.h"
+
+namespace brpc {
+namespace {
+
+class AdapterConnect : public AppConnect {
+public:
+    explicit AdapterConnect(const std::shared_ptr<AppConnect>& app_connect)
+        : _app_connect(app_connect) {}
+
+    static std::shared_ptr<AppConnect> Wrap(
+        const std::shared_ptr<AppConnect>& app_connect) {
+        if (std::dynamic_pointer_cast<AdapterConnect>(app_connect)) {
+            return app_connect;
+        }
+        return std::make_shared<AdapterConnect>(app_connect);
+    }
+
+    static std::shared_ptr<AppConnect> Unwrap(
+        const std::shared_ptr<AppConnect>& app_connect) {
+        const std::shared_ptr<AdapterConnect> adapter =
+            std::dynamic_pointer_cast<AdapterConnect>(app_connect);
+        return adapter ? adapter->_app_connect : app_connect;
+    }
+
+    void StartConnect(const Socket* socket,
+                      void (*done)(int, void*), void* data) override {
+        ApplicationConnectTask* task = new ApplicationConnectTask{
+            socket, _app_connect, done, data};
+        if (AdapterTransport::StartClientUpgrade(
+                socket, OnUpgradeComplete, task) != 0) {
+            const int error = errno != 0 ? errno : EAGAIN;
+            AdapterTransport::Get(const_cast<Socket*>(socket))
+                ->handshake_session()->MarkFailed();
+            delete task;
+            done(error, data);
+        }
+    }
+
+    void StopConnect(Socket* socket) override {
+        if (_app_connect) {
+            _app_connect->StopConnect(socket);
+        }
+    }
+
+private:
+    struct ApplicationConnectTask {
+        const Socket* socket;
+        std::shared_ptr<AppConnect> app_connect;
+        void (*done)(int, void*);
+        void* data;
+    };
+
+    static void OnApplicationComplete(int error, void* arg) {
+        std::unique_ptr<ApplicationConnectTask> task(
+            static_cast<ApplicationConnectTask*>(arg));
+        task->done(error, task->data);
+    }
+
+    static void OnUpgradeComplete(int error, void* arg) {
+        ApplicationConnectTask* task =
+            static_cast<ApplicationConnectTask*>(arg);
+        if (error != 0 || !task->app_connect) {
+            std::unique_ptr<ApplicationConnectTask> owned(task);
+            task->done(error, task->data);
+            return;
+        }
+        task->app_connect->StartConnect(
+            task->socket, OnApplicationComplete, task);
+    }
+
+    std::shared_ptr<AppConnect> _app_connect;
+};
+
+struct ClientHandshakeTask {
+    AdapterTransport* adapter;
+    void (*done)(int, void*);
+    void* data;
+    SocketUniquePtr socket;
+};
+
+}  // namespace
+
+AdapterTransport::AdapterTransport(
+    const handshake::HandshakeRegistration::Upgrade* upgrade)
+    : _upgrade(upgrade) {}
+AdapterTransport::~AdapterTransport() = default;
+
+AdapterTransport* AdapterTransport::Get(Socket* socket) {
+    CHECK(socket != nullptr);
+    return static_cast<AdapterTransport*>(socket->_transport.get());
+}
+
+const AdapterTransport* AdapterTransport::Get(const Socket* socket) {
+    CHECK(socket != nullptr);
+    return static_cast<const AdapterTransport*>(socket->_transport.get());
+}
+
+bool AdapterTransport::upgrade_capable(SocketMode mode) const {
+    return _socket->socket_mode() == mode && _high_speed_transport &&
+           _upgrade->ready(_high_speed_transport.get());
+}
+
+int AdapterTransport::StartClientUpgrade(const Socket* socket,
+                                         void (*done)(int, void*),
+                                         void* data) {
+    return StartClientUpgradeImpl(socket, done, data, 
bthread_start_background);
+}
+
+int AdapterTransport::StartClientUpgradeImpl(
+    const Socket* socket, void (*done)(int, void*), void* data,
+    StartBackground start_background) {
+    AdapterTransport* adapter = Get(const_cast<Socket*>(socket));
+    ClientHandshakeTask* task = new ClientHandshakeTask{adapter, done, data, 
SocketUniquePtr()};
+    if (Socket::Address(socket->id(), &task->socket) != 0) {
+        delete task;
+        errno = EFAILEDSOCKET;
+        return -1;
+    }
+    bthread_t tid;
+    bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
+    bthread_attr_set_name(&attr, "StartClientUpgrade");
+    const int rc = start_background(&tid, &attr, ProcessClientHandshake, task);
+    if (rc != 0) {
+        delete task;
+        errno = rc;
+        return -1;
+    }
+    return 0;
+}
+
+ParseResult AdapterTransport::ProcessUpgradeReadable(butil::IOBuf* source) {
+    if (_handshake.phase(butil::memory_order_acquire) ==
+        handshake::ESTABLISHED) {
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    ParseResult result(PARSE_ERROR_NOT_ENOUGH_DATA);
+    if (_socket->parsing_context() != nullptr) {
+        handshake::ServerHandshakeContext* context =
+            static_cast<handshake::ServerHandshakeContext*>(
+                _socket->parsing_context());
+        CHECK(context->adapter() != nullptr);
+        result = context->adapter()->ExecuteServerHandshake(source, _socket);
+    } else if (!source->empty()) {
+        auto* handler = handshake::HandshakeRegistration::MatchHello(*source);
+        result = handler ? handler->ExecuteServerHandshake(source, _socket)
+                         : MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    return result;
+}
+
+void* AdapterTransport::ProcessClientHandshake(void* arg) {
+    std::unique_ptr<ClientHandshakeTask> task(
+        static_cast<ClientHandshakeTask*>(arg));
+    AdapterTransport* adapter = task->adapter;
+    Socket* socket = task->socket.get();
+    if (!adapter->_upgrade) {
+        socket->SetFailed(EPROTO, "Unsupported client transport handshake");
+        adapter->_handshake.MarkFailed();
+        task->done(EPROTO, task->data);
+        return nullptr;
+    }
+    if (!adapter->_upgrade->available()) {
+        adapter->FallbackToTcp();
+        task->done(0, task->data);
+        return nullptr;
+    }
+
+    int error = adapter->_upgrade->run_client(
+        adapter->_high_speed_transport.get(), socket, adapter->_handshake);
+    if (error == 0 && adapter->handshake_phase() == handshake::ESTABLISHED) {
+        error = adapter->StartUpgradeReceive();
+        if (error != 0) {
+            LOG(WARNING) << "Fail to start upgrade receive events from "
+                         << socket->description() << ": " << berror(error);
+        }
+    }
+    task->done(error, task->data);
+    return nullptr;
+}
+
+void AdapterTransport::Init(Socket* socket, const SocketOptions& options) {
+    _socket = socket;
+    _default_connect = options.app_connect;
+    _on_edge_trigger = options.on_edge_triggered_events;
+    if (options.need_on_edge_trigger && _on_edge_trigger == nullptr) {
+        if (!_upgrade) {
+            _on_edge_trigger = InputMessenger::OnNewMessages;
+        } else if (options.user != static_cast<SocketUser*>(
+                       get_client_side_messenger())) {
+            _on_edge_trigger = OnNewMessagesAfterUpgrade;
+        } else {
+            _on_edge_trigger = OnNewDataFromTcp;
+        }
+    }
+    _handshake.Reset(socket);
+    _tcp_transport.reset(new TcpTransport);
+    _tcp_transport->Init(socket, options);
+
+    if (_upgrade) {
+        _high_speed_transport = _upgrade->create();
+        _high_speed_transport->Init(socket, options);
+    }
+}
+
+void AdapterTransport::Release() {
+    if (_high_speed_transport) {
+        _high_speed_transport->Release();
+    }
+    _tcp_transport->Release();
+}
+
+int AdapterTransport::Reset(int32_t expected_nref) {
+    if (_high_speed_transport) {
+        _high_speed_transport->Reset(expected_nref);
+    }
+    _tcp_transport->Reset(expected_nref);
+    _handshake.Reset(_socket);
+    return 0;
+}
+
+std::shared_ptr<AppConnect> AdapterTransport::Connect() {
+    if (upgrade_capable(_socket->socket_mode())) {
+        return AdapterConnect::Wrap(_default_connect);
+    }
+    SocketUser* const client_messenger =
+        static_cast<SocketUser*>(get_client_side_messenger());
+    if (client_messenger != nullptr &&
+        _socket->user() == client_messenger) {
+        // This socket keeps the unwrapped AppConnect across health-check
+        // resets, so its read path must not depend on another handshake.
+        _on_edge_trigger = _tcp_transport->GetOnEdgeTrigger();
+        FallbackToTcp();
+        return AdapterConnect::Unwrap(_tcp_transport->Connect());
+    }
+    return _tcp_transport->Connect();
+}
+
+Transport* AdapterTransport::ActiveTransport() const {
+    if (_high_speed_transport &&
+        _handshake.phase(butil::memory_order_acquire) ==
+            handshake::ESTABLISHED) {
+        return _high_speed_transport.get();
+    }
+    return _tcp_transport.get();
+}
+
+int AdapterTransport::CutFromIOBuf(butil::IOBuf* buf) {
+    return ActiveTransport()->CutFromIOBuf(buf);
+}
+
+ssize_t AdapterTransport::CutFromIOBufList(
+    butil::IOBuf** buf, size_t ndata) {
+    return ActiveTransport()->CutFromIOBufList(buf, ndata);
+}
+
+int AdapterTransport::WaitEpollOut(butil::atomic<int>* epollout_butex,
+                                    bool pollin, timespec duetime) {
+    return ActiveTransport()->WaitEpollOut(
+        epollout_butex, pollin, duetime);
+}
+
+void AdapterTransport::ProcessEvent(bthread_attr_t attr) {
+    ActiveTransport()->ProcessEvent(attr);
+}
+
+void AdapterTransport::QueueMessage(InputMessageClosure& input_msg,
+                                    int* num_bthread_created,
+                                    bool last_msg) {
+    ActiveTransport()->QueueMessage(
+        input_msg, num_bthread_created, last_msg);
+}
+
+void AdapterTransport::Debug(std::ostream& os) {
+    if (_high_speed_transport) {
+        _high_speed_transport->Debug(os);
+    }
+    const char* state = "UNKNOWN";
+    switch (_handshake.phase(butil::memory_order_acquire)) {
+    case handshake::UNINITIALIZED: state = "UNINITIALIZED"; break;
+    case handshake::PREPARING: state = "PREPARING"; break;
+    case handshake::HELLO_SEND: state = "HELLO_SEND"; break;
+    case handshake::HELLO_WAIT: state = "HELLO_WAIT"; break;
+    case handshake::NEGOTIATING: state = "NEGOTIATING"; break;
+    case handshake::ACK_SEND: state = "ACK_SEND"; break;
+    case handshake::ACK_WAIT: state = "ACK_WAIT"; break;
+    case handshake::EXTENSION_SEND: state = "EXTENSION_SEND"; break;
+    case handshake::EXTENSION_WAIT: state = "EXTENSION_WAIT"; break;
+    case handshake::ESTABLISHED: state = "ESTABLISHED"; break;
+    case handshake::FALLBACK_TCP: state = "FALLBACK_TCP"; break;
+    case handshake::FAILED: state = "FAILED"; break;
+    }
+    os << "\nhandshake_state=" << state
+       << "\nhandshake_version=" << _handshake.protocol_version();
+}
+
+void AdapterTransport::FallbackToTcp() {
+    _handshake.PublishFallback([this]() {
+        if (_high_speed_transport) {
+            _upgrade->disable(_high_speed_transport.get());
+        }
+    });
+}
+
+int AdapterTransport::StartUpgradeReceive() {
+    const int error = _upgrade->start_receive(_high_speed_transport.get());
+    if (error != 0) {
+        _handshake.MarkFailed();
+    }
+    return error;
+}
+
+void AdapterTransport::OnNewMessagesAfterUpgrade(Socket* socket) {
+    AdapterTransport* adapter = Get(socket);
+    if (adapter->_handshake.phase(butil::memory_order_acquire) ==
+        handshake::ESTABLISHED) {
+        adapter->CheckUnexpectedTcpData();
+        return;
+    }
+
+    InputMessenger::OnNewMessagesUntil(socket, [](Socket* s) {
+        return Get(s)->handshake_phase() == handshake::ESTABLISHED;
+    });
+
+    if (adapter->handshake_phase() == handshake::ESTABLISHED) {
+        const int error = adapter->StartUpgradeReceive();

Review Comment:
   `OnNewMessagesUntil` can also return because `MoreReadEvents` has yielded 
the read event. At that point, another callback may be parsing the final ACK, 
publishing `ESTABLISHED` before leaving the parsing guard. If the earlier 
callback starts the ring here, the first RPC can enter the same parser 
concurrently with the ACK.



##########
src/brpc/ubshm/shm/shm_ubs.cpp:
##########
@@ -133,68 +136,132 @@ static uint64_t AquireFlagIfWrDelayComp(const uint64_t 
flag)
     return flag | UBSM_FLAG_WR_DELAY_COMP;
 }
 
-RETURN_CODE UbsShmLocalMalloc(SHM *shm)
-{
-    int ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len, 
SHM_RIGHT_MODE,
-        AquireFlagIfWrDelayComp(UBSM_FLAG_ONLY_IMPORT_NONCACHE | 
UBSM_FLAG_MEM_ANONYMOUS));
-do {
-    if (ret == UBSM_ERR_ALREADY_EXIST) {
-        if (ubsmem_shmem_deallocate(shm->name) != UBSM_OK) {
-            LOG(ERROR) << "Ubs create shm name=" << shm->name << " failed, shm 
exists, ret=" << ret;
-            return SHM_ERR_EXIST;
-        }
-        LOG(INFO) << "Ubs delete shm name=" << shm->name << " success, try to 
recreate.";
-        ret = ubsmem_shmem_allocate(g_region_name, shm->name, shm->len, 
SHM_RIGHT_MODE,
-            AquireFlagIfWrDelayComp(UBSM_FLAG_ONLY_IMPORT_NONCACHE | 
UBSM_FLAG_MEM_ANONYMOUS));
+// Each task owns a copy of the mapping and its remaining operations.
+static bool TryUbsCleanup(ShmListNode* task) {
+    if (task->unmap) {
+        const int ret = ubsmem_shmem_unmap(task->shm.addr, task->shm.len);
         if (ret != UBSM_OK) {
-            LOG(ERROR) << "Ubs recreate shm name=" << shm->name << " failed, 
ret=" << ret;
-            return SHM_ERR;
+            LOG_EVERY_SECOND(WARNING) << "Ubs unmap shm=" << task->shm.name
+                                     << " failed, ret=" << ret;
+            return false;
         }
-    } else if (ret != UBSM_OK) {
-        LOG(ERROR) << "Ubs create shm name=" << shm->name << " failed, ret=" 
<< ret;
-        return SHM_ERR;
+        task->unmap = false;
+    }
+    if (task->deallocate) {
+        const int ret = ubsmem_shmem_deallocate(task->shm.name);
+        if (ret != UBSM_OK && ret != UBSM_ERR_NOT_FOUND) {
+            LOG_EVERY_SECOND(WARNING) << "Ubs deallocate shm=" << 
task->shm.name
+                                     << " failed, ret=" << ret;
+            return false;
+        }
+        task->deallocate = false;
     }
-} while (0);
+    return true;
+}
 
-    ret = ubsmem_shmem_map(nullptr, shm->len, PROT_READ | PROT_WRITE, 
MAP_SHARED, shm->name, 0, (void**)&(shm->addr));
-    if (ret != UBSM_OK) {
-        LOG(ERROR) << "Ubs map shm=" << shm->name << " failed, ret=" << ret;
-        if (ret == UBSM_ERR_NOT_FOUND) {
-            return SHM_ERR_NOT_FOUND;
+static RETURN_CODE ReleaseUbsShmLocked(ShmList* list, SHM* shm,
+                                      ShmListNode* task) {
+    if (list != nullptr) {
+        for (ShmListNode* node = list->head; node != nullptr; node = 
node->next) {
+            if (node->shm.addr == task->shm.addr &&
+                node->shm.len == task->shm.len &&
+                strcmp(node->shm.name, task->shm.name) == 0) {
+                node->deallocate |= task->deallocate;
+                free(task);
+                shm->addr = nullptr;
+                return UBRING_OK;

Review Comment:
   The SDK's address reuse behavior needs confirmation. The old task may have 
already completed unmapping, with only name deletion awaiting retry. If a new 
mapping with the same name gets the same address, this code frees the new task 
without recording its unmap.



##########
src/brpc/adapter_transport.cpp:
##########
@@ -0,0 +1,416 @@
+// 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.
+
+#include "brpc/adapter_transport.h"
+
+#include <cstdint>
+#include <errno.h>
+#include <unistd.h>
+
+#include "brpc/input_messenger.h"
+#include "brpc/handshake/handshake_adapter.h"
+#include "brpc/tcp_transport.h"
+
+namespace brpc {
+namespace {
+
+class AdapterConnect : public AppConnect {
+public:
+    explicit AdapterConnect(const std::shared_ptr<AppConnect>& app_connect)
+        : _app_connect(app_connect) {}
+
+    static std::shared_ptr<AppConnect> Wrap(
+        const std::shared_ptr<AppConnect>& app_connect) {
+        if (std::dynamic_pointer_cast<AdapterConnect>(app_connect)) {
+            return app_connect;
+        }
+        return std::make_shared<AdapterConnect>(app_connect);
+    }
+
+    static std::shared_ptr<AppConnect> Unwrap(
+        const std::shared_ptr<AppConnect>& app_connect) {
+        const std::shared_ptr<AdapterConnect> adapter =
+            std::dynamic_pointer_cast<AdapterConnect>(app_connect);
+        return adapter ? adapter->_app_connect : app_connect;
+    }
+
+    void StartConnect(const Socket* socket,
+                      void (*done)(int, void*), void* data) override {
+        ApplicationConnectTask* task = new ApplicationConnectTask{
+            socket, _app_connect, done, data};
+        if (AdapterTransport::StartClientUpgrade(
+                socket, OnUpgradeComplete, task) != 0) {
+            const int error = errno != 0 ? errno : EAGAIN;
+            AdapterTransport::Get(const_cast<Socket*>(socket))
+                ->handshake_session()->MarkFailed();
+            delete task;
+            done(error, data);
+        }
+    }
+
+    void StopConnect(Socket* socket) override {
+        if (_app_connect) {
+            _app_connect->StopConnect(socket);
+        }
+    }
+
+private:
+    struct ApplicationConnectTask {
+        const Socket* socket;
+        std::shared_ptr<AppConnect> app_connect;
+        void (*done)(int, void*);
+        void* data;
+    };
+
+    static void OnApplicationComplete(int error, void* arg) {
+        std::unique_ptr<ApplicationConnectTask> task(
+            static_cast<ApplicationConnectTask*>(arg));
+        task->done(error, task->data);
+    }
+
+    static void OnUpgradeComplete(int error, void* arg) {
+        ApplicationConnectTask* task =
+            static_cast<ApplicationConnectTask*>(arg);
+        if (error != 0 || !task->app_connect) {
+            std::unique_ptr<ApplicationConnectTask> owned(task);
+            task->done(error, task->data);
+            return;
+        }
+        task->app_connect->StartConnect(
+            task->socket, OnApplicationComplete, task);
+    }
+
+    std::shared_ptr<AppConnect> _app_connect;
+};
+
+struct ClientHandshakeTask {
+    AdapterTransport* adapter;
+    void (*done)(int, void*);
+    void* data;
+    SocketUniquePtr socket;
+};
+
+}  // namespace
+
+AdapterTransport::AdapterTransport(
+    const handshake::HandshakeRegistration::Upgrade* upgrade)
+    : _upgrade(upgrade) {}
+AdapterTransport::~AdapterTransport() = default;
+
+AdapterTransport* AdapterTransport::Get(Socket* socket) {
+    CHECK(socket != nullptr);
+    return static_cast<AdapterTransport*>(socket->_transport.get());
+}
+
+const AdapterTransport* AdapterTransport::Get(const Socket* socket) {
+    CHECK(socket != nullptr);
+    return static_cast<const AdapterTransport*>(socket->_transport.get());
+}
+
+bool AdapterTransport::upgrade_capable(SocketMode mode) const {
+    return _socket->socket_mode() == mode && _high_speed_transport &&
+           _upgrade->ready(_high_speed_transport.get());
+}
+
+int AdapterTransport::StartClientUpgrade(const Socket* socket,
+                                         void (*done)(int, void*),
+                                         void* data) {
+    return StartClientUpgradeImpl(socket, done, data, 
bthread_start_background);
+}
+
+int AdapterTransport::StartClientUpgradeImpl(
+    const Socket* socket, void (*done)(int, void*), void* data,
+    StartBackground start_background) {
+    AdapterTransport* adapter = Get(const_cast<Socket*>(socket));
+    ClientHandshakeTask* task = new ClientHandshakeTask{adapter, done, data, 
SocketUniquePtr()};
+    if (Socket::Address(socket->id(), &task->socket) != 0) {
+        delete task;
+        errno = EFAILEDSOCKET;
+        return -1;
+    }
+    bthread_t tid;
+    bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
+    bthread_attr_set_name(&attr, "StartClientUpgrade");
+    const int rc = start_background(&tid, &attr, ProcessClientHandshake, task);
+    if (rc != 0) {
+        delete task;
+        errno = rc;
+        return -1;
+    }
+    return 0;
+}
+
+ParseResult AdapterTransport::ProcessUpgradeReadable(butil::IOBuf* source) {
+    if (_handshake.phase(butil::memory_order_acquire) ==
+        handshake::ESTABLISHED) {
+        return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    ParseResult result(PARSE_ERROR_NOT_ENOUGH_DATA);
+    if (_socket->parsing_context() != nullptr) {
+        handshake::ServerHandshakeContext* context =
+            static_cast<handshake::ServerHandshakeContext*>(
+                _socket->parsing_context());
+        CHECK(context->adapter() != nullptr);
+        result = context->adapter()->ExecuteServerHandshake(source, _socket);
+    } else if (!source->empty()) {
+        auto* handler = handshake::HandshakeRegistration::MatchHello(*source);
+        result = handler ? handler->ExecuteServerHandshake(source, _socket)
+                         : MakeParseError(PARSE_ERROR_TRY_OTHERS);
+    }
+    return result;
+}
+
+void* AdapterTransport::ProcessClientHandshake(void* arg) {
+    std::unique_ptr<ClientHandshakeTask> task(
+        static_cast<ClientHandshakeTask*>(arg));
+    AdapterTransport* adapter = task->adapter;
+    Socket* socket = task->socket.get();
+    if (!adapter->_upgrade) {
+        socket->SetFailed(EPROTO, "Unsupported client transport handshake");
+        adapter->_handshake.MarkFailed();
+        task->done(EPROTO, task->data);
+        return nullptr;
+    }
+    if (!adapter->_upgrade->available()) {
+        adapter->FallbackToTcp();
+        task->done(0, task->data);
+        return nullptr;
+    }
+
+    int error = adapter->_upgrade->run_client(
+        adapter->_high_speed_transport.get(), socket, adapter->_handshake);

Review Comment:
   Other threads can still call `SetFailed` while the task holds a 
`SocketUniquePtr`. If the task then takes a fallback branch for unavailability 
or insufficient resources, it reports success directly and may even start the 
application `AppConnect` on a failed connection.



##########
example/ubring_performance/client.cpp:
##########
@@ -130,107 +135,79 @@ class PerformanceTest {
             }
             LOG(WARNING) << "RPC call failed, retrying... (" << retry << " 
left): " << cntl.ErrorText();
             retry--;
-            bthread_usleep(1000000); // 100ms delay before retry
+            bthread_usleep(1000000);
         }
         LOG(ERROR) << "RPC call failed after multiple retries";
         return -1;
     }
 
-    struct RespClosure {
-        brpc::Controller* cntl;
-        test::PerfTestResponse* resp;
-        PerformanceTest* test;
-    };
-
-    void SendRequest() {
-        if (FLAGS_expected_qps > 0) {
-            while (g_token.load(butil::memory_order_relaxed) <= 0) {
-                bthread_usleep(10);
+    static void* RunTest(void* arg) {
+        auto* self = static_cast<PerformanceTest*>(arg);
+        const int64_t deadline = butil::monotonic_time_ns() +
+            static_cast<int64_t>(FLAGS_test_seconds) * 1000000000L;
+        uint64_t sent = 0;
+        test::PerfTestService_Stub stub(self->_channel);
+        while (!g_failed.load() &&
+               (FLAGS_test_iterations == 0 ||
+                sent < static_cast<uint64_t>(FLAGS_test_iterations))) {
+            std::vector<brpc::Controller> cntls(FLAGS_queue_depth);
+            std::vector<test::PerfTestRequest> requests(FLAGS_queue_depth);
+            std::vector<test::PerfTestResponse> responses(FLAGS_queue_depth);
+            int pending = 0;
+            for (; pending < FLAGS_queue_depth; ++pending) {
+                if ((FLAGS_test_iterations > 0 &&
+                     sent >= static_cast<uint64_t>(FLAGS_test_iterations)) ||
+                    !WaitForRequest(deadline)) {
+                    break;
+                }
+                requests[pending].set_echo_attachment(self->_echo_attachment);
+                cntls[pending].request_attachment().append(self->_attachment);
+                stub.Test(&cntls[pending], &requests[pending],
+                          &responses[pending], brpc::DoNothing());
+                ++sent;
             }
-            g_token.fetch_sub(1, butil::memory_order_relaxed);
-        }
-        RespClosure* closure = new RespClosure;
-        test::PerfTestRequest request;
-        closure->resp = new test::PerfTestResponse();
-        closure->cntl = new brpc::Controller();
-        request.set_echo_attachment(_echo_attachment);
-        closure->cntl->request_attachment().append(_attachment);
-        closure->test = this;
-        google::protobuf::Closure* done = brpc::NewCallback(&HandleResponse, 
closure);
-        test::PerfTestService_Stub stub(_channel);
-        stub.Test(closure->cntl, &request, closure->resp, done);
-    }
-
-    static void HandleResponse(RespClosure* closure) {
-        std::unique_ptr<brpc::Controller> cntl_guard(closure->cntl);
-        std::unique_ptr<test::PerfTestResponse> response_guard(closure->resp);
-        if (closure->cntl->Failed()) {
-            LOG(ERROR) << "RPC call failed: " << closure->cntl->ErrorText();
-            // RPCs in this example are expected to succeed. Silently ignoring
-            // failures would hide problems and invalidate the performance 
result.
-            closure->test->_stop = true;
-            return;
-        }
-
-        g_latency_recorder << closure->cntl->latency_us();
-        if (closure->resp->cpu_usage().size() > 0) {
-            g_server_cpu_recorder << atof(closure->resp->cpu_usage().c_str()) 
* 100;
-        }
-        g_total_bytes.fetch_add(closure->cntl->request_attachment().size(), 
butil::memory_order_relaxed);
-        g_total_cnt.fetch_add(1, butil::memory_order_relaxed);
-
-        cntl_guard.reset(nullptr);
-        response_guard.reset(nullptr);
-
-        if (closure->test->_iterations == 0 && FLAGS_test_iterations > 0) {
-            closure->test->_stop = true;
-            return;
-        }
-        --closure->test->_iterations;
-        uint64_t last = g_last_time.load(butil::memory_order_relaxed);
-        uint64_t now = butil::gettimeofday_us();
-        if (now > last && now - last > 100000) {
-            if (g_last_time.exchange(now, butil::memory_order_relaxed) == 
last) {
-                g_client_cpu_recorder << 
-                    
atof(bvar::Variable::describe_exposed("process_cpu_usage").c_str()) * 100;
+            for (int i = 0; i < pending; ++i) {
+                brpc::Join(cntls[i].call_id());

Review Comment:
   With `queue_depth=16`, as long as one slow request remains in a batch, the 
15 completed slots cannot be refilled. This changes the concurrent load 
generated by the benchmark.



##########
src/brpc/rdma/rq_connection.cpp:
##########
@@ -1114,6 +1113,30 @@ void RqConnection::GlobalRelease() {
 
 std::vector<RqConnection::PollerGroup> RqConnection::_poller_groups;
 
+// Thread creation is replaceable in startup failure tests.
+decltype(&bthread_start_background) StartPollingThread = 
bthread_start_background;
+
+void RqConnection::PollerGroup::Stop(std::unique_lock<bthread::Mutex>& lock) {
+    if (stopping) {
+        stopped.wait(lock, [this] { return !stopping; });
+        return;
+    }

Review Comment:
   When an external thread joins the poller in `Stop`, the poller still needs 
to execute the user's `release_fn`. If that callback calls the stop interface 
for the same tag again, it will wait here for the outer join to finish, and 
neither side can return.



##########
src/brpc/handshake/handshake_io.cpp:
##########
@@ -0,0 +1,157 @@
+// 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.
+
+#include "brpc/handshake/handshake_io.h"
+
+#include <algorithm>
+#include <cstdint>
+#include <errno.h>
+#include <unistd.h>
+
+#include "bthread/butex.h"
+#include "butil/time.h"
+#include "brpc/errno.pb.h"
+#include "brpc/socket.h"
+
+namespace brpc {
+namespace handshake {
+
+static const int WAIT_TIMEOUT_MS = 50;
+
+SocketHandshakeIO::SocketHandshakeIO(Socket* socket)
+    : _socket(socket)
+    , _read_butex(bthread::butex_create_checked<butil::atomic<int> >()) {
+    _read_butex->store(0, butil::memory_order_relaxed);

Review Comment:
   Default TCP sockets also construct `SocketHandshakeIO`, so each connection 
permanently holds a cache-line-sized `Butex`. Regular clients do not wait for 
an upgrade, and the server parses directly from `IOBuf`.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to