Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Archaius a soft dependency through reflection and improve plugin… #1083

Merged
merged 8 commits into from
Feb 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.netflix.hystrix;

import static com.netflix.hystrix.strategy.properties.HystrixProperty.Factory.asProperty;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forBoolean;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forInteger;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forString;

import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedArchaiusProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import com.netflix.hystrix.util.HystrixRollingNumber;
Expand Down Expand Up @@ -72,22 +73,27 @@ protected HystrixCollapserProperties(HystrixCollapserKey key, Setter builder, St
}

private static HystrixProperty<Integer> getProperty(String propertyPrefix, HystrixCollapserKey key, String instanceProperty, Integer builderOverrideValue, Integer defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.IntegerProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)));
return forInteger()
.add(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)
.build();


}

private static HystrixProperty<Boolean> getProperty(String propertyPrefix, HystrixCollapserKey key, String instanceProperty, Boolean builderOverrideValue, Boolean defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.BooleanProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)));
return forBoolean()
.add(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)
.build();
}

@SuppressWarnings("unused")
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixCollapserKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.StringProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)));
return forString()
.add(propertyPrefix + ".collapser." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".collapser.default." + instanceProperty, defaultValue)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.netflix.hystrix;

import static com.netflix.hystrix.strategy.properties.HystrixProperty.Factory.asProperty;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forBoolean;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forInteger;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forString;

import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedArchaiusProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty;
import com.netflix.hystrix.strategy.properties.HystrixDynamicProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import com.netflix.hystrix.util.HystrixRollingNumber;
Expand Down Expand Up @@ -134,7 +135,7 @@ protected HystrixCommandProperties(HystrixCommandKey key, HystrixCommandProperti
this.requestLogEnabled = getProperty(propertyPrefix, key, "requestLog.enabled", builder.getRequestLogEnabled(), default_requestLogEnabled);

// threadpool doesn't have a global override, only instance level makes sense
this.executionIsolationThreadPoolKeyOverride = asProperty(new DynamicStringProperty(propertyPrefix + ".command." + key.name() + ".threadPoolKeyOverride", null));
this.executionIsolationThreadPoolKeyOverride = forString().add(propertyPrefix + ".command." + key.name() + ".threadPoolKeyOverride", null).build();
}

/**
Expand Down Expand Up @@ -412,22 +413,25 @@ public HystrixProperty<Boolean> requestLogEnabled() {
}

private static HystrixProperty<Boolean> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, Boolean builderOverrideValue, Boolean defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.BooleanProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".command.default." + instanceProperty, defaultValue)));
return forBoolean()
.add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".command.default." + instanceProperty, defaultValue)
.build();
}

private static HystrixProperty<Integer> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, Integer builderOverrideValue, Integer defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.IntegerProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".command.default." + instanceProperty, defaultValue)));
return forInteger()
.add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".command.default." + instanceProperty, defaultValue)
.build();
}

@SuppressWarnings("unused")
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixCommandKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.StringProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".command.default." + instanceProperty, defaultValue)));
return forString()
.add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".command.default." + instanceProperty, defaultValue)
.build();
}

private static HystrixProperty<ExecutionIsolationStrategy> getProperty(final String propertyPrefix, final HystrixCommandKey key, final String instanceProperty, final ExecutionIsolationStrategy builderOverrideValue, final ExecutionIsolationStrategy defaultValue) {
Expand All @@ -439,7 +443,7 @@ private static HystrixProperty<ExecutionIsolationStrategy> getProperty(final Str
* HystrixProperty that converts a String to ExecutionIsolationStrategy so we remain TypeSafe.
*/
private static final class ExecutionIsolationStrategyHystrixProperty implements HystrixProperty<ExecutionIsolationStrategy> {
private final HystrixPropertiesChainedArchaiusProperty.StringProperty property;
private final HystrixDynamicProperty<String> property;
private volatile ExecutionIsolationStrategy value;
private final ExecutionIsolationStrategy defaultValue;

Expand All @@ -449,9 +453,10 @@ private ExecutionIsolationStrategyHystrixProperty(ExecutionIsolationStrategy bui
if (builderOverrideValue != null) {
overrideValue = builderOverrideValue.name();
}
property = new HystrixPropertiesChainedArchaiusProperty.StringProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".command." + key.name() + "." + instanceProperty, overrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".command.default." + instanceProperty, defaultValue.name()));
property = forString()
.add(propertyPrefix + ".command." + key.name() + "." + instanceProperty, overrideValue)
.add(propertyPrefix + ".command.default." + instanceProperty, defaultValue.name())
.build();

// initialize the enum value from the property
parseProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package com.netflix.hystrix;

import static com.netflix.hystrix.strategy.properties.HystrixProperty.Factory.asProperty;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forBoolean;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forInteger;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forString;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedArchaiusProperty;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import com.netflix.hystrix.util.HystrixRollingNumber;
Expand Down Expand Up @@ -68,23 +69,26 @@ protected HystrixThreadPoolProperties(HystrixThreadPoolKey key, Setter builder,
}

