Skip to content

Commit 1b30bd7

Browse files
authored
Merge pull request #421 from elandau/bugfix/overrides
config: Fix isLoaded not set in the correct order
2 parents 5356690 + 7084f65 commit 1b30bd7

File tree

3 files changed

+79
-14
lines changed

3 files changed

+79
-14
lines changed

ribbon-archaius/src/main/java/com/netflix/client/config/ArchaiusPropertyResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ private static void invokeAction(Runnable action) {
4444

4545
@Override
4646
public <T> Optional<T> get(String key, Class<T> type) {
47-
LOG.debug("Loading property {}", key);
48-
4947
if (Integer.class.equals(type)) {
5048
return Optional.ofNullable((T) config.getInteger(key, null));
5149
} else if (Boolean.class.equals(type)) {
@@ -70,7 +68,8 @@ public <T> Optional<T> get(String key, Class<T> type) {
7068
.orElseThrow(() -> new IllegalArgumentException("Unable to convert value to desired type " + type));
7169
}
7270
});
73-
} }
71+
}
72+
}
7473

7574
@Override
7675
public void forEach(String prefix, BiConsumer<String, String> consumer) {

ribbon-core/src/main/java/com/netflix/client/config/ReloadableClientConfig.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@ public abstract class ReloadableClientConfig implements IClientConfig {
5959

6060
private boolean isLoaded = false;
6161

62+
/**
63+
* @deprecated Use {@link #ReloadableClientConfig(PropertyResolver, String, String)}
64+
*/
65+
@Deprecated
6266
protected ReloadableClientConfig(PropertyResolver resolver) {
63-
this.clientName = DEFAULT_CLIENT_NAME;
64-
this.resolver = resolver;
67+
this(resolver, DEFAULT_CLIENT_NAME);
6568
}
6669

70+
/**
71+
* @deprecated Use {@link #ReloadableClientConfig(PropertyResolver, String, String)}
72+
*/
73+
@Deprecated
6774
protected ReloadableClientConfig(PropertyResolver resolver, String clientName) {
75+
this(resolver, DEFAULT_NAMESPACE, DEFAULT_CLIENT_NAME);
76+
}
77+
78+
protected ReloadableClientConfig(PropertyResolver resolver, String namespace, String clientName) {
79+
this.namespace = namespace;
6880
this.clientName = clientName;
6981
this.resolver = resolver;
7082
}
@@ -106,14 +118,11 @@ public final void setNameSpace(String nameSpace) {
106118

107119
@Override
108120
public void loadProperties(String clientName) {
109-
Preconditions.checkState(isLoaded == false, "Config '{}' can only be loaded once", clientName);
110-
if (!isLoaded) {
111-
loadDefaultValues();
112-
this.isLoaded = true;
113-
resolver.onChange(this::reload);
114-
}
115-
121+
Preconditions.checkState(!isLoaded, "Config '{}' can only be loaded once", clientName);
116122
this.clientName = clientName;
123+
this.isLoaded = true;
124+
loadDefaultValues();
125+
resolver.onChange(this::reload);
117126
}
118127

119128
@Override
@@ -202,8 +211,6 @@ interface ReloadableProperty<T> extends Property<T> {
202211
}
203212

204213
private <T> ReloadableProperty<T> getClientDynamicProperty(IClientConfigKey<T> key) {
205-
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
206-
207214
return createProperty(
208215
() -> resolveFinalProperty(key),
209216
key::defaultValue);
@@ -295,18 +302,24 @@ public <T> Optional<T> getIfSet(IClientConfigKey<T> key) {
295302

296303
@Override
297304
public final <T> Property<T> getDynamicProperty(IClientConfigKey<T> key) {
305+
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
306+
298307
return getClientDynamicProperty(key);
299308
}
300309

301310
@Override
302311
public <T> Property<T> getScopedProperty(IClientConfigKey<T> key) {
312+
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
313+
303314
return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
304315
() -> resolverScopedProperty(key),
305316
key::defaultValue));
306317
}
307318

308319
@Override
309320
public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) {
321+
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
322+
310323
return (Property<T>) dynamicProperties.computeIfAbsent(key, ignore -> createProperty(
311324
getPrefixedMapPropertySupplier(key),
312325
key::defaultValue));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.netflix.client.config;
2+
3+
import com.netflix.config.ConfigurationManager;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.junit.rules.TestName;
9+
10+
public class ReloadableClientConfigTest {
11+
@Rule
12+
public TestName testName = new TestName();
13+
14+
private CommonClientConfigKey<Integer> testKey;
15+
16+
@Before
17+
public void before() {
18+
this.testKey = new CommonClientConfigKey<Integer>(testName.getMethodName(), -1) {};
19+
}
20+
21+
@Test
22+
public void testOverrideLoadedConfig() {
23+
final DefaultClientConfigImpl overrideconfig = new DefaultClientConfigImpl();
24+
overrideconfig.set(testKey, 123);
25+
26+
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
27+
config.loadDefaultValues();
28+
config.applyOverride(overrideconfig);
29+
30+
Assert.assertEquals(123, config.get(testKey).intValue());
31+
}
32+
33+
@Test
34+
public void setBeforeLoading() {
35+
ConfigurationManager.getConfigInstance().setProperty("ribbon." + testKey.key(), "123");
36+
37+
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
38+
config.loadProperties("foo");
39+
40+
Assert.assertEquals(123, config.get(testKey).intValue());
41+
}
42+
43+
@Test
44+
public void setAfterLoading() {
45+
final DefaultClientConfigImpl config = new DefaultClientConfigImpl();
46+
config.loadProperties("foo");
47+
config.set(testKey, 456);
48+
49+
ConfigurationManager.getConfigInstance().setProperty("ribbon." + testKey.key(), "123");
50+
51+
Assert.assertEquals(123, config.get(testKey).intValue());
52+
}
53+
}

0 commit comments

Comments
 (0)