feature:Support server-side load balancing for TC-to-RM reverse communication#8102
Open
YvCeung wants to merge 8 commits into
Open
feature:Support server-side load balancing for TC-to-RM reverse communication#8102YvCeung wants to merge 8 commits into
YvCeung wants to merge 8 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an opt-in, server-side load-balancing mechanism for TC→RM reverse calls (branch commit/rollback) so the TC can distribute requests across multiple active RM connections instead of always picking the first available channel.
Changes:
- Introduces a
ServerLoadBalanceSPI and factory with configurable strategies for AT/TCC (Random,RoundRobin,LeastActive), with XA/SAGA excluded by design. - Refactors
ChannelManagerchannel selection to optionally apply server-side LB and updates TC→RM sync request path to pass transaction context. - Adds
RpcContext.activeCounttracking (for least-active) and unit tests validating factory behavior and strategy selection.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| server/src/main/java/org/apache/seata/server/coordinator/DefaultCoordinator.java | Uses AT-aware RM channel selection for undo-log delete routing. |
| server/src/main/java/org/apache/seata/server/coordinator/AbstractCore.java | Passes xid/branchType into the remoting sync request path for server-side LB eligibility. |
| core/src/main/java/org/apache/seata/core/rpc/RpcContext.java | Adds activeCount to support least-active strategy selection. |
| core/src/main/java/org/apache/seata/core/rpc/RemotingServer.java | Adds an overload (default method) for sending sync requests with transaction context. |
| core/src/main/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServer.java | Overrides the new sync request overload and updates activeCount around sync calls. |
| core/src/main/java/org/apache/seata/core/rpc/netty/ChannelManager.java | Adds LB-aware channel selection and a branchType-aware getRmChannels. |
| core/src/main/java/org/apache/seata/core/rpc/netty/loadbalance/ServerLoadBalance.java | New SPI interface for server-side channel selection. |
| core/src/main/java/org/apache/seata/core/rpc/netty/loadbalance/ServerLoadBalanceFactory.java | Loads configured LB implementations for AT/TCC via SPI; falls back when unset/invalid. |
| core/src/main/java/org/apache/seata/core/rpc/netty/loadbalance/ServerRandomLoadBalance.java | Random strategy implementation. |
| core/src/main/java/org/apache/seata/core/rpc/netty/loadbalance/ServerRoundRobinLoadBalance.java | Round-robin strategy implementation. |
| core/src/main/java/org/apache/seata/core/rpc/netty/loadbalance/ServerLeastActiveLoadBalance.java | Least-active strategy implementation using RpcContext.activeCount. |
| core/src/main/resources/META-INF/services/org.apache.seata.core.rpc.netty.loadbalance.ServerLoadBalance | Registers the new LB SPI implementations. |
| core/src/test/java/org/apache/seata/core/rpc/netty/loadbalance/ServerLoadBalanceFactoryTest.java | Adds unit tests for factory/SPI behavior. |
| core/src/test/java/org/apache/seata/core/rpc/netty/loadbalance/ServerLoadBalanceBehaviorTest.java | Adds behavior tests for the three LB strategies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+43
to
+48
| @Test | ||
| public void testAtNotConfiguredReturnsNull() { | ||
| // When no type is configured, should return null (use original logic) | ||
| ServerLoadBalance loadBalance = ServerLoadBalanceFactory.getInstance(BranchType.AT); | ||
| Assertions.assertNull(loadBalance); | ||
| } |
Comment on lines
+77
to
+82
| @Test | ||
| public void testSpiLoadInvalidTypeThrowsException() { | ||
| // Loading a non-existent LB type should throw EnhancedServiceNotFoundException | ||
| Assertions.assertThrows( | ||
| Exception.class, () -> EnhancedServiceLoader.load(ServerLoadBalance.class, "NonExistentLoadBalance")); | ||
| } |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 2.x #8102 +/- ##
============================================
- Coverage 72.92% 72.57% -0.36%
+ Complexity 883 879 -4
============================================
Files 1327 1332 +5
Lines 50769 50895 +126
Branches 6058 6091 +33
============================================
- Hits 37025 36935 -90
- Misses 10735 10957 +222
+ Partials 3009 3003 -6
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ⅰ. Describe what this PR did
This PR introduces server-side load balancing for TC-to-RM reverse communication (branch commit/rollback), addressing the issue where
ChannelManager.getChannel()always selects the first available channel with no distribution strategy, causing requests to concentrate on specific RM instances.Core changes:
New
ServerLoadBalanceSPI interfaceThree LB strategy implementations:
ServerRandomLoadBalanceServerRoundRobinLoadBalanceServerLeastActiveLoadBalanceSimplified configuration system (
ServerLoadBalanceFactory): Only two configuration keys:server.loadBalance.at.type— LB algorithm for AT modeserver.loadBalance.tcc.type— LB algorithm for TCC modeIf not configured or configured as blank, the original priority-based channel selection logic is used (fully backward compatible). If explicitly configured, the specified LB algorithm is loaded via SPI.
XA and SAGA explicitly excluded: XA's second-phase operations are bound to the local database connection of the original RM; SAGA's state machine execution context is held in memory with no distributed lock protection. Both modes always use the original priority-based channel selection.
ChannelManagerrefactoring:getChannel()collects all active candidate channels and applies the LB algorithm when configured;getRmChannels()applies LB per resourceId. The originalgetChannelByPriority()logic is preserved as the default fallback path.Ⅱ. Does this pull request fix one issue?
fix #7758
Ⅲ. Why don't you add test cases (unit test/integration test)?
Unit tests are included:
ServerLoadBalanceFactoryTest— verifies that XA/SAGA return null, unconfigured AT/TCC return null, SPI loading works for all three strategies, and invalid type names are handled gracefullyServerLoadBalanceBehaviorTest— verifies randomness forRandomLoadBalance, even distribution forRoundRobinLoadBalance, least-active selection forLeastActiveLoadBalance, and single-candidate edge caseⅣ. Describe how to verify it
Ⅴ. Special notes for reviews
getChannelByPriority()is used. Zero risk for existing deployments.LoadBalanceinterface fromdiscoverymodule:LoadBalanceoperates onInetSocketAddressand lives in thediscoverymodule.ChannelManageris incore. Makingcoredepend ondiscoverywould create a circular dependency. The server-side context (RpcContextwith channel, applicationId, activeCount) is fundamentally different from the client-side context.