Commit 5207858e by 张乐 Committed by GitHub

Merge pull request #355 from nobodyiam/client-refactor

Client refactor
parents 4b487478 4df445f3
......@@ -49,6 +49,10 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
* @param namespace the namespace
*/
public LocalFileConfigRepository(String namespace) {
this(namespace, null);
}
public LocalFileConfigRepository(String namespace, ConfigRepository upstream) {
m_namespace = namespace;
m_container = ContainerLoader.getDefaultContainer();
try {
......@@ -57,13 +61,17 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
Cat.logError(ex);
throw new ApolloConfigException("Unable to load component!", ex);
}
this.setLocalCacheDir(findLocalCacheDir());
this.setLocalCacheDir(findLocalCacheDir(), false);
this.setUpstreamRepository(upstream);
this.trySync();
}
void setLocalCacheDir(File baseDir) {
void setLocalCacheDir(File baseDir, boolean syncImmediately) {
m_baseDir = baseDir;
this.checkLocalConfigCacheDir(m_baseDir);
this.trySync();
if (syncImmediately) {
this.trySync();
}
}
private File findLocalCacheDir() {
......@@ -95,6 +103,9 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
public void setUpstreamRepository(ConfigRepository upstreamConfigRepository) {
if (upstreamConfigRepository == null) {
return;
}
//clear previous listener
if (m_upstream != null) {
m_upstream.removeChangeListener(this);
......@@ -118,7 +129,7 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
protected void sync() {
//sync with upstream immediately
boolean syncFromUpstreamResultSuccess = trySyncFromUpstream();
boolean syncFromUpstreamResultSuccess = trySyncFromUpstream();
if (syncFromUpstreamResultSuccess) {
return;
......@@ -252,7 +263,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
} catch (IOException ex) {
ApolloConfigException exception =
new ApolloConfigException(
String.format("Create local config directory %s failed", baseDir.getAbsolutePath()), ex);
String.format("Create local config directory %s failed", baseDir.getAbsolutePath()),
ex);
Cat.logError(exception);
transaction.setStatus(exception);
logger.warn(
......
......@@ -171,6 +171,11 @@ public class RemoteConfigLongPollService implements Initializable {
notify(lastServiceDto, response.getBody());
}
//try to load balance
if (random.nextBoolean()) {
lastServiceDto = null;
}
m_longPollFailSchedulePolicyInSecond.success();
transaction.addData("StatusCode", response.getStatusCode());
transaction.setStatus(Message.SUCCESS);
......
......@@ -46,16 +46,13 @@ public class DefaultConfigFactory implements ConfigFactory {
}
LocalFileConfigRepository createLocalConfigRepository(String namespace) {
LocalFileConfigRepository localFileConfigRepository =
new LocalFileConfigRepository(namespace);
if (m_configUtil.isInLocalMode()) {
logger.warn(
"==== Apollo is in local mode! Won't pull configs from remote server for namespace {} ! ====",
namespace);
} else {
localFileConfigRepository.setUpstreamRepository(createRemoteConfigRepository(namespace));
return new LocalFileConfigRepository(namespace);
}
return localFileConfigRepository;
return new LocalFileConfigRepository(namespace, createRemoteConfigRepository(namespace));
}
RemoteConfigRepository createRemoteConfigRepository(String namespace) {
......
......@@ -33,7 +33,7 @@ import static org.mockito.Mockito.when;
public class LocalFileConfigRepositoryTest extends ComponentTestCase {
private File someBaseDir;
private String someNamespace;
private ConfigRepository fallbackRepo;
private ConfigRepository upstreamRepo;
private Properties someProperties;
private static String someAppId = "someApp";
private static String someCluster = "someCluster";
......@@ -51,8 +51,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
defaultKey = "defaultKey";
defaultValue = "defaultValue";
someProperties.setProperty(defaultKey, defaultValue);
fallbackRepo = mock(ConfigRepository.class);
when(fallbackRepo.getConfig()).thenReturn(someProperties);
upstreamRepo = mock(ConfigRepository.class);
when(upstreamRepo.getConfig()).thenReturn(someProperties);
defineComponent(ConfigUtil.class, MockConfigUtil.class);
}
......@@ -93,7 +93,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
createLocalCachePropertyFile(someProperties);
LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someNamespace);
localRepo.setLocalCacheDir(someBaseDir);
localRepo.setLocalCacheDir(someBaseDir, true);
Properties properties = localRepo.getConfig();
assertEquals(someValue, properties.getProperty(someKey));
......@@ -108,11 +108,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
Files.write(defaultKey + "=" + someValue, file, Charsets.UTF_8);
LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someNamespace);
localRepo.setLocalCacheDir(someBaseDir);
//when fallback is set, it will try to sync from it
localRepo.setUpstreamRepository(fallbackRepo);
LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someNamespace, upstreamRepo);
localRepo.setLocalCacheDir(someBaseDir, true);
Properties properties = localRepo.getConfig();
......@@ -123,10 +120,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
public void testLoadConfigWithNoLocalFile() throws Exception {
LocalFileConfigRepository
localFileConfigRepository =
new LocalFileConfigRepository(someNamespace);
localFileConfigRepository.setLocalCacheDir(someBaseDir);
localFileConfigRepository.setUpstreamRepository(fallbackRepo);
new LocalFileConfigRepository(someNamespace, upstreamRepo);
localFileConfigRepository.setLocalCacheDir(someBaseDir, true);
Properties result = localFileConfigRepository.getConfig();
......@@ -138,17 +133,15 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
@Test
public void testLoadConfigWithNoLocalFileMultipleTimes() throws Exception {
LocalFileConfigRepository localRepo =
new LocalFileConfigRepository(someNamespace);
localRepo.setLocalCacheDir(someBaseDir);
localRepo.setUpstreamRepository(fallbackRepo);
new LocalFileConfigRepository(someNamespace, upstreamRepo);
localRepo.setLocalCacheDir(someBaseDir, true);
Properties someProperties = localRepo.getConfig();
LocalFileConfigRepository
anotherLocalRepoWithNoFallback =
new LocalFileConfigRepository(someNamespace);
anotherLocalRepoWithNoFallback.setLocalCacheDir(someBaseDir);
anotherLocalRepoWithNoFallback.setLocalCacheDir(someBaseDir, true);
Properties anotherProperties = anotherLocalRepoWithNoFallback.getConfig();
......@@ -163,9 +156,8 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
RepositoryChangeListener someListener = mock(RepositoryChangeListener.class);
LocalFileConfigRepository localFileConfigRepository =
new LocalFileConfigRepository(someNamespace);
localFileConfigRepository.setLocalCacheDir(someBaseDir);
localFileConfigRepository.setUpstreamRepository(fallbackRepo);
new LocalFileConfigRepository(someNamespace, upstreamRepo);
localFileConfigRepository.setLocalCacheDir(someBaseDir, true);
localFileConfigRepository.addChangeListener(someListener);
localFileConfigRepository.getConfig();
......
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