Skip to content

Commit

Permalink
optimize: compatible with lower versions of SPI (#6315)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingfudeshi authored Feb 19, 2024
1 parent f80a0fa commit 5667d63
Show file tree
Hide file tree
Showing 47 changed files with 1,419 additions and 24 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] upgrade console frontend dependencies and supported nodejs versions
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] add saga related io.seata compatible api
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console display the version number
- [[#6315](https://github.com/apache/incubator-seata/pull/6315)] compatible with lower versions of SPI
- [[#6327](https://github.com/apache/incubator-seata/pull/6327)] compatible with integration.http and integration.http.Jakarta
- [[#6328](https://github.com/apache/incubator-seata/pull/6328)] compatible with integration.grpc
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] remove mariadb API
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
- [[#6301](https://github.com/apache/incubator-seata/pull/6301)] 升级console前端依赖及支持的nodejs版本
- [[#6301](https://github.com/apache/incubator-seata/pull/6312)] 添加saga相关的io.seata兼容性API
- [[#6313](https://github.com/apache/incubator-seata/pull/6313)] console展示版本号
- [[#6315](https://github.com/apache/incubator-seata/pull/6315)] 兼容低版本的SPI
- [[#6327](https://github.com/apache/incubator-seata/pull/6327)] 兼容 integration.http 和 integration.http.Jakarta API
- [[#6328](https://github.com/apache/incubator-seata/pull/6328)] 兼容 integration.grpc API
- [[#6330](https://github.com/apache/incubator-seata/pull/6330)] 去除 mariadb API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
package org.apache.seata.common.loader;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.seata.common.Constants;
import org.apache.seata.common.executor.Initialize;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -30,26 +38,31 @@
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import org.apache.seata.common.Constants;
import org.apache.seata.common.executor.Initialize;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 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 @@ -60,6 +73,15 @@ public class EnhancedServiceLoader {
* @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 @@ -72,6 +94,15 @@ 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 @@ -85,6 +116,15 @@ 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 @@ -100,6 +140,15 @@ 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 @@ -115,6 +164,15 @@ 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 @@ -131,6 +189,15 @@ 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 @@ -142,6 +209,15 @@ 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 @@ -155,6 +231,15 @@ 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 @@ -172,7 +257,17 @@ public static void unloadAll() {
* @param service the service
*/
public static <S> void unload(Class<S> service) {
InnerEnhancedServiceLoader.removeServiceLoader(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);
}
}

/**
Expand All @@ -183,11 +278,26 @@ public static <S> void unload(Class<S> service) {
* @param activateName the activate name
*/
public static <S> void unload(Class<S> service, String activateName) {

if (activateName == null) {
throw new IllegalArgumentException("activateName is null");
}
InnerEnhancedServiceLoader<S> serviceLoader = InnerEnhancedServiceLoader.getServiceLoader(service);
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);
}
}

private static <S> void doUnload(InnerEnhancedServiceLoader<S> serviceLoader, String activateName) {
ConcurrentMap<Class<?>, ExtensionDefinition<S>> classToDefinitionMap = serviceLoader.classToDefinitionMap;
List<ExtensionDefinition<S>> extensionDefinitions = new ArrayList<>();
for (Map.Entry<Class<?>, ExtensionDefinition<S>> entry : classToDefinitionMap.entrySet()) {
Expand All @@ -207,7 +317,6 @@ public static <S> void unload(Class<S> service, String activateName) {

}
}

}


Expand Down Expand Up @@ -259,6 +368,8 @@ private InnerEnhancedServiceLoader(Class<S> type) {
this.type = type;
}



/**
* Get the ServiceLoader for the specified Class
*
Expand Down
29 changes: 24 additions & 5 deletions compatible/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,38 @@
<dependencies>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-core</artifactId>
<artifactId>seata-saga-engine</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-saga-engine</artifactId>
<version>2.1.0-SNAPSHOT</version>
<artifactId>seata-saga-engine-store</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-saga-engine-store</artifactId>
<version>2.1.0-SNAPSHOT</version>
<artifactId>seata-integration-tx-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-metrics-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-sqlparser-druid</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-spring</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit 5667d63

Please sign in to comment.