Copilot commented on code in PR #7674: URL: https://github.com/apache/incubator-seata/pull/7674#discussion_r2415664837
########## 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() { Review Comment: This method is exposed solely for testing but exists in production code. Consider using package-private visibility instead of public, or use dependency injection to make the code more testable. ```suggestion SessionManager getSessionManager() { ``` ########## 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 [{}] {} {}", Review Comment: The error message format is unclear with three consecutive placeholders without descriptive text. Consider a more descriptive format like 'Failed to rollback transaction [{}]: code={}, message={}' ```suggestion "Failed to rollback transaction [{}]: code={}, message={}", ``` -- 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]
