Skip to content

perf: use weakly consistent snapshots in selective channel - #3650

Open
songgaoye wants to merge 5 commits into
apache:masterfrom
songgaoye:selective-channel-health-weak-snapshot
Open

songgaoye wants to merge 5 commits into
apache:masterfrom
songgaoye:selective-channel-health-weak-snapshot

Conversation

@songgaoye

@songgaoye songgaoye commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

SelectiveChannel::CheckHealth() and verbose Describe() invoke sub-channel
methods while holding the channel map mutex. Slow sub-channel operations can
therefore delay concurrent add and removal operations.

What is changed and the side effects?

Changed:

  • Use a weakly consistent snapshot for OWNS_CHANNEL registrations, resolving
    and accessing them after releasing the channel map mutex.
  • Keep synchronization for DOESNT_OWN_CHANNEL registrations so that removal
    cannot return and allow the caller to destroy an externally owned channel
    while it is being accessed.

Side effects:

  • CheckHealth() and verbose Describe() may observe a weakly consistent view
    of owning registrations during concurrent add or removal operations.

  • Non-owning registrations retain the previous synchronization behavior to
    preserve their lifetime contract.

  • Performance effects:

    • Reduces mutex contention and improves tail latency for concurrent add and
      removal operations when health checks or verbose descriptions of owning
      sub-channels are slow.
    • No lock-granularity improvement applies to non-owning registrations.
  • Breaking backward compatibility:

    • None.

Check List:

@songgaoye
songgaoye marked this pull request as draft October 23, 2026 12:36
@songgaoye
songgaoye force-pushed the selective-channel-health-weak-snapshot branch from 5add899 to bbeee76 Compare October 23, 2026 12:45
@songgaoye
songgaoye marked this pull request as ready for review October 23, 2026 12:45
@songgaoye
songgaoye force-pushed the selective-channel-health-weak-snapshot branch from bbeee76 to a79bd30 Compare October 23, 2026 12:52
@chenBright
chenBright requested a balanced review from Copilot October 23, 2026 16:24

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

5 open issues
What changed in this PR

This PR reduces lock contention in SelectiveChannel by snapshotting sub-channel SocketIds under ChannelBalancer::_mutex, then releasing the lock before running potentially slow CheckHealth() / verbose Describe() on each sub-channel. It also adds a regression test to ensure verbose Describe() doesn’t block AddChannel().

Changes:

  • Snapshot sub-channel SocketIds while holding ChannelBalancer::_mutex, then resolve/access sockets after unlocking.
  • Update CheckHealth() and verbose Describe() to use Socket::AddressFailedAsWell() + SocketUniquePtr for safe access.
  • Add a unit test ensuring verbose Describe() doesn’t block AddChannel().
File Description
test/​brpc_channel_unittest.cpp Adds a regression test with a blocking sub-channel Describe() to verify AddChannel() is not blocked by verbose Describe().
src/​brpc/​selective_channel.cpp Reworks CheckHealth() / verbose Describe() to avoid holding _mutex while invoking sub-channel operations by using snapshot-based iteration.

Review effort: Lite

Comment thread src/brpc/selective_channel.cpp
Comment thread src/brpc/selective_channel.cpp Outdated
Comment thread src/brpc/selective_channel.cpp Outdated
Comment thread test/brpc_channel_unittest.cpp Outdated
Comment thread test/brpc_channel_unittest.cpp Outdated
@songgaoye
songgaoye force-pushed the selective-channel-health-weak-snapshot branch 2 times, most recently from ac390a3 to edc0e2b Compare October 23, 2026 17:15
@songgaoye
songgaoye force-pushed the selective-channel-health-weak-snapshot branch from c6c1a56 to 461fdc9 Compare October 24, 2026 00:55
@chenBright
chenBright requested a balanced review from Copilot October 24, 2026 06:03

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.

Comment thread src/brpc/selective_channel.cpp

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

The independently changed health-check locking and lifetime behavior needs blocking concurrency regression tests.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/brpc/selective_channel.cpp
@wwbmmm

wwbmmm commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

I don't think this is necessary.

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.

3 participants