Skip to content

Commit

Permalink
Merge pull request Netflix#168 from mattnelson/1.x
Browse files Browse the repository at this point in the history
Support RibbonCommand group key parameter
  • Loading branch information
NiteshKant committed Oct 19, 2015
2 parents e64e537 + ce093dc commit c4dec00
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
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;
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;
Expand Down Expand Up @@ -53,13 +61,27 @@ public RibbonCommand(String commandKey,
MultivaluedMap<String, String> 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())));

// 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,
String commandKey,
RestClient restClient,
Verb verb,
String uri,
MultivaluedMap<String, String> headers,
MultivaluedMap<String, String> 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);
Expand Down Expand Up @@ -107,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());
}
}
}

0 comments on commit c4dec00

Please sign in to comment.