Search in sources :

Example 6 with D2Client

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

the class TestRouteLookupClient method testRouteLookupClientFuture.

@Test
public void testRouteLookupClientFuture() throws ExecutionException, InterruptedException {
    RouteLookup routeLookup = new SimpleTestRouteLookup();
    final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
    d2Client.start(new FutureCallback<None>());
    RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
    Future<RestResponse> future = routeLookupClient.restRequest(dummyRestRequest, "5436");
    try {
        future.get();
        // the request shouldn't succeed because we haven't set up a server or any service -> cluster -> uri
        // mapping; we want it to fail because we can get the service name we tried to get at from the
        // ServiceUnavailableException that is thrown.
        Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("_serviceName=simple_uriWestCoast5436Foo")) {
            Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5436Foo");
        }
    }
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Example 7 with D2Client

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

the class TestRouteLookupClient method testBadRequest.

@Test
public void testBadRequest() {
    RouteLookup routeLookup = new SimpleTestRouteLookup();
    final D2Client d2Client = new D2ClientBuilder().build();
    d2Client.start(new FutureCallback<None>());
    RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("http://simple_uri")).build();
    try {
        Future<RestResponse> future = routeLookupClient.restRequest(dummyRestRequest, "5436");
        future.get();
        Assert.fail("Unexpected success, request should have thrown an Exception");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
        String message = e.getMessage();
        if (!message.contains("Unsupported scheme in URI: http://simple_uri")) {
            Assert.fail("request was sent using http instead of d2, but we didn't get Unsupported scheme");
        }
    }
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Example 8 with D2Client

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

the class D2ClientBuilder method build.

/**
   * @return {@link D2Client} that is not started yet. Call start(Callback) to start it.
   */
public D2Client build() {
    final Map<String, TransportClientFactory> transportClientFactories = (_config.clientFactories == null) ? // if user didn't provide transportClientFactories we'll use default ones
    createDefaultTransportClientFactories() : _config.clientFactories;
    final LoadBalancerWithFacilitiesFactory loadBalancerFactory = (_config.lbWithFacilitiesFactory == null) ? new ZKFSLoadBalancerWithFacilitiesFactory() : _config.lbWithFacilitiesFactory;
    final D2ClientConfig cfg = new D2ClientConfig(_config.zkHosts, _config.zkSessionTimeoutInMs, _config.zkStartupTimeoutInMs, _config.lbWaitTimeout, _config.lbWaitUnit, _config.flagFile, _config.basePath, _config.fsBasePath, _config.componentFactory, transportClientFactories, _config.lbWithFacilitiesFactory, _config.sslContext, _config.sslParameters, _config.isSSLEnabled, _config.shutdownAsynchronously, _config.isSymlinkAware, _config.clientServicesConfig, _config.d2ServicePath, _config.useNewEphemeralStoreWatcher, _config._healthCheckOperations, _config._executorService, _config.retry, _config.retryLimit);
    final LoadBalancerWithFacilities loadBalancer = loadBalancerFactory.create(cfg);
    D2Client d2Client = new DynamicClient(loadBalancer, loadBalancer, _restOverStream);
    if (_config.retry) {
        d2Client = new RetryClient(d2Client, _config.retryLimit);
    }
    /**
     * If we created default transport client factories, we need to shut them down when d2Client
     * is being shut down.
     */
    if (_config.clientFactories != transportClientFactories) {
        d2Client = new TransportClientFactoryAwareD2Client(d2Client, transportClientFactories.values());
    }
    return d2Client;
}
Also used : DynamicClient(com.linkedin.d2.balancer.clients.DynamicClient) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) RetryClient(com.linkedin.d2.balancer.clients.RetryClient)

Example 9 with D2Client

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

the class EmailClientExample method main.

