Skip to content

Commit

Permalink
Merge pull request #411 from elandau/feature/config_repo
Browse files Browse the repository at this point in the history
config: PropertyResolver
  • Loading branch information
elandau authored May 11, 2019
2 parents 3c5f0f4 + 21cfdf9 commit 85ae574
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 289 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.netflix.client.config;

import com.netflix.config.ConfigurationManager;
import org.apache.commons.configuration.event.ConfigurationEvent;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class ArchaiusPropertyResolver implements PropertyResolver {
private static final Logger LOG = LoggerFactory.getLogger(ArchaiusPropertyResolver.class);

public static final ArchaiusPropertyResolver INSTANCE = new ArchaiusPropertyResolver();

@Override
public <T> Optional<T> get(String key, Class<T> type) {
LOG.debug("Loading property {}", key);

if (Integer.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getInteger(key, null));
} else if (Boolean.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getBoolean(key, null));
} else if (Float.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getFloat(key, null));
} else if (Long.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getLong(key, null));
} else if (Double.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getDouble(key, null));
} else if (TimeUnit.class.equals(type)) {
return Optional.ofNullable((T) TimeUnit.valueOf(ConfigurationManager.getConfigInstance().getString(key, null)));
} else {
return Optional.ofNullable(ConfigurationManager.getConfigInstance().getStringArray(key))
.filter(ar -> ar.length > 0)
.map(ar -> Arrays.stream(ar).collect(Collectors.joining(",")))
.map(value -> {
if (type.equals(String.class)) {
return (T)value;
} else {
return PropertyResolver.resolveWithValueOf(type, value)
.orElseThrow(() -> new IllegalArgumentException("Unable to convert value to desired type " + type));
}
});
} }

