Skip to content

Commit 7cbcdd7

Browse files
shwstpprdhslove
authored andcommitted
framework-config: improve configkey caching (apache#10513)
Using a simple hyphen as a delimiter for config cache key can lead to ambiguity if the “name” field itself contains hyphens. To address this, a Ternary object of configkey name, scope and scope ID is used as the config cache keys. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 8f21197 commit 7cbcdd7

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

framework/config/src/main/java/org/apache/cloudstack/framework/config/impl/ConfigDepotImpl.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class ConfigDepotImpl implements ConfigDepot, ConfigDepotAdmin {
8585
List<ScopedConfigStorage> _scopedStorages;
8686
Set<Configurable> _configured = Collections.synchronizedSet(new HashSet<Configurable>());
8787
Set<String> newConfigs = Collections.synchronizedSet(new HashSet<>());
88-
LazyCache<String, String> configCache;
88+
LazyCache<Ternary<String, ConfigKey.Scope, Long>, String> configCache;
8989

9090
private HashMap<String, Pair<String, ConfigKey<?>>> _allKeys = new HashMap<String, Pair<String, ConfigKey<?>>>(1007);
9191

@@ -275,15 +275,10 @@ public ConfigurationDao global() {
275275
return _configDao;
276276
}
277277

278-
protected String getConfigStringValueInternal(String cacheKey) {
279-
String[] parts = cacheKey.split("-");
280-
String key = parts[0];
281-
ConfigKey.Scope scope = ConfigKey.Scope.Global;
282-
Long scopeId = null;
283-
try {
284-
scope = ConfigKey.Scope.valueOf(parts[1]);
285-
scopeId = Long.valueOf(parts[2]);
286-
} catch (IllegalArgumentException ignored) {}
278+
protected String getConfigStringValueInternal(Ternary<String, ConfigKey.Scope, Long> cacheKey) {
279+
String key = cacheKey.first();
280+
ConfigKey.Scope scope = cacheKey.second();
281+
Long scopeId = cacheKey.third();
287282
if (!ConfigKey.Scope.Global.equals(scope) && scopeId != null) {
288283
ScopedConfigStorage scopedConfigStorage = getScopedStorage(scope);
289284
if (scopedConfigStorage == null) {
@@ -298,8 +293,8 @@ protected String getConfigStringValueInternal(String cacheKey) {
298293
return null;
299294
}
300295

301-
private String getConfigCacheKey(String key, ConfigKey.Scope scope, Long scopeId) {
302-
return String.format("%s-%s-%d", key, scope, (scopeId == null ? 0 : scopeId));
296+
protected Ternary<String, ConfigKey.Scope, Long> getConfigCacheKey(String key, ConfigKey.Scope scope, Long scopeId) {
297+
return new Ternary<>(key, scope, scopeId);
303298
}
304299

305300
@Override

framework/config/src/test/java/org/apache/cloudstack/framework/config/impl/ConfigDepotImplTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public void testGetConfigStringValue() {
8989
runTestGetConfigStringValue("test", "value");
9090
}
9191

92+
@Test
93+
public void testGetConfigStringValue_nameWithCharacters() {
94+
runTestGetConfigStringValue("test.1-1", "value");
95+
runTestGetConfigStringValue("test_1#2", "value");
96+
}
97+
9298
private void runTestGetConfigStringValueExpiry(long wait, int configDBRetrieval) {
9399
String key = "test1";
94100
String value = "expiry";

0 commit comments

Comments
 (0)