Skip to content

feat: add URMA unified connection resource framework and exclusive backend - #3788

Open
CarbonadoR wants to merge 4 commits into
apache:masterfrom
CarbonadoR:pr1-urma-connection-framework
Open

CarbonadoR wants to merge 4 commits into
apache:masterfrom
CarbonadoR:pr1-urma-connection-framework

Conversation

@CarbonadoR

@CarbonadoR CarbonadoR commented Dec 2, 2026 •

Copy link
Copy Markdown

What problem does this PR solve?

Issue Number: Part of #3778, related to #3707

Problem Summary:
Currently UrmaEndpoint couples TCP handshake/fallback and InputMessenger stream integration directly with per-connection URMA Jetty/JFR/JFC/JFCE lifecycle, send/recv ring buffers, credit windows, and CQ polling. This PR implements PR 1 of the staged plan in #3778: introducing an internal UrmaConnection / UrmaConnectionProvider boundary under src/brpc/urma/ and migrating the existing per-connection exclusive implementation into UrmaExclusiveConnection / UrmaExclusiveConnectionProvider, preparing for follow-up shared JFS/JFR pooled backend PRs (#3707 / #3778) without duplicating UrmaEndpoint or changing existing exclusive behavior.

What is changed and the side effects?

Changed:

  1. Unified internal connection resource interfaces (src/brpc/urma/urma_connection.{h,cpp}):
    • Introduces UrmaConnection, UrmaConnectionProvider, UrmaConnectionCallbacks, UrmaConnectionOptions, and UrmaConnectionDescription.
    • Adds startup-only --urma_resource_mode flag (default "exclusive"; "pooled" currently returns ENOTSUP until follow-up PRs).
  2. Exclusive backend migration (src/brpc/urma/urma_exclusive_connection.{h,cpp}):
    • Moves UrmaResource (as UrmaExclusiveResources), _sbuf/_rbuf, SQ/RQ credit windows, CQ event/polling handlers, and UrmaPollerGroup out of UrmaEndpoint.
    • Preserves existing exclusive v2/v3 handshake wire protocol (urma_handshake.* unchanged), event/polling/bonding behavior, and zero-copy IOBuf ownership.
  3. Endpoint / Transport / Helper adaptation (src/brpc/urma/urma_endpoint.{h,cpp}, src/brpc/urma_transport.cpp, src/brpc/urma/urma_helper.cpp, src/brpc/socket.h):
    • UrmaEndpoint no longer inherits SocketUser; it holds a stable UrmaConnectionHandle, validates callback source identity (OnReceivedBytes / OnConnectionError), caches early RX bytes until ESTABLISHED, preserves the original internal InputMessageClosure last_msg scope when on_input_ready receives nullptr, and delegates WaitWritable from UrmaTransport::WaitEpollOut.
    • GlobalRelease now waits for UrmaEndpoint::GlobalRelease() and verifies outstanding registered pool blocks before unregistering memory and destroying the URMA context.
  4. Tests (src/brpc/urma/mock_urma.cpp, test/brpc_urma_unittest.cpp):
    • Adds deterministic link-time mock fault-injection hooks and unit tests covering partial prefix sends, writable waits, Close/Shutdown ordering, early RX caching & stale-callback rejection, exclusive last_msg == nullptr contract, and v2/v3 multi-connection RPCs.
  5. Documentation (docs/cn/urma.md, docs/en/urma.md):
    • Updates the URMA architecture overview to reflect UrmaConnection / UrmaExclusiveConnection and documents --urma_resource_mode.

Side effects:

  • Performance effects:
    • No regression compared with master in exclusive mode. Verified on two openEuler 24.03 aarch64 URMA nodes ( URMA_CTP, 14-point matrix covering QPS 1000/500 and payload sizes 1 KiB–8 MiB, 0% error rate): 1 KiB avg/P99 28/37 us vs master 28/38 us; 8 MiB 4-run interleaved A/B mean 1230.4 MB/s vs master 1234.0 MB/s (-0.29%, within run-to-run variance).
  • Breaking backward compatibility:
    • None. Exclusive mode remains the default with unchanged v2/v3 wire format; refactored and unrefactored peers interoperate in both directions, and TCP fallback behavior is preserved.

Verification

  • -DWITH_URMA=OFF: brpc-static builds cleanly.
  • -DWITH_URMA=ON -DURMA_USE_MOCK=ON: brpc_urma_unittest passes in event mode (42/42, 2 polling/multi-tag skipped) and with --urma_use_polling=true --task_group_ntags=2 (44/44, plus --gtest_repeat=20 on shutdown/delete fault tests).
  • -DWITH_URMA=ON -DURMA_USE_MOCK=OFF (real liburma.so):
    • brpc_urma_unittest: 13 passed, 31 mock-only skipped, 0 failed.
    • Cross-node ub_test: event mode, polling mode, 8-connection concurrency, bonding_dev_0, TCP fallback, bidirectional PR 1 ↔ master interop, and 14-point matrix (1 KiB–8 MiB) all passed with 0 errors.

