Search in sources :

Example 1 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient in project rest.li by linkedin.

the class RetryClientTest method testRetryNoAvailableHosts.

@Test
public void testRetryNoAvailableHosts() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/retry2"));
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, 3);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TrackerClientTest.TestCallback<RestResponse> restCallback = new TrackerClientTest.TestCallback<RestResponse>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.toString().contains("retryService is in a bad state"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 2 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient in project rest.li by linkedin.

the class RetryClientTest method testRetryOverLimit.

@Test
public void testRetryOverLimit() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/retry2"));
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, 1);
    URI uri = URI.create("d2://retryService?arg1=empty&arg2=empty");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TrackerClientTest.TestCallback<RestResponse> restCallback = new TrackerClientTest.TestCallback<RestResponse>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.getMessage().contains("Data not available"));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 3 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient in project rest.li by linkedin.

the class LoadBalancerEchoClient method startClient.

public void startClient() throws URISyntaxException, InterruptedException, ExecutionException, IOException, PropertyStoreException {
    DynamicClient client = new DynamicClient(getLoadBalancer(_hostPort), null);
    for (; ; ) {
        int index = 0;
        if (_services.length > 1) {
            index = _random.nextInt(_services.length);
        }
        String service = _services[index];
        URI uri = URI.create("d2://" + service);
        RestRequest req = new RestRequestBuilder(uri).setEntity("hi there".getBytes("UTF-8")).build();
        try {
            Future<RestResponse> response = client.restRequest(req);
            String responseString = response.get().getEntity().asString("UTF-8");
            System.err.println(uri + " response: " + responseString);
        } catch (ExecutionException e) {
            System.err.println("future.get() failed for " + uri + ": " + e);
        }
        Thread.sleep(_random.nextInt(1000));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) DynamicClient(com.linkedin.d2.balancer.clients.DynamicClient) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI)

Example 4 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient in project rest.li by linkedin.

the class TestRestLiD2Integration method init.

@BeforeTest
public void init() {
    _loadBalancer = MockLBFactory.createLoadBalancer();
    _r2Client = new DynamicClient(_loadBalancer, null);
    _restClient = new RestClient(_r2Client, "d2://");
}
Also used : DynamicClient(com.linkedin.d2.balancer.clients.DynamicClient) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient in project rest.li by linkedin.

the class D2BaseTest method assertQuorumProcessAllRequests.

public void assertQuorumProcessAllRequests(int num, String jsonConfigData, String zkUriString, LoadBalancerClientCli cli, DynamicClient client, String miscmsg) throws Exception {
    Map<String, Object> clustersData = D2ConfigTestUtil.getClusterServiceConfiguration(jsonConfigData);
    for (int j = 0; j < num; j++) {
        for (String clusterName : clustersData.keySet()) {
            String msg = generateMessage(zkUriString);
            String response = null;
            @SuppressWarnings("unchecked") Map<String, Object> servicesData = (Map<String, Object>) clustersData.get(clusterName);
            @SuppressWarnings("unchecked") Map<String, Object> services = (Map<String, Object>) servicesData.get("services");
            for (String service : services.keySet()) {
                try {
                    response = cli.sendRequest(client, clusterName, service, msg);
                    assertTrue(response.contains(LoadBalancerEchoServer.getResponsePostfixString()), "No '" + LoadBalancerEchoServer.getResponsePostfixString() + "' found in response from " + clusterName + "/" + service + ". Response:" + response);
                    _log.error("Assert pass. Response contains " + LoadBalancerEchoServer.getResponsePostfixString());
                } catch (Exception e) {
                    _log.error("Response for " + clusterName + "/" + service + " failed." + miscmsg + " Error:" + e + "\n", e);
                    e.printStackTrace();
                    throw new Exception(e);
                }
                _log.debug("Response for " + clusterName + "/" + service + ":" + response);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)8 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)8 URI (java.net.URI)8 Test (org.testng.annotations.Test)6 DynamicClient (com.linkedin.d2.balancer.clients.DynamicClient)4 SimpleLoadBalancer (com.linkedin.d2.balancer.simple.SimpleLoadBalancer)4 Facilities (com.linkedin.d2.balancer.Facilities)2 TestCallback (com.linkedin.d2.balancer.clients.TrackerClientTest.TestCallback)2 DelegatingFacilities (com.linkedin.d2.balancer.util.DelegatingFacilities)2 RequestContext (com.linkedin.r2.message.RequestContext)2 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)1 RetryClient (com.linkedin.d2.balancer.clients.RetryClient)1 ClientFactoryProvider (com.linkedin.d2.balancer.util.ClientFactoryProvider)1 DirectoryProvider (com.linkedin.d2.balancer.util.DirectoryProvider)1 KeyMapperProvider (com.linkedin.d2.balancer.util.KeyMapperProvider)1 PropertyStoreException (com.linkedin.d2.discovery.stores.PropertyStoreException)1 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1