Skip to content

Commit

Permalink
adjust local cache directory logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Jun 27, 2016
1 parent 43db632 commit 5136361
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Expand Up @@ -25,6 +25,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

/**
Expand Down Expand Up @@ -55,15 +57,29 @@ public LocalFileConfigRepository(String namespace) {
Cat.logError(ex);
throw new ApolloConfigException("Unable to load component!", ex);
}
this.initialize(new File(ClassLoaderUtil.getClassPath() + CONFIG_DIR));
this.setLocalCacheDir(findLocalCacheDir());
}

void initialize(File baseDir) {
void setLocalCacheDir(File baseDir) {
m_baseDir = baseDir;
this.checkLocalConfigCacheDir(m_baseDir);
this.trySync();
}

private File findLocalCacheDir() {
try {
String defaultCacheDir = m_configUtil.getDefaultLocalCacheDir();
Path path = Paths.get(defaultCacheDir);
if (Files.exists(path) && Files.isWritable(path)) {
return new File(defaultCacheDir, CONFIG_DIR);
}
} catch (Throwable ex) {
//ignore
}

return new File(ClassLoaderUtil.getClassPath(), CONFIG_DIR);
}

@Override
public Properties getConfig() {
if (m_fileProperties == null) {
Expand Down Expand Up @@ -98,6 +114,13 @@ public void onRepositoryChange(String namespace, Properties newProperties) {

@Override
protected void sync() {
//sync with upstream immediately
boolean syncFromUpstreamResultSuccess = trySyncFromUpstream();

if (syncFromUpstreamResultSuccess) {
return;
}

Transaction transaction = Cat.newTransaction("Apollo.ConfigService", "syncLocalConfig");
Throwable exception = null;
try {
Expand All @@ -113,28 +136,27 @@ protected void sync() {
transaction.complete();
}

//sync with fallback immediately
trySyncFromUpstream();

if (m_fileProperties == null) {
throw new ApolloConfigException(
"Load config from local config failed!", exception);
}
}

private void trySyncFromUpstream() {
private boolean trySyncFromUpstream() {
if (m_upstream == null) {
return;
return false;
}
try {
Properties properties = m_upstream.getConfig();
updateFileProperties(properties);
return true;
} catch (Throwable ex) {
Cat.logError(ex);
logger
.warn("Sync config from upstream repository {} failed, reason: {}", m_upstream.getClass(),
ExceptionUtil.getDetailMessage(ex));
}
return false;
}

private synchronized void updateFileProperties(Properties newProperties) {
Expand Down
Expand Up @@ -179,4 +179,9 @@ public int getLoadConfigQPS() {
public int getLongPollQPS() {
return longPollQPS;
}

public String getDefaultLocalCacheDir() {
//TODO call Framework Foundation to get the default local cache dir
return String.format("/opt/data/%s", getAppId());
}
}
Expand Up @@ -93,7 +93,7 @@ public void testLoadConfigWithLocalFile() throws Exception {
createLocalCachePropertyFile(someProperties);

LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someNamespace);
localRepo.initialize(someBaseDir);
localRepo.setLocalCacheDir(someBaseDir);
Properties properties = localRepo.getConfig();

assertEquals(someValue, properties.getProperty(someKey));
Expand All @@ -109,7 +109,7 @@ public void testLoadConfigWithLocalFileAndFallbackRepo() throws Exception {
Files.write(defaultKey + "=" + someValue, file, Charsets.UTF_8);

LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someNamespace);
localRepo.initialize(someBaseDir);
localRepo.setLocalCacheDir(someBaseDir);

//when fallback is set, it will try to sync from it
localRepo.setUpstreamRepository(fallbackRepo);
Expand All @@ -124,7 +124,7 @@ public void testLoadConfigWithNoLocalFile() throws Exception {
LocalFileConfigRepository
localFileConfigRepository =
new LocalFileConfigRepository(someNamespace);
localFileConfigRepository.initialize(someBaseDir);
localFileConfigRepository.setLocalCacheDir(someBaseDir);

localFileConfigRepository.setUpstreamRepository(fallbackRepo);

Expand All @@ -139,7 +139,7 @@ public void testLoadConfigWithNoLocalFile() throws Exception {
public void testLoadConfigWithNoLocalFileMultipleTimes() throws Exception {
LocalFileConfigRepository localRepo =
new LocalFileConfigRepository(someNamespace);
localRepo.initialize(someBaseDir);
localRepo.setLocalCacheDir(someBaseDir);

localRepo.setUpstreamRepository(fallbackRepo);

Expand All @@ -148,7 +148,7 @@ public void testLoadConfigWithNoLocalFileMultipleTimes() throws Exception {
LocalFileConfigRepository
anotherLocalRepoWithNoFallback =
new LocalFileConfigRepository(someNamespace);
anotherLocalRepoWithNoFallback.initialize(someBaseDir);
anotherLocalRepoWithNoFallback.setLocalCacheDir(someBaseDir);

Properties anotherProperties = anotherLocalRepoWithNoFallback.getConfig();

Expand All @@ -164,7 +164,7 @@ public void testOnRepositoryChange() throws Exception {

LocalFileConfigRepository localFileConfigRepository =
new LocalFileConfigRepository(someNamespace);
localFileConfigRepository.initialize(someBaseDir);
localFileConfigRepository.setLocalCacheDir(someBaseDir);
localFileConfigRepository.setUpstreamRepository(fallbackRepo);
localFileConfigRepository.addChangeListener(someListener);

Expand Down

0 comments on commit 5136361

Please sign in to comment.