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


##########
server/src/main/java/org/apache/seata/server/coordinator/DefaultTMDisconnectHandler.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.server.coordinator;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.common.DefaultValues;
+import org.apache.seata.config.ConfigurationFactory;
+import org.apache.seata.core.exception.TransactionException;
+import org.apache.seata.core.model.GlobalStatus;
+import org.apache.seata.core.rpc.RpcContext;
+import org.apache.seata.core.rpc.TMDisconnectHandler;
+import org.apache.seata.server.session.GlobalSession;
+import org.apache.seata.server.session.SessionHolder;
+import org.apache.seata.server.session.SessionManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Objects;
+
+/**
+ * Default implementation of TMDisconnectHandler.
+ * Handles TM disconnection and performs early rollback of global transactions
+ * when enabled via configuration.
+ */
+public class DefaultTMDisconnectHandler implements TMDisconnectHandler {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultTMDisconnectHandler.class);
+
+    private final Core core;
+
+    /**
+     * Constructor with core dependency
+     *
+     * @param core the transaction core
+     */
+    public DefaultTMDisconnectHandler(Core core) {
+        this.core = core;
+    }
+
+    @Override
+    public void handleTMDisconnect(RpcContext rpcContext) {
+        if (rpcContext == null || rpcContext.getTransactionServiceGroup() == 
null) {
+            return;
+        }
+
+        // Check if early rollback is enabled
+        boolean enableRollbackWhenDisconnect = 
ConfigurationFactory.getInstance()
+                .getBoolean(
+                        ConfigurationKeys.ENABLE_ROLLBACK_WHEN_DISCONNECT,
+                        DefaultValues.DEFAULT_ENABLE_ROLLBACK_WHEN_DISCONNECT);
+
+        if (!enableRollbackWhenDisconnect) {
+            LOGGER.debug("Early rollback on TM disconnect is disabled");
+            return;
+        }
+
+        String transactionServiceGroup = 
rpcContext.getTransactionServiceGroup();
+        String applicationId = rpcContext.getApplicationId();
+
+        LOGGER.info(
+                "TM disconnected: transactionServiceGroup={}, 
applicationId={}, performing early rollback check",
+                transactionServiceGroup,
+                applicationId);
+
+        try {
+            // Find all global sessions in BEGIN status
+            Collection<GlobalSession> globalSessions =
+                    SessionHolder.getRootSessionManager().allSessions();
+
+            int rollbackCount = 0;
+            for (GlobalSession globalSession : globalSessions) {
+                if (shouldRollbackSession(globalSession, rpcContext)) {
+                    try {
+                        LOGGER.info(
+                                "Early rollback for TM disconnect: xid={}, 
transactionServiceGroup={}, applicationId={}",
+                                globalSession.getXid(),
+                                globalSession.getTransactionServiceGroup(),
+                                globalSession.getApplicationId());
+
+                        // Change status to TimeoutRollbacking
+                        
globalSession.changeGlobalStatus(GlobalStatus.TimeoutRollbacking);
+
+                        // Perform rollback
+                        core.doGlobalRollback(globalSession, false);
+                        rollbackCount++;
+
+                    } catch (TransactionException e) {
+                        LOGGER.error(
+                                "Failed to rollback transaction [{}] {} {}",
+                                globalSession.getXid(),
+                                e.getCode(),
+                                e.getMessage());
+                    }
+                }
+            }
+
+            if (rollbackCount > 0) {
+                LOGGER.info(
+                        "Early rollback completed for TM disconnect: 
transactionServiceGroup={}, rollbackCount={}",
+                        transactionServiceGroup,
+                        rollbackCount);
+            }
+
+        } catch (Exception e) {
+            LOGGER.error(
+                    "Error during TM disconnect handling for 
transactionServiceGroup={}", transactionServiceGroup, e);
+        }
+    }
+
+    /**
+     * Get the session manager instance. Made public for testing purposes.
+     *
+     * @return the session manager
+     */
+    public SessionManager getSessionManager() {
+        return SessionHolder.getRootSessionManager();
+    }

Review Comment:
   The `getSessionManager()` method appears to be unused. The comment states 
it's "Made public for testing purposes," but it's not used in any test files 
(`DefaultTMDisconnectHandlerTest.java` or `TMDisconnectIntegrationTest.java`). 
The method simply delegates to `SessionHolder.getRootSessionManager()`, which 
tests can call directly. Consider removing this method to reduce unnecessary 
public API surface.
   ```suggestion
   
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to