大纲
1.Seata XA分布式事务案例及AT与XA的区别
2.Seata XA分布式事务案例的各模块运行流程
3.Seata使用Spring Boot自动装配简化复杂配置
4.全局事务注解扫描组件的自动装配
5.全局事务注解扫描器的核心变量与初始化
6.全局事务注解扫描器创建AOP代理
7.全局事务降级检查开启与提交事务的源码
8.Seata Server故障时全局事务拦截器的降级处理
9.基于HTTP请求头的全局事务传播的源码
10.XA数据源代理的初始化与XA连接代理的获取
11.XA分支事务注册与原始XA连接启动的源码
12.XA分布式事务两阶段提交流程的源码
6.全局事务扫描器创建AOP代理
//The type Global transaction scanner.
//AbstractAutoProxyCreator,自动代理创建组件,继承了它以后,Spring容器里的Bean都会传递给wrapIfNecessary()
//从而让GlobalTransactionScanner.wrapIfNecessary()可以扫描每个Bean的每个方法
//判断是否添加了@GlobalTransactional注解,如果扫描到添加了就对这个Bean创建一个AOP代理
public class GlobalTransactionScanner extends AbstractAutoProxyCreator
implements ConfigurationChangeListener, InitializingBean, ApplicationContextAware, DisposableBean {
...
//The following will be scanned, and added corresponding interceptor:
//由于继承自AbstractAutoProxyCreator抽象类,所以Spring所有的Bean都会传递给这个方法来判断是否有一些Seata的注解
//如果有一些Seata的注解,那么就会针对这些注解创建对应的AOP代理
@Override
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
try {
synchronized (PROXYED_SET) {
if (PROXYED_SET.contains(beanName)) {
return bean;
}
interceptor = null;
//check TCC proxy
if (TCCBeanParserUtils.isTccAutoProxy(bean, beanName, applicationContext)) {
//TCC interceptor, proxy bean of sofa:reference/dubbo:reference, and LocalTCC
interceptor = new TccActionInterceptor(TCCBeanParserUtils.getRemotingDesc(beanName));
ConfigurationCache.addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (ConfigurationChangeListener)interceptor);
} else {
Class<?> serviceInterface = SpringProxyUtils.findTargetClass(bean);
Class<?>[] interfacesIfJdk = SpringProxyUtils.findInterfaces(bean);
if (!existsAnnotation(new Class[]{serviceInterface}) && !existsAnnotation(interfacesIfJdk)) {
return bean;
}
if (globalTransactionalInterceptor == null) {
//构建一个GlobalTransactionalInterceptor,即全局事务注解的拦截器
globalTransactionalInterceptor = new GlobalTransactionalInterceptor(failureHandlerHook);
ConfigurationCache.addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (ConfigurationChangeListener)globalTransactionalInterceptor);
}
interceptor = globalTransactionalInterceptor;
}
LOGGER.info("Bean[{}] with name [{}] would use interceptor [{}]", bean.getClass().getName(), beanName, interceptor.getClass().getName());
if (!AopUtils.isAopProxy(bean)) {
//接下来会基于Spring的AbstractAutoProxyCreator创建针对目标Bean接口的动态代理
//这样后续调用到目标Bean的方法,就会调用到GlobalTransactionInterceptor拦截器
bean = super.wrapIfNecessary(bean, beanName, cacheKey);
} else {
AdvisedSupport advised = SpringProxyUtils.getAdvisedSupport(bean);
Advisor[] advisor = buildAdvisors(beanName, getAdvicesAndAdvisorsForBean(null, null, null));
for (Advisor avr : advisor) {
advised.addAdvisor(0, avr);
}
}
PROXYED_SET.add(beanName);
return bean;
}
} catch (Exception exx) {
throw new RuntimeException(exx);
}
}
...
}
7.全局事务降级检查开启与提交事务的源码
在GlobalTransactionalInterceptor的初始化方法中,如果发现需要启用全局事务降级检查机制,那么就会调用startDegradeCheck()方法启动降级检查定时调度线程。
该定时调度线程默认会每隔2秒运行一次,也就是每隔2秒会尝试到Seata Server开启一个全局事务。如果开启成功,则获取到对应的xid,并对该全局事务xid进行提交,并且发布一个降级检查成功的事件到事件总线中。如果开启失败或提交失败,则发布一个降级检查失败的事件到事件总线中。
//The type Global transactional interceptor.
//当调用添加了全局事务注解@GlobalTransactional的方法时,会被AOP拦截进入到这个拦截器里的invoke()方法
public class GlobalTransactionalInterceptor implements ConfigurationChangeListener, MethodInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTransactionalInterceptor.class);
private static final FailureHandler DEFAULT_FAIL_HANDLER = new DefaultFailureHandlerImpl();
//全局事务执行模板
private final TransactionalTemplate transactionalTemplate = new TransactionalTemplate();
//全局锁模板
private final GlobalLockTemplate globalLockTemplate = new GlobalLockTemplate();
//失败处理器
private final FailureHandler failureHandler;
//是否禁用全局事务
private volatile boolean disable;
//降级检查周期
//降级机制:如果Seata Server挂掉导致全局事务没法推进,那么就可以进行降级运行本地事务
private static int degradeCheckPeriod;
//是否启用降级检查机制
private static volatile boolean degradeCheck;
//降级检查允许次数
private static int degradeCheckAllowTimes;
//降级数量
private static volatile Integer degradeNum = 0;
//达标数量
private static volatile Integer reachNum = 0;
//降级检查的事件总线
private static final EventBus EVENT_BUS = new GuavaEventBus("degradeCheckEventBus", true);
//降级检查定时调度线程池
private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("degradeCheckWorker", 1, true));
//region DEFAULT_GLOBAL_TRANSACTION_TIMEOUT
//默认的全局事务超时时间
private static int defaultGlobalTransactionTimeout = 0;
//endregion
//Instantiates a new Global transactional interceptor.
public GlobalTransactionalInterceptor(FailureHandler failureHandler) {
//失败处理器
this.failureHandler = failureHandler == null ? DEFAULT_FAIL_HANDLER : failureHandler;
//是否禁用全局事务,默认false
this.disable = ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, DEFAULT_DISABLE_GLOBAL_TRANSACTION);
//是否启用全局事务降级检查机制,默认是false
degradeCheck = ConfigurationFactory.getInstance().getBoolean(ConfigurationKeys.CLIENT_DEGRADE_CHECK, DEFAULT_TM_DEGRADE_CHECK);
//如果启用全局事务降级检查机制
if (degradeCheck) {
//添加一个监听器
ConfigurationCache.addConfigListener(ConfigurationKeys.CLIENT_DEGRADE_CHECK, this);
//设置降级检查周期,默认是2s一次
degradeCheckPeriod = ConfigurationFactory.getInstance().getInt(ConfigurationKeys.CLIENT_DEGRADE_CHECK_PERIOD, DEFAULT_TM_DEGRADE_CHECK_PERIOD);
//设置降级检查允许次数,默认10次
degradeCheckAllowTimes = ConfigurationFactory.getInstance().getInt(ConfigurationKeys.CLIENT_DEGRADE_CHECK_ALLOW_TIMES, DEFAULT_TM_DEGRADE_CHECK_ALLOW_TIMES);
//将自己注册到降级检查事件总线里,作为事件订阅者
EVENT_BUS.register(this);
//如果降级检查周期大于0,而且降级检查允许次数大于0,此时启动降级检查线程
if (degradeCheckPeriod > 0 && degradeCheckAllowTimes > 0) {
startDegradeCheck();
}
}
//初始化默认全局事务超时时间
this.initDefaultGlobalTransactionTimeout();
}
private void initDefaultGlobalTransactionTimeout() {
if (GlobalTransactionalInterceptor.defaultGlobalTransactionTimeout <= 0) {
int defaultGlobalTransactionTimeout;
try {
defaultGlobalTransactionTimeout = ConfigurationFactory.getInstance().getInt(ConfigurationKeys.DEFAULT_GLOBAL_TRANSACTION_TIMEOUT, DEFAULT_GLOBAL_TRANSACTION_TIMEOUT);
} catch (Exception e) {
LOGGER.error("Illegal global transaction timeout value: " + e.getMessage());
defaultGlobalTransactionTimeout = DEFAULT_GLOBAL_TRANSACTION_TIMEOUT;
}
if (defaultGlobalTransactionTimeout <= 0) {
LOGGER.warn("Global transaction timeout value '{}' is illegal, and has been reset to the default value '{}'", defaultGlobalTransactionTimeout, DEFAULT_GLOBAL_TRANSACTION_TIMEOUT);
defaultGlobalTransactionTimeout = DEFAULT_GLOBAL_TRANSACTION_TIMEOUT;
}
GlobalTransactionalInterceptor.defaultGlobalTransactionTimeout = defaultGlobalTransactionTimeout;
}
}
...
//auto upgrade service detection
//启动降级检查定时调度线程,默认每隔2s运行一次
private static void startDegradeCheck() {
executor.scheduleAtFixedRate(() -> {
if (degradeCheck) {
try {
//尝试通过应用id为null、事务服务分组为null、使用事务名称是degradeCheck、超时时间是60s的参数,去Seata Server开启一个全局事务
String xid = TransactionManagerHolder.get().begin(null, null, "degradeCheck", 60000);
//如果开启成功了,就获取到一个xid,直接对全局事务xid进行commit提交
TransactionManagerHolder.get().commit(xid);
//如果xid提交成功了,就发布一个降级检查事件到事件总线里,表明降级检查结果是true
EVENT_BUS.post(new DegradeCheckEvent(true));
} catch (Exception e) {
//如果开启一个全局事务失败,或者提交xid失败了,那么发布一个事件表示降级检查失败,结果是false
EVENT_BUS.post(new DegradeCheckEvent(false));
}
}
}, degradeCheckPeriod, degradeCheckPeriod, TimeUnit.MILLISECONDS);
}
...
}
8.Seata Server故障时全局事务拦截器的降级处理
降级检查定时调度线程每2秒进行降级检查的结果,会以事件的形式被发送到事件总线EventBus中。
GlobalTransactionalInterceptor在初始化时已把自己注册到了EventBus,所以添加了@Subscribe注解的onDegradeCheck()会消费事件总线的事件。
AOP切面拦截器GlobalTransactionalInterceptor的onDegradeCheck()方法,如果发现降级检查失败,会对degradeNum进行递增。如果发现降级检查成功,则会恢复degradeNum为0。
这样,当调用添加了@GlobalTransactional注解的方法时,会执行GlobalTransactionalInterceptor的invoke()方法,于是会根据degradeNum是否大于degradeCheckAllowTimes,也就是降级次数是否大于允许降级检查的次数,来决定是否执行降级。其中,降级的逻辑就是不开启全局事务而直接运行目标方法。
public class GlobalTransactionalInterceptor implements ConfigurationChangeListener, MethodInterceptor {
...
@Subscribe
public static void onDegradeCheck(DegradeCheckEvent event) {
if (event.isRequestSuccess()) {//如果检查成功,TC是有效的
if (degradeNum >= degradeCheckAllowTimes) {
reachNum++;
if (reachNum >= degradeCheckAllowTimes) {
reachNum = 0;
degradeNum = 0;
if (LOGGER.isInfoEnabled()) {
LOGGER.info("the current global transaction has been restored");
}
}
} else if (degradeNum != 0) {
degradeNum = 0;
}
} else {//如果检查失败,TC故障了,无法开启全局事务和提交了
//如果降级次数小于允许降级检查次数(10次)
if (degradeNum < degradeCheckAllowTimes) {
degradeNum++; //对降级次数+1
if (degradeNum >= degradeCheckAllowTimes) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("the current global transaction has been automatically downgraded");
}
}
} else if (reachNum != 0) {
reachNum = 0;
}
}
}
//如果调用添加了@GlobalTransactional注解的方法,就会执行如下invoke()方法
@Override
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
Class<?> targetClass = methodInvocation.getThis() != null ? AopUtils.getTargetClass(methodInvocation.getThis()) : null;
Method specificMethod = ClassUtils.getMostSpecificMethod(methodInvocation.getMethod(), targetClass);
if (specificMethod != null && !specificMethod.getDeclaringClass().equals(Object.class)) {
final Method method = BridgeMethodResolver.findBridgedMethod(specificMethod);
final GlobalTransactional globalTransactionalAnnotation = getAnnotation(method, targetClass, GlobalTransactional.class);
final GlobalLock globalLockAnnotation = getAnnotation(method, targetClass, GlobalLock.class);
//如果禁用了全局事务,或者开启了降级检查,同时降级次数大于了降级检查允许次数,那么localDisable就为true
//localDisable为true则表示全局事务被禁用了,此时就不可以开启全局事务了
boolean localDisable = disable || (degradeCheck && degradeNum >= degradeCheckAllowTimes);
//如果全局事务没有禁用
if (!localDisable) {
if (globalTransactionalAnnotation != null) {
//真正处理全局事务的入口
return handleGlobalTransaction(methodInvocation, globalTransactionalAnnotation);
} else if (globalLockAnnotation != null) {
return handleGlobalLock(methodInvocation, globalLockAnnotation);
}
}
}
//直接运行目标方法
return methodInvocation.proceed();
}
...
}
9.基于HTTP请求头的全局事务传播的源码
@Configuration
@ConditionalOnWebApplication
public class HttpAutoConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TransactionPropagationInterceptor());
}
@Override
public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HttpHandlerExceptionResolver());
}
}
//Springmvc Intercepter.
public class TransactionPropagationInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionPropagationInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String xid = RootContext.getXID();
//和Spring Cloud整合后,Feign在运行时也会执行Filter机制
//此时就可以把xid作为一个请求头放到HTTP请求里去
//接收方就会通过Spring MVC的拦截器,从请求头里提取xid,绑定到RootContext里
String rpcXid = request.getHeader(RootContext.KEY_XID);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("xid in RootContext[{}] xid in HttpContext[{}]", xid, rpcXid);
}
if (xid == null && rpcXid != null) {
RootContext.bind(rpcXid);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("bind[{}] to RootContext", rpcXid);
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) {
if (RootContext.inGlobalTransaction()) {
XidResource.cleanXid(request.getHeader(RootContext.KEY_XID));
}
}
}
10.XA数据源代理的初始化与XA连接代理的获取
//DataSource proxy for XA mode.
public class DataSourceProxyXA extends AbstractDataSourceProxyXA {
private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceProxyXA.class);
public DataSourceProxyXA(DataSource dataSource) {
this(dataSource, DEFAULT_RESOURCE_GROUP_ID);
}
public DataSourceProxyXA(DataSource dataSource, String resourceGroupId) {
if (dataSource instanceof SeataDataSourceProxy) {
LOGGER.info("Unwrap the data source, because the type is: {}", dataSource.getClass().getName());
dataSource = ((SeataDataSourceProxy) dataSource).getTargetDataSource();
}
this.dataSource = dataSource;
this.branchType = BranchType.XA;
JdbcUtils.initDataSourceResource(this, dataSource, resourceGroupId);
//Set the default branch type to 'XA' in the RootContext.
RootContext.setDefaultBranchType(this.getBranchType());
}
@Override
public Connection getConnection() throws SQLException {
Connection connection = dataSource.getConnection();
return getConnectionProxy(connection);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
Connection connection = dataSource.getConnection(username, password);
return getConnectionProxy(connection);
}
protected Connection getConnectionProxy(Connection connection) throws SQLException {
if (!RootContext.inGlobalTransaction()) {
return connection;
}
return getConnectionProxyXA(connection);
}
@Override
protected Connection getConnectionProxyXA() throws SQLException {
Connection connection = dataSource.getConnection();
return getConnectionProxyXA(connection);
}
private Connection getConnectionProxyXA(Connection connection) throws SQLException {
//物理连接
Connection physicalConn = connection.unwrap(Connection.class);
XAConnection xaConnection = XAUtils.createXAConnection(physicalConn, this);
ConnectionProxyXA connectionProxyXA = new ConnectionProxyXA(connection, xaConnection, this, RootContext.getXID());
connectionProxyXA.init();
return connectionProxyXA;
}
}
//Abstract DataSource proxy for XA mode.
public abstract class AbstractDataSourceProxyXA extends BaseDataSourceResource<ConnectionProxyXA> {
protected static final String DEFAULT_RESOURCE_GROUP_ID = "DEFAULT_XA";
//Get a ConnectionProxyXA instance for finishing XA branch(XA commit/XA rollback)
//@return ConnectionProxyXA instance
public ConnectionProxyXA getConnectionForXAFinish(XAXid xaXid) throws SQLException {
ConnectionProxyXA connectionProxyXA = lookup(xaXid.toString());
if (connectionProxyXA != null) {
return connectionProxyXA;
}
return (ConnectionProxyXA)getConnectionProxyXA();
}
protected abstract Connection getConnectionProxyXA() throws SQLException;
//Force close the physical connection kept for XA branch of given XAXid.
//@param xaXid the given XAXid
public void forceClosePhysicalConnection(XAXid xaXid) throws SQLException {
ConnectionProxyXA connectionProxyXA = lookup(xaXid.toString());
if (connectionProxyXA != null) {
connectionProxyXA.close();
Connection physicalConn = connectionProxyXA.getWrappedConnection();
if (physicalConn instanceof PooledConnection) {
physicalConn = ((PooledConnection)physicalConn).getConnection();
}
//Force close the physical connection
physicalConn.close();
}
}
}
11.XA分支事务注册与原始XA连接启动的源码
//Connection proxy for XA mode.
//XA模式的连接代理,通过它可以进行本地事务打开和提交/回滚,执行SQL语句,执行XA两阶段提交
public class ConnectionProxyXA extends AbstractConnectionProxyXA implements Holdable {
private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionProxyXA.class);
//当前自动提交事务状态,默认true
private boolean currentAutoCommitStatus = true;
//XA分支事务xid
private XAXid xaBranchXid;
//XA事务是否活跃的标记,默认false
private boolean xaActive = false;
//是否保持住XA事务,默认false
private boolean kept = false;
//Constructor of Connection Proxy for XA mode.
//@param originalConnection Normal Connection from the original DataSource.
//@param xaConnection XA Connection based on physical connection of the normal Connection above.
//@param resource The corresponding Resource(DataSource proxy) from which the connections was created.
//@param xid Seata global transaction xid.
public ConnectionProxyXA(Connection originalConnection, XAConnection xaConnection, BaseDataSourceResource resource, String xid) {
super(originalConnection, xaConnection, resource, xid);
}
public void init() {
try {
this.xaResource = xaConnection.getXAResource();
this.currentAutoCommitStatus = this.originalConnection.getAutoCommit();
if (!currentAutoCommitStatus) {
throw new IllegalStateException("Connection[autocommit=false] as default is NOT supported");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
...
//修改和调整自动提交事务状态时,会开始进行分支事务的注册
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
if (currentAutoCommitStatus == autoCommit) {
return;
}
if (autoCommit) {
//According to JDBC spec:
//If this method is called during a transaction and the auto-commit mode is changed, the transaction is committed.
if (xaActive) {
commit();
}
} else {
if (xaActive) {
throw new SQLException("should NEVER happen: setAutoCommit from true to false while xa branch is active");
}
//Start a XA branch
long branchId = 0L;
try {
//1. register branch to TC then get the branchId
//分支事务发起注册,类型为XA,传入resourceId和xid
branchId = DefaultResourceManager.get().branchRegister(BranchType.XA, resource.getResourceId(), null, xid, null, null);
} catch (TransactionException te) {
cleanXABranchContext();
throw new SQLException("failed to register xa branch " + xid + " since " + te.getCode() + ":" + te.getMessage(), te);
}
//2. build XA-Xid with xid and branchId
this.xaBranchXid = XAXidBuilder.build(xid, branchId);
try {
//3. XA Start
xaResource.start(this.xaBranchXid, XAResource.TMNOFLAGS);
} catch (XAException e) {
cleanXABranchContext();
throw new SQLException("failed to start xa branch " + xid + " since " + e.getMessage(), e);
}
//4. XA is active
this.xaActive = true;
}
currentAutoCommitStatus = autoCommit;
}
...
}
//The type Abstract connection proxy on XA mode.
public abstract class AbstractConnectionProxyXA implements Connection {
public static final String SQLSTATE_XA_NOT_END = "SQLSTATE_XA_NOT_END";
//原始连接
protected Connection originalConnection;
//XA包装过的连接
protected XAConnection xaConnection;
//XA事务资源
protected XAResource xaResource;
//基础数据库连接池资源
protected BaseDataSourceResource resource;
//分布式事务xid
protected String xid;
public AbstractConnectionProxyXA(Connection originalConnection, XAConnection xaConnection, BaseDataSourceResource resource, String xid) {
this.originalConnection = originalConnection;
this.xaConnection = xaConnection;
this.resource = resource;
this.xid = xid;
}
...
}
12.XA分布式事务两阶段提交流程的源码
Seata的XA是依赖MySQL的XA来实现的。
//Connection proxy for XA mode.
//XA模式的连接代理,通过它可以进行本地事务打开和提交/回滚,执行SQL语句,执行XA两阶段提交
public class ConnectionProxyXA extends AbstractConnectionProxyXA implements Holdable {
...
//第一阶段提交
@Override
public void commit() throws SQLException {
if (currentAutoCommitStatus) {
//Ignore the committing on an autocommit session.
return;
}
if (!xaActive || this.xaBranchXid == null) {
throw new SQLException("should NOT commit on an inactive session", SQLSTATE_XA_NOT_END);
}
try {
//XA End: Success
xaResource.end(xaBranchXid, XAResource.TMSUCCESS);
//XA Prepare
xaResource.prepare(xaBranchXid);
//Keep the Connection if necessary
keepIfNecessary();
} catch (XAException xe) {
try {
//Branch Report to TC: Failed
DefaultResourceManager.get().branchReport(BranchType.XA, xid, xaBranchXid.getBranchId(), BranchStatus.PhaseOne_Failed, null);
} catch (TransactionException te) {
LOGGER.warn("Failed to report XA branch commit-failure on " + xid + "-" + xaBranchXid.getBranchId() + " since " + te.getCode() + ":" + te.getMessage() + " and XAException:" + xe.getMessage());
}
throw new SQLException("Failed to end(TMSUCCESS)/prepare xa branch on " + xid + "-" + xaBranchXid.getBranchId() + " since " + xe.getMessage(), xe);
} finally {
cleanXABranchContext();
}
}
//第二阶段提交
public void xaCommit(String xid, long branchId, String applicationData) throws XAException {
XAXid xaXid = XAXidBuilder.build(xid, branchId);
xaResource.commit(xaXid, false);
releaseIfNecessary();
}
...
}