Commit 58351732 by 张乐 Committed by GitHub

Merge pull request #531 from nobodyiam/optimize-client-parse

Optimize client parse performance
parents 34d511fb 97d98f12
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -21,8 +21,8 @@ public class ConfigService {
private static final ConfigService s_instance = new ConfigService();
private PlexusContainer m_container;
private ConfigManager m_configManager;
private ConfigRegistry m_configRegistry;
private volatile ConfigManager m_configManager;
private volatile ConfigRegistry m_configRegistry;
private ConfigService() {
m_container = ContainerLoader.getDefaultContainer();
......@@ -128,8 +128,10 @@ public class ConfigService {
// for test only
static void setContainer(PlexusContainer m_container) {
s_instance.m_container = m_container;
s_instance.m_configManager = null;
s_instance.m_configRegistry = null;
synchronized (s_instance) {
s_instance.m_container = m_container;
s_instance.m_configManager = null;
s_instance.m_configRegistry = null;
}
}
}
......@@ -131,7 +131,7 @@ public class ConfigServiceLocator implements Initializable {
}
try {
TimeUnit.SECONDS.sleep(1);
m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
} catch (InterruptedException ex) {
//ignore
}
......
......@@ -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) {
......
......@@ -228,7 +228,7 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
}
try {
TimeUnit.SECONDS.sleep(1);
m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
} catch (InterruptedException ex) {
//ignore
}
......
......@@ -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,13 @@ public class ConfigUtil {
private String cluster;
private int loadConfigQPS = 2; //2 times per second
private int longPollQPS = 2; //2 times per second
//for on error retry
private long onErrorRetryInterval = 1;//1 second
private TimeUnit onErrorRetryIntervalTimeUnit = TimeUnit.SECONDS;//1 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 +43,7 @@ public class ConfigUtil {
initReadTimeout();
initCluster();
initQPS();
initMaxConfigCacheSize();
}
/**
......@@ -47,7 +55,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;
}
......@@ -206,6 +215,14 @@ public class ConfigUtil {
return longPollQPS;
}
public long getOnErrorRetryInterval() {
return onErrorRetryInterval;
}
public TimeUnit getOnErrorRetryIntervalTimeUnit() {
return onErrorRetryIntervalTimeUnit;
}
public String getDefaultLocalCacheDir() {
String cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s";
return String.format(cacheRoot, getAppId());
......@@ -228,4 +245,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";
......@@ -181,6 +182,16 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
public String getDefaultLocalCacheDir() {
return ClassLoaderUtil.getClassPath();
}
@Override
public long getOnErrorRetryInterval() {
return 10;
}
@Override
public TimeUnit getOnErrorRetryIntervalTimeUnit() {
return TimeUnit.MILLISECONDS;
}
}
/**
......
......@@ -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);
......
......@@ -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";
......@@ -232,6 +233,16 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
public int getLongPollQPS() {
return 200;
}
@Override
public long getOnErrorRetryInterval() {
return 10;
}
@Override
public TimeUnit getOnErrorRetryIntervalTimeUnit() {
return TimeUnit.MILLISECONDS;
}
}
public static class MockConfigServiceLocator extends ConfigServiceLocator {
......
......@@ -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);
}
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -16,7 +16,7 @@ public abstract class Tracer {
private static final Logger logger = LoggerFactory.getLogger(Tracer.class);
private static final MessageProducerManager NULL_MESSAGE_PRODUCER_MANAGER =
new NullMessageProducerManager();
private static MessageProducerManager producerManager;
private static volatile MessageProducerManager producerManager;
private static Object lock = new Object();
static {
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -10,7 +10,7 @@
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.4.1</version>
<version>0.5.0-SNAPSHOT</version>
<name>Apollo</name>
<packaging>pom</packaging>
<description>Ctrip Configuration Center</description>
......
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