Skip to content

Commit

Permalink
bugfix: fix getConfig throw ClassCastException when use JDK proxy (
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 authored Jan 29, 2023
1 parent 63f685e commit 72347f2
Show file tree
Hide file tree
Showing 24 changed files with 265 additions and 157 deletions.
5 changes: 2 additions & 3 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#5224](https://github.com/seata/seata/pull/5224)] fix oracle initialize script index_name is duplicate
- [[#5233](https://github.com/seata/seata/pull/5233)] fix the inconsistent configuration item names related to LoadBalance
- [[#5245](https://github.com/seata/seata/pull/5245)] fix the incomplete dependency of distribution module


- [[#5239](https://github.com/seata/seata/pull/5239)] fix `getConfig` throw `ClassCastException` when use JDK proxy

### optimize:
- [[#5208](https://github.com/seata/seata/pull/5208)] optimize throwable getCause once more
Expand All @@ -26,7 +25,6 @@ Add changes here for all PR submitted to the develop branch.
### security:
- [[#5172](https://github.com/seata/seata/pull/5172)] fix some security vulnerabilities


### test:
- [[#xxx](https://github.com/seata/seata/pull/xxx)] add test for xxx

Expand All @@ -42,6 +40,7 @@ Thanks to these contributors for their code commits. Please report an unintended
- [xingfudeshi](https://github.com/xingfudeshi)
- [Bughue](https://github.com/Bughue)
- [pengten](https://github.com/pengten)
- [wangliang181230](https://github.com/wangliang181230)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
3 changes: 2 additions & 1 deletion changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- [[#5224](https://github.com/seata/seata/pull/5224)] 修复 oracle初始化脚本索引名重复的问题
- [[#5233](https://github.com/seata/seata/pull/5233)] 修复LoadBalance相关配置不一致的问题
- [[#5245](https://github.com/seata/seata/pull/5245)] 修复不完整的distribution模块依赖

- [[#5239](https://github.com/seata/seata/pull/5239)] 修复当使用JDK代理时,`getConfig` 方法获取部分配置时抛出 `ClassCastException` 异常的问题

### optimize:
- [[#5208](https://github.com/seata/seata/pull/5208)] 优化多次重复获取Throwable#getCause问题
Expand Down Expand Up @@ -40,6 +40,7 @@
- [xingfudeshi](https://github.com/xingfudeshi)
- [Bughue](https://github.com/Bughue)
- [pengten](https://github.com/pengten)
- [wangliang181230](https://github.com/wangliang181230)


同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
11 changes: 8 additions & 3 deletions common/src/main/java/io/seata/common/DefaultValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public interface DefaultValues {
int DEFAULT_SERVICE_SESSION_RELOAD_READ_SIZE = 100;

/**
*the constant DEFAULT_PROMETHEUS_PORT
* the constant DEFAULT_PROMETHEUS_PORT
*/
int DEFAULT_PROMETHEUS_PORT = 9898;

Expand All @@ -248,12 +248,12 @@ public interface DefaultValues {
/**
* the const DEFAULT_MAX_COMMIT_RETRY_TIMEOUT
*/
long DEFAULT_MAX_COMMIT_RETRY_TIMEOUT = 100;
long DEFAULT_MAX_COMMIT_RETRY_TIMEOUT = -1L;

/**
* the const DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT
*/
long DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT = 100;
long DEFAULT_MAX_ROLLBACK_RETRY_TIMEOUT = -1L;

/**
* the const DEFAULT_ROLLBACK_RETRY_TIMEOUT_UNLOCK_ENABLE
Expand Down Expand Up @@ -281,4 +281,9 @@ public interface DefaultValues {
int DEFAULT_REDIS_MIN_IDLE = 10;

int DEFAULT_QUERY_LIMIT = 1000;

/**
* Default druid location in classpath
*/
String DRUID_LOCATION = "lib/sqlparser/druid.jar";
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,12 @@ private S loadExtension(ClassLoader loader, Class<?>[] argTypes,
loadAllExtensionClass(loader);
ExtensionDefinition<S> defaultExtensionDefinition = getDefaultExtensionDefinition();
return getExtensionInstance(defaultExtensionDefinition, loader, argTypes, args);
} catch (EnhancedServiceNotFoundException e) {
throw e;
} catch (Throwable e) {
if (e instanceof EnhancedServiceNotFoundException) {
throw (EnhancedServiceNotFoundException)e;
} else {
throw new EnhancedServiceNotFoundException(
"not found service provider for : " + type.getName() + " caused by " + ExceptionUtils
.getFullStackTrace(e));
}
throw new EnhancedServiceNotFoundException(
"not found service provider for : " + type.getName()
+ " caused by " + ExceptionUtils.getFullStackTrace(e));
}
}

Expand Down Expand Up @@ -519,7 +517,10 @@ private void loadFile(String dir, ClassLoader loader, List<ExtensionDefinition<S
urls = ClassLoader.getSystemResources(fileName);
}
if (urls != null) {
boolean hasServiceFile = false;
boolean hasClasses = false;
while (urls.hasMoreElements()) {
hasServiceFile = true;
java.net.URL url = urls.nextElement();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), Constants.DEFAULT_CHARSET))) {
String line;
Expand All @@ -530,6 +531,7 @@ private void loadFile(String dir, ClassLoader loader, List<ExtensionDefinition<S
}
line = line.trim();
if (line.length() > 0) {
hasClasses = true;
try {
ExtensionDefinition<S> extensionDefinition = getUnloadedExtensionDefinition(line, loader);
if (extensionDefinition == null) {
Expand All @@ -540,17 +542,29 @@ private void loadFile(String dir, ClassLoader loader, List<ExtensionDefinition<S
}
extensions.add(extensionDefinition);
} catch (LinkageError | ClassNotFoundException e) {
LOGGER.warn("Load [{}] class fail. ", line, e);
LOGGER.warn("Load [{}] class fail.", line, e);
} catch (ClassCastException e) {
LOGGER.error("Load [{}] class fail, please make sure the extension" +
" config in {} implements {}.", line, fileName, type.getName());
}
}
}
} catch (Throwable e) {
LOGGER.warn("load clazz instance error: ", e);
LOGGER.warn("load class instance error:", e);
}
}

if (LOGGER.isDebugEnabled()) {
if (!hasServiceFile) {
LOGGER.warn("Load [{}] class fail: no service files found in '{}'.", type.getName(), dir);
} else if (!hasClasses) {
LOGGER.warn("Load [{}] class fail: the service files in '{}' is all empty.", type.getName(), dir);
}
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.warn("Load [{}] class fail: no urls found in '{}'.", type.getName(), dir);
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions common/src/main/java/io/seata/common/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,46 @@ public static Class<?> getClassByName(String className) throws ClassNotFoundExce
return Class.forName(className, true, Thread.currentThread().getContextClassLoader());
}

/**
* Get the wrapped class
*
* @param clazz the class
* @return the wrapped class
*/
public static Class<?> getWrappedClass(Class<?> clazz) {
if (clazz.isPrimitive()) {
if (clazz.equals(byte.class)) {
return Byte.class;
}
if (clazz.equals(boolean.class)) {
return Boolean.class;
}
if (clazz.equals(char.class)) {
return Character.class;
}
if (clazz.equals(short.class)) {
return Short.class;
}
if (clazz.equals(int.class)) {
return Integer.class;
}
if (clazz.equals(long.class)) {
return Long.class;
}
if (clazz.equals(float.class)) {
return Float.class;
}
if (clazz.equals(double.class)) {
return Double.class;
}
if (clazz.equals(void.class)) {
return Void.class;
}
}

return clazz;
}

//endregion


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ public Configuration proxy(Configuration originalConfiguration) throws Exception
return new ByteBuddy().subclass(Configuration.class).method(ElementMatchers.any())
.intercept(InvocationHandlerAdapter.of((proxy, method, args) -> {
String methodName = method.getName();
if (methodName.startsWith(METHOD_PREFIX) && !method.getName().equalsIgnoreCase(METHOD_LATEST_CONFIG)) {
if (methodName.startsWith(METHOD_PREFIX) && !methodName.equalsIgnoreCase(METHOD_LATEST_CONFIG)) {
String rawDataId = (String)args[0];
ObjectWrapper wrapper = CONFIG_CACHE.get(rawDataId);
ObjectWrapper.ConfigType type =
ObjectWrapper.getTypeByName(method.getName().substring(METHOD_PREFIX.length()));
ObjectWrapper.getTypeByName(methodName.substring(METHOD_PREFIX.length()));
Object defaultValue = null;
if (args.length > 1
&& method.getParameterTypes()[1].getSimpleName().equalsIgnoreCase(type.name())) {
&& method.getParameterTypes()[1].getSimpleName().equalsIgnoreCase(type.name())) {
defaultValue = args[1];
}
if (null == wrapper
|| (null != defaultValue && !Objects.equals(defaultValue, wrapper.lastDefaultValue))) {
|| (null != defaultValue && !Objects.equals(defaultValue, wrapper.lastDefaultValue))) {
Object result = method.invoke(originalConfiguration, args);
// The wrapper.data only exists in the cache when it is not null.
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ private static void load() {
LOGGER.info("load Configuration from :{}", extConfiguration == null ?
configuration.getClass().getSimpleName() : "Spring Configuration");
}
} catch (EnhancedServiceNotFoundException ignore) {

} catch (EnhancedServiceNotFoundException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.warn("failed to load extConfiguration: {}", e.getMessage(), e);
}
} catch (Exception e) {
LOGGER.error("failed to load extConfiguration:{}", e.getMessage(), e);
LOGGER.error("failed to load extConfiguration: {}", e.getMessage(), e);
}
CURRENT_FILE_INSTANCE = extConfiguration == null ? configuration : extConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ public abstract class AbstractTableMetaCache implements TableMetaCache {

private static final long EXPIRE_TIME = 900 * 1000;

private static final Cache<String, TableMeta> TABLE_META_CACHE = Caffeine.newBuilder().maximumSize(CACHE_SIZE)
.expireAfterWrite(EXPIRE_TIME, TimeUnit.MILLISECONDS).softValues().build();
private static final Cache<String, TableMeta> TABLE_META_CACHE;

static {
try {
TABLE_META_CACHE = Caffeine.newBuilder().maximumSize(CACHE_SIZE)
.expireAfterWrite(EXPIRE_TIME, TimeUnit.MILLISECONDS).softValues().build();
} catch (Throwable t) {
LOGGER.error("Build the `TABLE_META_CACHE` failed:", t);
throw t;
}
}


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static String buildResourceId(String jdbcUrl) {
}

public static Driver loadDriver(String driverClassName) throws SQLException {
Class clazz = null;
Class<?> clazz = null;
try {
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
if (contextLoader != null) {
Expand All @@ -124,9 +124,7 @@ public static Driver loadDriver(String driverClassName) throws SQLException {

try {
return (Driver)clazz.newInstance();
} catch (IllegalAccessException e) {
throw new SQLException(e.getMessage(), e);
} catch (InstantiationException e) {
} catch (IllegalAccessException | InstantiationException e) {
throw new SQLException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class ShutdownProperties {
/**
* when destroy server, wait seconds
*/
private long wait = DEFAULT_SHUTDOWN_TIMEOUT_SEC;
private int wait = DEFAULT_SHUTDOWN_TIMEOUT_SEC;

public long getWait() {
public int getWait() {
return wait;
}

public ShutdownProperties setWait(long wait) {
public ShutdownProperties setWait(int wait) {
this.wait = wait;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
@ConfigurationProperties(prefix = CONFIG_ZK_PREFIX)
public class ConfigZooKeeperProperties {
private String serverAddr;
private long sessionTimeout = 6000L;
private long connectTimeout = 2000L;
private int sessionTimeout = 6000;
private int connectTimeout = 2000;
private String username;
private String password;
private String nodePath = "/seata/seata.properties";
Expand All @@ -46,16 +46,16 @@ public long getSessionTimeout() {
return sessionTimeout;
}

public ConfigZooKeeperProperties setSessionTimeout(long sessionTimeout) {
public ConfigZooKeeperProperties setSessionTimeout(int sessionTimeout) {
this.sessionTimeout = sessionTimeout;
return this;
}

public long getConnectTimeout() {
public int getConnectTimeout() {
return connectTimeout;
}

public ConfigZooKeeperProperties setConnectTimeout(long connectTimeout) {
public ConfigZooKeeperProperties setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public class RegistryZooKeeperProperties {
private String cluster = "default";
private String serverAddr = "127.0.0.1:2181";
private long sessionTimeout = 6000L;
private long connectTimeout = 2000L;
private int sessionTimeout = 6000;
private int connectTimeout = 2000;
private String username;
private String password;

Expand All @@ -51,20 +51,20 @@ public RegistryZooKeeperProperties setServerAddr(String serverAddr) {
return this;
}

public long getSessionTimeout() {
public int getSessionTimeout() {
return sessionTimeout;
}

public RegistryZooKeeperProperties setSessionTimeout(long sessionTimeout) {
public RegistryZooKeeperProperties setSessionTimeout(int sessionTimeout) {
this.sessionTimeout = sessionTimeout;
return this;
}

public long getConnectTimeout() {
public int getConnectTimeout() {
return connectTimeout;
}

public RegistryZooKeeperProperties setConnectTimeout(long connectTimeout) {
public RegistryZooKeeperProperties setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
Expand Down
Loading

0 comments on commit 72347f2

Please sign in to comment.