Repository navigation
[RIPâ70] Optimizing Lock Mechanisms
lizhimins edited this page Feb 12, 2025
·
1 revision
- Current State: Draft
- Authors: HuaiYuan Wang
- Shepherds: RongTong Jin
- Mailing List discussion: dev@rocketmq.apache.org
- Pull Request: https://github.com/apache/rocketmq/pull/8663
- Released: no
- Design flexible lock optimization strategies and optimize message sending and receiving logic to improve message sending and processing performance.
- As concurrent systems grow more complex internally, deploying effective lock management strategies is key to preserving performance. The adoption of locks in the concurrent code of RocketMQ may have room for optimization. For instance, the current usage of locks, while critical for ensuring consistency and preventing race conditions, could potentially be refined to improve overall message throughput without significantly impacting performance. In practice, we have demonstrated that adjusting the lock strategy can impact the message-sending performance of RocketMQ. Merely altering the backoff strategy of SpinLock can result in a performance difference of 20% (or even more) between the best and worst cases.
- éçĺšśĺçłťçťĺ é¨ĺĺžčśćĽčśĺ¤ćďźé¨ç˝˛ććçé玥çççĽćŻäżćć§č˝çĺ łéŽăRocketMQ ĺšśĺ䝣ç ä¸éçéç¨ĺŻč˝čżćäźĺ犺é´ăäžĺŚďźĺ˝ĺéç使ç¨č˝çśĺŻšäşçĄŽäżä¸č´ć§ĺé˛ć˘çŤäşćĄäťśčłĺ łéčŚďźä˝ĺŻäťĽčżčĄćščżäťĽćéŤć´ä˝ćśćŻĺĺéďźčä¸äźćžç役ĺć§č˝ăĺ¨ĺŽčˇľä¸ďźć䝏塲çťčŻćč°ć´éççĽäźĺ˝ąĺ RocketMQ çćśćŻĺéć§č˝ăäť äť ćšĺ SpinLock çééżççĽĺ°ąäźĺŻźč´ć佳ć ĺľĺćĺć ĺľäšé´çć§č˝ĺˇŽĺźčžžĺ° 20%ďźçčłć´ĺ¤ďźă
-
What problem is this proposal designed to solve?
- Design locking strategies to cope with different concurrent pressures
- Optimize the message sending and receiving logic
- Design an adaptive locking mechanism
- ä¸.莞莥ĺşĺŻšä¸ĺĺšśĺĺĺä¸çéĺŽççĽ
- äş.äźĺćśćŻćśĺéťčž
- ä¸.莞莥čŞéĺşéĺŽćşĺś
-
To what degree should we solve the problem?
- In different lock competition situations, adaptive lock selects the appropriate locking mechanism according to the critical size and competition situation.
- Design the conversion structure between different locking mechanisms to avoid deadlock or lock failure caused by the switching of locking mechanisms.
- Optimize the message sending and receiving logic
- ä¸.ĺ¨ä¸ĺçéçŤäşć ĺľďźčŞéĺşéć šćŽä¸´ç大ĺ°äťĽĺçŤäşć ĺľéĺĺéçéĺŽćşĺś.
- äş.莞莥ä¸ĺéĺŽćşĺśäšé´ç¸äşč˝Źć˘ççťćďźéżĺ é ćĺć˘éĺŽćşĺśčżç¨é ććťéćéĺŽĺ¤ąć.
- ä¸.äźĺćśćŻćśĺéťčž
-
Are there any limits of this proposal?
- The calculation process of the adaptive mechanism may be affected by hardware fluctuations
- In special scenarios, the optimization results may not be ideal. Therefore, you need to provide supporting tools to update related configurations
- ä¸.čŞéĺşćşĺśç莥çŽčżç¨ĺŻč˝ĺ祏䝜波ĺ¨ĺ˝ąĺ
- äş.çšćŽĺşćŻä¸äźĺçťćĺŻč˝ĺšśä¸çćłďźéćäžé ĺĽĺˇĽĺ ˇĺŻšç¸ĺ łé 罎ć´ć°ç
- Since each locking mechanism cannot take into account various competitive scenarios even if it dynamically ADAPTS, the design of the adaptive lock realized by integrating various locking mechanisms will adjust to a suitable locking mechanism when the dynamic adjustment of a locking mechanism reaches the limit and cannot adapt to a certain scenarioă
- çąäşćŻç§éĺŽćşĺśĺłä˝żĺ¨ćéĺşäšä¸č˝ĺ źéĄžĺç§çŤäşĺşćŻďźĺ ć¤čŽžčŽĄçťźĺĺç§éĺŽćşĺśćĺŽç°çčŞéĺşéďźĺ˝ä¸ç§éĺŽćşĺśĺ¨ćč°ć´čžžĺ°ćéäšć ćłéĺşćç§ĺşćŻćśďźčŞéĺşéäžżäźč°ć´ä¸şä¸ç§éĺçéĺŽćşĺś
- As can be seen from the figure, when each thread enters the critical section, the average critical size and lock contention degree are calculated.
- When the current locking mechanism is judged to be unsuitable, the command to change the locking mechanism is issued, and the status is set to 0 to prevent the thread from obtaining the lock.
- The lock status is synchronized, and after resetting the calculation, the status is set to 1 to restore the normal state
- ä¸.äťĺžä¸ĺŻäťĽçĺşďźĺ˝ćŻä¸Şçşżç¨čżĺ Ľä¸´çĺşĺé˝äźčŽĄçŽĺšłĺ临ç大ĺ°ĺéçŤäşç¨ĺşŚ
- äş.ĺ˝ĺ¤ĺŽĺ˝ĺéĺŽćşĺśä¸éĺĺďźĺĺşć´ćšéĺŽćşĺśĺ˝äť¤ďźĺšśĺ°status莞罎为0ďźéťć˘çşżç¨čˇĺé
- ä¸.čżčĄéĺŽçśćĺćĽďźé罎莥çŽĺĺ°status莞罎为1ďźć˘ĺ¤ćŁĺ¸¸çść
- At present, the locking mechanism of spin optimal K-order retreat strategy is preliminarily verified
- Single-machine four-process stress test (message body size 2B):
- çŽĺĺćĽéŞčŻčŞććäźK揥ééżççĽçéĺŽćşĺś
- ĺćşĺčżç¨ĺĺćľčŻ(ćśćŻä˝ĺ¤§ĺ°2B):
| CPU Arch | Flush Policy | Original QPS | k | Optimal QPS | Improvement |
|---|---|---|---|---|---|
| X86 | ASYNC | 176312.35 | 10^3 | 184214.98 | +4.47% |
| X86 | SYNC | 177403.12 | 10^3 | 187215.47 | +5.56% |
| ARM | ASYNC | 185321.49 | 10^3 | 206431.82 | +11.44% |
| ARM | SYNC | 188312.17 | 10^3 | 212314.43 | +12.85% |
- As can be seen from the above figure, QPS changes, and RocketMQ's different locking mechanisms are used without selection guidance, and the performance gap between different locking mechanisms can not be small, so we will also unify the locking mechanism to adaptive locking.
- çąä¸ĺžĺŻçĺ°QPSçĺĺďźĺšśä¸RocketMQçä¸ĺéĺŽćşĺśä˝żç¨äšé´ć éç¨ć察ďźĺćśä¸ĺéĺŽćşĺśäšé´çć§č˝ĺˇŽčˇä¸ĺŽšĺ°ĺďźĺ ć¤ć䝏ĺćśĺ°çťä¸éĺŽćşĺśä¸şčŞéĺşé.
- GSOC-2024ďźOptimize Lock MechanismsçťéĄšćĽĺďź https://shimo.im/docs/e1AzdMJYBjS6xmqW/
- We will implement the proposed changes by 3 phases.
- Optimize the locking logic for message delivery to commitLog
- Spin optimal degree K retreat locking mechanism is introduced
- Optimize the back pressure mechanism of the client
- äźĺćśćŻćéĺ°commitLogçéĺŽéťčž
- ĺćĽĺźĺ ĽčŞććäźćŹĄć°KééżéĺŽćşĺś
- äźĺ厢ćˇçŤŻčĺćşĺś
- https://shimo.im/docs/0l3NMnen8GFGDaAR/
- Adaptive lock is implemented initially
- Introduce other locking mechanisms
- Optimize message receiving logic
- ĺćĽĺŽç°čŞéĺşéďźäźĺééżççĽćşĺś
- ĺźĺ Ľĺ śäťéĺŽćşĺś
- äźĺćśćŻćĽćśéťčž
- https://shimo.im/docs/XKq42a0gdJsJ4ANG/
- Improved adaptive locks
- Provides tools for adjusting the lock mechanism
- Design the necessary tests
- ĺŽĺčŞéĺşé
- ćäžéĺŽćşĺśč°ć´é ĺĽĺˇĽĺ ˇ
- čŽžčŽĄĺż čŚçćľčŻ
-
How does alternatives solve the issue you proposed?
- None
-
Pros and Cons of alternatives?
- None
-
Why should we reject above alternatives?
- None
Copyright Š 2016~2022 The Apache Software Foundation.
- Home
- RocketMQ Improvement Proposal
- User Guide
- Community