use of com.netflix.loadbalancer.BaseLoadBalancer 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());
}
use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.
the class ServerListRefreshTest method testServerListRefresh.
/**
* This test ensures that when server list is refreshed in the load balancer, the set of servers
* which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server
* that is not part of oldList should stay in the map.
*
* @throws IOException
*/
@Test
public void testServerListRefresh() throws IOException {
String content = "Hello world";
MockWebServer server1 = new MockWebServer();
MockWebServer server2 = new MockWebServer();
MockWebServer server3 = new MockWebServer();
MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
server1.enqueue(mockResponse);
server2.enqueue(mockResponse);
server3.enqueue(mockResponse);
server1.play();
server2.play();
server3.play();
try {
BaseLoadBalancer lb = new BaseLoadBalancer();
List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()));
lb.setServersList(initialList);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
client.submit(request).toBlocking().last();
client.submit(request).toBlocking().last();
HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort());
client.submit(request2).toBlocking().last();
Set<Server> cachedServers = client.getRxClients().keySet();
assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers);
List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999));
lb.setServersList(newList);
cachedServers = client.getRxClients().keySet();
assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers);
} finally {
server1.shutdown();
server2.shutdown();
server3.shutdown();
}
}
use of com.netflix.loadbalancer.BaseLoadBalancer in project ribbon by Netflix.
the class UdpClientTest method testUdpClientWithoutTimeout.
@Test
public void testUdpClientWithoutTimeout() throws Exception {
int port = choosePort();
UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
server.start();
BaseLoadBalancer lb = new BaseLoadBalancer();
lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
try {
String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<DatagramPacket>>() {
@Override
public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
connection.writeStringAndFlush("Is there anybody out there?");
return connection.getInput();
}
}).take(1).map(new Func1<DatagramPacket, String>() {
@Override
public String call(DatagramPacket datagramPacket) {
return datagramPacket.content().toString(Charset.defaultCharset());
}
}).toBlocking().first();
assertEquals(HelloUdpServer.WELCOME_MSG, response);
} finally {
server.shutdown();
}
}
use of com.netflix.loadbalancer.BaseLoadBalancer 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);
System.out.println(config);
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.newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<>(request, overrideConfig);
List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.newArrayList(listener);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
System.out.println(listener);
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());
}
use of com.netflix.loadbalancer.BaseLoadBalancer 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());
}
Aggregations