Search in sources :

Example 6 with DynamicClient

use of com.linkedin.d2.balancer.clients.DynamicClient 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 7 with DynamicClient

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

the class LoadBalancerClientCli method getSchema.

public String getSchema(ZKConnection zkclient, String zkserver, String d2path, String cluster, String service, String requestType) throws URISyntaxException, InterruptedException, ExecutionException, IOException, PropertyStoreException, TimeoutException {
    String responseString = null;
    if (hasService(zkclient, zkserver, d2path, cluster, service)) {
        DynamicClient client = new DynamicClient(getLoadBalancer(zkclient, zkserver, d2path, service), null);
        URI uri = URI.create("d2://" + service + "/");
        try {
            RestRequest restRequest = new RestRequestBuilder(uri).setEntity("".getBytes("UTF-8")).build();
            Future<RestResponse> response = client.restRequest(restRequest, new RequestContext());
            responseString = response.get().getEntity().asString("UTF-8");
        } finally {
            LoadBalancerUtil.syncShutdownClient(_client, _log);
            zkclient.shutdown();
        }
    } else {
        System.out.println("Service '" + service + "' is not defined for cluster '" + cluster + "'.");
    }
    return responseString;
}
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) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI)

Example 8 with DynamicClient

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

the class DynamicClientTest method testClient.

@Test(groups = { "small", "back-end" }, dataProvider = "restOverStreamSwitch")
@SuppressWarnings("deprecation")
public void testClient(boolean restOverStream) throws URISyntaxException {
    TestLoadBalancer balancer = new TestLoadBalancer(false);
    DirectoryProvider dirProvider = new TestDirectoryProvider();
    KeyMapperProvider keyMapperProvider = new TestKeyMapperProvider();
    ClientFactoryProvider clientFactoryProvider = new TestClientFactoryProvider();
    Facilities facilities = new DelegatingFacilities(dirProvider, keyMapperProvider, clientFactoryProvider);
    DynamicClient client = new DynamicClient(balancer, facilities, restOverStream);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TestCallback<RestResponse> restCallback = new TestCallback<RestResponse>();
    client.restRequest(restRequest, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
    Facilities myFacilities = client.getFacilities();
    assertNotNull(facilities, "facilities should not be null");
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) KeyMapperProvider(com.linkedin.d2.balancer.util.KeyMapperProvider) ClientFactoryProvider(com.linkedin.d2.balancer.util.ClientFactoryProvider) TestCallback(com.linkedin.d2.balancer.clients.TrackerClientTest.TestCallback) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) RestRequest(com.linkedin.r2.message.rest.RestRequest) DirectoryProvider(com.linkedin.d2.balancer.util.DirectoryProvider) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Example 9 with DynamicClient

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

the class DynamicClientTest method testUnavailable.

@Test(groups = { "small", "back-end" }, dataProvider = "restOverStreamSwitch")
public void testUnavailable(boolean restOverStream) throws URISyntaxException {
    TestLoadBalancer balancer = new TestLoadBalancer(true);
    DynamicClient client = new DynamicClient(balancer, null, restOverStream);
    URI uri = URI.create("d2://test");
    RestRequest restRequest = new RestRequestBuilder(uri).build();
    TestCallback<RestResponse> restCallback = new TestCallback<RestResponse>();
    client.restRequest(restRequest, restCallback);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e instanceof ServiceUnavailableException);
    assertNull(restCallback.t);
    Facilities facilities = client.getFacilities();
    assertNull(facilities, "facilities should be null");
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) TestCallback(com.linkedin.d2.balancer.clients.TrackerClientTest.TestCallback) URI(java.net.URI) Facilities(com.linkedin.d2.balancer.Facilities) DelegatingFacilities(com.linkedin.d2.balancer.util.DelegatingFacilities) Test(org.testng.annotations.Test)

Example 10 with DynamicClient

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

the class RetryClientTest method testRestException.

@Test
public void testRestException() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/bad"));
    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>();
    RequestContext context = new RequestContext();
    KeyMapper.TargetHostHints.setRequestContextTargetHost(context, URI.create("http://test.linkedin.com/bad"));
    client.restRequest(restRequest, context, restCallback);
    assertNull(restCallback.t);
    assertNotNull(restCallback.e);
    assertTrue(restCallback.e.getMessage().contains("exception happens"));
}
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) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

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