Check List:

CarbonadoRain added 4 commits November 28, 2026 11:20
Introduce the internal UrmaConnection / UrmaConnectionProvider interfaces under src/brpc/urma/ together with the per-connection exclusive backend (UrmaExclusiveConnection and UrmaExclusiveConnectionProvider).

This commit is purely additive (aside from friend declarations in socket.h) and leaves existing UrmaEndpoint / UrmaTransport behavior untouched so the subsequent migration diff stays focused on existing classes.

Ref: apache#3778
Switch UrmaEndpoint, UrmaTransport, and urma_helper to the UrmaConnection / UrmaConnectionProvider boundary:
- Remove per-connection Jetty/JFR/JFC resources, ring buffers, credit windows, CQ polling, and SocketUser inheritance from UrmaEndpoint.
- Delegate UrmaTransport::WaitEpollOut to UrmaEndpoint::WaitWritable and allocate per-socket UrmaConnect instances for cloned sockets.
- Preserve existing v2/v3 handshake wire format, internal InputMessageClosure last_msg scope, and safe GlobalRelease teardown order.
- Hand TCP fallback reads back to the socket event owner via Socket::OnInputEvent.

Ref: apache#3778
…fecycle

Extend the URMA link-time mock with deterministic fault injection and add unit tests covering UrmaConnection prefix sends, writable waits, early RX caching, stale-callback filtering, exclusive last_msg=nullptr behavior, TCP fallback handoff, Close/Shutdown ordering, and v2/v3 multi-connection RPCs.

Ref: apache#3778
Sync docs/cn/urma.md and docs/en/urma.md with the UrmaConnection / UrmaExclusiveConnection architecture split and document the startup-only --urma_resource_mode flag.

Ref: apache#3778

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Three critical concurrency and ownership bugs remain in connection publication, TCP fallback callback handling, and carrier cleanup.

3 open issues
What changed in this PR

Refactors URMA around a unified connection/provider abstraction while preserving the exclusive backend and existing handshake, polling, fallback, and data-flow behavior.

Changes:

  • Adds connection/provider interfaces and startup resource-mode selection.
  • Migrates exclusive resource management into a dedicated backend.
  • Updates endpoint integration, tests, and documentation.
File Description
test/​brpc_urma_unittest.cpp Adds URMA lifecycle, callback, fault-injection, and multi-connection tests.
src/​brpc/​urma/​urma_helper.cpp Integrates provider initialization and global resource release.
src/​brpc/​urma/​urma_exclusive_connection.h Declares the exclusive URMA connection backend interfaces.
src/​brpc/​urma/​urma_exclusive_connection.cpp Implements exclusive resources, polling, flow control, and connection lifecycle.
src/​brpc/​urma/​urma_endpoint.h Updates endpoint state and connection ownership declarations.
src/​brpc/​urma/​urma_endpoint.cpp Integrates endpoint handshake, callbacks, connection delegation, and TCP fallback.
src/​brpc/​urma/​urma_connection.h Defines unified URMA connection, provider, callback, and option interfaces.
src/​brpc/​urma/​urma_connection.cpp Implements provider selection and connection lifecycle behavior.
src/​brpc/​urma/​mock_urma.cpp Adds deterministic mock fault-injection hooks for URMA tests.
src/​brpc/​urma_transport.cpp Adapts transport connection creation and writable-wait delegation.
src/​brpc/​socket.h Adds URMA connection friendship declarations.
docs/​en/​urma.md Documents the revised URMA architecture and resource-mode flag.
docs/​cn/​urma.md Documents the revised URMA architecture and resource-mode flag in Chinese.

Review effort: Lite

Comment on lines +378 to +381
UrmaConnectionHandle UrmaEndpoint::PinConnection() const {
// An acquire load makes the immutable owner visible. Normal RX/TX uses
// BorrowConnection to avoid touching the shared reference count.
return BorrowConnection() ? _connection : UrmaConnectionHandle();
Comment on lines +460 to +462
if (ep && expected_connection &&
expected_connection.get() == ep->BorrowConnection()) {
socket->SetFailed(error, "URMA connection failure");
Comment on lines +372 to +375
if (Socket::Create(options, &_cq_sid) < 0) {
PLOG(ERROR) << "Fail to create CQ socket";
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants