use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancingObservablesWithReadTimeout.
@Test
public void testLoadBalancingObservablesWithReadTimeout() throws Exception {
NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
MockWebServer server = new MockWebServer();
String content = "{\"name\": \"ribbon\", \"age\": 2}";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
server.play();
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 100);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/readTimeout");
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
Server goodServer = new Server("localhost:" + server.getPort());
Server badServer = new Server("localhost:" + port);
lb.setServersList(Lists.newArrayList(goodServer, badServer, badServer, goodServer));
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
observableWithRetries.subscribe(observer);
observer.await();
if (observer.error != null) {
observer.error.printStackTrace();
}
assertEquals("ribbon", observer.obj.name);
assertEquals(2, observer.obj.age);
ServerStats stats = lbObservables.getServerStats(badServer);
server.shutdown();
final HttpClientListener listener = lbObservables.getListener();
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 5;
}
});
assertEquals(0, listener.getPoolReuse());
// two requests to bad server because retry same server is set to 1
assertEquals(4, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(4, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancerThrottle.
@Test
public void testLoadBalancerThrottle() throws Exception {
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/throttle");
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(IClientConfigKey.Keys.MaxAutoRetriesNextServer, 1).set(IClientConfigKey.Keys.OkToRetryOnAllOperations, true);
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
Server server = new Server(host, port);
lb.setServersList(Lists.newArrayList(server, server, server));
Observable<HttpClientResponse<ByteBuf>> response = lbObservables.submit(request);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
response.subscribe(new Action1<HttpClientResponse<ByteBuf>>() {
@Override
public void call(HttpClientResponse<ByteBuf> t1) {
System.err.println("Get response: " + t1.getStatus().code());
latch.countDown();
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
error.set(t1);
latch.countDown();
}
}, new Action0() {
@Override
public void call() {
Thread.dumpStack();
latch.countDown();
}
});
latch.await();
assertTrue(error.get() instanceof ClientException);
ClientException ce = (ClientException) error.get();
assertTrue(ce.toString(), ce.getErrorType() == ClientException.ErrorType.NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED);
assertEquals(2, lbObservables.getServerStats(server).getSuccessiveConnectionFailureCount());
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testStreamWithLoadBalancer.
@Test
public void testStreamWithLoadBalancer() throws Exception {
// NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
// IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true).withMaxAutoRetries(1).withMaxAutoRetriesNextServer(3).build();
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream");
List<Person> result = Lists.newArrayList();
Server goodServer = new Server("localhost:" + port);
Server badServer = new Server("localhost:12245");
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
lb.setServersList(servers);
result = getPersonListFromResponse(lbObservables.submit(request, null, null));
assertEquals(EmbeddedResources.entityStream, result);
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class ListenerTest method testDisabledListener.
@Test
public void testDisabledListener() throws Exception {
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myClient").withProperty(CommonClientConfigKey.ConnectTimeout, "2000").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
List<Server> servers = Lists.newArrayList(badServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
ConfigurationManager.getConfigInstance().setProperty("ribbon.listener." + TestExecutionListener.class.getName() + ".disabled", "true");
try {
client.submit(request, null, overrideConfig).toBlocking().last();
} catch (Exception e) {
assertNotNull(e);
}
assertEquals(0, listener.executionStartCounter.get());
assertEquals(0, listener.startWithServerCounter.get());
assertEquals(0, listener.exceptionWithServerCounter.get());
assertEquals(0, listener.executionFailedCounter.get());
assertEquals(0, listener.executionSuccessCounter.get());
try {
client.submit(request, null, overrideConfig).toBlocking().last();
} catch (Exception e) {
assertNotNull(e);
}
assertEquals(0, listener.executionStartCounter.get());
assertEquals(0, listener.startWithServerCounter.get());
assertEquals(0, listener.exceptionWithServerCounter.get());
assertEquals(0, listener.executionFailedCounter.get());
assertEquals(0, listener.executionSuccessCounter.get());
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class ListenerTest method testSuccessExecution.
@Test
public void testSuccessExecution() throws IOException {
MockWebServer server = new MockWebServer();
String content = "OK";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
server.play();
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
Server goodServer = new Server("localhost:" + server.getPort());
List<Server> servers = Lists.newArrayList(goodServer, badServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
assertEquals(200, response.getStatus().code());
assertEquals(1, listener.executionStartCounter.get());
assertEquals(3, listener.startWithServerCounter.get());
assertEquals(2, listener.exceptionWithServerCounter.get());
assertEquals(0, listener.executionFailedCounter.get());
assertEquals(1, listener.executionSuccessCounter.get());
assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
assertTrue(listener.isContextChecked());
assertTrue(listener.isCheckExecutionInfo());
assertSame(response, listener.getResponse());
}
Aggregations