Skip to content

Commit

Permalink
optimize: branch register resource only at RM server end (#5399)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrui1225 authored Jan 8, 2024
1 parent 607624c commit 1679849
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 99 deletions.
3 changes: 3 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6184](https://github.com/apache/incubator-seata/pull/6184)] update NOTICE file
- [[#6192](https://github.com/apache/incubator-seata/pull/6192)] remove the useless file
- [[#6194](https://github.com/apache/incubator-seata/pull/6194)] fix asf.yaml parse error
- [[#5399](https://github.com/apache/incubator-seata/pull/5399)] optimizing branch register resource only at RM server end
- [[#6154](https://github.com/apache/incubator-seata/pull/6154)] console log optimize for "kubectl logs -f"
- [[#6116](https://github.com/apache/incubator-seata/pull/6116)] rewrite NettyPoolKey's hashcode and equals to fix duplicate construction of channel object pools
- [[#6195](https://github.com/apache/incubator-seata/pull/6195)] update the url in change log to apache/incubator-seata
Expand Down Expand Up @@ -78,10 +79,12 @@ Thanks to these contributors for their code commits. Please report an unintended
- [lightClouds917](https://github.com/lightClouds917)
- [xingfudeshi](https://github.com/xingfudeshi)
- [PleaseGiveMeTheCoke](https://github.com/PleaseGiveMeTheCoke)
- [sunrui1225](https://github.com/sunrui1225)
- [PeppaO](https://github.com/PeppaO)
- [AlbumenJ](https://github.com/AlbumenJ)
- [dreamskyvision](https://github.com/dreamskyvision)
- [jsbxyyx](https://github.com/jsbxyyx)
- [liuqiufeng](https://github.com/liuqiufeng)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
5 changes: 4 additions & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
- [[#6184](https://github.com/apache/incubator-seata/pull/6184)] 更新NOTICE文件
- [[#6192](https://github.com/apache/incubator-seata/pull/6192)] 移除无用文件
- [[#6194](https://github.com/apache/incubator-seata/pull/6194)] 修复 asf.yaml 解析错误问题
- [[#6116](https://github.com/apache/incubator-seata/pull/6116)] 重写NettyPoolKey的hashcode和equals,修复了channel对象池重复构建问题
- [[#5399](https://github.com/apache/incubator-seata/pull/5399)] 分支注册只在RM端
- [[#6154](https://github.com/apache/incubator-seata/pull/6154)] 控制台日志优化 "kubectl logs -f"
- [[#6116](https://github.com/apache/incubator-seata/pull/6116)] 重写NettyPoolKey的hashcode和equals,修复了channel对象池重复构建问题
- [[#6195](https://github.com/apache/incubator-seata/pull/6195)] 更新 change log 中的 seata url 为 apache/incubator-seata
Expand All @@ -49,6 +49,7 @@
- [[#6004](https://github.com/apache/incubator-seata/pull/6004)] 优化RM,TM连接server快速失败
- [[#6243](https://github.com/apache/incubator-seata/pull/6243)] 优化控制台页眉中的链接


### security:
- [[#6069](https://github.com/apache/incubator-seata/pull/6069)] 升级Guava依赖版本,修复安全漏洞
- [[#6144](https://github.com/apache/incubator-seata/pull/6144)] 升级Nacos依赖版本至1.4.6
Expand Down Expand Up @@ -78,10 +79,12 @@
- [lightClouds917](https://github.com/lightClouds917)
- [xingfudeshi](https://github.com/xingfudeshi)
- [PleaseGiveMeTheCoke](https://github.com/PleaseGiveMeTheCoke)
- [sunrui1225](https://github.com/sunrui1225)
- [PeppaO](https://github.com/PeppaO)
- [AlbumenJ](https://github.com/AlbumenJ)
- [dreamskyvision](https://github.com/dreamskyvision)
- [jsbxyyx](https://github.com/jsbxyyx)
- [liuqiufeng](https://github.com/liuqiufeng)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;

/**
* @author leezongjie
*/
public class DefaultInterfaceParser implements InterfaceParser {

Expand Down Expand Up @@ -63,4 +64,15 @@ public ProxyInvocationHandler parserInterfaceToProxy(Object target, String objec
return null;
}

@Override
public IfNeedEnhanceBean parseIfNeedEnhancement(Class<?> beanClass) {
for (InterfaceParser interfaceParser : ALL_INTERFACE_PARSERS) {
IfNeedEnhanceBean ifNeedEnhanceBean = interfaceParser.parseIfNeedEnhancement(beanClass);
if (ifNeedEnhanceBean.isIfNeed()) {
return ifNeedEnhanceBean;
}
}
return new IfNeedEnhanceBean();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.seata.common.ConfigurationKeys;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.ReflectionUtil;
import io.seata.config.ConfigurationCache;
import io.seata.config.ConfigurationChangeListener;
import io.seata.integration.tx.api.interceptor.handler.GlobalTransactionalInterceptorHandler;
Expand Down Expand Up @@ -59,6 +60,19 @@ public ProxyInvocationHandler parserInterfaceToProxy(Object target, String objec
return null;
}

@Override
public IfNeedEnhanceBean parseIfNeedEnhancement(Class<?> beanClass) {
Set<Class<?>> interfaceClasses = ReflectionUtil.getInterfaces(beanClass);
Class<?>[] interfaceClasseArray = interfaceClasses.toArray(new Class<?>[0]);

IfNeedEnhanceBean ifNeedEnhanceBean = new IfNeedEnhanceBean();
if (existsAnnotation(beanClass) || existsAnnotation(interfaceClasseArray)) {
ifNeedEnhanceBean.setIfNeed(true);
ifNeedEnhanceBean.setNeedEnhanceEnum(NeedEnhanceEnum.GLOBAL_TRANSACTIONAL_BEAN);
}
return ifNeedEnhanceBean;
}

private boolean existsAnnotation(Class<?>... classes) {
boolean result = false;
if (CollectionUtils.isNotEmpty(classes)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 io.seata.integration.tx.api.interceptor.parser;

public class IfNeedEnhanceBean {

private boolean ifNeed;

private NeedEnhanceEnum needEnhanceEnum;

public boolean isIfNeed() {
return ifNeed;
}

public void setIfNeed(boolean ifNeed) {
this.ifNeed = ifNeed;
}

public NeedEnhanceEnum getNeedEnhanceEnum() {
return needEnhanceEnum;
}

public void setNeedEnhanceEnum(NeedEnhanceEnum needEnhanceEnum) {
this.needEnhanceEnum = needEnhanceEnum;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public interface InterfaceParser {

ProxyInvocationHandler parserInterfaceToProxy(Object target, String objectName) throws Exception;

IfNeedEnhanceBean parseIfNeedEnhancement(Class<?> beanClass);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 io.seata.integration.tx.api.interceptor.parser;


public enum NeedEnhanceEnum {

/**
* global transactional bean type
*/
GLOBAL_TRANSACTIONAL_BEAN,
/**
* service bean type
*/
SERVICE_BEAN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public interface RemotingParser {
*/
boolean isService(Object bean, String beanName) throws FrameworkException;

/**
* if it is service bean ?
*
* @param beanClass the bean class
* @return boolean boolean
* @throws FrameworkException the framework exception
*/
boolean isService(Class<?> beanClass) throws FrameworkException;

/**
* get the remoting bean info
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ public boolean isService(Object bean, String beanName) {
return false;
}

/**
* is service bean ?
*
* @param beanClass the bean class
* @return boolean boolean
*/
public boolean isService(Class<?> beanClass) {
for (RemotingParser remotingParser : allRemotingParsers) {
if (remotingParser.isService(beanClass)) {
return true;
}
}
return false;
}

/**
* get the remoting Service desc
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public boolean isService(Object bean, String beanName) throws FrameworkException
|| "org.apache.dubbo.config.spring.ServiceBean".equals(c.getName());
}

@Override
public boolean isService(Class<?> beanClass) throws FrameworkException {
return "com.alibaba.dubbo.config.spring.ServiceBean".equals(beanClass.getName())
|| "org.apache.dubbo.config.spring.ServiceBean".equals(beanClass.getName());
}

@Override
public RemotingDesc getServiceDesc(Object bean, String beanName) throws FrameworkException {
if (!this.isRemoting(bean, beanName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public boolean isService(Object bean, String beanName) {
return isHsf && "com.taobao.hsf.app.spring.util.HSFSpringProviderBean".equals(beanClassName);
}

@Override
public boolean isService(Class<?> beanClass) throws FrameworkException {
String beanClassName = beanClass.getName();
return isHsf && "com.taobao.hsf.app.spring.util.HSFSpringProviderBean".equals(beanClassName);
}

@Override
public RemotingDesc getServiceDesc(Object bean, String beanName) throws FrameworkException {
if (!this.isRemoting(bean, beanName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public boolean isService(Object bean, String beanName) throws FrameworkException
return "com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean".equals(beanClassName);
}

@Override
public boolean isService(Class<?> beanClass) throws FrameworkException {
String beanClassName = beanClass.getName();
return "com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean".equals(beanClassName);
}

@Override
public RemotingDesc getServiceDesc(Object bean, String beanName) throws FrameworkException {
if (!this.isRemoting(bean, beanName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public static <T> T createProxy(T target) {
return createProxy(target, target.getClass().getName());
}

/**
* The API for generating proxy for target. It can be used by spring aop, or
* provide user to generate proxy manually.
* <p>
* At TM side, It can be used for the target bean with @GlobalTransactional or @GlobalLock to generate proxy which start global transaction.
* At RM side, if you use TCC mode, It can be for target bean with @TwoPhaseBusinessAction to generate proxy which register branch source
* and branch transaction. If you want to use this API to generate proxy manual like dubbo, you must make sure the target bean is the
* business bean with @GlobalTransactional annotation. If you pass the ServiceBean(com.alibaba.dubbo.config.spring.ServiceBean) or
* ReferenceBean(com.alibaba.dubbo.config.spring.ReferenceBean), it will don't work.
*
* @param target the business bean
* @param beanName the business bean name
* @return the proxy bean
* @param <T> the generics class
*/
public static <T> T createProxy(T target, String beanName) {
try {
synchronized (PROXYED_SET) {
Expand Down
12 changes: 8 additions & 4 deletions spring/src/main/java/io/seata/rm/fence/SpringFenceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ public boolean commitFence(Method commitMethod, Object targetTCCBean,
}
return false;
}
return updateStatusAndInvokeTargetMethod(conn, commitMethod, targetTCCBean, xid, branchId, CommonFenceConstant.STATUS_COMMITTED, status, args);
boolean result = updateStatusAndInvokeTargetMethod(conn, commitMethod, targetTCCBean, xid, branchId, CommonFenceConstant.STATUS_COMMITTED, status, args);
LOGGER.info("Common fence commit result: {}. xid: {}, branchId: {}", result, xid, branchId);
return result;
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
Expand Down Expand Up @@ -210,7 +212,9 @@ public boolean rollbackFence(Method rollbackMethod, Object targetTCCBean,
return false;
}
}
return updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, CommonFenceConstant.STATUS_ROLLBACKED, status, args);
boolean result = updateStatusAndInvokeTargetMethod(conn, rollbackMethod, targetTCCBean, xid, branchId, CommonFenceConstant.STATUS_ROLLBACKED, status, args);
LOGGER.info("Common fence rollback result: {}. xid: {}, branchId: {}", result, xid, branchId);
return result;
} catch (Throwable t) {
status.setRollbackOnly();
throw new SkipCallbackWrapperException(t);
Expand All @@ -237,13 +241,13 @@ private static boolean insertCommonFenceLog(Connection conn, String xid, Long br
}

/**
* Update TCC Fence status and invoke target method
* Update Common Fence status and invoke target method
*
* @param method target method
* @param targetTCCBean target bean
* @param xid the global transaction id
* @param branchId the branch transaction id
* @param status the tcc fence status
* @param status the common fence status
* @return the boolean
*/
private static boolean updateStatusAndInvokeTargetMethod(Connection conn, Method method, Object targetTCCBean,
Expand Down
Loading

0 comments on commit 1679849

Please sign in to comment.