Commit 6a5f57e6 by Jason Song

store parse result in cache

parent b3eb51f5
......@@ -86,7 +86,8 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
}
if (value == null && m_configProperties.get() == null) {
logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace);
logger.warn("Could not load config for namespace {} from Apollo, please check whether the configs are released " +
"in Apollo! Return default value now!", m_namespace);
}
return value == null ? defaultValue : value;
......@@ -138,6 +139,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
//2. update m_configProperties
m_configProperties.set(newConfigProperties);
clearConfigCache();
//3. use getProperty to update configChange's new value and calc the final changes
for (ConfigChange change : configChanges) {
......
......@@ -90,6 +90,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
});
m_configProperties = newConfigProperties;
clearConfigCache();
this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap));
......
......@@ -29,6 +29,10 @@ public class ConfigUtil {
private String cluster;
private int loadConfigQPS = 2; //2 times per second
private int longPollQPS = 2; //2 times per second
//for typed config cache of parser result, e.g. integer, double, long, etc.
private long maxConfigCacheSize = 500;//500 cache key
private long configCacheExpireTime = 1;//1 minute
private TimeUnit configCacheExpireTimeUnit = TimeUnit.MINUTES;//1 minute
public ConfigUtil() {
initRefreshInterval();
......@@ -36,6 +40,7 @@ public class ConfigUtil {
initReadTimeout();
initCluster();
initQPS();
initMaxConfigCacheSize();
}
/**
......@@ -47,7 +52,8 @@ public class ConfigUtil {
String appId = Foundation.app().getAppId();
if (Strings.isNullOrEmpty(appId)) {
appId = ConfigConsts.NO_APPID_PLACEHOLDER;
logger.warn("app.id is not set, please make sure it is set in classpath:/META-INF/app.properties, now apollo will only load public namespace configurations!");
logger.warn("app.id is not set, please make sure it is set in classpath:/META-INF/app.properties, now apollo " +
"will only load public namespace configurations!");
}
return appId;
}
......@@ -228,4 +234,27 @@ public class ConfigUtil {
}
return osName.startsWith("Windows");
}
private void initMaxConfigCacheSize() {
String customizedConfigCacheSize = System.getProperty("apollo.configCacheSize");
if (!Strings.isNullOrEmpty(customizedConfigCacheSize)) {
try {
maxConfigCacheSize = Long.valueOf(customizedConfigCacheSize);
} catch (Throwable ex) {
logger.error("Config for apollo.configCacheSize is invalid: {}", customizedConfigCacheSize);
}
}
}
public long getMaxConfigCacheSize() {
return maxConfigCacheSize;
}
public long getConfigCacheExpireTime() {
return configCacheExpireTime;
}
public TimeUnit getConfigCacheExpireTimeUnit() {
return configCacheExpireTimeUnit;
}
}
package com.ctrip.framework.apollo.util.function;
import com.google.common.base.Function;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
import com.ctrip.framework.apollo.util.parser.ParserException;
import com.ctrip.framework.apollo.util.parser.Parsers;
import java.util.Date;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface Functions {
Function<String, Integer> TO_INT_FUNCTION = new Function<String, Integer>() {
@Override
public Integer apply(String input) {
return Integer.parseInt(input);
}
};
Function<String, Long> TO_LONG_FUNCTION = new Function<String, Long>() {
@Override
public Long apply(String input) {
return Long.parseLong(input);
}
};
Function<String, Short> TO_SHORT_FUNCTION = new Function<String, Short>() {
@Override
public Short apply(String input) {
return Short.parseShort(input);
}
};
Function<String, Float> TO_FLOAT_FUNCTION = new Function<String, Float>() {
@Override
public Float apply(String input) {
return Float.parseFloat(input);
}
};
Function<String, Double> TO_DOUBLE_FUNCTION = new Function<String, Double>() {
@Override
public Double apply(String input) {
return Double.parseDouble(input);
}
};
Function<String, Byte> TO_BYTE_FUNCTION = new Function<String, Byte>() {
@Override
public Byte apply(String input) {
return Byte.parseByte(input);
}
};
Function<String, Boolean> TO_BOOLEAN_FUNCTION = new Function<String, Boolean>() {
@Override
public Boolean apply(String input) {
return Boolean.parseBoolean(input);
}
};
Function<String, Date> TO_DATE_FUNCTION = new Function<String, Date>() {
@Override
public Date apply(String input) {
try {
return Parsers.forDate().parse(input);
} catch (ParserException ex) {
throw new ApolloConfigException("Parse date failed", ex);
}
}
};
Function<String, Long> TO_DURATION_FUNCTION = new Function<String, Long>() {
@Override
public Long apply(String input) {
try {
return Parsers.forDuration().parseToMillis(input);
} catch (ParserException ex) {
throw new ApolloConfigException("Parse duration failed", ex);
}
}
};
}
......@@ -56,6 +56,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "1003171";
someClusterName = "someClusterName";
......
......@@ -23,6 +23,7 @@ public class ConfigServiceTest extends ComponentTestCase {
@Override
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "someAppId";
//as ConfigService is singleton, so we must manually clear its container
......
......@@ -26,6 +26,7 @@ public class DefaultConfigManagerTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
defineComponent(ConfigFactoryManager.class, MockConfigManager.class);
defaultConfigManager = (DefaultConfigManager) lookup(ConfigManager.class);
......
......@@ -12,10 +12,12 @@ import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.util.ConfigUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import java.io.File;
import java.util.Calendar;
......@@ -26,12 +28,14 @@ import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class DefaultConfigTest {
public class DefaultConfigTest extends ComponentTestCase {
private File someResourceDir;
private String someNamespace;
private ConfigRepository configRepository;
......@@ -39,6 +43,10 @@ public class DefaultConfigTest {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
defineComponent(ConfigUtil.class, MockConfigUtil.class);
someResourceDir = new File(ClassLoaderUtil.getClassPath() + "/META-INF/config");
someResourceDir.mkdirs();
someNamespace = "someName";
......@@ -47,6 +55,7 @@ public class DefaultConfigTest {
@After
public void tearDown() throws Exception {
super.tearDown();
recursiveDelete(someResourceDir);
}
......@@ -131,6 +140,120 @@ public class DefaultConfigTest {
}
@Test
public void testGetIntPropertyMultipleTimesWithCache() throws Exception {
String someKey = "someKey";
Integer someValue = 2;
Integer someDefaultValue = -1;
//set up config repo
someProperties = mock(Properties.class);
when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue));
when(configRepository.getConfig()).thenReturn(someProperties);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
verify(someProperties, times(1)).getProperty(someKey);
}
@Test
public void testGetIntPropertyMultipleTimesWithPropertyChanges() throws Exception {
String someKey = "someKey";
Integer someValue = 2;
Integer anotherValue = 3;
Integer someDefaultValue = -1;
//set up config repo
someProperties = new Properties();
someProperties.setProperty(someKey, String.valueOf(someValue));
when(configRepository.getConfig()).thenReturn(someProperties);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
Properties anotherProperties = new Properties();
anotherProperties.setProperty(someKey, String.valueOf(anotherValue));
defaultConfig.onRepositoryChange(someNamespace, anotherProperties);
assertEquals(anotherValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
}
@Test
public void testGetIntPropertyMultipleTimesWithSmallCache() throws Exception {
String someKey = "someKey";
Integer someValue = 2;
String anotherKey = "anotherKey";
Integer anotherValue = 3;
Integer someDefaultValue = -1;
defineComponent(ConfigUtil.class, MockConfigUtilWithSmallCache.class);
//set up config repo
someProperties = mock(Properties.class);
when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue));
when(someProperties.getProperty(anotherKey)).thenReturn(String.valueOf(anotherValue));
when(configRepository.getConfig()).thenReturn(someProperties);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
verify(someProperties, times(1)).getProperty(someKey);
assertEquals(anotherValue, defaultConfig.getIntProperty(anotherKey, someDefaultValue));
assertEquals(anotherValue, defaultConfig.getIntProperty(anotherKey, someDefaultValue));
verify(someProperties, times(1)).getProperty(anotherKey);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
verify(someProperties, times(2)).getProperty(someKey);
}
@Test
public void testGetIntPropertyMultipleTimesWithShortExpireTime() throws Exception {
String someKey = "someKey";
Integer someValue = 2;
Integer someDefaultValue = -1;
defineComponent(ConfigUtil.class, MockConfigUtilWithShortExpireTime.class);
//set up config repo
someProperties = mock(Properties.class);
when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue));
when(configRepository.getConfig()).thenReturn(someProperties);
DefaultConfig defaultConfig =
new DefaultConfig(someNamespace, configRepository);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
verify(someProperties, times(1)).getProperty(someKey);
TimeUnit.MILLISECONDS.sleep(50);
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
verify(someProperties, times(2)).getProperty(someKey);
}
@Test
public void testGetLongProperty() throws Exception {
String someStringKey = "someStringKey";
String someStringValue = "someStringValue";
......@@ -288,7 +411,8 @@ public class DefaultConfigTest {
new DefaultConfig(someNamespace, configRepository);
assertArrayEquals(values, defaultConfig.getArrayProperty(someKey, someDelimiter, someDefaultValue));
assertArrayEquals(someDefaultValue, defaultConfig.getArrayProperty(someKey, someInvalidDelimiter, someDefaultValue));
assertArrayEquals(someDefaultValue, defaultConfig.getArrayProperty(someKey, someInvalidDelimiter,
someDefaultValue));
}
@Test
......@@ -311,9 +435,11 @@ public class DefaultConfigTest {
new DefaultConfig(someNamespace, configRepository);
checkDatePropertyWithFormat(defaultConfig, shortDate, "shortDateProperty", "yyyy-MM-dd", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, mediumDate, "mediumDateProperty", "yyyy-MM-dd HH:mm:ss", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, mediumDate, "mediumDateProperty", "yyyy-MM-dd HH:mm:ss",
someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, shortDate, "mediumDateProperty", "yyyy-MM-dd", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, longDate, "longDateProperty", "yyyy-MM-dd HH:mm:ss.SSS", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, longDate, "longDateProperty", "yyyy-MM-dd HH:mm:ss.SSS",
someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, mediumDate, "longDateProperty", "yyyy-MM-dd HH:mm:ss", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, shortDate, "longDateProperty", "yyyy-MM-dd", someDefaultValue);
checkDatePropertyWithFormat(defaultConfig, someDefaultValue, "stringProperty", "yyyy-MM-dd", someDefaultValue);
......@@ -460,7 +586,8 @@ public class DefaultConfigTest {
assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType());
}
private void checkDatePropertyWithFormat(Config config, Date expected, String propertyName, String format, Date defaultValue) {
private void checkDatePropertyWithFormat(Config config, Date expected, String propertyName, String format, Date
defaultValue) {
assertEquals(expected, config.getDateProperty(propertyName, format, defaultValue));
}
......@@ -479,4 +606,40 @@ public class DefaultConfigTest {
private enum SomeEnum {
someValue, defaultValue
}
public static class MockConfigUtil extends ConfigUtil {
@Override
public long getMaxConfigCacheSize() {
return 10;
}
@Override
public long getConfigCacheExpireTime() {
return 1;
}
@Override
public TimeUnit getConfigCacheExpireTimeUnit() {
return TimeUnit.MINUTES;
}
}
public static class MockConfigUtilWithSmallCache extends MockConfigUtil {
@Override
public long getMaxConfigCacheSize() {
return 1;
}
}
public static class MockConfigUtilWithShortExpireTime extends MockConfigUtil {
@Override
public long getConfigCacheExpireTime() {
return 50;
}
@Override
public TimeUnit getConfigCacheExpireTimeUnit() {
return TimeUnit.MILLISECONDS;
}
}
}
......@@ -42,6 +42,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someBaseDir = new File("src/test/resources/config-cache");
someBaseDir.mkdir();
......
......@@ -58,6 +58,7 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
defineComponent(ConfigUtil.class, MockConfigUtil.class);
......
......@@ -59,6 +59,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someNamespace = "someName";
......
......@@ -21,6 +21,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
defineComponent(ConfigRegistry.class, MockConfigRegistry.class);
defaultConfigFactoryManager =
......
......@@ -40,6 +40,7 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
someAppId = "someId";
someEnv = Env.DEV;
......
......@@ -20,6 +20,7 @@ public class DefaultConfigRegistryTest extends ComponentTestCase {
@Before
public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp();
defaultConfigRegistry = (DefaultConfigRegistry) lookup(ConfigRegistry.class);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment