diff --git a/apollo-adminservice/src/main/resources/logback.xml b/apollo-adminservice/src/main/resources/logback.xml index 85bbe82e3221cf983c2ca5d665459e3f1dcc8ba3..bb1f023b39189ba605c1aab5d9cef72914a4f985 100644 --- a/apollo-adminservice/src/main/resources/logback.xml +++ b/apollo-adminservice/src/main/resources/logback.xml @@ -4,7 +4,9 @@ + + diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java index 73ffbb8a4c2179b1c0d6b7ae4c6f1cee8f7c346c..acfec1a4995f9485d43bc4a7e815cdba6cdb9901 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollService.java @@ -103,9 +103,18 @@ public class RemoteConfigLongPollService { final String appId = m_configUtil.getAppId(); final String cluster = m_configUtil.getCluster(); final String dataCenter = m_configUtil.getDataCenter(); + final long longPollingInitialDelayInMills = m_configUtil.getLongPollingInitialDelayInMills(); m_longPollingService.submit(new Runnable() { @Override public void run() { + if (longPollingInitialDelayInMills > 0) { + try { + logger.debug("Long polling will start in {} ms.", longPollingInitialDelayInMills); + TimeUnit.MILLISECONDS.sleep(longPollingInitialDelayInMills); + } catch (InterruptedException e) { + //ignore + } + } doLongPollingRefresh(appId, cluster, dataCenter); } }); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java index 57420487028cf017e468c2f7cb210664fc8d692b..2c3414f81caee00067a7b443b5074a1a872a85f0 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java @@ -18,7 +18,6 @@ import com.google.common.base.Strings; */ public class ConfigUtil { private static final Logger logger = LoggerFactory.getLogger(ConfigUtil.class); - private static final String TOOLING_CLUSTER = "tooling"; private int refreshInterval = 5; private TimeUnit refreshIntervalTimeUnit = TimeUnit.MINUTES; private int connectTimeout = 1000; //1 second @@ -33,6 +32,7 @@ public class ConfigUtil { private long maxConfigCacheSize = 500;//500 cache key private long configCacheExpireTime = 1;//1 minute private TimeUnit configCacheExpireTimeUnit = TimeUnit.MINUTES;//1 minute + private long longPollingInitialDelayInMills = 2000;//2 seconds public ConfigUtil() { initRefreshInterval(); @@ -41,6 +41,7 @@ public class ConfigUtil { initCluster(); initQPS(); initMaxConfigCacheSize(); + initLongPollingInitialDelayInMills(); } /** @@ -71,19 +72,6 @@ public class ConfigUtil { //Load data center from system property cluster = System.getProperty(ConfigConsts.APOLLO_CLUSTER_KEY); - String env = Foundation.server().getEnvType(); - //LPT and DEV will be treated as a cluster(lower case) - if (Strings.isNullOrEmpty(cluster) && - (Env.DEV.name().equalsIgnoreCase(env) || Env.LPT.name().equalsIgnoreCase(env)) - ) { - cluster = env.toLowerCase(); - } - - //Use TOOLING cluster if tooling=true in server.properties - if (Strings.isNullOrEmpty(cluster) && isToolingZone()) { - cluster = TOOLING_CLUSTER; - } - //Use data center as cluster if (Strings.isNullOrEmpty(cluster)) { cluster = getDataCenter(); @@ -95,11 +83,6 @@ public class ConfigUtil { } } - private boolean isToolingZone() { - //do not use the new isTooling method since it might not be available in the client side - return "true".equalsIgnoreCase(Foundation.server().getProperty("tooling", "false").trim()); - } - /** * Get the cluster name for the current application. * @@ -265,4 +248,19 @@ public class ConfigUtil { public TimeUnit getConfigCacheExpireTimeUnit() { return configCacheExpireTimeUnit; } + + private void initLongPollingInitialDelayInMills() { + String customizedLongPollingInitialDelay = System.getProperty("apollo.longPollingInitialDelayInMills"); + if (!Strings.isNullOrEmpty(customizedLongPollingInitialDelay)) { + try { + longPollingInitialDelayInMills = Long.valueOf(customizedLongPollingInitialDelay); + } catch (Throwable ex) { + logger.error("Config for apollo.longPollingInitialDelayInMills is invalid: {}", customizedLongPollingInitialDelay); + } + } + } + + public long getLongPollingInitialDelayInMills() { + return longPollingInitialDelayInMills; + } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java index 93798492e7ed5101e8feb292190754712c9391cc..9d89d0888dc15f4d2c500229a4465dd0f5826940 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/AllTests.java @@ -22,6 +22,7 @@ import com.ctrip.framework.apollo.spring.JavaConfigAnnotationTest; import com.ctrip.framework.apollo.spring.JavaConfigPlaceholderTest; import com.ctrip.framework.apollo.spring.XMLConfigAnnotationTest; import com.ctrip.framework.apollo.spring.XmlConfigPlaceholderTest; +import com.ctrip.framework.apollo.util.ConfigUtilTest; import com.ctrip.framework.apollo.util.ExceptionUtilTest; import com.ctrip.framework.apollo.util.parser.DateParserTest; import com.ctrip.framework.apollo.util.parser.DurationParserTest; @@ -34,7 +35,7 @@ import com.ctrip.framework.apollo.util.parser.DurationParserTest; ConfigIntegrationTest.class, ExceptionUtilTest.class, XmlConfigFileTest.class, PropertiesConfigFileTest.class, RemoteConfigLongPollServiceTest.class, DateParserTest.class, DurationParserTest.class, JsonConfigFileTest.class, XmlConfigPlaceholderTest.class, JavaConfigPlaceholderTest.class, XMLConfigAnnotationTest.class, - JavaConfigAnnotationTest.class + JavaConfigAnnotationTest.class, ConfigUtilTest.class }) public class AllTests { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java index 351d16c1fe219cc543e8aa0dbf03c691081e653f..c12281eda1fbec8f4a77a5f3cacbf3bc262be29e 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/BaseIntegrationTest.java @@ -191,6 +191,11 @@ public abstract class BaseIntegrationTest{ public TimeUnit getOnErrorRetryIntervalTimeUnit() { return TimeUnit.MILLISECONDS; } + + @Override + public long getLongPollingInitialDelayInMills() { + return 0; + } } /** diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java index 5a56df94112a26b74fc4dd0c0c803e69a75175c0..c86f79746328d5be066830092e0988112a935067 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigLongPollServiceTest.java @@ -62,6 +62,7 @@ public class RemoteConfigLongPollServiceTest { MockInjector.setInstance(HttpUtil.class, httpUtil); + someServerUrl = "http://someServer"; ServiceDTO serviceDTO = mock(ServiceDTO.class); when(serviceDTO.getHomepageUrl()).thenReturn(someServerUrl); when(configServiceLocator.getConfigServices()).thenReturn(Lists.newArrayList(serviceDTO)); @@ -74,7 +75,6 @@ public class RemoteConfigLongPollServiceTest { responseType = (Type) ReflectionTestUtils.getField(remoteConfigLongPollService, "m_responseType"); - someServerUrl = "http://someServer"; someAppId = "someAppId"; someCluster = "someCluster"; } @@ -367,6 +367,11 @@ public class RemoteConfigLongPollServiceTest { public int getLongPollQPS() { return 200; } + + @Override + public long getLongPollingInitialDelayInMills() { + return 0; + } } } diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java index 81937a93cf21d56466315a8c1a338d2a5745bf0a..6641819722f085b341f641ad8e331499a3c90de4 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java @@ -250,6 +250,11 @@ public class RemoteConfigRepositoryTest { public TimeUnit getOnErrorRetryIntervalTimeUnit() { return TimeUnit.MILLISECONDS; } + + @Override + public long getLongPollingInitialDelayInMills() { + return 0; + } } public static class MockHttpUtil extends HttpUtil { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ConfigUtilTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ConfigUtilTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ab9dac9c6cd9a9c9f491d14cd9b75b558666ad46 --- /dev/null +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/util/ConfigUtilTest.java @@ -0,0 +1,176 @@ +package com.ctrip.framework.apollo.util; + +import com.ctrip.framework.apollo.core.ConfigConsts; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Jason Song(song_s@ctrip.com) + */ +public class ConfigUtilTest { + @After + public void tearDown() throws Exception { + System.clearProperty(ConfigConsts.APOLLO_CLUSTER_KEY); + System.clearProperty("apollo.connectTimeout"); + System.clearProperty("apollo.readTimeout"); + System.clearProperty("apollo.refreshInterval"); + System.clearProperty("apollo.loadConfigQPS"); + System.clearProperty("apollo.longPollQPS"); + System.clearProperty("apollo.configCacheSize"); + System.clearProperty("apollo.longPollingInitialDelayInMills"); + } + + @Test + public void testApolloCluster() throws Exception { + String someCluster = "someCluster"; + System.setProperty(ConfigConsts.APOLLO_CLUSTER_KEY, someCluster); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someCluster, configUtil.getCluster()); + } + + @Test + public void testCustomizeConnectTimeout() throws Exception { + int someConnectTimeout = 1; + System.setProperty("apollo.connectTimeout", String.valueOf(someConnectTimeout)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someConnectTimeout, configUtil.getConnectTimeout()); + } + + @Test + public void testCustomizeInvalidConnectTimeout() throws Exception { + String someInvalidConnectTimeout = "a"; + System.setProperty("apollo.connectTimeout", someInvalidConnectTimeout); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getConnectTimeout() > 0); + } + + @Test + public void testCustomizeReadTimeout() throws Exception { + int someReadTimeout = 1; + System.setProperty("apollo.readTimeout", String.valueOf(someReadTimeout)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someReadTimeout, configUtil.getReadTimeout()); + } + + @Test + public void testCustomizeInvalidReadTimeout() throws Exception { + String someInvalidReadTimeout = "a"; + System.setProperty("apollo.readTimeout", someInvalidReadTimeout); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getReadTimeout() > 0); + } + + @Test + public void testCustomizeRefreshInterval() throws Exception { + int someRefreshInterval = 1; + System.setProperty("apollo.refreshInterval", String.valueOf(someRefreshInterval)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someRefreshInterval, configUtil.getRefreshInterval()); + } + + @Test + public void testCustomizeInvalidRefreshInterval() throws Exception { + String someInvalidRefreshInterval = "a"; + System.setProperty("apollo.refreshInterval", someInvalidRefreshInterval); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getRefreshInterval() > 0); + } + + @Test + public void testCustomizeLoadConfigQPS() throws Exception { + int someQPS = 1; + System.setProperty("apollo.loadConfigQPS", String.valueOf(someQPS)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someQPS, configUtil.getLoadConfigQPS()); + } + + @Test + public void testCustomizeInvalidLoadConfigQPS() throws Exception { + String someInvalidQPS = "a"; + System.setProperty("apollo.loadConfigQPS", someInvalidQPS); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getLoadConfigQPS() > 0); + } + + @Test + public void testCustomizeLongPollQPS() throws Exception { + int someQPS = 1; + System.setProperty("apollo.longPollQPS", String.valueOf(someQPS)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someQPS, configUtil.getLongPollQPS()); + } + + @Test + public void testCustomizeInvalidLongPollQPS() throws Exception { + String someInvalidQPS = "a"; + System.setProperty("apollo.longPollQPS", someInvalidQPS); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getLongPollQPS() > 0); + } + + @Test + public void testCustomizeMaxConfigCacheSize() throws Exception { + long someCacheSize = 1; + System.setProperty("apollo.configCacheSize", String.valueOf(someCacheSize)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someCacheSize, configUtil.getMaxConfigCacheSize()); + } + + @Test + public void testCustomizeInvalidMaxConfigCacheSize() throws Exception { + String someInvalidCacheSize = "a"; + System.setProperty("apollo.configCacheSize", someInvalidCacheSize); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getMaxConfigCacheSize() > 0); + } + + @Test + public void testCustomizeLongPollingInitialDelayInMills() throws Exception { + long someLongPollingDelayInMills = 1; + System.setProperty("apollo.longPollingInitialDelayInMills", String.valueOf(someLongPollingDelayInMills)); + + ConfigUtil configUtil = new ConfigUtil(); + + assertEquals(someLongPollingDelayInMills, configUtil.getLongPollingInitialDelayInMills()); + } + + @Test + public void testCustomizeInvalidLongPollingInitialDelayInMills() throws Exception { + String someInvalidLongPollingDelayInMills = "a"; + System.setProperty("apollo.longPollingInitialDelayInMills", someInvalidLongPollingDelayInMills); + + ConfigUtil configUtil = new ConfigUtil(); + + assertTrue(configUtil.getLongPollingInitialDelayInMills() > 0); + } +} \ No newline at end of file diff --git a/apollo-configservice/src/main/resources/logback.xml b/apollo-configservice/src/main/resources/logback.xml index 3e3b2623649e282804e566cc7ac418b80bfe19e4..26d53cb6af70762f23d5144868b66e1b9771b214 100644 --- a/apollo-configservice/src/main/resources/logback.xml +++ b/apollo-configservice/src/main/resources/logback.xml @@ -4,7 +4,9 @@ + + diff --git a/apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java b/apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java index 0bbd0723811bb71b4e9a2fd7092d93f902b30bf6..252cc71766f10d485c736483796df757a3101d41 100644 --- a/apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java +++ b/apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/AppNamespaceServiceWithCacheTest.java @@ -50,7 +50,7 @@ public class AppNamespaceServiceWithCacheTest { appNamespaceRepository); ReflectionTestUtils.setField(appNamespaceServiceWithCache, "bizConfig", bizConfig); - scanInterval = 10; + scanInterval = 50; scanIntervalTimeUnit = TimeUnit.MILLISECONDS; when(bizConfig.appNamespaceCacheRebuildInterval()).thenReturn(scanInterval); when(bizConfig.appNamespaceCacheRebuildIntervalTimeUnit()).thenReturn(scanIntervalTimeUnit);