Skip to content

Commit

Permalink
iss993: implemented support for default properties for fallback commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgcodevil committed Jul 2, 2016
1 parent 584f362 commit c226fdd
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
public @interface DefaultProperties {

/**
* Specifies default group key used for each hystrix command by default unless a command specifies it's own group key.
* Specifies default group key used for each hystrix command by default unless a command specifies group key explicitly.
* For additional info about this property see {@link HystrixCommand#groupKey()}.
*
* @return default group key
*/
String groupKey() default "";

/**
* Specifies default thread pool key used for each hystrix command by default unless a command specifies it's own thread pool key.
* Specifies default thread pool key used for each hystrix command by default unless a command specifies thread pool key explicitly.
* For additional info about this property see {@link HystrixCommand#threadPoolKey()}
*
* @return default thread pool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ private CommandAction createFallbackAction(MetaHolder metaHolder) {
.observable(ExecutionType.OBSERVABLE == fallbackMethod.getExecutionType())
.defaultCommandKey(fMethod.getName())
.defaultGroupKey(metaHolder.getDefaultGroupKey())
.defaultThreadPoolKey(metaHolder.getDefaultThreadPoolKey())
.defaultProperties(metaHolder.getDefaultProperties().orNull())
.hystrixCollapser(metaHolder.getHystrixCollapser())
.observableExecutionMode(hystrixCommand.observableExecutionMode())
.hystrixCommand(hystrixCommand).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ private MetaHolder createCopy(MetaHolder source, ExecutionType executionType) {
.defaultCollapserKey(source.getDefaultCollapserKey())
.defaultCommandKey(source.getDefaultCommandKey())
.defaultGroupKey(source.getDefaultGroupKey())
.defaultThreadPoolKey(source.getDefaultThreadPoolKey())
.defaultProperties(source.getDefaultProperties().orNull())
.hystrixCollapser(source.getHystrixCollapser())
.hystrixCommand(source.getHystrixCommand()).build();
}
Expand All @@ -100,6 +102,8 @@ private MetaHolder createCopy(MetaHolder source, ExecutionType executionType, Ob
.defaultCollapserKey(source.getDefaultCollapserKey())
.defaultCommandKey(source.getDefaultCommandKey())
.defaultGroupKey(source.getDefaultGroupKey())
.defaultThreadPoolKey(source.getDefaultThreadPoolKey())
.defaultProperties(source.getDefaultProperties().orNull())
.hystrixCollapser(source.getHystrixCollapser())
.hystrixCommand(source.getHystrixCommand()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ public String getCommandGroupKey() {
return isCommandAnnotationPresent() ? get(hystrixCommand.groupKey(), defaultGroupKey) : "";
}

@Deprecated // use getCommandGroupKey that returns default group key if command annotation doesn't specify it
public String getDefaultGroupKey() {
return defaultGroupKey;
}

public String getDefaultThreadPoolKey() {
return defaultThreadPoolKey;
}

public String getCollapserKey() {
return isCollapserAnnotationPresent() ? get(hystrixCollapser.collapserKey(), defaultCollapserKey) : "";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.netflix.hystrix.contrib.javanica.test.common.configuration.fallback;

import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.test.common.BasicHystrixTest;
import org.junit.Before;
import org.junit.Test;

import static com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS;
import static com.netflix.hystrix.contrib.javanica.test.common.CommonUtils.getHystrixCommandByKey;
import static org.junit.Assert.assertEquals;


public abstract class BasicFallbackDefaultPropertiesTest extends BasicHystrixTest {

private Service service;

protected abstract Service createService();

@Before
public void setUp() throws Exception {
service = createService();
}

@Test
public void testFallbackInheritsDefaultGroupKey() {
service.commandWithFallbackInheritsDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackInheritsDefaultProperties");
assertEquals("DefaultGroupKey", fallbackCommand.getCommandGroup().name());
}

@Test
public void testFallbackInheritsDefaultThreadPoolKey() {
service.commandWithFallbackInheritsDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackInheritsDefaultProperties");
assertEquals("DefaultThreadPoolKey", fallbackCommand.getThreadPoolKey().name());
}

@Test
public void testFallbackInheritsDefaultCommandProperties() {
service.commandWithFallbackInheritsDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackInheritsDefaultProperties");
assertEquals(456, fallbackCommand.getProperties().executionTimeoutInMilliseconds().get().intValue());
}

@Test
public void testFallbackInheritsThreadPollProperties() {
service.commandWithFallbackInheritsDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackInheritsDefaultProperties");

HystrixThreadPoolProperties properties = getThreadPoolProperties(fallbackCommand);

assertEquals(123, properties.maxQueueSize().get().intValue());
}

@Test
public void testFallbackOverridesDefaultGroupKey() {
service.commandWithFallbackOverridesDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackOverridesDefaultProperties");
assertEquals("FallbackGroupKey", fallbackCommand.getCommandGroup().name());
}

@Test
public void testFallbackOverridesDefaultThreadPoolKey() {
service.commandWithFallbackOverridesDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackOverridesDefaultProperties");
assertEquals("FallbackThreadPoolKey", fallbackCommand.getThreadPoolKey().name());
}

@Test
public void testFallbackOverridesDefaultCommandProperties() {
service.commandWithFallbackOverridesDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackOverridesDefaultProperties");
assertEquals(654, fallbackCommand.getProperties().executionTimeoutInMilliseconds().get().intValue());
}

@Test
public void testFallbackOverridesThreadPollProperties() {
service.commandWithFallbackOverridesDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackOverridesDefaultProperties");

HystrixThreadPoolProperties properties = getThreadPoolProperties(fallbackCommand);

assertEquals(321, properties.maxQueueSize().get().intValue());
}

@Test
public void testCommandOverridesDefaultPropertiesWithFallbackInheritsDefaultProperties(){
service.commandOverridesDefaultPropertiesWithFallbackInheritsDefaultProperties();
com.netflix.hystrix.HystrixInvokableInfo fallbackCommand = getHystrixCommandByKey("fallbackInheritsDefaultProperties");
HystrixThreadPoolProperties properties = getThreadPoolProperties(fallbackCommand);
assertEquals("DefaultGroupKey", fallbackCommand.getCommandGroup().name());
assertEquals("DefaultThreadPoolKey", fallbackCommand.getThreadPoolKey().name());
assertEquals(456, fallbackCommand.getProperties().executionTimeoutInMilliseconds().get().intValue());
assertEquals(123, properties.maxQueueSize().get().intValue());
}

@DefaultProperties(groupKey = "DefaultGroupKey",
threadPoolKey = "DefaultThreadPoolKey",
commandProperties = {
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "456")
},
threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "123")
})
public static class Service {

@HystrixCommand(fallbackMethod = "fallbackInheritsDefaultProperties")
public Object commandWithFallbackInheritsDefaultProperties() {
throw new RuntimeException();
}

@HystrixCommand(fallbackMethod = "fallbackOverridesDefaultProperties")
public Object commandWithFallbackOverridesDefaultProperties() {
throw new RuntimeException();
}

@HystrixCommand(groupKey = "CommandGroupKey",
threadPoolKey = "CommandThreadPoolKey",
commandProperties = {
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "654")
},
threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "321")
}, fallbackMethod = "fallbackInheritsDefaultProperties")
public Object commandOverridesDefaultPropertiesWithFallbackInheritsDefaultProperties() {
throw new RuntimeException();
}

@HystrixCommand
private Object fallbackInheritsDefaultProperties() {
return null;
}

@HystrixCommand(groupKey = "FallbackGroupKey",
threadPoolKey = "FallbackThreadPoolKey",
commandProperties = {
@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "654")
},
threadPoolProperties = {
@HystrixProperty(name = "maxQueueSize", value = "321")
})
private Object fallbackOverridesDefaultProperties() {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.netflix.hystrix.contrib.javanica.test.spring.configuration.fallback;

import com.netflix.hystrix.contrib.javanica.test.common.configuration.fallback.BasicFallbackDefaultPropertiesTest;
import com.netflix.hystrix.contrib.javanica.test.spring.conf.AopCglibConfig;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AopCglibConfig.class, FallbackDefaultPropertiesTest.Config.class})
public class FallbackDefaultPropertiesTest extends BasicFallbackDefaultPropertiesTest {

@Autowired
private Service service;

@Override
protected Service createService() {
return service;
}

@Configurable
public static class Config {
@Bean
public BasicFallbackDefaultPropertiesTest.Service service() {
return new BasicFallbackDefaultPropertiesTest.Service();
}
}
}

0 comments on commit c226fdd

Please sign in to comment.