-
Notifications
You must be signed in to change notification settings - Fork 1.2k
support custom UserThreadPool for interface method #1500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
support custom UserThreadPool for interface method #1500
Conversation
WalkthroughThis update introduces method-level user thread pool management in Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant BoltServer
participant BoltServerProcessor
participant UserThreadPoolSelector
participant UserThreadPoolManager
Client->>BoltServer: Send request (with service & method)
BoltServer->>BoltServerProcessor: Process request
BoltServerProcessor->>UserThreadPoolSelector: select(requestClass, requestHeader)
UserThreadPoolSelector->>UserThreadPoolManager: getUserThread(service, method)
alt Thread pool found
UserThreadPoolManager-->>UserThreadPoolSelector: Return UserThreadPool
UserThreadPoolSelector-->>BoltServerProcessor: Return user executor
else Not found
UserThreadPoolSelector-->>BoltServerProcessor: Return default executor
end
BoltServerProcessor-->>BoltServer: Execute request with selected executor
BoltServer-->>Client: Return response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~18 minutes Suggested labels
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 6
🧹 Nitpick comments (4)
remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/server/bolt/BoltServer.java (1)
327-333
: Consider the implications of exposing internal stateWhile this getter enables external configuration of the processor's executor selector, exposing the internal
BoltServerProcessor
instance breaks encapsulation and could lead to thread-safety issues if the processor is modified after server initialization.Consider alternatives such as:
- Adding a method to set the executor selector directly on
BoltServer
- Making the processor immutable or returning a defensive copy
- Documenting thread-safety guarantees
core/api/src/test/java/com/alipay/sofa/rpc/config/UserThreadPoolManagerTest.java (1)
59-63
: Enhance test coverage and organizationWhile the basic functionality is tested, consider:
- Creating a separate test method for method-level thread pool testing
- Adding edge case tests (null parameters, empty strings, concurrent access)
- Testing the interaction between service-level and method-level pools
+@Test +public void testMethodLevelUserThreadPool() { + // Test basic functionality + UserThreadPool methodUserThreadPool = new UserThreadPool(); + UserThreadPoolManager.registerUserThread("service1", "method1", methodUserThreadPool); + Assert.assertEquals(methodUserThreadPool, UserThreadPoolManager.getUserThread("service1", "method1")); + + // Test that service-level pool doesn't interfere + Assert.assertNull(UserThreadPoolManager.getUserThread("service1")); + + // Test unregistration + UserThreadPoolManager.unRegisterUserThread("service1", "method1"); + Assert.assertNull(UserThreadPoolManager.getUserThread("service1", "method1")); + + // Test edge cases + UserThreadPoolManager.registerUserThread("", "method", new UserThreadPool()); + Assert.assertNotNull(UserThreadPoolManager.getUserThread("", "method")); +}core/api/src/main/java/com/alipay/sofa/rpc/config/UserThreadPoolManager.java (1)
69-74
: Consider optimizing synchronization strategyUsing
synchronized
on all methods creates a potential bottleneck. SinceConcurrentHashMap
is already thread-safe, consider:
- Using double-checked locking for initialization
- Removing synchronization from read operations
- Using
ConcurrentHashMap.computeIfAbsent()
for atomic operations-public static synchronized void registerUserThread(String service, String methodName, UserThreadPool userThreadPool) { +public static void registerUserThread(String service, String methodName, UserThreadPool userThreadPool) { if (userThreadMap == null) { - userThreadMap = new ConcurrentHashMap<String, UserThreadPool>(); + synchronized (UserThreadPoolManager.class) { + if (userThreadMap == null) { + userThreadMap = new ConcurrentHashMap<String, UserThreadPool>(); + } + } } userThreadMap.put(service + SPLIT + methodName, userThreadPool); }Also applies to: 93-97
remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/server/bolt/UserThreadPoolSelectorTest.java (1)
84-98
: Consider adding type safety to the header casting.The helper method correctly demonstrates custom executor selector configuration. However, the unchecked cast on line 87 could be made more robust.
Consider this improvement for better type safety:
boltServerProcessor.setExecutorSelector((requestClass, requestHeader) -> { - Map<String, String> headerMap = (Map<String, String>) requestHeader; + if (!(requestHeader instanceof Map)) { + return boltServer.getBizExecutor(); + } + @SuppressWarnings("unchecked") + Map<String, String> headerMap = (Map<String, String>) requestHeader; if (headerMap.containsKey("customThreadPool")) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
core/api/src/main/java/com/alipay/sofa/rpc/config/UserThreadPoolManager.java
(4 hunks)core/api/src/test/java/com/alipay/sofa/rpc/config/UserThreadPoolManagerTest.java
(1 hunks)remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/server/bolt/BoltServer.java
(1 hunks)remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/server/bolt/BoltServerProcessor.java
(1 hunks)remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/server/bolt/UserThreadPoolSelector.java
(1 hunks)remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/server/bolt/UserThreadPoolSelectorTest.java
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: build
- GitHub Check: sca
🔇 Additional comments (6)
remoting/remoting-bolt/src/main/java/com/alipay/sofa/rpc/server/bolt/BoltServerProcessor.java (1)
91-91
: LGTM!The instantiation of the new
UserThreadPoolSelector
class with the default executor is correct and follows the intended design.remoting/remoting-bolt/src/test/java/com/alipay/sofa/rpc/server/bolt/UserThreadPoolSelectorTest.java (5)
1-35
: LGTM! Clean imports and standard license header.The package structure and imports are well-organized and include all necessary dependencies for testing the UserThreadPoolSelector functionality.
48-55
: LGTM! Setup correctly demonstrates interface and method-level thread pool registration.The beforeClass method properly initializes both UserThreadPool instances and registers them at different granularities (interface-level and method-level), which aligns perfectly with the PR objectives for supporting custom UserThreadPool for interface methods.
57-69
: Excellent test coverage of the UserThreadPoolSelector logic.This test method comprehensively validates the thread pool selection hierarchy:
- Method-level selection takes precedence
- Falls back to interface-level when method is unregistered
- Falls back to default executor when service is unregistered
The test scenarios effectively demonstrate the new functionality introduced by this PR.
71-82
: LGTM! Good validation of custom executor selector functionality.This test effectively demonstrates the extensibility of the thread pool selection mechanism, showing how users can implement custom selection logic based on their own header keys. This aligns well with the PR's goal of providing flexible thread pool management.
100-107
: LGTM! Clean teardown of thread pool registrations.The cleanup method properly unregisters the thread pools and clears static references, ensuring no memory leaks from the thread pool manager. The cleanup matches the setup performed in beforeClass().
support custom UserThreadPool for interface method
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Refactor