@Override
public void onChange(Runnable action) {
ConfigurationManager.getConfigInstance().addConfigurationListener(new ConfigurationListener() {
@Override
public void configurationChanged(ConfigurationEvent event) {
if (!event.isBeforeUpdate()) {
action.run();
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@
*/
package com.netflix.client.config;

import com.netflix.client.VipAddressResolver;
import com.netflix.config.ConfigurationManager;
import org.apache.commons.configuration.event.ConfigurationEvent;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* Default client configuration that loads properties from Archaius's ConfigurationManager.
Expand Down Expand Up @@ -74,157 +66,11 @@ values are specified in this class as constants.
* @author awang
*
*/
public class DefaultClientConfigImpl extends AbstractReloadableClientConfig {
private static final Logger LOG = LoggerFactory.getLogger(DefaultClientConfigImpl.class);

@Deprecated
public static final Boolean DEFAULT_PRIORITIZE_VIP_ADDRESS_BASED_SERVERS = CommonClientConfigKey.PrioritizeVipAddressBasedServers.defaultValue();

@Deprecated
public static final String DEFAULT_NFLOADBALANCER_PING_CLASSNAME = CommonClientConfigKey.NFLoadBalancerPingClassName.defaultValue();

@Deprecated
public static final String DEFAULT_NFLOADBALANCER_RULE_CLASSNAME = CommonClientConfigKey.NFLoadBalancerRuleClassName.defaultValue();

@Deprecated
public static final String DEFAULT_NFLOADBALANCER_CLASSNAME = CommonClientConfigKey.NFLoadBalancerClassName.defaultValue();

@Deprecated
public static final boolean DEFAULT_USEIPADDRESS_FOR_SERVER = CommonClientConfigKey.UseIPAddrForServer.defaultValue();

@Deprecated
public static final String DEFAULT_CLIENT_CLASSNAME = CommonClientConfigKey.ClientClassName.defaultValue();

@Deprecated
public static final String DEFAULT_VIPADDRESS_RESOLVER_CLASSNAME = CommonClientConfigKey.VipAddressResolverClassName.defaultValue();

@Deprecated
public static final String DEFAULT_PRIME_CONNECTIONS_URI = CommonClientConfigKey.PrimeConnectionsURI.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_TOTAL_TIME_TO_PRIME_CONNECTIONS = CommonClientConfigKey.MaxTotalTimeToPrimeConnections.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_RETRIES_PER_SERVER_PRIME_CONNECTION = CommonClientConfigKey.MaxRetriesPerServerPrimeConnection.defaultValue();

@Deprecated
public static final Boolean DEFAULT_ENABLE_PRIME_CONNECTIONS = CommonClientConfigKey.EnablePrimeConnections.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_REQUESTS_ALLOWED_PER_WINDOW = Integer.MAX_VALUE;

@Deprecated
public static final int DEFAULT_REQUEST_THROTTLING_WINDOW_IN_MILLIS = 60000;

@Deprecated
public static final Boolean DEFAULT_ENABLE_REQUEST_THROTTLING = Boolean.FALSE;

@Deprecated
public static final Boolean DEFAULT_ENABLE_GZIP_CONTENT_ENCODING_FILTER = CommonClientConfigKey.EnableGZIPContentEncodingFilter.defaultValue();

@Deprecated
public static final Boolean DEFAULT_CONNECTION_POOL_CLEANER_TASK_ENABLED = CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled.defaultValue();

@Deprecated
public static final Boolean DEFAULT_FOLLOW_REDIRECTS = CommonClientConfigKey.FollowRedirects.defaultValue();

@Deprecated
public static final float DEFAULT_PERCENTAGE_NIWS_EVENT_LOGGED = 0.0f;

@Deprecated
public static final int DEFAULT_MAX_AUTO_RETRIES_NEXT_SERVER = CommonClientConfigKey.MaxAutoRetriesNextServer.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_AUTO_RETRIES = CommonClientConfigKey.MaxAutoRetries.defaultValue();

@Deprecated
public static final int DEFAULT_BACKOFF_INTERVAL = CommonClientConfigKey.BackoffInterval.defaultValue();

@Deprecated
public static final int DEFAULT_READ_TIMEOUT = CommonClientConfigKey.ReadTimeout.defaultValue();

@Deprecated
public static final int DEFAULT_CONNECTION_MANAGER_TIMEOUT = CommonClientConfigKey.ConnectionManagerTimeout.defaultValue();

@Deprecated
public static final int DEFAULT_CONNECT_TIMEOUT = CommonClientConfigKey.ConnectTimeout.defaultValue();

@Deprecated
public static final Boolean DEFAULT_ENABLE_CONNECTION_POOL = CommonClientConfigKey.EnableConnectionPool.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_HTTP_CONNECTIONS_PER_HOST = CommonClientConfigKey.MaxHttpConnectionsPerHost.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_TOTAL_HTTP_CONNECTIONS = CommonClientConfigKey.MaxTotalHttpConnections.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_CONNECTIONS_PER_HOST = CommonClientConfigKey.MaxConnectionsPerHost.defaultValue();

@Deprecated
public static final int DEFAULT_MAX_TOTAL_CONNECTIONS = CommonClientConfigKey.MaxTotalConnections.defaultValue();

@Deprecated
public static final float DEFAULT_MIN_PRIME_CONNECTIONS_RATIO = CommonClientConfigKey.MinPrimeConnectionsRatio.defaultValue();

@Deprecated
public static final String DEFAULT_PRIME_CONNECTIONS_CLASS = CommonClientConfigKey.PrimeConnectionsClassName.defaultValue();

@Deprecated
public static final String DEFAULT_SEVER_LIST_CLASS = CommonClientConfigKey.NIWSServerListClassName.defaultValue();

@Deprecated
public static final String DEFAULT_SERVER_LIST_UPDATER_CLASS = CommonClientConfigKey.ServerListUpdaterClassName.defaultValue();

@Deprecated
public static final int DEFAULT_CONNECTION_IDLE_TIMERTASK_REPEAT_IN_MSECS = CommonClientConfigKey.ConnectionCleanerRepeatInterval.defaultValue(); // every half minute (30 secs)

@Deprecated
public static final int DEFAULT_CONNECTIONIDLE_TIME_IN_MSECS = CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds.defaultValue(); // all connections idle for 30 secs

private VipAddressResolver resolver = null;

/**
* Defaults for the parameters for the thread pool used by batchParallel
* calls
*/
@Deprecated
public static final int DEFAULT_POOL_MAX_THREADS = CommonClientConfigKey.MaxTotalHttpConnections.defaultValue();

@Deprecated
public static final int DEFAULT_POOL_MIN_THREADS = CommonClientConfigKey.PoolMinThreads.defaultValue();

@Deprecated
public static final long DEFAULT_POOL_KEEP_ALIVE_TIME = CommonClientConfigKey.PoolKeepAliveTime.defaultValue();

@Deprecated
public static final TimeUnit DEFAULT_POOL_KEEP_ALIVE_TIME_UNITS = TimeUnit.valueOf(CommonClientConfigKey.PoolKeepAliveTimeUnits.defaultValue());

@Deprecated
public static final Boolean DEFAULT_ENABLE_ZONE_AFFINITY = CommonClientConfigKey.EnableZoneAffinity.defaultValue();

@Deprecated
public static final Boolean DEFAULT_ENABLE_ZONE_EXCLUSIVITY = CommonClientConfigKey.EnableZoneExclusivity.defaultValue();

@Deprecated
public static final int DEFAULT_PORT = CommonClientConfigKey.Port.defaultValue();

@Deprecated
public static final Boolean DEFAULT_ENABLE_LOADBALANCER = CommonClientConfigKey.InitializeNFLoadBalancer.defaultValue();

public class DefaultClientConfigImpl extends AbstractDefaultClientConfigImpl {
public static final String DEFAULT_PROPERTY_NAME_SPACE = CommonClientConfigKey.DEFAULT_NAME_SPACE;

private String propertyNameSpace = DEFAULT_PROPERTY_NAME_SPACE;

@Deprecated
public static final Boolean DEFAULT_OK_TO_RETRY_ON_ALL_OPERATIONS = CommonClientConfigKey.OkToRetryOnAllOperations.defaultValue();

@Deprecated
public static final Boolean DEFAULT_ENABLE_NIWS_EVENT_LOGGING = Boolean.TRUE;

@Deprecated
public static final Boolean DEFAULT_IS_CLIENT_AUTH_REQUIRED = Boolean.FALSE;

@Deprecated
public Boolean getDefaultPrioritizeVipAddressBasedServers() {
return DEFAULT_PRIORITIZE_VIP_ADDRESS_BASED_SERVERS;
Expand Down Expand Up @@ -440,15 +286,11 @@ public Boolean getDefaultEnableConnectionPool() {
return DEFAULT_ENABLE_CONNECTION_POOL;
}

public VipAddressResolver getResolver() {
return resolver;
}

/**
* Create instance with no properties in default name space {@link #DEFAULT_PROPERTY_NAME_SPACE}
*/
public DefaultClientConfigImpl() {
super();
super(ArchaiusPropertyResolver.INSTANCE);
}

/**
Expand Down Expand Up @@ -512,15 +354,6 @@ public void loadDefaultValues() {
set(CommonClientConfigKey.IsClientAuthRequired, getDefaultIsClientAuthRequired());
set(CommonClientConfigKey.UseIPAddrForServer, getDefaultUseIpAddressForServer());
set(CommonClientConfigKey.ListOfServers, "");

ConfigurationManager.getConfigInstance().addConfigurationListener(new ConfigurationListener() {
@Override
public void configurationChanged(ConfigurationEvent event) {
if (!event.isBeforeUpdate()) {
reload();
}
}
});
}

@Deprecated
Expand Down Expand Up @@ -570,84 +403,6 @@ public String getDefaultPropName(IClientConfigKey propName) {
return getDefaultPropName(propName.key());
}

/**
* Load properties for a given client. It first loads the default values for all properties,
* and any properties already defined with Archaius ConfigurationManager.
*/
@Override
public void loadProperties(String restClientName) {
setClientName(restClientName);
loadDefaultValues();
}

@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "DC_DOUBLECHECK")
private VipAddressResolver getVipAddressResolver() {
if (resolver == null) {
synchronized (this) {
if (resolver == null) {
try {
resolver = (VipAddressResolver) Class
.forName(getOrDefault(CommonClientConfigKey.VipAddressResolverClassName))
.newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException("Cannot instantiate VipAddressResolver", e);
}
}
}
}
return resolver;
}

@Override
public String resolveDeploymentContextbasedVipAddresses(){
String deploymentContextBasedVipAddressesMacro = get(CommonClientConfigKey.DeploymentContextBasedVipAddresses);
if (deploymentContextBasedVipAddressesMacro == null) {
return null;
}
return getVipAddressResolver().resolve(deploymentContextBasedVipAddressesMacro, this);
}

@Deprecated
public String getAppName(){
return get(CommonClientConfigKey.AppName);
}

@Deprecated
public String getVersion(){
return get(CommonClientConfigKey.Version);
}

@Override
protected <T> Optional<T> loadProperty(String key, Class<T> type) {
LOG.debug("Loading property {}", key);

if (Integer.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getInteger(key, null));
} else if (Boolean.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getBoolean(key, null));
} else if (Float.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getFloat(key, null));
} else if (Long.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getLong(key, null));
} else if (Double.class.equals(type)) {
return Optional.ofNullable((T) ConfigurationManager.getConfigInstance().getDouble(key, null));
} else if (TimeUnit.class.equals(type)) {
return Optional.ofNullable((T) TimeUnit.valueOf(ConfigurationManager.getConfigInstance().getString(key, null)));
} else {
return Optional.ofNullable(ConfigurationManager.getConfigInstance().getStringArray(key))
.filter(ar -> ar.length > 0)
.map(ar -> Arrays.stream(ar).collect(Collectors.joining(",")))
.map(value -> {
if (type.equals(String.class)) {
return (T)value;
} else {
return resolveWithValueOf(type, value)
.orElseThrow(() -> new IllegalArgumentException("Unable to convert value to desired type " + type));
}
});
}
}

public DefaultClientConfigImpl withProperty(IClientConfigKey key, Object value) {
setProperty(key, value);
return this;
Expand Down
Loading

0 comments on commit 85ae574

Please sign in to comment.