From e6e821acd9d83ec3482da83a9d3e90d57f632a9f Mon Sep 17 00:00:00 2001 From: Matt Nelson Date: Mon, 5 Oct 2015 14:24:18 -0500 Subject: [PATCH 1/2] Support RibbonCommand group key parameter --- .../ribbon/hystrix/RibbonCommand.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java index 1c8c938900..99deeab3e2 100644 --- a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java +++ b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java @@ -4,6 +4,7 @@ import com.netflix.client.http.HttpResponse; import com.netflix.config.DynamicPropertyFactory; import com.netflix.hystrix.HystrixCommand; +import com.netflix.hystrix.HystrixCommandKey; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandProperties; import com.netflix.niws.client.http.RestClient; @@ -53,13 +54,25 @@ public RibbonCommand(String commandKey, MultivaluedMap params, InputStream requestEntity) throws URISyntaxException { - super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( - // we want to default to semaphore-isolation since this wraps - // 2 others commands that are already thread isolated - HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) - .withExecutionIsolationSemaphoreMaxConcurrentRequests(DynamicPropertyFactory.getInstance(). - getIntProperty(ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores", 100).get()))); - + this("default", RibbonCommand.class.getSimpleName(), restClient, verb, uri, headers, params, requestEntity); + } + + public RibbonCommand(String groupKey, + String commandKey, + RestClient restClient, + Verb verb, + String uri, + MultivaluedMap headers, + MultivaluedMap params, + InputStream requestEntity) throws URISyntaxException { + + super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( + // we want to default to semaphore-isolation since this wraps + // 2 others commands that are already thread isolated + HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) + .withExecutionIsolationSemaphoreMaxConcurrentRequests(DynamicPropertyFactory.getInstance(). + getIntProperty(ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores", 100).get()))); + this.restClient = restClient; this.verb = verb; this.uri = new URI(uri); From ce093dc0cb3eee75f698f16954553cc570e26323 Mon Sep 17 00:00:00 2001 From: Matt Nelson Date: Thu, 8 Oct 2015 14:08:51 -0500 Subject: [PATCH 2/2] Support HostCommand group key parameter --- .../httpclient/hystrix/HostCommand.java | 45 +++++++++++++++++-- .../ribbon/hystrix/RibbonCommand.java | 41 +++++++++++++++-- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/httpclient/hystrix/HostCommand.java b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/httpclient/hystrix/HostCommand.java index 243b47ea50..507d39649b 100644 --- a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/httpclient/hystrix/HostCommand.java +++ b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/httpclient/hystrix/HostCommand.java @@ -3,12 +3,23 @@ import com.netflix.config.DynamicPropertyFactory; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; +import com.netflix.hystrix.HystrixCommandKey; import com.netflix.hystrix.HystrixCommandProperties; +import com.netflix.hystrix.HystrixCommand.Setter; import com.netflix.zuul.constants.ZuulConstants; +import com.netflix.zuul.context.NFRequestContext; +import com.netflix.zuul.context.RequestContext; + import org.apache.http.HttpHost; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.junit.Assert.assertNotNull; import java.io.IOException; @@ -30,8 +41,14 @@ public HostCommand(HttpClient httpclient, HttpHost httpHost, HttpRequest httpReq } public HostCommand(String commandKey, HttpClient httpclient, HttpHost httpHost, HttpRequest httpRequest) { + // Switch the command/group key to remain passive with the previous release which used the command key as the group key + this(commandKey, HostCommand.class.getSimpleName(), httpclient, httpHost, httpRequest); + } + + public HostCommand(String groupKey, String commandKey, HttpClient httpclient, HttpHost httpHost, HttpRequest httpRequest) { - super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( + super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) + .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( // we want to default to semaphore-isolation since this wraps // 2 others commands that are already thread isolated HystrixCommandProperties.Setter() @@ -56,6 +73,28 @@ protected HttpResponse run() throws Exception { HttpResponse forward() throws IOException { return httpclient.execute(httpHost, httpRequest); } - - + + public static class UnitTest { + + @Test + public void testConstruction() { + HostCommand hc = new HostCommand(null, null, null); + Assert.assertEquals("default", hc.getCommandGroup().name()); + Assert.assertEquals(HostCommand.class.getSimpleName(), hc.getCommandKey().name()); + } + + @Test + public void testConstructionWithCommandKey() { + HostCommand hc = new HostCommand("myCommand", null, null, null); + Assert.assertEquals("myCommand", hc.getCommandGroup().name()); + Assert.assertEquals(HostCommand.class.getSimpleName(), hc.getCommandKey().name()); + } + + @Test + public void testConstructionWithGroupKeyAndCommandKey() { + HostCommand hc = new HostCommand("myGroup", "myCommand", null, null, null); + Assert.assertEquals("myGroup", hc.getCommandGroup().name()); + Assert.assertEquals("myCommand", hc.getCommandKey().name()); + } + } } diff --git a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java index 99deeab3e2..512fb9a7b8 100644 --- a/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java +++ b/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java @@ -10,8 +10,15 @@ import com.netflix.niws.client.http.RestClient; import com.netflix.zuul.constants.ZuulConstants; import com.netflix.zuul.context.NFRequestContext; +import com.netflix.zuul.dependency.httpclient.hystrix.HostCommand; import javax.ws.rs.core.MultivaluedMap; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; @@ -54,7 +61,8 @@ public RibbonCommand(String commandKey, MultivaluedMap params, InputStream requestEntity) throws URISyntaxException { - this("default", RibbonCommand.class.getSimpleName(), restClient, verb, uri, headers, params, requestEntity); + // Switch the command/group key to remain passive with the previous release which used the command key as the group key + this(commandKey, RibbonCommand.class.getSimpleName(), restClient, verb, uri, headers, params, requestEntity); } public RibbonCommand(String groupKey, @@ -66,7 +74,8 @@ public RibbonCommand(String groupKey, MultivaluedMap params, InputStream requestEntity) throws URISyntaxException { - super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)).andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( + super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey)) + .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)).andCommandPropertiesDefaults( // we want to default to semaphore-isolation since this wraps // 2 others commands that are already thread isolated HystrixCommandProperties.Setter().withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) @@ -120,6 +129,30 @@ HttpResponse forward() throws Exception { context.setZuulResponse(response); return response; } - - + + public static class UnitTest { + + private static final String localhost = "http://localhost"; + + @Test + public void testConstruction() throws URISyntaxException { + RibbonCommand rc = new RibbonCommand(null, null, localhost, null, null, null); + Assert.assertEquals("default", rc.getCommandGroup().name()); + Assert.assertEquals(RibbonCommand.class.getSimpleName(), rc.getCommandKey().name()); + } + + @Test + public void testConstructionWithCommandKey() throws URISyntaxException { + RibbonCommand rc = new RibbonCommand("myCommand", null, null, localhost, null, null, null); + Assert.assertEquals("myCommand", rc.getCommandGroup().name()); + Assert.assertEquals(RibbonCommand.class.getSimpleName(), rc.getCommandKey().name()); + } + + @Test + public void testConstructionWithGroupKeyAndCommandKey() throws URISyntaxException { + RibbonCommand rc = new RibbonCommand("myGroup", "myCommand", null, null, localhost, null, null, null); + Assert.assertEquals("myGroup", rc.getCommandGroup().name()); + Assert.assertEquals("myCommand", rc.getCommandKey().name()); + } + } }