From 5bbaf6e08a6445681cb8225f77d97c2488445490 Mon Sep 17 00:00:00 2001 From: ayue Date: Thu, 5 Sep 2024 09:26:11 +0800 Subject: [PATCH] fix: Backend Use Nacos cause OutOfMemoryError Signed-off-by: ayue --- .../nacos/NacosBufferedClient.java | 40 ++----------------- .../backend/service/ConfigService.java | 6 --- 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosBufferedClient.java b/sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosBufferedClient.java index 28caf99ae3..a78c8d6b8a 100644 --- a/sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosBufferedClient.java +++ b/sermant-agentcore/sermant-agentcore-implement/src/main/java/io/sermant/implement/service/dynamicconfig/nacos/NacosBufferedClient.java @@ -45,10 +45,6 @@ * @since 2023-08-17 */ public class NacosBufferedClient implements Closeable { - /** - * configService connection status type - */ - public static final String KEY_CONNECTED = "UP"; /** * logger @@ -233,44 +229,16 @@ private Properties createProperties(String connectString, int sessionTimeout, St * @throws NacosInitException Connect to Nacos failed */ private void createNacosClient(String connectString, Properties properties) { + ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); try { - if (!connect(properties)) { - LOGGER.log(Level.SEVERE, "Nacos connection reaches the maximum number of retries"); - throw new NacosInitException(connectString); - } + nacosClient = new NacosClient(properties); } catch (NacosException e) { LOGGER.log(Level.SEVERE, "Nacos connection exception, msg is: {0}", e.getMessage()); throw new NacosInitException(connectString); - } - } - - /** - * Connect to Nacos - * - * @param properties Nacos Client connection configuration - * @return Nacos connection status - * @throws NacosException Nacos initialization exception - */ - private boolean connect(Properties properties) throws NacosException { - int tryNum = 0; - while (tryNum++ <= CONFIG.getConnectRetryTimes()) { - // Nacos client initialization gets the current thread's classloader, which needs to be changed and then - // changed back to the original classloader - ClassLoader tempClassLoader = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); - nacosClient = new NacosClient(properties); + } finally { Thread.currentThread().setContextClassLoader(tempClassLoader); - if (KEY_CONNECTED.equals(nacosClient.getServerStatus())) { - return true; - } - try { - Thread.sleep(CONFIG.getConnectTimeout()); - LOGGER.log(Level.INFO, "The {0} times to retry to connect to nacos", tryNum); - } catch (InterruptedException e) { - LOGGER.log(Level.SEVERE, "Nacos connection sleep exception, msg is: {0}", e.getMessage()); - } } - return false; } @Override diff --git a/sermant-backend/src/main/java/io/sermant/backend/service/ConfigService.java b/sermant-backend/src/main/java/io/sermant/backend/service/ConfigService.java index 7aaf57e40e..19ade77cf1 100644 --- a/sermant-backend/src/main/java/io/sermant/backend/service/ConfigService.java +++ b/sermant-backend/src/main/java/io/sermant/backend/service/ConfigService.java @@ -339,12 +339,6 @@ public void reConnection() { if (ConfigCenterType.ZOOKEEPER.name().equals(dynamicConfig.getDynamicConfigType()) && !configClient.isConnect()) { configClient = new ZooKeeperClient(dynamicConfig.getServerAddress(), dynamicConfig.getTimeout(), watcher); - return; - } - if (ConfigCenterType.NACOS.name().equals(dynamicConfig.getDynamicConfigType()) && !configClient.isConnect()) { - for (Map.Entry entry : CONFIG_CLIENT_MAP.entrySet()) { - createNacosClient(entry.getKey()); - } } } }