Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
2f620e6f
Commit
2f620e6f
authored
Jun 27, 2016
by
张乐
Committed by
GitHub
Jun 27, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #298 from nobodyiam/client-local-config
adjust local cache directory logic
parents
43db6322
51363619
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
13 deletions
+40
-13
LocalFileConfigRepository.java
...framework/apollo/internals/LocalFileConfigRepository.java
+29
-7
ConfigUtil.java
...main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
+5
-0
LocalFileConfigRepositoryTest.java
...ework/apollo/internals/LocalFileConfigRepositoryTest.java
+6
-6
No files found.
apollo-client/src/main/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepository.java
View file @
2f620e6f
...
@@ -25,6 +25,8 @@ import java.io.IOException;
...
@@ -25,6 +25,8 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.Properties
;
import
java.util.Properties
;
/**
/**
...
@@ -55,15 +57,29 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -55,15 +57,29 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
Cat
.
logError
(
ex
);
Cat
.
logError
(
ex
);
throw
new
ApolloConfigException
(
"Unable to load component!"
,
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
;
m_baseDir
=
baseDir
;
this
.
checkLocalConfigCacheDir
(
m_baseDir
);
this
.
checkLocalConfigCacheDir
(
m_baseDir
);
this
.
trySync
();
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
@Override
public
Properties
getConfig
()
{
public
Properties
getConfig
()
{
if
(
m_fileProperties
==
null
)
{
if
(
m_fileProperties
==
null
)
{
...
@@ -98,6 +114,13 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -98,6 +114,13 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
@Override
@Override
protected
void
sync
()
{
protected
void
sync
()
{
//sync with upstream immediately
boolean
syncFromUpstreamResultSuccess
=
trySyncFromUpstream
();
if
(
syncFromUpstreamResultSuccess
)
{
return
;
}
Transaction
transaction
=
Cat
.
newTransaction
(
"Apollo.ConfigService"
,
"syncLocalConfig"
);
Transaction
transaction
=
Cat
.
newTransaction
(
"Apollo.ConfigService"
,
"syncLocalConfig"
);
Throwable
exception
=
null
;
Throwable
exception
=
null
;
try
{
try
{
...
@@ -113,28 +136,27 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
...
@@ -113,28 +136,27 @@ public class LocalFileConfigRepository extends AbstractConfigRepository
transaction
.
complete
();
transaction
.
complete
();
}
}
//sync with fallback immediately
trySyncFromUpstream
();
if
(
m_fileProperties
==
null
)
{
if
(
m_fileProperties
==
null
)
{
throw
new
ApolloConfigException
(
throw
new
ApolloConfigException
(
"Load config from local config failed!"
,
exception
);
"Load config from local config failed!"
,
exception
);
}
}
}
}
private
void
trySyncFromUpstream
()
{
private
boolean
trySyncFromUpstream
()
{
if
(
m_upstream
==
null
)
{
if
(
m_upstream
==
null
)
{
return
;
return
false
;
}
}
try
{
try
{
Properties
properties
=
m_upstream
.
getConfig
();
Properties
properties
=
m_upstream
.
getConfig
();
updateFileProperties
(
properties
);
updateFileProperties
(
properties
);
return
true
;
}
catch
(
Throwable
ex
)
{
}
catch
(
Throwable
ex
)
{
Cat
.
logError
(
ex
);
Cat
.
logError
(
ex
);
logger
logger
.
warn
(
"Sync config from upstream repository {} failed, reason: {}"
,
m_upstream
.
getClass
(),
.
warn
(
"Sync config from upstream repository {} failed, reason: {}"
,
m_upstream
.
getClass
(),
ExceptionUtil
.
getDetailMessage
(
ex
));
ExceptionUtil
.
getDetailMessage
(
ex
));
}
}
return
false
;
}
}
private
synchronized
void
updateFileProperties
(
Properties
newProperties
)
{
private
synchronized
void
updateFileProperties
(
Properties
newProperties
)
{
...
...
apollo-client/src/main/java/com/ctrip/framework/apollo/util/ConfigUtil.java
View file @
2f620e6f
...
@@ -179,4 +179,9 @@ public class ConfigUtil {
...
@@ -179,4 +179,9 @@ public class ConfigUtil {
public
int
getLongPollQPS
()
{
public
int
getLongPollQPS
()
{
return
longPollQPS
;
return
longPollQPS
;
}
}
public
String
getDefaultLocalCacheDir
()
{
//TODO call Framework Foundation to get the default local cache dir
return
String
.
format
(
"/opt/data/%s"
,
getAppId
());
}
}
}
apollo-client/src/test/java/com/ctrip/framework/apollo/internals/LocalFileConfigRepositoryTest.java
View file @
2f620e6f
...
@@ -93,7 +93,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -93,7 +93,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
createLocalCachePropertyFile
(
someProperties
);
createLocalCachePropertyFile
(
someProperties
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
Properties
properties
=
localRepo
.
getConfig
();
Properties
properties
=
localRepo
.
getConfig
();
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
assertEquals
(
someValue
,
properties
.
getProperty
(
someKey
));
...
@@ -109,7 +109,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -109,7 +109,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
Files
.
write
(
defaultKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
Files
.
write
(
defaultKey
+
"="
+
someValue
,
file
,
Charsets
.
UTF_8
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
//when fallback is set, it will try to sync from it
//when fallback is set, it will try to sync from it
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
...
@@ -124,7 +124,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -124,7 +124,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
LocalFileConfigRepository
localFileConfigRepository
=
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
initialize
(
someBaseDir
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
...
@@ -139,7 +139,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -139,7 +139,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
public
void
testLoadConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
public
void
testLoadConfigWithNoLocalFileMultipleTimes
()
throws
Exception
{
LocalFileConfigRepository
localRepo
=
LocalFileConfigRepository
localRepo
=
new
LocalFileConfigRepository
(
someNamespace
);
new
LocalFileConfigRepository
(
someNamespace
);
localRepo
.
initialize
(
someBaseDir
);
localRepo
.
setLocalCacheDir
(
someBaseDir
);
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
localRepo
.
setUpstreamRepository
(
fallbackRepo
);
...
@@ -148,7 +148,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -148,7 +148,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
LocalFileConfigRepository
anotherLocalRepoWithNoFallback
=
anotherLocalRepoWithNoFallback
=
new
LocalFileConfigRepository
(
someNamespace
);
new
LocalFileConfigRepository
(
someNamespace
);
anotherLocalRepoWithNoFallback
.
initialize
(
someBaseDir
);
anotherLocalRepoWithNoFallback
.
setLocalCacheDir
(
someBaseDir
);
Properties
anotherProperties
=
anotherLocalRepoWithNoFallback
.
getConfig
();
Properties
anotherProperties
=
anotherLocalRepoWithNoFallback
.
getConfig
();
...
@@ -164,7 +164,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
...
@@ -164,7 +164,7 @@ public class LocalFileConfigRepositoryTest extends ComponentTestCase {
LocalFileConfigRepository
localFileConfigRepository
=
LocalFileConfigRepository
localFileConfigRepository
=
new
LocalFileConfigRepository
(
someNamespace
);
new
LocalFileConfigRepository
(
someNamespace
);
localFileConfigRepository
.
initialize
(
someBaseDir
);
localFileConfigRepository
.
setLocalCacheDir
(
someBaseDir
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
localFileConfigRepository
.
setUpstreamRepository
(
fallbackRepo
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
localFileConfigRepository
.
addChangeListener
(
someListener
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment