Skip to content

Commit ff1fa8f

Browse files
authored
optimize: optimize NacosConfiguration singleton reload (#6763)
1 parent b7fc0c1 commit ff1fa8f

File tree

13 files changed

+405
-50
lines changed

13 files changed

+405
-50
lines changed

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Add changes here for all PR submitted to the 2.x branch.
5757
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] optimize ConsistentHashLoadBalance Algorithm
5858
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] optimize fastjson deserialization
5959
- [[#6755](https://github.com/apache/incubator-seata/pull/6755)] optimize namingserver code logic
60+
- [[#6763](https://github.com/apache/incubator-seata/pull/6763)] optimize NacosConfiguration singleton reload
6061
- [[#6761](https://github.com/apache/incubator-seata/pull/6761)] optimize the namingserver code to improve readability
6162

6263

changes/zh-cn/2.x.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] 优化 ConsistentHashLoadBalance 算法
5959
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] 优化 fastjson 反序列化
6060
- [[#6755](https://github.com/apache/incubator-seata/pull/6755)] 优化namingserver代码逻辑
61+
- [[#6763](https://github.com/apache/incubator-seata/pull/6763)] 优化 NacosConfiguration 单例加载
6162
- [[#6761](https://github.com/apache/incubator-seata/pull/6761)] 提升namingserver manager代码可读性
6263

64+
6365
### refactor:
6466

6567

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.seata.config;
18+
19+
/**
20+
* The interface Dispose.
21+
*/
22+
public interface Dispose {
23+
/**
24+
* Dispose.
25+
*/
26+
void dispose();
27+
}

config/seata-config-nacos/src/main/java/org/apache/seata/config/nacos/NacosConfiguration.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.seata.config.ConfigurationChangeListener;
3939
import org.apache.seata.config.ConfigurationFactory;
4040
import org.apache.seata.config.ConfigurationKeys;
41+
import org.apache.seata.config.Dispose;
4142
import org.apache.seata.config.processor.ConfigProcessor;
4243
import org.slf4j.Logger;
4344
import org.slf4j.LoggerFactory;
@@ -47,7 +48,7 @@
4748
* The type Nacos configuration.
4849
*
4950
*/
50-
public class NacosConfiguration extends AbstractConfiguration {
51+
public class NacosConfiguration extends AbstractConfiguration implements Dispose {
5152
private static volatile NacosConfiguration instance;
5253

5354
private static final Logger LOGGER = LoggerFactory.getLogger(NacosConfiguration.class);
@@ -66,10 +67,10 @@ public class NacosConfiguration extends AbstractConfiguration {
6667
private static final String RAM_ROLE_NAME_KEY = "ramRoleName";
6768
private static final String USE_PARSE_RULE = "false";
6869
private static final String CONTEXT_PATH = "contextPath";
69-
private static final Configuration FILE_CONFIG = ConfigurationFactory.CURRENT_FILE_INSTANCE;
70+
private Configuration fileConfig = ConfigurationFactory.CURRENT_FILE_INSTANCE;
7071
private static volatile ConfigService configService;
7172
private static final int MAP_INITIAL_CAPACITY = 8;
72-
private static final ConcurrentMap<String, ConcurrentMap<ConfigurationChangeListener, NacosListener>> CONFIG_LISTENERS_MAP
73+
private static volatile ConcurrentMap<String, ConcurrentMap<ConfigurationChangeListener, NacosListener>> CONFIG_LISTENERS_MAP
7374
= new ConcurrentHashMap<>(MAP_INITIAL_CAPACITY);
7475
private static volatile Properties seataConfig = new Properties();
7576

@@ -203,14 +204,14 @@ public Set<ConfigurationChangeListener> getConfigListeners(String dataId) {
203204
}
204205
}
205206

206-
private static Properties getConfigProperties() {
207+
private Properties getConfigProperties() {
207208
Properties properties = new Properties();
208209
properties.setProperty(ConfigurationKeys.IS_USE_CLOUD_NAMESPACE_PARSING, USE_PARSE_RULE);
209210
properties.setProperty(ConfigurationKeys.IS_USE_ENDPOINT_PARSING_RULE, USE_PARSE_RULE);
210211
if (System.getProperty(PRO_SERVER_ADDR_KEY) != null) {
211212
properties.setProperty(PRO_SERVER_ADDR_KEY, System.getProperty(PRO_SERVER_ADDR_KEY));
212213
} else {
213-
String address = FILE_CONFIG.getConfig(getNacosAddrFileKey());
214+
String address = fileConfig.getConfig(getNacosAddrFileKey());
214215
if (address != null) {
215216
properties.setProperty(PRO_SERVER_ADDR_KEY, address);
216217
}
@@ -219,7 +220,7 @@ private static Properties getConfigProperties() {
219220
if (System.getProperty(PRO_NAMESPACE_KEY) != null) {
220221
properties.setProperty(PRO_NAMESPACE_KEY, System.getProperty(PRO_NAMESPACE_KEY));
221222
} else {
222-
String namespace = FILE_CONFIG.getConfig(getNacosNameSpaceFileKey());
223+
String namespace = fileConfig.getConfig(getNacosNameSpaceFileKey());
223224
if (namespace == null) {
224225
namespace = DEFAULT_NAMESPACE;
225226
}
@@ -228,7 +229,7 @@ private static Properties getConfigProperties() {
228229
if (!initNacosAuthProperties(properties)) {
229230
LOGGER.info("Nacos config auth properties empty.");
230231
}
231-
String contextPath = StringUtils.isNotBlank(System.getProperty(CONTEXT_PATH)) ? System.getProperty(CONTEXT_PATH) : FILE_CONFIG.getConfig(getNacosContextPathKey());
232+
String contextPath = StringUtils.isNotBlank(System.getProperty(CONTEXT_PATH)) ? System.getProperty(CONTEXT_PATH) : fileConfig.getConfig(getNacosContextPathKey());
232233
if (StringUtils.isNotBlank(contextPath)) {
233234
properties.setProperty(CONTEXT_PATH, contextPath);
234235
}
@@ -242,21 +243,21 @@ private static Properties getConfigProperties() {
242243
* @param sourceProperties the source properties
243244
* @return auth properties
244245
*/
245-
private static boolean initNacosAuthProperties(Properties sourceProperties) {
246-
String userName = StringUtils.isNotBlank(System.getProperty(USER_NAME)) ? System.getProperty(USER_NAME) : FILE_CONFIG.getConfig(getNacosUserName());
246+
private boolean initNacosAuthProperties(Properties sourceProperties) {
247+
String userName = StringUtils.isNotBlank(System.getProperty(USER_NAME)) ? System.getProperty(USER_NAME) : fileConfig.getConfig(getNacosUserName());
247248
if (StringUtils.isNotBlank(userName)) {
248-
String password = StringUtils.isNotBlank(System.getProperty(PASSWORD)) ? System.getProperty(PASSWORD) : FILE_CONFIG.getConfig(getNacosPassword());
249+
String password = StringUtils.isNotBlank(System.getProperty(PASSWORD)) ? System.getProperty(PASSWORD) : fileConfig.getConfig(getNacosPassword());
249250
if (StringUtils.isNotBlank(password)) {
250251
sourceProperties.setProperty(USER_NAME, userName);
251252
sourceProperties.setProperty(PASSWORD, password);
252253
LOGGER.info("Nacos check auth with userName/password.");
253254
return true;
254255
}
255256
} else {
256-
String accessKey = StringUtils.isNotBlank(System.getProperty(ACCESS_KEY)) ? System.getProperty(ACCESS_KEY) : FILE_CONFIG.getConfig(getNacosAccessKey());
257-
String ramRoleName = StringUtils.isNotBlank(System.getProperty(RAM_ROLE_NAME_KEY)) ? System.getProperty(RAM_ROLE_NAME_KEY) : FILE_CONFIG.getConfig(getNacosRamRoleNameKey());
257+
String accessKey = StringUtils.isNotBlank(System.getProperty(ACCESS_KEY)) ? System.getProperty(ACCESS_KEY) : fileConfig.getConfig(getNacosAccessKey());
258+
String ramRoleName = StringUtils.isNotBlank(System.getProperty(RAM_ROLE_NAME_KEY)) ? System.getProperty(RAM_ROLE_NAME_KEY) : fileConfig.getConfig(getNacosRamRoleNameKey());
258259
if (StringUtils.isNotBlank(accessKey)) {
259-
String secretKey = StringUtils.isNotBlank(System.getProperty(SECRET_KEY)) ? System.getProperty(SECRET_KEY) : FILE_CONFIG.getConfig(getNacosSecretKey());
260+
String secretKey = StringUtils.isNotBlank(System.getProperty(SECRET_KEY)) ? System.getProperty(SECRET_KEY) : fileConfig.getConfig(getNacosSecretKey());
260261
if (StringUtils.isNotBlank(secretKey)) {
261262
sourceProperties.put(ACCESS_KEY, accessKey);
262263
sourceProperties.put(SECRET_KEY, secretKey);
@@ -310,15 +311,15 @@ public static String getNacosRamRoleNameKey() {
310311
return String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR, ConfigurationKeys.FILE_ROOT_CONFIG, CONFIG_TYPE, RAM_ROLE_NAME_KEY);
311312
}
312313

313-
private static String getNacosGroup() {
314-
return FILE_CONFIG.getConfig(getNacosGroupKey(), DEFAULT_GROUP);
314+
private String getNacosGroup() {
315+
return fileConfig.getConfig(getNacosGroupKey(), DEFAULT_GROUP);
315316
}
316317

317-
private static String getNacosDataId() {
318-
return FILE_CONFIG.getConfig(getNacosDataIdKey(), DEFAULT_DATA_ID);
318+
private String getNacosDataId() {
319+
return fileConfig.getConfig(getNacosDataIdKey(), DEFAULT_DATA_ID);
319320
}
320321

321-
private static String getNacosDataType() {
322+
private String getNacosDataType() {
322323
return ConfigProcessor.resolverConfigDataType(getNacosDataId());
323324
}
324325

@@ -339,7 +340,7 @@ private static String getNacosContextPathKey() {
339340
return String.join(ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR, ConfigurationKeys.FILE_ROOT_CONFIG, CONFIG_TYPE, CONTEXT_PATH);
340341
}
341342

342-
private static void initSeataConfig() {
343+
private void initSeataConfig() {
343344
try {
344345
String nacosDataId = getNacosDataId();
345346
String config = configService.getConfig(nacosDataId, getNacosGroup(), DEFAULT_CONFIG_TIMEOUT);
@@ -360,10 +361,27 @@ public String getTypeName() {
360361
return CONFIG_TYPE;
361362
}
362363

364+
@Override
365+
public void dispose() {
366+
if (null != CONFIG_LISTENERS_MAP) {
367+
CONFIG_LISTENERS_MAP.clear();
368+
}
369+
if (null != seataConfig) {
370+
seataConfig.clear();
371+
}
372+
if (null != configService) {
373+
configService = null;
374+
}
375+
if (null != instance) {
376+
instance = null;
377+
}
378+
fileConfig = ConfigurationFactory.CURRENT_FILE_INSTANCE;
379+
}
380+
363381
/**
364382
* Non-blocking subscriptions prohibit adding subscriptions in the thread pool to prevent thread termination
365383
*/
366-
public static class NacosListener extends AbstractSharedListener {
384+
public class NacosListener extends AbstractSharedListener {
367385
private final String dataId;
368386
private final ConfigurationChangeListener listener;
369387

config/seata-config-nacos/src/test/java/io/seata/config/nacos/NacosConfiguration.java renamed to config/seata-config-nacos/src/test/java/io/seata/config/extend/NacosConfiguration.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package io.seata.config.nacos;
17+
package io.seata.config.extend;
1818

1919
import java.io.IOException;
2020
import java.util.Enumeration;
@@ -23,8 +23,6 @@
2323
import java.util.Set;
2424
import java.util.concurrent.ConcurrentHashMap;
2525
import java.util.concurrent.ConcurrentMap;
26-
import java.util.concurrent.Executor;
27-
import java.util.concurrent.Executors;
2826

2927
import com.alibaba.nacos.api.NacosFactory;
3028
import com.alibaba.nacos.api.config.ConfigService;
@@ -245,7 +243,6 @@ public static String getNacosDataIdKey() {
245243
NACOS_DATA_ID_KEY);
246244
}
247245

248-
249246
private static String getNacosGroup() {
250247
return FILE_CONFIG.getString(getNacosGroupKey());
251248
}
@@ -316,17 +313,17 @@ public void innerReceive(String dataId, String group, String configInfo) {
316313
}
317314
}
318315
//Get all the monitored dataids and judge whether it has been modified
319-
for (Map.Entry<String, ConcurrentMap<ConfigurationChangeListener, NacosListener>> entry : CONFIG_LISTENERS_MAP.entrySet()) {
316+
for (Map.Entry<String, ConcurrentMap<ConfigurationChangeListener, NacosListener>> entry :
317+
CONFIG_LISTENERS_MAP.entrySet()) {
320318
String listenedDataId = entry.getKey();
321319
String propertyOld = seataConfig.getProperty(listenedDataId, "");
322320
String propertyNew = seataConfigNew.getProperty(listenedDataId, "");
323321
if (!propertyOld.equals(propertyNew)) {
324-
ConfigurationChangeEvent event =
325-
new ConfigurationChangeEvent().setDataId(listenedDataId).setNewValue(propertyNew)
326-
.setNamespace(group);
322+
ConfigurationChangeEvent event = new ConfigurationChangeEvent().setDataId(listenedDataId)
323+
.setNewValue(propertyNew).setNamespace(group);
327324

328-
ConcurrentMap<ConfigurationChangeListener, NacosListener> configListeners =
329-
entry.getValue();
325+
ConcurrentMap<ConfigurationChangeListener, NacosListener> configListeners
326+
= entry.getValue();
330327
for (ConfigurationChangeListener configListener : configListeners.keySet()) {
331328
configListener.onProcessEvent(event);
332329
}
@@ -340,8 +337,8 @@ public void innerReceive(String dataId, String group, String configInfo) {
340337
LOGGER.error("innerReceive error: {}", e.getMessage(), e);
341338
}
342339
//Compatible with old writing
343-
ConfigurationChangeEvent event =
344-
new ConfigurationChangeEvent().setDataId(dataId).setNewValue(configInfo).setNamespace(group);
340+
ConfigurationChangeEvent event = new ConfigurationChangeEvent().setDataId(dataId).setNewValue(configInfo)
341+
.setNamespace(group);
345342
listener.onProcessEvent(event);
346343
}
347344
}

config/seata-config-nacos/src/test/java/io/seata/config/nacos/NacosConfigurationProvider.java renamed to config/seata-config-nacos/src/test/java/io/seata/config/extend/NacosConfigurationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package io.seata.config.nacos;
17+
package io.seata.config.extend;
1818

1919
import io.seata.config.Configuration;
2020
import org.apache.seata.common.loader.LoadLevel;

config/seata-config-nacos/src/test/java/io/seata/config/nacos/TestConfigCustomSPI.java renamed to config/seata-config-nacos/src/test/java/io/seata/config/extend/TestConfigFromExtendSPI.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package io.seata.config.nacos;
17+
package io.seata.config.extend;
1818

1919
import java.security.SecureRandom;
20-
import java.util.Properties;
2120
import java.util.Set;
2221
import java.util.concurrent.CountDownLatch;
23-
import java.util.concurrent.Executor;
24-
import java.util.concurrent.Executors;
2522
import java.util.concurrent.TimeUnit;
2623

2724
import com.alibaba.nacos.api.NacosFactory;
2825
import com.alibaba.nacos.api.config.ConfigService;
29-
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
3026
import com.alibaba.nacos.api.exception.NacosException;
3127

3228
import com.typesafe.config.Config;
@@ -42,7 +38,7 @@
4238
import org.junit.jupiter.api.BeforeAll;
4339
import org.junit.jupiter.api.Test;
4440

45-
public class TestConfigCustomSPI {
41+
public class TestConfigFromExtendSPI {
4642

4743
private static Config FILE_CONFIG;
4844
private static ConfigService configService;
@@ -67,7 +63,7 @@ public void testGetConfigProperties() throws Exception {
6763
Assertions.assertNotNull(configService);
6864
Configuration configuration = ConfigurationFactory.getInstance();
6965
String postfix = generateRandomString();
70-
String dataId = "nacos.config.custom.spi." + postfix;
66+
String dataId = "nacos.config.extension.spi." + postfix;
7167
String group = FILE_CONFIG.getString("config.test.group");
7268
String content = "seata";
7369
CountDownLatch listenerCountDown = new CountDownLatch(1);

config/seata-config-nacos/src/test/java/org/apache/seata/config/nacos/NacosConfigurationTest.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,45 @@
1919
import java.lang.reflect.Method;
2020
import java.util.Properties;
2121

22+
import com.alibaba.nacos.api.exception.NacosException;
23+
2224
import org.apache.seata.common.util.ReflectionUtil;
23-
import org.assertj.core.api.Assertions;
25+
import org.apache.seata.config.Configuration;
26+
import org.apache.seata.config.ConfigurationFactory;
27+
import org.apache.seata.config.Dispose;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.BeforeAll;
2430
import org.junit.jupiter.api.Test;
2531

26-
2732
/**
2833
* The type Nacos configuration test
29-
*
3034
*/
3135
public class NacosConfigurationTest {
3236

37+
private static Configuration configuration;
38+
39+
@BeforeAll
40+
public static void setup() throws NacosException {
41+
System.clearProperty("seataEnv");
42+
configuration = NacosConfiguration.getInstance();
43+
if (configuration instanceof Dispose) {
44+
((Dispose)configuration).dispose();
45+
}
46+
ConfigurationFactory.reload();
47+
configuration = NacosConfiguration.getInstance();
48+
}
49+
3350
@Test
3451
public void testGetConfigProperties() throws Exception {
52+
Assertions.assertNotNull(configuration);
3553
Method method = ReflectionUtil.getMethod(NacosConfiguration.class, "getConfigProperties");
36-
Properties properties = (Properties) ReflectionUtil.invokeMethod(null, method);
37-
Assertions.assertThat(properties.getProperty("contextPath")).isEqualTo("/bar");
54+
//do not use `ConfigurationFactory.getInstance()`, it's a proxy object
55+
Properties properties = (Properties)method.invoke(configuration);
56+
Assertions.assertEquals("/bar", properties.getProperty("contextPath"));
3857
System.setProperty("contextPath", "/foo");
39-
properties = (Properties) ReflectionUtil.invokeMethod(null, method);
40-
Assertions.assertThat(properties.getProperty("contextPath")).isEqualTo("/foo");
58+
properties = (Properties)method.invoke(configuration);
59+
Assertions.assertEquals("/foo", properties.getProperty("contextPath"));
60+
System.clearProperty("contextPath");
4161
}
4262

43-
4463
}

0 commit comments

Comments
 (0)