funky-eyes commented on code in PR #7344:
URL: https://github.com/apache/incubator-seata/pull/7344#discussion_r2094531438


##########
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:
   > I’ve got to say, this is a nice feature of Copilot. 👍🏻😁
   
   You're right



-- 
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