Skip to content

Commit

Permalink
Merge pull request #429 from elandau/bugfix/reduce_property_resolution
Browse files Browse the repository at this point in the history
Bugfix/reduce property resolution
  • Loading branch information
elandau authored May 23, 2019
2 parents 15cfb49 + 4275e2a commit e590d00
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,47 +301,47 @@ public DefaultClientConfigImpl(String nameSpace) {
}

public void loadDefaultValues() {
set(CommonClientConfigKey.MaxHttpConnectionsPerHost, getDefaultMaxHttpConnectionsPerHost());
set(CommonClientConfigKey.MaxTotalHttpConnections, getDefaultMaxTotalHttpConnections());
set(CommonClientConfigKey.EnableConnectionPool, getDefaultEnableConnectionPool());
set(CommonClientConfigKey.MaxConnectionsPerHost, getDefaultMaxConnectionsPerHost());
set(CommonClientConfigKey.MaxTotalConnections, getDefaultMaxTotalConnections());
set(CommonClientConfigKey.ConnectTimeout, getDefaultConnectTimeout());
set(CommonClientConfigKey.ConnectionManagerTimeout, getDefaultConnectionManagerTimeout());
set(CommonClientConfigKey.ReadTimeout, getDefaultReadTimeout());
set(CommonClientConfigKey.MaxAutoRetries, getDefaultMaxAutoRetries());
set(CommonClientConfigKey.MaxAutoRetriesNextServer, getDefaultMaxAutoRetriesNextServer());
set(CommonClientConfigKey.OkToRetryOnAllOperations, getDefaultOkToRetryOnAllOperations());
set(CommonClientConfigKey.FollowRedirects, getDefaultFollowRedirects());
set(CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled, getDefaultConnectionPoolCleanerTaskEnabled());
set(CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, getDefaultConnectionidleTimeInMsecs());
set(CommonClientConfigKey.ConnectionCleanerRepeatInterval, getDefaultConnectionIdleTimertaskRepeatInMsecs());
set(CommonClientConfigKey.EnableGZIPContentEncodingFilter, getDefaultEnableGzipContentEncodingFilter());
set(CommonClientConfigKey.ProxyHost, null);
set(CommonClientConfigKey.ProxyPort, null);
set(CommonClientConfigKey.Port, getDefaultPort());
set(CommonClientConfigKey.EnablePrimeConnections, getDefaultEnablePrimeConnections());
set(CommonClientConfigKey.MaxRetriesPerServerPrimeConnection, getDefaultMaxRetriesPerServerPrimeConnection());
set(CommonClientConfigKey.MaxTotalTimeToPrimeConnections, getDefaultMaxTotalTimeToPrimeConnections());
set(CommonClientConfigKey.PrimeConnectionsURI, getDefaultPrimeConnectionsUri());
set(CommonClientConfigKey.PoolMinThreads, getDefaultPoolMinThreads());
set(CommonClientConfigKey.PoolMaxThreads, getDefaultPoolMaxThreads());
set(CommonClientConfigKey.PoolKeepAliveTime, (int)getDefaultPoolKeepAliveTime());
set(CommonClientConfigKey.PoolKeepAliveTimeUnits, getDefaultPoolKeepAliveTimeUnits().toString());
set(CommonClientConfigKey.EnableZoneAffinity, getDefaultEnableZoneAffinity());
set(CommonClientConfigKey.EnableZoneExclusivity, getDefaultEnableZoneExclusivity());
set(CommonClientConfigKey.ClientClassName, getDefaultClientClassname());
set(CommonClientConfigKey.NFLoadBalancerClassName, getDefaultNfloadbalancerClassname());
set(CommonClientConfigKey.NFLoadBalancerRuleClassName, getDefaultNfloadbalancerRuleClassname());
set(CommonClientConfigKey.NFLoadBalancerPingClassName, getDefaultNfloadbalancerPingClassname());
set(CommonClientConfigKey.PrioritizeVipAddressBasedServers, getDefaultPrioritizeVipAddressBasedServers());
set(CommonClientConfigKey.MinPrimeConnectionsRatio, getDefaultMinPrimeConnectionsRatio());
set(CommonClientConfigKey.PrimeConnectionsClassName, getDefaultPrimeConnectionsClass());
set(CommonClientConfigKey.NIWSServerListClassName, getDefaultSeverListClass());
set(CommonClientConfigKey.VipAddressResolverClassName, getDefaultVipaddressResolverClassname());
set(CommonClientConfigKey.IsClientAuthRequired, getDefaultIsClientAuthRequired());
set(CommonClientConfigKey.UseIPAddrForServer, getDefaultUseIpAddressForServer());
set(CommonClientConfigKey.ListOfServers, "");
setDefault(CommonClientConfigKey.MaxHttpConnectionsPerHost, getDefaultMaxHttpConnectionsPerHost());
setDefault(CommonClientConfigKey.MaxTotalHttpConnections, getDefaultMaxTotalHttpConnections());
setDefault(CommonClientConfigKey.EnableConnectionPool, getDefaultEnableConnectionPool());
setDefault(CommonClientConfigKey.MaxConnectionsPerHost, getDefaultMaxConnectionsPerHost());
setDefault(CommonClientConfigKey.MaxTotalConnections, getDefaultMaxTotalConnections());
setDefault(CommonClientConfigKey.ConnectTimeout, getDefaultConnectTimeout());
setDefault(CommonClientConfigKey.ConnectionManagerTimeout, getDefaultConnectionManagerTimeout());
setDefault(CommonClientConfigKey.ReadTimeout, getDefaultReadTimeout());
setDefault(CommonClientConfigKey.MaxAutoRetries, getDefaultMaxAutoRetries());
setDefault(CommonClientConfigKey.MaxAutoRetriesNextServer, getDefaultMaxAutoRetriesNextServer());
setDefault(CommonClientConfigKey.OkToRetryOnAllOperations, getDefaultOkToRetryOnAllOperations());
setDefault(CommonClientConfigKey.FollowRedirects, getDefaultFollowRedirects());
setDefault(CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled, getDefaultConnectionPoolCleanerTaskEnabled());
setDefault(CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, getDefaultConnectionidleTimeInMsecs());
setDefault(CommonClientConfigKey.ConnectionCleanerRepeatInterval, getDefaultConnectionIdleTimertaskRepeatInMsecs());
setDefault(CommonClientConfigKey.EnableGZIPContentEncodingFilter, getDefaultEnableGzipContentEncodingFilter());
setDefault(CommonClientConfigKey.ProxyHost, null);
setDefault(CommonClientConfigKey.ProxyPort, null);
setDefault(CommonClientConfigKey.Port, getDefaultPort());
setDefault(CommonClientConfigKey.EnablePrimeConnections, getDefaultEnablePrimeConnections());
setDefault(CommonClientConfigKey.MaxRetriesPerServerPrimeConnection, getDefaultMaxRetriesPerServerPrimeConnection());
setDefault(CommonClientConfigKey.MaxTotalTimeToPrimeConnections, getDefaultMaxTotalTimeToPrimeConnections());
setDefault(CommonClientConfigKey.PrimeConnectionsURI, getDefaultPrimeConnectionsUri());
setDefault(CommonClientConfigKey.PoolMinThreads, getDefaultPoolMinThreads());
setDefault(CommonClientConfigKey.PoolMaxThreads, getDefaultPoolMaxThreads());
setDefault(CommonClientConfigKey.PoolKeepAliveTime, (int)getDefaultPoolKeepAliveTime());
setDefault(CommonClientConfigKey.PoolKeepAliveTimeUnits, getDefaultPoolKeepAliveTimeUnits().toString());
setDefault(CommonClientConfigKey.EnableZoneAffinity, getDefaultEnableZoneAffinity());
setDefault(CommonClientConfigKey.EnableZoneExclusivity, getDefaultEnableZoneExclusivity());
setDefault(CommonClientConfigKey.ClientClassName, getDefaultClientClassname());
setDefault(CommonClientConfigKey.NFLoadBalancerClassName, getDefaultNfloadbalancerClassname());
setDefault(CommonClientConfigKey.NFLoadBalancerRuleClassName, getDefaultNfloadbalancerRuleClassname());
setDefault(CommonClientConfigKey.NFLoadBalancerPingClassName, getDefaultNfloadbalancerPingClassname());
setDefault(CommonClientConfigKey.PrioritizeVipAddressBasedServers, getDefaultPrioritizeVipAddressBasedServers());
setDefault(CommonClientConfigKey.MinPrimeConnectionsRatio, getDefaultMinPrimeConnectionsRatio());
setDefault(CommonClientConfigKey.PrimeConnectionsClassName, getDefaultPrimeConnectionsClass());
setDefault(CommonClientConfigKey.NIWSServerListClassName, getDefaultSeverListClass());
setDefault(CommonClientConfigKey.VipAddressResolverClassName, getDefaultVipaddressResolverClassname());
setDefault(CommonClientConfigKey.IsClientAuthRequired, getDefaultIsClientAuthRequired());
setDefault(CommonClientConfigKey.UseIPAddrForServer, getDefaultUseIpAddressForServer());
setDefault(CommonClientConfigKey.ListOfServers, "");
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public abstract class ReloadableClientConfig implements IClientConfig {

private String namespace = DEFAULT_NAMESPACE;

private boolean isLoaded = false;
private boolean isDynamic = false;

protected ReloadableClientConfig(PropertyResolver resolver) {
this.resolver = resolver;
Expand Down Expand Up @@ -99,15 +99,13 @@ public final void setNameSpace(String nameSpace) {

@Override
public void loadProperties(String clientName) {
Preconditions.checkState(!isLoaded, "Config '{}' can only be loaded once", clientName);

LOG.info("Loading config for `{}`", clientName);
LOG.info("[{}] loading config", clientName);
this.clientName = clientName;
this.isLoaded = true;
this.isDynamic = true;
loadDefaultValues();
resolver.onChange(this::reload);

internalProperties.forEach((key, value) -> LOG.info("{} : {} -> {}", clientName, key, value.orElse(null)));
internalProperties.forEach((key, value) -> LOG.info("[{}] {}={}", clientName, key, value.orElse(null)));
}

/**
Expand Down Expand Up @@ -148,8 +146,8 @@ private <T> void autoRefreshFromPropertyResolver(final IClientConfigKey<T> key)
return () -> {
final Optional<T> next = valueSupplier.get();
if (!next.equals(previous.get())) {
LOG.info("New value {}: {} -> {}", key.key(), previous.get(), next);
previous.set(next);
LOG.info("[{}] new value for {}: {} -> {}", clientName, key.key(), previous.get(), next);
previous.set(next);
internalProperties.put(key, next);
}
};
Expand Down Expand Up @@ -208,7 +206,7 @@ public String toString() {
public final <T> T get(IClientConfigKey<T> key) {
Optional<T> value = (Optional<T>)internalProperties.get(key);
if (value == null) {
if (!isLoaded) {
if (!isDynamic) {
return null;
} else {
set(key, null);
Expand All @@ -221,7 +219,7 @@ public final <T> T get(IClientConfigKey<T> key) {

@Override
public final <T> Property<T> getGlobalProperty(IClientConfigKey<T> key) {
LOG.debug("Get global property {} default {}", key.key(), key.defaultValue());
LOG.debug("[{}] get global property '{}' with default '{}'", clientName, key.key(), key.defaultValue());

return getOrCreateProperty(
key,
Expand All @@ -231,11 +229,9 @@ public final <T> Property<T> getGlobalProperty(IClientConfigKey<T> key) {

@Override
public final <T> Property<T> getDynamicProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
LOG.debug("[{}] get dynamic property key={} ns={}", clientName, key.key(), getNameSpace());

if (isLoaded) {
autoRefreshFromPropertyResolver(key);
}
get(key);

return getOrCreateProperty(
key,
Expand All @@ -245,7 +241,7 @@ public final <T> Property<T> getDynamicProperty(IClientConfigKey<T> key) {

@Override
public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) {
LOG.debug("Get dynamic property key={} ns={} client={}", key.key(), getNameSpace(), clientName);
LOG.debug("[{}] get dynamic property key={} ns={} client={}", clientName, key.key(), getNameSpace());

return getOrCreateProperty(
key,
Expand All @@ -254,14 +250,9 @@ public <T> Property<T> getPrefixMappedProperty(IClientConfigKey<T> key) {
}

/**
* Resolve a properties final value in the following order or precedence
* Resolve a property's final value from the property value.
* - client scope
* - default scope
* - internally set default
* - IClientConfigKey defaultValue
* @param key
* @param <T>
* @return
*/
private <T> Optional<T> resolveFromPropertyResolver(IClientConfigKey<T> key) {
Optional<T> value;
Expand Down Expand Up @@ -353,12 +344,33 @@ public final <T> T get(IClientConfigKey<T> key, T defaultValue) {
return Optional.ofNullable(get(key)).orElse(defaultValue);
}

/**
* Store the implicit default value for key while giving precedence to default values in the property resolver
*/
protected final <T> void setDefault(IClientConfigKey<T> key) {
setDefault(key, key.defaultValue());
}

/**
* Store the default value for key while giving precedence to default values in the property resolver
*/
protected final <T> void setDefault(IClientConfigKey<T> key, T value) {
Preconditions.checkArgument(key != null, "key cannot be null");

value = resolveFromPropertyResolver(key).orElse(value);
internalProperties.put(key, Optional.ofNullable(value));
if (isDynamic) {
autoRefreshFromPropertyResolver(key);
}
cachedToString = null;
}

@Override
public <T> IClientConfig set(IClientConfigKey<T> key, T value) {
Preconditions.checkArgument(key != null, "key cannot be null");

value = resolveValueToType(key, value);
if (isLoaded) {
if (isDynamic) {
internalProperties.put(key, Optional.ofNullable(resolveFromPropertyResolver(key).orElse(value)));
autoRefreshFromPropertyResolver(key);
} else {
Expand Down

0 comments on commit e590d00

Please sign in to comment.