private static HystrixProperty<Integer> getProperty(String propertyPrefix, HystrixThreadPoolKey key, String instanceProperty, Integer builderOverrideValue, Integer defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.IntegerProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)));
return forInteger()
.add(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)
.build();
}

@SuppressWarnings("unused")
private static HystrixProperty<Boolean> getProperty(String propertyPrefix, HystrixThreadPoolKey key, String instanceProperty, Boolean builderOverrideValue, Boolean defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.BooleanProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicBooleanProperty(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)));
return forBoolean()
.add(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)
.build();
}

@SuppressWarnings("unused")
private static HystrixProperty<String> getProperty(String propertyPrefix, HystrixThreadPoolKey key, String instanceProperty, String builderOverrideValue, String defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.StringProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue),
new HystrixPropertiesChainedArchaiusProperty.DynamicStringProperty(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)));
return forString()
.add(propertyPrefix + ".threadpool." + key.name() + "." + instanceProperty, builderOverrideValue)
.add(propertyPrefix + ".threadpool.default." + instanceProperty, defaultValue)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.netflix.hystrix;

import com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedArchaiusProperty;
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forInteger;

import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixProperty;

import static com.netflix.hystrix.strategy.properties.HystrixProperty.Factory.asProperty;

/**
* Properties for Hystrix timer thread pool.
* <p>
Expand All @@ -24,8 +23,10 @@ protected HystrixTimerThreadPoolProperties(Setter setter) {
}

private static HystrixProperty<Integer> getProperty(String propertyPrefix, String instanceProperty, Integer defaultValue) {
return asProperty(new HystrixPropertiesChainedArchaiusProperty.IntegerProperty(
new HystrixPropertiesChainedArchaiusProperty.DynamicIntegerProperty(propertyPrefix + ".timer.threadpool.default." + instanceProperty, defaultValue)));

return forInteger()
.add(propertyPrefix + ".timer.threadpool.default." + instanceProperty, defaultValue)
.build();
}

public HystrixProperty<Integer> getCorePoolSize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright 2016 Netflix, Inc.
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.hystrix.strategy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.netflix.hystrix.strategy.properties.HystrixDynamicProperties;

/**
* @ExcludeFromJavadoc
* @author agentgt
*/
class HystrixArchaiusHelper {

/**
* To keep class loading minimal for those that have archaius in the classpath but choose not to use it.
* @ExcludeFromJavadoc
* @author agent
*/
private static class LazyHolder {
private final static Method loadCascadedPropertiesFromResources;
private final static String CONFIG_MANAGER_CLASS = "com.netflix.config.ConfigurationManager";

static {
Method load = null;
try {
Class<?> configManager = Class.forName(CONFIG_MANAGER_CLASS);
load = configManager.getMethod("loadCascadedPropertiesFromResources", String.class);
} catch (Exception e) {
}

loadCascadedPropertiesFromResources = load;
}
}

/**
* @ExcludeFromJavadoc
*/
static boolean isArchaiusV1Available() {
return LazyHolder.loadCascadedPropertiesFromResources != null;
}

static void loadCascadedPropertiesFromResources(String name) {
if (isArchaiusV1Available()) {
try {
LazyHolder.loadCascadedPropertiesFromResources.invoke(null, name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the return value here? If we find implementations in the hystrix-plugins file, shouldn't we try to instantiate them and return them? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this code comes from the original 1.4 code https://github.com/Netflix/Hystrix/blob/1.4.x/hystrix-core/src/main/java/com/netflix/hystrix/strategy/HystrixPlugins.java#L57

What it does is indirectly boots up Archaius and adds the hystrix-plugins properties file as an additional configuration resource. The actual method does not return anything.The assumption here is Archaius isn't going to provide configuration properties to point to use a different HystrixDynamicProperties.

This is only to create HystrixDynamicProperties... all the other implementations will be loaded using cascaded configuration (assuming you have Archaius). That is hystrix-plugins.properties will be used.

This does bring up an excellent point though that the HystrixDynamicPropertiesSystemProperties (I picked that name to be consistent with the naming scheme) perhaps should load hystrix-plugins.properties? Let me know what you think about that.

EDIT actually I think its a bad idea given the properties file could have string interpolation characters and/or could be a more advanced properties file (e.g. commons config style and is UTF-8... standard properties files are shockingly not UTF-8 but ISO).

} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
}
}

/**
* @ExcludeFromJavadoc
*/
static HystrixDynamicProperties createArchaiusDynamicProperties() {
if (isArchaiusV1Available()) {
loadCascadedPropertiesFromResources("hystrix-plugins");
try {
Class<?> defaultProperties = Class.forName(
"com.netflix.hystrix.strategy.properties.archaius" + ".HystrixDynamicPropertiesArchaius");
return (HystrixDynamicProperties) defaultProperties.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
// Fallback to System properties.
return null;
}

}
Loading