-
Notifications
You must be signed in to change notification settings - Fork 8.9k
optimize: raft mode performs transaction size check in advance #7344
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 2.x #7344 +/- ##
============================================
+ Coverage 54.90% 54.92% +0.02%
- Complexity 7399 7415 +16
============================================
Files 1181 1181
Lines 42106 42136 +30
Branches 4935 4945 +10
============================================
+ Hits 23119 23144 +25
+ Misses 16818 16813 -5
- Partials 2169 2179 +10
🚀 New features to boost your workflow:
|
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.
Pull Request Overview
This PR adds pre-emptive size checks for global and branch sessions in Raft mode to prevent oversized transactions, and updates related tests and session manager calls.
- Introduces
checkSizemethods inGlobalSessionandBranchSessionto validate payload sizes. - Calls
checkSizeinRaftSessionManagerbefore Raft operations (onBegin,onAddBranch, etc.). - Adds unit tests verifying that sessions exceeding size limits throw
TransactionException.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java | Added checkSizeTest for GlobalSession. |
| server/src/test/java/org/apache/seata/server/session/BranchSessionTest.java | Added checkSizeTest for BranchSession and updated codecTest signature. |
| server/src/main/java/org/apache/seata/server/storage/raft/session/RaftSessionManager.java | Imported and invoked checkSize, refactored exception construction. |
| server/src/main/java/org/apache/seata/server/session/GlobalSession.java | Refactored encode to use checkSize, implemented new checkSize and helper methods. |
| server/src/main/java/org/apache/seata/server/session/BranchSession.java | Refactored encode to use checkSize, implemented new checkSize and helper methods. |
| changes/zh-cn/2.x.md | Updated changelog entry for size-check feature. |
| changes/en-us/2.x.md | Updated changelog entry for size-check feature. |
Comments suppressed due to low confidence (2)
server/src/main/java/org/apache/seata/server/session/GlobalSession.java:726
- [nitpick] Method name 'calGlobalSessionSize' is abbreviated; consider renaming to 'calculateGlobalSessionSize' for better readability.
private int calGlobalSessionSize(byte[] byApplicationIdBytes, byte[] byServiceGroupBytes, byte[] byTxNameBytes, byte[] xidBytes, byte[] applicationDataBytes) {
server/src/main/java/org/apache/seata/server/session/BranchSession.java:449
- [nitpick] Method name 'calBranchSessionSize' could be more descriptive as 'calculateBranchSessionSize'.
private int calBranchSessionSize(byte[] resourceIdBytes, byte[] lockKeyBytes, byte[] clientIdBytes, byte[] applicationDataBytes, byte[] xidBytes) {
server/src/main/java/org/apache/seata/server/session/BranchSession.java
Outdated
Show resolved
Hide resolved
server/src/main/java/org/apache/seata/server/storage/raft/session/RaftSessionManager.java
Outdated
Show resolved
Hide resolved
…sion.java Co-authored-by: Copilot <[email protected]>
…ion/RaftSessionManager.java Co-authored-by: Copilot <[email protected]>
YongGoose
left a comment
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.
Left few comments!
Please take a look :)
server/src/main/java/org/apache/seata/server/session/BranchSession.java
Outdated
Show resolved
Hide resolved
server/src/test/java/org/apache/seata/server/session/GlobalSessionTest.java
Outdated
Show resolved
Hide resolved
…sionTest.java Co-authored-by: Yongjun Hong <[email protected]>
server/src/main/java/org/apache/seata/server/session/BranchSession.java
Outdated
Show resolved
Hide resolved
| if (!RaftServerManager.isRaftMode()) { | ||
| checkSize(resourceIdBytes, lockKeyBytes, clientIdBytes, applicationDataBytes, xidBytes); |
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.
I have one question.
The title of the PR says that the size check for transactions is performed in advance in Raft mode, but the relevant code seems to perform the check in advance even in modes other than Raft.
If I’ve misunderstood something, I apologize in advance :)
YongGoose
left a comment
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.
I’ve written one question above!
Also, I think it would be great to add a unit test to verify that the checkSize method is called when in Raft mode.
If that unit test is added, I believe a full integration test may not be strictly necessary.
In "file" mode, because GlobalSession and BranchSession instances need to be written to disk, their encode method is inevitably called when these instances are added. At this stage, the transaction size is validated. Conversely, "db" and "redis" modes do not require writing transaction information to disk, so the encode method is not called at all; consequently, these modes do not perform a transaction size check. In "raft" mode, however, transaction information is stored entirely in memory. It's only when a Raft snapshot needs to be created that these in-memory transactions are flushed to disk. If the encode method is executed at this point, it's too late. This can lead to snapshot creation failure because if a transaction in memory exceeds the maximum size limit, a runtime exception is thrown, causing the snapshot process to fail. Therefore, for "raft" mode, the transaction size check is performed belatedly. The purpose of this pull request (PR) is to implement this check at an earlier stage to prevent the aforementioned issue. |
It's not possible to construct multiple Raft nodes to form a Raft cluster within unit tests. Therefore, I believe that calling the checkSize method in RaftSessionManager is not fundamentally different from directly calling the checkSize method of GlobalSession and BranchSession, unless we use integration tests instead of unit tests. |
Oh, I see !! |
YongGoose
left a comment
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.
LGTM 👍🏻
Integration tests can be added here or in a separate PR—both options are fine :)
I think it would be good to proceed with the integration test in the following way. While working locally to create a PR for your branch in order to help you, I got stuck on the part where the RaftServerManager is initialized. 😭 Have a good night! |
Thank you for your assistance. However, I believe that while the mock approach is meaningful for testing and verification in a single-instance deployment, it may not fully and accurately validate the process of adding transactions in Raft mode, which is a cluster-based setup. |
Ⅰ. Describe what this PR did
Ⅱ. Does this pull request fix one issue?
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews