Skip to content
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

feature:Transfer global timeout and branch transaction begin time #7164

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions core/src/main/java/org/apache/seata/core/context/RootContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ private RootContext() {
* The constant KEY_TIMEOUT.
*/
public static final String KEY_TIMEOUT = "TX_TIMEOUT";

/**
* The constant KEY_BRANCH_BEIGN_TIME.
*/
public static final String KEY_BRANCH_BEIGN_TIME = "TX_BRANCH_BEIGN_TIME";

/**
* The constant MDC_KEY_XID for logback
Expand Down Expand Up @@ -141,6 +146,14 @@ public static Integer getTimeout() {
public static void setTimeout(Integer timeout) {
CONTEXT_HOLDER.put(KEY_TIMEOUT,timeout);
}

public static Long getBranchBeignTime() {
return (Long) CONTEXT_HOLDER.get(KEY_BRANCH_BEIGN_TIME);
}

public static void setBranchBeignTime(Long branchBeignTime) {
CONTEXT_HOLDER.put(KEY_BRANCH_BEIGN_TIME,branchBeignTime);
}

/**
* declare local transactions will use global lock check for update/delete/insert/selectForUpdate SQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.seata.common.lock.ResourceLock;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.model.BranchStatus;
import org.apache.seata.core.model.BranchType;
Expand Down Expand Up @@ -62,11 +63,11 @@ public class ConnectionProxyXA extends AbstractConnectionProxyXA implements Hold

private volatile boolean rollBacked = false;

private volatile Long branchRegisterTime = null;
private volatile Long branchBeginTime = null;

private volatile Long prepareTime = null;

private static final Integer TIMEOUT = Math.max(BRANCH_EXECUTION_TIMEOUT, DefaultValues.DEFAULT_GLOBAL_TRANSACTION_TIMEOUT);
private volatile Integer timeout = null;

private boolean shouldBeHeld = false;

Expand All @@ -93,6 +94,15 @@ public void init() {
if (!currentAutoCommitStatus) {
throw new IllegalStateException("Connection[autocommit=false] as default is NOT supported");
}
Integer transactionTimeout = RootContext.getTimeout();
if (transactionTimeout == null) {
transactionTimeout = DefaultValues.DEFAULT_GLOBAL_TRANSACTION_TIMEOUT;
}
timeout = Math.max(BRANCH_EXECUTION_TIMEOUT, transactionTimeout);
branchBeginTime = RootContext.getBranchBeignTime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this branchBeginTime value being passed from the downstream service, and if it's not passed, it's no different from the previous function. Or what you're trying to say is timeout is the timeout of a branch transaction not the timeout of a global transaction?

if(branchBeginTime == null) {
branchBeginTime = System.currentTimeMillis();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -190,7 +200,6 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
long branchId;
try {
// 1. register branch to TC then get the branch message
branchRegisterTime = System.currentTimeMillis();
branchId = DefaultResourceManager.get().branchRegister(BranchType.XA, resource.getResourceId(), null, xid, null,
null);
} catch (TransactionException te) {
Expand Down Expand Up @@ -318,7 +327,6 @@ private synchronized void end(int flags) throws XAException, SQLException {

private void cleanXABranchContext() {
xaEnded = false;
branchRegisterTime = null;
prepareTime = null;
xaActive = false;
if (!isHeld()) {
Expand All @@ -327,7 +335,7 @@ private void cleanXABranchContext() {
}

private void checkTimeout(Long now) throws XAException {
if (now - branchRegisterTime > TIMEOUT) {
if (now - branchBeginTime > timeout) {
xaRollback(xaBranchXid);
throw new XAException("XA branch timeout error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public void begin(int timeout, String name) throws TransactionException {
xid = transactionManager.begin(null, null, name, timeout);
status = GlobalStatus.Begin;
RootContext.bind(xid);
RootContext.setTimeout(timeout);
RootContext.setBranchBeignTime(createTime);
Comment on lines +116 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integration模块中需要传递两个值
Two values need to be passed in the integration module

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I set this value here I think it is ambiguous, the time here should be the start time of the global transaction and not the begin time of the branch transaction.

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Begin new global transaction [{}]", xid);
}
Expand Down
Loading