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

optimize: Compatible with the SPI 、saga、spring module #6427

Merged
merged 27 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ header:
- 'ext/apm-seata-skywalking-plugin/config/agent.config'
- 'server/src/main/resources/lua/redislocker/redislock.lua'
- 'server/src/main/resources/banner.txt'
- '**/*.json'


comment: on-failure
Expand Down
5 changes: 5 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Add changes here for all PR submitted to the 2.x branch.
### refactor:
- [[#6269](https://github.com/apache/incubator-seata/pull/6269)] standardize Seata Exception
- [[#6419](https://github.com/apache/incubator-seata/pull/6419)] optimize integration-tx-api compatible
- [[#6427](https://github.com/apache/incubator-seata/pull/6427)] support spi、saga、spring module compatible

### refactor:
- [[#6269](https://github.com/apache/incubator-seata/pull/6269)] standardize Seata Exception
- [[#6405](https://github.com/apache/incubator-seata/pull/6405)] fix kotlin compile failure
- [[#6412](https://github.com/apache/incubator-seata/pull/6412)] optimize core compatible module
- [[#6429](https://github.com/apache/incubator-seata/pull/6429)] remove repetitive words
Expand Down Expand Up @@ -178,6 +182,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [gggyd123](https://github.com/gggyd123)
- [jonasHanhan](https://github.com/jonasHanhan)
- [Code-breaker1998](https://github.com/Code-breaker1998)
- [yixia](https://github.com/wt-better)
- [MikhailNavitski](https://github.com/MikhailNavitski)
- [deung](https://github.com/deung)

Expand Down
2 changes: 2 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
- [[#6387](https://github.com/apache/incubator-seata/pull/6387)] 优化tcc使用兼容
- [[#6402](https://github.com/apache/incubator-seata/pull/6402)] 优化rm-datasource向下兼容
- [[#6419](https://github.com/apache/incubator-seata/pull/6419)] 优化integration-tx-api向下兼容
- [[#6427](https://github.com/apache/incubator-seata/pull/6427)] 支持spi、saga、spring模块的向下兼容
- [[#6442](https://github.com/apache/incubator-seata/pull/6442)] 阐明 if


Expand Down Expand Up @@ -177,6 +178,7 @@
- [gggyd123](https://github.com/gggyd123)
- [jonasHanhan](https://github.com/jonasHanhan)
- [Code-breaker1998](https://github.com/Code-breaker1998)
- [yixia](https://github.com/wt-better)
- [MikhailNavitski](https://github.com/MikhailNavitski)
- [deung](https://github.com/deung)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,16 @@

/**
* The type Enhanced service loader.
*
*/
public class EnhancedServiceLoader {
private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedServiceLoader.class);
private static final String APACHE_SEATA_PACKAGE_NAME = "org.apache.seata";
private static final String IO_SEATA_PACKAGE_NAME = "io.seata";

/**
* Class->InnerEnhancedServiceLoader map
*/
private static final ConcurrentMap<Class<?>, InnerEnhancedServiceLoader<?>> SERVICE_LOADERS =
new ConcurrentHashMap<>();

private static <S> Class<S> getCompatibleService(Class<S> originService) {
String apacheSeataName = originService.getName();
String ioSeataName = apacheSeataName.replace(APACHE_SEATA_PACKAGE_NAME, IO_SEATA_PACKAGE_NAME);
try {
Class clasz = Class.forName(ioSeataName);
return clasz;
} catch (ClassNotFoundException e) {
return null;
}
}
/**
* Specify classLoader to load the service provider
*
Expand All @@ -73,15 +60,6 @@ private static <S> Class<S> getCompatibleService(Class<S> originService) {
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service, ClassLoader loader) throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(loader);
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(loader);
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(loader);
}

Expand All @@ -94,15 +72,6 @@ public static <S> S load(Class<S> service, ClassLoader loader) throws EnhancedSe
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service) throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(findClassLoader());
}

Expand All @@ -116,15 +85,6 @@ public static <S> S load(Class<S> service) throws EnhancedServiceNotFoundExcepti
* @throws EnhancedServiceNotFoundException the enhanced service not found exception
*/
public static <S> S load(Class<S> service, String activateName) throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, findClassLoader());
}

Expand All @@ -140,15 +100,6 @@ public static <S> S load(Class<S> service, String activateName) throws EnhancedS
*/
public static <S> S load(Class<S> service, String activateName, ClassLoader loader)
throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, loader);
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, loader);
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, loader);
}

Expand All @@ -164,15 +115,6 @@ public static <S> S load(Class<S> service, String activateName, ClassLoader load
*/
public static <S> S load(Class<S> service, String activateName, Object[] args)
throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, args, findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, args, findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, args, findClassLoader());
}

Expand All @@ -189,15 +131,6 @@ public static <S> S load(Class<S> service, String activateName, Object[] args)
*/
public static <S> S load(Class<S> service, String activateName, Class<?>[] argsType, Object[] args)
throws EnhancedServiceNotFoundException {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, argsType, args, findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).load(activateName, argsType, args, findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).load(activateName, argsType, args, findClassLoader());
}

Expand All @@ -209,15 +142,6 @@ public static <S> S load(Class<S> service, String activateName, Class<?>[] argsT
* @return list list
*/
public static <S> List<S> loadAll(Class<S> service) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).loadAll(findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(findClassLoader());
}

Expand All @@ -231,15 +155,6 @@ public static <S> List<S> loadAll(Class<S> service) {
* @return list list
*/
public static <S> List<S> loadAll(Class<S> service, Class<?>[] argsType, Object[] args) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(argsType, args, findClassLoader());
} catch (EnhancedServiceNotFoundException ignore) {
LOGGER.warn("Can't load SPI for :{} from org.apache.seata package,will try to load SPI from io.seata package", service.getName());
}
return InnerEnhancedServiceLoader.getServiceLoader(compatibleService).loadAll(argsType, args, findClassLoader());
}
return InnerEnhancedServiceLoader.getServiceLoader(service).loadAll(argsType, args, findClassLoader());
}

Expand All @@ -257,17 +172,7 @@ public static void unloadAll() {
* @param service the service
*/
public static <S> void unload(Class<S> service) {
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
InnerEnhancedServiceLoader.removeServiceLoader(service);
} catch (Exception ignore) {
LOGGER.warn("Can't unload SPI for :{} from org.apache.seata package,will try to unload SPI from io.seata package", service.getName());
InnerEnhancedServiceLoader.removeServiceLoader(compatibleService);
}
} else {
InnerEnhancedServiceLoader.removeServiceLoader(service);
}
InnerEnhancedServiceLoader.removeServiceLoader(service);
}

/**
Expand All @@ -281,20 +186,9 @@ public static <S> void unload(Class<S> service, String activateName) {
if (activateName == null) {
throw new IllegalArgumentException("activateName is null");
}
Class<S> compatibleService = getCompatibleService(service);
if (compatibleService != null) {
try {
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service);
doUnload(serviceLoader, activateName);
} catch (Exception ignore) {
LOGGER.warn("Can't unload SPI for :{} from org.apache.seata package,will try to unload SPI from io.seata package", service.getName());
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(compatibleService);
doUnload(serviceLoader, activateName);
}
} else {
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service);
doUnload(serviceLoader, activateName);
}

InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service);
doUnload(serviceLoader, activateName);
}

private static <S> void doUnload(InnerEnhancedServiceLoader<S> serviceLoader, String activateName) {
Expand Down Expand Up @@ -342,6 +236,7 @@ static <S> List<Class<S>> getAllExtensionClass(Class<S> service) {
static <S> List<Class<S>> getAllExtensionClass(Class<S> service, ClassLoader loader) {
return InnerEnhancedServiceLoader.getServiceLoader(service).getAllExtensionClass(loader);
}

/**
* Cannot use TCCL, in the pandora container will cause the class in the plugin not to be loaded
*
Expand All @@ -357,6 +252,9 @@ private static class InnerEnhancedServiceLoader<S> {
private static final String SERVICES_DIRECTORY = "META-INF/services/";
private static final String SEATA_DIRECTORY = "META-INF/seata/";

private static final String APACHE_SEATA_PACKAGE_NAME = "org.apache.seata";
private static final String IO_SEATA_PACKAGE_NAME = "io.seata";

private final Class<S> type;
private final Holder<List<ExtensionDefinition<S>>> definitionsHolder = new Holder<>();
private final ConcurrentMap<ExtensionDefinition<S>, Holder<Object>> definitionToInstanceMap =
Expand All @@ -369,7 +267,6 @@ private InnerEnhancedServiceLoader(Class<S> type) {
}



/**
* Get the ServiceLoader for the specified Class
*
Expand All @@ -391,7 +288,7 @@ private static <S> InnerEnhancedServiceLoader<S> removeServiceLoader(Class<S> ty
if (type == null) {
throw new IllegalArgumentException("Enhanced Service type is null");
}
return (InnerEnhancedServiceLoader<S>)SERVICE_LOADERS.remove(type);
return (InnerEnhancedServiceLoader<S>) SERVICE_LOADERS.remove(type);
}

private static void removeAllServiceLoader() {
Expand Down Expand Up @@ -588,8 +485,20 @@ private List<Class<S>> loadAllExtensionClass(ClassLoader loader) {
private List<ExtensionDefinition<S>> findAllExtensionDefinition(ClassLoader loader) {
List<ExtensionDefinition<S>> extensionDefinitions = new ArrayList<>();
try {
loadFile(SERVICES_DIRECTORY, loader, extensionDefinitions);
loadFile(SEATA_DIRECTORY, loader, extensionDefinitions);
loadFile(SERVICES_DIRECTORY, type, loader, extensionDefinitions);
loadFile(SEATA_DIRECTORY, type, loader, extensionDefinitions);

@SuppressWarnings("rawtypes") Class compatibleService = getCompatibleService(type);
if (compatibleService != null) {
if (type.isAssignableFrom(compatibleService)) {
LOGGER.info("Load compatible class {}", compatibleService.getName());
loadFile(SERVICES_DIRECTORY, compatibleService, loader, extensionDefinitions);
loadFile(SEATA_DIRECTORY, compatibleService, loader, extensionDefinitions);
} else {
LOGGER.info("Ignore load compatible class {}, because is not assignable from origin type {}", compatibleService.getName(), type.getName());
}
}

} catch (IOException e) {
throw new EnhancedServiceNotFoundException(e);
}
Expand All @@ -616,8 +525,17 @@ private List<ExtensionDefinition<S>> findAllExtensionDefinition(ClassLoader load
return extensionDefinitions;
}

private static Class getCompatibleService(Class originType) {
String ioSeataType = originType.getName().replace(APACHE_SEATA_PACKAGE_NAME, IO_SEATA_PACKAGE_NAME);
try {
return Class.forName(ioSeataType);
} catch (ClassNotFoundException e) {
return null;
}
}


private void loadFile(String dir, ClassLoader loader, List<ExtensionDefinition<S>> extensions)
private void loadFile(String dir, Class type, ClassLoader loader, List<ExtensionDefinition<S>> extensions)
slievrly marked this conversation as resolved.
Show resolved Hide resolved
throws IOException {
String fileName = dir + type.getName();
Enumeration<java.net.URL> urls;
Expand Down
7 changes: 7 additions & 0 deletions compatible/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@
<version>${mariadb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.seata.rm;
package io.seata.core.model;


import org.apache.seata.common.loader.EnhancedServiceLoader;

/**
* The Abstract RM event handler
*
* @see EnhancedServiceLoader.InnerEnhancedServiceLoader#findAllExtensionDefinition(ClassLoader)
*/
public abstract class AbstractRMHandler extends org.apache.seata.rm.AbstractRMHandler {
public interface ResourceManager extends org.apache.seata.core.model.ResourceManager {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import io.seata.saga.engine.expression.ExpressionFactoryManager;
import io.seata.saga.engine.repo.StateLogRepository;
import io.seata.saga.engine.repo.StateMachineRepository;
import io.seata.saga.engine.store.StateLogStore;
import org.apache.seata.saga.engine.expression.ExpressionResolver;
import org.apache.seata.saga.engine.invoker.ServiceInvokerManager;
import org.apache.seata.saga.engine.sequence.SeqGenerator;
import org.apache.seata.saga.engine.store.StateLangStore;
import org.apache.seata.saga.engine.store.StateLogStore;
import org.apache.seata.saga.engine.strategy.StatusDecisionStrategy;
import org.apache.seata.saga.proctrl.eventing.impl.ProcessCtrlEventPublisher;

Expand Down
Loading
Loading