-
Notifications
You must be signed in to change notification settings - Fork 8.9k
feature: implement early rollback of global transactions when TM disconnects #7674
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
Open
kssumin
wants to merge
1
commit into
apache:2.x
Choose a base branch
from
kssumin:feature/early-rollback-tm-disconnect-4422
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
core/src/main/java/org/apache/seata/core/rpc/TMDisconnectHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * 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.core.rpc; | ||
|
|
||
| /** | ||
| * Handler for TM disconnection events | ||
| * to enable early rollback of global transactions | ||
| */ | ||
| public interface TMDisconnectHandler { | ||
|
|
||
| /** | ||
| * Handle TM disconnection and perform early rollback if enabled | ||
| * | ||
| * @param rpcContext the rpc context of the disconnected TM | ||
| */ | ||
| void handleTMDisconnect(RpcContext rpcContext); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...est/java/org/apache/seata/core/rpc/netty/AbstractNettyRemotingServerTMDisconnectTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.core.rpc.netty; | ||
|
|
||
| import org.apache.seata.core.rpc.TMDisconnectHandler; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.MockitoAnnotations; | ||
|
|
||
| import java.util.concurrent.LinkedBlockingDeque; | ||
| import java.util.concurrent.ThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| /** | ||
| * Test for TM disconnect handling in AbstractNettyRemotingServer | ||
| */ | ||
| public class AbstractNettyRemotingServerTMDisconnectTest { | ||
|
|
||
| private NettyRemotingServer server; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| MockitoAnnotations.openMocks(this); | ||
|
|
||
| // Use real NettyRemotingServer like other tests | ||
| ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>()); | ||
| server = new NettyRemotingServer(executor); | ||
| } | ||
|
|
||
| @Test | ||
| void testTMDisconnectHandlerSetup() { | ||
| // Test that the TM disconnect handler can be set and retrieved | ||
| TMDisconnectHandler testHandler = mock(TMDisconnectHandler.class); | ||
|
|
||
| // Verify no exceptions are thrown when setting the handler | ||
| assertDoesNotThrow(() -> server.setTmDisconnectHandler(testHandler)); | ||
| assertDoesNotThrow(() -> server.setTmDisconnectHandler(null)); | ||
| assertDoesNotThrow(() -> server.setTmDisconnectHandler(testHandler)); | ||
| } | ||
|
|
||
| @Test | ||
| void testTMDisconnectHandlerIntegration() { | ||
| // Test that handler is properly integrated in the AbstractNettyRemotingServer | ||
| // This verifies the new method exists and works | ||
| assertDoesNotThrow(() -> { | ||
| TMDisconnectHandler handler = mock(TMDisconnectHandler.class); | ||
| server.setTmDisconnectHandler(handler); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
server/src/main/java/org/apache/seata/server/coordinator/DefaultTMDisconnectHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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() { | ||||||
|
||||||
| public SessionManager getSessionManager() { | |
| SessionManager getSessionManager() { |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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={}'