Copilot commented on code in PR #7344:
URL: https://github.com/apache/incubator-seata/pull/7344#discussion_r2094436308


##########
server/src/main/java/org/apache/seata/server/session/BranchSession.java:
##########
@@ -405,31 +383,92 @@ public byte[] encode() {
 
         byteBuffer.put(branchTypeByte);
 
-        byteBuffer.put((byte)status.getCode());
-        byteBuffer.put((byte)lockStatus.getCode());
+        byteBuffer.put((byte) status.getCode());
+        byteBuffer.put((byte) lockStatus.getCode());
         BufferUtils.flip(byteBuffer);
         byte[] result = new byte[byteBuffer.limit()];
         byteBuffer.get(result);
         return result;
     }
 
-    private int calBranchSessionSize(byte[] resourceIdBytes, byte[] 
lockKeyBytes, byte[] clientIdBytes,
-                                     byte[] applicationDataBytes, byte[] 
xidBytes) {
-        final int size = 8 // trascationId
-            + 8 // branchId
-            + 4 // resourceIdBytes.length
-            + 4 // lockKeyBytes.length
-            + 2 // clientIdBytes.length
-            + 4 // applicationDataBytes.length
-            + 4 // xidBytes.size
-            + 1 // statusCode
-            + (resourceIdBytes == null ? 0 : resourceIdBytes.length)
-            + (lockKeyBytes == null ? 0 : lockKeyBytes.length)
-            + (clientIdBytes == null ? 0 : clientIdBytes.length)
-            + (applicationDataBytes == null ? 0 : applicationDataBytes.length)
-            + (xidBytes == null ? 0 : xidBytes.length)
-            + 1; //branchType
-        return size;
+    public void checkSize() throws TransactionException {
+        byte[] resourceIdBytes = resourceId != null ? resourceId.getBytes() : 
null;
+
+        byte[] lockKeyBytes = lockKey != null ? lockKey.getBytes() : null;
+
+        byte[] clientIdBytes = clientId != null ? clientId.getBytes() : null;
+
+        byte[] applicationDataBytes = applicationData != null ? 
applicationData.getBytes() : null;
+
+        byte[] xidBytes = xid != null ? xid.getBytes() : null;
+
+        try {
+            checkSize(resourceIdBytes, lockKeyBytes, clientIdBytes, 
applicationDataBytes, xidBytes);
+        } catch (RuntimeException e) {
+            throw new TransactionException(e.getMessage(), e);
+        }
+    }
+
+    private void checkSize(
+            byte[] resourceIdBytes,
+            byte[] lockKeyBytes,
+            byte[] clientIdBytes,
+            byte[] applicationDataBytes,
+            byte[] xidBytes) {
+
+        int size = calBranchSessionSize(resourceIdBytes, lockKeyBytes, 
clientIdBytes, applicationDataBytes, xidBytes);
+
+        if (size > MAX_BRANCH_SESSION_SIZE) {
+            if (lockKeyBytes == null) {
+                throw new RuntimeException("branch session size exceeded, size 
: " + size + " maxBranchSessionSize : "
+                        + MAX_BRANCH_SESSION_SIZE);
+            }
+            // try compress lockkey
+            try {
+                size -= lockKeyBytes.length;
+                lockKeyBytes = CompressUtil.compress(lockKeyBytes);
+            } catch (IOException e) {
+                LOGGER.error("compress lockKey error", e);
+            } finally {
+                size += lockKeyBytes.length;
+            }
+
+            if (size > MAX_BRANCH_SESSION_SIZE) {
+                throw new RuntimeException("compress branch session size 
exceeded, compressSize : " + size
+                        + " maxBranchSessionSize : " + 
MAX_BRANCH_SESSION_SIZE);
+            }
+        }
+    }
+
+    private int calBranchSessionSize(
+            byte[] resourceIdBytes,
+            byte[] lockKeyBytes,
+            byte[] clientIdBytes,
+            byte[] applicationDataBytes,
+            byte[] xidBytes) {
+        // trascationId

Review Comment:
   Typo in comment: 'trascationId' should be 'transactionId'.
   ```suggestion
           // transactionId
   ```



##########
server/src/main/java/org/apache/seata/server/session/GlobalSession.java:
##########
@@ -682,29 +677,65 @@ public byte[] encode() {
             byteBuffer.putInt(0);
         }
         byteBuffer.putLong(beginTime);
-        byteBuffer.put((byte)status.getCode());
+        byteBuffer.put((byte) status.getCode());
         BufferUtils.flip(byteBuffer);
         byte[] result = new byte[byteBuffer.limit()];
         byteBuffer.get(result);
         return result;
     }
 
-    private int calGlobalSessionSize(byte[] byApplicationIdBytes, byte[] 
byServiceGroupBytes, byte[] byTxNameBytes,
-        byte[] xidBytes, byte[] applicationDataBytes) {
+    public void checkSize() throws TransactionException {

Review Comment:
   [nitpick] There's duplicated byte-array size calculation logic in 
GlobalSession and BranchSession; consider extracting a shared utility method to 
reduce duplication.



##########
server/src/main/java/org/apache/seata/server/storage/raft/session/RaftSessionManager.java:
##########
@@ -100,7 +101,8 @@ public void removeGlobalSession(GlobalSession session) 
throws TransactionExcepti
         GlobalSession globalSession = sessionMap.remove(session.getXid());
         if (globalSession != null) {
             List<BranchSession> branchSessionList = 
globalSession.getBranchSessions();
-            // For the follower, the following code will not be executed 
because when the follower receives the remove global session
+            // For the follower, the following code will not be executed 
because when the follower receives the remove
+            // global session
             // the branch session on the leader side has already been 
completely cleared.

Review Comment:
   [nitpick] This comment is split across lines awkwardly; consider merging 
into a single coherent sentence for clarity.
   ```suggestion
               // For the follower, this code will not execute because, by the 
time the follower receives the remove global session request, the branch 
sessions on the leader side have already been completely cleared.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to