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;
}
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;
}
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");
}
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");
}
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"));
}
Aggregations