Skip to content

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

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

CarbonadoR wants to merge 7 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 thread src/brpc/urma/urma_endpoint.cpp Resolved
Comment thread src/brpc/urma/urma_endpoint.cpp Resolved
… lookup

- Synchronize UrmaEndpoint::PinConnection() under mutable _dispatch_mutex.
- Reject racing OnConnectionError() callbacks once the endpoint has closed
  or transitioned to FALLBACK_TCP / FAILED, and add a regression unit test.
- Create the CQ carrier socket before attaching Carrier and JFCE fd so a
  Socket::Create() failure cannot double-delete Carrier via BeforeRecycle().
- Pin the owning Socket once per DrainCq() invocation and reuse it for
  WakeAsEpollOut() and DeliverReceivedBytes() to avoid per-CQE refcount
  overhead under high CQE rates.

Ref: apache#3778, apache#3707

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

Active critical issues remain: jetty deletion can race sends causing use-after-free, local SGE errors can incorrectly fail healthy connections, and consumed IOBuf data may be restored incorrectly; a test also leaks global flag state.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Low severity Restore the handshake version flag after the test

test/​brpc_urma_unittest.cpp:1219

This test changes urma::FLAGS_urma_client_handshake_version for both iterations and leaves it at 3, so later tests (or reruns with a different order/filter) inherit state from this test. The surrounding handshake tests use GFLAGS_NAMESPACE::FlagSaver for exactly this reason (see lines 214-215). Add a FlagSaver at the start of the test to restore the flag on every exit.

3 resolved since last review

Review effort: Lite

…and defer CQ socket lookup

- Cut staged WR prefixes forward and prepend in reverse (with adjacent-slice coalescing) so repeated input IOBuf pointers restore in original order.
- Skip ReportError() on local SGE construction failures (ERDMAMEM) and already-closed sends, keeping ReportError() for provider send errors.
- Resolve the owner Socket lazily in DrainCq() so idle polling cycles do not transiently pin a Socket or incur atomic refcount traffic.
- Add FlagSaver in multiconnection_rpc_preserves_v2_v3_and_credit_progress, assert zero connection errors on local ERDMAMEM, and trigger on_error via mock send failure in callbacks_can_close_their_own_connection.

Ref: apache#3778, apache#3707

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.

🔵 Needs a closer look

A critical lifetime race remains: teardown can unregister and unmap the pool while a concurrent allocation still acquires it, leaving an IOBuf pointing to invalid memory.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

High severity Synchronize pool allocation with teardown before unmapping

src/​brpc/​urma/​urma_helper.cpp:423

This snapshot is not a safe lifetime barrier for the pool allocator. g_urma_available is set false, but PoolAllocate() does not consult it, and neither allocation nor deallocation is synchronized with this check. A concurrent allocation can observe g_pool->outstanding == 0 just before incrementing it, after which this code unregisters g_pool_seg and unmaps the pool while the new IOBuf still points into it. Gate new pool allocations and synchronize the allocator with teardown (or otherwise hold a lifecycle lock) before unregistering/unmapping.

2 resolved since last review

Review effort: Lite

- Call UrmaEndpoint::GlobalInitialize() before installing global IOBuf allocator hooks so init rollback in GlobalRelease() cannot race with live pool allocations.
- Gate PoolAllocate() with BufferPool::accepting (re-checked under each shard lock) and acquire all shard locks in GlobalRelease() before checking outstanding blocks and unregistering/unmapping the pool.

Ref: apache#3778, apache#3707
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