From d5b124d8cdd4d23cc7afb70f97ebc7772ab7c040 Mon Sep 17 00:00:00 2001 From: Xianming Lei <31424839+leixm@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:15:04 +0800 Subject: [PATCH] [CELEBORN-1516] DynamicConfigServiceFactory should support singleton ### What changes were proposed in this pull request? DynamicConfigServiceFactory supports singleton. ### Why are the changes needed? Improve code. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Existing UTs. Closes #2635 from leixm/singleton. Authored-by: Xianming Lei <31424839+leixm@users.noreply.github.com> Signed-off-by: Shuang --- .../config/DynamicConfigServiceFactory.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/service/src/main/java/org/apache/celeborn/server/common/service/config/DynamicConfigServiceFactory.java b/service/src/main/java/org/apache/celeborn/server/common/service/config/DynamicConfigServiceFactory.java index 0623d3973..8ee3c38c5 100644 --- a/service/src/main/java/org/apache/celeborn/server/common/service/config/DynamicConfigServiceFactory.java +++ b/service/src/main/java/org/apache/celeborn/server/common/service/config/DynamicConfigServiceFactory.java @@ -22,18 +22,26 @@ import java.io.IOException; import org.apache.celeborn.common.CelebornConf; public class DynamicConfigServiceFactory { + private static volatile ConfigService _INSTANCE; public static ConfigService getConfigService(CelebornConf celebornConf) throws IOException { if (celebornConf.dynamicConfigStoreBackend().isEmpty()) { return null; - } else { - String configStoreBackend = celebornConf.dynamicConfigStoreBackend().get(); - // celeborn.dynamicConfig.store.backend checks value with FS, DB. - if ("FS".equals(configStoreBackend)) { - return new FsConfigServiceImpl(celebornConf); - } else { - return new DbConfigServiceImpl(celebornConf); + } + + if (_INSTANCE == null) { + synchronized (DynamicConfigServiceFactory.class) { + if (_INSTANCE == null) { + String configStoreBackend = celebornConf.dynamicConfigStoreBackend().get(); + if ("FS".equals(configStoreBackend)) { + _INSTANCE = new FsConfigServiceImpl(celebornConf); + } else { + _INSTANCE = new DbConfigServiceImpl(celebornConf); + } + } } } + + return _INSTANCE; } }