-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
optimize: distinguish database behavior according to the branch type #2415
Changes from 36 commits
92a1e4b
7829ce7
f4e4078
ef1ea3c
65312dd
d3f1483
9bbdd1f
8fb6dc2
0596108
52335c8
57e7dfd
06e79ea
d0b3128
e721fb1
f6a1cb6
03d9a95
a3837de
6030169
fd12c62
c50587f
97510c0
657c45e
3d9a032
5afdfc2
d5dd5cd
3a0613b
4785c86
640477d
bd8b7eb
9979b00
95ddbe2
7666bc1
127b3e8
d7d249b
69f29b7
7e86dd3
f5c438e
7d41df6
970e86f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,10 @@ | |
import com.alibaba.dubbo.rpc.Result; | ||
import com.alibaba.dubbo.rpc.RpcContext; | ||
import com.alibaba.dubbo.rpc.RpcException; | ||
import io.seata.common.util.StringUtils; | ||
import io.seata.core.context.RootContext; | ||
import io.seata.core.constants.DubboConstants; | ||
import io.seata.core.model.BranchType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -43,45 +45,51 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept | |
return invoker.invoke(invocation); | ||
} | ||
String xid = RootContext.getXID(); | ||
String xidInterceptorType = RootContext.getXIDInterceptorType(); | ||
String branchType = RootContext.getBranchType(); | ||
|
||
String rpcXid = getRpcXid(); | ||
String rpcXidInterceptorType = RpcContext.getContext().getAttachment(RootContext.KEY_XID_INTERCEPTOR_TYPE); | ||
String rpcBranchType = RpcContext.getContext().getAttachment(RootContext.KEY_BRANCH_TYPE); | ||
if (LOGGER.isDebugEnabled()) { | ||
LOGGER.debug("xid in RootContext[{}] xid in RpcContext[{}]", xid, rpcXid); | ||
} | ||
boolean bind = false; | ||
if (xid != null) { | ||
RpcContext.getContext().setAttachment(RootContext.KEY_XID, xid); | ||
RpcContext.getContext().setAttachment(RootContext.KEY_XID_INTERCEPTOR_TYPE, xidInterceptorType); | ||
RpcContext.getContext().setAttachment(RootContext.KEY_BRANCH_TYPE, branchType); | ||
} else { | ||
if (rpcXid != null) { | ||
RootContext.bind(rpcXid); | ||
RootContext.bindInterceptorType(rpcXidInterceptorType); | ||
if (StringUtils.equals(BranchType.TCC.name(), rpcBranchType)) { | ||
RootContext.bindBranchType(BranchType.TCC); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Saga branch never set ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have discussed with the author of saga. This pr is only responsible for the branchType judgment logic in ExecuteTemplate and the TCC branchType passing in Filter. The specific Saga branchtype assignment and other logic will be provided by the feature saga related pr There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this #2551, autor is wangliang1986 |
||
} | ||
bind = true; | ||
if (LOGGER.isDebugEnabled()) { | ||
LOGGER.debug("bind[{}] interceptorType[{}] to RootContext", rpcXid, rpcXidInterceptorType); | ||
LOGGER.debug("bind xid [{}] branchType [{}] to RootContext", rpcXid, rpcBranchType); | ||
} | ||
} | ||
} | ||
try { | ||
return invoker.invoke(invocation); | ||
|
||
} finally { | ||
if (bind) { | ||
String unbindInterceptorType = RootContext.unbindInterceptorType(); | ||
String unbindXid = RootContext.unbind(); | ||
String previousBranchType = RootContext.getBranchType(); | ||
if (StringUtils.equals(BranchType.TCC.name(), previousBranchType)) { | ||
RootContext.unbindBranchType(); | ||
} | ||
if (LOGGER.isDebugEnabled()) { | ||
LOGGER.debug("unbind[{}] interceptorType[{}] from RootContext", unbindXid, unbindInterceptorType); | ||
LOGGER.debug("unbind xid [{}] branchType [{}] from RootContext", unbindXid, previousBranchType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BranchType print null in AT mode is not very elegant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about judge in the log output temporarily,and print "AT" if it is null |
||
} | ||
if (!rpcXid.equalsIgnoreCase(unbindXid)) { | ||
LOGGER.warn("xid in change during RPC from {} to {}, xidInterceptorType from {} to {} ", rpcXid, | ||
unbindXid, rpcXidInterceptorType, unbindInterceptorType); | ||
LOGGER.warn("xid in change during RPC from {} to {},branchType from {} to {}", rpcXid, unbindXid, | ||
rpcBranchType, previousBranchType); | ||
if (unbindXid != null) { | ||
RootContext.bind(unbindXid); | ||
RootContext.bindInterceptorType(unbindInterceptorType); | ||
LOGGER.warn("bind [{}] interceptorType[{}] back to RootContext", unbindXid, | ||
unbindInterceptorType); | ||
LOGGER.warn("bind xid [{}] back to RootContext", unbindXid); | ||
if (StringUtils.equals(BranchType.TCC.name(), previousBranchType)) { | ||
RootContext.bindBranchType(BranchType.TCC); | ||
LOGGER.warn("bind branchType [{}] back to RootContext", previousBranchType); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
*/ | ||
package io.seata.rm.datasource.exec; | ||
|
||
import io.seata.common.util.StringUtils; | ||
import io.seata.common.util.CollectionUtils; | ||
import io.seata.core.context.RootContext; | ||
import io.seata.core.model.BranchType; | ||
import io.seata.rm.datasource.StatementProxy; | ||
import io.seata.rm.datasource.sql.SQLVisitorFactory; | ||
import io.seata.sqlparser.SQLRecognizer; | ||
|
@@ -54,7 +56,7 @@ public static <T, S extends Statement> T execute(StatementProxy<S> statementProx | |
* | ||
* @param <T> the type parameter | ||
* @param <S> the type parameter | ||
* @param sqlRecognizer the sql recognizer | ||
* @param sqlRecognizers the sql recognizer list | ||
* @param statementProxy the statement proxy | ||
* @param statementCallback the statement callback | ||
* @param args the args | ||
|
@@ -66,7 +68,7 @@ public static <T, S extends Statement> T execute(List<SQLRecognizer> sqlRecogniz | |
StatementCallback<T, S> statementCallback, | ||
Object... args) throws SQLException { | ||
|
||
if (!RootContext.inGlobalTransaction() && !RootContext.requireGlobalLock()) { | ||
if (!requiresLockOrUndoFunction()) { | ||
// Just work as original statement | ||
return statementCallback.execute(statementProxy.getTargetStatement(), args); | ||
} | ||
|
@@ -115,4 +117,17 @@ public static <T, S extends Statement> T execute(List<SQLRecognizer> sqlRecogniz | |
} | ||
return rs; | ||
} | ||
|
||
private static boolean requiresLockOrUndoFunction() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other more meaningful method name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change it to "shouldExecuteInATMode" |
||
if (!RootContext.inGlobalTransaction() && !RootContext.requireGlobalLock()) { | ||
return false; | ||
} | ||
if (RootContext.inGlobalTransaction()) { | ||
String branchType = RootContext.getBranchType(); | ||
if (StringUtils.equals(BranchType.TCC.name(), branchType) || StringUtils.equals(BranchType.SAGA.name(), branchType)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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.
Is it better to separate and judge?
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.
It will not be processed this time, and transaction context object encapsulation will be provided later to handle log output uniformly.