Commit 58351732 authored by 张乐's avatar 张乐 Committed by GitHub

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

Optimize client parse performance
parents 34d511fb 97d98f12
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId> <artifactId>apollo-biz</artifactId>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -21,8 +21,8 @@ public class ConfigService { ...@@ -21,8 +21,8 @@ public class ConfigService {
private static final ConfigService s_instance = new ConfigService(); private static final ConfigService s_instance = new ConfigService();
private PlexusContainer m_container; private PlexusContainer m_container;
private ConfigManager m_configManager; private volatile ConfigManager m_configManager;
private ConfigRegistry m_configRegistry; private volatile ConfigRegistry m_configRegistry;
private ConfigService() { private ConfigService() {
m_container = ContainerLoader.getDefaultContainer(); m_container = ContainerLoader.getDefaultContainer();
...@@ -128,8 +128,10 @@ public class ConfigService { ...@@ -128,8 +128,10 @@ public class ConfigService {
// for test only // for test only
static void setContainer(PlexusContainer m_container) { static void setContainer(PlexusContainer m_container) {
s_instance.m_container = m_container; synchronized (s_instance) {
s_instance.m_configManager = null; s_instance.m_container = m_container;
s_instance.m_configRegistry = null; s_instance.m_configManager = null;
s_instance.m_configRegistry = null;
}
} }
} }
...@@ -131,7 +131,7 @@ public class ConfigServiceLocator implements Initializable { ...@@ -131,7 +131,7 @@ public class ConfigServiceLocator implements Initializable {
} }
try { try {
TimeUnit.SECONDS.sleep(1); m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
//ignore //ignore
} }
......
...@@ -86,7 +86,8 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis ...@@ -86,7 +86,8 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
} }
if (value == null && m_configProperties.get() == null) { 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; return value == null ? defaultValue : value;
...@@ -138,6 +139,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis ...@@ -138,6 +139,7 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
//2. update m_configProperties //2. update m_configProperties
m_configProperties.set(newConfigProperties); m_configProperties.set(newConfigProperties);
clearConfigCache();
//3. use getProperty to update configChange's new value and calc the final changes //3. use getProperty to update configChange's new value and calc the final changes
for (ConfigChange change : configChanges) { for (ConfigChange change : configChanges) {
......
...@@ -228,7 +228,7 @@ public class RemoteConfigRepository extends AbstractConfigRepository { ...@@ -228,7 +228,7 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
} }
try { try {
TimeUnit.SECONDS.sleep(1); m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
//ignore //ignore
} }
......
...@@ -90,6 +90,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList ...@@ -90,6 +90,7 @@ public class SimpleConfig extends AbstractConfig implements RepositoryChangeList
}); });
m_configProperties = newConfigProperties; m_configProperties = newConfigProperties;
clearConfigCache();
this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap)); this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap));
......
...@@ -29,6 +29,13 @@ public class ConfigUtil { ...@@ -29,6 +29,13 @@ public class ConfigUtil {
private String cluster; private String cluster;
private int loadConfigQPS = 2; //2 times per second private int loadConfigQPS = 2; //2 times per second
private int longPollQPS = 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() { public ConfigUtil() {
initRefreshInterval(); initRefreshInterval();
...@@ -36,6 +43,7 @@ public class ConfigUtil { ...@@ -36,6 +43,7 @@ public class ConfigUtil {
initReadTimeout(); initReadTimeout();
initCluster(); initCluster();
initQPS(); initQPS();
initMaxConfigCacheSize();
} }
/** /**
...@@ -47,7 +55,8 @@ public class ConfigUtil { ...@@ -47,7 +55,8 @@ public class ConfigUtil {
String appId = Foundation.app().getAppId(); String appId = Foundation.app().getAppId();
if (Strings.isNullOrEmpty(appId)) { if (Strings.isNullOrEmpty(appId)) {
appId = ConfigConsts.NO_APPID_PLACEHOLDER; 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; return appId;
} }
...@@ -206,6 +215,14 @@ public class ConfigUtil { ...@@ -206,6 +215,14 @@ public class ConfigUtil {
return longPollQPS; return longPollQPS;
} }
public long getOnErrorRetryInterval() {
return onErrorRetryInterval;
}
public TimeUnit getOnErrorRetryIntervalTimeUnit() {
return onErrorRetryIntervalTimeUnit;
}
public String getDefaultLocalCacheDir() { public String getDefaultLocalCacheDir() {
String cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s"; String cacheRoot = isOSWindows() ? "C:\\opt\\data\\%s" : "/opt/data/%s";
return String.format(cacheRoot, getAppId()); return String.format(cacheRoot, getAppId());
...@@ -228,4 +245,27 @@ public class ConfigUtil { ...@@ -228,4 +245,27 @@ public class ConfigUtil {
} }
return osName.startsWith("Windows"); 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 { ...@@ -56,6 +56,7 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
someAppId = "1003171"; someAppId = "1003171";
someClusterName = "someClusterName"; someClusterName = "someClusterName";
...@@ -181,6 +182,16 @@ public abstract class BaseIntegrationTest extends ComponentTestCase { ...@@ -181,6 +182,16 @@ public abstract class BaseIntegrationTest extends ComponentTestCase {
public String getDefaultLocalCacheDir() { public String getDefaultLocalCacheDir() {
return ClassLoaderUtil.getClassPath(); 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 { ...@@ -23,6 +23,7 @@ public class ConfigServiceTest extends ComponentTestCase {
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
someAppId = "someAppId"; someAppId = "someAppId";
//as ConfigService is singleton, so we must manually clear its container //as ConfigService is singleton, so we must manually clear its container
......
...@@ -26,6 +26,7 @@ public class DefaultConfigManagerTest extends ComponentTestCase { ...@@ -26,6 +26,7 @@ public class DefaultConfigManagerTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
defineComponent(ConfigFactoryManager.class, MockConfigManager.class); defineComponent(ConfigFactoryManager.class, MockConfigManager.class);
defaultConfigManager = (DefaultConfigManager) lookup(ConfigManager.class); defaultConfigManager = (DefaultConfigManager) lookup(ConfigManager.class);
......
...@@ -42,6 +42,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase { ...@@ -42,6 +42,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
someBaseDir = new File("src/test/resources/config-cache"); someBaseDir = new File("src/test/resources/config-cache");
someBaseDir.mkdir(); someBaseDir.mkdir();
......
...@@ -58,6 +58,7 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase { ...@@ -58,6 +58,7 @@ public class RemoteConfigLongPollServiceTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
defineComponent(ConfigUtil.class, MockConfigUtil.class); defineComponent(ConfigUtil.class, MockConfigUtil.class);
......
...@@ -59,6 +59,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { ...@@ -59,6 +59,7 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
someNamespace = "someName"; someNamespace = "someName";
...@@ -232,6 +233,16 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase { ...@@ -232,6 +233,16 @@ public class RemoteConfigRepositoryTest extends ComponentTestCase {
public int getLongPollQPS() { public int getLongPollQPS() {
return 200; return 200;
} }
@Override
public long getOnErrorRetryInterval() {
return 10;
}
@Override
public TimeUnit getOnErrorRetryIntervalTimeUnit() {
return TimeUnit.MILLISECONDS;
}
} }
public static class MockConfigServiceLocator extends ConfigServiceLocator { public static class MockConfigServiceLocator extends ConfigServiceLocator {
......
...@@ -21,6 +21,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase { ...@@ -21,6 +21,7 @@ public class DefaultConfigFactoryManagerTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
defineComponent(ConfigRegistry.class, MockConfigRegistry.class); defineComponent(ConfigRegistry.class, MockConfigRegistry.class);
defaultConfigFactoryManager = defaultConfigFactoryManager =
......
...@@ -40,6 +40,7 @@ public class DefaultConfigFactoryTest extends ComponentTestCase { ...@@ -40,6 +40,7 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
someAppId = "someId"; someAppId = "someId";
someEnv = Env.DEV; someEnv = Env.DEV;
......
...@@ -20,6 +20,7 @@ public class DefaultConfigRegistryTest extends ComponentTestCase { ...@@ -20,6 +20,7 @@ public class DefaultConfigRegistryTest extends ComponentTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.tearDown();//clear the container
super.setUp(); super.setUp();
defaultConfigRegistry = (DefaultConfigRegistry) lookup(ConfigRegistry.class); defaultConfigRegistry = (DefaultConfigRegistry) lookup(ConfigRegistry.class);
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -16,7 +16,7 @@ public abstract class Tracer { ...@@ -16,7 +16,7 @@ public abstract class Tracer {
private static final Logger logger = LoggerFactory.getLogger(Tracer.class); private static final Logger logger = LoggerFactory.getLogger(Tracer.class);
private static final MessageProducerManager NULL_MESSAGE_PRODUCER_MANAGER = private static final MessageProducerManager NULL_MESSAGE_PRODUCER_MANAGER =
new NullMessageProducerManager(); new NullMessageProducerManager();
private static MessageProducerManager producerManager; private static volatile MessageProducerManager producerManager;
private static Object lock = new Object(); private static Object lock = new Object();
static { static {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId> <artifactId>apollo-demo</artifactId>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<parent> <parent>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId> <artifactId>apollo</artifactId>
<version>0.4.1</version> <version>0.5.0-SNAPSHOT</version>
<name>Apollo</name> <name>Apollo</name>
<packaging>pom</packaging> <packaging>pom</packaging>
<description>Ctrip Configuration Center</description> <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