Skip to content

Commit

Permalink
optimize: optimize ConfigurationCache proxy method (apache#6518)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored May 6, 2024
1 parent 37e7746 commit 2f19941
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 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 @@ -127,6 +127,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#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
- [[#6518](https://github.com/apache/incubator-seata/pull/6518)] optimize ConfigurationCache proxy method
- [[#6458](https://github.com/apache/incubator-seata/pull/6458)] add null value check for MAC address
- [[#6516](https://github.com/apache/incubator-seata/pull/6516)] optimize code format

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 @@ -128,6 +128,7 @@
- [[#6429](https://github.com/apache/incubator-seata/pull/6429)] 移除重复注释
- [[#6405](https://github.com/apache/incubator-seata/pull/6405)] 修复 kotlin 编译失败
- [[#6412](https://github.com/apache/incubator-seata/pull/6412)] 优化 core 兼容模块
- [[#6518](https://github.com/apache/incubator-seata/pull/6518)] 优化 ConfigurationCache 代理方法

### security:
- [[#6069](https://github.com/apache/incubator-seata/pull/6069)] 升级Guava依赖版本,修复安全漏洞
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.seata.config;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -31,10 +32,9 @@
*/
public class ConfigurationCache implements ConfigurationChangeListener {

private static final String METHOD_PREFIX = "get";

private static final String METHOD_LATEST_CONFIG = METHOD_PREFIX + "LatestConfig";
private static final String PROXY_METHOD_PREFIX = "get";

private static final String[] NOT_PROXY_METHOD_NAMES = new String[] {PROXY_METHOD_PREFIX + "LatestConfig", PROXY_METHOD_PREFIX + "ConfigListeners"};
private static final Map<String, ObjectWrapper> CONFIG_CACHE = new ConcurrentHashMap<>();

private static final Set<String> DATA_ID_CACHED = new HashSet<>();
Expand Down Expand Up @@ -72,12 +72,11 @@ public void onChangeEvent(ConfigurationChangeEvent event) {
public Configuration proxy(Configuration originalConfiguration) throws Exception {
return (Configuration)Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{Configuration.class}
, (proxy, method, args) -> {
String methodName = method.getName();
if (methodName.startsWith(METHOD_PREFIX) && !methodName.equalsIgnoreCase(METHOD_LATEST_CONFIG)) {
if (isProxyTargetMethod(method)) {
String rawDataId = (String)args[0];
ObjectWrapper wrapper = CONFIG_CACHE.get(rawDataId);
ObjectWrapper.ConfigType type =
ObjectWrapper.getTypeByName(methodName.substring(METHOD_PREFIX.length()));
ObjectWrapper.getTypeByName(method.getName().substring(PROXY_METHOD_PREFIX.length()));
Object defaultValue = null;
if (args.length > 1
&& method.getParameterTypes()[1].getSimpleName().equalsIgnoreCase(type.name())) {
Expand All @@ -102,6 +101,19 @@ public Configuration proxy(Configuration originalConfiguration) throws Exception
);
}

private boolean isProxyTargetMethod(Method method) {
String methodName = method.getName();
if (!methodName.startsWith(PROXY_METHOD_PREFIX)) {
return false;
}
for (String name : NOT_PROXY_METHOD_NAMES) {
if (methodName.equalsIgnoreCase(name)) {
return false;
}
}
return true;
}

private static class ConfigurationCacheInstance {
private static final ConfigurationCache INSTANCE = new ConfigurationCache();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.seata.config.nacos;

import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -57,10 +58,23 @@ public void testGetConfigProperties() throws Exception {
String dataId = "nacos.config.custom.spi.test";
String group = FILE_CONFIG.getString("config.test.group");
String content = "seata";
CountDownLatch listenerCountDown = new CountDownLatch(1);
configuration.addConfigListener(dataId, new CachedConfigurationChangeListener() {
@Override
public void onChangeEvent(ConfigurationChangeEvent event) {
Assertions.assertEquals(content, event.getNewValue());
listenerCountDown.countDown();
}
});
configService.publishConfig(dataId, group, content);
boolean reachZero = listenerCountDown.await(5, TimeUnit.SECONDS);
Assertions.assertFalse(reachZero);
//get config
String config = configuration.getConfig(dataId);
Assertions.assertEquals(content, config);
//listener
Set<ConfigurationChangeListener> listeners = configuration.getConfigListeners(dataId);
Assertions.assertEquals(2, listeners.size());

}

Expand Down

0 comments on commit 2f19941

Please sign in to comment.