Skip to content

Commit 8df1161

Browse files
authored
framework-config: improve configkey caching (#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 653b973 commit 8df1161

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

@@ -273,15 +273,10 @@ public ConfigurationDao global() {
273273
return _configDao;
274274
}
275275

276-
protected String getConfigStringValueInternal(String cacheKey) {
277-
String[] parts = cacheKey.split("-");
278-
String key = parts[0];
279-
ConfigKey.Scope scope = ConfigKey.Scope.Global;
280-
Long scopeId = null;
281-
try {
282-
scope = ConfigKey.Scope.valueOf(parts[1]);
283-
scopeId = Long.valueOf(parts[2]);
284-
} catch (IllegalArgumentException ignored) {}
276+
protected String getConfigStringValueInternal(Ternary<String, ConfigKey.Scope, Long> cacheKey) {
277+
String key = cacheKey.first();
278+
ConfigKey.Scope scope = cacheKey.second();
279+
Long scopeId = cacheKey.third();
285280
if (!ConfigKey.Scope.Global.equals(scope) && scopeId != null) {
286281
ScopedConfigStorage scopedConfigStorage = null;
287282
for (ScopedConfigStorage storage : _scopedStorages) {
@@ -301,8 +296,8 @@ protected String getConfigStringValueInternal(String cacheKey) {
301296
return null;
302297
}
303298

304-
private String getConfigCacheKey(String key, ConfigKey.Scope scope, Long scopeId) {
305-
return String.format("%s-%s-%d", key, scope, (scopeId == null ? 0 : scopeId));
299+
protected Ternary<String, ConfigKey.Scope, Long> getConfigCacheKey(String key, ConfigKey.Scope scope, Long scopeId) {
300+
return new Ternary<>(key, scope, scopeId);
306301
}
307302

308303
@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
@@ -81,6 +81,12 @@ public void testGetConfigStringValue() {
8181
runTestGetConfigStringValue("test", "value");
8282
}
8383

84+
@Test
85+
public void testGetConfigStringValue_nameWithCharacters() {
86+
runTestGetConfigStringValue("test.1-1", "value");
87+
runTestGetConfigStringValue("test_1#2", "value");
88+
}
89+
8490
private void runTestGetConfigStringValueExpiry(long wait, int configDBRetrieval) {
8591
String key = "test1";
8692
String value = "expiry";

0 commit comments

Comments
 (0)