public static void main(String[] args) throws Exception {
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Long> trafficProportion = (Map<String, Long>) json.get("trafficProportion");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    System.out.println("Finished parsing client config");
    //create d2 client
    final D2Client d2Client = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath).build();
    System.out.println("Finished creating d2 client, starting d2 client...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    //start d2 client by connecting to zookeeper
    startClient(d2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 client is sending traffic to both " + "partition 0 and partition1.");
    System.out.println("Note that traffic for server1 will be 22x more than server2");
    ScheduledFuture task = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, d2Client);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to shutdown");
    System.out.println("===========================================================\n\n");
    System.in.read();
    task.cancel(false);
    System.out.println("Shutting down...");
    shutdown(d2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) None(com.linkedin.common.util.None)

Example 10 with D2Client

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

the class ProfileClientExample method main.

public static void main(String[] args) throws Exception {
    //get client configuration
    JSONObject json = parseConfig();
    String zkConnectString = (String) json.get("zkConnectString");
    Long zkSessionTimeout = (Long) json.get("zkSessionTimeout");
    String zkBasePath = (String) json.get("zkBasePath");
    Long zkStartupTimeout = (Long) json.get("zkStartupTimeout");
    Long zkLoadBalancerNotificationTimeout = (Long) json.get("zkLoadBalancerNotificationTimeout");
    String zkFlagFile = (String) json.get("zkFlagFile");
    String fsBasePath = (String) json.get("fsBasePath");
    final Map<String, Map<String, Long>> trafficProportion = (Map<String, Map<String, Long>>) json.get("trafficProportion");
    final Long clientShutdownTimeout = (Long) json.get("clientShutdownTimeout");
    final Long clientStartTimeout = (Long) json.get("clientStartTimeout");
    System.out.println("Finished parsing client config");
    //create d2 client
    final D2Client d2Client = new D2ClientBuilder().setZkHosts(zkConnectString).setZkSessionTimeout(zkSessionTimeout, TimeUnit.MILLISECONDS).setZkStartupTimeout(zkStartupTimeout, TimeUnit.MILLISECONDS).setLbWaitTimeout(zkLoadBalancerNotificationTimeout, TimeUnit.MILLISECONDS).setFlagFile(zkFlagFile).setBasePath(zkBasePath).setFsBasePath(fsBasePath).build();
    System.out.println("Finished creating d2 client, starting d2 client...");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    //start d2 client by connecting to zookeeper
    startClient(d2Client, executorService, clientStartTimeout, new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            System.exit(1);
        }

        @Override
        public void onSuccess(None result) {
            latch.countDown();
        }
    });
    latch.await();
    System.out.println("D2 client is sending traffic.");
    System.out.println("Note that traffic for 'member' will go mostly to ProfileService 1,3,5 servers.");
    System.out.println("Because we make ProfileService 2,4,6 servers respond slowly \n");
    ScheduledFuture task = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, d2Client, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    System.out.println("Press enter to restore the health of all the servers\n\n\n");
    System.out.println("After this line you will see d2 client will start logging warning" + " message because server 2,4,6's latencies are higher than" + "the threshold (high water mark).");
    System.out.println("===========================================================");
    System.in.read();
    task.cancel(false);
    task = executorService.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {
            try {
                sendTraffic(trafficProportion, d2Client, 0l);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1000, TimeUnit.MILLISECONDS);
    System.out.println("=========================================================\n\n\n");
    System.out.println("Now all servers are healthy. Traffic for 'member' " + "will be balanced between profile service 1,2,3,4,5,6.");
    System.out.println("Press enter to shut down\n\n");
    System.in.read();
    task.cancel(false);
    System.out.println("Shutting down...");
    shutdown(d2Client, executorService, clientShutdownTimeout);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) D2Client(com.linkedin.d2.balancer.D2Client) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ScheduledFuture(java.util.concurrent.ScheduledFuture) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) None(com.linkedin.common.util.None)

Aggregations

None (com.linkedin.common.util.None)10 D2Client (com.linkedin.d2.balancer.D2Client)9 D2ClientBuilder (com.linkedin.d2.balancer.D2ClientBuilder)9 Map (java.util.Map)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)5 JSONObject (org.json.simple.JSONObject)5 IOException (java.io.IOException)4 ParseException (org.json.simple.parser.ParseException)4 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Test (org.testng.annotations.Test)3 URISyntaxException (java.net.URISyntaxException)2 Callback (com.linkedin.common.callback.Callback)1 FutureCallback (com.linkedin.common.callback.FutureCallback)1 DynamicClient (com.linkedin.d2.balancer.clients.DynamicClient)1