Skip to content

Commit

Permalink
optimize the ConfigCacheFactoryDelegate interface, fix test bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisea committed Nov 21, 2024
1 parent fc7a15a commit 9644edc
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,12 @@ public interface ConfigCacheFactory {
*/
public ConfigCache createConfigCache();

/**
* Create config cache config cache.
*
* @param md5 the md 5
* @param lastModifiedTs the last modified ts
* @return the config cache
*/
public ConfigCache createConfigCache(String md5, long lastModifiedTs);

/**
* Create config cache gray config cache gray.
*
* @param grayName the gray name
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray(String grayName);

/**
* Create config cache gray config cache gray.
*
* @param md5 the md 5
* @param lastModifiedTs the last modified ts
* @param grayRule the gray rule
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray(String md5, long lastModifiedTs, String grayRule);
public ConfigCacheGray createConfigCacheGray();

/**
* Gets config cache factroy name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,56 @@ private ConfigCacheFactoryDelegate() {
}
}

/**
* Gets instance.
*
* @return the instance
*/
public static ConfigCacheFactoryDelegate getInstance() {
return INSTANCE;
}

/**
* Create config cache config cache.
*
* @return the config cache
*/
public ConfigCache createConfigCache() {
return configCacheFactory.createConfigCache();
}

/**
* Create config cache config cache.
*
* @param md5 the md 5
* @param lastModifiedTs the last modified ts
* @return the config cache
*/
public ConfigCache createConfigCache(String md5, long lastModifiedTs) {
return configCacheFactory.createConfigCache(md5, lastModifiedTs);
ConfigCache configCache = this.createConfigCache();
configCache.setMd5(md5);
configCache.setLastModifiedTs(lastModifiedTs);
return configCache;
}

public ConfigCacheGray createConfigCacheGray(String grayName) {
return configCacheFactory.createConfigCacheGray(grayName);
/**
* Create config cache gray config cache gray.
*
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray() {
return configCacheFactory.createConfigCacheGray();
}

public ConfigCacheGray createConfigCacheGray(String md5, long lastModifiedTs, String grayRule) {
return configCacheFactory.createConfigCacheGray(md5, lastModifiedTs, grayRule);
/**
* Create config cache gray config cache gray.
*
* @param grayName the gray name
* @return the config cache gray
*/
public ConfigCacheGray createConfigCacheGray(String grayName) {
ConfigCacheGray configCacheGray = configCacheFactory.createConfigCacheGray();
configCacheGray.setGrayName(grayName);
return configCacheGray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public void clear() {
super.clear();
}

public ConfigCacheGray() {}

public ConfigCacheGray(String grayName) {
this.grayName = grayName;
}
Expand All @@ -49,15 +51,6 @@ public GrayRule getGrayRule() {
return grayRule;
}

public ConfigCacheGray(String md5, long lastModifiedTs, String grayRule)
throws RuntimeException {
super(md5, lastModifiedTs);
this.grayRule = GrayRuleManager.constructGrayRule(GrayRuleManager.deserializeConfigGrayPersistInfo(grayRule));
if (this.grayRule == null || !this.grayRule.isValid()) {
throw new RuntimeException("raw gray rule is invalid");
}
}

public String getGrayName() {
return grayName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Sunrisea
*/
public interface ConfigCacheMd5PostProcessor {
public interface ConfigCachePostProcessor {

/**
* Gets post processor name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@
*
* @author Sunrisea
*/
public class ConfigCacheMd5PostProcessorDelegate {
public class ConfigCachePostProcessorDelegate {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigCacheFactoryDelegate.class);

private static final ConfigCacheMd5PostProcessorDelegate INSTANCE = new ConfigCacheMd5PostProcessorDelegate();
private static final ConfigCachePostProcessorDelegate INSTANCE = new ConfigCachePostProcessorDelegate();

private String configCacheMd5PostProcessorType = EnvUtil.getProperty("nacos.config.cache.type", "nacos");

private ConfigCacheMd5PostProcessor configCacheMd5PostProcessor;
private ConfigCachePostProcessor configCachePostProcessor;

private ConfigCacheMd5PostProcessorDelegate() {
Collection<ConfigCacheMd5PostProcessor> processors = NacosServiceLoader.load(ConfigCacheMd5PostProcessor.class);
for (ConfigCacheMd5PostProcessor processor : processors) {
private ConfigCachePostProcessorDelegate() {
Collection<ConfigCachePostProcessor> processors = NacosServiceLoader.load(ConfigCachePostProcessor.class);
for (ConfigCachePostProcessor processor : processors) {
if (StringUtils.isEmpty(processor.getPostProcessorName())) {
LOGGER.warn(
"[ConfigCacheMd5PostProcessor] Load ConfigCacheMd5PostProcessor({}) PostProcessorName(null/empty) fail. "
"[ConfigCachePostProcessor] Load ConfigCachePostProcessor({}) PostProcessorName(null/empty) fail. "
+ "Please add PostProcessorName to resolve",
processor.getClass().getName());
continue;
}
if (StringUtils.equals(configCacheMd5PostProcessorType, processor.getPostProcessorName())) {
this.configCacheMd5PostProcessor = processor;
this.configCachePostProcessor = processor;
}
}
if (configCacheMd5PostProcessor == null) {
configCacheMd5PostProcessor = new NacosConfigCacheMd5PostProcessor();
if (configCachePostProcessor == null) {
configCachePostProcessor = new NacosConfigCachePostProcessor();
}
}

public static ConfigCacheMd5PostProcessorDelegate getInstance() {
public static ConfigCachePostProcessorDelegate getInstance() {
return INSTANCE;
}

public void postProcess(ConfigCache configCache, String content) {
configCacheMd5PostProcessor.postProcess(configCache, content);
configCachePostProcessor.postProcess(configCache, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,8 @@ public ConfigCache createConfigCache() {
}

@Override
public ConfigCache createConfigCache(String md5, long lastModifiedTs) {
return new ConfigCache(md5, lastModifiedTs);
}

@Override
public ConfigCacheGray createConfigCacheGray(String grayName) {
return new ConfigCacheGray(grayName);
}

@Override
public ConfigCacheGray createConfigCacheGray(String md5, long lastModifiedTs, String grayRule) {
return new ConfigCacheGray(md5, lastModifiedTs, grayRule);
public ConfigCacheGray createConfigCacheGray() {
return new ConfigCacheGray();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Sunrisea
*/
public class NacosConfigCacheMd5PostProcessor implements ConfigCacheMd5PostProcessor {
public class NacosConfigCachePostProcessor implements ConfigCachePostProcessor {

@Override
public String getPostProcessorName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigCache;
import com.alibaba.nacos.config.server.model.ConfigCacheGray;
import com.alibaba.nacos.config.server.model.ConfigCacheMd5PostProcessorDelegate;
import com.alibaba.nacos.config.server.model.ConfigCachePostProcessorDelegate;
import com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent;
import com.alibaba.nacos.config.server.model.gray.GrayRule;
import com.alibaba.nacos.config.server.model.gray.GrayRuleManager;
Expand Down Expand Up @@ -363,7 +363,7 @@ public static void updateMd5(String groupKey, String md5, String content, long l
configCache.setMd5(md5);
configCache.setLastModifiedTs(lastModifiedTs);
configCache.setEncryptedDataKey(encryptedDataKey);
ConfigCacheMd5PostProcessorDelegate.getInstance().postProcess(configCache, content);
ConfigCachePostProcessorDelegate.getInstance().postProcess(configCache, content);
NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey));
}
}
Expand All @@ -389,7 +389,7 @@ public static void updateGrayMd5(String groupKey, String grayName, String grayRu
configCache.setEncryptedDataKey(encryptedDataKey);
configCache.resetGrayRule(grayRule);
cache.sortConfigGray();
ConfigCacheMd5PostProcessorDelegate.getInstance().postProcess(configCache, content);
ConfigCachePostProcessorDelegate.getInstance().postProcess(configCache, content);
NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class ConfigCacheFactoryDelegateTest {
void setUp() {
envUtilMockedStatic = mockStatic(EnvUtil.class);
nacosServiceLoaderMockedStatic = mockStatic(NacosServiceLoader.class);
nacosConfigCacheFactoryMockedConstruction = mockConstruction(NacosConfigCacheFactory.class);
nacosConfigCacheFactoryMockedConstruction = mockConstruction(NacosConfigCacheFactory.class, (mock, context) -> {
when(mock.createConfigCache()).thenReturn(new ConfigCache());
when(mock.createConfigCacheGray()).thenReturn(new ConfigCacheGray());
});
}

@AfterEach
Expand All @@ -64,26 +67,24 @@ void tearDown() {

@Test
public void test() {

when(nacosConfigCacheFactory.getConfigCacheFactoryName()).thenReturn("nacos");
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCacheFactory.class))
.thenReturn(Collections.singletonList(nacosConfigCacheFactory));
envUtilMockedStatic.when(() -> EnvUtil.getProperty("nacos.config.cache.type", "nacos")).thenReturn("lalala");
ConfigCache configCache = ConfigCacheFactoryDelegate.getInstance().createConfigCache();
ConfigCache configCache1 = ConfigCacheFactoryDelegate.getInstance().createConfigCache("md5", 123456789L);
ConfigCacheGray configCacheGray = ConfigCacheFactoryDelegate.getInstance().createConfigCacheGray("grayName");
ConfigCacheGray configCacheGray1 = ConfigCacheFactoryDelegate.getInstance().createConfigCacheGray("md5", 123456789L, "grayRule");
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(1)).createConfigCache();
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(1)).createConfigCache("md5",
123456789L);
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(1)).createConfigCacheGray(
"grayName");
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(1)).createConfigCacheGray("md5",
123456789L, "grayRule");
ConfigCacheGray configCacheGray1 = ConfigCacheFactoryDelegate.getInstance().createConfigCacheGray();
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(2)).createConfigCache();
verify(nacosConfigCacheFactoryMockedConstruction.constructed().get(0), times(2)).createConfigCacheGray();
}

@Test
public void test2() throws Exception {
when(nacosConfigCacheFactory.getConfigCacheFactoryName()).thenReturn("nacos");
when(nacosConfigCacheFactory.createConfigCache()).thenReturn(new ConfigCache());
when(nacosConfigCacheFactory.createConfigCacheGray()).thenReturn(new ConfigCacheGray());
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCacheFactory.class))
.thenReturn(Collections.singletonList(nacosConfigCacheFactory));
envUtilMockedStatic.when(() -> EnvUtil.getProperty("nacos.config.cache.type", "nacos")).thenReturn("nacos");
Expand All @@ -93,10 +94,8 @@ public void test2() throws Exception {
configCacheFactoryDelegate.createConfigCache();
configCacheFactoryDelegate.createConfigCache("md5", 123456789L);
configCacheFactoryDelegate.createConfigCacheGray("grayName");
configCacheFactoryDelegate.createConfigCacheGray("md5", 123456789L, "grayRule");
verify(nacosConfigCacheFactory, times(1)).createConfigCache();
verify(nacosConfigCacheFactory, times(1)).createConfigCache("md5", 123456789L);
verify(nacosConfigCacheFactory, times(1)).createConfigCacheGray("grayName");
verify(nacosConfigCacheFactory, times(1)).createConfigCacheGray("md5", 123456789L, "grayRule");
configCacheFactoryDelegate.createConfigCacheGray();
verify(nacosConfigCacheFactory, times(2)).createConfigCache();
verify(nacosConfigCacheFactory, times(2)).createConfigCacheGray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ConfigCacheMd5PostProcessorDelegateTest {
class ConfigCachePostProcessorDelegateTest {

MockedConstruction<NacosConfigCacheMd5PostProcessor> mockedConstruction;
MockedConstruction<NacosConfigCachePostProcessor> mockedConstruction;

MockedStatic<EnvUtil> envUtilMockedStatic;

MockedStatic<NacosServiceLoader> nacosServiceLoaderMockedStatic;

@Mock
public NacosConfigCacheMd5PostProcessor mockConfigCacheMd5PostProcessor;
public NacosConfigCachePostProcessor mockConfigCacheMd5PostProcessor;

@BeforeEach
void setUp() {
envUtilMockedStatic = mockStatic(EnvUtil.class);
mockedConstruction = mockConstruction(NacosConfigCacheMd5PostProcessor.class);
mockedConstruction = mockConstruction(NacosConfigCachePostProcessor.class);
nacosServiceLoaderMockedStatic = mockStatic(NacosServiceLoader.class);

}
Expand All @@ -71,11 +71,10 @@ void tearDown() {
void test1() {
when(mockConfigCacheMd5PostProcessor.getPostProcessorName()).thenReturn("nacos");
envUtilMockedStatic.when(() -> EnvUtil.getProperty("nacos.config.cache.type", "nacos")).thenReturn("lalala");
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCacheMd5PostProcessor.class))
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCachePostProcessor.class))
.thenReturn(Collections.singletonList(mockConfigCacheMd5PostProcessor));
ConfigCacheMd5PostProcessorDelegate.getInstance().postProcess(null, null);
ConfigCachePostProcessorDelegate.getInstance().postProcess(null, null);
verify(mockConfigCacheMd5PostProcessor, times(0)).postProcess(null, null);
verify(mockedConstruction.constructed().get(0), times(1)).postProcess(null, null);
}

@Test
Expand All @@ -85,18 +84,18 @@ void test2()
when(mockConfigCacheMd5PostProcessor.getPostProcessorName()).thenReturn("nacos");
doNothing().when(mockConfigCacheMd5PostProcessor).postProcess(null, null);
envUtilMockedStatic.when(() -> EnvUtil.getProperty("nacos.config.cache.type", "nacos")).thenReturn("nacos");
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCacheMd5PostProcessor.class))
nacosServiceLoaderMockedStatic.when(() -> NacosServiceLoader.load(ConfigCachePostProcessor.class))
.thenReturn(Collections.singletonList(mockConfigCacheMd5PostProcessor));
Constructor constructor = ConfigCacheMd5PostProcessorDelegate.class.getDeclaredConstructor();
Constructor constructor = ConfigCachePostProcessorDelegate.class.getDeclaredConstructor();
constructor.setAccessible(true);
Field field = ConfigCacheMd5PostProcessorDelegate.class.getDeclaredField("INSTANCE");
Field field = ConfigCachePostProcessorDelegate.class.getDeclaredField("INSTANCE");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
field.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
ConfigCacheMd5PostProcessorDelegate delegate = (ConfigCacheMd5PostProcessorDelegate) constructor.newInstance();
ConfigCachePostProcessorDelegate delegate = (ConfigCachePostProcessorDelegate) constructor.newInstance();
field.set(null, delegate);
ConfigCacheMd5PostProcessorDelegate.getInstance().postProcess(null, null);
ConfigCachePostProcessorDelegate.getInstance().postProcess(null, null);
verify(mockConfigCacheMd5PostProcessor, times(1)).postProcess(null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.alibaba.nacos.config.server.model;

import com.alibaba.nacos.config.server.model.gray.BetaGrayRule;
import com.alibaba.nacos.config.server.model.gray.ConfigGrayPersistInfo;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -30,19 +27,8 @@ public void testCreateConfigCache() {
NacosConfigCacheFactory nacosConfigCacheFactory = new NacosConfigCacheFactory();
ConfigCache configCache = nacosConfigCacheFactory.createConfigCache();
assertEquals(ConfigCache.class, configCache.getClass());
ConfigCache configCache2 = nacosConfigCacheFactory.createConfigCache("md5", 1L);
assertEquals(ConfigCache.class, configCache2.getClass());
assertEquals("md5", configCache2.getMd5());
assertEquals(1L, configCache2.getLastModifiedTs());
ConfigCacheGray configCacheGray = nacosConfigCacheFactory.createConfigCacheGray("grayName");
ConfigCacheGray configCacheGray = nacosConfigCacheFactory.createConfigCacheGray();
assertEquals(ConfigCacheGray.class, configCacheGray.getClass());
assertEquals("grayName", configCacheGray.getGrayName());
ConfigGrayPersistInfo localConfigGrayPersistInfo = new ConfigGrayPersistInfo(BetaGrayRule.TYPE_BETA,
BetaGrayRule.VERSION, "1.1.1.1", Integer.MAX_VALUE);
ConfigCacheGray configCacheGray2 = nacosConfigCacheFactory.createConfigCacheGray("md5", 1L, (new Gson()).toJson(localConfigGrayPersistInfo));
assertEquals(ConfigCacheGray.class, configCacheGray2.getClass());
assertEquals("md5", configCacheGray2.getMd5());
assertEquals(1L, configCacheGray2.getLastModifiedTs());
}

@Test
Expand Down
Loading

0 comments on commit 9644edc

Please sign in to comment.