use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancingWithTwoServers.
@Test
public void testLoadBalancingWithTwoServers() throws Exception {
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();
HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)).withHeader("Content-type", "application/json");
NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
HttpClientListener externalListener = HttpClientListener.newHttpListener("external");
lbObservables.subscribe(externalListener);
Server server1 = new Server("localhost:" + server.getPort());
Server server2 = new Server("localhost:" + port);
lb.setServersList(Lists.newArrayList(server1, server2));
RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
@Override
public boolean isRetriableException(Throwable e, boolean sameServer) {
return true;
}
};
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
observableWithRetries.subscribe(observer);
observer.await();
if (observer.error != null) {
observer.error.printStackTrace();
}
assertEquals("ribbon", observer.obj.name);
assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age);
observer = new ObserverWithLatch<Person>();
observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
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(server1);
server.shutdown();
// assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
stats = lbObservables.getServerStats(server2);
// two requests to bad server because retry same server is set to 1
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
final HttpClientListener listener = lbObservables.getListener();
assertEquals(2, listener.getPoolAcquires());
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 2;
}
});
assertEquals(2, listener.getConnectionCount());
assertEquals(0, listener.getPoolReuse());
assertEquals(2, externalListener.getPoolAcquires());
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testObservableWithMultipleServersFailed.
@Test
public void testObservableWithMultipleServersFailed() throws Exception {
IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues().withRetryOnAllOperations(true).withMaxAutoRetries(1).withMaxAutoRetriesNextServer(3).withConnectTimeout(100).build();
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config);
Server badServer = new Server("localhost:12345");
Server badServer1 = new Server("localhost:12346");
Server badServer2 = new Server("localhost:12347");
List<Server> servers = Lists.newArrayList(badServer, badServer1, badServer2);
lb.setServersList(servers);
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
observableWithRetries.subscribe(observer);
observer.await();
assertNull(observer.obj);
observer.error.printStackTrace();
assertTrue(observer.error instanceof ClientException);
ServerStats stats = lbObservables.getServerStats(badServer);
// two requests to bad server because retry same server is set to 1
assertEquals(2, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(2, stats.getSuccessiveConnectionFailureCount());
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class DefaultNIWSServerListFilterTest method testZoneAffinityEnabled.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testZoneAffinityEnabled() throws Exception {
ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.DeploymentContextBasedVipAddresses", "l10nservicegeneral.cloud.netflix.net:7001");
ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NFLoadBalancerClassName", DynamicServerListLoadBalancer.class.getName());
ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.NIWSServerListClassName", DiscoveryEnabledNIWSServerList.class.getName());
ConfigurationManager.getConfigInstance().setProperty("DefaultNIWSServerListFilterTest1.ribbon.EnableZoneAffinity", "true");
DynamicServerListLoadBalancer lb = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer("DefaultNIWSServerListFilterTest1");
assertTrue(lb.getRule() instanceof AvailabilityFilteringRule);
ZoneAffinityServerListFilter filter = (ZoneAffinityServerListFilter) lb.getFilter();
LoadBalancerStats loadBalancerStats = lb.getLoadBalancerStats();
List<DiscoveryEnabledServer> servers = new ArrayList<DiscoveryEnabledServer>();
servers.add(createServer(1, "a"));
servers.add(createServer(2, "a"));
servers.add(createServer(3, "a"));
servers.add(createServer(4, "a"));
servers.add(createServer(1, "b"));
servers.add(createServer(2, "b"));
servers.add(createServer(3, "b"));
servers.add(createServer(1, "c"));
servers.add(createServer(2, "c"));
servers.add(createServer(3, "c"));
servers.add(createServer(4, "c"));
servers.add(createServer(5, "c"));
List<DiscoveryEnabledServer> filtered = filter.getFilteredListOfServers(servers);
List<DiscoveryEnabledServer> expected = new ArrayList<DiscoveryEnabledServer>();
expected.add(createServer(1, "c"));
expected.add(createServer(2, "c"));
expected.add(createServer(3, "c"));
expected.add(createServer(4, "c"));
expected.add(createServer(5, "c"));
assertEquals(expected, filtered);
lb.setServersList(filtered);
for (int i = 1; i <= 4; i++) {
loadBalancerStats.incrementActiveRequestsCount(createServer(i, "c"));
}
filtered = filter.getFilteredListOfServers(servers);
assertEquals(servers, filtered);
}
use of com.netflix.loadbalancer.AvailabilityFilteringRule in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancingPostWithReadTimeout.
@Test
public void testLoadBalancingPostWithReadTimeout() throws Exception {
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.createPost("/testAsync/postTimeout").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)).withHeader("Content-type", "application/json");
NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
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);
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
lb.setServersList(servers);
RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
@Override
public boolean isRetriableException(Throwable e, boolean sameServer) {
return true;
}
};
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
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();
assertEquals(4, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(4, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
// two requests to bad server because retry same server is set to 1
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 testLoadBalancingPostWithNoRetrySameServer.
@Test
public void testLoadBalancingPostWithNoRetrySameServer() throws Exception {
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.createPost("/testAsync/postTimeout").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)).withHeader("Content-type", "application/json");
NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(0, 3, true);
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);
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
lb.setServersList(servers);
RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {
@Override
public boolean isRetriableException(Throwable e, boolean sameServer) {
return true;
}
};
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
observableWithRetries.subscribe(observer);
observer.await();
if (observer.error != null) {
observer.error.printStackTrace();
}
server.shutdown();
assertEquals("ribbon", observer.obj.name);
assertEquals(2, observer.obj.age);
ServerStats stats = lbObservables.getServerStats(badServer);
assertEquals(2, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(2, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
Aggregations